Files
SWE/webseite/classes/TopicData.php
2024-11-06 13:43:44 +01:00

130 lines
3.5 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
*/
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;
}
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);
$topicDirectory = "config/subjects/$subjectId/topics";
$filename = "$topicDirectory/$topicId/properties.json";
$data = Util::parseJsonFromFile($filename);
$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();
}
if(isset($data->article)) {
$result->article = $data->article;
} else {
return null;
}
return $result;
}
}