Konstruktoren zum erstellen von neuen Objekten eingebaut

This commit is contained in:
Matthias Grief
2024-12-09 12:07:17 +01:00
committed by Eric Blommel
parent b81068b701
commit ff92b4c875
2 changed files with 75 additions and 57 deletions

View File

@@ -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;
}

View File

@@ -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;
}