scormMetadata.class.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php //$id:$
  2. /**
  3. * Container for the scormMetadata class, setup to hold information about the <metadata> element in imsmanifest files
  4. * @package dokeos.scorm
  5. */
  6. /**
  7. * scormMetadata class, handling each <metadata> element found in an imsmanifest file
  8. */
  9. class scormMetadata {
  10. var $lom = '';
  11. var $schema = '';
  12. var $schemaversion = '';
  13. var $location = '';
  14. var $text = '';
  15. var $attribs = array();
  16. /**
  17. * Class constructor. Works in two different ways defined by the first element, being 'db' or 'manifest'.
  18. * If 'db', then it is built using the information available in the Dokeos database. If 'manifest', then it
  19. * is built using the element given as a parameter, expecting it to be a <metadata> element pointer from the
  20. * DOM parser.
  21. * @param string Type of creation required. Can be 'db' or 'manifest' (default)
  22. * @param mixed Depending on the type, can be the DB ID of the learnpath item or the pointer to the <metadata> element in the imsmanifest.xml file
  23. * @return boolean True on success, false on failure
  24. */
  25. function scormMetadata($type='manifest', &$element) {
  26. if(isset($element))
  27. {
  28. $v = substr(phpversion(),0,1);
  29. if($v == 4){
  30. switch($type){
  31. case 'db':
  32. //TODO implement this way of metadata object creation
  33. return false;
  34. //break;
  35. case 'manifest': //do the same as the default
  36. //if($first_item->type == XML_ELEMENT_NODE) this is already check prior to the call to this function
  37. $children = $element->children();
  38. foreach($children as $a => $dummy)
  39. {
  40. $child =& $children[$a];
  41. switch($child->type)
  42. {
  43. case XML_ELEMENT_NODE:
  44. //could be 'lom','schema','schemaversion' or 'location'
  45. switch($child->tagname){
  46. case 'lom':
  47. $childchildren = $child->children();
  48. foreach($childchildren as $index => $dummy)
  49. {
  50. $my_elem = $childchildren[$index];
  51. //there is generally only one child here
  52. //$this->lom[] = $my_elem->content;
  53. $this->lom = $my_elem->content;
  54. }
  55. break;
  56. case 'schema':
  57. $childchildren = $child->children();
  58. foreach($childchildren as $index => $dummy)
  59. {
  60. $my_elem = $childchildren[$index];
  61. //there is generally only one child here
  62. //$this->schema[] = $my_elem->content;
  63. $this->schema = $my_elem->content;
  64. }
  65. break;
  66. case 'schemaversion':
  67. $childchildren = $child->children();
  68. foreach($childchildren as $index => $dummy)
  69. {
  70. $my_elem = $childchildren[$index];
  71. //there is generally only one child here
  72. //$this->schemaversion[] = $my_elem->content;
  73. $this->schemaversion = $my_elem->content;
  74. }
  75. break;
  76. case 'location':
  77. $childchildren = $child->children();
  78. foreach($childchildren as $index => $dummy)
  79. {
  80. $my_elem = $childchildren[$index];
  81. //there is generally only one child here
  82. //$this->location[] = $my_elem->content;
  83. $this->location = $my_elem->content;
  84. }
  85. break;
  86. }
  87. break;
  88. case XML_TEXT_NODE:
  89. if(trim($child->content) != '')
  90. {
  91. if(count($children == 1)){
  92. //if this is the only child at this level and it is a content... save differently
  93. $this->text = $child->content;
  94. }else{
  95. $this->text[$element->tagname] = $child->content;
  96. }
  97. }
  98. break;
  99. }
  100. }
  101. $attributes = $element->attributes();
  102. //$keep_href = '';
  103. if(is_array($attributes)){
  104. foreach($attributes as $a1 => $dummy)
  105. {
  106. $attrib =& $attributes[$a1];
  107. if(trim($attrib->value) != ''){
  108. $this->attribs[$attrib->name] = $attrib->value;
  109. }
  110. }
  111. }
  112. return true;
  113. //break;
  114. }
  115. }elseif($v == 5){
  116. //parsing using PHP5 DOMXML methods
  117. switch($type){
  118. case 'db':
  119. //TODO implement this way of metadata object creation
  120. return false;
  121. //break;
  122. case 'manifest': //do the same as the default
  123. $children = $element->childNodes;
  124. foreach($children as $child)
  125. {
  126. switch($child->nodeType)
  127. {
  128. case XML_ELEMENT_NODE:
  129. //could be 'lom','schema','schemaversion' or 'location'
  130. switch($child->tagName){
  131. case 'lom':
  132. $childchildren = $child->childNodes;
  133. foreach($childchildren as $childchild)
  134. {
  135. //$this->lom = $childchild->textContent;
  136. $this->lom = $childchild->nodeValue;
  137. }
  138. break;
  139. case 'schema':
  140. $childchildren = $child->childNodes;
  141. foreach($childchildren as $childchild)
  142. {
  143. //there is generally only one child here
  144. //$this->schema = $childchildren[$index]->textContent;
  145. $this->schema = $childchild->nodeValue;
  146. }
  147. break;
  148. case 'schemaversion':
  149. $childchildren = $child->childNodes;
  150. foreach($childchildren as $childchild)
  151. {
  152. //there is generally only one child here
  153. //$this->schemaversion = $childchildren[$index]->textContent;
  154. $this->schemaversion = $childchild->nodeValue;
  155. }
  156. break;
  157. case 'location':
  158. $childchildren = $child->childNodes;
  159. foreach($childchildren as $childchild)
  160. {
  161. //there is generally only one child here
  162. //$this->location = $childchildren[$index]->textContent;
  163. $this->location = $childchild->nodeValue;
  164. }
  165. break;
  166. }
  167. break;
  168. case XML_TEXT_NODE:
  169. if(trim($child->textContent) != '')
  170. {
  171. if(count($children == 1)){
  172. //if this is the only child at this level and it is a content... save differently
  173. $this->text = $child->textContent;
  174. }else{
  175. $this->text[$element->tagName] = $child->textContent;
  176. }
  177. }
  178. break;
  179. }
  180. }
  181. $attributes = $element->attributes;
  182. //$keep_href = '';
  183. if(is_array($attributes)){
  184. foreach($attributes as $attrib)
  185. {
  186. if(trim($attrib->value) != ''){
  187. $this->attribs[$attrib->name] = $attrib->value;
  188. }
  189. }
  190. }
  191. return true;
  192. //break;
  193. }
  194. }else{
  195. //cannot parse because not PHP4 nor PHP5... We should not even be here anyway...
  196. return false;
  197. }
  198. }
  199. return false;
  200. }
  201. }
  202. ?>