ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
class.ilLearningSequenceXMLWriter.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
23
25{
26 public const string TAG_LSO = 'LearningSequence';
27 public const string TAG_LSITEMS = 'LSItems';
28 public const string TAG_LSITEM = 'LSItem';
29 public const string TAG_MEMBERSGALLERY = 'MembersGallery';
30 public const string TAG_CONDITION = 'Condition';
31 public const string TAG_ITEM_CONDITIONS = 'ItemConditions';
32 public const string TAG_ITEM_CONDITION = 'ItemCondition';
33 public const string TAG_LPSETTING = 'LPSetting';
34 public const string TAG_LPREFID = 'LPRefId';
35 public const string TAG_TITLE = 'title';
36 public const string TAG_DESCRIPTION = 'description';
37 public const string TAG_CONTAINERSETTING = 'ContainerSetting';
38
41
42 public function __construct(
43 protected ilObjLearningSequence $ls_object,
44 protected ilSetting $settings,
45 protected ilLPObjSettings $lp_settings
46 ) {
48 $this->ls_settings = $ls_object->getLSSettings();
49 $this->condition_factory = $ls_object->getDI()["conditions.factory"];
50 }
51
52 public function getXml(): string
53 {
54 return $this->xmlDumpMem(false);
55 }
56
57 public function start(): void
58 {
59 $this->writeHeader();
60 $this->writeLearningSequence();
61 $this->writeLSItems();
62 $this->writeFooter();
63 }
64
65 protected function writeHeader(): void
66 {
67 $this->xmlSetDtdDef(
68 "<!DOCTYPE learning sequence PUBLIC \"-//ILIAS//DTD LearningSequence//EN\" \"" .
69 ILIAS_HTTP_PATH . "/xml/ilias_lso_11_0.dtd\">"
70 );
71
72 $this->xmlSetGenCmt(
73 "Export of ILIAS LearningSequence " .
74 $this->ls_object->getId() .
75 " of installation " .
76 $this->settings->get("inst_id") .
77 "."
78 );
79 }
80
81 protected function writeLearningSequence(): void
82 {
83 $boundaries = $this->ls_object->getDI()["db.boundaries"]
84 ->getBoundariesFor($this->ls_object->getId());
85
86 $attributes = [
87 'ref_id' => $this->ls_object->getRefId(),
88 'members_gallery' => $this->ls_settings->getMembersGallery() ? 'true' : 'false',
89 'mode' => $this->ls_settings->getMode(),
90 'start_ref_id' => $boundaries['start_ref_id'],
91 'end_ref_id' => $boundaries['end_ref_id'],
92 ];
93 $this->xmlStartTag(self::TAG_LSO, $attributes);
94
95 $this->xmlElement(self::TAG_TITLE, null, $this->ls_object->getTitle());
96 if ($desc = $this->ls_object->getDescription()) {
97 $this->xmlElement(self::TAG_DESCRIPTION, null, $desc);
98 }
99
100 $this->writeLPSettings();
101 \ilContainer::_exportContainerSettings($this, $this->ls_object->getId());
102 }
103
104 protected function writeLPSettings(): void
105 {
106 $type = $this->lp_settings->getObjType();
107 $mode = $this->lp_settings->getMode();
108 $this->xmlStartTag(
109 self::TAG_LPSETTING,
110 [
111 'type' => $type,
112 'mode' => $mode
113 ]
114 );
116 $this->ls_object->getId(),
117 $mode
118 );
119 if (!is_null($collection)) {
120 $items = $collection->getItems();
121 foreach ($items as $item) {
122 $this->xmlElement(self::TAG_LPREFID, null, $item);
123 }
124 }
125 $this->xmlEndTag(self::TAG_LPSETTING);
126 }
127
128 protected function writeLSItems(): void
129 {
130 $this->xmlStartTag(self::TAG_LSITEMS);
131 $ls_items = $this->ls_object->getLSItems();
132 foreach ($ls_items as $ls_item) {
133 $post_condition = $ls_item->getPostCondition();
134
135 $this->xmlStartTag(
136 self::TAG_LSITEM,
137 [
138 'obj_id' => \ilObject::_lookupObjectId($ls_item->getRefId()),
139 'ref_id' => $ls_item->getRefId(),
140 'position' => $ls_item->getOrderNumber()
141 ]
142 );
143
144 $this->xmlElement(
145 self::TAG_CONDITION,
146 ['type' => $post_condition->getConditionOperator()],
147 $post_condition->getValue()
148 );
149
150 $this->writeConditionsXml($ls_item);
151
152 $this->xmlEndTag(self::TAG_LSITEM);
153 }
154
155 $this->xmlEndTag(self::TAG_LSITEMS);
156 }
157
158 protected function writeConditionsXml($ls_item): void
159 {
160 $conditions = $this->getConditionData($ls_item);
161
162 $this->xmlStartTag(self::TAG_ITEM_CONDITIONS);
163 foreach ($conditions as $condition) {
164 $attrs = [
165 'type' => $condition['type']
166 ];
167
168 if (isset($condition['subtype'])) {
169 $attrs['subtype'] = (string) $condition['subtype'];
170 }
171
172 $this->xmlStartTag(self::TAG_ITEM_CONDITION, $attrs);
173
174 $payload = $condition['data'] ?? [];
175 $this->writeConditionPayloadXml($payload);
176
177 $this->xmlEndTag(self::TAG_ITEM_CONDITION);
178 }
179 $this->xmlEndTag(self::TAG_ITEM_CONDITIONS);
180 }
181
185 protected function writeConditionPayloadXml(array $payload): void
186 {
187 $this->xmlStartTag('Data');
188
189 foreach ($payload as $table_data) {
190 $table_name = (string) ($table_data['table'] ?? '');
191 $this->xmlStartTag('Table', ['name' => $table_name]);
192
193 $fields = is_array($table_data['fields'] ?? null) ? $table_data['fields'] : [];
194 $this->xmlStartTag('Fields');
195 foreach ($fields as $field_name => $definition) {
196 $attrs = ['name' => (string) $field_name];
197 if (is_array($definition)) {
198 foreach ($definition as $meta_key => $meta_value) {
199 $attrs[(string) $meta_key] = is_bool($meta_value)
200 ? ($meta_value ? '1' : '0')
201 : (string) $meta_value;
202 }
203 }
204 $this->xmlElement('Field', $attrs);
205 }
206 $this->xmlEndTag('Fields');
207
208 $rows = is_array($table_data['rows'] ?? null) ? $table_data['rows'] : [];
209 $this->xmlStartTag('Rows');
210 foreach ($rows as $row) {
211 $this->xmlStartTag('Row');
212 if (is_array($row)) {
213 foreach ($row as $col => $value) {
214 $this->xmlElement(
215 'Col',
216 ['name' => (string) $col],
217 $value === null ? '' : (string) $value
218 );
219 }
220 }
221 $this->xmlEndTag('Row');
222 }
223 $this->xmlEndTag('Rows');
224
225 $this->xmlEndTag('Table');
226 }
227
228 $this->xmlEndTag('Data');
229 }
230
231 protected function getConditionData($ls_item): array
232 {
233 $all_conditions = [];
234
235 $condition_ids = $this->condition_factory->getConditionIdsForItem($ls_item->getRefId());
236 foreach ($condition_ids as $condition_id) {
237 try {
238 $condition = $this->condition_factory->getConditionInstanceById($condition_id);
239 } catch (\Throwable) {
240 continue;
241 }
242
243 $entry = [
244 'type' => $condition->getIdentifierForClass($condition::class),
245 'subtype' => null,
246 'data' => [],
247 ];
248
249 if ($condition instanceof SubtypeAwareInterface) {
250 $entry['subtype'] = $condition->getSubtype();
251 }
252
253 $entry['data'] = $condition->export();
254 $all_conditions[] = $entry;
255 }
256
257 return $all_conditions;
258 }
259
260 protected function writeFooter(): void
261 {
262 $this->xmlEndTag(self::TAG_LSO);
263 }
264}
$settings
Definition: index.php:44
static _exportContainerSettings(ilXmlWriter $a_xml, int $a_obj_id)
static getInstanceByMode(int $a_obj_id, int $a_mode)
Settings for an LSO (like abstract, extro)
__construct(protected ilObjLearningSequence $ls_object, protected ilSetting $settings, protected ilLPObjSettings $lp_settings)
static _lookupObjectId(int $ref_id)
ILIAS Setting Class.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
xmlSetGenCmt(string $genCmt)
Sets generated comment.
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
xmlEndTag(string $tag)
Writes an endtag.
xmlSetDtdDef(string $dtdDef)
Sets dtd definition.
xmlDumpMem(bool $format=true)
Returns xml document from memory.
xmlStartTag(string $tag, ?array $attrs=null, bool $empty=false, bool $encode=true, bool $escape=true)
Writes a starttag.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc