Files
SWE/webseite/classes/SubjectData.php
2024-11-19 22:24:37 +01:00

121 lines
3.0 KiB
PHP

<?php
require_once("Util.php");
require_once("TopicData.php");
/**
* Stellt alle relevanten Daten für ein einzeles Fach bereit
*
*/
class SubjectData
{
/**
* @var string Ein eindeutiger Bezeichner für das Fach, darf nur A-Z, a-Z, 0-9 sowie _ und - enthalten
*/
public string $id;
/**
* @var string Der für User angezeigte Name des Faches, nur reiner Text
*/
public string $displayName;
/**
* @var string Eine kurze Beschreibung des Faches, z.B. für den Text auf der Startseite, kann HTML enthalten
*/
public string $description;
/**
* @var string Themenfarbe des Faches, entweder als hexcode oder CSS-Klasse, noch nicht spezifiziert
*/
public string $color;
/**
* @var string Icon des Faches als Font-Awesome CSS-Klasse
*/
public string $icon;
/**
* @var array Alle Themen des Faches als TopicData Objekt
* @see TopicData
*/
public array $topics;
/**
* Gibt alle Fächer als SubjectData Objekt zurück
* @return array Alle Fächer als SubjectData
*/
public static function getAll(): array
{
$result = array();
$subjectDirectory = "config/subjects";
$subjectNames = scandir($subjectDirectory);
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();
$subjectId = Util::removeIllegalCharacters($subjectId);
$subjectDirectory = "config/subjects/$subjectId";
$filename = "$subjectDirectory/properties.json";
$data = Util::parseJsonFromFile($filename);
if (!isset($data)) {
return null;
}
$result->id = $subjectId;
if (isset($data->displayName)) {
$result->displayName = $data->displayName;
} else {
return null;
}
if (isset($data->description)) {
$result->description = $data->description;
} else {
return null;
}
if (isset($data->color)) {
$result->color = $data->color;
} else {
return null;
}
if (isset($data->icon)) {
$result->icon = $data->icon;
} else {
return null;
}
$result->topics = TopicData::getAll($subjectId);
return $result;
}
}