learnpathItem.class.php 84 KB

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