ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
PersonalSettingsExporter.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\FileDelivery\Services as FileDeliveryServices;
30
32{
33 private int $template_id;
35
36 public function __construct(
37 private readonly FileDeliveryServices $file_delivery,
38 private readonly PersonalSettingsRepository $repository,
39 private readonly MainSettingsRepository $main_settings_repository,
40 private readonly ScoreSettingsRepository $score_settings_repository,
41 private readonly MarksRepository $marks_repository
42 ) {
43 }
44
45 public function setTemplateId(int $template_id): void
46 {
47 $this->template_id = $template_id;
48 $this->template = null;
49 }
50
52 {
53 return $this->template ??= $this->repository->getById($this->template_id);
54 }
55
56 public function deliver(): void
57 {
58 if (($xml_content = $this->write()) === null) {
59 return;
60 }
61
62 $this->file_delivery->delivery()->attached(
63 Streams::ofString($xml_content),
64 "{$this->escapeName($this->getTemplate()->getName())}.xml",
65 'text/xml',
66 );
67 }
68
69 public function write(): ?string
70 {
71 if (($template = $this->getTemplate()) === null) {
72 return null;
73 }
74
75 $main_settings = $this->main_settings_repository->getById($template->getSettingsId());
76 $score_settings = $this->score_settings_repository->getById($template->getSettingsId());
77 $mark_schema = $this->marks_repository->getMarkSchemaBySteps(
78 $this->repository->lookupMarkSteps($template->getId())
79 );
80
81
82 $xml_writer = new \XMLWriter();
83 $xml_writer->openMemory();
84
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);
89
90 $xml_writer->startElement('template');
91 $xml_writer->writeAttribute('ilias-version', ILIAS_VERSION_NUMERIC);
92 foreach ($template->toExport() as $name => $value) {
93 $xml_writer->writeAttribute(str_replace('_', '-', $name), (string) $value);
94 }
95
96 $xml_writer->startElement('main-settings');
97 $this->writeRecursive($xml_writer, $main_settings->toExport(), ['settings-group', 'settings-entry']);
98 $xml_writer->endElement();
99
100 $xml_writer->startElement('score-settings');
101 $this->writeRecursive($xml_writer, $score_settings->toExport(), ['settings-group', 'settings-entry']);
102 $xml_writer->endElement();
103
104 $xml_writer->startElement('mark-schema');
105 $this->writeRecursive($xml_writer, $mark_schema->toExport(), ['mark-steps', 'mark']);
106 $xml_writer->endElement();
107
108 $xml_writer->endElement();
109 $xml_writer->endDocument();
110
111 return $xml_writer->outputMemory(true);
112 }
113
120 private function writeRecursive(\XMLWriter $xml_writer, array $values, array $elements = []): void
121 {
122 $element = array_shift($elements) ?? 'entry';
123
124 foreach ($values as $name => $value) {
125 $type = gettype($value);
126 $is_nested = is_array($value);
127
128 $xml_writer->startElement($element);
129 if (!$is_nested) {
130 $xml_writer->writeAttribute('type', $type);
131 }
132 if (is_string($name)) {
133 $xml_writer->writeAttribute('name', $name);
134 }
135
136 if (!$is_nested) {
137 $value = match ($type) {
138 'NULL' => 'NULL',
139 'boolean' => $value ? 'true' : 'false',
140 default => htmlspecialchars((string) $value),
141 };
142
143 $xml_writer->writeRaw($value);
144 } else {
145 $this->writeRecursive($xml_writer, $value, $elements);
146 }
147
148 $xml_writer->endElement();
149 }
150 }
151
152 private function escapeName(string $name): string
153 {
154 // Replace all special characters except "_" from the string for safe filename usage
155 return preg_replace('/[\W_]/', '-', $name);
156 }
157}
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
static ofString(string $string)
Creates a new stream with an initial value.
Definition: Streams.php:41
writeRecursive(\XMLWriter $xml_writer, array $values, array $elements=[])
Writes the provided array as a nested tree of elements to the XML writer.
__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 IL_INST_ID
Definition: constants.php:40
$file_delivery
Definition: deliver.php:29
const ILIAS_VERSION_NUMERIC
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...