learnpathItem.class.php 99 KB

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