scormparsing.lib.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php //$id: $
  2. /*
  3. ----------------------------------------------------------------------
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2005 Dokeos S.A.
  6. Copyright (c) Yannick Warnier (yannick.warnier@dokeos.com)
  7. Copyright (c) Denes Nagy (darkden@freemail.hu)
  8. For a full list of contributors, see "credits.txt".
  9. The full license can be read in "license.txt".
  10. This program is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU General Public License
  12. as published by the Free Software Foundation; either version 2
  13. of the License, or (at your option) any later version.
  14. See the GNU General Public License for more details.
  15. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  16. ----------------------------------------------------------------------
  17. */
  18. /**
  19. ==============================================================================
  20. * Library containing functions to parse the imsmanifest.xml file that goes
  21. * along every SCORM content.
  22. *
  23. * @author Denes Nagy <darkden@freemail.hu>
  24. * @access public
  25. *
  26. * @package dokeos.scorm
  27. *
  28. ==============================================================================
  29. */
  30. /**
  31. * Sets the href string value of the element whose identifierref field equals the given identifier.
  32. * Modifies the $items array.
  33. * @param string Identifier seeked
  34. * @param string href link to set in the $items array
  35. * @return void Nothing
  36. */
  37. function put_it_to_items($ident,$href) { //from the resources array, it puts the href to the appropriate place in items array
  38. global $items;
  39. $i=1;
  40. while ($items[$i]) {
  41. if ($items[$i]['identifierref']==$ident) { $items[$i]['href']=$href; }
  42. $i++;
  43. }
  44. }
  45. /**
  46. * This handles the start tags. Modifies the $items array.
  47. * name is the name of the tag, if more attributes than $attribs is an array
  48. * @param resource Parser handler
  49. * @param string Name of the tag
  50. * @param mixed String or array of attributes
  51. * @return void Nothing
  52. */
  53. function startElement($parser, $name, $attribs)
  54. {
  55. global $xml_parser, $defaultorgref, $defaultorgtitle, $inorg, $initem, $intitle, $inmeta, $items, $itemindex, $tabledraw, $inversion, $prereq, $previouslevel, $clusterinfo, $ingeneral;
  56. if ($tabledraw) {
  57. echo "<tr><td>";
  58. echo xml_get_current_line_number($xml_parser);
  59. echo "</td><td>$inorg</td><td>$initem</td>";
  60. }
  61. if ($name=='ORGANIZATIONS') {
  62. list($key, $value) = each($attribs);
  63. $defaultorgref=$value;
  64. }
  65. if (($name=='ORGANIZATION') or ($name=='tableofcontents')) {
  66. $inorg=true;
  67. }
  68. if ($name=='SCHEMAVERSION') {
  69. $inversion=true;
  70. }
  71. if ($name=='ADLCP:PREREQUISITES') {
  72. $prereq=true;
  73. }
  74. if ($name=='METADATA') {
  75. $inmeta=true;
  76. }
  77. if ($name=='GENERAL') {
  78. $ingeneral=true;
  79. }
  80. if ($name=='ITEM') {
  81. $initem++;
  82. $itemindex++;
  83. while (list($key, $value) = each($attribs)) {
  84. if ($key=='IDENTIFIERREF') { $items[$itemindex]['identifierref']=$value; }
  85. if ($key=='IDENTIFIER') { $items[$itemindex]['identifier']=$value; }
  86. if ($key=='PARAMETERS') { $items[$itemindex]['parameters']=$value; }
  87. if ($key=='TITLE') { $items[$itemindex]['title']=$value; }
  88. }
  89. $items[$itemindex]['index']=$itemindex;
  90. $items[$itemindex]['level']=$initem;
  91. if ($initem==$previouslevel) { $clusterinfo++; }
  92. if ($initem>$previouslevel) { $clusterinfo=$clusterinfo*10+1; $previouslevel=$initem; }
  93. if ($initem<$previouslevel) { $clusterinfo=floor($clusterinfo/10)+1; $previouslevel=$initem; }
  94. $items[$itemindex]['clusterinfo']=$clusterinfo;
  95. }
  96. if ($name=='TITLE') {
  97. $intitle=true;
  98. }
  99. if ($name=='RESOURCE') { //by the time it gets there, all the items are already read (overpassed)
  100. while (list($key, $value) = each($attribs)) {
  101. if ($key=='HREF') { $href=$value; }
  102. if ($key=='IDENTIFIER') { $ident=$value; }
  103. }
  104. put_it_to_items($ident,$href);
  105. }
  106. reset($attribs); //drawing the table
  107. if ($tabledraw) {
  108. echo "<td><font color='#0000cc'>$name</font></td>";
  109. if (sizeof($attribs)) {
  110. while (list($k, $v) = each($attribs)) {
  111. echo "<td><font color='#009900'>$k</font>=<font color='#990000'>$v</font></td>";
  112. }
  113. }
  114. }
  115. }
  116. /**
  117. * Handles the end tags
  118. * @param resource Parser handler
  119. * @param string Name of the closing tag
  120. * @return void Nothing
  121. */
  122. function endElement($parser, $name)
  123. {
  124. global $inorg,$initem,$inmeta,$ingeneral,$intitle,$inversion,$prereq;
  125. if (($name=='ORGANIZATION') or ($name=='tableofcontents')) {
  126. $inorg=false;
  127. }
  128. if ($name=='ITEM') {
  129. $initem--;
  130. }
  131. if ($name=='METADATA') {
  132. $inmeta==false;
  133. }
  134. if ($name=='GENERAL') {
  135. $ingeneral==false;
  136. }
  137. if ($name=='TITLE') {
  138. $intitle=false;
  139. }
  140. if ($name=='SCHEMAVERSION') {
  141. $inversion=false;
  142. }
  143. if ($name=='ADLCP:PREREQUISITES') { //this is only good for scorm 1.2
  144. $prereq=false;
  145. }
  146. }
  147. /**
  148. * handles the data between start and and tags. Modifies the $items array.
  149. * note : if f.ex. Steering & sailing is the data, then this function is called 3 times !!!
  150. * @param resource Parser handler
  151. * @param string The data between start and stop tags
  152. * @return void Nothing
  153. */
  154. function characterData($parser, $data)
  155. {
  156. global $defaultorgtitle, $initem, $intitle, $inmeta, $inorg, $items, $itemindex, $inversion, $version, $prereq, $ingeneral;
  157. //echo "<td>int:$intitle ino:$inorg ini:$initem inm:$inmeta</td>";
  158. if (($intitle==true) and ($inorg==true) and ($initem==false) and ($ingeneral==false)) {
  159. $defaultorgtitle.=$data;
  160. return;
  161. }
  162. if (($intitle==true) and ($initem==true)) {
  163. $items[$itemindex]['title'].=$data;
  164. return;
  165. }
  166. if ($inversion==true) {
  167. $version=$data; //this will provide the last schemaversion tag version number only !
  168. return;
  169. }
  170. if ($prereq==true) {
  171. $items[$itemindex]['prereq']=$data;
  172. }
  173. }
  174. /**
  175. * Creates a new xml parser and returns an array containing the parser resource and the xml file resource.
  176. * @param string Path to the XML file we want to parse
  177. * @return mixed Array containing the parser handler and the XML file handler.
  178. */
  179. function new_xml_parser($request_file)
  180. {
  181. global $parser_file;
  182. $xml_parser = xml_parser_create();
  183. xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 1);
  184. xml_set_element_handler($xml_parser, "startElement", "endElement"); //telling which function will handle start and end tags
  185. xml_set_character_data_handler($xml_parser, "characterData");
  186. if (!($fp = @fopen($request_file, "r"))) {
  187. return false;
  188. }
  189. if (!is_array($parser_file)) {
  190. settype($parser_file, "array");
  191. }
  192. $parser_file[$xml_parser] = $request_file;
  193. return array($xml_parser, $fp);
  194. }
  195. ?>