learnpathItem.class.php 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182
  1. <?php //$id:$
  2. /**
  3. * This file contains the lp_item class, that inherits from the learnpath class
  4. * @package dokeos.learnpath
  5. * @author Yannick Warnier <ywarnier@beeznest.org>
  6. * @license GNU/GPL - See Dokeos license directory for details
  7. */
  8. /**
  9. * lp_item defines items belonging to a learnpath. Each item has a name, a score, a use time and additional
  10. * information that enables tracking a user's progress in a learning path
  11. * @package dokeos.learnpath
  12. */
  13. class learnpathItem{
  14. var $attempt_id; //also called "objectives" SCORM-wise
  15. var $children = array(); //contains the ids of children items
  16. var $condition; //if this item has a special condition embedded
  17. var $current_score;
  18. var $current_start_time;
  19. var $current_stop_time;
  20. var $current_data = '';
  21. var $db_id;
  22. var $db_item_view_id = '';
  23. var $description = '';
  24. var $file;
  25. //at the moment, interactions are just an array of arrays with a structure of 8 text fields
  26. //id(0), type(1), time(2), weighting(3),correct_responses(4),student_response(5),result(6),latency(7)
  27. var $interactions = array();
  28. var $interactions_count = 0;
  29. var $objectives = array();
  30. var $objectives_count = 0;
  31. var $launch_data = '';
  32. var $lesson_location = '';
  33. var $level = 0;
  34. //var $location; //only set this for SCORM?
  35. var $lp_id;
  36. var $max_score;
  37. var $mastery_score;
  38. var $min_score;
  39. var $max_time_allowed = '';
  40. var $name;
  41. var $next;
  42. var $parent;
  43. var $path;
  44. var $possible_status = array('not attempted','incomplete','completed','passed','failed','browsed');
  45. var $prereq_string = '';
  46. var $prereq_alert = '';
  47. var $prereqs = array();
  48. var $previous;
  49. var $prevent_reinit = 1;
  50. var $ref;
  51. var $save_on_close = true;
  52. var $status;
  53. var $title;
  54. var $type; // this attribute can contain chapter|link|student_publication|module|quiz|document|forum|thread
  55. var $view_id;
  56. var $debug = 0; //logging param
  57. /**
  58. * Class constructor. Prepares the learnpath_item for later launch
  59. *
  60. * Don't forget to use set_lp_view() if applicable after creating the item.
  61. * Setting an lp_view will finalise the item_view data collection
  62. * @param integer Learnpath item ID
  63. * @param integer User ID
  64. * @return boolean True on success, false on failure
  65. */
  66. function learnpathItem($db_id, $user_id) {
  67. //get items table
  68. if($this->debug>0){error_log('New LP - In learnpathItem constructor: '.$db_id.','.$user_id,0);}
  69. $items_table = Database::get_course_table('lp_item');
  70. $id = (int) $db_id;
  71. $sql = "SELECT * FROM $items_table WHERE id = $id";
  72. //error_log('New LP - Creating item object from DB: '.$sql,0);
  73. $res = @api_sql_query($sql);
  74. if(Database::num_rows($res)<1)
  75. {
  76. $this->error = "Could not find given learnpath item in learnpath_item table";
  77. //error_log('New LP - '.$this->error,0);
  78. return false;
  79. }
  80. $row = Database::fetch_array($res);
  81. $this->lp_id = $row['lp_id'];
  82. $this->max_score = $row['max_score'];
  83. $this->min_score = $row['min_score'];
  84. $this->name = $row['title'];
  85. $this->type = $row['item_type'];
  86. $this->ref = $row['ref'];
  87. $this->title = $row['title'];
  88. $this->description = $row['description'];
  89. $this->path = $row['path'];
  90. $this->mastery_score = $row['mastery_score'];
  91. $this->parent = $row['parent_item_id'];
  92. $this->next = $row['next_item_id'];
  93. $this->previous = $row['previous_item_id'];
  94. $this->display_order = $row['display_order'];
  95. $this->prereq_string = $row['prerequisite'];
  96. $this->max_time_allowed = $row['max_time_allowed'];
  97. if(isset($row['launch_data'])){
  98. $this->launch_data = $row['launch_data'];
  99. }
  100. $this->save_on_close = true;
  101. $this->db_id = $id;
  102. //error_log('New LP - End of learnpathItem constructor for item '.$id,0);
  103. return true;
  104. }
  105. /**
  106. * Adds a child to the current item
  107. */
  108. function add_child($item)
  109. {
  110. if($this->debug>0){error_log('New LP - In learnpathItem::add_child()',0);}
  111. if(!empty($item))
  112. {
  113. //do not check in DB as we expect the call to come from the learnpath class which should
  114. //be aware of any fake
  115. $this->children[] = $item;
  116. }
  117. }
  118. /**
  119. * Adds an interaction to the current item
  120. * @param int Index (order ID) of the interaction inside this item
  121. * @param array Array of parameters: id(0), type(1), time(2), weighting(3),correct_responses(4),student_response(5),result(6),latency(7)
  122. * @result void
  123. */
  124. function add_interaction($index,$params)
  125. {
  126. $this->interactions[$index] = $params;
  127. //take the current maximum index to generate the interactions_count
  128. if(($index+1)>$this->interactions_count){
  129. $this->interactions_count = $index+1;
  130. }
  131. /*
  132. if(is_array($this->interactions[$index]) && count($this->interactions[$index])>0){
  133. $this->interactions[$index] = $params;
  134. return false;
  135. }else{
  136. if(count($params)==8){//we rely on the calling script to provide parameters in the right order
  137. $this->interactions[$index] = $params;
  138. return true;
  139. }else{
  140. return false;
  141. }
  142. }
  143. */
  144. }
  145. /**
  146. * Adds an objective to the current item
  147. * @param array Array of parameters: id(0), status(1), score_raw(2), score_max(3), score_min(4)
  148. * @result void
  149. */
  150. function add_objective($index,$params)
  151. {
  152. if(empty($params[0])){return null;}
  153. $this->objectives[$index] = $params;
  154. //take the current maximum index to generate the objectives_count
  155. if((count($this->objectives)+1)>$this->objectives_count){
  156. $this->objectives_count = (count($this->objectives)+1);
  157. }
  158. }
  159. /**
  160. * Closes/stops the item viewing. Finalises runtime values. If required, save to DB.
  161. * @return boolean True on success, false otherwise
  162. */
  163. function close()
  164. {
  165. if($this->debug>0){error_log('New LP - In learnpathItem::close()',0);}
  166. $this->current_stop_time = time();
  167. $type = $this->get_type();
  168. if($type != 'sco'){
  169. if($type == TOOL_QUIZ or $type == TOOL_HOTPOTATOES)
  170. {
  171. $this->get_status(true,true);//update status (second option forces the update)
  172. }
  173. else
  174. {
  175. $this->status = $this->possible_status[2];
  176. }
  177. }
  178. if($this->save_on_close)
  179. {
  180. $this->save();
  181. }
  182. return true;
  183. }
  184. /**
  185. * Deletes all traces of this item in the database
  186. * @return boolean true. Doesn't check for errors yet.
  187. */
  188. function delete()
  189. {
  190. if($this->debug>0){error_log('New LP - In learnpath_item::delete() for item '.$this->db_id,0);}
  191. $lp_item_view = Database::get_course_table('lp_item_view');
  192. $lp_item = Database::get_course_table('lp_item');
  193. $sql_del_view = "DELETE FROM $lp_item_view WHERE item_id = ".$this->db_id;
  194. //error_log('New LP - Deleting from lp_item_view: '.$sql_del_view,0);
  195. $res_del_view = api_sql_query($sql_del_view);
  196. $sql_del_item = "DELETE FROM $lp_item WHERE id = ".$this->db_id;
  197. //error_log('New LP - Deleting from lp_item: '.$sql_del_view,0);
  198. $res_del_item = api_sql_query($sql_del_item);
  199. return true;
  200. }
  201. /**
  202. * Drops a child from the children array
  203. * @param string index of child item to drop
  204. * @return void
  205. */
  206. function drop_child($item)
  207. {
  208. if($this->debug>0){error_log('New LP - In learnpathItem::drop_child()',0);}
  209. if(!empty($item))
  210. {
  211. foreach($this->children as $index => $child)
  212. {
  213. if($child == $item){
  214. $this->children[$index] = null;
  215. }
  216. }
  217. }
  218. }
  219. /**
  220. * Gets the current attempt_id for this user on this item
  221. * @return integer The attempt_id for this item view by this user, or 1 if none defined
  222. */
  223. function get_attempt_id()
  224. {
  225. if($this->debug>0){error_log('New LP - In learnpathItem::get_attempt_id() on item '.$this->db_id,0);}
  226. $res = 1;
  227. if(!empty($this->attempt_id))
  228. {
  229. $res = $this->attempt_id;
  230. }
  231. if($this->debug>0){error_log('New LP - End of learnpathItem::get_attempt_id() on item '.$this->db_id.' - Returning '.$res,0);}
  232. return $res;
  233. }
  234. /**
  235. * Gets a list of the item's children
  236. * @return array Array of children items IDs
  237. */
  238. function get_children()
  239. {
  240. if($this->debug>0){error_log('New LP - In learnpathItem::get_children()',0);}
  241. $list = array();
  242. foreach($this->children as $child){
  243. if(!empty($child))
  244. {
  245. //error_log('New LP - Found '.$child,0);
  246. $list[] = $child;
  247. }
  248. }
  249. return $list;
  250. }
  251. /**
  252. * Gets the core_exit value from the database
  253. */
  254. function get_core_exit()
  255. {
  256. return $this->core_exit;
  257. }
  258. /**
  259. * Gets the credit information (rather scorm-stuff) based on current status and reinit
  260. * autorization. Credit tells the sco(content) if Dokeos will record the data it is sent (credit) or not (no-credit)
  261. * @return string 'credit' or 'no-credit'. Defaults to 'credit' because if we don't know enough about this item, it's probably because it was never used before.
  262. */
  263. function get_credit(){
  264. if(!empty($this->debug) && $this->debug>1){error_log('New LP - In learnpathItem::get_credit()',0);}
  265. $credit = 'credit';
  266. //now check the value of prevent_reinit (if it's 0, return credit as the default was)
  267. if($this->get_prevent_reinit() != 0){ //if prevent_reinit == 1 (or more)
  268. //if status is not attempted or incomplete, credit anyway. Otherwise:
  269. //check the status in the database rather than in the object, as checking in the object
  270. //would always return "no-credit" when we want to set it to completed
  271. $status = $this->get_status(true);
  272. if(!empty($this->debug) && $this->debug>2){error_log('New LP - In learnpathItem::get_credit() - get_prevent_reinit!=0 and status is '.$status,0);}
  273. if($status != $this->possible_status[0] AND $status != $this->possible_status[1]){
  274. $credit = 'no-credit';
  275. }
  276. }
  277. return $credit;
  278. }
  279. /**
  280. * Gets the current start time property
  281. * @return integer Current start time, or current time if none
  282. */
  283. function get_current_start_time()
  284. {
  285. if($this->debug>0){error_log('New LP - In learnpathItem::get_current_start_time()',0);}
  286. if(empty($this->current_start_time))
  287. {
  288. return time();
  289. }else{
  290. return $this->current_start_time;
  291. }
  292. }
  293. /**
  294. * Gets the item's description
  295. * @return string Description
  296. */
  297. function get_description(){
  298. if($this->debug>0){error_log('New LP - In learnpathItem::get_description()',0);}
  299. if(empty($this->description)){return '';}
  300. return $this->description;
  301. }
  302. /**
  303. * Gets the file path from the course's root directory, no matter what tool it is from.
  304. * @return string The file path, or an empty string if there is no file attached, or '-1' if the file must be replaced by an error page
  305. */
  306. function get_file_path($path_to_scorm_dir=''){
  307. if($this->debug>0){error_log('New LP - In learnpathItem::get_file_path()',0);}
  308. $path = $this->get_path();
  309. $type = $this->get_type();
  310. if(empty($path))
  311. {
  312. if($type == 'dokeos_chapter' OR $type=='chapter' OR $type == 'dir')
  313. {
  314. return '';
  315. }
  316. else
  317. {
  318. return '-1';
  319. }
  320. }elseif($path == strval(intval($path))){
  321. //the path is numeric, so it is a reference to a Dokeos object
  322. switch($type)
  323. {
  324. case 'dokeos_chapter':
  325. case 'dir':
  326. case 'chapter':
  327. return '';
  328. case TOOL_DOCUMENT:
  329. $table_doc = Database::get_course_table(TABLE_DOCUMENT);
  330. $sql = 'SELECT path FROM '.$table_doc.' WHERE id = '.$path;
  331. $res = api_sql_query($sql,__FILE__,__LINE__);
  332. $row = Database::fetch_array($res);
  333. $real_path = 'document'.$row['path'];
  334. return $real_path;
  335. case TOOL_STUDENTPUBLICATION:
  336. case TOOL_QUIZ:
  337. case TOOL_FORUM:
  338. case TOOL_THREAD:
  339. case TOOL_LINK:
  340. default:
  341. return '-1';
  342. }
  343. }else{
  344. if(!empty($path_to_scorm_dir))
  345. {
  346. $path = $path_to_scorm_dir.$path;
  347. }
  348. return $path;
  349. }
  350. }
  351. /**
  352. * Gets the DB ID
  353. * @return integer Database ID for the current item
  354. */
  355. function get_id(){
  356. if($this->debug>1){error_log('New LP - In learnpathItem::get_id()',0);}
  357. if(!empty($this->db_id))
  358. {
  359. return $this->db_id;
  360. }
  361. //TODO check this return value is valid for children classes (SCORM?)
  362. return 0;
  363. }
  364. /**
  365. * Gets the current count of interactions recorded in the database
  366. * @return int The current number of interactions recorder
  367. */
  368. function get_interactions_count()
  369. {
  370. if($this->debug>1){error_log('New LP - In learnpathItem::get_interactions_count()',0);}
  371. $res = 0;
  372. if(!empty($this->interactions_count)){
  373. $res = $this->interactions_count;
  374. }
  375. return $res;
  376. }
  377. /**
  378. * Gets the current count of objectives recorded in the database
  379. * @return int The current number of objectives recorder
  380. */
  381. function get_objectives_count()
  382. {
  383. if($this->debug>1){error_log('New LP - In learnpathItem::get_objectives_count()',0);}
  384. $res = 0;
  385. if(!empty($this->objectives_count)){
  386. $res = $this->objectives_count;
  387. }
  388. return $res;
  389. }
  390. /**
  391. * Gets the launch_data field found in imsmanifests (this is SCORM- or AICC-related, really)
  392. * @return string Launch data as found in imsmanifest and stored in Dokeos (read only). Defaults to ''.
  393. */
  394. function get_launch_data(){
  395. if($this->debug>0){error_log('New LP - In learnpathItem::get_launch_data()',0);}
  396. if(!empty($this->launch_data)){
  397. return $this->launch_data;
  398. }
  399. return '';
  400. }
  401. /**
  402. * Gets the lesson location
  403. * @return string lesson location as recorded by the SCORM and AICC elements. Defaults to ''
  404. */
  405. function get_lesson_location(){
  406. if($this->debug>0){error_log('New LP - In learnpathItem::get_lesson_location()',0);}
  407. if(!empty($this->lesson_location)){return $this->lesson_location;}else{return '';}
  408. }
  409. /**
  410. * Gets the lesson_mode (scorm feature, but might be used by aicc as well as dokeos paths)
  411. *
  412. * The "browse" mode is not supported yet (because there is no such way of seeing a sco in Dokeos)
  413. * @return string 'browse','normal' or 'review'. Defaults to 'normal'
  414. */
  415. function get_lesson_mode(){
  416. $mode = 'normal';
  417. if($this->get_prevent_reinit() != 0){ //if prevent_reinit == 0
  418. $my_status = $this->get_status();
  419. if($my_status != $this->possible_status[0] AND $my_status != $this->possible_status[1]){
  420. $mode = 'review';
  421. }
  422. }
  423. return $mode;
  424. }
  425. /**
  426. * Gets the depth level
  427. * @return int Level. Defaults to 0
  428. */
  429. function get_level(){
  430. if($this->debug>0){error_log('New LP - In learnpathItem::get_level()',0);}
  431. if(empty($this->level)){return 0;}
  432. return $this->level;
  433. }
  434. /**
  435. * Gets the mastery score
  436. */
  437. function get_mastery_score()
  438. {
  439. if($this->debug>0){error_log('New LP - In learnpathItem::get_mastery_score()',0);}
  440. if(isset($this->mastery_score)){return $this->mastery_score;}else{return -1;}
  441. }
  442. /**
  443. * Gets the maximum (score)
  444. * @return int Maximum score. Defaults to 100 if nothing else is defined
  445. */
  446. function get_max(){
  447. if($this->debug>0){error_log('New LP - In learnpathItem::get_max()',0);}
  448. if($this->type == 'sco')
  449. {
  450. if(!empty($this->view_max_score) and $this->view_max_score>0)
  451. {
  452. return $this->view_max_score;
  453. }
  454. elseif($this->view_max_score === '')
  455. {
  456. return $this->view_max_score;
  457. }
  458. else
  459. {
  460. if(!empty($this->max_score)){return $this->max_score;}else{return 100;}
  461. }
  462. }
  463. else
  464. {
  465. if(!empty($this->max_score)){return $this->max_score;}else{return 100;}
  466. }
  467. }
  468. /**
  469. * Gets the maximum time allowed for this user in this attempt on this item
  470. * @return string Time string in SCORM format (HH:MM:SS or HH:MM:SS.SS or HHHH:MM:SS.SS)
  471. */
  472. function get_max_time_allowed()
  473. {
  474. if($this->debug>0){error_log('New LP - In learnpathItem::get_max_time_allowed()',0);}
  475. if(!empty($this->max_time_allowed)){return $this->max_time_allowed;}else{return '';}
  476. }
  477. /**
  478. * Gets the minimum (score)
  479. * @return int Minimum score. Defaults to 0
  480. */
  481. function get_min(){
  482. if($this->debug>0){error_log('New LP - In learnpathItem::get_min()',0);}
  483. if(!empty($this->min_score)){return $this->min_score;}else{return 0;}
  484. }
  485. /**
  486. * Gets the parent ID
  487. * @return int Parent ID. Defaults to null
  488. */
  489. function get_parent(){
  490. if($this->debug>0){error_log('New LP - In learnpathItem::get_parent()',0);}
  491. if(!empty($this->parent))
  492. {
  493. return $this->parent;
  494. }
  495. //TODO check this return value is valid for children classes (SCORM?)
  496. return null;
  497. }
  498. /**
  499. * Gets the path attribute.
  500. * @return string Path. Defaults to ''
  501. */
  502. function get_path(){
  503. if($this->debug>0){error_log('New LP - In learnpathItem::get_path()',0);}
  504. if(empty($this->path)){return '';}
  505. return $this->path;
  506. }
  507. /**
  508. * Gets the prerequisites string
  509. * @return string Empty string or prerequisites string if defined. Defaults to
  510. */
  511. function get_prereq_string()
  512. {
  513. if($this->debug>0){error_log('New LP - In learnpathItem::get_prereq_string()',0);}
  514. if(!empty($this->prereq_string))
  515. {
  516. return $this->prereq_string;
  517. }else{
  518. return '';
  519. }
  520. }
  521. /**
  522. * Gets the prevent_reinit attribute value (and sets it if not set already)
  523. * @return int 1 or 0 (defaults to 1)
  524. */
  525. function get_prevent_reinit(){
  526. if($this->debug>2){error_log('New LP - In learnpathItem::get_prevent_reinit()',0);}
  527. if(!isset($this->prevent_reinit)){
  528. if(!empty($this->lp_id)){
  529. $db = Database::get_course_table('lp');
  530. $sql = "SELECT * FROM $db WHERE id = ".$this->lp_id;
  531. $res = @api_sql_query($sql);
  532. if(Database::num_rows($res)<1)
  533. {
  534. $this->error = "Could not find parent learnpath in learnpath table";
  535. if($this->debug>2){error_log('New LP - End of learnpathItem::get_prevent_reinit() - Returning false',0);}
  536. return false;
  537. }else{
  538. $row = Database::fetch_array($res);
  539. $this->prevent_reinit = $row['prevent_reinit'];
  540. }
  541. }else{
  542. $this->prevent_reinit = 1;//prevent reinit is always 1 by default - see learnpath.class.php
  543. }
  544. }
  545. if($this->debug>2){error_log('New LP - End of learnpathItem::get_prevent_reinit() - Returned '.$this->prevent_reinit,0);}
  546. return $this->prevent_reinit;
  547. }
  548. /**
  549. * Gets the item's reference column
  550. * @return string The item's reference field (generally used for SCORM identifiers)
  551. */
  552. function get_ref()
  553. {
  554. return $this->ref;
  555. }
  556. /**
  557. * Gets the list of included resources as a list of absolute or relative paths of
  558. * resources included in the current item. This allows for a better SCORM export.
  559. * The list will generally include pictures, flash objects, java applets, or any other
  560. * stuff included in the source of the current item. The current item is expected
  561. * to be an HTML file. If it is not, then the function will return and empty list.
  562. * @param string type (one of the Dokeos tools) - optional (otherwise takes the current item's type)
  563. * @param string path (absolute file path) - optional (otherwise takes the current item's path)
  564. * @param int level of recursivity we're in
  565. * @return array List of file paths. An additional field containing 'local' or 'remote' helps determine if the file should be copied into the zip or just linked
  566. */
  567. function get_resources_from_source($type=null,$abs_path=null, $recursivity=1)
  568. {
  569. $max = 5;
  570. if($recursivity > $max)
  571. {
  572. return array();
  573. }
  574. if(!isset($type))
  575. {
  576. $type = $this->get_type();
  577. }
  578. if(!isset($abs_path))
  579. {
  580. $path = $this->get_file_path();
  581. $abs_path = api_get_path(SYS_COURSE_PATH).api_get_course_path().'/'.$path;
  582. //echo "Abs path coming from item : ".$abs_path."<br/>\n";
  583. }
  584. /*
  585. else
  586. {
  587. echo "Abs path coming from param: ".$abs_path."<br/>\n";
  588. }
  589. */
  590. //error_log(str_repeat(' ',$recursivity).'Analyse file '.$abs_path,0);
  591. $files_list = array();
  592. $type = $this->get_type();
  593. switch($type)
  594. {
  595. case TOOL_DOCUMENT :
  596. case TOOL_QUIZ:
  597. case 'sco':
  598. //get the document and, if HTML, open it
  599. if(is_file($abs_path))
  600. {
  601. //for now, read the whole file in one go (that's gonna be a problem when the file is too big)
  602. $info = pathinfo($abs_path);
  603. $ext = $info['extension'];
  604. switch(strtolower($ext))
  605. {
  606. case 'html':
  607. case 'htm':
  608. case 'shtml':
  609. case 'css':
  610. $wanted_attributes = array('src','url','@import','href','value');
  611. //parse it for included resources
  612. $file_content = file_get_contents($abs_path);
  613. //get an array of attributes from the HTML source
  614. $attributes = learnpathItem::parse_HTML_attributes($file_content,$wanted_attributes);
  615. //look at 'src' attributes in this file
  616. foreach($wanted_attributes as $attr)
  617. {
  618. if(isset($attributes[$attr]))
  619. {
  620. //find which kind of path these are (local or remote)
  621. $sources = $attributes[$attr];
  622. foreach($sources as $source)
  623. {
  624. //skip what is obviously not a resource
  625. if(strpos($source,"+this.")) continue; //javascript code - will still work unaltered
  626. if(strpos($source,'.')=== false) continue; //no dot, should not be an external file anyway
  627. if(strpos($source,'mailto:')) continue; //mailto link
  628. if(strpos($source,';') && !strpos($source,'&amp;')) continue; //avoid code - that should help
  629. if($attr == 'value')
  630. {
  631. if(strpos($source , 'mp3file'))
  632. {
  633. $files_list[] = array(substr($source, 0, strpos($source , '.swf')+4),'local','abs');
  634. $mp3file = substr($source , strpos($source , 'mp3file=')+8);
  635. if(substr($mp3file,0,1) == '/')
  636. $files_list[] = array($mp3file,'local','abs');
  637. else
  638. $files_list[] = array($mp3file,'local','rel');
  639. }
  640. elseif(strpos($source, 'flv=')===0)
  641. {
  642. $source = substr($source, 4);
  643. if(strpos($source, '&')>0)
  644. {
  645. $source = substr($source,0,strpos($source, '&'));
  646. }
  647. if(strpos($source,'://')>0)
  648. {
  649. if(strpos($source,api_get_path(WEB_PATH))!==false)
  650. {
  651. //we found the current portal url
  652. $files_list[] = array($source,'local','url');
  653. }
  654. else
  655. {
  656. //we didn't find any trace of current portal
  657. $files_list[] = array($source,'remote','url');
  658. }
  659. }
  660. else
  661. {
  662. $files_list[] = array($source,'local','abs');
  663. }
  664. continue; //skipping anything else to avoid two entries (while the others can have sub-files in their url, flv's can't)
  665. }
  666. }
  667. if(strpos($source,'://') > 0)
  668. {
  669. //cut at '?' in a URL with params
  670. if(strpos($source,'?')>0)
  671. {
  672. $second_part = substr($source,strpos($source,'?'));
  673. if(strpos($second_part,'://')>0)
  674. {//if the second part of the url contains a url too, treat the second one before cutting
  675. $pos1 = strpos($second_part,'=');
  676. $pos2 = strpos($second_part,'&');
  677. $second_part = substr($second_part,$pos1+1,$pos2-($pos1+1));
  678. if(strpos($second_part,api_get_path(WEB_PATH))!==false)
  679. {
  680. //we found the current portal url
  681. $files_list[] = array($second_part,'local','url');
  682. $in_files_list[] = learnpathItem::get_resources_from_source(TOOL_DOCUMENT,$second_part,$recursivity+1);
  683. if(count($in_files_list)>0)
  684. {
  685. $files_list = array_merge($files_list,$in_files_list);
  686. }
  687. }
  688. else
  689. {
  690. //we didn't find any trace of current portal
  691. $files_list[] = array($second_part,'remote','url');
  692. }
  693. }
  694. elseif(strpos($second_part,'=')>0)
  695. {
  696. if(substr($second_part,0,1) === '/')
  697. { //link starts with a /, making it absolute (relative to DocumentRoot)
  698. $files_list[] = array($second_part,'local','abs');
  699. $in_files_list[] = learnpathItem::get_resources_from_source(TOOL_DOCUMENT,$second_part,$recursivity+1);
  700. if(count($in_files_list)>0)
  701. {
  702. $files_list = array_merge($files_list,$in_files_list);
  703. }
  704. }
  705. elseif(strstr($second_part,'..') === 0)
  706. { //link is relative but going back in the hierarchy
  707. $files_list[] = array($second_part,'local','rel');
  708. $dir = dirname($abs_path);
  709. $new_abs_path = realpath($dir.'/'.$second_part);
  710. $in_files_list[] = learnpathItem::get_resources_from_source(TOOL_DOCUMENT,$new_abs_path,$recursivity+1);
  711. if(count($in_files_list)>0)
  712. {
  713. $files_list = array_merge($files_list,$in_files_list);
  714. }
  715. }
  716. else
  717. { //no starting '/', making it relative to current document's path
  718. if(substr($second_part,0,2) == './')
  719. {
  720. $second_part = substr($second_part,2);
  721. }
  722. $files_list[] = array($second_part,'local','rel');
  723. $dir = dirname($abs_path);
  724. $new_abs_path = realpath($dir.'/'.$second_part);
  725. $in_files_list[] = learnpathItem::get_resources_from_source(TOOL_DOCUMENT,$new_abs_path,$recursivity+1);
  726. if(count($in_files_list)>0)
  727. {
  728. $files_list = array_merge($files_list,$in_files_list);
  729. }
  730. }
  731. }
  732. //leave that second part behind now
  733. $source = substr($source,0,strpos($source,'?'));
  734. if(strpos($source,'://') > 0)
  735. {
  736. if(strpos($source,api_get_path(WEB_PATH))!==false)
  737. {
  738. //we found the current portal url
  739. $files_list[] = array($source,'local','url');
  740. $in_files_list[] = learnpathItem::get_resources_from_source(TOOL_DOCUMENT,$source,$recursivity+1);
  741. if(count($in_files_list)>0)
  742. {
  743. $files_list = array_merge($files_list,$in_files_list);
  744. }
  745. }
  746. else
  747. {
  748. //we didn't find any trace of current portal
  749. $files_list[] = array($source,'remote','url');
  750. }
  751. }
  752. else
  753. {
  754. //no protocol found, make link local
  755. if(substr($source,0,1) === '/')
  756. { //link starts with a /, making it absolute (relative to DocumentRoot)
  757. $files_list[] = array($source,'local','abs');
  758. $in_files_list[] = learnpathItem::get_resources_from_source(TOOL_DOCUMENT,$source,$recursivity+1);
  759. if(count($in_files_list)>0)
  760. {
  761. $files_list = array_merge($files_list,$in_files_list);
  762. }
  763. }
  764. elseif(strstr($source,'..') === 0)
  765. { //link is relative but going back in the hierarchy
  766. $files_list[] = array($source,'local','rel');
  767. $dir = dirname($abs_path);
  768. $new_abs_path = realpath($dir.'/'.$source);
  769. $in_files_list[] = learnpathItem::get_resources_from_source(TOOL_DOCUMENT,$new_abs_path,$recursivity+1);
  770. if(count($in_files_list)>0)
  771. {
  772. $files_list = array_merge($files_list,$in_files_list);
  773. }
  774. }
  775. else
  776. { //no starting '/', making it relative to current document's path
  777. if(substr($source,0,2) == './')
  778. {
  779. $source = substr($source,2);
  780. }
  781. $files_list[] = array($source,'local','rel');
  782. $dir = dirname($abs_path);
  783. $new_abs_path = realpath($dir.'/'.$source);
  784. $in_files_list[] = learnpathItem::get_resources_from_source(TOOL_DOCUMENT,$new_abs_path,$recursivity+1);
  785. if(count($in_files_list)>0)
  786. {
  787. $files_list = array_merge($files_list,$in_files_list);
  788. }
  789. }
  790. }
  791. }
  792. //found some protocol there
  793. if(strpos($source,api_get_path(WEB_PATH))!==false)
  794. {
  795. //we found the current portal url
  796. $files_list[] = array($source,'local','url');
  797. $in_files_list[] = learnpathItem::get_resources_from_source(TOOL_DOCUMENT,$source,$recursivity+1);
  798. if(count($in_files_list)>0)
  799. {
  800. $files_list = array_merge($files_list,$in_files_list);
  801. }
  802. }
  803. else
  804. {
  805. //we didn't find any trace of current portal
  806. $files_list[] = array($source,'remote','url');
  807. }
  808. }
  809. else
  810. {
  811. //no protocol found, make link local
  812. if(substr($source,0,1) === '/')
  813. { //link starts with a /, making it absolute (relative to DocumentRoot)
  814. $files_list[] = array($source,'local','abs');
  815. $in_files_list[] = learnpathItem::get_resources_from_source(TOOL_DOCUMENT,$source,$recursivity+1);
  816. if(count($in_files_list)>0)
  817. {
  818. $files_list = array_merge($files_list,$in_files_list);
  819. }
  820. }
  821. elseif(strstr($source,'..') === 0)
  822. { //link is relative but going back in the hierarchy
  823. $files_list[] = array($source,'local','rel');
  824. $dir = dirname($abs_path);
  825. $new_abs_path = realpath($dir.'/'.$source);
  826. $in_files_list[] = learnpathItem::get_resources_from_source(TOOL_DOCUMENT,$new_abs_path,$recursivity+1);
  827. if(count($in_files_list)>0)
  828. {
  829. $files_list = array_merge($files_list,$in_files_list);
  830. }
  831. }
  832. else
  833. { //no starting '/', making it relative to current document's path
  834. if(substr($source,0,2) == './')
  835. {
  836. $source = substr($source,2);
  837. }
  838. $files_list[] = array($source,'local','rel');
  839. $dir = dirname($abs_path);
  840. $new_abs_path = realpath($dir.'/'.$source);
  841. $in_files_list[] = learnpathItem::get_resources_from_source(TOOL_DOCUMENT,$new_abs_path,$recursivity+1);
  842. if(count($in_files_list)>0)
  843. {
  844. $files_list = array_merge($files_list,$in_files_list);
  845. }
  846. }
  847. }
  848. }
  849. }
  850. }
  851. break;
  852. default:
  853. break;
  854. }
  855. }
  856. else
  857. {
  858. //the file could not be found
  859. return false;
  860. }
  861. break;
  862. default: //ignore
  863. break;
  864. }
  865. //error_log(str_repeat(' ',$recursivity),'found files '.print_r($files_list,true),0);
  866. //return $files_list;
  867. $checked_files_list = array();
  868. $checked_array_list = array();
  869. foreach($files_list as $idx => $file)
  870. {
  871. if(!empty($file[0]))
  872. {
  873. if(!in_array($file[0],$checked_files_list))
  874. {
  875. $checked_files_list[] = $files_list[$idx][0];
  876. $checked_array_list[] = $files_list[$idx];
  877. }
  878. }
  879. }
  880. return $checked_array_list;
  881. }
  882. /**
  883. * Gets the score
  884. * @return float The current score or 0 if no score set yet
  885. */
  886. function get_score(){
  887. if($this->debug>0){error_log('New LP - In learnpathItem::get_score()',0);}
  888. $res = 0;
  889. if(!empty($this->current_score))
  890. {
  891. $res = $this->current_score;
  892. }
  893. if($this->debug>1){error_log('New LP - Out of learnpathItem::get_score() - returning '.$res,0);}
  894. return $res;
  895. }
  896. /**
  897. * Gets the item status
  898. * @param boolean Do or don't check into the database for the latest value. Optional. Default is true
  899. * @param boolean Do or don't update the local attribute value with what's been found in DB
  900. * @return string Current status or 'Nnot attempted' if no status set yet
  901. */
  902. function get_status($check_db=true,$update_local=false)
  903. {
  904. if($this->debug>0){error_log('New LP - In learnpathItem::get_status() on item '.$this->db_id,0);}
  905. if($check_db)
  906. {
  907. if($this->debug>2){error_log('New LP - In learnpathItem::get_status(): checking db',0);}
  908. $table = Database::get_course_table('lp_item_view');
  909. $sql = "SELECT * FROM $table WHERE id = '".$this->db_item_view_id."' AND view_count = '".$this->get_attempt_id()."'";
  910. if($this->debug>2){error_log('New LP - In learnpathItem::get_status() - Checking DB: '.$sql,0);}
  911. $res = api_sql_query($sql);
  912. if(Database::num_rows($res)==1){
  913. $row = Database::fetch_array($res);
  914. if($update_local==true){
  915. $this->set_status($row['status']);
  916. }
  917. if($this->debug>2){error_log('New LP - In learnpathItem::get_status() - Returning db value '.$row['status'],0);}
  918. return $row['status'];
  919. }
  920. }
  921. else
  922. {
  923. if($this->debug>2){error_log('New LP - In learnpathItem::get_status() - in get_status: using attrib',0);}
  924. if(!empty($this->status))
  925. {
  926. if($this->debug>2){error_log('New LP - In learnpathItem::get_status() - Returning attrib: '.$this->status,0);}
  927. return $this->status;
  928. }
  929. }
  930. if($this->debug>2){error_log('New LP - In learnpathItem::get_status() - Returning default '.$this->possible_status[0],0);}
  931. return $this->possible_status[0];
  932. }
  933. /**
  934. * Gets the suspend data
  935. */
  936. function get_suspend_data()
  937. {
  938. if($this->debug>0){error_log('New LP - In learnpathItem::get_suspend_data()',0);}
  939. //TODO : improve cleaning of breaklines ... it works but is it really a beautiful way to do it ?
  940. if(!empty($this->current_data)){return str_replace(array("\r","\n"),array('\r','\n'),$this->current_data);}else{return '';}
  941. }
  942. /**
  943. * Gets the total time spent on this item view so far
  944. * @param string Origin of the request. If coming from PHP, send formatted as xxhxx'xx", otherwise use scorm format 00:00:00
  945. * @param integer Given time is a default time to return formatted
  946. */
  947. function get_scorm_time($origin='php',$given_time=null){
  948. //if($this->debug>0){error_log('New LP - In learnpathItem::get_scorm_time()',0);}
  949. $h = get_lang('h');
  950. if(!isset($given_time)){
  951. if(!empty($this->current_start_time) && is_object($this)){
  952. if(!empty($this->current_stop_time)){
  953. $time = $this->current_stop_time - $this->current_start_time;
  954. }else{
  955. $time = time() - $this->current_start_time;
  956. }
  957. }else{
  958. if($origin == 'js'){
  959. return '00:00:00';
  960. }else{
  961. return '00'.$h.'00\'00"';
  962. }
  963. }
  964. }else{
  965. $time = $given_time;
  966. }
  967. $hours = $time/3600;
  968. $mins = ($time%3600)/60;
  969. $secs = ($time%60);
  970. if($origin == 'js'){
  971. $scorm_time = trim(sprintf("%4d:%02d:%02d",$hours,$mins,$secs));
  972. }else{
  973. $scorm_time = trim(sprintf("%4d$h%02d'%02d\"",$hours,$mins,$secs));
  974. }
  975. return $scorm_time;
  976. }
  977. /**
  978. * Returns the item's title
  979. * @return string Title
  980. */
  981. function get_title(){
  982. if($this->debug>0){error_log('New LP - In learnpathItem::get_title()',0);}
  983. if(empty($this->title)){return '';}
  984. return $this->title;
  985. }
  986. /**
  987. * Returns the total time used to see that item
  988. * @return integer Total time
  989. */
  990. function get_total_time(){
  991. if($this->debug>0){error_log('New LP - In learnpathItem::get_total_time()',0);}
  992. if($this->current_start_time == 0){ //shouldn't be necessary thanks to the open() method
  993. $this->current_start_time = time();
  994. }
  995. $time = $this->current_stop_time - $this->current_start_time;
  996. if($time < 0){
  997. return 0;
  998. }else{
  999. if($this->debug>2){error_log('New LP - In learnpathItem::get_total_time() - Current stop time = '.$this->current_stop_time.', current start time = '.$this->current_start_time.' Returning '.$time,0);}
  1000. return $time;
  1001. }
  1002. }
  1003. /**
  1004. * Gets the item type
  1005. * @return string The item type (can be doc, dir, sco, asset)
  1006. */
  1007. function get_type()
  1008. {
  1009. $res = 'asset';
  1010. if($this->debug>0){error_log('New LP - In learnpathItem::get_type() on item '.$this->db_id,0);}
  1011. if(!empty($this->type))
  1012. {
  1013. //error_log('In item::get_type() - returning '.$this->type,0);
  1014. $res = $this->type;
  1015. }
  1016. if($this->debug>2){error_log('New LP - In learnpathItem::get_type() - Returning '.$res.' for item '.$this->db_id,0);}
  1017. return $res;
  1018. }
  1019. /**
  1020. * Gets the view count for this item
  1021. */
  1022. function get_view_count(){
  1023. if($this->debug>0){error_log('New LP - In learnpathItem::get_view_count()',0);}
  1024. if(!empty($this->attempt_id)){
  1025. return $this->attempt_id;
  1026. }else{
  1027. return 0;
  1028. }
  1029. }
  1030. /**
  1031. * Tells if an item is done ('completed','passed','succeeded') or not
  1032. * @return bool True if the item is done ('completed','passed','succeeded'), false otherwise
  1033. */
  1034. function is_done(){
  1035. if($this->status_is(array('completed','passed','succeeded'))){
  1036. if($this->debug>2){error_log('New LP - In learnpath::is_done() - Item '.$this->get_id().' is complete',0);}
  1037. return true;
  1038. }else{
  1039. if($this->debug>2){error_log('New LP - In learnpath::is_done() - Item '.$this->get_id().' is not complete',0);}
  1040. return false;
  1041. }
  1042. }
  1043. /**
  1044. * Tells if a restart is allowed (take it from $this->prevent_reinit and $this->status)
  1045. * @return integer -1 if retaking the sco another time for credit is not allowed,
  1046. * 0 if it is not allowed but the item has to be finished
  1047. * 1 if it is allowed. Defaults to 1
  1048. */
  1049. function is_restart_allowed()
  1050. {
  1051. if($this->debug>2){error_log('New LP - In learnpathItem::is_restart_allowed()',0);}
  1052. $restart = 1;
  1053. $mystatus = $this->get_status(true);
  1054. if($this->get_prevent_reinit() > 0){ //if prevent_reinit == 1 (or more)
  1055. //if status is not attempted or incomplete, authorize retaking (of the same) anyway. Otherwise:
  1056. if($mystatus != $this->possible_status[0] AND $mystatus != $this->possible_status[1]){
  1057. $restart = -1;
  1058. }else{
  1059. $restart = 0;
  1060. }
  1061. }else{
  1062. if($mystatus == $this->possible_status[0] OR $mystatus == $this->possible_status[1]){
  1063. $restart = -1;
  1064. }
  1065. }
  1066. if($this->debug>2){error_log('New LP - End of learnpathItem::is_restart_allowed() - Returning '.$restart,0);}
  1067. return $restart;
  1068. }
  1069. /**
  1070. * Opens/launches the item. Initialises runtime values.
  1071. * @return boolean True on success, false on failure.
  1072. */
  1073. function open($allow_new_attempt=false){
  1074. if($this->debug>0){error_log('New LP - In learnpathItem::open()',0);}
  1075. if($this->prevent_reinit == 0)
  1076. {
  1077. $this->current_score = 0;
  1078. $this->current_start_time = time();
  1079. //In this case, as we are opening the item, what is important to us
  1080. //is the database status, in order to know if this item has already
  1081. //been used in the past (rather than just loaded and modified by
  1082. //some javascript but not written in the database).
  1083. //If the database status is different from 'not attempted', we can
  1084. //consider this item has already been used, and as such we can
  1085. //open a new attempt. Otherwise, we'll just reuse the current
  1086. //attempt, which is generally created the first time the item is
  1087. //loaded (for example as part of the table of contents)
  1088. $stat = $this->get_status(true);
  1089. if($allow_new_attempt && isset($stat) && ($stat != $this->possible_status[0]))
  1090. {
  1091. $this->attempt_id = $this->attempt_id + 1; //open a new attempt
  1092. }
  1093. $this->status = $this->possible_status[1];
  1094. }
  1095. else
  1096. {
  1097. if($this->current_start_time == 0)
  1098. { //small exception for start time, to avoid amazing values
  1099. $this->current_start_time = time();
  1100. }
  1101. //error_log('New LP - reinit blocked by setting',0);
  1102. }
  1103. }
  1104. /**
  1105. * Outputs the item contents
  1106. * @return string HTML file (displayable in an <iframe>) or empty string if no path defined
  1107. */
  1108. function output()
  1109. {
  1110. if($this->debug>0){error_log('New LP - In learnpathItem::output()',0);}
  1111. if(!empty($this->path) and is_file($this->path))
  1112. {
  1113. $output = '';
  1114. $output .= file_get_contents($this->path);
  1115. return $output;
  1116. }
  1117. return '';
  1118. }
  1119. /**
  1120. * Parses the prerequisites string with the AICC logic language
  1121. * @param string The prerequisites string as it figures in imsmanifest.xml
  1122. * @param Array Array of items in the current learnpath object. Although we're in the learnpathItem object, it's necessary to have a list of all items to be able to check the current item's prerequisites
  1123. * @param Array List of references (the "ref" column in the lp_item table) that are strings used in the expression of prerequisites.
  1124. * @param integer The user ID. In some cases like Dokeos quizzes, it's necessary to have the user ID to query other tables (like the results of quizzes)
  1125. * @return boolean True if the list of prerequisites given is entirely satisfied, false otherwise
  1126. */
  1127. function parse_prereq($prereqs_string, $items, $refs_list,$user_id){
  1128. if($this->debug>0){error_log('New LP - In learnpathItem::parse_prereq() for learnpath '.$this->lp_id.' with string '.$prereqs_string,0);}
  1129. //deal with &, |, ~, =, <>, {}, ,, X*, () in reverse order
  1130. $this->prereq_alert = '';
  1131. // First parse all parenthesis by using a sequential loop (looking for less-inclusives first)
  1132. if($prereqs_string == '_true_'){return true;}
  1133. if($prereqs_string == '_false_'){
  1134. if(empty($this->prereq_alert)){
  1135. $this->prereq_alert = get_lang('_prereq_not_complete');
  1136. }
  1137. return false;
  1138. }
  1139. while(strpos($prereqs_string,'(')!==false){
  1140. //remove any () set and replace with its value
  1141. $matches = array();
  1142. $res = preg_match_all('/(\(([^\(\)]*)\))/',$prereqs_string,$matches);
  1143. if($res){
  1144. foreach($matches[2] as $id=>$match){
  1145. $str_res = $this->parse_prereq($match,$items,$refs_list,$user_id);
  1146. if($str_res){
  1147. $prereqs_string = str_replace($matches[1][$id],'_true_',$prereqs_string);
  1148. }else{
  1149. $prereqs_string = str_replace($matches[1][$id],'_false_',$prereqs_string);
  1150. }
  1151. }
  1152. }
  1153. }
  1154. //parenthesis removed, now look for ORs as it is the lesser-priority binary operator (= always uses one text operand)
  1155. if(strpos($prereqs_string,"|")===false){
  1156. if($this->debug>1){error_log('New LP - Didnt find any OR, looking for AND',0);}
  1157. if(strpos($prereqs_string,"&")!==false){
  1158. $list = split("&",$prereqs_string);
  1159. if(count($list)>1){
  1160. $andstatus = true;
  1161. foreach($list as $condition){
  1162. $andstatus = $andstatus && $this->parse_prereq($condition,$items,$refs_list,$user_id);
  1163. if($andstatus==false){
  1164. if($this->debug>1){error_log('New LP - One condition in AND was false, short-circuit',0);}
  1165. break;
  1166. }
  1167. }
  1168. if(empty($this->prereq_alert) && !$andstatus){
  1169. $this->prereq_alert = get_lang('_prereq_not_complete');
  1170. }
  1171. return $andstatus;
  1172. }else{
  1173. if(isset($items[$refs_list[$list[0]]])){
  1174. $status = $items[$refs_list[$list[0]]]->get_status(true);
  1175. $returnstatus = (($status == $this->possible_status[2]) OR ($status == $this->possible_status[3]));
  1176. if(empty($this->prereq_alert) && !$returnstatus){
  1177. $this->prereq_alert = get_lang('_prereq_not_complete');
  1178. }
  1179. return $returnstatus;
  1180. }
  1181. $this->prereq_alert = get_lang('_prereq_not_complete');
  1182. return false;
  1183. }
  1184. }else{
  1185. //no ORs found, now look for ANDs
  1186. if($this->debug>1){error_log('New LP - Didnt find any AND, looking for =',0);}
  1187. if(strpos($prereqs_string,"=")!==false){
  1188. if($this->debug>1){error_log('New LP - Found =, looking into it',0);}
  1189. //we assume '=' signs only appear when there's nothing else around
  1190. $params = split('=',$prereqs_string);
  1191. if(count($params) == 2){
  1192. //right number of operands
  1193. if(isset($items[$refs_list[$params[0]]])){
  1194. $status = $items[$refs_list[$params[0]]]->get_status(true);
  1195. $returnstatus = ($status == $params[1]);
  1196. if(empty($this->prereq_alert) && !$returnstatus){
  1197. $this->prereq_alert = get_lang('_prereq_not_complete');
  1198. }
  1199. return $returnstatus;
  1200. }
  1201. $this->prereq_alert = get_lang('_prereq_not_complete');
  1202. return false;
  1203. }
  1204. }else{
  1205. //No ANDs found, look for <>
  1206. if($this->debug>1){error_log('New LP - Didnt find any =, looking for <>',0);}
  1207. if(strpos($prereqs_string,"<>")!==false){
  1208. if($this->debug>1){error_log('New LP - Found <>, looking into it',0);}
  1209. //we assume '<>' signs only appear when there's nothing else around
  1210. $params = split('<>',$prereqs_string);
  1211. if(count($params) == 2){
  1212. //right number of operands
  1213. if(isset($items[$refs_list[$params[0]]])){
  1214. $status = $items[$refs_list[$params[0]]]->get_status(true);
  1215. $returnstatus = ($status != $params[1]);
  1216. if(empty($this->prereq_alert) && !$returnstatus){
  1217. $this->prereq_alert = get_lang('_prereq_not_complete');
  1218. }
  1219. return $returnstatus;
  1220. }
  1221. $this->prereq_alert = get_lang('_prereq_not_complete');
  1222. return false;
  1223. }
  1224. }else{
  1225. //No <> found, look for ~ (unary)
  1226. if($this->debug>1){error_log('New LP - Didnt find any =, looking for ~',0);}
  1227. //only remains: ~ and X*{}
  1228. if(strpos($prereqs_string,"~")!==false){
  1229. //found NOT
  1230. if($this->debug>1){error_log('New LP - Found ~, looking into it',0);}
  1231. $list = array();
  1232. $myres = preg_match('/~([^(\d+\*)\{]*)/',$prereqs_string,$list);
  1233. if($myres){
  1234. $returnstatus = !$this->parse_prereq($list[1],$items,$refs_list,$user_id);
  1235. if(empty($this->prereq_alert) && !$returnstatus){
  1236. $this->prereq_alert = get_lang('_prereq_not_complete');
  1237. }
  1238. return $returnstatus;
  1239. }else{
  1240. //strange...
  1241. if($this->debug>1){error_log('New LP - Found ~ but strange string: '.$prereqs_string,0);}
  1242. }
  1243. }else{
  1244. //Finally, look for sets/groups
  1245. if($this->debug>1){error_log('New LP - Didnt find any ~, looking for groups',0);}
  1246. //only groups here
  1247. $groups = array();
  1248. $groups_there = preg_match_all('/((\d+\*)?\{([^\}]+)\}+)/',$prereqs_string,$groups);
  1249. if($groups_there){
  1250. foreach($groups[1] as $gr) //only take the results that correspond to the big brackets-enclosed condition
  1251. {
  1252. if($this->debug>1){error_log('New LP - Dealing with group '.$gr,0);}
  1253. $multi = array();
  1254. $mycond = false;
  1255. if(preg_match('/(\d+)\*\{([^\}]+)\}/',$gr,$multi)){
  1256. if($this->debug>1){error_log('New LP - Found multiplier '.$multi[0],0);}
  1257. $count = $multi[1];
  1258. $list = split(',',$multi[2]);
  1259. $mytrue = 0;
  1260. foreach($list as $cond){
  1261. if(isset($items[$refs_list[$cond]])){
  1262. $status = $items[$refs_list[$cond]]->get_status(true);
  1263. if (($status == $this->possible_status[2]) OR ($status == $this->possible_status[3])){
  1264. $mytrue ++;
  1265. if($this->debug>1){error_log('New LP - Found true item, counting.. ('.($mytrue).')',0);}
  1266. }
  1267. }else{
  1268. if($this->debug>1){error_log('New LP - item '.$cond.' does not exist in items list',0);}
  1269. }
  1270. }
  1271. if($mytrue >= $count){
  1272. if($this->debug>1){error_log('New LP - Got enough true results, return true',0);}
  1273. $mycond = true;
  1274. }else{
  1275. if($this->debug>1){error_log('New LP - Not enough true results',0);}
  1276. }
  1277. }
  1278. else{
  1279. if($this->debug>1){error_log('New LP - No multiplier',0);}
  1280. $list = split(',',$gr);
  1281. $mycond = true;
  1282. foreach($list as $cond){
  1283. if(isset($items[$refs_list[$cond]])){
  1284. $status = $items[$refs_list[$cond]]->get_status(true);
  1285. if (($status == $this->possible_status[2]) OR ($status == $this->possible_status[3])){
  1286. $mycond = true;
  1287. if($this->debug>1){error_log('New LP - Found true item',0);}
  1288. }else{
  1289. if($this->debug>1){error_log('New LP - Found false item, the set is not true, return false',0);}
  1290. $mycond = false;
  1291. break;
  1292. }
  1293. }else{
  1294. if($this->debug>1){error_log('New LP - item '.$cond.' does not exist in items list',0);}
  1295. if($this->debug>1){error_log('New LP - Found false item, the set is not true, return false',0);}
  1296. $mycond = false;
  1297. break;
  1298. }
  1299. }
  1300. }
  1301. if(!$mycond && empty($this->prereq_alert)){
  1302. $this->prereq_alert = get_lang('_prereq_not_complete');
  1303. }
  1304. return $mycond;
  1305. }
  1306. }else{
  1307. //Nothing found there either. Now return the value of the corresponding resource completion status
  1308. if($this->debug>1){error_log('New LP - Didnt find any group, returning value for '.$prereqs_string,0);}
  1309. if(isset($items[$refs_list[$prereqs_string]])){
  1310. if($items[$refs_list[$prereqs_string]]->type == 'quiz')
  1311. {
  1312. $sql = 'SELECT exe_result, exe_weighting
  1313. FROM '.Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES).'
  1314. WHERE exe_exo_id = '.$items[$refs_list[$prereqs_string]]->path.'
  1315. AND exe_user_id = '.$user_id.'
  1316. ORDER BY exe_date DESC
  1317. LIMIT 0, 1';
  1318. $rs_quiz = api_sql_query($sql, __FILE__, __LINE__);
  1319. if($quiz = Database :: fetch_array($rs_quiz))
  1320. {
  1321. if($quiz['exe_result'] >= $items[$refs_list[$prereqs_string]]->get_mastery_score())
  1322. {
  1323. $returnstatus = true;
  1324. }
  1325. else
  1326. {
  1327. $this->prereq_alert = get_lang('_prereq_not_complete');
  1328. $returnstatus = false;
  1329. }
  1330. }
  1331. else
  1332. {
  1333. $this->prereq_alert = get_lang('_prereq_not_complete');
  1334. $returnstatus = false;
  1335. }
  1336. return $returnstatus;
  1337. }
  1338. else
  1339. {
  1340. $status = $items[$refs_list[$prereqs_string]]->get_status(true);
  1341. $returnstatus = (($status == $this->possible_status[2]) OR ($status == $this->possible_status[3]));
  1342. if(!$returnstatus && empty($this->prereq_alert)){
  1343. $this->prereq_alert = get_lang('_prereq_not_complete');
  1344. }
  1345. if(!$returnstatus){
  1346. if($this->debug>1){error_log('New LP - Prerequisite '.$prereqs_string.' not complete',0);}
  1347. }else{
  1348. if($this->debug>1){error_log('New LP - Prerequisite '.$prereqs_string.' complete',0);}
  1349. }
  1350. return $returnstatus;
  1351. }
  1352. }else{
  1353. if($this->debug>1){error_log('New LP - Could not find '.$prereqs_string.' in '.print_r($refs_list,true),0);}
  1354. }
  1355. }
  1356. }
  1357. }
  1358. }
  1359. }
  1360. }else{
  1361. $list = split("\|",$prereqs_string);
  1362. if(count($list)>1){
  1363. if($this->debug>1){error_log('New LP - Found OR, looking into it',0);}
  1364. $orstatus = false;
  1365. foreach($list as $condition){
  1366. if($this->debug>1){error_log('New LP - Found OR, adding it ('.$condition.')',0);}
  1367. $orstatus = $orstatus || $this->parse_prereq($condition,$items,$refs_list,$user_id);
  1368. if($orstatus == true){
  1369. //shortcircuit OR
  1370. if($this->debug>1){error_log('New LP - One condition in OR was true, short-circuit',0);}
  1371. break;
  1372. }
  1373. }
  1374. if(!$orstatus && empty($this->prereq_alert)){
  1375. $this->prereq_alert = get_lang('_prereq_not_complete');
  1376. }
  1377. return $orstatus;
  1378. }else{
  1379. if($this->debug>1){error_log('New LP - OR was found but only one elem present !?',0);}
  1380. if(isset($items[$refs_list[$list[0]]])){
  1381. $status = $items[$refs_list[$list[0]]]->get_status(true);
  1382. $returnstatus = (($status == 'completed') OR ($status == 'passed'));
  1383. if(!$returnstatus && empty($this->prereq_alert)){
  1384. $this->prereq_alert = get_lang('_prereq_not_complete');
  1385. }
  1386. return $returnstatus;
  1387. }
  1388. }
  1389. }
  1390. if(empty($this->prereq_alert)){
  1391. $this->prereq_alert = get_lang('_prereq_not_complete');
  1392. }
  1393. if($this->debug>1){error_log('New LP - End of parse_prereq. Error code is now '.$this->prereq_alert,0);}
  1394. return false;
  1395. }
  1396. /**
  1397. * Parses the HTML attributes given as string.
  1398. *
  1399. * @param string HTML attribute string
  1400. * @param array List of attributes that we want to get back
  1401. * @return array An associative array of attributes
  1402. * @author Based on a function from the HTML_Common2 PEAR module
  1403. */
  1404. function parse_HTML_attributes($attrString,$wanted=array())
  1405. {
  1406. $attributes = array();
  1407. $regs = array();
  1408. $reduced = false;
  1409. if(count($wanted)>0)
  1410. {
  1411. $reduced = true;
  1412. }
  1413. try {
  1414. //Find all occurences of something that looks like a URL
  1415. // The structure of this regexp is:
  1416. // (find protocol) then
  1417. // (optionally find some kind of space 1 or more times) then
  1418. // find (either an equal sign or a bracket) followed by an optional space
  1419. // followed by some text without quotes (between quotes itself or not)
  1420. // then possible closing brackets if we were in the opening bracket case
  1421. // OR something like @import()
  1422. $res = preg_match_all(
  1423. '/(((([A-Za-z_:])([A-Za-z0-9_:\.-]*))' .
  1424. // '/(((([A-Za-z_:])([A-Za-z0-9_:\.-]|[^\x00-\x7F])*)' . -> seems to be taking too much
  1425. // '/(((([A-Za-z_:])([^\x00-\x7F])*)' . -> takes only last letter of parameter name
  1426. '([ \n\t\r]+)?(' .
  1427. // '(=([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+))' . -> doesn't restrict close enough to the url itself
  1428. '(=([ \n\t\r]+)?("[^"\)]+"|\'[^\'\)]+\'|[^ \n\t\r\)]+))' .
  1429. '|' .
  1430. // '(\(([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+)\))' . -> doesn't restrict close enough to the url itself
  1431. '(\(([ \n\t\r]+)?("[^"\)]+"|\'[^\'\)]+\'|[^ \n\t\r\)]+)\))' .
  1432. '))' .
  1433. '|' .
  1434. // '(@import([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+)))?/', -> takes a lot (like 100's of thousands of empty possibilities)
  1435. '(@import([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+)))/',
  1436. $attrString,
  1437. $regs
  1438. );
  1439. } catch (Exception $e) {
  1440. error_log('Caught exception: '. $e->getMessage(),0) ;
  1441. }
  1442. if ($res) {
  1443. for ($i = 0; $i < count($regs[1]); $i++) {
  1444. $name = trim($regs[3][$i]);
  1445. $check = trim($regs[0][$i]);
  1446. $value = trim($regs[10][$i]);
  1447. if(empty($value) and !empty($regs[13][$i]))
  1448. {
  1449. $value = $regs[13][$i];
  1450. }
  1451. if(empty($name) && !empty($regs[16][$i]))
  1452. {
  1453. $name = '@import';
  1454. $value = trim($regs[16][$i]);
  1455. }
  1456. if(!empty($name))
  1457. {
  1458. if(!$reduced OR in_array(strtolower($name),$wanted))
  1459. {
  1460. if ($name == $check) {
  1461. $attributes[strtolower($name)][] = strtolower($name);
  1462. } else {
  1463. if (!empty($value) && ($value[0] == '\'' || $value[0] == '"')) {
  1464. $value = substr($value, 1, -1);
  1465. }
  1466. $attributes[strtolower($name)][] = $value;
  1467. }
  1468. }
  1469. }
  1470. }
  1471. }else{
  1472. error_log('preg_match did not find anything',0);
  1473. }
  1474. return $attributes;
  1475. }
  1476. /**
  1477. * Reinits all local values as the learnpath is restarted
  1478. * @return boolean True on success, false otherwise
  1479. */
  1480. function restart()
  1481. {
  1482. if($this->debug>0){error_log('New LP - In learnpathItem::restart()',0);}
  1483. $this->save();
  1484. $allowed = $this->is_restart_allowed();
  1485. if($allowed === -1){
  1486. //nothing allowed, do nothing
  1487. }elseif($allowed === 1){
  1488. //restart as new attempt is allowed, record a new attempt
  1489. $this->attempt_id = $this->attempt_id + 1; //simply reuse the previous attempt_id
  1490. $this->current_score = 0;
  1491. $this->current_start_time = 0;
  1492. $this->current_stop_time = 0;
  1493. $this->current_data = '';
  1494. $this->status = $this->possible_status[0];
  1495. $this->interactions_count = 0;
  1496. $this->interactions = array();
  1497. $this->objectives_count = 0;
  1498. $this->objectives = array();
  1499. $this->lesson_location = '';
  1500. $this->write_to_db();
  1501. }else{
  1502. //restart current element is allowed (because it's not finished yet),
  1503. // reinit current
  1504. $this->current_score = 0;
  1505. $this->current_start_time = 0;
  1506. $this->current_stop_time = 0;
  1507. $this->current_data = '';
  1508. $this->status = $this->possible_status[0];
  1509. }
  1510. return true;
  1511. }
  1512. /**
  1513. * Saves data in the database
  1514. * @param boolean Save from URL params (1) or from object attributes (0)
  1515. * @param boolean The results of a check on prerequisites for this item. True if prerequisites are completed, false otherwise. Defaults to false. Only used if not sco or au
  1516. * @return boolean True on success, false on failure
  1517. */
  1518. function save($from_outside=true,$prereqs_complete=false)
  1519. {
  1520. if($this->debug>0){error_log('New LP - In learnpathItem::save()',0);}
  1521. //$item_view_table = Database::get_course_table(COURSEID,LEARNPATH_ITEM_VIEW_TABLE);
  1522. $item_id = $this->get_id();
  1523. //first check if parameters passed via GET can be saved here
  1524. //in case it's a SCORM, we should get:
  1525. if($this->type == 'sco' || $this->type== 'au'){
  1526. $s = $this->get_status(true);
  1527. if($this->prevent_reinit == 1 AND
  1528. $s != $this->possible_status[0] AND $s != $this->possible_status[1]){
  1529. if($this->debug>1){error_log('New LP - In learnpathItem::save() - save reinit blocked by setting',0);}
  1530. //do nothing because the status has already been set. Don't allow it to change.
  1531. //TODO check there isn't a special circumstance where this should be saved
  1532. }else{
  1533. if($this->debug>1){error_log('New LP - In learnpathItem::save() - SCORM save request received',0);}
  1534. //get all new settings from the URL
  1535. if($from_outside==true){
  1536. if($this->debug>1){error_log('New LP - In learnpathItem::save() - Getting item data from outside',0);}
  1537. foreach($_GET as $param => $value)
  1538. {
  1539. $value = Database::escape_string($value);
  1540. switch($param){
  1541. case 'score':
  1542. $this->set_score($value);
  1543. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting score to '.$value,0);}
  1544. break;
  1545. case 'max':
  1546. $this->set_max_score($value);
  1547. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting view_max_score to '.$value,0);}
  1548. break;
  1549. case 'min':
  1550. $this->min_score = $value;
  1551. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting min_score to '.$value,0);}
  1552. break;
  1553. case 'lesson_status':
  1554. if(!empty($value)){
  1555. $this->set_status($value);
  1556. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting status to '.$value,0);}
  1557. }
  1558. break;
  1559. case 'time':
  1560. $this->set_time($value);
  1561. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting time to '.$value,0);}
  1562. break;
  1563. case 'suspend_data':
  1564. $this->current_data = $value;
  1565. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting suspend_data to '.$value,0);}
  1566. break;
  1567. case 'lesson_location':
  1568. $this->set_lesson_location($value);
  1569. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting lesson_location to '.$value,0);}
  1570. break;
  1571. case 'core_exit':
  1572. $this->set_core_exit($value);
  1573. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting core_exit to '.$value,0);}
  1574. break;
  1575. case 'interactions':
  1576. //$interactions = unserialize($value);
  1577. //foreach($interactions as $interaction){
  1578. // ;
  1579. //}
  1580. break;
  1581. case 'objectives':
  1582. break;
  1583. //case 'maxtimeallowed':
  1584. // $this->set_max_time_allowed($value);
  1585. // break;
  1586. /*
  1587. case 'objectives._count':
  1588. $this->attempt_id = $value;
  1589. break;
  1590. */
  1591. default:
  1592. //ignore
  1593. break;
  1594. }
  1595. }
  1596. }else{
  1597. if($this->debug>1){error_log('New LP - In learnpathItem::save() - Using inside item status',0);}
  1598. //do nothing, just let the local attributes be used
  1599. }
  1600. }
  1601. }else{ //if not SCO, such messages should not be expected
  1602. $type = strtolower($this->type);
  1603. switch($type){
  1604. case 'asset':
  1605. if($prereqs_complete)
  1606. {
  1607. $this->set_status($this->possible_status[2]);
  1608. }
  1609. break;
  1610. case TOOL_HOTPOTATOES:
  1611. case TOOL_QUIZ:
  1612. default:
  1613. //for now, everything that is not sco and not asset is set to
  1614. //completed when saved
  1615. if($prereqs_complete)
  1616. {
  1617. $this->set_status($this->possible_status[2]);
  1618. }
  1619. break;
  1620. }
  1621. }
  1622. //$time = $this->time
  1623. if($this->debug>1){error_log('New LP - End of learnpathItem::save() - Calling write_to_db()',0);}
  1624. return $this->write_to_db();
  1625. }
  1626. /**
  1627. * Sets the number of attempt_id to a given value
  1628. * @param integer The given value to set attempt_id to
  1629. * @return boolean TRUE on success, FALSE otherwise
  1630. */
  1631. function set_attempt_id($num)
  1632. {
  1633. if($this->debug>0){error_log('New LP - In learnpathItem::set_attempt_id()',0);}
  1634. if($num == strval(intval($num)) && $num>=0){
  1635. $this->attempt_id = $num;
  1636. return true;
  1637. }
  1638. return false;
  1639. }
  1640. /**
  1641. * Sets the core_exit value to the one given
  1642. */
  1643. function set_core_exit($value)
  1644. {
  1645. switch($value){
  1646. case '':
  1647. $this->core_exit = '';
  1648. break;
  1649. case 'suspend':
  1650. $this->core_exit = 'suspend';
  1651. break;
  1652. default:
  1653. $this->core_exit = 'none';
  1654. break;
  1655. }
  1656. return true;
  1657. }
  1658. /**
  1659. * Sets the item's description
  1660. * @param string Description
  1661. */
  1662. function set_description($string=''){
  1663. if($this->debug>0){error_log('New LP - In learnpathItem::set_description()',0);}
  1664. if(!empty($string)){$this->description = $string;}
  1665. }
  1666. /**
  1667. * Sets the lesson_location value
  1668. * @param string lesson_location as provided by the SCO
  1669. * @return boolean True on success, false otherwise
  1670. */
  1671. function set_lesson_location($location)
  1672. {
  1673. if($this->debug>0){error_log('New LP - In learnpathItem::set_lesson_location()',0);}
  1674. if(isset($location)){
  1675. $this->lesson_location = Database::escape_string($location);
  1676. return true;
  1677. }
  1678. return false;
  1679. }
  1680. /**
  1681. * Sets the item's depth level in the LP tree (0 is at root)
  1682. * @param integer Level
  1683. */
  1684. function set_level($int=0){
  1685. if($this->debug>0){error_log('New LP - In learnpathItem::set_level('.$int.')',0);}
  1686. if(!empty($int) AND $int == strval(intval($int))){$this->level = $int;}
  1687. }
  1688. /**
  1689. * Sets the lp_view id this item view is registered to
  1690. * @param integer lp_view DB ID
  1691. * @todo //todo insert into lp_item_view if lp_view not exists
  1692. */
  1693. function set_lp_view($lp_view_id)
  1694. {
  1695. if($this->debug>0){error_log('New LP - In learnpathItem::set_lp_view('.$lp_view_id.')',0);}
  1696. if(!empty($lp_view_id) and $lp_view_id = intval(strval($lp_view_id)))
  1697. {
  1698. $this->view_id = $lp_view_id;
  1699. $item_view_table = Database::get_course_table('lp_item_view');
  1700. //get the lp_item_view with the highest view_count
  1701. $sql = "SELECT * FROM $item_view_table WHERE lp_item_id = ".$this->get_id()." " .
  1702. " AND lp_view_id = ".$lp_view_id." ORDER BY view_count DESC";
  1703. if($this->debug>2){error_log('New LP - In learnpathItem::set_lp_view() - Querying lp_item_view: '.$sql,0);}
  1704. $res = api_sql_query($sql,__FILE__,__LINE__);
  1705. if(Database::num_rows($res)>0){
  1706. $row = Database::fetch_array($res);
  1707. $this->db_item_view_id = $row['id'];
  1708. $this->attempt_id = $row['view_count'];
  1709. $this->current_score = $row['score'];
  1710. $this->current_data = $row['suspend_data'];
  1711. $this->view_max_score = $row['max_score'];
  1712. //$this->view_min_score = $row['min_score'];
  1713. $this->status = $row['status'];
  1714. $this->current_start_time = $row['start_time'];
  1715. $this->current_stop_time = $this->current_start_time + $row['total_time'];
  1716. $this->lesson_location = $row['lesson_location'];
  1717. $this->core_exit = $row['core_exit'];
  1718. if($this->debug>2){error_log('New LP - In learnpathItem::set_lp_view() - Updated item object with database values',0);}
  1719. //now get the number of interactions for this little guy
  1720. $item_view_interaction_table = Database::get_course_table('lp_iv_interaction');
  1721. $sql = "SELECT * FROM $item_view_interaction_table WHERE lp_iv_id = '".$this->db_item_view_id."'";
  1722. $res = api_sql_query($sql,__FILE__,__LINE__);
  1723. if($res !== false){
  1724. $this->interactions_count = Database::num_rows($res);
  1725. }else{
  1726. $this->interactions_count = 0;
  1727. }
  1728. //now get the number of objectives for this little guy
  1729. $item_view_objective_table = Database::get_course_table('lp_iv_objective');
  1730. $sql = "SELECT * FROM $item_view_objective_table WHERE lp_iv_id = '".$this->db_item_view_id."'";
  1731. $res = api_sql_query($sql,__FILE__,__LINE__);
  1732. if($res !== false){
  1733. $this->objectives_count = Database::num_rows($res);
  1734. }else{
  1735. $this->objectives_count = 0;
  1736. }
  1737. }
  1738. }
  1739. //end
  1740. if($this->debug>2){error_log('New LP - End of learnpathItem::set_lp_view()',0);}
  1741. }
  1742. /**
  1743. * Sets the path
  1744. * @param string Path
  1745. */
  1746. function set_path($string=''){
  1747. if($this->debug>0){error_log('New LP - In learnpathItem::set_path()',0);}
  1748. if(!empty($string)){$this->path = $string;}
  1749. }
  1750. /**
  1751. * Sets the prevent_reinit attribute. This is based on the LP value and is set at creation time for
  1752. * each learnpathItem. It is a (bad?) way of avoiding a reference to the LP when saving an item.
  1753. * @param integer 1 for "prevent", 0 for "don't prevent" saving freshened values (new "not attempted" status etc)
  1754. */
  1755. function set_prevent_reinit($prevent){
  1756. if($this->debug>0){error_log('New LP - In learnpathItem::set_prevent_reinit()',0);}
  1757. if($prevent){
  1758. $this->prevent_reinit = 1;
  1759. }else{
  1760. $this->prevent_reinit = 0;
  1761. }
  1762. }
  1763. /**
  1764. * Sets the score value. If the mastery_score is set and the score reaches
  1765. * it, then set the status to 'passed'.
  1766. * @param float Score
  1767. * @return boolean True on success, false otherwise
  1768. */
  1769. function set_score($score)
  1770. {
  1771. if($this->debug>0){error_log('New LP - In learnpathItem::set_score('.$score.')',0);}
  1772. if(($this->max_score<=0 || $score <= $this->max_score) && ($score >= $this->min_score))
  1773. {
  1774. $this->current_score = $score;
  1775. $master = $this->get_mastery_score();
  1776. $current_status = $this->get_status(false);
  1777. //if mastery_score is set AND the current score reaches the mastery score AND the current status is different from 'completed', then set it to 'passed'
  1778. if($master != -1 && $this->current_score >= $master && $current_status != $this->possible_status[2])
  1779. {
  1780. $this->set_status($this->possible_status[3]);
  1781. }
  1782. elseif($master != -1 && $this->current_score<$master)
  1783. {
  1784. $this->set_status($this->possible_status[4]);
  1785. }
  1786. return true;
  1787. }
  1788. return false;
  1789. }
  1790. /**
  1791. * Sets the maximum score for this item
  1792. * @param int Maximum score - must be a decimal or an empty string
  1793. * @return boolean True on success, false on error
  1794. */
  1795. function set_max_score($score)
  1796. {
  1797. if($this->debug>0){error_log('New LP - In learnpathItem::set_max_score('.$score.')',0);}
  1798. if(is_int($score) or $score == '')
  1799. {
  1800. $this->view_max_score = Database::escape_string($score);
  1801. if($this->debug>1){error_log('New LP - In learnpathItem::set_max_score() - Updated object score of item '.$this->db_id.' to '.$this->view_max_score,0);}
  1802. return true;
  1803. }
  1804. return false;
  1805. }
  1806. /**
  1807. * Sets the status for this item
  1808. * @param string Status - must be one of the values defined in $this->possible_status
  1809. * @return boolean True on success, false on error
  1810. */
  1811. function set_status($status)
  1812. {
  1813. if($this->debug>0){error_log('New LP - In learnpathItem::set_status('.$status.')',0);}
  1814. $found = false;
  1815. foreach($this->possible_status as $possible){
  1816. if(preg_match('/^'.$possible.'$/i',$status)){
  1817. $found = true;
  1818. }
  1819. }
  1820. //if(in_array($status,$this->possible_status))
  1821. if($found)
  1822. {
  1823. $this->status = Database::escape_string($status);
  1824. if($this->debug>1){error_log('New LP - In learnpathItem::set_status() - Updated object status of item '.$this->db_id.' to '.$this->status,0);}
  1825. return true;
  1826. }
  1827. //error_log('New LP - '.$status.' was not in the possible status',0);
  1828. $this->status = $this->possible_status[0];
  1829. return false;
  1830. }
  1831. /**
  1832. * Sets the item viewing time in a usable form, given that SCORM packages often give it as 00:00:00.0000
  1833. * @param string Time as given by SCORM
  1834. */
  1835. function set_time($scorm_time,$format='scorm')
  1836. {
  1837. if($this->debug>0){error_log('New LP - In learnpathItem::set_time('.$scorm_time.')',0);}
  1838. if($scorm_time == 0 and ($this->type!='sco') and $this->current_start_time!=0){
  1839. $my_time = time() - $this->current_start_time;
  1840. if($my_time > 0){
  1841. $this->update_time($my_time);
  1842. if($this->debug>0){error_log('New LP - In learnpathItem::set_time('.$scorm_time.') - found asset - set time to '.$my_time,0);}
  1843. }
  1844. }else{
  1845. if($format == 'scorm'){
  1846. $res = array();
  1847. if(preg_match('/^(\d{1,4}):(\d{2}):(\d{2})(\.\d{1,4})?/',$scorm_time,$res)){
  1848. $time = time();
  1849. $hour = $res[1];
  1850. $min = $res[2];
  1851. $sec = $res[3];
  1852. //getting total number of seconds spent
  1853. $total_sec = $hour*3600 + $min*60 + $sec;
  1854. $this->update_time($total_sec);
  1855. }
  1856. }elseif($format == 'int'){
  1857. $this->update_time($scorm_time);
  1858. }
  1859. }
  1860. }
  1861. /**
  1862. * Sets the item's title
  1863. * @param string Title
  1864. */
  1865. function set_title($string=''){
  1866. if($this->debug>0){error_log('New LP - In learnpathItem::set_title()',0);}
  1867. if(!empty($string)){$this->title = $string;}
  1868. }
  1869. /**
  1870. * Sets the item's type
  1871. * @param string Type
  1872. */
  1873. function set_type($string=''){
  1874. if($this->debug>0){error_log('New LP - In learnpathItem::set_type()',0);}
  1875. if(!empty($string)){$this->type = $string;}
  1876. }
  1877. /**
  1878. * Checks if the current status is part of the list of status given
  1879. * @param strings_array An array of status to check for. If the current status is one of the strings, return true
  1880. * @return boolean True if the status was one of the given strings, false otherwise
  1881. */
  1882. function status_is($list=array())
  1883. {
  1884. if($this->debug>1){error_log('New LP - In learnpathItem::status_is('.print_r($list,true).') on item '.$this->db_id,0);}
  1885. $mystatus = $this->get_status(true);
  1886. if(empty($mystatus)){
  1887. return false;
  1888. }
  1889. $found = false;
  1890. foreach($list as $status)
  1891. {
  1892. if(preg_match('/^'.$status.'$/i',$mystatus))
  1893. {
  1894. if($this->debug>2){error_log('New LP - learnpathItem::status_is() - Found status '.$status.' corresponding to current status',0);}
  1895. $found = true;
  1896. return $found;
  1897. }
  1898. }
  1899. if($this->debug>2){error_log('New LP - learnpathItem::status_is() - Status '.$mystatus.' did not match request',0);}
  1900. return $found;
  1901. }
  1902. /**
  1903. * Updates the time info according to the given session_time
  1904. * @param integer Time in seconds
  1905. * //TODO @TODO make this method better by allowing better/multiple time slices
  1906. */
  1907. function update_time($total_sec=0){
  1908. if($this->debug>0){error_log('New LP - In learnpathItem::update_time('.$total_sec.')',0);}
  1909. if($total_sec>=0){
  1910. //getting start time from finish time. The only problem in the calculation is it might be
  1911. //modified by the scripts processing time
  1912. $now = time();
  1913. $start = $now-$total_sec;
  1914. $this->current_start_time = $start;
  1915. $this->current_stop_time = $now;
  1916. /*if(empty($this->current_start_time)){
  1917. $this->current_start_time = $start;
  1918. $this->current_stop_time = $now;
  1919. }else{
  1920. //if($this->current_stop_time != $this->current_start_time){
  1921. //if the stop time has already been set before to something else
  1922. //than the start time, add the given time to what's already been
  1923. //recorder.
  1924. //This is the SCORM way of doing things, because the time comes from
  1925. //core.session_time, not core.total_time
  1926. // UPDATE: adding time to previous time is only done on SCORM's finish()
  1927. // call, not normally, so for now ignore this section
  1928. // $this->current_stop_time = $this->current_stop_time + $stop;
  1929. // error_log('New LP - Adding '.$stop.' seconds - now '.$this->current_stop_time,0);
  1930. //}else{
  1931. //if no previous stop time set, use the one just calculated now from
  1932. //start time
  1933. //$this->current_start_time = $start;
  1934. //$this->current_stop_time = $now;
  1935. //error_log('New LP - Setting '.$stop.' seconds - now '.$this->current_stop_time,0);
  1936. //}
  1937. }*/
  1938. }
  1939. }
  1940. /**
  1941. * Write objectives to DB. This method is separate from write_to_db() because otherwise
  1942. * objectives are lost as a side effect to AJAX and session concurrent access
  1943. * @return boolean True or false on error
  1944. */
  1945. function write_objectives_to_db()
  1946. {
  1947. if($this->debug>0){error_log('New LP - In learnpathItem::write_objectives_to_db()',0);}
  1948. if(is_array($this->objectives) && count($this->objectives)>0){
  1949. //save objectives
  1950. $tbl = Database::get_course_table('lp_item_view');
  1951. $sql = "SELECT id FROM $tbl " .
  1952. "WHERE lp_item_id = ".$this->db_id." " .
  1953. "AND lp_view_id = ".$this->view_id." " .
  1954. "AND view_count = ".$this->attempt_id;
  1955. $res = api_sql_query($sql,__FILE__,__LINE__);
  1956. if(Database::num_rows($res)>0){
  1957. $row = Database::fetch_array($res);
  1958. $lp_iv_id = $row[0];
  1959. if($this->debug>2){error_log('New LP - In learnpathItem::write_to_db() - Got item_view_id '.$lp_iv_id.', now checking objectives ',0);}
  1960. foreach($this->objectives as $index => $objective){
  1961. $iva_table = Database::get_course_table('lp_iv_objective');
  1962. $iva_sql = "SELECT id FROM $iva_table " .
  1963. "WHERE lp_iv_id = $lp_iv_id " .
  1964. //"AND order_id = $index";
  1965. //also check for the objective ID as it must be unique for this SCO view
  1966. "AND objective_id = '".$objective[0]."'";
  1967. $iva_res = api_sql_query($iva_sql,__FILE__,__LINE__);
  1968. //id(0), type(1), time(2), weighting(3),correct_responses(4),student_response(5),result(6),latency(7)
  1969. if(Database::num_rows($iva_res)>0){
  1970. //update (or don't)
  1971. $iva_row = Database::fetch_array($iva_res);
  1972. $iva_id = $iva_row[0];
  1973. $ivau_sql = "UPDATE $iva_table " .
  1974. "SET objective_id = '".$objective[0]."'," .
  1975. "status = '".$objective[1]."'," .
  1976. "score_raw = '".$objective[2]."'," .
  1977. "score_min = '".$objective[4]."'," .
  1978. "score_max = '".$objective[3]."' " .
  1979. "WHERE id = $iva_id";
  1980. $ivau_res = api_sql_query($ivau_sql,__FILE__,__LINE__);
  1981. //error_log($ivau_sql,0);
  1982. }else{
  1983. //insert new one
  1984. $ivai_sql = "INSERT INTO $iva_table " .
  1985. "(lp_iv_id, order_id, objective_id, status, score_raw, score_min, score_max )" .
  1986. "VALUES" .
  1987. "(".$lp_iv_id.", ".$index.",'".$objective[0]."','".$objective[1]."'," .
  1988. "'".$objective[2]."','".$objective[4]."','".$objective[3]."')";
  1989. $ivai_res = api_sql_query($ivai_sql,__FILE__,__LINE__);
  1990. //error_log($ivai_sql);
  1991. }
  1992. }
  1993. }
  1994. }
  1995. else
  1996. {
  1997. //error_log('no objective to save: '.print_r($this->objectives,1));
  1998. }
  1999. }
  2000. /**
  2001. * Writes the current data to the database
  2002. * @return boolean Query result
  2003. */
  2004. function write_to_db()
  2005. {
  2006. if($this->debug>0){error_log('New LP - In learnpathItem::write_to_db()',0);}
  2007. $mode = $this->get_lesson_mode();
  2008. $credit = $this->get_credit();
  2009. if(($this->type == 'sco') && ($credit == 'no-credit' OR $mode == 'review' OR $mode == 'browse'))
  2010. {
  2011. //this info shouldn't be saved as the credit or lesson mode info prevent it
  2012. if($this->debug>1){error_log('New LP - In learnpathItem::write_to_db() - credit('.$credit.') or lesson_mode('.$mode.') prevent recording!',0);}
  2013. }else{
  2014. //check the row exists
  2015. $item_view_table = Database::get_course_table('lp_item_view');
  2016. $check = "SELECT * FROM $item_view_table " .
  2017. "WHERE lp_item_id = ".$this->db_id. " " .
  2018. "AND lp_view_id = ".$this->view_id. " ".
  2019. "AND view_count = ".$this->get_attempt_id();
  2020. if($this->debug>2){error_log('New LP - In learnpathItem::write_to_db() - Querying item_view: '.$check,0);}
  2021. $check_res = api_sql_query($check);
  2022. //depending on what we want (really), we'll update or insert a new row
  2023. //now save into DB
  2024. $res = 0;
  2025. if(Database::num_rows($check_res)<1){
  2026. $sql = "INSERT INTO $item_view_table " .
  2027. "(total_time, " .
  2028. "start_time, " .
  2029. "score, " .
  2030. "status, " .
  2031. "max_score, ".
  2032. "lp_item_id, " .
  2033. "lp_view_id, " .
  2034. "view_count, " .
  2035. "suspend_data, " .
  2036. //"max_time_allowed," .
  2037. "lesson_location)" .
  2038. "VALUES" .
  2039. "(".$this->get_total_time()."," .
  2040. "".$this->current_start_time."," .
  2041. "".$this->get_score()."," .
  2042. "'".$this->get_status(false)."'," .
  2043. "'".$this->get_max()."'," .
  2044. "".$this->db_id."," .
  2045. "".$this->view_id."," .
  2046. "".$this->get_attempt_id()."," .
  2047. "'".Database::escape_string($this->current_data)."'," .
  2048. //"'".$this->get_max_time_allowed()."'," .
  2049. "'".$this->lesson_location."')";
  2050. if($this->debug>2){error_log('New LP - In learnpathItem::write_to_db() - Inserting into item_view: '.$sql,0);}
  2051. $res = api_sql_query($sql,__FILE__,__LINE__);
  2052. $this->db_item_view_id = Database::get_last_insert_id();
  2053. }else{
  2054. $sql = '';
  2055. if($this->type=='hotpotatoes')
  2056. { //make an exception for HotPotatoes, don't update the score
  2057. //because it has been saved outside of this tool
  2058. $sql = "UPDATE $item_view_table " .
  2059. "SET total_time = ".$this->get_total_time().", " .
  2060. " start_time = ".$this->get_current_start_time().", " .
  2061. " score = ".$this->get_score().", " .
  2062. " status = '".$this->get_status(false)."'," .
  2063. " max_score = '".$this->get_max()."'," .
  2064. " suspend_data = '".Database::escape_string($this->current_data)."'," .
  2065. " lesson_location = '".$this->lesson_location."' " .
  2066. "WHERE lp_item_id = ".$this->db_id." " .
  2067. "AND lp_view_id = ".$this->view_id." " .
  2068. "AND view_count = ".$this->attempt_id;
  2069. }
  2070. else
  2071. { //for all other content types...
  2072. $sql = "UPDATE $item_view_table " .
  2073. "SET total_time = ".$this->get_total_time().", " .
  2074. " start_time = ".$this->get_current_start_time().", " .
  2075. " score = ".$this->get_score().", " .
  2076. " status = '".$this->get_status(false)."'," .
  2077. " max_score = '".$this->get_max()."'," .
  2078. " suspend_data = '".Database::escape_string($this->current_data)."'," .
  2079. //" max_time_allowed = '".$this->get_max_time_allowed()."'," .
  2080. " lesson_location = '".$this->lesson_location."' " .
  2081. "WHERE lp_item_id = ".$this->db_id." " .
  2082. "AND lp_view_id = ".$this->view_id." " .
  2083. "AND view_count = ".$this->attempt_id;
  2084. }
  2085. if($this->debug>2){error_log('New LP - In learnpathItem::write_to_db() - Updating item_view: '.$sql,0);}
  2086. $res = api_sql_query($sql,__FILE__,__LINE__);
  2087. }
  2088. //if(!$res)
  2089. //{
  2090. // $this->error = 'Could not update item_view table...'.mysql_error();
  2091. //}
  2092. if(is_array($this->interactions) && count($this->interactions)>0){
  2093. //save interactions
  2094. $tbl = Database::get_course_table('lp_item_view');
  2095. $sql = "SELECT id FROM $tbl " .
  2096. "WHERE lp_item_id = ".$this->db_id." " .
  2097. "AND lp_view_id = ".$this->view_id." " .
  2098. "AND view_count = ".$this->attempt_id;
  2099. $res = api_sql_query($sql,__FILE__,__LINE__);
  2100. if(Database::num_rows($res)>0){
  2101. $row = Database::fetch_array($res);
  2102. $lp_iv_id = $row[0];
  2103. if($this->debug>2){error_log('New LP - In learnpathItem::write_to_db() - Got item_view_id '.$lp_iv_id.', now checking interactions ',0);}
  2104. foreach($this->interactions as $index => $interaction){
  2105. $correct_resp = '';
  2106. if(is_array($interaction[4]) && !empty($interaction[4][0]) ){
  2107. foreach($interaction[4] as $resp){
  2108. $correct_resp .= $resp.',';
  2109. }
  2110. $correct_resp = substr($correct_resp,0,strlen($correct_resp)-1);
  2111. }
  2112. $iva_table = Database::get_course_table('lp_iv_interaction');
  2113. $iva_sql = "SELECT id FROM $iva_table " .
  2114. "WHERE lp_iv_id = $lp_iv_id " .
  2115. // "AND order_id = $index";
  2116. //also check for the interaction ID as it must be unique for this SCO view
  2117. "AND (order_id = $index " .
  2118. "OR interaction_id = '".$interaction[0]."')";
  2119. $iva_res = api_sql_query($iva_sql,__FILE__,__LINE__);
  2120. //id(0), type(1), time(2), weighting(3),correct_responses(4),student_response(5),result(6),latency(7)
  2121. if(Database::num_rows($iva_res)>0){
  2122. //update (or don't)
  2123. $iva_row = Database::fetch_array($iva_res);
  2124. $iva_id = $iva_row[0];
  2125. $ivau_sql = "UPDATE $iva_table " .
  2126. "SET interaction_id = '".$interaction[0]."'," .
  2127. "interaction_type = '".$interaction[1]."'," .
  2128. "weighting = '".$interaction[3]."'," .
  2129. "completion_time = '".$interaction[2]."'," .
  2130. "correct_responses = '".$correct_resp."'," .
  2131. "student_response = '".$interaction[5]."'," .
  2132. "result = '".$interaction[6]."'," .
  2133. "latency = '".$interaction[7]."'" .
  2134. "WHERE id = $iva_id";
  2135. $ivau_res = api_sql_query($ivau_sql,__FILE__,__LINE__);
  2136. }else{
  2137. //insert new one
  2138. $ivai_sql = "INSERT INTO $iva_table " .
  2139. "(order_id, lp_iv_id, interaction_id, interaction_type, " .
  2140. "weighting, completion_time, correct_responses, " .
  2141. "student_response, result, latency)" .
  2142. "VALUES" .
  2143. "(".$index.",".$lp_iv_id.",'".$interaction[0]."','".$interaction[1]."'," .
  2144. "'".$interaction[3]."','".$interaction[2]."','".$correct_resp."'," .
  2145. "'".$interaction[5]."','".$interaction[6]."','".$interaction[7]."'" .
  2146. ")";
  2147. $ivai_res = api_sql_query($ivai_sql,__FILE__,__LINE__);
  2148. }
  2149. }
  2150. }
  2151. }
  2152. }
  2153. if($this->debug>2){error_log('New LP - End of learnpathItem::write_to_db()',0);}
  2154. return true;
  2155. }
  2156. }
  2157. ?>