learnpathItem.class.php 85 KB

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