19declare(strict_types=1);
48 $this->
template =
null;
53 return $this->
template ??= $this->
repository->getById($this->template_id);
58 if (($xml_content = $this->
write()) ===
null) {
62 $this->file_delivery->delivery()->attached(
64 "{$this->escapeName($this->getTemplate()->getName())}.xml",
69 public function write(): ?string
77 $mark_schema = $this->marks_repository->getMarkSchemaBySteps(
82 $xml_writer = new \XMLWriter();
83 $xml_writer->openMemory();
85 $xml_writer->setIndent(
true);
86 $xml_writer->startDocument(
'1.0',
'UTF-8');
87 $xml_writer->writeDTD(
'PTST',
null,
'http://www.ilias.uni-koeln.de/download/dtd/ilias_co.dtd');
88 $xml_writer->writeComment(
"Export of Personal Test Settings Template for installation " .
IL_INST_ID);
90 $xml_writer->startElement(
'template');
93 $xml_writer->writeAttribute(str_replace(
'_',
'-', $name), (
string) $value);
96 $xml_writer->startElement(
'main-settings');
97 $this->
writeRecursive($xml_writer, $main_settings->toExport(), [
'settings-group',
'settings-entry']);
98 $xml_writer->endElement();
100 $xml_writer->startElement(
'score-settings');
101 $this->
writeRecursive($xml_writer, $score_settings->toExport(), [
'settings-group',
'settings-entry']);
102 $xml_writer->endElement();
104 $xml_writer->startElement(
'mark-schema');
105 $this->
writeRecursive($xml_writer, $mark_schema->toExport(), [
'mark-steps',
'mark']);
106 $xml_writer->endElement();
108 $xml_writer->endElement();
109 $xml_writer->endDocument();
111 return $xml_writer->outputMemory(
true);
120 private function writeRecursive(\XMLWriter $xml_writer, array $values, array $elements = []): void
122 $element = array_shift($elements) ??
'entry';
124 foreach ($values as $name => $value) {
125 $type = gettype($value);
126 $is_nested = is_array($value);
128 $xml_writer->startElement($element);
130 $xml_writer->writeAttribute(
'type', $type);
132 if (is_string($name)) {
133 $xml_writer->writeAttribute(
'name', $name);
137 $value = match ($type) {
139 'boolean' => $value ?
'true' :
'false',
140 default => htmlspecialchars((
string) $value),
143 $xml_writer->writeRaw($value);
148 $xml_writer->endElement();
155 return preg_replace(
'/[\W_]/',
'-', $name);
Stream factory which enables the user to create streams without the knowledge of the concrete class.
static ofString(string $string)
Creates a new stream with an initial value.
setTemplateId(int $template_id)
writeRecursive(\XMLWriter $xml_writer, array $values, array $elements=[])
Writes the provided array as a nested tree of elements to the XML writer.
PersonalSettingsTemplate $template
__construct(private readonly FileDeliveryServices $file_delivery, private readonly PersonalSettingsRepository $repository, private readonly MainSettingsRepository $main_settings_repository, private readonly ScoreSettingsRepository $score_settings_repository, private readonly MarksRepository $marks_repository)
toExport()
Transform the object into a simple, associative array.
const ILIAS_VERSION_NUMERIC
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...