scormItem.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php //$id:$
  2. /**
  3. * Container for the scormItem class that deals with <item> elements in an imsmanifest file
  4. * @package dokeos.learnpath.scorm
  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 $max_time_allowed = ''; //should be something like HHHH:MM:SS.SS
  21. var $timelimitaction = '';
  22. var $datafromlms = '';
  23. var $mastery_score = '';
  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->max_time_allowed = $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 'datafromlms':
  82. $tmp_children = $child->children();
  83. if(count($tmp_children)==1 and $tmp_children[0]->content!='' )
  84. {
  85. $this->datafromlms = $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->mastery_score = $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 'max_score':
  160. if($tmp_children->length==1 and $child->firstChild->nodeValue!='' ) {
  161. $this->max_score = $child->firstChild->nodeValue;
  162. }
  163. break;
  164. case 'maxtimeallowed':
  165. case 'adlcp:maxtimeallowed':
  166. $tmp_children = $child->childNodes;
  167. //if(count($tmp_children)==1 and $tmp_children[0]->textContent!='' )
  168. if($tmp_children->length==1 and $child->firstChild->nodeValue!='' )
  169. {
  170. $this->max_time_allowed = $child->firstChild->nodeValue;
  171. }
  172. break;
  173. case 'prerequisites':
  174. case 'adlcp:prerequisites':
  175. $tmp_children = $child->childNodes;
  176. //if(count($tmp_children)==1 and $tmp_children[0]->textContent!='' )
  177. if($tmp_children->length==1 and $child->firstChild->nodeValue!='' )
  178. {
  179. $this->prereq_string = $child->firstChild->nodeValue;
  180. }
  181. break;
  182. case 'timelimitaction':
  183. case 'adlcp:timelimitaction':
  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->timelimitaction = $child->firstChild->nodeValue;
  189. }
  190. break;
  191. case 'datafromlms':
  192. case 'adlcp:datafromlms':
  193. case 'adlcp:launchdata': //in some cases (Wouters)
  194. $tmp_children = $child->childNodes;
  195. //if(count($tmp_children)==1 and $tmp_children[0]->textContent!='' )
  196. if($tmp_children->length==1 and $child->firstChild->nodeValue!='' )
  197. {
  198. $this->datafromlms = $child->firstChild->nodeValue;
  199. }
  200. break;
  201. case 'masteryscore':
  202. case 'adlcp:masteryscore':
  203. $tmp_children = $child->childNodes;
  204. //if(count($tmp_children)==1 and $tmp_children[0]->textContent!='' )
  205. if($tmp_children->length==1 and $child->firstChild->nodeValue!='' )
  206. {
  207. $this->mastery_score = $child->firstChild->nodeValue;
  208. }
  209. break;
  210. case 'item':
  211. $oItem = new scormItem('manifest',$child);
  212. if($oItem->identifier != ''){
  213. $this->sub_items[$oItem->identifier] = $oItem;
  214. }
  215. break;
  216. case 'metadata':
  217. $this->metadata = new scormMetadata('manifest',$child);
  218. break;
  219. }
  220. break;
  221. case XML_TEXT_NODE:
  222. //this case is actually treated by looking into ELEMENT_NODEs above
  223. break;
  224. }
  225. }
  226. if($element->hasAttributes()){
  227. $attributes = $element->attributes;
  228. //$keep_href = '';
  229. foreach($attributes as $attrib)
  230. {
  231. switch($attrib->name){
  232. case 'identifier':
  233. $this->identifier = $attrib->value;
  234. break;
  235. case 'identifierref':
  236. $this->identifierref = $attrib->value;
  237. break;
  238. case 'isvisible':
  239. $this->isvisible = $attrib->value;
  240. break;
  241. case 'parameters':
  242. $this->parameters = $attrib->value;
  243. break;
  244. }
  245. }
  246. }
  247. return true;
  248. }
  249. }else{
  250. //cannot parse because not PHP4 nor PHP5... We should not even be here anyway...
  251. return false;
  252. }
  253. }
  254. return false;
  255. }
  256. /**
  257. * Builds a flat list with the current item and calls itself recursively on all children
  258. * @param array Reference to the array to complete with the current item
  259. * @param integer Optional absolute order (pointer) of the item in this learning path
  260. * @param integer Optional relative order of the item at this level
  261. * @param integer Optional level. If not given, assumes it's level 0
  262. */
  263. function get_flat_list(&$list,&$abs_order,$rel_order=1,$level=0)
  264. {
  265. $list[] = array(
  266. 'abs_order' => $abs_order,
  267. 'datafromlms' => $this->datafromlms,
  268. 'identifier' => $this->identifier,
  269. 'identifierref' => $this->identifierref,
  270. 'isvisible' => $this->isvisible,
  271. 'level' => $level,
  272. 'masteryscore' => $this->mastery_score,
  273. 'maxtimeallowed' => $this->max_time_allowed,
  274. 'metadata' => $this->metadata,
  275. 'parameters' => $this->parameters,
  276. 'prerequisites' => (!empty($this->prereq_string)?$this->prereq_string:''),
  277. 'rel_order' => $rel_order,
  278. 'timelimitaction' => $this->timelimitaction,
  279. 'title' => $this->title,
  280. 'max_score' => $this->max_score
  281. );
  282. $abs_order++;
  283. $i = 1;
  284. foreach($this->sub_items as $id => $dummy)
  285. {
  286. $oSubitem =& $this->sub_items[$id];
  287. $oSubitem->get_flat_list($list,$abs_order,$i,$level+1);
  288. $i++;
  289. }
  290. }
  291. /**
  292. * Save function. Uses the parent save function and adds a layer for SCORM.
  293. * @param boolean Save from URL params (1) or from object attributes (0)
  294. */
  295. function save($from_outside=true,$prereqs_complete=false)
  296. {
  297. parent::save($from_outside,$prereqs_complete);
  298. //under certain conditions, the scorm_contact should not be set, because no scorm signal was sent
  299. $this->scorm_contact = true;
  300. if(!$this->scorm_contact){
  301. //error_log('New LP - was expecting SCORM message but none received',0);
  302. }
  303. }
  304. }
  305. ?>