learnpathItem.class.php 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167
  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(strpos($mp3file,0,1) == '/')
  636. $files_list[] = array($mp3file,'local','abs');
  637. else
  638. $files_list[] = array($mp3file,'local','rel');
  639. }
  640. }
  641. if(strpos($source,'://') > 0)
  642. {
  643. //cut at '?' in a URL with params
  644. if(strpos($source,'?')>0)
  645. {
  646. $second_part = substr($source,strpos($source,'?'));
  647. if(strpos($second_part,'://')>0)
  648. {//if the second part of the url contains a url too, treat the second one before cutting
  649. $pos1 = strpos($second_part,'=');
  650. $pos2 = strpos($second_part,'&');
  651. $second_part = substr($second_part,$pos1+1,$pos2-($pos1+1));
  652. if(strpos($second_part,api_get_path(WEB_PATH))!==false)
  653. {
  654. //we found the current portal url
  655. $files_list[] = array($second_part,'local','url');
  656. $in_files_list[] = learnpathItem::get_resources_from_source(TOOL_DOCUMENT,$second_part,$recursivity+1);
  657. if(count($in_files_list)>0)
  658. {
  659. $files_list = array_merge($files_list,$in_files_list);
  660. }
  661. }
  662. else
  663. {
  664. //we didn't find any trace of current portal
  665. $files_list[] = array($second_part,'remote','url');
  666. }
  667. }
  668. elseif(strpos($second_part,'=')>0)
  669. {
  670. if(substr($second_part,0,1) === '/')
  671. { //link starts with a /, making it absolute (relative to DocumentRoot)
  672. $files_list[] = array($second_part,'local','abs');
  673. $in_files_list[] = learnpathItem::get_resources_from_source(TOOL_DOCUMENT,$second_part,$recursivity+1);
  674. if(count($in_files_list)>0)
  675. {
  676. $files_list = array_merge($files_list,$in_files_list);
  677. }
  678. }
  679. elseif(strstr($second_part,'..') === 0)
  680. { //link is relative but going back in the hierarchy
  681. $files_list[] = array($second_part,'local','rel');
  682. $dir = dirname($abs_path);
  683. $new_abs_path = realpath($dir.'/'.$second_part);
  684. $in_files_list[] = learnpathItem::get_resources_from_source(TOOL_DOCUMENT,$new_abs_path,$recursivity+1);
  685. if(count($in_files_list)>0)
  686. {
  687. $files_list = array_merge($files_list,$in_files_list);
  688. }
  689. }
  690. else
  691. { //no starting '/', making it relative to current document's path
  692. if(substr($second_part,0,2) == './')
  693. {
  694. $second_part = substr($second_part,2);
  695. }
  696. $files_list[] = array($second_part,'local','rel');
  697. $dir = dirname($abs_path);
  698. $new_abs_path = realpath($dir.'/'.$second_part);
  699. $in_files_list[] = learnpathItem::get_resources_from_source(TOOL_DOCUMENT,$new_abs_path,$recursivity+1);
  700. if(count($in_files_list)>0)
  701. {
  702. $files_list = array_merge($files_list,$in_files_list);
  703. }
  704. }
  705. }
  706. //leave that second part behind now
  707. $source = substr($source,0,strpos($source,'?'));
  708. if(strpos($source,'://') > 0)
  709. {
  710. if(strpos($source,api_get_path(WEB_PATH))!==false)
  711. {
  712. //we found the current portal url
  713. $files_list[] = array($source,'local','url');
  714. $in_files_list[] = learnpathItem::get_resources_from_source(TOOL_DOCUMENT,$source,$recursivity+1);
  715. if(count($in_files_list)>0)
  716. {
  717. $files_list = array_merge($files_list,$in_files_list);
  718. }
  719. }
  720. else
  721. {
  722. //we didn't find any trace of current portal
  723. $files_list[] = array($source,'remote','url');
  724. }
  725. }
  726. else
  727. {
  728. //no protocol found, make link local
  729. if(substr($source,0,1) === '/')
  730. { //link starts with a /, making it absolute (relative to DocumentRoot)
  731. $files_list[] = array($source,'local','abs');
  732. $in_files_list[] = learnpathItem::get_resources_from_source(TOOL_DOCUMENT,$source,$recursivity+1);
  733. if(count($in_files_list)>0)
  734. {
  735. $files_list = array_merge($files_list,$in_files_list);
  736. }
  737. }
  738. elseif(strstr($source,'..') === 0)
  739. { //link is relative but going back in the hierarchy
  740. $files_list[] = array($source,'local','rel');
  741. $dir = dirname($abs_path);
  742. $new_abs_path = realpath($dir.'/'.$source);
  743. $in_files_list[] = learnpathItem::get_resources_from_source(TOOL_DOCUMENT,$new_abs_path,$recursivity+1);
  744. if(count($in_files_list)>0)
  745. {
  746. $files_list = array_merge($files_list,$in_files_list);
  747. }
  748. }
  749. else
  750. { //no starting '/', making it relative to current document's path
  751. if(substr($source,0,2) == './')
  752. {
  753. $source = substr($source,2);
  754. }
  755. $files_list[] = array($source,'local','rel');
  756. $dir = dirname($abs_path);
  757. $new_abs_path = realpath($dir.'/'.$source);
  758. $in_files_list[] = learnpathItem::get_resources_from_source(TOOL_DOCUMENT,$new_abs_path,$recursivity+1);
  759. if(count($in_files_list)>0)
  760. {
  761. $files_list = array_merge($files_list,$in_files_list);
  762. }
  763. }
  764. }
  765. }
  766. //found some protocol there
  767. if(strpos($source,api_get_path(WEB_PATH))!==false)
  768. {
  769. //we found the current portal url
  770. $files_list[] = array($source,'local','url');
  771. $in_files_list[] = learnpathItem::get_resources_from_source(TOOL_DOCUMENT,$source,$recursivity+1);
  772. if(count($in_files_list)>0)
  773. {
  774. $files_list = array_merge($files_list,$in_files_list);
  775. }
  776. }
  777. else
  778. {
  779. //we didn't find any trace of current portal
  780. $files_list[] = array($source,'remote','url');
  781. }
  782. }
  783. else
  784. {
  785. //no protocol found, make link local
  786. if(substr($source,0,1) === '/')
  787. { //link starts with a /, making it absolute (relative to DocumentRoot)
  788. $files_list[] = array($source,'local','abs');
  789. $in_files_list[] = learnpathItem::get_resources_from_source(TOOL_DOCUMENT,$source,$recursivity+1);
  790. if(count($in_files_list)>0)
  791. {
  792. $files_list = array_merge($files_list,$in_files_list);
  793. }
  794. }
  795. elseif(strstr($source,'..') === 0)
  796. { //link is relative but going back in the hierarchy
  797. $files_list[] = array($source,'local','rel');
  798. $dir = dirname($abs_path);
  799. $new_abs_path = realpath($dir.'/'.$source);
  800. $in_files_list[] = learnpathItem::get_resources_from_source(TOOL_DOCUMENT,$new_abs_path,$recursivity+1);
  801. if(count($in_files_list)>0)
  802. {
  803. $files_list = array_merge($files_list,$in_files_list);
  804. }
  805. }
  806. else
  807. { //no starting '/', making it relative to current document's path
  808. if(substr($source,0,2) == './')
  809. {
  810. $source = substr($source,2);
  811. }
  812. $files_list[] = array($source,'local','rel');
  813. $dir = dirname($abs_path);
  814. $new_abs_path = realpath($dir.'/'.$source);
  815. $in_files_list[] = learnpathItem::get_resources_from_source(TOOL_DOCUMENT,$new_abs_path,$recursivity+1);
  816. if(count($in_files_list)>0)
  817. {
  818. $files_list = array_merge($files_list,$in_files_list);
  819. }
  820. }
  821. }
  822. }
  823. }
  824. }
  825. break;
  826. default:
  827. break;
  828. }
  829. }
  830. else
  831. {
  832. //the file could not be found
  833. return false;
  834. }
  835. break;
  836. default: //ignore
  837. break;
  838. }
  839. //error_log(str_repeat(' ',$recursivity),'found files '.print_r($files_list,true),0);
  840. //return $files_list;
  841. $checked_files_list = array();
  842. $checked_array_list = array();
  843. foreach($files_list as $idx => $file)
  844. {
  845. if(!empty($file[0]))
  846. {
  847. if(!in_array($file[0],$checked_files_list))
  848. {
  849. $checked_files_list[] = $files_list[$idx][0];
  850. $checked_array_list[] = $files_list[$idx];
  851. }
  852. }
  853. }
  854. return $checked_array_list;
  855. }
  856. /**
  857. * Gets the score
  858. * @return float The current score or 0 if no score set yet
  859. */
  860. function get_score(){
  861. if($this->debug>0){error_log('New LP - In learnpathItem::get_score()',0);}
  862. $res = 0;
  863. if($this->type == 'quiz'){
  864. //get score directly from db and update current
  865. $item_view_table = Database::get_course_table('lp_item_view');
  866. $check = "SELECT * FROM $item_view_table " .
  867. "WHERE lp_item_id = ".$this->db_id. " " .
  868. "AND lp_view_id = ".$this->view_id. " ".
  869. "AND view_count = ".$this->get_attempt_id();
  870. $resq = api_sql_query($check,__FILE__,__LINE__);
  871. if(Database::num_rows($resq)>0){
  872. $row = Database::fetch_array($resq);
  873. if(!empty($row['score'])){
  874. //update current object score
  875. $this->current_score = $row['score'];
  876. $res = $this->current_score;
  877. }
  878. }
  879. }else{
  880. if(!empty($this->current_score))
  881. {
  882. $res = $this->current_score;
  883. }
  884. }
  885. //TODO check this return value is valid for children classes (SCORM?)
  886. if($this->debug>1){error_log('New LP - Out of learnpathItem::get_score() - returning '.$res,0);}
  887. return $res;
  888. }
  889. /**
  890. * Gets the item status
  891. * @param boolean Do or don't check into the database for the latest value. Optional. Default is true
  892. * @param boolean Do or don't update the local attribute value with what's been found in DB
  893. * @return string Current status or 'Nnot attempted' if no status set yet
  894. */
  895. function get_status($check_db=true,$update_local=false)
  896. {
  897. if($this->debug>0){error_log('New LP - In learnpathItem::get_status() on item '.$this->db_id,0);}
  898. if($check_db)
  899. {
  900. if($this->debug>2){error_log('New LP - In learnpathItem::get_status(): checking db',0);}
  901. $table = Database::get_course_table('lp_item_view');
  902. $sql = "SELECT * FROM $table WHERE id = '".$this->db_item_view_id."' AND view_count = '".$this->get_attempt_id()."'";
  903. if($this->debug>2){error_log('New LP - In learnpathItem::get_status() - Checking DB: '.$sql,0);}
  904. $res = api_sql_query($sql);
  905. if(Database::num_rows($res)==1){
  906. $row = Database::fetch_array($res);
  907. if($update_local==true){
  908. $this->set_status($row['status']);
  909. }
  910. if($this->debug>2){error_log('New LP - In learnpathItem::get_status() - Returning db value '.$row['status'],0);}
  911. return $row['status'];
  912. }
  913. }
  914. else
  915. {
  916. if($this->debug>2){error_log('New LP - In learnpathItem::get_status() - in get_status: using attrib',0);}
  917. if(!empty($this->status))
  918. {
  919. if($this->debug>2){error_log('New LP - In learnpathItem::get_status() - Returning attrib: '.$this->status,0);}
  920. return $this->status;
  921. }
  922. }
  923. if($this->debug>2){error_log('New LP - In learnpathItem::get_status() - Returning default '.$this->possible_status[0],0);}
  924. return $this->possible_status[0];
  925. }
  926. /**
  927. * Gets the suspend data
  928. */
  929. function get_suspend_data()
  930. {
  931. if($this->debug>0){error_log('New LP - In learnpathItem::get_suspend_data()',0);}
  932. //TODO : improve cleaning of breaklines ... it works but is it really a beautiful way to do it ?
  933. if(!empty($this->current_data)){return str_replace(array("\r","\n"),array('\r','\n'),$this->current_data);}else{return '';}
  934. }
  935. /**
  936. * Gets the total time spent on this item view so far
  937. * @param string Origin of the request. If coming from PHP, send formatted as xxhxx'xx", otherwise use scorm format 00:00:00
  938. * @param integer Given time is a default time to return formatted
  939. */
  940. function get_scorm_time($origin='php',$given_time=null){
  941. //if($this->debug>0){error_log('New LP - In learnpathItem::get_scorm_time()',0);}
  942. $h = get_lang('h');
  943. if(!isset($given_time)){
  944. if(!empty($this->current_start_time) && is_object($this)){
  945. if(!empty($this->current_stop_time)){
  946. $time = $this->current_stop_time - $this->current_start_time;
  947. }else{
  948. $time = time() - $this->current_start_time;
  949. }
  950. }else{
  951. if($origin == 'js'){
  952. return '00:00:00';
  953. }else{
  954. return '00'.$h.'00\'00"';
  955. }
  956. }
  957. }else{
  958. $time = $given_time;
  959. }
  960. $hours = $time/3600;
  961. $mins = ($time%3600)/60;
  962. $secs = ($time%60);
  963. if($origin == 'js'){
  964. $scorm_time = trim(sprintf("%4d:%02d:%02d",$hours,$mins,$secs));
  965. }else{
  966. $scorm_time = trim(sprintf("%4d$h%02d'%02d\"",$hours,$mins,$secs));
  967. }
  968. return $scorm_time;
  969. }
  970. /**
  971. * Returns the item's title
  972. * @return string Title
  973. */
  974. function get_title(){
  975. if($this->debug>0){error_log('New LP - In learnpathItem::get_title()',0);}
  976. if(empty($this->title)){return '';}
  977. return $this->title;
  978. }
  979. /**
  980. * Returns the total time used to see that item
  981. * @return integer Total time
  982. */
  983. function get_total_time(){
  984. if($this->debug>0){error_log('New LP - In learnpathItem::get_total_time()',0);}
  985. if($this->current_start_time == 0){ //shouldn't be necessary thanks to the open() method
  986. $this->current_start_time = time();
  987. }
  988. $time = $this->current_stop_time - $this->current_start_time;
  989. if($time < 0){
  990. return 0;
  991. }else{
  992. 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);}
  993. return $time;
  994. }
  995. }
  996. /**
  997. * Gets the item type
  998. * @return string The item type (can be doc, dir, sco, asset)
  999. */
  1000. function get_type()
  1001. {
  1002. $res = 'asset';
  1003. if($this->debug>0){error_log('New LP - In learnpathItem::get_type() on item '.$this->db_id,0);}
  1004. if(!empty($this->type))
  1005. {
  1006. //error_log('In item::get_type() - returning '.$this->type,0);
  1007. $res = $this->type;
  1008. }
  1009. if($this->debug>2){error_log('New LP - In learnpathItem::get_type() - Returning '.$res.' for item '.$this->db_id,0);}
  1010. return $res;
  1011. }
  1012. /**
  1013. * Gets the view count for this item
  1014. */
  1015. function get_view_count(){
  1016. if($this->debug>0){error_log('New LP - In learnpathItem::get_view_count()',0);}
  1017. if(!empty($this->attempt_id)){
  1018. return $this->attempt_id;
  1019. }else{
  1020. return 0;
  1021. }
  1022. }
  1023. /**
  1024. * Tells if an item is done ('completed','passed','succeeded') or not
  1025. * @return bool True if the item is done ('completed','passed','succeeded'), false otherwise
  1026. */
  1027. function is_done(){
  1028. if($this->status_is(array('completed','passed','succeeded'))){
  1029. if($this->debug>2){error_log('New LP - In learnpath::is_done() - Item '.$this->get_id().' is complete',0);}
  1030. return true;
  1031. }else{
  1032. if($this->debug>2){error_log('New LP - In learnpath::is_done() - Item '.$this->get_id().' is not complete',0);}
  1033. return false;
  1034. }
  1035. }
  1036. /**
  1037. * Tells if a restart is allowed (take it from $this->prevent_reinit and $this->status)
  1038. * @return integer -1 if retaking the sco another time for credit is not allowed,
  1039. * 0 if it is not allowed but the item has to be finished
  1040. * 1 if it is allowed. Defaults to 1
  1041. */
  1042. function is_restart_allowed()
  1043. {
  1044. if($this->debug>2){error_log('New LP - In learnpathItem::is_restart_allowed()',0);}
  1045. $restart = 1;
  1046. $mystatus = $this->get_status(true);
  1047. if($this->get_prevent_reinit() > 0){ //if prevent_reinit == 1 (or more)
  1048. //if status is not attempted or incomplete, authorize retaking (of the same) anyway. Otherwise:
  1049. if($mystatus != $this->possible_status[0] AND $mystatus != $this->possible_status[1]){
  1050. $restart = -1;
  1051. }else{
  1052. $restart = 0;
  1053. }
  1054. }else{
  1055. if($mystatus == $this->possible_status[0] OR $mystatus == $this->possible_status[1]){
  1056. $restart = -1;
  1057. }
  1058. }
  1059. if($this->debug>2){error_log('New LP - End of learnpathItem::is_restart_allowed() - Returning '.$restart,0);}
  1060. return $restart;
  1061. }
  1062. /**
  1063. * Opens/launches the item. Initialises runtime values.
  1064. * @return boolean True on success, false on failure.
  1065. */
  1066. function open($allow_new_attempt=false){
  1067. if($this->debug>0){error_log('New LP - In learnpathItem::open()',0);}
  1068. if($this->prevent_reinit == 0)
  1069. {
  1070. $this->current_score = 0;
  1071. $this->current_start_time = time();
  1072. //In this case, as we are opening the item, what is important to us
  1073. //is the database status, in order to know if this item has already
  1074. //been used in the past (rather than just loaded and modified by
  1075. //some javascript but not written in the database).
  1076. //If the database status is different from 'not attempted', we can
  1077. //consider this item has already been used, and as such we can
  1078. //open a new attempt. Otherwise, we'll just reuse the current
  1079. //attempt, which is generally created the first time the item is
  1080. //loaded (for example as part of the table of contents)
  1081. $stat = $this->get_status(true);
  1082. if($allow_new_attempt && isset($stat) && ($stat != $this->possible_status[0]))
  1083. {
  1084. $this->attempt_id = $this->attempt_id + 1; //open a new attempt
  1085. }
  1086. $this->status = $this->possible_status[1];
  1087. }
  1088. else
  1089. {
  1090. if($this->current_start_time == 0)
  1091. { //small exception for start time, to avoid amazing values
  1092. $this->current_start_time = time();
  1093. }
  1094. //error_log('New LP - reinit blocked by setting',0);
  1095. }
  1096. }
  1097. /**
  1098. * Outputs the item contents
  1099. * @return string HTML file (displayable in an <iframe>) or empty string if no path defined
  1100. */
  1101. function output()
  1102. {
  1103. if($this->debug>0){error_log('New LP - In learnpathItem::output()',0);}
  1104. if(!empty($this->path) and is_file($this->path))
  1105. {
  1106. $output = '';
  1107. $output .= file_get_contents($this->path);
  1108. return $output;
  1109. }
  1110. return '';
  1111. }
  1112. /**
  1113. * Parses the prerequisites string with the AICC logic language
  1114. * @param string The prerequisites string as it figures in imsmanifest.xml
  1115. * @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
  1116. * @param Array List of references (the "ref" column in the lp_item table) that are strings used in the expression of prerequisites.
  1117. * @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)
  1118. * @return boolean True if the list of prerequisites given is entirely satisfied, false otherwise
  1119. */
  1120. function parse_prereq($prereqs_string, $items, $refs_list,$user_id){
  1121. if($this->debug>0){error_log('New LP - In learnpathItem::parse_prereq() for learnpath '.$this->lp_id.' with string '.$prereqs_string,0);}
  1122. //deal with &, |, ~, =, <>, {}, ,, X*, () in reverse order
  1123. $this->prereq_alert = '';
  1124. // First parse all parenthesis by using a sequential loop (looking for less-inclusives first)
  1125. if($prereqs_string == '_true_'){return true;}
  1126. if($prereqs_string == '_false_'){
  1127. if(empty($this->prereq_alert)){
  1128. $this->prereq_alert = get_lang('_prereq_not_complete');
  1129. }
  1130. return false;
  1131. }
  1132. while(strpos($prereqs_string,'(')!==false){
  1133. //remove any () set and replace with its value
  1134. $matches = array();
  1135. $res = preg_match_all('/(\(([^\(\)]*)\))/',$prereqs_string,$matches);
  1136. if($res){
  1137. foreach($matches[2] as $id=>$match){
  1138. $str_res = $this->parse_prereq($match,$items,$refs_list,$user_id);
  1139. if($str_res){
  1140. $prereqs_string = str_replace($matches[1][$id],'_true_',$prereqs_string);
  1141. }else{
  1142. $prereqs_string = str_replace($matches[1][$id],'_false_',$prereqs_string);
  1143. }
  1144. }
  1145. }
  1146. }
  1147. //parenthesis removed, now look for ORs as it is the lesser-priority binary operator (= always uses one text operand)
  1148. if(strpos($prereqs_string,"|")===false){
  1149. if($this->debug>1){error_log('New LP - Didnt find any OR, looking for AND',0);}
  1150. if(strpos($prereqs_string,"&")!==false){
  1151. $list = split("&",$prereqs_string);
  1152. if(count($list)>1){
  1153. $andstatus = true;
  1154. foreach($list as $condition){
  1155. $andstatus = $andstatus && $this->parse_prereq($condition,$items,$refs_list,$user_id);
  1156. if($andstatus==false){
  1157. if($this->debug>1){error_log('New LP - One condition in AND was false, short-circuit',0);}
  1158. break;
  1159. }
  1160. }
  1161. if(empty($this->prereq_alert) && !$andstatus){
  1162. $this->prereq_alert = get_lang('_prereq_not_complete');
  1163. }
  1164. return $andstatus;
  1165. }else{
  1166. if(isset($items[$refs_list[$list[0]]])){
  1167. $status = $items[$refs_list[$list[0]]]->get_status(true);
  1168. $returnstatus = (($status == $this->possible_status[2]) OR ($status == $this->possible_status[3]));
  1169. if(empty($this->prereq_alert) && !$returnstatus){
  1170. $this->prereq_alert = get_lang('_prereq_not_complete');
  1171. }
  1172. return $returnstatus;
  1173. }
  1174. $this->prereq_alert = get_lang('_prereq_not_complete');
  1175. return false;
  1176. }
  1177. }else{
  1178. //no ORs found, now look for ANDs
  1179. if($this->debug>1){error_log('New LP - Didnt find any AND, looking for =',0);}
  1180. if(strpos($prereqs_string,"=")!==false){
  1181. if($this->debug>1){error_log('New LP - Found =, looking into it',0);}
  1182. //we assume '=' signs only appear when there's nothing else around
  1183. $params = split('=',$prereqs_string);
  1184. if(count($params) == 2){
  1185. //right number of operands
  1186. if(isset($items[$refs_list[$params[0]]])){
  1187. $status = $items[$refs_list[$params[0]]]->get_status(true);
  1188. $returnstatus = ($status == $params[1]);
  1189. if(empty($this->prereq_alert) && !$returnstatus){
  1190. $this->prereq_alert = get_lang('_prereq_not_complete');
  1191. }
  1192. return $returnstatus;
  1193. }
  1194. $this->prereq_alert = get_lang('_prereq_not_complete');
  1195. return false;
  1196. }
  1197. }else{
  1198. //No ANDs found, look for <>
  1199. if($this->debug>1){error_log('New LP - Didnt find any =, looking for <>',0);}
  1200. if(strpos($prereqs_string,"<>")!==false){
  1201. if($this->debug>1){error_log('New LP - Found <>, looking into it',0);}
  1202. //we assume '<>' signs only appear when there's nothing else around
  1203. $params = split('<>',$prereqs_string);
  1204. if(count($params) == 2){
  1205. //right number of operands
  1206. if(isset($items[$refs_list[$params[0]]])){
  1207. $status = $items[$refs_list[$params[0]]]->get_status(true);
  1208. $returnstatus = ($status != $params[1]);
  1209. if(empty($this->prereq_alert) && !$returnstatus){
  1210. $this->prereq_alert = get_lang('_prereq_not_complete');
  1211. }
  1212. return $returnstatus;
  1213. }
  1214. $this->prereq_alert = get_lang('_prereq_not_complete');
  1215. return false;
  1216. }
  1217. }else{
  1218. //No <> found, look for ~ (unary)
  1219. if($this->debug>1){error_log('New LP - Didnt find any =, looking for ~',0);}
  1220. //only remains: ~ and X*{}
  1221. if(strpos($prereqs_string,"~")!==false){
  1222. //found NOT
  1223. if($this->debug>1){error_log('New LP - Found ~, looking into it',0);}
  1224. $list = array();
  1225. $myres = preg_match('/~([^(\d+\*)\{]*)/',$prereqs_string,$list);
  1226. if($myres){
  1227. $returnstatus = !$this->parse_prereq($list[1],$items,$refs_list,$user_id);
  1228. if(empty($this->prereq_alert) && !$returnstatus){
  1229. $this->prereq_alert = get_lang('_prereq_not_complete');
  1230. }
  1231. return $returnstatus;
  1232. }else{
  1233. //strange...
  1234. if($this->debug>1){error_log('New LP - Found ~ but strange string: '.$prereqs_string,0);}
  1235. }
  1236. }else{
  1237. //Finally, look for sets/groups
  1238. if($this->debug>1){error_log('New LP - Didnt find any ~, looking for groups',0);}
  1239. //only groups here
  1240. $groups = array();
  1241. $groups_there = preg_match_all('/((\d+\*)?\{([^\}]+)\}+)/',$prereqs_string,$groups);
  1242. if($groups_there){
  1243. foreach($groups[1] as $gr) //only take the results that correspond to the big brackets-enclosed condition
  1244. {
  1245. if($this->debug>1){error_log('New LP - Dealing with group '.$gr,0);}
  1246. $multi = array();
  1247. $mycond = false;
  1248. if(preg_match('/(\d+)\*\{([^\}]+)\}/',$gr,$multi)){
  1249. if($this->debug>1){error_log('New LP - Found multiplier '.$multi[0],0);}
  1250. $count = $multi[1];
  1251. $list = split(',',$multi[2]);
  1252. $mytrue = 0;
  1253. foreach($list as $cond){
  1254. if(isset($items[$refs_list[$cond]])){
  1255. $status = $items[$refs_list[$cond]]->get_status(true);
  1256. if (($status == $this->possible_status[2]) OR ($status == $this->possible_status[3])){
  1257. $mytrue ++;
  1258. if($this->debug>1){error_log('New LP - Found true item, counting.. ('.($mytrue).')',0);}
  1259. }
  1260. }else{
  1261. if($this->debug>1){error_log('New LP - item '.$cond.' does not exist in items list',0);}
  1262. }
  1263. }
  1264. if($mytrue >= $count){
  1265. if($this->debug>1){error_log('New LP - Got enough true results, return true',0);}
  1266. $mycond = true;
  1267. }else{
  1268. if($this->debug>1){error_log('New LP - Not enough true results',0);}
  1269. }
  1270. }
  1271. else{
  1272. if($this->debug>1){error_log('New LP - No multiplier',0);}
  1273. $list = split(',',$gr);
  1274. $mycond = true;
  1275. foreach($list as $cond){
  1276. if(isset($items[$refs_list[$cond]])){
  1277. $status = $items[$refs_list[$cond]]->get_status(true);
  1278. if (($status == $this->possible_status[2]) OR ($status == $this->possible_status[3])){
  1279. $mycond = true;
  1280. if($this->debug>1){error_log('New LP - Found true item',0);}
  1281. }else{
  1282. if($this->debug>1){error_log('New LP - Found false item, the set is not true, return false',0);}
  1283. $mycond = false;
  1284. break;
  1285. }
  1286. }else{
  1287. if($this->debug>1){error_log('New LP - item '.$cond.' does not exist in items list',0);}
  1288. if($this->debug>1){error_log('New LP - Found false item, the set is not true, return false',0);}
  1289. $mycond = false;
  1290. break;
  1291. }
  1292. }
  1293. }
  1294. if(!$mycond && empty($this->prereq_alert)){
  1295. $this->prereq_alert = get_lang('_prereq_not_complete');
  1296. }
  1297. return $mycond;
  1298. }
  1299. }else{
  1300. //Nothing found there either. Now return the value of the corresponding resource completion status
  1301. if($this->debug>1){error_log('New LP - Didnt find any group, returning value for '.$prereqs_string,0);}
  1302. if(isset($items[$refs_list[$prereqs_string]])){
  1303. if($items[$refs_list[$prereqs_string]]->type == 'quiz')
  1304. {
  1305. $sql = 'SELECT exe_result, exe_weighting
  1306. FROM '.Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES).'
  1307. WHERE exe_exo_id = '.$items[$refs_list[$prereqs_string]]->path.'
  1308. AND exe_user_id = '.$user_id.'
  1309. ORDER BY exe_date DESC
  1310. LIMIT 0, 1';
  1311. $rs_quiz = api_sql_query($sql, __FILE__, __LINE__);
  1312. if($quiz = Database :: fetch_array($rs_quiz))
  1313. {
  1314. if($quiz['exe_result'] >= $items[$refs_list[$prereqs_string]]->get_mastery_score())
  1315. {
  1316. $returnstatus = true;
  1317. }
  1318. else
  1319. {
  1320. $this->prereq_alert = get_lang('_prereq_not_complete');
  1321. $returnstatus = false;
  1322. }
  1323. }
  1324. else
  1325. {
  1326. $this->prereq_alert = get_lang('_prereq_not_complete');
  1327. $returnstatus = false;
  1328. }
  1329. return $returnstatus;
  1330. }
  1331. else
  1332. {
  1333. $status = $items[$refs_list[$prereqs_string]]->get_status(true);
  1334. $returnstatus = (($status == $this->possible_status[2]) OR ($status == $this->possible_status[3]));
  1335. if(!$returnstatus && empty($this->prereq_alert)){
  1336. $this->prereq_alert = get_lang('_prereq_not_complete');
  1337. }
  1338. if(!$returnstatus){
  1339. if($this->debug>1){error_log('New LP - Prerequisite '.$prereqs_string.' not complete',0);}
  1340. }else{
  1341. if($this->debug>1){error_log('New LP - Prerequisite '.$prereqs_string.' complete',0);}
  1342. }
  1343. return $returnstatus;
  1344. }
  1345. }else{
  1346. if($this->debug>1){error_log('New LP - Could not find '.$prereqs_string.' in '.print_r($refs_list,true),0);}
  1347. }
  1348. }
  1349. }
  1350. }
  1351. }
  1352. }
  1353. }else{
  1354. $list = split("\|",$prereqs_string);
  1355. if(count($list)>1){
  1356. if($this->debug>1){error_log('New LP - Found OR, looking into it',0);}
  1357. $orstatus = false;
  1358. foreach($list as $condition){
  1359. if($this->debug>1){error_log('New LP - Found OR, adding it ('.$condition.')',0);}
  1360. $orstatus = $orstatus || $this->parse_prereq($condition,$items,$refs_list,$user_id);
  1361. if($orstatus == true){
  1362. //shortcircuit OR
  1363. if($this->debug>1){error_log('New LP - One condition in OR was true, short-circuit',0);}
  1364. break;
  1365. }
  1366. }
  1367. if(!$orstatus && empty($this->prereq_alert)){
  1368. $this->prereq_alert = get_lang('_prereq_not_complete');
  1369. }
  1370. return $orstatus;
  1371. }else{
  1372. if($this->debug>1){error_log('New LP - OR was found but only one elem present !?',0);}
  1373. if(isset($items[$refs_list[$list[0]]])){
  1374. $status = $items[$refs_list[$list[0]]]->get_status(true);
  1375. $returnstatus = (($status == 'completed') OR ($status == 'passed'));
  1376. if(!$returnstatus && empty($this->prereq_alert)){
  1377. $this->prereq_alert = get_lang('_prereq_not_complete');
  1378. }
  1379. return $returnstatus;
  1380. }
  1381. }
  1382. }
  1383. if(empty($this->prereq_alert)){
  1384. $this->prereq_alert = get_lang('_prereq_not_complete');
  1385. }
  1386. if($this->debug>1){error_log('New LP - End of parse_prereq. Error code is now '.$this->prereq_alert,0);}
  1387. return false;
  1388. }
  1389. /**
  1390. * Parses the HTML attributes given as string.
  1391. *
  1392. * @param string HTML attribute string
  1393. * @param array List of attributes that we want to get back
  1394. * @return array An associative array of attributes
  1395. * @author Based on a function from the HTML_Common2 PEAR module
  1396. */
  1397. function parse_HTML_attributes($attrString,$wanted=array())
  1398. {
  1399. //error_log('Entering parse_HTML_attributes',0);
  1400. $attributes = array();
  1401. $regs = array();
  1402. $reduced = false;
  1403. if(count($wanted)>0)
  1404. {
  1405. $reduced = true;
  1406. }
  1407. //error_log('launching preg_match',0);
  1408. try {
  1409. $res = preg_match_all(
  1410. "/(((([A-Za-z_:])([A-Za-z0-9_:\\.-]|[^\\x00-\\x7F])*)" .
  1411. "([ \\n\\t\\r]+)?(" .
  1412. "(=([ \\n\\t\\r]+)?(\"[^\"]*\"|'[^']*'|[^ \\n\\t\\r]*))" .
  1413. "|" .
  1414. "(\\(([ \\n\\t\\r]+)?(\"[^\"]*\"|'[^']*'|[^ \\n\\t\\r]*)\\))" .
  1415. "))" .
  1416. "|" .
  1417. "(@import([ \\n\\t\\r]+)?(\"[^\"]*\"|'[^']*'|[^ \\n\\t\\r]*)))?/",
  1418. $attrString,
  1419. $regs
  1420. );
  1421. } catch (Exception $e) {
  1422. error_log('Caught exception: '. $e->getMessage(),0) ;
  1423. }
  1424. if ($res) {
  1425. //error_log('preg_match ok',0);
  1426. for ($i = 0; $i < count($regs[1]); $i++) {
  1427. $name = trim($regs[3][$i]);
  1428. $check = trim($regs[0][$i]);
  1429. $value = trim($regs[10][$i]);
  1430. if(empty($value) and !empty($regs[13][$i]))
  1431. {
  1432. $value = $regs[13][$i];
  1433. }
  1434. if(empty($name) && !empty($regs[16][$i]))
  1435. {
  1436. $name = '@import';
  1437. $value = trim($regs[16][$i]);
  1438. }
  1439. if(!empty($name))
  1440. {
  1441. if(!$reduced OR in_array(strtolower($name),$wanted))
  1442. {
  1443. if ($name == $check) {
  1444. $attributes[strtolower($name)][] = strtolower($name);
  1445. } else {
  1446. if (!empty($value) && ($value[0] == '\'' || $value[0] == '"')) {
  1447. $value = substr($value, 1, -1);
  1448. }
  1449. $attributes[strtolower($name)][] = $value;
  1450. }
  1451. }
  1452. }
  1453. }
  1454. }else{
  1455. error_log('preg_match did not find anything',0);
  1456. }
  1457. //error_log('Exiting parse_HTML_attributes() - found '.print_r($attributes,true),0);
  1458. return $attributes;
  1459. }
  1460. /**
  1461. * Reinits all local values as the learnpath is restarted
  1462. * @return boolean True on success, false otherwise
  1463. */
  1464. function restart()
  1465. {
  1466. if($this->debug>0){error_log('New LP - In learnpathItem::restart()',0);}
  1467. $this->save();
  1468. $allowed = $this->is_restart_allowed();
  1469. if($allowed === -1){
  1470. //nothing allowed, do nothing
  1471. }elseif($allowed === 1){
  1472. //restart as new attempt is allowed, record a new attempt
  1473. $this->attempt_id = $this->attempt_id + 1; //simply reuse the previous attempt_id
  1474. $this->current_score = 0;
  1475. $this->current_start_time = 0;
  1476. $this->current_stop_time = 0;
  1477. $this->current_data = '';
  1478. $this->status = $this->possible_status[0];
  1479. $this->interactions_count = 0;
  1480. $this->interactions = array();
  1481. $this->objectives_count = 0;
  1482. $this->objectives = array();
  1483. $this->lesson_location = '';
  1484. $this->write_to_db();
  1485. }else{
  1486. //restart current element is allowed (because it's not finished yet),
  1487. // reinit current
  1488. $this->current_score = 0;
  1489. $this->current_start_time = 0;
  1490. $this->current_stop_time = 0;
  1491. $this->current_data = '';
  1492. $this->status = $this->possible_status[0];
  1493. }
  1494. return true;
  1495. }
  1496. /**
  1497. * Saves data in the database
  1498. * @param boolean Save from URL params (1) or from object attributes (0)
  1499. * @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
  1500. * @return boolean True on success, false on failure
  1501. */
  1502. function save($from_outside=true,$prereqs_complete=false)
  1503. {
  1504. if($this->debug>0){error_log('New LP - In learnpathItem::save()',0);}
  1505. //$item_view_table = Database::get_course_table(COURSEID,LEARNPATH_ITEM_VIEW_TABLE);
  1506. $item_id = $this->get_id();
  1507. //first check if parameters passed via GET can be saved here
  1508. //in case it's a SCORM, we should get:
  1509. if($this->type == 'sco' || $this->type== 'au'){
  1510. $s = $this->get_status(true);
  1511. if($this->prevent_reinit == 1 AND
  1512. $s != $this->possible_status[0] AND $s != $this->possible_status[1]){
  1513. if($this->debug>1){error_log('New LP - In learnpathItem::save() - save reinit blocked by setting',0);}
  1514. //do nothing because the status has already been set. Don't allow it to change.
  1515. //TODO check there isn't a special circumstance where this should be saved
  1516. }else{
  1517. if($this->debug>1){error_log('New LP - In learnpathItem::save() - SCORM save request received',0);}
  1518. //get all new settings from the URL
  1519. if($from_outside==true){
  1520. if($this->debug>1){error_log('New LP - In learnpathItem::save() - Getting item data from outside',0);}
  1521. foreach($_GET as $param => $value)
  1522. {
  1523. $value = Database::escape_string($value);
  1524. switch($param){
  1525. case 'score':
  1526. $this->set_score($value);
  1527. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting score to '.$value,0);}
  1528. break;
  1529. case 'max':
  1530. $this->set_max_score($value);
  1531. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting view_max_score to '.$value,0);}
  1532. break;
  1533. case 'min':
  1534. $this->min_score = $value;
  1535. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting min_score to '.$value,0);}
  1536. break;
  1537. case 'lesson_status':
  1538. if(!empty($value)){
  1539. $this->set_status($value);
  1540. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting status to '.$value,0);}
  1541. }
  1542. break;
  1543. case 'time':
  1544. $this->set_time($value);
  1545. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting time to '.$value,0);}
  1546. break;
  1547. case 'suspend_data':
  1548. $this->current_data = $value;
  1549. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting suspend_data to '.$value,0);}
  1550. break;
  1551. case 'lesson_location':
  1552. $this->set_lesson_location($value);
  1553. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting lesson_location to '.$value,0);}
  1554. break;
  1555. case 'core_exit':
  1556. $this->set_core_exit($value);
  1557. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting core_exit to '.$value,0);}
  1558. break;
  1559. case 'interactions':
  1560. //$interactions = unserialize($value);
  1561. //foreach($interactions as $interaction){
  1562. // ;
  1563. //}
  1564. break;
  1565. case 'objectives':
  1566. break;
  1567. //case 'maxtimeallowed':
  1568. // $this->set_max_time_allowed($value);
  1569. // break;
  1570. /*
  1571. case 'objectives._count':
  1572. $this->attempt_id = $value;
  1573. break;
  1574. */
  1575. default:
  1576. //ignore
  1577. break;
  1578. }
  1579. }
  1580. }else{
  1581. if($this->debug>1){error_log('New LP - In learnpathItem::save() - Using inside item status',0);}
  1582. //do nothing, just let the local attributes be used
  1583. }
  1584. }
  1585. }else{ //if not SCO, such messages should not be expected
  1586. $type = strtolower($this->type);
  1587. switch($type){
  1588. case 'asset':
  1589. if($prereqs_complete)
  1590. {
  1591. $this->set_status($this->possible_status[2]);
  1592. }
  1593. break;
  1594. case TOOL_HOTPOTATOES:
  1595. break;
  1596. case TOOL_QUIZ:
  1597. default:
  1598. //for now, everything that is not sco and not asset is set to
  1599. //completed when saved
  1600. if($prereqs_complete)
  1601. {
  1602. $this->set_status($this->possible_status[2]);
  1603. }
  1604. break;
  1605. }
  1606. }
  1607. //$time = $this->time
  1608. if($this->debug>1){error_log('New LP - End of learnpathItem::save() - Calling write_to_db()',0);}
  1609. return $this->write_to_db();
  1610. }
  1611. /**
  1612. * Sets the number of attempt_id to a given value
  1613. * @param integer The given value to set attempt_id to
  1614. * @return boolean TRUE on success, FALSE otherwise
  1615. */
  1616. function set_attempt_id($num)
  1617. {
  1618. if($this->debug>0){error_log('New LP - In learnpathItem::set_attempt_id()',0);}
  1619. if($num == strval(intval($num)) && $num>=0){
  1620. $this->attempt_id = $num;
  1621. return true;
  1622. }
  1623. return false;
  1624. }
  1625. /**
  1626. * Sets the core_exit value to the one given
  1627. */
  1628. function set_core_exit($value)
  1629. {
  1630. switch($value){
  1631. case '':
  1632. $this->core_exit = '';
  1633. break;
  1634. case 'suspend':
  1635. $this->core_exit = 'suspend';
  1636. break;
  1637. default:
  1638. $this->core_exit = 'none';
  1639. break;
  1640. }
  1641. return true;
  1642. }
  1643. /**
  1644. * Sets the item's description
  1645. * @param string Description
  1646. */
  1647. function set_description($string=''){
  1648. if($this->debug>0){error_log('New LP - In learnpathItem::set_description()',0);}
  1649. if(!empty($string)){$this->description = $string;}
  1650. }
  1651. /**
  1652. * Sets the lesson_location value
  1653. * @param string lesson_location as provided by the SCO
  1654. * @return boolean True on success, false otherwise
  1655. */
  1656. function set_lesson_location($location)
  1657. {
  1658. if($this->debug>0){error_log('New LP - In learnpathItem::set_lesson_location()',0);}
  1659. if(isset($location)){
  1660. $this->lesson_location = Database::escape_string($location);
  1661. return true;
  1662. }
  1663. return false;
  1664. }
  1665. /**
  1666. * Sets the item's depth level in the LP tree (0 is at root)
  1667. * @param integer Level
  1668. */
  1669. function set_level($int=0){
  1670. if($this->debug>0){error_log('New LP - In learnpathItem::set_level('.$int.')',0);}
  1671. if(!empty($int) AND $int == strval(intval($int))){$this->level = $int;}
  1672. }
  1673. /**
  1674. * Sets the lp_view id this item view is registered to
  1675. * @param integer lp_view DB ID
  1676. * @todo //todo insert into lp_item_view if lp_view not exists
  1677. */
  1678. function set_lp_view($lp_view_id)
  1679. {
  1680. if($this->debug>0){error_log('New LP - In learnpathItem::set_lp_view('.$lp_view_id.')',0);}
  1681. if(!empty($lp_view_id) and $lp_view_id = intval(strval($lp_view_id)))
  1682. {
  1683. $this->view_id = $lp_view_id;
  1684. $item_view_table = Database::get_course_table('lp_item_view');
  1685. //get the lp_item_view with the highest view_count
  1686. $sql = "SELECT * FROM $item_view_table WHERE lp_item_id = ".$this->get_id()." " .
  1687. " AND lp_view_id = ".$lp_view_id." ORDER BY view_count DESC";
  1688. if($this->debug>2){error_log('New LP - In learnpathItem::set_lp_view() - Querying lp_item_view: '.$sql,0);}
  1689. $res = api_sql_query($sql,__FILE__,__LINE__);
  1690. if(Database::num_rows($res)>0){
  1691. $row = Database::fetch_array($res);
  1692. $this->db_item_view_id = $row['id'];
  1693. $this->attempt_id = $row['view_count'];
  1694. $this->current_score = $row['score'];
  1695. $this->current_data = $row['suspend_data'];
  1696. $this->view_max_score = $row['max_score'];
  1697. //$this->view_min_score = $row['min_score'];
  1698. $this->status = $row['status'];
  1699. $this->current_start_time = $row['start_time'];
  1700. $this->current_stop_time = $this->current_start_time + $row['total_time'];
  1701. $this->lesson_location = $row['lesson_location'];
  1702. $this->core_exit = $row['core_exit'];
  1703. if($this->debug>2){error_log('New LP - In learnpathItem::set_lp_view() - Updated item object with database values',0);}
  1704. //now get the number of interactions for this little guy
  1705. $item_view_interaction_table = Database::get_course_table('lp_iv_interaction');
  1706. $sql = "SELECT * FROM $item_view_interaction_table WHERE lp_iv_id = '".$this->db_item_view_id."'";
  1707. $res = api_sql_query($sql,__FILE__,__LINE__);
  1708. if($res !== false){
  1709. $this->interactions_count = Database::num_rows($res);
  1710. }else{
  1711. $this->interactions_count = 0;
  1712. }
  1713. //now get the number of objectives for this little guy
  1714. $item_view_objective_table = Database::get_course_table('lp_iv_objective');
  1715. $sql = "SELECT * FROM $item_view_objective_table WHERE lp_iv_id = '".$this->db_item_view_id."'";
  1716. $res = api_sql_query($sql,__FILE__,__LINE__);
  1717. if($res !== false){
  1718. $this->objectives_count = Database::num_rows($res);
  1719. }else{
  1720. $this->objectives_count = 0;
  1721. }
  1722. }
  1723. }
  1724. //end
  1725. if($this->debug>2){error_log('New LP - End of learnpathItem::set_lp_view()',0);}
  1726. }
  1727. /**
  1728. * Sets the path
  1729. * @param string Path
  1730. */
  1731. function set_path($string=''){
  1732. if($this->debug>0){error_log('New LP - In learnpathItem::set_path()',0);}
  1733. if(!empty($string)){$this->path = $string;}
  1734. }
  1735. /**
  1736. * Sets the prevent_reinit attribute. This is based on the LP value and is set at creation time for
  1737. * each learnpathItem. It is a (bad?) way of avoiding a reference to the LP when saving an item.
  1738. * @param integer 1 for "prevent", 0 for "don't prevent" saving freshened values (new "not attempted" status etc)
  1739. */
  1740. function set_prevent_reinit($prevent){
  1741. if($this->debug>0){error_log('New LP - In learnpathItem::set_prevent_reinit()',0);}
  1742. if($prevent){
  1743. $this->prevent_reinit = 1;
  1744. }else{
  1745. $this->prevent_reinit = 0;
  1746. }
  1747. }
  1748. /**
  1749. * Sets the score value. If the mastery_score is set and the score reaches
  1750. * it, then set the status to 'passed'.
  1751. * @param float Score
  1752. * @return boolean True on success, false otherwise
  1753. */
  1754. function set_score($score)
  1755. {
  1756. if($this->debug>0){error_log('New LP - In learnpathItem::set_score('.$score.')',0);}
  1757. if(($score <= $this->max_score) && ($score >= $this->min_score))
  1758. {
  1759. $this->current_score = $score;
  1760. $master = $this->get_mastery_score();
  1761. $current_status = $this->get_status(false);
  1762. //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'
  1763. if($master != -1 && $this->current_score >= $master && $current_status != $this->possible_status[2])
  1764. {
  1765. $this->set_status($this->possible_status[3]);
  1766. }
  1767. elseif($master != -1 && $this->current_score<$master)
  1768. {
  1769. $this->set_status($this->possible_status[4]);
  1770. }
  1771. return true;
  1772. }
  1773. return false;
  1774. }
  1775. /**
  1776. * Sets the maximum score for this item
  1777. * @param int Maximum score - must be a decimal or an empty string
  1778. * @return boolean True on success, false on error
  1779. */
  1780. function set_max_score($score)
  1781. {
  1782. if($this->debug>0){error_log('New LP - In learnpathItem::set_max_score('.$score.')',0);}
  1783. if(is_int($score) or $score == '')
  1784. {
  1785. $this->view_max_score = Database::escape_string($score);
  1786. 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);}
  1787. return true;
  1788. }
  1789. return false;
  1790. }
  1791. /**
  1792. * Sets the status for this item
  1793. * @param string Status - must be one of the values defined in $this->possible_status
  1794. * @return boolean True on success, false on error
  1795. */
  1796. function set_status($status)
  1797. {
  1798. if($this->debug>0){error_log('New LP - In learnpathItem::set_status('.$status.')',0);}
  1799. $found = false;
  1800. foreach($this->possible_status as $possible){
  1801. if(preg_match('/^'.$possible.'$/i',$status)){
  1802. $found = true;
  1803. }
  1804. }
  1805. //if(in_array($status,$this->possible_status))
  1806. if($found)
  1807. {
  1808. $this->status = Database::escape_string($status);
  1809. if($this->debug>1){error_log('New LP - In learnpathItem::set_status() - Updated object status of item '.$this->db_id.' to '.$this->status,0);}
  1810. return true;
  1811. }
  1812. //error_log('New LP - '.$status.' was not in the possible status',0);
  1813. $this->status = $this->possible_status[0];
  1814. return false;
  1815. }
  1816. /**
  1817. * Sets the item viewing time in a usable form, given that SCORM packages often give it as 00:00:00.0000
  1818. * @param string Time as given by SCORM
  1819. */
  1820. function set_time($scorm_time,$format='scorm')
  1821. {
  1822. if($this->debug>0){error_log('New LP - In learnpathItem::set_time('.$scorm_time.')',0);}
  1823. if($scorm_time == 0 and ($this->type!='sco') and $this->current_start_time!=0){
  1824. $my_time = time() - $this->current_start_time;
  1825. if($my_time > 0){
  1826. $this->update_time($my_time);
  1827. if($this->debug>0){error_log('New LP - In learnpathItem::set_time('.$scorm_time.') - found asset - set time to '.$my_time,0);}
  1828. }
  1829. }else{
  1830. if($format == 'scorm'){
  1831. $res = array();
  1832. if(preg_match('/^(\d{1,4}):(\d{2}):(\d{2})(\.\d{1,4})?/',$scorm_time,$res)){
  1833. $time = time();
  1834. $hour = $res[1];
  1835. $min = $res[2];
  1836. $sec = $res[3];
  1837. //getting total number of seconds spent
  1838. $total_sec = $hour*3600 + $min*60 + $sec;
  1839. $this->update_time($total_sec);
  1840. }
  1841. }elseif($format == 'int'){
  1842. $this->update_time($scorm_time);
  1843. }
  1844. }
  1845. }
  1846. /**
  1847. * Sets the item's title
  1848. * @param string Title
  1849. */
  1850. function set_title($string=''){
  1851. if($this->debug>0){error_log('New LP - In learnpathItem::set_title()',0);}
  1852. if(!empty($string)){$this->title = $string;}
  1853. }
  1854. /**
  1855. * Sets the item's type
  1856. * @param string Type
  1857. */
  1858. function set_type($string=''){
  1859. if($this->debug>0){error_log('New LP - In learnpathItem::set_type()',0);}
  1860. if(!empty($string)){$this->type = $string;}
  1861. }
  1862. /**
  1863. * Checks if the current status is part of the list of status given
  1864. * @param strings_array An array of status to check for. If the current status is one of the strings, return true
  1865. * @return boolean True if the status was one of the given strings, false otherwise
  1866. */
  1867. function status_is($list=array())
  1868. {
  1869. if($this->debug>1){error_log('New LP - In learnpathItem::status_is('.print_r($list,true).') on item '.$this->db_id,0);}
  1870. $mystatus = $this->get_status(true);
  1871. if(empty($mystatus)){
  1872. return false;
  1873. }
  1874. $found = false;
  1875. foreach($list as $status)
  1876. {
  1877. if(preg_match('/^'.$status.'$/i',$mystatus))
  1878. {
  1879. if($this->debug>2){error_log('New LP - learnpathItem::status_is() - Found status '.$status.' corresponding to current status',0);}
  1880. $found = true;
  1881. return $found;
  1882. }
  1883. }
  1884. if($this->debug>2){error_log('New LP - learnpathItem::status_is() - Status '.$mystatus.' did not match request',0);}
  1885. return $found;
  1886. }
  1887. /**
  1888. * Updates the time info according to the given session_time
  1889. * @param integer Time in seconds
  1890. * //TODO @TODO make this method better by allowing better/multiple time slices
  1891. */
  1892. function update_time($total_sec=0){
  1893. if($this->debug>0){error_log('New LP - In learnpathItem::update_time('.$total_sec.')',0);}
  1894. if($total_sec>=0){
  1895. //getting start time from finish time. The only problem in the calculation is it might be
  1896. //modified by the scripts processing time
  1897. $now = time();
  1898. $start = $now-$total_sec;
  1899. $this->current_start_time = $start;
  1900. $this->current_stop_time = $now;
  1901. /*if(empty($this->current_start_time)){
  1902. $this->current_start_time = $start;
  1903. $this->current_stop_time = $now;
  1904. }else{
  1905. //if($this->current_stop_time != $this->current_start_time){
  1906. //if the stop time has already been set before to something else
  1907. //than the start time, add the given time to what's already been
  1908. //recorder.
  1909. //This is the SCORM way of doing things, because the time comes from
  1910. //core.session_time, not core.total_time
  1911. // UPDATE: adding time to previous time is only done on SCORM's finish()
  1912. // call, not normally, so for now ignore this section
  1913. // $this->current_stop_time = $this->current_stop_time + $stop;
  1914. // error_log('New LP - Adding '.$stop.' seconds - now '.$this->current_stop_time,0);
  1915. //}else{
  1916. //if no previous stop time set, use the one just calculated now from
  1917. //start time
  1918. //$this->current_start_time = $start;
  1919. //$this->current_stop_time = $now;
  1920. //error_log('New LP - Setting '.$stop.' seconds - now '.$this->current_stop_time,0);
  1921. //}
  1922. }*/
  1923. }
  1924. }
  1925. /**
  1926. * Write objectives to DB. This method is separate from write_to_db() because otherwise
  1927. * objectives are lost as a side effect to AJAX and session concurrent access
  1928. * @return boolean True or false on error
  1929. */
  1930. function write_objectives_to_db()
  1931. {
  1932. if($this->debug>0){error_log('New LP - In learnpathItem::write_objectives_to_db()',0);}
  1933. if(is_array($this->objectives) && count($this->objectives)>0){
  1934. //save objectives
  1935. $tbl = Database::get_course_table('lp_item_view');
  1936. $sql = "SELECT id FROM $tbl " .
  1937. "WHERE lp_item_id = ".$this->db_id." " .
  1938. "AND lp_view_id = ".$this->view_id." " .
  1939. "AND view_count = ".$this->attempt_id;
  1940. $res = api_sql_query($sql,__FILE__,__LINE__);
  1941. if(Database::num_rows($res)>0){
  1942. $row = Database::fetch_array($res);
  1943. $lp_iv_id = $row[0];
  1944. if($this->debug>2){error_log('New LP - In learnpathItem::write_to_db() - Got item_view_id '.$lp_iv_id.', now checking objectives ',0);}
  1945. foreach($this->objectives as $index => $objective){
  1946. $iva_table = Database::get_course_table('lp_iv_objective');
  1947. $iva_sql = "SELECT id FROM $iva_table " .
  1948. "WHERE lp_iv_id = $lp_iv_id " .
  1949. //"AND order_id = $index";
  1950. //also check for the objective ID as it must be unique for this SCO view
  1951. "AND objective_id = '".$objective[0]."'";
  1952. $iva_res = api_sql_query($iva_sql,__FILE__,__LINE__);
  1953. //id(0), type(1), time(2), weighting(3),correct_responses(4),student_response(5),result(6),latency(7)
  1954. if(Database::num_rows($iva_res)>0){
  1955. //update (or don't)
  1956. $iva_row = Database::fetch_array($iva_res);
  1957. $iva_id = $iva_row[0];
  1958. $ivau_sql = "UPDATE $iva_table " .
  1959. "SET objective_id = '".$objective[0]."'," .
  1960. "status = '".$objective[1]."'," .
  1961. "score_raw = '".$objective[2]."'," .
  1962. "score_min = '".$objective[4]."'," .
  1963. "score_max = '".$objective[3]."' " .
  1964. "WHERE id = $iva_id";
  1965. $ivau_res = api_sql_query($ivau_sql,__FILE__,__LINE__);
  1966. //error_log($ivau_sql,0);
  1967. }else{
  1968. //insert new one
  1969. $ivai_sql = "INSERT INTO $iva_table " .
  1970. "(lp_iv_id, order_id, objective_id, status, score_raw, score_min, score_max )" .
  1971. "VALUES" .
  1972. "(".$lp_iv_id.", ".$index.",'".$objective[0]."','".$objective[1]."'," .
  1973. "'".$objective[2]."','".$objective[4]."','".$objective[3]."')";
  1974. $ivai_res = api_sql_query($ivai_sql,__FILE__,__LINE__);
  1975. //error_log($ivai_sql);
  1976. }
  1977. }
  1978. }
  1979. }
  1980. else
  1981. {
  1982. //error_log('no objective to save: '.print_r($this->objectives,1));
  1983. }
  1984. }
  1985. /**
  1986. * Writes the current data to the database
  1987. * @return boolean Query result
  1988. */
  1989. function write_to_db()
  1990. {
  1991. if($this->debug>0){error_log('New LP - In learnpathItem::write_to_db()',0);}
  1992. $mode = $this->get_lesson_mode();
  1993. $credit = $this->get_credit();
  1994. if(($this->type == 'sco') && ($credit == 'no-credit' OR $mode == 'review' OR $mode == 'browse'))
  1995. {
  1996. //this info shouldn't be saved as the credit or lesson mode info prevent it
  1997. if($this->debug>1){error_log('New LP - In learnpathItem::write_to_db() - credit('.$credit.') or lesson_mode('.$mode.') prevent recording!',0);}
  1998. }else{
  1999. //check the row exists
  2000. $item_view_table = Database::get_course_table('lp_item_view');
  2001. $check = "SELECT * FROM $item_view_table " .
  2002. "WHERE lp_item_id = ".$this->db_id. " " .
  2003. "AND lp_view_id = ".$this->view_id. " ".
  2004. "AND view_count = ".$this->get_attempt_id();
  2005. if($this->debug>2){error_log('New LP - In learnpathItem::write_to_db() - Querying item_view: '.$check,0);}
  2006. $check_res = api_sql_query($check);
  2007. //depending on what we want (really), we'll update or insert a new row
  2008. //now save into DB
  2009. $res = 0;
  2010. if(Database::num_rows($check_res)<1){
  2011. $sql = "INSERT INTO $item_view_table " .
  2012. "(total_time, " .
  2013. "start_time, " .
  2014. "score, " .
  2015. "status, " .
  2016. "max_score, ".
  2017. "lp_item_id, " .
  2018. "lp_view_id, " .
  2019. "view_count, " .
  2020. "suspend_data, " .
  2021. //"max_time_allowed," .
  2022. "lesson_location)" .
  2023. "VALUES" .
  2024. "(".$this->get_total_time()."," .
  2025. "".$this->current_start_time."," .
  2026. "".$this->get_score()."," .
  2027. "'".$this->get_status(false)."'," .
  2028. "'".$this->get_max()."'," .
  2029. "".$this->db_id."," .
  2030. "".$this->view_id."," .
  2031. "".$this->get_attempt_id()."," .
  2032. "'".Database::escape_string($this->current_data)."'," .
  2033. //"'".$this->get_max_time_allowed()."'," .
  2034. "'".$this->lesson_location."')";
  2035. if($this->debug>2){error_log('New LP - In learnpathItem::write_to_db() - Inserting into item_view: '.$sql,0);}
  2036. $res = api_sql_query($sql,__FILE__,__LINE__);
  2037. $this->db_item_view_id = Database::get_last_insert_id();
  2038. }else{
  2039. $sql = '';
  2040. if($this->type=='hotpotatoes')
  2041. { //make an exception for HotPotatoes, don't update the score
  2042. //because it has been saved outside of this tool
  2043. $sql = "UPDATE $item_view_table " .
  2044. "SET total_time = ".$this->get_total_time().", " .
  2045. " start_time = ".$this->get_current_start_time().", " .
  2046. " status = '".$this->get_status(false)."'," .
  2047. " max_score = '".$this->get_max()."'," .
  2048. " suspend_data = '".Database::escape_string($this->current_data)."'," .
  2049. " lesson_location = '".$this->lesson_location."' " .
  2050. "WHERE lp_item_id = ".$this->db_id." " .
  2051. "AND lp_view_id = ".$this->view_id." " .
  2052. "AND view_count = ".$this->attempt_id;
  2053. }
  2054. else
  2055. { //for all other content types...
  2056. $sql = "UPDATE $item_view_table " .
  2057. "SET total_time = ".$this->get_total_time().", " .
  2058. " start_time = ".$this->get_current_start_time().", " .
  2059. " score = ".$this->get_score().", " .
  2060. " status = '".$this->get_status(false)."'," .
  2061. " max_score = '".$this->get_max()."'," .
  2062. " suspend_data = '".Database::escape_string($this->current_data)."'," .
  2063. //" max_time_allowed = '".$this->get_max_time_allowed()."'," .
  2064. " lesson_location = '".$this->lesson_location."' " .
  2065. "WHERE lp_item_id = ".$this->db_id." " .
  2066. "AND lp_view_id = ".$this->view_id." " .
  2067. "AND view_count = ".$this->attempt_id;
  2068. }
  2069. if($this->debug>2){error_log('New LP - In learnpathItem::write_to_db() - Updating item_view: '.$sql,0);}
  2070. $res = api_sql_query($sql,__FILE__,__LINE__);
  2071. }
  2072. //if(!$res)
  2073. //{
  2074. // $this->error = 'Could not update item_view table...'.mysql_error();
  2075. //}
  2076. if(is_array($this->interactions) && count($this->interactions)>0){
  2077. //save interactions
  2078. $tbl = Database::get_course_table('lp_item_view');
  2079. $sql = "SELECT id FROM $tbl " .
  2080. "WHERE lp_item_id = ".$this->db_id." " .
  2081. "AND lp_view_id = ".$this->view_id." " .
  2082. "AND view_count = ".$this->attempt_id;
  2083. $res = api_sql_query($sql,__FILE__,__LINE__);
  2084. if(Database::num_rows($res)>0){
  2085. $row = Database::fetch_array($res);
  2086. $lp_iv_id = $row[0];
  2087. if($this->debug>2){error_log('New LP - In learnpathItem::write_to_db() - Got item_view_id '.$lp_iv_id.', now checking interactions ',0);}
  2088. foreach($this->interactions as $index => $interaction){
  2089. $correct_resp = '';
  2090. if(is_array($interaction[4]) && !empty($interaction[4][0]) ){
  2091. foreach($interaction[4] as $resp){
  2092. $correct_resp .= $resp.',';
  2093. }
  2094. $correct_resp = substr($correct_resp,0,strlen($correct_resp)-1);
  2095. }
  2096. $iva_table = Database::get_course_table('lp_iv_interaction');
  2097. $iva_sql = "SELECT id FROM $iva_table " .
  2098. "WHERE lp_iv_id = $lp_iv_id " .
  2099. // "AND order_id = $index";
  2100. //also check for the interaction ID as it must be unique for this SCO view
  2101. "AND (order_id = $index " .
  2102. "OR interaction_id = '".$interaction[0]."')";
  2103. $iva_res = api_sql_query($iva_sql,__FILE__,__LINE__);
  2104. //id(0), type(1), time(2), weighting(3),correct_responses(4),student_response(5),result(6),latency(7)
  2105. if(Database::num_rows($iva_res)>0){
  2106. //update (or don't)
  2107. $iva_row = Database::fetch_array($iva_res);
  2108. $iva_id = $iva_row[0];
  2109. $ivau_sql = "UPDATE $iva_table " .
  2110. "SET interaction_id = '".$interaction[0]."'," .
  2111. "interaction_type = '".$interaction[1]."'," .
  2112. "weighting = '".$interaction[3]."'," .
  2113. "completion_time = '".$interaction[2]."'," .
  2114. "correct_responses = '".$correct_resp."'," .
  2115. "student_response = '".$interaction[5]."'," .
  2116. "result = '".$interaction[6]."'," .
  2117. "latency = '".$interaction[7]."'" .
  2118. "WHERE id = $iva_id";
  2119. $ivau_res = api_sql_query($ivau_sql,__FILE__,__LINE__);
  2120. }else{
  2121. //insert new one
  2122. $ivai_sql = "INSERT INTO $iva_table " .
  2123. "(order_id, lp_iv_id, interaction_id, interaction_type, " .
  2124. "weighting, completion_time, correct_responses, " .
  2125. "student_response, result, latency)" .
  2126. "VALUES" .
  2127. "(".$index.",".$lp_iv_id.",'".$interaction[0]."','".$interaction[1]."'," .
  2128. "'".$interaction[3]."','".$interaction[2]."','".$correct_resp."'," .
  2129. "'".$interaction[5]."','".$interaction[6]."','".$interaction[7]."'" .
  2130. ")";
  2131. $ivai_res = api_sql_query($ivai_sql,__FILE__,__LINE__);
  2132. }
  2133. }
  2134. }
  2135. }
  2136. }
  2137. if($this->debug>2){error_log('New LP - End of learnpathItem::write_to_db()',0);}
  2138. return true;
  2139. }
  2140. }
  2141. ?>