id = $id; $result->displayName = $displayName; $result->description = $description; $result->color = $color; $result->icon = $icon; $result->topics = $topics; return $result; } /** * Prüft, ob das Thema zu den angegebenen IDs existiert * @param string $subjectId ID des Faches * @return bool true, wenn es existiert, sonst false */ public static function exists(string $subjectId): bool { if(!is_dir(Config::getSubjectDirectory($subjectId))) { return false; } return true; } /** * Gibt alle Fächer als SubjectData Objekt zurück * @return array Alle Fächer als SubjectData */ public static function getAll(): array { $result = array(); $subjectNames = scandir(Config::getSubjectsDirectory()); usort($subjectNames, function ($a, $b) { return strcmp($a, $b); }); foreach ($subjectNames as $subjectName) { if ($subjectName == "." || $subjectName == "..") { continue; } $subjectData = SubjectData::fromName($subjectName); if (!isset($subjectData)) { continue; } $result[$subjectData->id] = $subjectData; } return $result; } /** * Lädt ein Fach über eine gegebene ID * @param $subjectId string Die eindeutige ID des Faches * @return SubjectData|null Das Fach zu der ID oder null, wenn kein entsprechendes Fach gefunden wurde */ public static function fromName(string $subjectId): SubjectData|null { $result = new SubjectData(); if (Util::containsIllegalCharacters($subjectId)) { return null; } $result->id = $subjectId; $filename = Config::getSubjectDirectory($subjectId) . "properties.json"; $data = Util::parseJsonFromFile($filename); if (!isset($data)) { return null; } if (!isset($data->displayName)) { return null; } $result->displayName = $data->displayName; if (!isset($data->description)) { return null; } $result->description = $data->description; if (!isset($data->color)) { return null; } $result->color = $data->color; if (!isset($data->icon)) { return null; } $result->icon = $data->icon; $result->topics = TopicData::getAll($subjectId); return $result; } /** * Schreibt alle Daten in Dateien * @return bool true, wenn erfolgreich, sonst false */ public function save(): bool { $subjectDirectory = Config::getSubjectDirectory($this->getId()); $topicsDirectory = Config::getTopicsDirectory($this->getId()); // Create directories if they don't exist if (!is_dir($subjectDirectory)) { if (!mkdir($subjectDirectory, 0777, true)) { return false; } } if (!is_dir($topicsDirectory)) { if (!mkdir($topicsDirectory, 0777, true)) { return false; } } $data = array(); $data["displayName"] = $this->displayName; $data["description"] = $this->description; $data["color"] = $this->color; $data["icon"] = $this->icon; $json = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); if (!$json) { return false; } if (!Util::writeFileContent($subjectDirectory . "properties.json", $json)) { return false; } return true; } /** * Löscht das Fach inklusive aller zugehörigen Themen * @return bool true, wenn erfolgreich, sonst false */ public function delete(): bool { if (!Util::delete(Config::getSubjectDirectory($this->getId()))) { return false; } return true; } /** * Fügt ein neues Thema zum Fach hinzu * @param TopicData $topic Das neue Thema * @return bool true, wenn erfolgreich, sonst false */ public function addTopic(TopicData $topic): bool { if(isset($this->topics[$topic->getId()])) { return false; } $this->topics[] = $topic; return true; } /** * Entfernt ein Thema vom Fach * @param TopicData $topic Das zu entfernende Thema * @return bool true, wenn erfolgreich, sonst false */ public function removeTopic(TopicData $topic): bool { if(!isset($this->topics[$topic->getId()])) { return false; } $this->topics = array_diff($this->topics, [$topic]); return true; } public function getId(): string { return $this->id; } public function setId(string $id): void { $this->id = $id; } public function getDisplayName(): string { return $this->displayName; } public function setDisplayName(string $displayName): void { $this->displayName = $displayName; } public function getDescription(): string { return $this->description; } public function setDescription(string $description): void { $this->description = $description; } public function getColor(): string { return $this->color; } public function setColor(string $color): void { $this->color = $color; } public function getIcon(): string { return $this->icon; } public function setIcon(string $icon): void { $this->icon = $icon; } public function getTopics(): array { return $this->topics; } public function setTopics(array $topics): void { $this->topics = $topics; } }