Files
SWE/webseite/classes/TopicData.php

139 lines
3.9 KiB
PHP

<?php
require_once("Util.php");
class TopicData
{
/**
* @var string Innerhalb des zugehörigen Faches eindeutige ID, darf nur A-Z, a-z, 0-9 sowie - und _ enthalten
*/
public string $id;
/**
* @var string Die eindeutige ID des zugehörigen Faches
*/
public string $subjectId;
/**
* @var string Der für User angezeigt Name des Themas, darf nur reinen Text enthalten
*/
public string $displayName;
/**
* @var string Das Icon des Themas als Font-Awesome CSS-Klasse
*/
public string $icon;
/**
* @var string Eine kurze Beschreibung des Themas, z.B. für die Fachübersichtsseite, darf HTML enthalten
*/
public string $description;
/**
* @var array Die IDs aller verwandten Themen als String
*/
public array $relatedTopics;
/**
* @var array Die Dateinamen (Datei.pdf) aller downloadbarer Dateien zu diesem Thema als String
*/
public array $files;
/**
* @var string 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 string $article;
/**
* Gibt alle Themen zu einem gegebenen Fach zurück
* @param $subjectId string Die ID des Faches
* @return array Alle zugehörigen Themen als TopicData Objekte
*/
public static function getAll(string $subjectId): array
{
$result = array();
$topicDirectory = "config/subjects/$subjectId/topics";
$topicNames = scandir($topicDirectory);
foreach ($topicNames as $topicName) {
if ($topicName == "." || $topicName == "..") {
continue;
}
$topicData = TopicData::fromName($subjectId, $topicName);
if (!isset($topicData)) {
continue;
}
$result[$topicData->id] = $topicData;
}
usort($result, function ($a, $b) {
return strcmp($a->displayName, $b->displayName);
});
return $result;
}
/**
* Gibt Themendaten zu einem bestimmten Thema eines Faches zurück
* @param $subjectId string Die ID des Faches
* @param $topicId string Die ID des Themas
* @return TopicData|null Die Themendaten oder null, wenn das Thema nicht existiert
*/
public static function fromName(string $subjectId, string $topicId): TopicData|null
{
$result = new TopicData();
$subjectId = Util::removeIllegalCharacters($subjectId);
$topicId = Util::removeIllegalCharacters($topicId);
$topicsDirectory = "config/subjects/$subjectId/topics";
$topicDataDirectory = "$topicsDirectory/$topicId";
$data = Util::parseJsonFromFile("$topicDataDirectory/properties.json");
if(!isset($data)) {
return null;
}
$result->id = $topicId;
$result->subjectId = $subjectId;
if (isset($data->displayName)) {
$result->displayName = $data->displayName;
} else {
return null;
}
if (isset($data->icon)) {
$result->icon = $data->icon;
} else {
return null;
}
if (isset($data->description)) {
$result->description = $data->description;
} else {
return null;
}
if (isset($data->relatedTopics)) {
$result->relatedTopics = $data->relatedTopics;
} else {
$result->relatedTopics = array();
}
if (isset($data->files)) {
$result->files = $data->files;
} else {
$result->files = array();
}
$article = Util::readFileContent("$topicDataDirectory/article.html");
if (!isset($article)) {
$article = "Kein Erklärtext vorhanden";
}
$article = str_replace('$TOPICPATH', $topicDataDirectory, $article);
$result->article = $article;
return $result;
}
}