From ff92b4c8752ed4869308d2166479c0d932e48a99 Mon Sep 17 00:00:00 2001 From: Matthias Grief Date: Mon, 9 Dec 2024 12:07:17 +0100 Subject: [PATCH] Konstruktoren zum erstellen von neuen Objekten eingebaut --- webseite/classes/SubjectData.php | 45 +++++++++-------- webseite/classes/TopicData.php | 87 ++++++++++++++++++-------------- 2 files changed, 75 insertions(+), 57 deletions(-) diff --git a/webseite/classes/SubjectData.php b/webseite/classes/SubjectData.php index 5dc2d7f..487326c 100644 --- a/webseite/classes/SubjectData.php +++ b/webseite/classes/SubjectData.php @@ -40,6 +40,25 @@ class SubjectData */ public array $topics; + /** + * Erstellt ein neues Fach. Es wird noch nichts gespeichert + * @param string $id Ein eindeutiger Bezeichner für das Fach, darf nur A-Z, a-Z, 0-9 sowie _ und - enthalten + * @param string $displayName Der für User angezeigte Name des Faches, nur reiner Text + * @param string $description Eine kurze Beschreibung des Faches, z.B. für den Text auf der Startseite, kann HTML enthalten + * @param string $color Themenfarbe des Faches als hexcode + * @param string $icon Icon des Faches als Font-Awesome CSS-Klasse + * @param array $topics Alle Themen des Faches als TopicData Objekt + */ + public function __construct(string $id, string $displayName, string $description, string $color, string $icon, array $topics) + { + $this->id = $id; + $this->displayName = $displayName; + $this->description = $description; + $this->color = $color; + $this->icon = $icon; + $this->topics = $topics; + } + /** * Gibt alle Fächer als SubjectData Objekt zurück * @return array Alle Fächer als SubjectData @@ -77,8 +96,6 @@ class SubjectData */ public static function fromName(string $subjectId): SubjectData|null { - $result = new SubjectData(); - if (Util::containsIllegalCharacters($subjectId)) { return null; } @@ -89,35 +106,23 @@ class SubjectData return null; } - $result->id = $subjectId; - - if (isset($data->displayName)) { - $result->displayName = $data->displayName; - } else { + if (!isset($data->displayName)) { return null; } - if (isset($data->description)) { - $result->description = $data->description; - } else { + if (!isset($data->description)) { return null; } - if (isset($data->color)) { - $result->color = $data->color; - } else { + if (!isset($data->color)) { return null; } - if (isset($data->icon)) { - $result->icon = $data->icon; - } else { + if (!isset($data->icon)) { return null; } - $result->topics = TopicData::getAll($subjectId); - - return $result; + return new SubjectData($subjectId, $data->displayName, $data->description, $data->color, $data->icon, TopicData::getAll($subjectId)); } /** @@ -155,7 +160,7 @@ class SubjectData */ public function delete(): bool { - if(!Util::delete(Config::getSubjectDirectory($this->getId()))) { + if (!Util::delete(Config::getSubjectDirectory($this->getId()))) { return false; } diff --git a/webseite/classes/TopicData.php b/webseite/classes/TopicData.php index c4bb0c5..b13d48f 100644 --- a/webseite/classes/TopicData.php +++ b/webseite/classes/TopicData.php @@ -44,6 +44,29 @@ class TopicData */ public string $article; + /** + * Erstellt ein neues Thema. Es wird noch nichts gespeichert + * @param string $id Innerhalb des zugehörigen Faches eindeutige ID, darf nur A-Z, a-z, 0-9 sowie - und _ enthalten + * @param string $subjectId Die eindeutige ID des zugehörigen Faches, das Fach muss schon existieren + * @param string $displayName Der für User angezeigt Name des Themas, darf nur reinen Text enthalten + * @param string $icon Das Icon des Themas als Font-Awesome CSS-Klasse + * @param string $description Eine kurze Beschreibung des Themas, z.B. für die Fachübersichtsseite, darf HTML enthalten + * @param array $relatedTopics Die IDs aller verwandten Themen als String + * @param array $files Die Dateinamen (Datei.pdf) aller downloadbarer Dateien zu diesem Thema als String + * @param string $article Der gesamte Erklärungstext zum Thema, enthält fertiges HTML und LATEX Formelsyntax für MathJax https://docs.mathjax.org/en/latest/basic/mathematics.html + */ + public function __construct(string $id, string $subjectId, string $displayName, string $icon, string $description, array $relatedTopics, array $files, string $article) + { + $this->id = $id; + $this->subjectId = $subjectId; + $this->displayName = $displayName; + $this->icon = $icon; + $this->description = $description; + $this->relatedTopics = $relatedTopics; + $this->files = $files; + $this->article = $article; + } + /** * Gibt alle Themen zu einem gegebenen Fach zurück * @param $subjectId string Die ID des Faches @@ -82,53 +105,45 @@ class TopicData */ public static function fromName(string $subjectId, string $topicId): TopicData|null { - $result = new TopicData(); - - $subjectId = Util::removeIllegalCharacters($subjectId); - $topicId = Util::removeIllegalCharacters($topicId); + if (Util::containsIllegalCharacters($subjectId)) { + return null; + } + if (Util::containsIllegalCharacters($topicId)) { + return null; + } $data = Util::parseJsonFromFile(Config::getTopicDirectory($subjectId, $topicId) . "properties.json"); if (!isset($data)) { return null; } - $result->id = $topicId; - $result->subjectId = $subjectId; - - if (isset($data->displayName)) { - $result->displayName = $data->displayName; - } else { + if (!isset($data->displayName)) { return null; } - if (isset($data->icon)) { - $result->icon = $data->icon; - } else { + if (!isset($data->icon)) { return null; } - if (isset($data->description)) { - $result->description = $data->description; - } else { + if (!isset($data->description)) { return null; } + $relatedTopics = array(); if (isset($data->relatedTopics)) { - $result->relatedTopics = $data->relatedTopics; - } else { - $result->relatedTopics = array(); + $relatedTopics = $data->relatedTopics; } - $result->files = array(); + $files = array(); $downloadDirectory = Config::getTopicDirectory($subjectId, $topicId) . "downloads/"; - if(is_dir($downloadDirectory)) { + if (is_dir($downloadDirectory)) { $fileNames = scandir($downloadDirectory); foreach ($fileNames as $fileName) { if ($fileName == "." || $fileName == "..") { continue; } - $result->files[] = $fileName; + $files[] = $fileName; } } @@ -138,9 +153,7 @@ class TopicData } $article = str_replace('$TOPICPATH', Config::getTopicDirectory($subjectId, $topicId) . "images", $article); - $result->article = $article; - - return $result; + return new TopicData($topicId, $subjectId, $data->displayName, $data->icon, $data->description, $relatedTopics, $files, $article); } /** @@ -187,15 +200,15 @@ class TopicData */ public function addDownload(string $name, string $tmp_name): bool { - $downloadDirectory = Config::getTopicDirectory($this->getSubjectId() , $this->getId()) . "downloads/"; + $downloadDirectory = Config::getTopicDirectory($this->getSubjectId(), $this->getId()) . "downloads/"; - if(!is_dir($downloadDirectory)) { - if(!mkdir($downloadDirectory)) { + if (!is_dir($downloadDirectory)) { + if (!mkdir($downloadDirectory)) { return false; } } - if(!move_uploaded_file($tmp_name, $downloadDirectory . $name)) { + if (!move_uploaded_file($tmp_name, $downloadDirectory . $name)) { return false; } @@ -211,11 +224,11 @@ class TopicData */ public function deleteDownload(string $name): bool { - if(!isset($this->files[$name])) { + if (!isset($this->files[$name])) { return false; } - if(!unlink(Config::getTopicDirectory($this->getSubjectId() , $this->getId()) . "downloads/$name")) { + if (!unlink(Config::getTopicDirectory($this->getSubjectId(), $this->getId()) . "downloads/$name")) { return false; } @@ -232,15 +245,15 @@ class TopicData */ public function addImage(string $name, string $tmp_name): bool { - $imageDirectory = Config::getTopicDirectory($this->getSubjectId() , $this->getId()) . "images/"; + $imageDirectory = Config::getTopicDirectory($this->getSubjectId(), $this->getId()) . "images/"; - if(!is_dir($imageDirectory)) { - if(!mkdir($imageDirectory)) { + if (!is_dir($imageDirectory)) { + if (!mkdir($imageDirectory)) { return false; } } - if(!move_uploaded_file($tmp_name, $imageDirectory . $name)) { + if (!move_uploaded_file($tmp_name, $imageDirectory . $name)) { return false; } @@ -254,7 +267,7 @@ class TopicData */ public function deleteImage(string $name): bool { - if(!unlink(Config::getTopicDirectory($this->getSubjectId() , $this->getId()) . "images/$name")) { + if (!unlink(Config::getTopicDirectory($this->getSubjectId(), $this->getId()) . "images/$name")) { return false; } @@ -267,7 +280,7 @@ class TopicData */ public function delete(): bool { - if(!Util::delete(Config::getTopicDirectory($this->getSubjectId(), $this->getId()))) { + if (!Util::delete(Config::getTopicDirectory($this->getSubjectId(), $this->getId()))) { return false; }