isLoggedIn()) { header("Location: index.php"); die(); } $allSubjects = SubjectData::getAll(); $editingTopic = null; $defaultValues = array(); $defaultValues['displayName'] = ""; $defaultValues['id'] = ""; $defaultValues['subjectId'] = ""; $defaultValues['description'] = ""; $defaultValues['icon'] = "fa-book"; $defaultValues['relatedTopics'] = ""; $defaultValues['existing_files'] = ""; $defaultValues['formulas'] = ""; $defaultValues['article'] = ""; $errors = array(); if (isset($_GET['subject']) && isset($_GET['topic'])) { if (isset($allSubjects[$_GET['subject']]->getTopics()[$_GET['topic']])) { $editingTopic = $allSubjects[$_GET['subject']]->getTopics()[$_GET['topic']]; $defaultValues['displayName'] = $editingTopic->getDisplayName(); $defaultValues['id'] = $editingTopic->getId(); $defaultValues['subjectId'] = $editingTopic->getSubjectId(); $defaultValues['description'] = $editingTopic->getDescription(); $defaultValues['icon'] = $editingTopic->getIcon(); $defaultValues['relatedTopics'] = implode(", ", $editingTopic->getRelatedTopics()); $defaultValues['existing_files'] = implode(", ", $editingTopic->getFiles()); $tasks = array(); foreach($editingTopic->getTasks() as $task) { $variables = array(); foreach($task->getVariables() as $name => $variable) { $variables[] = $name . "::" . $variable; } $tasks[] = $task->getText() . ";;" . implode(";;", $variables); } $defaultValues['formulas'] = implode(";;;;", $tasks); $defaultValues['article'] = $editingTopic->getFinishedArticle(); } } else if (isset($_GET['subject'])) { $defaultValues['subjectId'] = $_GET['subject']; } if ($_SERVER['REQUEST_METHOD'] == 'POST') { foreach ($defaultValues as $key => $value) { $defaultValues[$key] = $_POST[$key]; } if (!isset($_POST['displayName']) || trim($_POST['displayName']) == "") { $errors["displayName"] = "Bitte geben Sie einen Namen an."; } if (!isset($_POST['id']) || trim($_POST['id']) == "") { $errors["id"] = "Bitte geben Sie eine ID an."; } if (!isset($_POST['subjectId']) || trim($_POST['subjectId']) == "") { $errors["subjectId"] = "Bitte geben Sie ein Fach an."; } if (!isset($_POST['description']) || trim($_POST['description']) == "") { $errors["description"] = "Bitte geben Sie eine Beschreibung an."; } if (!isset($_POST['icon']) || trim($_POST['icon']) == "") { $errors["icon"] = "Bitte geben Sie ein Icon an."; } if (!isset($_POST['relatedTopics'])) { $errors["relatedTopics"] = "Feld relatedTopics nicht mitgesendet!"; } if (!isset($_POST['existing_files'])) { $errors["existing_files"] = "Feld existing_files nicht mitgesendet!"; } if (!isset($_POST['formulas'])) { $errors["formulas"] = "Feld formulas nicht mitgesendet!"; } if (!isset($_POST['article']) || trim($_POST['article']) == "") { $errors["article"] = "Bitte geben Sie einen Erklärtext an."; } if (!isset($_POST['submit']) || !($_POST['submit'] == "Speichern" || $_POST['submit'] == "Thema löschen")) { $errors["submit"] = "Ungültig abgeschickt!"; } if (empty($errors)) { $newTopic = false; $relatedTopics = array(); foreach (explode(",", $_POST['relatedTopics']) as $relatedTopic) { $relatedTopic = trim($relatedTopic); if ($relatedTopic == "") { continue; } $relatedTopics[] = $relatedTopic; } $existingFiles = array(); foreach (explode(",", $_POST['existing_files']) as $existingTopic) { $existingTopic = trim($existingTopic); if ($existingTopic == "") { continue; } $existingFiles[] = trim($existingTopic); } $formulas = array(); foreach (explode(";;;;", $_POST['formulas']) as $formulaString) { $text = false; $answers = array(); foreach (explode(";;", $formulaString) as $formulaEntry) { if(!$text) { $text = trim($formulaEntry); continue; } $answer = explode("::", $formulaEntry); if(count($answer) != 2) { $errors['formulas'] = "Jede Formel muss einen Text und mindestens eine Antwortmöglichkeit haben!"; break; } $answers[trim($answer[0])] = trim($answer[1]); } if(!$text) { $errors['formulas'] = "Jede Formel muss einen Text haben!"; break; } if(count($answers) == 0) { $errors['formulas'] = "Jede Formel muss mindestens eine Antwortmöglichkeit haben!"; break; } $formulas[] = new Task($text, $answers); } $article = mb_convert_encoding($_POST['article'], 'HTML-ENTITIES', 'UTF-8'); $dom = new DOMDocument(); $dom->loadHTML($article, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $htmlImages = $dom->getElementsByTagName('img'); $newImages = array(); foreach ($htmlImages as $htmlImage) { $src = $htmlImage->getAttribute('src'); if (str_starts_with($src, "data:image")) { $image = file_get_contents($src); if (!$image) { continue; } $image_format = false; if (str_starts_with($src, 'data:image/jpeg;')) { $image_format = "jpg"; } else if (str_starts_with($src, 'data:image/png;')) { $image_format = "png"; } if (!$image_format) { continue; } $imagename = uniqid("image_", true) . ".$image_format"; $newImages[$imagename] = $image; } else { $imagePath = explode("/", $src); $imagename = end($imagePath); } $htmlImage->setAttribute("src", '__TOPICPATH__/' . $imagename); } // extension=mbstring in php.ini muss aktiviert sein! $article = mb_convert_encoding($dom->saveHTML(), 'UTF-8', 'HTML-ENTITIES'); $article = preg_replace("/^[ \r\n\t]*

/", "

", $article); $article = preg_replace("#

[ \r\n\t]*$#", "

", $article); if (isset($allSubjects[$_POST['oldSubjectId']]->getTopics()[$_POST['oldTopicId']])) { $newTopic = $allSubjects[$_POST['oldSubjectId']]->getTopics()[$_POST['oldTopicId']]; $newTopic->setId($_POST['id']); $newTopic->setDisplayName($_POST['displayName']); $newTopic->setSubjectId($_POST['subjectId']); $newTopic->setDescription($_POST['description']); $newTopic->setIcon($_POST['icon']); $newTopic->setRelatedTopics($relatedTopics); foreach ($newTopic->getFiles() as $file) { if (!in_array($file, $existingFiles)) { $newTopic->deleteDownload($file); } } $newTopic->setArticle($article); $newTopic->removeAllTasks(); } else { $newTopic = TopicData::createNew($_POST['id'], $_POST['subjectId'], $_POST['displayName'], $_POST['icon'], $_POST['description'], $relatedTopics, $article); } if (!$newTopic) { $errors["error"] = "Fehler beim Speichern des Themas."; } else { for ($i = 0; $i < count($_FILES['new_files']['name']); $i++) { !$newTopic->addDownload($_FILES['new_files']['name'][$i], $_FILES['new_files']['tmp_name'][$i]); } foreach ($newImages as $name => $image) { $newTopic->uploadImage($name, $image); } foreach ($formulas as $formula) { $newTopic->addTask($formula); } if ($_POST['submit'] == "Thema löschen") { $newTopic->delete(); header("Location: " . "subject.php?subject=" . $_POST['subjectId']); } else { $newTopic->save(); header("Location: " . "topic.php?subject=" . $newTopic->getSubjectId() . "&topic=" . $newTopic->getId()); } } } } ?> Lehrer Dashboard
"; } ?>
Dateien hier ablegen oder klicken, um Dateien auszuwählen