ILIAS  release_8 Revision v8.24
class.ilObjectXMLParser.php
Go to the documentation of this file.
1<?php
2
3/******************************************************************************
4 * This file is part of ILIAS, a powerful learning management system.
5 * ILIAS is licensed with the GPL-3.0, you should have received a copy
6 * of said license along with the source code.
7 * If this is not the case or you just want to try ILIAS, you'll find
8 * us at:
9 * https://www.ilias.de
10 * https://github.com/ILIAS-eLearning
11 *****************************************************************************/
13{
14 public array $object_data = [];
15
16 private int $ref_id = 0;
17 private int $parent_id = 0;
18 private int $curr_obj = 0;
19 private array $time_target = [];
20 private string $cdata = '';
21
22 public function __construct(string $a_xml_data = '', ?bool $throw_exception = false)
23 {
25 $this->setXMLContent($a_xml_data);
26 }
27
28 public function getObjectData() : array
29 {
30 return $this->object_data;
31 }
32
37 public function setHandlers($a_xml_parser) : void
38 {
39 xml_set_object($a_xml_parser, $this);
40 xml_set_element_handler($a_xml_parser, 'handlerBeginTag', 'handlerEndTag');
41 xml_set_character_data_handler($a_xml_parser, 'handlerCharacterData');
42 }
43
47 public function handlerBeginTag($a_xml_parser, string $a_name, array $a_attribs) : void
48 {
49 switch ($a_name) {
50 case 'Objects':
51 $this->curr_obj = -1;
52 break;
53
54 case 'Object':
56
57 $this->addProperty('type', (string) $a_attribs['type']);
58 $this->addProperty(
59 'obj_id',
60 is_numeric($a_attribs['obj_id']) ? (int) $a_attribs["obj_id"] : ilUtil::__extractId(
61 $a_attribs["obj_id"] ?? '',
63 )
64 );
65 $this->addProperty('offline', $a_attribs['offline']);
66 break;
67
68 case 'ImportId':
69 case 'LastUpdate':
70 case 'CreateDate':
71 case 'Owner':
72 case 'Description':
73 case 'Title':
74 break;
75
76 case 'References':
77 $this->time_target = [];
78 $this->ref_id = $a_attribs["ref_id"];
79 $this->parent_id = $a_attribs['parent_id'];
80 break;
81
82 case 'TimeTarget':
83 $this->time_target['timing_type'] = $a_attribs['type'];
84 break;
85
86 case 'Timing':
87 $this->time_target['timing_visibility'] = $a_attribs['visibility'];
88 if (isset($a_attribs['starting_time'])) {
89 $this->time_target['starting_time'] = $a_attribs['starting_time'];
90 }
91 if (isset($a_attribs['ending_time'])) {
92 $this->time_target['ending_time'] = $a_attribs['ending_time'];
93 }
94
95 if ($a_attribs['ending_time'] < $a_attribs['starting_time']) {
96 throw new ilObjectXMLException('Starting time must be earlier than ending time.');
97 }
98 break;
99
100 case 'Suggestion':
101 $this->time_target['changeable'] = $a_attribs['changeable'];
102
103 if (isset($a_attribs['starting_time'])) {
104 $this->time_target['suggestion_start'] = $a_attribs['starting_time'];
105 }
106 if (isset($a_attribs['ending_time'])) {
107 $this->time_target['suggestion_end'] = $a_attribs['ending_time'];
108 }
109 break;
110
111 }
112 }
113
119 public function handlerEndTag($a_xml_parser, string $a_name) : void
120 {
121 switch ($a_name) {
122 case 'Object':
123 case 'Objects':
124 break;
125
126 case 'Title':
127 $this->addProperty('title', trim($this->cdata));
128 break;
129
130 case 'Description':
131 $this->addProperty('description', trim($this->cdata));
132 break;
133
134 case 'Owner':
135 $this->addProperty('owner', trim($this->cdata));
136 break;
137
138 case 'CreateDate':
139 $this->addProperty('create_date', trim($this->cdata));
140 break;
141
142 case 'LastUpdate':
143 $this->addProperty('last_update', trim($this->cdata));
144 break;
145
146 case 'ImportId':
147 $this->addProperty('import_id', trim($this->cdata));
148 break;
149
150 case 'References':
151 $this->addReference($this->ref_id, $this->parent_id, $this->time_target);
152 break;
153 }
154
155 $this->cdata = '';
156 }
157
163 public function handlerCharacterData($a_xml_parser, string $a_data) : void
164 {
165 if ($a_data !== "\n") {
166 // Replace multiple tabs with one space
167 $a_data = preg_replace("/\t+/", " ", $a_data);
168
169 $this->cdata .= $a_data;
170 }
171 }
172
178 private function addProperty(string $a_name, $a_value) : void
179 {
180 $this->object_data[$this->curr_obj][$a_name] = $a_value;
181 }
182
183 private function addReference(int $a_ref_id, int $a_parent_id, array $a_time_target) : void
184 {
185 $reference['ref_id'] = $a_ref_id;
186 $reference['parent_id'] = $a_parent_id;
187 $reference['time_target'] = $a_time_target;
188
189 if (isset($reference['time_target']['changeable']) && $reference['time_target']['changeable'] &&
190 !isset($reference['time_target']['suggestion_start'], $reference['time_target']['suggestion_end'])) {
191 throw new ilObjectXMLException(
192 'Missing attributes: "starting_time" and "ending_time" required for attribute "changeable"'
193 );
194 }
195
196 $this->object_data[$this->curr_obj]['references'][] = $reference;
197 }
198}
Exception class for ObjectXMLWriter and ObjectXMLParser.
handlerCharacterData($a_xml_parser, string $a_data)
addProperty(string $a_name, $a_value)
addReference(int $a_ref_id, int $a_parent_id, array $a_time_target)
handlerEndTag($a_xml_parser, string $a_name)
__construct(string $a_xml_data='', ?bool $throw_exception=false)
handlerBeginTag($a_xml_parser, string $a_name, array $a_attribs)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setXMLContent(string $a_xml_content)
static __extractId(string $ilias_id, int $inst_id)
extract ref id from role title, e.g.
const IL_INST_ID
Definition: constants.php:40
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc