scormResource.class.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php //$id:$
  2. /**
  3. * Container for the scormResource class
  4. * @package dokeos.learnpath.scorm
  5. * @author Yannick Warnier <ywarnier@beeznest.org>
  6. */
  7. /**
  8. * Class defining the <resource> tag in an imsmanifest.xml file
  9. *
  10. */
  11. class scormResource {
  12. var $identifier = '';
  13. var $type = 'webcontent';
  14. //var $identifierref = '';
  15. var $scormtype = 'sco'; //fix problems with ENI content where asset is not defined
  16. var $base = '';
  17. var $href = '';
  18. var $metadata;
  19. //var $file_href;
  20. //var $file_metadata;
  21. var $files = array();
  22. var $dependencies = array();
  23. /**
  24. * Class constructor. Depending of the type of construction called ('db' or 'manifest'), will create a scormResource
  25. * object from database records or from the DOM element given as parameter
  26. * @param string Type of construction needed ('db' or 'manifest', default = 'manifest')
  27. * @param mixed Depending on the type given, DB id for the lp_item or reference to the DOM element
  28. */
  29. function scormResource($type='manifest',&$element) {
  30. /*
  31. echo "<pre>Analysing resource:<br />\n";
  32. var_dump($element);
  33. echo "</pre><br />\n";
  34. */
  35. if(isset($element))
  36. {
  37. $v = substr(phpversion(),0,1);
  38. if($v == 4){
  39. switch($type){
  40. case 'db':
  41. //TODO implement this way of metadata object creation
  42. return false;
  43. case 'manifest': //do the same as the default
  44. default:
  45. //if($first_item->type == XML_ELEMENT_NODE) this is already check prior to the call to this function
  46. $children = $element->children();
  47. foreach($children as $a => $dummy)
  48. {
  49. $child =& $children[$a];
  50. switch($child->type)
  51. {
  52. case XML_ELEMENT_NODE:
  53. switch($child->tagname){
  54. case 'file':
  55. //echo "Child is a file tag<br />\n";
  56. $this->files[] = $child->get_attribute('href');
  57. //var_dump($this->files);
  58. //ignoring file metadata
  59. //files[] array contains all <file href='x'> tags one by one
  60. break;
  61. case 'metadata':
  62. //echo "Child is a metadata tag<br />\n";
  63. $this->metadata = new scormMetadata('manifest',$child);
  64. break;
  65. case 'dependency':
  66. //echo "Child is a dependency tag<br />\n";
  67. //need to get identifierref attribute inside dependency node
  68. //dependencies[] array represents all <dependency identifierref='x'> tags united
  69. $this->dependencies[] = $child->get_attribute('identifierref');
  70. break;
  71. }
  72. break;
  73. }
  74. }
  75. $attributes = $element->attributes();
  76. //$keep_href = '';
  77. if(count($attributes)>0){ //in some cases we get here with an empty attributes array
  78. //TODO find when and why we get such a case (empty array)
  79. foreach($attributes as $a1 => $dummy)
  80. {
  81. $attrib =& $attributes[$a1];
  82. switch($attrib->name){
  83. case 'identifier':
  84. $this->identifier = $attrib->value;
  85. break;
  86. case 'type':
  87. if(!empty($attrib->value)){
  88. $this->type = $attrib->value;
  89. }
  90. break;
  91. case 'scormtype':
  92. if(!empty($attrib->value)){
  93. $this->scormtype = $attrib->value;
  94. }
  95. break;
  96. case 'base':
  97. $this->base = $attrib->value;
  98. break;
  99. case 'href':
  100. $this->href = $attrib->value;
  101. break;
  102. }
  103. }
  104. }
  105. return true;
  106. }
  107. }elseif($v == 5){
  108. //parsing using PHP5 DOMXML methods
  109. switch($type){
  110. case 'db':
  111. //TODO implement this way of metadata object creation
  112. return false;
  113. case 'manifest': //do the same as the default
  114. default:
  115. //if($first_item->type == XML_ELEMENT_NODE) this is already check prior to the call to this function
  116. $children = $element->childNodes;
  117. if(is_array($children)){
  118. foreach($children as $child)
  119. {
  120. switch($child->nodeType)
  121. {
  122. case XML_ELEMENT_NODE:
  123. switch($child->tagName){
  124. case 'file':
  125. //echo "Child is a file tag<br />\n";
  126. $this->files[] = $child->getAttribute('href');
  127. break;
  128. case 'metadata':
  129. //echo "Child is a metadata tag<br />\n";
  130. $this->metadata = new scormMetadata('manifest',$child);
  131. break;
  132. case 'dependency':
  133. //need to get identifierref attribute inside dependency node
  134. //dependencies[] array represents all <dependency identifierref='x'> tags united
  135. $this->dependencies[] = $child->getAttribute('identifierref');
  136. break;
  137. }
  138. break;
  139. }
  140. }
  141. }
  142. //$keep_href = '';
  143. if($element->hasAttributes()){ //in some cases we get here with an empty attributes array
  144. //TODO find when and why we get such a case (empty array)
  145. $attributes = $element->attributes;
  146. foreach($attributes as $attrib)
  147. {
  148. switch($attrib->name){
  149. case 'identifier':
  150. $this->identifier = $attrib->value;
  151. break;
  152. case 'type':
  153. if(!empty($attrib->value)){
  154. $this->type = $attrib->value;
  155. }
  156. break;
  157. case 'scormtype':
  158. if(!empty($attrib->value)){
  159. $this->scormtype = $attrib->value;
  160. }
  161. break;
  162. case 'base':
  163. $this->base = $attrib->value;
  164. break;
  165. case 'href':
  166. $this->href = $attrib->value;
  167. break;
  168. }
  169. }
  170. }
  171. return true;
  172. }
  173. }else{
  174. //cannot parse because not PHP4 nor PHP5... We should not even be here anyway...
  175. return false;
  176. }
  177. }
  178. return false;
  179. }
  180. /**
  181. * Path getter
  182. * @return string Path for this resource
  183. */
  184. function get_path()
  185. {
  186. if(!empty($this->href))
  187. {
  188. require_once('learnpath.class.php');
  189. return learnpath::escape_string($this->href);
  190. }else{
  191. return '';
  192. }
  193. }
  194. /**
  195. * Scorm type getter
  196. * @return string generally 'asset' or 'sco' as these are the only two values defined in SCORM 1.2
  197. */
  198. function get_scorm_type()
  199. {
  200. if(!empty($this->scormtype)){
  201. require_once('learnpath.class.php');
  202. return learnpath::escape_string($this->scormtype);
  203. }else{
  204. return '';
  205. }
  206. }
  207. }
  208. ?>