scormItem.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php //$id:$
  2. /**
  3. * Container for the scormItem class that deals with <item> elements in an imsmanifest file
  4. * @package dokeos.learnpath
  5. * @author Yannick Warnier <ywarnier@beeznest.org>
  6. */
  7. /**
  8. * This class handles the <item> elements from an imsmanifest file.
  9. */
  10. require_once('learnpathItem.class.php');
  11. class scormItem extends learnpathItem{
  12. var $identifier = '';
  13. var $identifierref = '';
  14. var $isvisible = '';
  15. var $parameters = '';
  16. var $title = '';
  17. var $sub_items = array();
  18. var $metadata;
  19. //var $prerequisites = ''; - defined in learnpathItem.class.php
  20. var $maxtimeallowed = '00:00:00';
  21. var $timelimitaction = '';
  22. var $datafromims = '';
  23. var $masteryscore = '';
  24. var $scorm_contact;
  25. /**
  26. * Class constructor. Depending of the type of construction called ('db' or 'manifest'), will create a scormItem
  27. * object from database records or from the DOM element given as parameter
  28. * @param string Type of construction needed ('db' or 'manifest', default = 'manifest')
  29. * @param mixed Depending on the type given, DB id for the lp_item or reference to the DOM element
  30. */
  31. function scormItem($type='manifest',&$element) {
  32. if(isset($element))
  33. {
  34. $v = substr(phpversion(),0,1);
  35. if($v == 4){
  36. switch($type){
  37. case 'db':
  38. parent::learnpathItem($element,api_get_user_id());
  39. $this->scorm_contact = false;
  40. //TODO implement this way of metadata object creation
  41. return false;
  42. case 'manifest': //do the same as the default
  43. default:
  44. //if($first_item->type == XML_ELEMENT_NODE) this is already check prior to the call to this function
  45. $children = $element->children();
  46. foreach($children as $a => $dummy)
  47. {
  48. $child =& $children[$a];
  49. switch($child->type)
  50. {
  51. case XML_ELEMENT_NODE:
  52. switch($child->tagname){
  53. case 'title':
  54. $tmp_children = $child->children();
  55. if(count($tmp_children)==1 and $tmp_children[0]->content!='' )
  56. {
  57. $this->title = $tmp_children[0]->content;
  58. }
  59. break;
  60. case 'maxtimeallowed':
  61. $tmp_children = $child->children();
  62. if(count($tmp_children)==1 and $tmp_children[0]->content!='' )
  63. {
  64. $this->maxtimeallowed = $tmp_children[0]->content;
  65. }
  66. break;
  67. case 'prerequisites':
  68. $tmp_children = $child->children();
  69. if(count($tmp_children)==1 and $tmp_children[0]->content!='' )
  70. {
  71. $this->prereq_string = $tmp_children[0]->content;
  72. }
  73. break;
  74. case 'timelimitaction':
  75. $tmp_children = $child->children();
  76. if(count($tmp_children)==1 and $tmp_children[0]->content!='' )
  77. {
  78. $this->timelimitaction = $tmp_children[0]->content;
  79. }
  80. break;
  81. case 'datafromims':
  82. $tmp_children = $child->children();
  83. if(count($tmp_children)==1 and $tmp_children[0]->content!='' )
  84. {
  85. $this->datafromims = $tmp_children[0]->content;
  86. }
  87. break;
  88. case 'masteryscore':
  89. $tmp_children = $child->children();
  90. if(count($tmp_children)==1 and $tmp_children[0]->content!='' )
  91. {
  92. $this->masteryscore = $tmp_children[0]->content;
  93. }
  94. break;
  95. case 'item':
  96. $oItem = new scormItem('manifest',$child);
  97. if($oItem->identifier != ''){
  98. $this->sub_items[$oItem->identifier] = $oItem;
  99. }
  100. break;
  101. case 'metadata':
  102. $this->metadata = new scormMetadata('manifest',$child);
  103. break;
  104. }
  105. break;
  106. case XML_TEXT_NODE:
  107. //this case is actually treated by looking into ELEMENT_NODEs above
  108. break;
  109. }
  110. }
  111. $attributes = $element->attributes();
  112. //$keep_href = '';
  113. foreach($attributes as $a1 => $dummy)
  114. {
  115. $attrib =& $attributes[$a1];
  116. switch($attrib->name){
  117. case 'identifier':
  118. $this->identifier = $attrib->value;
  119. break;
  120. case 'identifierref':
  121. $this->identifierref = $attrib->value;
  122. break;
  123. case 'isvisible':
  124. $this->isvisible = $attrib->value;
  125. break;
  126. case 'parameters':
  127. $this->parameters = $attrib->value;
  128. break;
  129. }
  130. }
  131. return true;
  132. }
  133. }elseif($v == 5){
  134. //parsing using PHP5 DOMXML methods
  135. switch($type){
  136. case 'db':
  137. parent::learnpathItem($element,api_get_user_id());
  138. $this->scorm_contact = false;
  139. //TODO implement this way of metadata object creation
  140. return false;
  141. case 'manifest': //do the same as the default
  142. default:
  143. //if($first_item->type == XML_ELEMENT_NODE) this is already check prior to the call to this function
  144. $children = $element->childNodes;
  145. foreach($children as $child)
  146. {
  147. switch($child->nodeType)
  148. {
  149. case XML_ELEMENT_NODE:
  150. switch($child->tagName){
  151. case 'title':
  152. $tmp_children = $child->childNodes;
  153. //if(count($tmp_children)==1 and $tmp_children[0]->textContent!='' )
  154. if($tmp_children->length==1 and $child->firstChild->nodeValue!='' )
  155. {
  156. $this->title = $child->firstChild->nodeValue;
  157. }
  158. break;
  159. case 'maxtimeallowed':
  160. $tmp_children = $child->childNodes;
  161. //if(count($tmp_children)==1 and $tmp_children[0]->textContent!='' )
  162. if($tmp_children->length==1 and $child->firstChild->nodeValue!='' )
  163. {
  164. $this->maxtimeallowed = $child->firstChild->nodeValue;
  165. }
  166. break;
  167. case 'prerequisites':
  168. $tmp_children = $child->childNodes;
  169. //if(count($tmp_children)==1 and $tmp_children[0]->textContent!='' )
  170. if($tmp_children->length==1 and $child->firstChild->nodeValue!='' )
  171. {
  172. $this->prereq_string = $child->firstChild->nodeValue;
  173. }
  174. break;
  175. case 'timelimitaction':
  176. $tmp_children = $child->childNodes;
  177. //if(count($tmp_children)==1 and $tmp_children[0]->textContent!='' )
  178. if($tmp_children->length==1 and $child->firstChild->nodeValue!='' )
  179. {
  180. $this->timelimitaction = $child->firstChild->nodeValue;
  181. }
  182. break;
  183. case 'datafromims':
  184. $tmp_children = $child->childNodes;
  185. //if(count($tmp_children)==1 and $tmp_children[0]->textContent!='' )
  186. if($tmp_children->length==1 and $child->firstChild->nodeValue!='' )
  187. {
  188. $this->datafromims = $child->firstChild->nodeValue;
  189. }
  190. break;
  191. case 'masteryscore':
  192. $tmp_children = $child->childNodes;
  193. //if(count($tmp_children)==1 and $tmp_children[0]->textContent!='' )
  194. if($tmp_children->length==1 and $child->firstChild->nodeValue!='' )
  195. {
  196. $this->masteryscore = $child->firstChild->nodeValue;
  197. }
  198. break;
  199. case 'item':
  200. $oItem = new scormItem('manifest',$child);
  201. if($oItem->identifier != ''){
  202. $this->sub_items[$oItem->identifier] = $oItem;
  203. }
  204. break;
  205. case 'metadata':
  206. $this->metadata = new scormMetadata('manifest',$child);
  207. break;
  208. }
  209. break;
  210. case XML_TEXT_NODE:
  211. //this case is actually treated by looking into ELEMENT_NODEs above
  212. break;
  213. }
  214. }
  215. if($element->hasAttributes()){
  216. $attributes = $element->attributes;
  217. //$keep_href = '';
  218. foreach($attributes as $attrib)
  219. {
  220. switch($attrib->name){
  221. case 'identifier':
  222. $this->identifier = $attrib->value;
  223. break;
  224. case 'identifierref':
  225. $this->identifierref = $attrib->value;
  226. break;
  227. case 'isvisible':
  228. $this->isvisible = $attrib->value;
  229. break;
  230. case 'parameters':
  231. $this->parameters = $attrib->value;
  232. break;
  233. }
  234. }
  235. }
  236. return true;
  237. }
  238. }else{
  239. //cannot parse because not PHP4 nor PHP5... We should not even be here anyway...
  240. return false;
  241. }
  242. }
  243. return false;
  244. }
  245. /**
  246. * Builds a flat list with the current item and calls itself recursively on all children
  247. * @param array Reference to the array to complete with the current item
  248. * @param integer Optional absolute order (pointer) of the item in this learning path
  249. * @param integer Optional relative order of the item at this level
  250. * @param integer Optional level. If not given, assumes it's level 0
  251. */
  252. function get_flat_list(&$list,&$abs_order,$rel_order=1,$level=0)
  253. {
  254. $list[] = array(
  255. 'abs_order' => $abs_order,
  256. 'datafromims' => $this->datafromims,
  257. 'identifier' => $this->identifier,
  258. 'identifierref' => $this->identifierref,
  259. 'isvisible' => $this->isvisible,
  260. 'level' => $level,
  261. 'masteryscore' => $this->masteryscore,
  262. 'maxtimeallowed' => $this->maxtimeallowed,
  263. 'metadata' => $this->metadata,
  264. 'parameters' => $this->parameters,
  265. 'prerequisites' => (!empty($this->prereq_string)?$this->prereq_string:''),
  266. 'rel_order' => $rel_order,
  267. 'timelimitaction' => $this->timelimitaction,
  268. 'title' => $this->title
  269. );
  270. $abs_order++;
  271. $i = 1;
  272. foreach($this->sub_items as $id => $dummy)
  273. {
  274. $oSubitem =& $this->sub_items[$id];
  275. $oSubitem->get_flat_list($list,$abs_order,$i,$level+1);
  276. $i++;
  277. }
  278. }
  279. /**
  280. * Save function. Uses the parent save function and adds a layer for SCORM.
  281. * @param boolean Save from URL params (1) or from object attributes (0)
  282. */
  283. function save($from_outside=true)
  284. {
  285. parent::save($from_outside);
  286. //under certain conditions, the scorm_contact should not be set, because no scorm signal was sent
  287. $this->scorm_contact = true;
  288. if(!$this->scorm_contact){
  289. //error_log('New LP - was expecting SCORM message but none received',0);
  290. }
  291. }
  292. }
  293. ?>