ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilSCORMPackageParser.php
Go to the documentation of this file.
1<?php
2
19declare(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_element_handler($a_xml_parser, $this->handlerBeginTag(...), $this->handlerEndTag(...));
68 xml_set_character_data_handler($a_xml_parser, $this->handlerCharacterData(...));
69 }
70
74 public function startParsing(): void
75 {
76 parent::startParsing();
77 }
78
79 public function getPackageTitle(): string
80 {
81 return ilUtil::stripSlashes($this->package_title);
82 }
83
87 public function beginElement(string $a_name): void
88 {
89 // if (!isset($this->status["$a_name"])) {
90 // $this->cnt[$a_name] == 1;
91 // } else {
92 // $this->cnt[$a_name]++;
93 // }
94 $this->current_element[count($this->current_element)] = $a_name;
95 }
96
100 public function endElement(string $a_name): void
101 {
102 // $this->cnt[$a_name]--;
103 unset($this->current_element[count($this->current_element) - 1]);
104 }
105
109 public function getCurrentElement(): ?string
110 {
111 return ($this->current_element[count($this->current_element) - 1]);
112 }
113
114 public function getAncestorElement(int $nr = 1): ?string
115 {
116 return ($this->current_element[count($this->current_element) - 1 - $nr]);
117 }
118
119 /*
120 * returns number of current open elements of type $a_name
121 */
122 // public function getOpenCount($a_name)
123 // {
124 // if (isset($this->cnt[$a_name])) {
125 // return $this->cnt[$a_name];
126 // } else {
127 // return 0;
128 // }
129 // }
130
134 public function buildTag(string $type, string $name, ?array $attr = null): string
135 {
136 $tag = "<";
137
138 if ($type === "end") {
139 $tag .= "/";
140 }
141
142 $tag .= $name;
143
144 if (is_array($attr)) {
145 foreach ($attr as $k => $v) {
146 $tag .= " " . $k . "=\"$v\"";
147 }
148 }
149
150 $tag .= ">";
151
152 return $tag;
153 }
154
155 public function getCurrentParent(): int
156 {
157 return $this->parent_stack[count($this->parent_stack) - 1];
158 }
159
167 public function handlerBeginTag(XMLParser $a_xml_parser, string $a_name, array $a_attribs): void
168 {
169 //echo "<br>handlerBeginTag:".$a_name;
170 switch ($a_name) {
171 case "manifest":
172 $mVersion = "";
173 if (isset($a_attribs["version"])) {
174 $mVersion = $a_attribs["version"];
175 }
176 $manifest = new ilSCORMManifest();
177 $manifest->setSLMId($this->slm_object->getId());
178 $manifest->setImportId($a_attribs["identifier"]);
179 $manifest->setVersion($mVersion);
180 if (isset($a_attribs["xml:base"])) {
181 $manifest->setXmlBase($a_attribs["xml:base"]);
182 }
183 $manifest->create();
184 if (!$this->tree_created) {
185 $this->sc_tree = new ilSCORMTree($this->slm_object->getId());
186 $this->sc_tree->addTree($this->slm_object->getId(), $manifest->getId());
187 } else {
188 $this->sc_tree->insertNode($manifest->getId(), $this->getCurrentParent());
189 }
190 $this->parent_stack[] = $manifest->getId();
191 break;
192
193 case "organizations":
194 $organizations = new ilSCORMOrganizations();
195 $organizations->setSLMId($this->slm_object->getId());
196 if (isset($a_attribs["default"])) {
197 $organizations->setDefaultOrganization($a_attribs["default"]);
198 } else {
199 $organizations->setDefaultOrganization("");
200 }
201 $organizations->create();
202 $this->sc_tree->insertNode($organizations->getId(), $this->getCurrentParent());
203 $this->parent_stack[] = $organizations->getId();
204 break;
205
206 case "organization":
207 $organization = new ilSCORMOrganization();
208 $organization->setSLMId($this->slm_object->getId());
209 $organization->setImportId($a_attribs["identifier"]);
210 if (isset($a_attribs["structure"])) {
211 $organization->setStructure($a_attribs["structure"]);
212 }
213 $organization->create();
214 $this->current_organization = &$organization;
215 $this->sc_tree->insertNode($organization->getId(), $this->getCurrentParent());
216 $this->parent_stack[] = $organization->getId();
217 break;
218
219 case "item":
220 $item = new ilSCORMItem();
221 $item->setSLMId($this->slm_object->getId());
222 $item->setImportId($a_attribs["identifier"]);
223 $item->setIdentifierRef((string) $a_attribs["identifierref"]);
224 if (isset($a_attribs["isvisible"])) {
225 if (strtolower((string) $a_attribs["isvisible"]) !== "false") {
226 $item->setVisible(true);
227 } else {
228 $item->setVisible(false);
229 }
230 }
231 if (isset($a_attribs["parameters"])) {
232 $item->setParameters($a_attribs["parameters"]);
233 }
234 $item->create();
235 $this->sc_tree->insertNode($item->getId(), $this->getCurrentParent());
236 $this->parent_stack[] = $item->getId();
237 $this->item_stack[count($this->item_stack)] = &$item;
238 break;
239
240 case "adlcp:prerequisites":
241 $this->item_stack[count($this->item_stack) - 1]->setPrereqType($a_attribs["type"]);
242 break;
243
244 case "resources":
246 $resources->setSLMId($this->slm_object->getId());
247 if (isset($a_attribs["xml:base"])) {
248 $resources->setXmlBase($a_attribs["xml:base"]);
249 }
250 $resources->create();
251 $this->sc_tree->insertNode($resources->getId(), $this->getCurrentParent());
252 $this->parent_stack[] = $resources->getId();
253 break;
254
255 case "resource":
256 $resource = new ilSCORMResource();
257 $resource->setSLMId($this->slm_object->getId());
258 $resource->setImportId($a_attribs["identifier"]);
259 $resource->setResourceType($a_attribs["type"]);
260 if (isset($a_attribs["adlcp:scormtype"])) {
261 $resource->setScormType($a_attribs["adlcp:scormtype"]);
262 }
263 if (isset($a_attribs["xml:base"])) {
264 $resource->setXmlBase($a_attribs["xml:base"]);
265 }
266 if (isset($a_attribs["href"])) {
267 $resource->setHRef($a_attribs["href"]);
268 }
269 $resource->create();
270 $this->current_resource = &$resource;
271 $this->sc_tree->insertNode($resource->getId(), $this->getCurrentParent());
272 $this->parent_stack[] = $resource->getId();
273 break;
274
275 case "file":
276 $file = new ilSCORMResourceFile();
277 $file->setHRef($a_attribs["href"]);
278 $this->current_resource->addFile($file);
279 break;
280
281 case "dependency":
282 $dependency = new ilSCORMResourceDependency();
283 $dependency->setIdentifierRef($a_attribs["identifierref"]);
284 $this->current_resource->addDependency($dependency);
285 break;
286
287 }
288 $this->beginElement($a_name);
289 }
290
297 public function handlerEndTag(XMLParser $a_xml_parser, string $a_name): void
298 {
299 //echo "<br>handlerEndTag:".$a_name;
300
301 switch ($a_name) {
302 case "manifest":
303 case "organizations":
304 case "resources":
305 array_pop($this->parent_stack);
306 break;
307
308 case "organization":
309 $this->current_organization->update();
310 array_pop($this->parent_stack);
311 break;
312
313 case "item":
314 $this->item_stack[count($this->item_stack) - 1]->update();
315 unset($this->item_stack[count($this->item_stack) - 1]);
316 array_pop($this->parent_stack);
317 break;
318
319 case "resource":
320 $this->current_resource->update();
321 array_pop($this->parent_stack);
322 break;
323
324 }
325 $this->endElement($a_name);
326 }
327
334 public function handlerCharacterData(XMLParser $a_xml_parser, ?string $a_data): void
335 {
336 //echo "<br>handlerCharacterData:".$this->getCurrentElement().":".$a_data;
337 // DELETE WHITESPACES AND NEWLINES OF CHARACTER DATA
338 $a_data = preg_replace("/\n/", "", $a_data);
339 $a_data = preg_replace("/\t+/", "", $a_data);
340 if (!empty($a_data)) {
341 switch ($this->getCurrentElement()) {
342 case "title":
343 switch ($this->getAncestorElement(1)) {
344 case "organization":
345 $this->current_organization->setTitle(
346 ilUtil::stripSlashes($this->current_organization->getTitle() . $a_data)
347 );
348 $this->package_title = ilUtil::stripSlashes($this->current_organization->getTitle());
349 break;
350
351 case "item":
352 $this->item_stack[count($this->item_stack) - 1]->setTitle(
353 ilUtil::stripSlashes($this->item_stack[count($this->item_stack) - 1]->getTitle() . $a_data)
354 );
355 break;
356 }
357 break;
358
359 case "adlcp:prerequisites":
360 $this->item_stack[count($this->item_stack) - 1]->setPrerequisites($a_data);
361 break;
362
363 case "adlcp:maxtimeallowed":
364 $this->item_stack[count($this->item_stack) - 1]->setMaxTimeAllowed($a_data);
365 break;
366
367 case "adlcp:timelimitaction":
368 $this->item_stack[count($this->item_stack) - 1]->setTimeLimitAction($a_data);
369 break;
370
371 case "adlcp:datafromlms":
372 $this->item_stack[count($this->item_stack) - 1]->setDataFromLms($a_data);
373 break;
374
375 case "adlcp:masteryscore":
376 $this->item_stack[count($this->item_stack) - 1]->setMasteryScore($a_data);
377 break;
378
379 }
380 }
381 }
382}
handlerCharacterData(XMLParser $a_xml_parser, ?string $a_data)
handler for character data
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
__construct(object $a_slm_object, string $a_xml_file)
Constructor.
beginElement(string $a_name)
update parsing status for a element begin
setHandlers($a_xml_parser)
set event handler should be overwritten by inherited class
handlerBeginTag(XMLParser $a_xml_parser, string $a_name, array $a_attribs)
handler for begin of element
getCurrentElement()
returns current element
handlerEndTag(XMLParser $a_xml_parser, string $a_name)
handler for end of element
SCORM Resource Dependency, DB accesses are done in ilSCORMResource.
SCORM Resource File, DB accesses are done in ilSCORMResource.
SCORM Resources Element.
SCORM Object Tree.
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
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc