ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilSCORMPackageParser.php
Go to the documentation of this file.
1<?php
2/*
3 +-----------------------------------------------------------------------------+
4 | ILIAS open source |
5 +-----------------------------------------------------------------------------+
6 | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7 | |
8 | This program is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU General Public License |
10 | as published by the Free Software Foundation; either version 2 |
11 | of the License, or (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21 +-----------------------------------------------------------------------------+
22*/
23
24require_once("./Modules/ScormAicc/classes/SCORM/class.ilSCORMManifest.php");
25require_once("./Modules/ScormAicc/classes/SCORM/class.ilSCORMOrganizations.php");
26require_once("./Modules/ScormAicc/classes/SCORM/class.ilSCORMOrganization.php");
27require_once("./Modules/ScormAicc/classes/SCORM/class.ilSCORMItem.php");
28require_once("./Modules/ScormAicc/classes/SCORM/class.ilSCORMResources.php");
29require_once("./Modules/ScormAicc/classes/SCORM/class.ilSCORMResource.php");
30require_once("./Modules/ScormAicc/classes/SCORM/class.ilSCORMResourceFile.php");
31require_once("./Modules/ScormAicc/classes/SCORM/class.ilSCORMResourceDependency.php");
32require_once("./Modules/ScormAicc/classes/SCORM/class.ilSCORMTree.php");
33
43{
44 public $cnt; // counts open elements
45 public $current_element; // store current element type
47 public $parent_stack; // stack of current parent nodes
48 public $tree_created; // flag that determines wether the scorm tree has been created
49 public $scorm_tree; // manifest tree
50 public $current_organization; // current organization object
51 public $current_resource; // current resource object
52 public $item_stack; // current open item objects
53 public $package_title = ""; // title for the package (title from organisation)
54
55
63 public function __construct(&$a_slm_object, $a_xml_file)
64 {
65 parent::__construct($a_xml_file);
66 $this->cnt = array();
67 $this->current_element = array();
68 $this->slm_object = $a_slm_object;
69 $this->tree_created = false;
70 $this->parent_stack = array();
71 $this->item_stack = array();
72 }
73
79 public function setHandlers($a_xml_parser)
80 {
81 xml_set_object($a_xml_parser, $this);
82 xml_set_element_handler($a_xml_parser, 'handlerBeginTag', 'handlerEndTag');
83 xml_set_character_data_handler($a_xml_parser, 'handlerCharacterData');
84 }
85
86 public function startParsing()
87 {
88 parent::startParsing();
89 }
90
91 public function getPackageTitle()
92 {
94 }
95
96 /*
97 * update parsing status for a element begin
98 */
99 public function beginElement($a_name)
100 {
101 if (!isset($this->status["$a_name"])) {
102 $this->cnt[$a_name] == 1;
103 } else {
104 $this->cnt[$a_name]++;
105 }
106 $this->current_element[count($this->current_element)] = $a_name;
107 }
108
109 /*
110 * update parsing status for an element ending
111 */
112 public function endElement($a_name)
113 {
114 $this->cnt[$a_name]--;
115 unset($this->current_element[count($this->current_element) - 1]);
116 }
117
118 /*
119 * returns current element
120 */
121 public function getCurrentElement()
122 {
123 return ($this->current_element[count($this->current_element) - 1]);
124 }
125
126 /*
127 * returns current element
128 */
129 public function getAncestorElement($nr = 1)
130 {
131 return ($this->current_element[count($this->current_element) - 1 - $nr]);
132 }
133
134 /*
135 * returns number of current open elements of type $a_name
136 */
137 public function getOpenCount($a_name)
138 {
139 if (isset($this->cnt[$a_name])) {
140 return $this->cnt[$a_name];
141 } else {
142 return 0;
143 }
144 }
145
153 public function buildTag($type, $name, $attr = "")
154 {
155 $tag = "<";
156
157 if ($type == "end") {
158 $tag .= "/";
159 }
160
161 $tag .= $name;
162
163 if (is_array($attr)) {
164 foreach ($attr as $k => $v) {
165 $tag .= " " . $k . "=\"$v\"";
166 }
167 }
168
169 $tag .= ">";
170
171 return $tag;
172 }
173
174 public function getCurrentParent()
175 {
176 return $this->parent_stack[count($this->parent_stack) - 1];
177 }
178
182 public function handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
183 {
184 //echo "<br>handlerBeginTag:".$a_name;
185 switch ($a_name) {
186 case "manifest":
187 $manifest = new ilSCORMManifest();
188 $manifest->setSLMId($this->slm_object->getId());
189 $manifest->setImportId($a_attribs["identifier"]);
190 $manifest->setVersion($a_attribs["version"]);
191 $manifest->setXmlBase($a_attribs["xml:base"]);
192 $manifest->create();
193 if (!$this->tree_created) {
194 $this->sc_tree = new ilSCORMTree($this->slm_object->getId());
195 $this->sc_tree->addTree($this->slm_object->getId(), $manifest->getId());
196 } else {
197 $this->sc_tree->insertNode($manifest->getId(), $this->getCurrentParent());
198 }
199 array_push($this->parent_stack, $manifest->getId());
200 break;
201
202 case "organizations":
203 $organizations = new ilSCORMOrganizations();
204 $organizations->setSLMId($this->slm_object->getId());
205 $organizations->setDefaultOrganization($a_attribs["default"]);
206 $organizations->create();
207 $this->sc_tree->insertNode($organizations->getId(), $this->getCurrentParent());
208 array_push($this->parent_stack, $organizations->getId());
209 break;
210
211 case "organization":
212 $organization = new ilSCORMOrganization();
213 $organization->setSLMId($this->slm_object->getId());
214 $organization->setImportId($a_attribs["identifier"]);
215 $organization->setStructure($a_attribs["structure"]);
216 $organization->create();
217 $this->current_organization = &$organization;
218 $this->sc_tree->insertNode($organization->getId(), $this->getCurrentParent());
219 array_push($this->parent_stack, $organization->getId());
220 break;
221
222 case "item":
223 $item = new ilSCORMItem();
224 $item->setSLMId($this->slm_object->getId());
225 $item->setImportId($a_attribs["identifier"]);
226 $item->setIdentifierRef($a_attribs["identifierref"]);
227 if (strtolower($a_attribs["isvisible"]) != "false") {
228 $item->setVisible(true);
229 } else {
230 $item->setVisible(false);
231 }
232 $item->setParameters($a_attribs["parameters"]);
233 $item->create();
234 $this->sc_tree->insertNode($item->getId(), $this->getCurrentParent());
235 array_push($this->parent_stack, $item->getId());
236 $this->item_stack[count($this->item_stack)] = &$item;
237 break;
238
239 case "adlcp:prerequisites":
240 $this->item_stack[count($this->item_stack) - 1]->setPrereqType($a_attribs["type"]);
241 break;
242
243 case "resources":
244 $resources = new ilSCORMResources();
245 $resources->setSLMId($this->slm_object->getId());
246 $resources->setXmlBase($a_attribs["xml:base"]);
247 $resources->create();
248 $this->sc_tree->insertNode($resources->getId(), $this->getCurrentParent());
249 array_push($this->parent_stack, $resources->getId());
250 break;
251
252 case "resource":
253 $resource = new ilSCORMResource();
254 $resource->setSLMId($this->slm_object->getId());
255 $resource->setImportId($a_attribs["identifier"]);
256 $resource->setResourceType($a_attribs["type"]);
257 $resource->setScormType($a_attribs["adlcp:scormtype"]);
258 $resource->setXmlBase($a_attribs["xml:base"]);
259 $resource->setHRef($a_attribs["href"]);
260 $resource->create();
261 $this->current_resource = &$resource;
262 $this->sc_tree->insertNode($resource->getId(), $this->getCurrentParent());
263 array_push($this->parent_stack, $resource->getId());
264 break;
265
266 case "file":
267 $file = new ilSCORMResourceFile();
268 $file->setHRef($a_attribs["href"]);
269 $this->current_resource->addFile($file);
270 break;
271
272 case "dependency":
273 $dependency = new ilSCORMResourceDependency();
274 $dependency->setIdentifierRef($a_attribs["identifierref"]);
275 $this->current_resource->addDependency($dependency);
276 break;
277
278 }
279 $this->beginElement($a_name);
280 }
281
285 public function handlerEndTag($a_xml_parser, $a_name)
286 {
287 //echo "<br>handlerEndTag:".$a_name;
288
289 switch ($a_name) {
290 case "manifest":
291 case "organizations":
292 case "resources":
293 array_pop($this->parent_stack);
294 break;
295
296 case "organization":
297 $this->current_organization->update();
298 array_pop($this->parent_stack);
299 break;
300
301 case "item":
302 $this->item_stack[count($this->item_stack) - 1]->update();
303 unset($this->item_stack[count($this->item_stack) - 1]);
304 array_pop($this->parent_stack);
305 break;
306
307 case "resource":
308 $this->current_resource->update();
309 array_pop($this->parent_stack);
310 break;
311
312 }
313 $this->endElement($a_name);
314 }
315
319 public function handlerCharacterData($a_xml_parser, $a_data)
320 {
321 //echo "<br>handlerCharacterData:".$this->getCurrentElement().":".$a_data;
322 // DELETE WHITESPACES AND NEWLINES OF CHARACTER DATA
323 $a_data = preg_replace("/\n/", "", $a_data);
324 $a_data = preg_replace("/\t+/", "", $a_data);
325 if (!empty($a_data)) {
326 switch ($this->getCurrentElement()) {
327 case "title":
328 switch ($this->getAncestorElement(1)) {
329 case "organization":
330 $this->current_organization->setTitle(
331 $this->current_organization->getTitle() . $a_data
332 );
333 $this->package_title = $this->current_organization->getTitle();
334 break;
335
336 case "item":
337 $this->item_stack[count($this->item_stack) - 1]->setTitle(
338 $this->item_stack[count($this->item_stack) - 1]->getTitle() . $a_data
339 );
340 break;
341 }
342 break;
343
344 case "adlcp:prerequisites":
345 $this->item_stack[count($this->item_stack) - 1]->setPrerequisites($a_data);
346 break;
347
348 case "adlcp:maxtimeallowed":
349 $this->item_stack[count($this->item_stack) - 1]->setMaxTimeAllowed($a_data);
350 break;
351
352 case "adlcp:timelimitaction":
353 $this->item_stack[count($this->item_stack) - 1]->setTimeLimitAction($a_data);
354 break;
355
356 case "adlcp:datafromlms":
357 $this->item_stack[count($this->item_stack) - 1]->setDataFromLms($a_data);
358 break;
359
360 case "adlcp:masteryscore":
361 $this->item_stack[count($this->item_stack) - 1]->setMasteryScore($a_data);
362 break;
363
364 }
365 }
366 }
367}
An exception for terminatinating execution or to throw for unit testing.
buildTag($type, $name, $attr="")
generate a tag with given name and attributes
__construct(&$a_slm_object, $a_xml_file)
Constructor.
setHandlers($a_xml_parser)
set event handler should be overwritten by inherited class @access private
handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
handler for begin of element
handlerEndTag($a_xml_parser, $a_name)
handler for end of element
startParsing()
stores xml data in array
handlerCharacterData($a_xml_parser, $a_data)
handler for character data
SCORM Resource Dependency, DB accesses are done in ilSCORMResource.
SCORM Resource File, DB accesses are done in ilSCORMResource.
SCORM Resources Element.
SCORM Object Tree.
Base class for sax-based expat parsing extended classes need to overwrite the method setHandlers and ...
if($format !==null) $name
Definition: metadata.php:230
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
$type