ILIAS  release_8 Revision v8.24
class.ilSCORMPackageParser.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
29{
31 // public array $cnt; // counts open elements
32 public array $current_element; // store current element type
33 public object $slm_object; //better ilObjSCORMModule
34 public array $parent_stack; // stack of current parent nodes
35 public bool $tree_created; // flag that determines wether the scorm tree has been created
36 public object $scorm_tree; // manifest tree
37 public object $current_organization; // current organization object
38 public object $current_resource; // current resource object
39 public array $item_stack; // current open item objects
40 public string $package_title = ""; // title for the package (title from organisation)
47 public function __construct(object $a_slm_object, string $a_xml_file)
48 {
49 parent::__construct($a_xml_file);
50 // $this->cnt = array();
51 $this->current_element = array();
52 $this->slm_object = $a_slm_object;
53 $this->tree_created = false;
54 $this->parent_stack = array();
55 $this->item_stack = array();
56 }
57
65 public function setHandlers($a_xml_parser): void
66 {
67 xml_set_object($a_xml_parser, $this);
68 xml_set_element_handler($a_xml_parser, 'handlerBeginTag', 'handlerEndTag');
69 xml_set_character_data_handler($a_xml_parser, 'handlerCharacterData');
70 }
71
75 public function startParsing(): void
76 {
77 parent::startParsing();
78 }
79
80 public function getPackageTitle(): string
81 {
82 return ilUtil::stripSlashes($this->package_title);
83 }
84
88 public function beginElement(string $a_name): void
89 {
90 // if (!isset($this->status["$a_name"])) {
91 // $this->cnt[$a_name] == 1;
92 // } else {
93 // $this->cnt[$a_name]++;
94 // }
95 $this->current_element[count($this->current_element)] = $a_name;
96 }
97
101 public function endElement(string $a_name): void
102 {
103 // $this->cnt[$a_name]--;
104 unset($this->current_element[count($this->current_element) - 1]);
105 }
106
110 public function getCurrentElement(): ?string
111 {
112 return ($this->current_element[count($this->current_element) - 1]);
113 }
114
115 public function getAncestorElement(int $nr = 1): ?string
116 {
117 return ($this->current_element[count($this->current_element) - 1 - $nr]);
118 }
119
120 /*
121 * returns number of current open elements of type $a_name
122 */
123 // public function getOpenCount($a_name)
124 // {
125 // if (isset($this->cnt[$a_name])) {
126 // return $this->cnt[$a_name];
127 // } else {
128 // return 0;
129 // }
130 // }
131
135 public function buildTag(string $type, string $name, ?array $attr = null): string
136 {
137 $tag = "<";
138
139 if ($type === "end") {
140 $tag .= "/";
141 }
142
143 $tag .= $name;
144
145 if (is_array($attr)) {
146 foreach ($attr as $k => $v) {
147 $tag .= " " . $k . "=\"$v\"";
148 }
149 }
150
151 $tag .= ">";
152
153 return $tag;
154 }
155
156 public function getCurrentParent(): int
157 {
158 return $this->parent_stack[count($this->parent_stack) - 1];
159 }
160
168 public function handlerBeginTag($a_xml_parser, string $a_name, array $a_attribs): void
169 {
170 //echo "<br>handlerBeginTag:".$a_name;
171 switch ($a_name) {
172 case "manifest":
173 $mVersion = "";
174 if (isset($a_attribs["version"])) {
175 $mVersion = $a_attribs["version"];
176 }
177 $manifest = new ilSCORMManifest();
178 $manifest->setSLMId($this->slm_object->getId());
179 $manifest->setImportId($a_attribs["identifier"]);
180 $manifest->setVersion($mVersion);
181 if (isset($a_attribs["xml:base"])) {
182 $manifest->setXmlBase($a_attribs["xml:base"]);
183 }
184 $manifest->create();
185 if (!$this->tree_created) {
186 $this->sc_tree = new ilSCORMTree($this->slm_object->getId());
187 $this->sc_tree->addTree($this->slm_object->getId(), $manifest->getId());
188 } else {
189 $this->sc_tree->insertNode($manifest->getId(), $this->getCurrentParent());
190 }
191 $this->parent_stack[] = $manifest->getId();
192 break;
193
194 case "organizations":
195 $organizations = new ilSCORMOrganizations();
196 $organizations->setSLMId($this->slm_object->getId());
197 if (isset($a_attribs["default"])) {
198 $organizations->setDefaultOrganization($a_attribs["default"]);
199 } else {
200 $organizations->setDefaultOrganization("");
201 }
202 $organizations->create();
203 $this->sc_tree->insertNode($organizations->getId(), $this->getCurrentParent());
204 $this->parent_stack[] = $organizations->getId();
205 break;
206
207 case "organization":
208 $organization = new ilSCORMOrganization();
209 $organization->setSLMId($this->slm_object->getId());
210 $organization->setImportId($a_attribs["identifier"]);
211 if (isset($a_attribs["structure"])) {
212 $organization->setStructure($a_attribs["structure"]);
213 }
214 $organization->create();
215 $this->current_organization = &$organization;
216 $this->sc_tree->insertNode($organization->getId(), $this->getCurrentParent());
217 $this->parent_stack[] = $organization->getId();
218 break;
219
220 case "item":
221 $item = new ilSCORMItem();
222 $item->setSLMId($this->slm_object->getId());
223 $item->setImportId($a_attribs["identifier"]);
224 $item->setIdentifierRef($a_attribs["identifierref"]);
225 if (isset($a_attribs["isvisible"])) {
226 if (strtolower((string) $a_attribs["isvisible"]) !== "false") {
227 $item->setVisible(true);
228 } else {
229 $item->setVisible(false);
230 }
231 }
232 if (isset($a_attribs["parameters"])) {
233 $item->setParameters($a_attribs["parameters"]);
234 }
235 $item->create();
236 $this->sc_tree->insertNode($item->getId(), $this->getCurrentParent());
237 $this->parent_stack[] = $item->getId();
238 $this->item_stack[count($this->item_stack)] = &$item;
239 break;
240
241 case "adlcp:prerequisites":
242 $this->item_stack[count($this->item_stack) - 1]->setPrereqType($a_attribs["type"]);
243 break;
244
245 case "resources":
247 $resources->setSLMId($this->slm_object->getId());
248 if (isset($a_attribs["xml:base"])) {
249 $resources->setXmlBase($a_attribs["xml:base"]);
250 }
251 $resources->create();
252 $this->sc_tree->insertNode($resources->getId(), $this->getCurrentParent());
253 $this->parent_stack[] = $resources->getId();
254 break;
255
256 case "resource":
257 $resource = new ilSCORMResource();
258 $resource->setSLMId($this->slm_object->getId());
259 $resource->setImportId($a_attribs["identifier"]);
260 $resource->setResourceType($a_attribs["type"]);
261 if (isset($a_attribs["adlcp:scormtype"])) {
262 $resource->setScormType($a_attribs["adlcp:scormtype"]);
263 }
264 if (isset($a_attribs["xml:base"])) {
265 $resource->setXmlBase($a_attribs["xml:base"]);
266 }
267 if (isset($a_attribs["href"])) {
268 $resource->setHRef($a_attribs["href"]);
269 }
270 $resource->create();
271 $this->current_resource = &$resource;
272 $this->sc_tree->insertNode($resource->getId(), $this->getCurrentParent());
273 $this->parent_stack[] = $resource->getId();
274 break;
275
276 case "file":
277 $file = new ilSCORMResourceFile();
278 $file->setHRef($a_attribs["href"]);
279 $this->current_resource->addFile($file);
280 break;
281
282 case "dependency":
283 $dependency = new ilSCORMResourceDependency();
284 $dependency->setIdentifierRef($a_attribs["identifierref"]);
285 $this->current_resource->addDependency($dependency);
286 break;
287
288 }
289 $this->beginElement($a_name);
290 }
291
298 public function handlerEndTag($a_xml_parser, string $a_name): void
299 {
300 //echo "<br>handlerEndTag:".$a_name;
301
302 switch ($a_name) {
303 case "manifest":
304 case "organizations":
305 case "resources":
306 array_pop($this->parent_stack);
307 break;
308
309 case "organization":
310 $this->current_organization->update();
311 array_pop($this->parent_stack);
312 break;
313
314 case "item":
315 $this->item_stack[count($this->item_stack) - 1]->update();
316 unset($this->item_stack[count($this->item_stack) - 1]);
317 array_pop($this->parent_stack);
318 break;
319
320 case "resource":
321 $this->current_resource->update();
322 array_pop($this->parent_stack);
323 break;
324
325 }
326 $this->endElement($a_name);
327 }
328
335 public function handlerCharacterData($a_xml_parser, ?string $a_data): void
336 {
337 //echo "<br>handlerCharacterData:".$this->getCurrentElement().":".$a_data;
338 // DELETE WHITESPACES AND NEWLINES OF CHARACTER DATA
339 $a_data = preg_replace("/\n/", "", $a_data);
340 $a_data = preg_replace("/\t+/", "", $a_data);
341 if (!empty($a_data)) {
342 switch ($this->getCurrentElement()) {
343 case "title":
344 switch ($this->getAncestorElement(1)) {
345 case "organization":
346 $this->current_organization->setTitle(
347 ilUtil::stripSlashes($this->current_organization->getTitle() . $a_data)
348 );
349 $this->package_title = ilUtil::stripSlashes($this->current_organization->getTitle());
350 break;
351
352 case "item":
353 $this->item_stack[count($this->item_stack) - 1]->setTitle(
354 ilUtil::stripSlashes($this->item_stack[count($this->item_stack) - 1]->getTitle() . $a_data)
355 );
356 break;
357 }
358 break;
359
360 case "adlcp:prerequisites":
361 $this->item_stack[count($this->item_stack) - 1]->setPrerequisites($a_data);
362 break;
363
364 case "adlcp:maxtimeallowed":
365 $this->item_stack[count($this->item_stack) - 1]->setMaxTimeAllowed($a_data);
366 break;
367
368 case "adlcp:timelimitaction":
369 $this->item_stack[count($this->item_stack) - 1]->setTimeLimitAction($a_data);
370 break;
371
372 case "adlcp:datafromlms":
373 $this->item_stack[count($this->item_stack) - 1]->setDataFromLms($a_data);
374 break;
375
376 case "adlcp:masteryscore":
377 $this->item_stack[count($this->item_stack) - 1]->setMasteryScore($a_data);
378 break;
379
380 }
381 }
382 }
383}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
endElement(string $a_name)
update parsing status for an element ending
buildTag(string $type, string $name, ?array $attr=null)
generate a tag with given name and attributes
handlerBeginTag($a_xml_parser, string $a_name, array $a_attribs)
handler for begin of element
__construct(object $a_slm_object, string $a_xml_file)
Constructor.
beginElement(string $a_name)
update parsing status for a element begin
handlerCharacterData($a_xml_parser, ?string $a_data)
handler for character data
setHandlers($a_xml_parser)
set event handler should be overwritten by inherited class
getCurrentElement()
returns current element
handlerEndTag($a_xml_parser, string $a_name)
handler for end of element
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
$resources
Definition: ltiservices.php:68
if($format !==null) $name
Definition: metadata.php:247
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
$type