ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 
24 require_once("./Modules/ScormAicc/classes/SCORM/class.ilSCORMManifest.php");
25 require_once("./Modules/ScormAicc/classes/SCORM/class.ilSCORMOrganizations.php");
26 require_once("./Modules/ScormAicc/classes/SCORM/class.ilSCORMOrganization.php");
27 require_once("./Modules/ScormAicc/classes/SCORM/class.ilSCORMItem.php");
28 require_once("./Modules/ScormAicc/classes/SCORM/class.ilSCORMResources.php");
29 require_once("./Modules/ScormAicc/classes/SCORM/class.ilSCORMResource.php");
30 require_once("./Modules/ScormAicc/classes/SCORM/class.ilSCORMResourceFile.php");
31 require_once("./Modules/ScormAicc/classes/SCORM/class.ilSCORMResourceDependency.php");
32 require_once("./Modules/ScormAicc/classes/SCORM/class.ilSCORMTree.php");
33 
43 {
44  var $cnt; // counts open elements
45  var $current_element; // store current element type
47  var $parent_stack; // stack of current parent nodes
48  var $tree_created; // flag that determines wether the scorm tree has been created
49  var $scorm_tree; // manifest tree
50  var $current_organization; // current organization object
51  var $current_resource; // current resource object
52  var $item_stack; // current open item objects
53  var $package_title = ""; // title for the package (title from organisation)
54 
55 
63  function ilSCORMPackageParser(&$a_slm_object, $a_xml_file)
64  {
65  parent::ilSaxParser($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  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  function startParsing()
87  {
89  }
90 
91  function getPackageTitle()
92  {
93  return $this->package_title;
94  }
95 
96  /*
97  * update parsing status for a element begin
98  */
99  function beginElement($a_name)
100  {
101  if(!isset($this->status["$a_name"]))
102  {
103  $this->cnt[$a_name] == 1;
104  }
105  else
106  {
107  $this->cnt[$a_name]++;
108  }
109  $this->current_element[count($this->current_element)] = $a_name;
110  }
111 
112  /*
113  * update parsing status for an element ending
114  */
115  function endElement($a_name)
116  {
117  $this->cnt[$a_name]--;
118  unset ($this->current_element[count($this->current_element) - 1]);
119  }
120 
121  /*
122  * returns current element
123  */
124  function getCurrentElement()
125  {
126  return ($this->current_element[count($this->current_element) - 1]);
127  }
128 
129  /*
130  * returns current element
131  */
132  function getAncestorElement($nr = 1)
133  {
134  return ($this->current_element[count($this->current_element) - 1 - $nr]);
135  }
136 
137  /*
138  * returns number of current open elements of type $a_name
139  */
140  function getOpenCount($a_name)
141  {
142  if (isset($this->cnt[$a_name]))
143  {
144  return $this->cnt[$a_name];
145  }
146  else
147  {
148  return 0;
149  }
150 
151  }
152 
160  function buildTag ($type, $name, $attr="")
161  {
162  $tag = "<";
163 
164  if ($type == "end")
165  $tag.= "/";
166 
167  $tag.= $name;
168 
169  if (is_array($attr))
170  {
171  while (list($k,$v) = each($attr))
172  $tag.= " ".$k."=\"$v\"";
173  }
174 
175  $tag.= ">";
176 
177  return $tag;
178  }
179 
180  function getCurrentParent()
181  {
182  return $this->parent_stack[count($this->parent_stack) - 1];
183  }
184 
188  function handlerBeginTag($a_xml_parser,$a_name,$a_attribs)
189  {
190 //echo "<br>handlerBeginTag:".$a_name;
191  switch($a_name)
192  {
193  case "manifest":
194  $manifest =& new ilSCORMManifest();
195  $manifest->setSLMId($this->slm_object->getId());
196  $manifest->setImportId($a_attribs["identifier"]);
197  $manifest->setVersion($a_attribs["version"]);
198  $manifest->setXmlBase($a_attribs["xml:base"]);
199  $manifest->create();
200  if (!$this->tree_created)
201  {
202  $this->sc_tree =& new ilSCORMTree($this->slm_object->getId());
203  $this->sc_tree->addTree($this->slm_object->getId(), $manifest->getId());
204  }
205  else
206  {
207  $this->sc_tree->insertNode($manifest->getId(), $this->getCurrentParent());
208  }
209  array_push($this->parent_stack, $manifest->getId());
210  break;
211 
212  case "organizations":
213  $organizations =& new ilSCORMOrganizations();
214  $organizations->setSLMId($this->slm_object->getId());
215  $organizations->setDefaultOrganization($a_attribs["default"]);
216  $organizations->create();
217  $this->sc_tree->insertNode($organizations->getId(), $this->getCurrentParent());
218  array_push($this->parent_stack, $organizations->getId());
219  break;
220 
221  case "organization":
222  $organization =& new ilSCORMOrganization();
223  $organization->setSLMId($this->slm_object->getId());
224  $organization->setImportId($a_attribs["identifier"]);
225  $organization->setStructure($a_attribs["structure"]);
226  $organization->create();
227  $this->current_organization =& $organization;
228  $this->sc_tree->insertNode($organization->getId(), $this->getCurrentParent());
229  array_push($this->parent_stack, $organization->getId());
230  break;
231 
232  case "item":
233  $item =& new ilSCORMItem();
234  $item->setSLMId($this->slm_object->getId());
235  $item->setImportId($a_attribs["identifier"]);
236  $item->setIdentifierRef($a_attribs["identifierref"]);
237  if (strtolower($a_attribs["isvisible"]) != "false")
238  {
239  $item->setVisible(true);
240  }
241  else
242  {
243  $item->setVisible(false);
244  }
245  $item->setParameters($a_attribs["parameters"]);
246  $item->create();
247  $this->sc_tree->insertNode($item->getId(), $this->getCurrentParent());
248  array_push($this->parent_stack, $item->getId());
249  $this->item_stack[count($this->item_stack)] =& $item;
250  break;
251 
252  case "adlcp:prerequisites":
253  $this->item_stack[count($this->item_stack) - 1]->setPrereqType($a_attribs["type"]);
254  break;
255 
256  case "resources":
257  $resources =& new ilSCORMResources();
258  $resources->setSLMId($this->slm_object->getId());
259  $resources->setXmlBase($a_attribs["xml:base"]);
260  $resources->create();
261  $this->sc_tree->insertNode($resources->getId(), $this->getCurrentParent());
262  array_push($this->parent_stack, $resources->getId());
263  break;
264 
265  case "resource":
266  $resource =& new ilSCORMResource();
267  $resource->setSLMId($this->slm_object->getId());
268  $resource->setImportId($a_attribs["identifier"]);
269  $resource->setResourceType($a_attribs["type"]);
270  $resource->setScormType($a_attribs["adlcp:scormtype"]);
271  $resource->setXmlBase($a_attribs["xml:base"]);
272  $resource->setHRef($a_attribs["href"]);
273  $resource->create();
274  $this->current_resource =& $resource;
275  $this->sc_tree->insertNode($resource->getId(), $this->getCurrentParent());
276  array_push($this->parent_stack, $resource->getId());
277  break;
278 
279  case "file":
280  $file =& new ilSCORMResourceFile();
281  $file->setHRef($a_attribs["href"]);
282  $this->current_resource->addFile($file);
283  break;
284 
285  case "dependency":
286  $dependency =& new ilSCORMResourceDependency();
287  $dependency->setIdentifierRef($a_attribs["identifierref"]);
288  $this->current_resource->addDependency($dependency);
289  break;
290 
291  }
292  $this->beginElement($a_name);
293  }
294 
298  function handlerEndTag($a_xml_parser,$a_name)
299  {
300 //echo "<br>handlerEndTag:".$a_name;
301 
302  switch($a_name)
303  {
304  case "manifest":
305  case "organizations":
306  case "resources":
307  array_pop($this->parent_stack);
308  break;
309 
310  case "organization":
311  $this->current_organization->update();
312  array_pop($this->parent_stack);
313  break;
314 
315  case "item":
316  $this->item_stack[count($this->item_stack) - 1]->update();
317  unset($this->item_stack[count($this->item_stack) - 1]);
318  array_pop($this->parent_stack);
319  break;
320 
321  case "resource":
322  $this->current_resource->update();
323  array_pop($this->parent_stack);
324  break;
325 
326  }
327  $this->endElement($a_name);
328 
329  }
330 
334  function handlerCharacterData($a_xml_parser,$a_data)
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  {
342  switch($this->getCurrentElement())
343  {
344  case "title":
345  switch ($this->getAncestorElement(1))
346  {
347  case "organization":
348  $this->current_organization->setTitle(
349  $this->current_organization->getTitle() . $a_data
350  );
351  $this->package_title = $this->current_organization->getTitle();
352  break;
353 
354  case "item":
355  $this->item_stack[count($this->item_stack) - 1]->setTitle(
356  $this->item_stack[count($this->item_stack) - 1]->getTitle() . $a_data
357  );
358  break;
359  }
360  break;
361 
362  case "adlcp:prerequisites":
363  $this->item_stack[count($this->item_stack) - 1]->setPrerequisites($a_data);
364  break;
365 
366  case "adlcp:maxtimeallowed":
367  $this->item_stack[count($this->item_stack) - 1]->setMaxTimeAllowed($a_data);
368  break;
369 
370  case "adlcp:timelimitaction":
371  $this->item_stack[count($this->item_stack) - 1]->setTimeLimitAction($a_data);
372  break;
373 
374  case "adlcp:datafromlms":
375  $this->item_stack[count($this->item_stack) - 1]->setDataFromLms($a_data);
376  break;
377 
378  case "adlcp:masteryscore":
379  $this->item_stack[count($this->item_stack) - 1]->setMasteryScore($a_data);
380  break;
381 
382  }
383  }
384  }
385 }
386 ?>