scormResource.class.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php //$id:$
  2. /**
  3. * Container for the scormResource class
  4. * @package scorm.learnpath
  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 = 'asset';
  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. foreach($children as $child)
  118. {
  119. switch($child->nodeType)
  120. {
  121. case XML_ELEMENT_NODE:
  122. switch($child->tagName){
  123. case 'file':
  124. //echo "Child is a file tag<br />\n";
  125. $this->files[] = $child->getAttribute('href');
  126. break;
  127. case 'metadata':
  128. //echo "Child is a metadata tag<br />\n";
  129. $this->metadata = new scormMetadata('manifest',$child);
  130. break;
  131. case 'dependency':
  132. //need to get identifierref attribute inside dependency node
  133. //dependencies[] array represents all <dependency identifierref='x'> tags united
  134. $this->dependencies[] = $child->getAttribute('identifierref');
  135. break;
  136. }
  137. break;
  138. }
  139. }
  140. //$keep_href = '';
  141. if($element->hasAttributes()){ //in some cases we get here with an empty attributes array
  142. //TODO find when and why we get such a case (empty array)
  143. $attributes = $element->attributes;
  144. foreach($attributes as $attrib)
  145. {
  146. switch($attrib->name){
  147. case 'identifier':
  148. $this->identifier = $attrib->value;
  149. break;
  150. case 'type':
  151. if(!empty($attrib->value)){
  152. $this->type = $attrib->value;
  153. }
  154. break;
  155. case 'scormtype':
  156. if(!empty($attrib->value)){
  157. $this->scormtype = $attrib->value;
  158. }
  159. break;
  160. case 'base':
  161. $this->base = $attrib->value;
  162. break;
  163. case 'href':
  164. $this->href = $attrib->value;
  165. break;
  166. }
  167. }
  168. }
  169. return true;
  170. }
  171. }else{
  172. //cannot parse because not PHP4 nor PHP5... We should not even be here anyway...
  173. return false;
  174. }
  175. }
  176. return false;
  177. }
  178. /**
  179. * Path getter
  180. * @return string Path for this resource
  181. */
  182. function get_path()
  183. {
  184. if(!empty($this->href))
  185. {
  186. require_once('learnpath.class.php');
  187. return learnpath::escape_string($this->href);
  188. }else{
  189. return '';
  190. }
  191. }
  192. /**
  193. * Scorm type getter
  194. * @return string generally 'asset' or 'sco' as these are the only two values defined in SCORM 1.2
  195. */
  196. function get_scorm_type()
  197. {
  198. if(!empty($this->scormtype)){
  199. require_once('learnpath.class.php');
  200. return learnpath::escape_string($this->scormtype);
  201. }else{
  202. return '';
  203. }
  204. }
  205. }
  206. ?>