learnpathItem.class.php 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471
  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(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(TABLE_LP_ITEM_VIEW);
  207. $lp_item = Database::get_course_table(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(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(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(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(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(TABLE_LP_MAIN);
  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. if($this->debug>0){error_log('New LP - In learnpathItem::get_status() on item '.$this->db_id,0);}
  996. if($check_db) {
  997. if($this->debug>2){error_log('New LP - In learnpathItem::get_status(): checking db',0);}
  998. $table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  999. $sql = "SELECT * FROM $table WHERE id = '".$this->db_item_view_id."' AND view_count = '".$this->get_attempt_id()."'";
  1000. if($this->debug>2){error_log('New LP - In learnpathItem::get_status() - Checking DB: '.$sql,0);}
  1001. $res = api_sql_query($sql);
  1002. if (Database::num_rows($res)==1) {
  1003. $row = Database::fetch_array($res);
  1004. if($update_local==true){
  1005. $this->set_status($row['status']);
  1006. }
  1007. if($this->debug>2){error_log('New LP - In learnpathItem::get_status() - Returning db value '.$row['status'],0);}
  1008. return $row['status'];
  1009. }
  1010. } else {
  1011. if($this->debug>2){error_log('New LP - In learnpathItem::get_status() - in get_status: using attrib',0);}
  1012. if(!empty($this->status)) {
  1013. if($this->debug>2){error_log('New LP - In learnpathItem::get_status() - Returning attrib: '.$this->status,0);}
  1014. return $this->status;
  1015. }
  1016. }
  1017. if($this->debug>2){error_log('New LP - In learnpathItem::get_status() - Returning default '.$this->possible_status[0],0);}
  1018. return $this->possible_status[0];
  1019. }
  1020. /**
  1021. * Gets the suspend data
  1022. */
  1023. function get_suspend_data()
  1024. {
  1025. if($this->debug>0){error_log('New LP - In learnpathItem::get_suspend_data()',0);}
  1026. //TODO : improve cleaning of breaklines ... it works but is it really a beautiful way to do it ?
  1027. if(!empty($this->current_data)){return str_replace(array("\r","\n"),array('\r','\n'),$this->current_data);}else{return '';}
  1028. }
  1029. /**
  1030. * Gets the total time spent on this item view so far
  1031. * @param string Origin of the request. If coming from PHP, send formatted as xxhxx'xx", otherwise use scorm format 00:00:00
  1032. * @param integer Given time is a default time to return formatted
  1033. */
  1034. function get_scorm_time($origin='php',$given_time=null){
  1035. //if($this->debug>0){error_log('New LP - In learnpathItem::get_scorm_time()',0);}
  1036. $h = get_lang('h');
  1037. if(!isset($given_time)){
  1038. if(!empty($this->current_start_time) && is_object($this)){
  1039. if(!empty($this->current_stop_time)){
  1040. $time = $this->current_stop_time - $this->current_start_time;
  1041. }else{
  1042. $time = time() - $this->current_start_time;
  1043. }
  1044. }else{
  1045. if($origin == 'js'){
  1046. return '00:00:00';
  1047. }else{
  1048. return '00'.$h.'00\'00"';
  1049. }
  1050. }
  1051. }else{
  1052. $time = $given_time;
  1053. }
  1054. $hours = $time/3600;
  1055. $mins = ($time%3600)/60;
  1056. $secs = ($time%60);
  1057. if($origin == 'js'){
  1058. $scorm_time = trim(sprintf("%4d:%02d:%02d",$hours,$mins,$secs));
  1059. }else{
  1060. $scorm_time = trim(sprintf("%4d$h%02d'%02d\"",$hours,$mins,$secs));
  1061. }
  1062. return $scorm_time;
  1063. }
  1064. function get_terms()
  1065. {
  1066. $lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1067. $sql = "SELECT * FROM $lp_item WHERE id='".Database::escape_string($this->db_id)."'";
  1068. $res = api_sql_query($sql,__FILE__,__LINE__);
  1069. $row = Database::fetch_array($res);
  1070. return $row['terms'];
  1071. }
  1072. /**
  1073. * Returns the item's title
  1074. * @return string Title
  1075. */
  1076. function get_title(){
  1077. if($this->debug>0){error_log('New LP - In learnpathItem::get_title()',0);}
  1078. if(empty($this->title)){return '';}
  1079. return $this->title;
  1080. }
  1081. /**
  1082. * Returns the total time used to see that item
  1083. * @return integer Total time
  1084. */
  1085. function get_total_time(){
  1086. if($this->debug>0){error_log('New LP - In learnpathItem::get_total_time()',0);}
  1087. if($this->current_start_time == 0){ //shouldn't be necessary thanks to the open() method
  1088. $this->current_start_time = time();
  1089. }
  1090. //$this->current_stop_time=time();
  1091. $time = $this->current_stop_time - $this->current_start_time;
  1092. if($time < 0){
  1093. return 0;
  1094. }else{
  1095. 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);}
  1096. return $time;
  1097. }
  1098. }
  1099. /**
  1100. * Gets the item type
  1101. * @return string The item type (can be doc, dir, sco, asset)
  1102. */
  1103. function get_type()
  1104. {
  1105. $res = 'asset';
  1106. if($this->debug>0){error_log('New LP - In learnpathItem::get_type() on item '.$this->db_id,0);}
  1107. if(!empty($this->type))
  1108. {
  1109. //error_log('In item::get_type() - returning '.$this->type,0);
  1110. $res = $this->type;
  1111. }
  1112. if($this->debug>2){error_log('New LP - In learnpathItem::get_type() - Returning '.$res.' for item '.$this->db_id,0);}
  1113. return $res;
  1114. }
  1115. /**
  1116. * Gets the view count for this item
  1117. */
  1118. function get_view_count(){
  1119. if($this->debug>0){error_log('New LP - In learnpathItem::get_view_count()',0);}
  1120. if(!empty($this->attempt_id)){
  1121. return $this->attempt_id;
  1122. }else{
  1123. return 0;
  1124. }
  1125. }
  1126. /**
  1127. * Tells if an item is done ('completed','passed','succeeded') or not
  1128. * @return bool True if the item is done ('completed','passed','succeeded'), false otherwise
  1129. */
  1130. function is_done(){
  1131. if($this->status_is(array('completed','passed','succeeded'))){
  1132. if($this->debug>2){error_log('New LP - In learnpath::is_done() - Item '.$this->get_id().' is complete',0);}
  1133. return true;
  1134. }else{
  1135. if($this->debug>2){error_log('New LP - In learnpath::is_done() - Item '.$this->get_id().' is not complete',0);}
  1136. return false;
  1137. }
  1138. }
  1139. /**
  1140. * Tells if a restart is allowed (take it from $this->prevent_reinit and $this->status)
  1141. * @return integer -1 if retaking the sco another time for credit is not allowed,
  1142. * 0 if it is not allowed but the item has to be finished
  1143. * 1 if it is allowed. Defaults to 1
  1144. */
  1145. function is_restart_allowed()
  1146. {
  1147. if($this->debug>2){error_log('New LP - In learnpathItem::is_restart_allowed()',0);}
  1148. $restart = 1;
  1149. $mystatus = $this->get_status(true);
  1150. if($this->get_prevent_reinit() > 0){ //if prevent_reinit == 1 (or more)
  1151. //if status is not attempted or incomplete, authorize retaking (of the same) anyway. Otherwise:
  1152. if($mystatus != $this->possible_status[0] AND $mystatus != $this->possible_status[1]){
  1153. $restart = -1;
  1154. }else{
  1155. $restart = 0;
  1156. }
  1157. }else{
  1158. if($mystatus == $this->possible_status[0] OR $mystatus == $this->possible_status[1]){
  1159. $restart = -1;
  1160. }
  1161. }
  1162. if($this->debug>2){error_log('New LP - End of learnpathItem::is_restart_allowed() - Returning '.$restart,0);}
  1163. return $restart;
  1164. }
  1165. /**
  1166. * Opens/launches the item. Initialises runtime values.
  1167. * @return boolean True on success, false on failure.
  1168. */
  1169. function open($allow_new_attempt=false){
  1170. if($this->debug>0){error_log('New LP - In learnpathItem::open()',0);}
  1171. if($this->prevent_reinit == 0)
  1172. {
  1173. $this->current_score = 0;
  1174. $this->current_start_time = time();
  1175. //In this case, as we are opening the item, what is important to us
  1176. //is the database status, in order to know if this item has already
  1177. //been used in the past (rather than just loaded and modified by
  1178. //some javascript but not written in the database).
  1179. //If the database status is different from 'not attempted', we can
  1180. //consider this item has already been used, and as such we can
  1181. //open a new attempt. Otherwise, we'll just reuse the current
  1182. //attempt, which is generally created the first time the item is
  1183. //loaded (for example as part of the table of contents)
  1184. $stat = $this->get_status(true);
  1185. if($allow_new_attempt && isset($stat) && ($stat != $this->possible_status[0]))
  1186. {
  1187. $this->attempt_id = $this->attempt_id + 1; //open a new attempt
  1188. }
  1189. $this->status = $this->possible_status[1];
  1190. }
  1191. else
  1192. {
  1193. if($this->current_start_time == 0)
  1194. { //small exception for start time, to avoid amazing values
  1195. $this->current_start_time = time();
  1196. }
  1197. //error_log('New LP - reinit blocked by setting',0);
  1198. }
  1199. }
  1200. /**
  1201. * Outputs the item contents
  1202. * @return string HTML file (displayable in an <iframe>) or empty string if no path defined
  1203. */
  1204. function output()
  1205. {
  1206. if($this->debug>0){error_log('New LP - In learnpathItem::output()',0);}
  1207. if(!empty($this->path) and is_file($this->path))
  1208. {
  1209. $output = '';
  1210. $output .= file_get_contents($this->path);
  1211. return $output;
  1212. }
  1213. return '';
  1214. }
  1215. /**
  1216. * Parses the prerequisites string with the AICC logic language
  1217. * @param string The prerequisites string as it figures in imsmanifest.xml
  1218. * @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
  1219. * @param Array List of references (the "ref" column in the lp_item table) that are strings used in the expression of prerequisites.
  1220. * @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)
  1221. * @return boolean True if the list of prerequisites given is entirely satisfied, false otherwise
  1222. */
  1223. function parse_prereq($prereqs_string, $items, $refs_list,$user_id){
  1224. if($this->debug>0){error_log('New LP - In learnpathItem::parse_prereq() for learnpath '.$this->lp_id.' with string '.$prereqs_string,0);}
  1225. //deal with &, |, ~, =, <>, {}, ,, X*, () in reverse order
  1226. $this->prereq_alert = '';
  1227. // First parse all parenthesis by using a sequential loop (looking for less-inclusives first)
  1228. if($prereqs_string == '_true_'){return true;}
  1229. if($prereqs_string == '_false_'){
  1230. if(empty($this->prereq_alert)){
  1231. $this->prereq_alert = get_lang('_prereq_not_complete');
  1232. }
  1233. return false;
  1234. }
  1235. while(strpos($prereqs_string,'(')!==false){
  1236. //remove any () set and replace with its value
  1237. $matches = array();
  1238. $res = preg_match_all('/(\(([^\(\)]*)\))/',$prereqs_string,$matches);
  1239. if($res){
  1240. foreach($matches[2] as $id=>$match){
  1241. $str_res = $this->parse_prereq($match,$items,$refs_list,$user_id);
  1242. if($str_res){
  1243. $prereqs_string = str_replace($matches[1][$id],'_true_',$prereqs_string);
  1244. }else{
  1245. $prereqs_string = str_replace($matches[1][$id],'_false_',$prereqs_string);
  1246. }
  1247. }
  1248. }
  1249. }
  1250. //parenthesis removed, now look for ORs as it is the lesser-priority binary operator (= always uses one text operand)
  1251. if(strpos($prereqs_string,"|")===false){
  1252. if($this->debug>1){error_log('New LP - Didnt find any OR, looking for AND',0);}
  1253. if(strpos($prereqs_string,"&")!==false){
  1254. $list = split("&",$prereqs_string);
  1255. if(count($list)>1){
  1256. $andstatus = true;
  1257. foreach($list as $condition){
  1258. $andstatus = $andstatus && $this->parse_prereq($condition,$items,$refs_list,$user_id);
  1259. if($andstatus==false){
  1260. if($this->debug>1){error_log('New LP - One condition in AND was false, short-circuit',0);}
  1261. break;
  1262. }
  1263. }
  1264. if(empty($this->prereq_alert) && !$andstatus){
  1265. $this->prereq_alert = get_lang('_prereq_not_complete');
  1266. }
  1267. return $andstatus;
  1268. }else{
  1269. if(isset($items[$refs_list[$list[0]]])){
  1270. $status = $items[$refs_list[$list[0]]]->get_status(true);
  1271. $returnstatus = (($status == $this->possible_status[2]) OR ($status == $this->possible_status[3]));
  1272. if(empty($this->prereq_alert) && !$returnstatus){
  1273. $this->prereq_alert = get_lang('_prereq_not_complete');
  1274. }
  1275. return $returnstatus;
  1276. }
  1277. $this->prereq_alert = get_lang('_prereq_not_complete');
  1278. return false;
  1279. }
  1280. }else{
  1281. //no ORs found, now look for ANDs
  1282. if($this->debug>1){error_log('New LP - Didnt find any AND, looking for =',0);}
  1283. if(strpos($prereqs_string,"=")!==false){
  1284. if($this->debug>1){error_log('New LP - Found =, looking into it',0);}
  1285. //we assume '=' signs only appear when there's nothing else around
  1286. $params = split('=',$prereqs_string);
  1287. if(count($params) == 2){
  1288. //right number of operands
  1289. if(isset($items[$refs_list[$params[0]]])){
  1290. $status = $items[$refs_list[$params[0]]]->get_status(true);
  1291. $returnstatus = ($status == $params[1]);
  1292. if(empty($this->prereq_alert) && !$returnstatus){
  1293. $this->prereq_alert = get_lang('_prereq_not_complete');
  1294. }
  1295. return $returnstatus;
  1296. }
  1297. $this->prereq_alert = get_lang('_prereq_not_complete');
  1298. return false;
  1299. }
  1300. }else{
  1301. //No ANDs found, look for <>
  1302. if($this->debug>1){error_log('New LP - Didnt find any =, looking for <>',0);}
  1303. if(strpos($prereqs_string,"<>")!==false){
  1304. if($this->debug>1){error_log('New LP - Found <>, looking into it',0);}
  1305. //we assume '<>' signs only appear when there's nothing else around
  1306. $params = split('<>',$prereqs_string);
  1307. if(count($params) == 2){
  1308. //right number of operands
  1309. if(isset($items[$refs_list[$params[0]]])){
  1310. $status = $items[$refs_list[$params[0]]]->get_status(true);
  1311. $returnstatus = ($status != $params[1]);
  1312. if(empty($this->prereq_alert) && !$returnstatus){
  1313. $this->prereq_alert = get_lang('_prereq_not_complete');
  1314. }
  1315. return $returnstatus;
  1316. }
  1317. $this->prereq_alert = get_lang('_prereq_not_complete');
  1318. return false;
  1319. }
  1320. }else{
  1321. //No <> found, look for ~ (unary)
  1322. if($this->debug>1){error_log('New LP - Didnt find any =, looking for ~',0);}
  1323. //only remains: ~ and X*{}
  1324. if(strpos($prereqs_string,"~")!==false){
  1325. //found NOT
  1326. if($this->debug>1){error_log('New LP - Found ~, looking into it',0);}
  1327. $list = array();
  1328. $myres = preg_match('/~([^(\d+\*)\{]*)/',$prereqs_string,$list);
  1329. if($myres){
  1330. $returnstatus = !$this->parse_prereq($list[1],$items,$refs_list,$user_id);
  1331. if(empty($this->prereq_alert) && !$returnstatus){
  1332. $this->prereq_alert = get_lang('_prereq_not_complete');
  1333. }
  1334. return $returnstatus;
  1335. }else{
  1336. //strange...
  1337. if($this->debug>1){error_log('New LP - Found ~ but strange string: '.$prereqs_string,0);}
  1338. }
  1339. }else{
  1340. //Finally, look for sets/groups
  1341. if($this->debug>1){error_log('New LP - Didnt find any ~, looking for groups',0);}
  1342. //only groups here
  1343. $groups = array();
  1344. $groups_there = preg_match_all('/((\d+\*)?\{([^\}]+)\}+)/',$prereqs_string,$groups);
  1345. if($groups_there){
  1346. foreach($groups[1] as $gr) //only take the results that correspond to the big brackets-enclosed condition
  1347. {
  1348. if($this->debug>1){error_log('New LP - Dealing with group '.$gr,0);}
  1349. $multi = array();
  1350. $mycond = false;
  1351. if(preg_match('/(\d+)\*\{([^\}]+)\}/',$gr,$multi)){
  1352. if($this->debug>1){error_log('New LP - Found multiplier '.$multi[0],0);}
  1353. $count = $multi[1];
  1354. $list = split(',',$multi[2]);
  1355. $mytrue = 0;
  1356. foreach($list as $cond){
  1357. if(isset($items[$refs_list[$cond]])){
  1358. $status = $items[$refs_list[$cond]]->get_status(true);
  1359. if (($status == $this->possible_status[2]) OR ($status == $this->possible_status[3])){
  1360. $mytrue ++;
  1361. if($this->debug>1){error_log('New LP - Found true item, counting.. ('.($mytrue).')',0);}
  1362. }
  1363. }else{
  1364. if($this->debug>1){error_log('New LP - item '.$cond.' does not exist in items list',0);}
  1365. }
  1366. }
  1367. if($mytrue >= $count){
  1368. if($this->debug>1){error_log('New LP - Got enough true results, return true',0);}
  1369. $mycond = true;
  1370. }else{
  1371. if($this->debug>1){error_log('New LP - Not enough true results',0);}
  1372. }
  1373. }
  1374. else{
  1375. if($this->debug>1){error_log('New LP - No multiplier',0);}
  1376. $list = split(',',$gr);
  1377. $mycond = true;
  1378. foreach($list as $cond){
  1379. if(isset($items[$refs_list[$cond]])){
  1380. $status = $items[$refs_list[$cond]]->get_status(true);
  1381. if (($status == $this->possible_status[2]) OR ($status == $this->possible_status[3])){
  1382. $mycond = true;
  1383. if($this->debug>1){error_log('New LP - Found true item',0);}
  1384. }else{
  1385. if($this->debug>1){error_log('New LP - Found false item, the set is not true, return false',0);}
  1386. $mycond = false;
  1387. break;
  1388. }
  1389. }else{
  1390. if($this->debug>1){error_log('New LP - item '.$cond.' does not exist in items list',0);}
  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. }
  1396. }
  1397. if(!$mycond && empty($this->prereq_alert)){
  1398. $this->prereq_alert = get_lang('_prereq_not_complete');
  1399. }
  1400. return $mycond;
  1401. }
  1402. }else{
  1403. //Nothing found there either. Now return the value of the corresponding resource completion status
  1404. if($this->debug>1){error_log('New LP - Didnt find any group, returning value for '.$prereqs_string,0);}
  1405. if(isset($items[$refs_list[$prereqs_string]])){
  1406. if($items[$refs_list[$prereqs_string]]->type == 'quiz')
  1407. {
  1408. $sql = 'SELECT exe_result, exe_weighting
  1409. FROM '.Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES).'
  1410. WHERE exe_exo_id = '.$items[$refs_list[$prereqs_string]]->path.'
  1411. AND exe_user_id = '.$user_id.'
  1412. AND status <> "incomplete"
  1413. ORDER BY exe_date DESC
  1414. LIMIT 0, 1';
  1415. $rs_quiz = api_sql_query($sql, __FILE__, __LINE__);
  1416. if($quiz = Database :: fetch_array($rs_quiz))
  1417. {
  1418. if($quiz['exe_result'] >= $items[$refs_list[$prereqs_string]]->get_mastery_score())
  1419. {
  1420. $returnstatus = true;
  1421. }
  1422. else
  1423. {
  1424. $this->prereq_alert = get_lang('_prereq_not_complete');
  1425. $returnstatus = false;
  1426. }
  1427. }
  1428. else
  1429. {
  1430. $this->prereq_alert = get_lang('_prereq_not_complete');
  1431. $returnstatus = false;
  1432. }
  1433. return $returnstatus;
  1434. }
  1435. else
  1436. {
  1437. $status = $items[$refs_list[$prereqs_string]]->get_status(true);
  1438. $returnstatus = (($status == $this->possible_status[2]) OR ($status == $this->possible_status[3]));
  1439. if(!$returnstatus && empty($this->prereq_alert)){
  1440. $this->prereq_alert = get_lang('_prereq_not_complete');
  1441. }
  1442. if(!$returnstatus){
  1443. if($this->debug>1){error_log('New LP - Prerequisite '.$prereqs_string.' not complete',0);}
  1444. }else{
  1445. if($this->debug>1){error_log('New LP - Prerequisite '.$prereqs_string.' complete',0);}
  1446. }
  1447. return $returnstatus;
  1448. }
  1449. }else{
  1450. if($this->debug>1){error_log('New LP - Could not find '.$prereqs_string.' in '.print_r($refs_list,true),0);}
  1451. }
  1452. }
  1453. }
  1454. }
  1455. }
  1456. }
  1457. }else{
  1458. $list = split("\|",$prereqs_string);
  1459. if(count($list)>1){
  1460. if($this->debug>1){error_log('New LP - Found OR, looking into it',0);}
  1461. $orstatus = false;
  1462. foreach($list as $condition){
  1463. if($this->debug>1){error_log('New LP - Found OR, adding it ('.$condition.')',0);}
  1464. $orstatus = $orstatus || $this->parse_prereq($condition,$items,$refs_list,$user_id);
  1465. if($orstatus == true){
  1466. //shortcircuit OR
  1467. if($this->debug>1){error_log('New LP - One condition in OR was true, short-circuit',0);}
  1468. break;
  1469. }
  1470. }
  1471. if(!$orstatus && empty($this->prereq_alert)){
  1472. $this->prereq_alert = get_lang('_prereq_not_complete');
  1473. }
  1474. return $orstatus;
  1475. }else{
  1476. if($this->debug>1){error_log('New LP - OR was found but only one elem present !?',0);}
  1477. if(isset($items[$refs_list[$list[0]]])){
  1478. $status = $items[$refs_list[$list[0]]]->get_status(true);
  1479. $returnstatus = (($status == 'completed') OR ($status == 'passed'));
  1480. if(!$returnstatus && empty($this->prereq_alert)){
  1481. $this->prereq_alert = get_lang('_prereq_not_complete');
  1482. }
  1483. return $returnstatus;
  1484. }
  1485. }
  1486. }
  1487. if(empty($this->prereq_alert)){
  1488. $this->prereq_alert = get_lang('_prereq_not_complete');
  1489. }
  1490. if($this->debug>1){error_log('New LP - End of parse_prereq. Error code is now '.$this->prereq_alert,0);}
  1491. return false;
  1492. }
  1493. /**
  1494. * Parses the HTML attributes given as string.
  1495. *
  1496. * @param string HTML attribute string
  1497. * @param array List of attributes that we want to get back
  1498. * @return array An associative array of attributes
  1499. * @author Based on a function from the HTML_Common2 PEAR module
  1500. */
  1501. function parse_HTML_attributes($attrString,$wanted=array())
  1502. {
  1503. $attributes = array();
  1504. $regs = array();
  1505. $reduced = false;
  1506. if(count($wanted)>0)
  1507. {
  1508. $reduced = true;
  1509. }
  1510. try {
  1511. //Find all occurences of something that looks like a URL
  1512. // The structure of this regexp is:
  1513. // (find protocol) then
  1514. // (optionally find some kind of space 1 or more times) then
  1515. // find (either an equal sign or a bracket) followed by an optional space
  1516. // followed by some text without quotes (between quotes itself or not)
  1517. // then possible closing brackets if we were in the opening bracket case
  1518. // OR something like @import()
  1519. $res = preg_match_all(
  1520. '/(((([A-Za-z_:])([A-Za-z0-9_:\.-]*))' .
  1521. // '/(((([A-Za-z_:])([A-Za-z0-9_:\.-]|[^\x00-\x7F])*)' . -> seems to be taking too much
  1522. // '/(((([A-Za-z_:])([^\x00-\x7F])*)' . -> takes only last letter of parameter name
  1523. '([ \n\t\r]+)?(' .
  1524. // '(=([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+))' . -> doesn't restrict close enough to the url itself
  1525. '(=([ \n\t\r]+)?("[^"\)]+"|\'[^\'\)]+\'|[^ \n\t\r\)]+))' .
  1526. '|' .
  1527. // '(\(([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+)\))' . -> doesn't restrict close enough to the url itself
  1528. '(\(([ \n\t\r]+)?("[^"\)]+"|\'[^\'\)]+\'|[^ \n\t\r\)]+)\))' .
  1529. '))' .
  1530. '|' .
  1531. // '(@import([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+)))?/', -> takes a lot (like 100's of thousands of empty possibilities)
  1532. '(@import([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+)))/',
  1533. $attrString,
  1534. $regs
  1535. );
  1536. } catch (Exception $e) {
  1537. error_log('Caught exception: '. $e->getMessage(),0) ;
  1538. }
  1539. if ($res) {
  1540. for ($i = 0; $i < count($regs[1]); $i++) {
  1541. $name = trim($regs[3][$i]);
  1542. $check = trim($regs[0][$i]);
  1543. $value = trim($regs[10][$i]);
  1544. if(empty($value) and !empty($regs[13][$i])){
  1545. $value = $regs[13][$i];
  1546. }
  1547. if(empty($name) && !empty($regs[16][$i]))
  1548. {
  1549. $name = '@import';
  1550. $value = trim($regs[16][$i]);
  1551. }
  1552. if(!empty($name))
  1553. {
  1554. if(!$reduced OR in_array(strtolower($name),$wanted))
  1555. {
  1556. if ($name == $check) {
  1557. $attributes[strtolower($name)][] = strtolower($name);
  1558. } else {
  1559. if (!empty($value) && ($value[0] == '\'' || $value[0] == '"')) {
  1560. $value = substr($value, 1, -1);
  1561. }
  1562. if ($value=='API.LMSGetValue(name') {
  1563. $value='API.LMSGetValue(name)';
  1564. }
  1565. $attributes[strtolower($name)][] = $value;
  1566. }
  1567. }
  1568. }
  1569. }
  1570. }else{
  1571. error_log('preg_match did not find anything',0);
  1572. }
  1573. return $attributes;
  1574. }
  1575. /**
  1576. * Reinits all local values as the learnpath is restarted
  1577. * @return boolean True on success, false otherwise
  1578. */
  1579. function restart()
  1580. {
  1581. if($this->debug>0){error_log('New LP - In learnpathItem::restart()',0);}
  1582. $this->save();
  1583. $allowed = $this->is_restart_allowed();
  1584. if($allowed === -1){
  1585. //nothing allowed, do nothing
  1586. }elseif($allowed === 1){
  1587. //restart as new attempt is allowed, record a new attempt
  1588. $this->attempt_id = $this->attempt_id + 1; //simply reuse the previous attempt_id
  1589. $this->current_score = 0;
  1590. $this->current_start_time = 0;
  1591. $this->current_stop_time = 0;
  1592. $this->current_data = '';
  1593. $this->status = $this->possible_status[0];
  1594. $this->interactions_count = 0;
  1595. $this->interactions = array();
  1596. $this->objectives_count = 0;
  1597. $this->objectives = array();
  1598. $this->lesson_location = '';
  1599. if ($this->type != TOOL_QUIZ) {
  1600. $this->write_to_db();
  1601. }
  1602. }else{
  1603. //restart current element is allowed (because it's not finished yet),
  1604. // reinit current
  1605. $this->current_score = 0;
  1606. $this->current_start_time = 0;
  1607. $this->current_stop_time = 0;
  1608. $this->current_data = '';
  1609. $this->status = $this->possible_status[0];
  1610. $this->interactions_count = $this->get_interactions_count(true);
  1611. }
  1612. return true;
  1613. }
  1614. /**
  1615. * Saves data in the database
  1616. * @param boolean Save from URL params (1) or from object attributes (0)
  1617. * @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
  1618. * @return boolean True on success, false on failure
  1619. */
  1620. function save($from_outside=true,$prereqs_complete=false)
  1621. {
  1622. if($this->debug>0){error_log('New LP - In learnpathItem::save()',0);}
  1623. //$item_view_table = Database::get_course_table(COURSEID,LEARNPATH_ITEM_VIEW_TABLE);
  1624. $item_id = $this->get_id();
  1625. //first check if parameters passed via GET can be saved here
  1626. //in case it's a SCORM, we should get:
  1627. if($this->type == 'sco' || $this->type== 'au'){
  1628. $s = $this->get_status(true);
  1629. if($this->prevent_reinit == 1 AND
  1630. $s != $this->possible_status[0] AND $s != $this->possible_status[1]){
  1631. if($this->debug>1){error_log('New LP - In learnpathItem::save() - save reinit blocked by setting',0);}
  1632. //do nothing because the status has already been set. Don't allow it to change.
  1633. //TODO check there isn't a special circumstance where this should be saved
  1634. }else{
  1635. if($this->debug>1){error_log('New LP - In learnpathItem::save() - SCORM save request received',0);}
  1636. //get all new settings from the URL
  1637. if($from_outside==true){
  1638. if($this->debug>1){error_log('New LP - In learnpathItem::save() - Getting item data from outside',0);}
  1639. foreach($_GET as $param => $value)
  1640. {
  1641. $value = Database::escape_string($value);
  1642. switch($param){
  1643. case 'score':
  1644. $this->set_score($value);
  1645. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting score to '.$value,0);}
  1646. break;
  1647. case 'max':
  1648. $this->set_max_score($value);
  1649. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting view_max_score to '.$value,0);}
  1650. break;
  1651. case 'min':
  1652. $this->min_score = $value;
  1653. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting min_score to '.$value,0);}
  1654. break;
  1655. case 'lesson_status':
  1656. if(!empty($value)){
  1657. $this->set_status($value);
  1658. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting status to '.$value,0);}
  1659. }
  1660. break;
  1661. case 'time':
  1662. $this->set_time($value);
  1663. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting time to '.$value,0);}
  1664. break;
  1665. case 'suspend_data':
  1666. $this->current_data = $value;
  1667. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting suspend_data to '.$value,0);}
  1668. break;
  1669. case 'lesson_location':
  1670. $this->set_lesson_location($value);
  1671. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting lesson_location to '.$value,0);}
  1672. break;
  1673. case 'core_exit':
  1674. $this->set_core_exit($value);
  1675. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting core_exit to '.$value,0);}
  1676. break;
  1677. case 'interactions':
  1678. //$interactions = unserialize($value);
  1679. //foreach($interactions as $interaction){
  1680. // ;
  1681. //}
  1682. break;
  1683. case 'objectives':
  1684. break;
  1685. //case 'maxtimeallowed':
  1686. // $this->set_max_time_allowed($value);
  1687. // break;
  1688. /*
  1689. case 'objectives._count':
  1690. $this->attempt_id = $value;
  1691. break;
  1692. */
  1693. default:
  1694. //ignore
  1695. break;
  1696. }
  1697. }
  1698. }else{
  1699. if($this->debug>1){error_log('New LP - In learnpathItem::save() - Using inside item status',0);}
  1700. //do nothing, just let the local attributes be used
  1701. }
  1702. }
  1703. }else{ //if not SCO, such messages should not be expected
  1704. $type = strtolower($this->type);
  1705. switch($type){
  1706. case 'asset':
  1707. if($prereqs_complete)
  1708. {
  1709. $this->set_status($this->possible_status[2]);
  1710. }
  1711. break;
  1712. case TOOL_HOTPOTATOES:
  1713. case TOOL_QUIZ: return false;break;
  1714. default:
  1715. //for now, everything that is not sco and not asset is set to
  1716. //completed when saved
  1717. if($prereqs_complete)
  1718. {
  1719. $this->set_status($this->possible_status[2]);
  1720. }
  1721. break;
  1722. }
  1723. }
  1724. //$time = $this->time
  1725. if($this->debug>1){error_log('New LP - End of learnpathItem::save() - Calling write_to_db()',0);}
  1726. return $this->write_to_db();
  1727. }
  1728. /**
  1729. * Sets the number of attempt_id to a given value
  1730. * @param integer The given value to set attempt_id to
  1731. * @return boolean TRUE on success, FALSE otherwise
  1732. */
  1733. function set_attempt_id($num)
  1734. {
  1735. if($this->debug>0){error_log('New LP - In learnpathItem::set_attempt_id()',0);}
  1736. if($num == strval(intval($num)) && $num>=0){
  1737. $this->attempt_id = $num;
  1738. return true;
  1739. }
  1740. return false;
  1741. }
  1742. /**
  1743. * Sets the core_exit value to the one given
  1744. */
  1745. function set_core_exit($value)
  1746. {
  1747. switch($value){
  1748. case '':
  1749. $this->core_exit = '';
  1750. break;
  1751. case 'suspend':
  1752. $this->core_exit = 'suspend';
  1753. break;
  1754. default:
  1755. $this->core_exit = 'none';
  1756. break;
  1757. }
  1758. return true;
  1759. }
  1760. /**
  1761. * Sets the item's description
  1762. * @param string Description
  1763. */
  1764. function set_description($string=''){
  1765. if($this->debug>0){error_log('New LP - In learnpathItem::set_description()',0);}
  1766. if(!empty($string)){$this->description = $string;}
  1767. }
  1768. /**
  1769. * Sets the lesson_location value
  1770. * @param string lesson_location as provided by the SCO
  1771. * @return boolean True on success, false otherwise
  1772. */
  1773. function set_lesson_location($location)
  1774. {
  1775. if($this->debug>0){error_log('New LP - In learnpathItem::set_lesson_location()',0);}
  1776. if(isset($location)){
  1777. $this->lesson_location = Database::escape_string($location);
  1778. return true;
  1779. }
  1780. return false;
  1781. }
  1782. /**
  1783. * Sets the item's depth level in the LP tree (0 is at root)
  1784. * @param integer Level
  1785. */
  1786. function set_level($int=0){
  1787. if($this->debug>0){error_log('New LP - In learnpathItem::set_level('.$int.')',0);}
  1788. if(!empty($int) AND $int == strval(intval($int))){$this->level = $int;}
  1789. }
  1790. /**
  1791. * Sets the lp_view id this item view is registered to
  1792. * @param integer lp_view DB ID
  1793. * @todo //todo insert into lp_item_view if lp_view not exists
  1794. */
  1795. function set_lp_view($lp_view_id)
  1796. {
  1797. if($this->debug>0){error_log('New LP - In learnpathItem::set_lp_view('.$lp_view_id.')',0);}
  1798. if(!empty($lp_view_id) and $lp_view_id = intval(strval($lp_view_id)))
  1799. {
  1800. $this->view_id = $lp_view_id;
  1801. $item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  1802. //get the lp_item_view with the highest view_count
  1803. $sql = "SELECT * FROM $item_view_table WHERE lp_item_id = ".$this->get_id()." " .
  1804. " AND lp_view_id = ".$lp_view_id." ORDER BY view_count DESC";
  1805. if($this->debug>2){error_log('New LP - In learnpathItem::set_lp_view() - Querying lp_item_view: '.$sql,0);}
  1806. $res = api_sql_query($sql,__FILE__,__LINE__);
  1807. if(Database::num_rows($res)>0){
  1808. $row = Database::fetch_array($res);
  1809. $this->db_item_view_id = $row['id'];
  1810. $this->attempt_id = $row['view_count'];
  1811. $this->current_score = $row['score'];
  1812. $this->current_data = $row['suspend_data'];
  1813. $this->view_max_score = $row['max_score'];
  1814. //$this->view_min_score = $row['min_score'];
  1815. $this->status = $row['status'];
  1816. $this->current_start_time = $row['start_time'];
  1817. $this->current_stop_time = $this->current_start_time + $row['total_time'];
  1818. $this->lesson_location = $row['lesson_location'];
  1819. $this->core_exit = $row['core_exit'];
  1820. if($this->debug>2){error_log('New LP - In learnpathItem::set_lp_view() - Updated item object with database values',0);}
  1821. //now get the number of interactions for this little guy
  1822. $item_view_interaction_table = Database::get_course_table(TABLE_LP_IV_INTERACTION);
  1823. $sql = "SELECT * FROM $item_view_interaction_table WHERE lp_iv_id = '".$this->db_item_view_id."'";
  1824. $res = api_sql_query($sql,__FILE__,__LINE__);
  1825. if($res !== false){
  1826. $this->interactions_count = Database::num_rows($res);
  1827. }else{
  1828. $this->interactions_count = 0;
  1829. }
  1830. //now get the number of objectives for this little guy
  1831. $item_view_objective_table = Database::get_course_table(TABLE_LP_IV_OBJECTIVE);
  1832. $sql = "SELECT * FROM $item_view_objective_table WHERE lp_iv_id = '".$this->db_item_view_id."'";
  1833. $res = api_sql_query($sql,__FILE__,__LINE__);
  1834. if($res !== false){
  1835. $this->objectives_count = Database::num_rows($res);
  1836. }else{
  1837. $this->objectives_count = 0;
  1838. }
  1839. }
  1840. }
  1841. //end
  1842. if($this->debug>2){error_log('New LP - End of learnpathItem::set_lp_view()',0);}
  1843. }
  1844. /**
  1845. * Sets the path
  1846. * @param string Path
  1847. */
  1848. function set_path($string=''){
  1849. if($this->debug>0){error_log('New LP - In learnpathItem::set_path()',0);}
  1850. if(!empty($string)){$this->path = $string;}
  1851. }
  1852. /**
  1853. * Sets the prevent_reinit attribute. This is based on the LP value and is set at creation time for
  1854. * each learnpathItem. It is a (bad?) way of avoiding a reference to the LP when saving an item.
  1855. * @param integer 1 for "prevent", 0 for "don't prevent" saving freshened values (new "not attempted" status etc)
  1856. */
  1857. function set_prevent_reinit($prevent){
  1858. if($this->debug>0){error_log('New LP - In learnpathItem::set_prevent_reinit()',0);}
  1859. if($prevent){
  1860. $this->prevent_reinit = 1;
  1861. }else{
  1862. $this->prevent_reinit = 0;
  1863. }
  1864. }
  1865. /**
  1866. * Sets the score value. If the mastery_score is set and the score reaches
  1867. * it, then set the status to 'passed'.
  1868. * @param float Score
  1869. * @return boolean True on success, false otherwise
  1870. */
  1871. function set_score($score)
  1872. {
  1873. if($this->debug>0){error_log('New LP - In learnpathItem::set_score('.$score.')',0);}
  1874. if(($this->max_score<=0 || $score <= $this->max_score) && ($score >= $this->min_score))
  1875. {
  1876. $this->current_score = $score;
  1877. $master = $this->get_mastery_score();
  1878. $current_status = $this->get_status(false);
  1879. //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'
  1880. if($master != -1 && $this->current_score >= $master && $current_status != $this->possible_status[2])
  1881. {
  1882. $this->set_status($this->possible_status[3]);
  1883. }
  1884. elseif($master != -1 && $this->current_score<$master)
  1885. {
  1886. $this->set_status($this->possible_status[4]);
  1887. }
  1888. return true;
  1889. }
  1890. return false;
  1891. }
  1892. /**
  1893. * Sets the maximum score for this item
  1894. * @param int Maximum score - must be a decimal or an empty string
  1895. * @return boolean True on success, false on error
  1896. */
  1897. function set_max_score($score)
  1898. {
  1899. if($this->debug>0){error_log('New LP - In learnpathItem::set_max_score('.$score.')',0);}
  1900. if(is_int($score) or $score == '')
  1901. {
  1902. $this->view_max_score = Database::escape_string($score);
  1903. 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);}
  1904. return true;
  1905. }
  1906. return false;
  1907. }
  1908. /**
  1909. * Sets the status for this item
  1910. * @param string Status - must be one of the values defined in $this->possible_status
  1911. * @return boolean True on success, false on error
  1912. */
  1913. function set_status($status)
  1914. {
  1915. if($this->debug>0){error_log('New LP - In learnpathItem::set_status('.$status.')',0);}
  1916. $found = false;
  1917. foreach($this->possible_status as $possible){
  1918. if(preg_match('/^'.$possible.'$/i',$status)){
  1919. $found = true;
  1920. }
  1921. }
  1922. //if(in_array($status,$this->possible_status))
  1923. if($found)
  1924. {
  1925. $this->status = Database::escape_string($status);
  1926. if($this->debug>1){error_log('New LP - In learnpathItem::set_status() - Updated object status of item '.$this->db_id.' to '.$this->status,0);}
  1927. return true;
  1928. }
  1929. //error_log('New LP - '.$status.' was not in the possible status',0);
  1930. $this->status = $this->possible_status[0];
  1931. return false;
  1932. }
  1933. /**
  1934. * Set the terms for this learnpath item
  1935. * @param string Terms, as a comma-split list
  1936. * @return boolean Always return true
  1937. */
  1938. function set_terms($terms) {
  1939. global $charset;
  1940. $lp_item = Database::get_course_table(TABLE_LP_ITEM);
  1941. require_once(api_get_path(LIBRARY_PATH).'search/DokeosIndexer.class.php');
  1942. $a_terms = split(',',$terms);
  1943. $i_terms = split(',',$this->get_terms());
  1944. foreach ( $i_terms as $term ) {
  1945. if ( !in_array($term,$a_terms) ) { array_push($a_terms,$term); }
  1946. }
  1947. $new_terms = $a_terms;
  1948. $new_terms_string = implode(',',$new_terms);
  1949. $terms_update_sql='';
  1950. //TODO: validate csv string
  1951. $terms_update_sql = "UPDATE $lp_item SET terms = '". Database::escape_string(api_htmlentities($new_terms_string, ENT_QUOTES, $charset)) . "' WHERE id=".$this->get_id();
  1952. $res = api_sql_query($terms_update_sql,__FILE__,__LINE__);
  1953. // save it to search engine
  1954. if (api_get_setting('search_enabled') == 'true') {
  1955. $di = new DokeosIndexer();
  1956. $di->update_terms($this->get_search_did(), $new_terms);
  1957. }
  1958. return true;
  1959. }
  1960. /**
  1961. * Get the document ID from inside the text index database
  1962. * @return int Search index database document ID
  1963. */
  1964. function get_search_did()
  1965. {
  1966. return $this->search_did;
  1967. }
  1968. /**
  1969. * Sets the item viewing time in a usable form, given that SCORM packages often give it as 00:00:00.0000
  1970. * @param string Time as given by SCORM
  1971. */
  1972. function set_time($scorm_time,$format='scorm')
  1973. {
  1974. if($this->debug>0){error_log('New LP - In learnpathItem::set_time('.$scorm_time.')',0);}
  1975. if($scorm_time == 0 and ($this->type!='sco') and $this->current_start_time!=0){
  1976. $my_time = time() - $this->current_start_time;
  1977. if($my_time > 0){
  1978. $this->update_time($my_time);
  1979. if($this->debug>0){error_log('New LP - In learnpathItem::set_time('.$scorm_time.') - found asset - set time to '.$my_time,0);}
  1980. }
  1981. }else{
  1982. if($format == 'scorm'){
  1983. $res = array();
  1984. if(preg_match('/^(\d{1,4}):(\d{2}):(\d{2})(\.\d{1,4})?/',$scorm_time,$res)){
  1985. $time = time();
  1986. $hour = $res[1];
  1987. $min = $res[2];
  1988. $sec = $res[3];
  1989. //getting total number of seconds spent
  1990. $total_sec = $hour*3600 + $min*60 + $sec;
  1991. $this->update_time($total_sec);
  1992. }
  1993. }elseif($format == 'int'){
  1994. $this->update_time($scorm_time);
  1995. }
  1996. }
  1997. }
  1998. /**
  1999. * Sets the item's title
  2000. * @param string Title
  2001. */
  2002. function set_title($string=''){
  2003. if($this->debug>0){error_log('New LP - In learnpathItem::set_title()',0);}
  2004. if(!empty($string)){$this->title = $string;}
  2005. }
  2006. /**
  2007. * Sets the item's type
  2008. * @param string Type
  2009. */
  2010. function set_type($string=''){
  2011. if($this->debug>0){error_log('New LP - In learnpathItem::set_type()',0);}
  2012. if(!empty($string)){$this->type = $string;}
  2013. }
  2014. /**
  2015. * Checks if the current status is part of the list of status given
  2016. * @param strings_array An array of status to check for. If the current status is one of the strings, return true
  2017. * @return boolean True if the status was one of the given strings, false otherwise
  2018. */
  2019. function status_is($list=array())
  2020. {
  2021. if($this->debug>1){error_log('New LP - In learnpathItem::status_is('.print_r($list,true).') on item '.$this->db_id,0);}
  2022. $mystatus = $this->get_status(true);
  2023. if(empty($mystatus)){
  2024. return false;
  2025. }
  2026. $found = false;
  2027. foreach($list as $status)
  2028. {
  2029. if(preg_match('/^'.$status.'$/i',$mystatus))
  2030. {
  2031. if($this->debug>2){error_log('New LP - learnpathItem::status_is() - Found status '.$status.' corresponding to current status',0);}
  2032. $found = true;
  2033. return $found;
  2034. }
  2035. }
  2036. if($this->debug>2){error_log('New LP - learnpathItem::status_is() - Status '.$mystatus.' did not match request',0);}
  2037. return $found;
  2038. }
  2039. /**
  2040. * Updates the time info according to the given session_time
  2041. * @param integer Time in seconds
  2042. * //TODO @TODO make this method better by allowing better/multiple time slices
  2043. */
  2044. function update_time($total_sec=0){
  2045. if($this->debug>0){error_log('New LP - In learnpathItem::update_time('.$total_sec.')',0);}
  2046. if($total_sec>=0){
  2047. //getting start time from finish time. The only problem in the calculation is it might be
  2048. //modified by the scripts processing time
  2049. $now = time();
  2050. $start = $now-$total_sec;
  2051. $this->current_start_time = $start;
  2052. $this->current_stop_time = $now;
  2053. /*if(empty($this->current_start_time)){
  2054. $this->current_start_time = $start;
  2055. $this->current_stop_time = $now;
  2056. }else{
  2057. //if($this->current_stop_time != $this->current_start_time){
  2058. //if the stop time has already been set before to something else
  2059. //than the start time, add the given time to what's already been
  2060. //recorder.
  2061. //This is the SCORM way of doing things, because the time comes from
  2062. //core.session_time, not core.total_time
  2063. // UPDATE: adding time to previous time is only done on SCORM's finish()
  2064. // call, not normally, so for now ignore this section
  2065. // $this->current_stop_time = $this->current_stop_time + $stop;
  2066. // error_log('New LP - Adding '.$stop.' seconds - now '.$this->current_stop_time,0);
  2067. //}else{
  2068. //if no previous stop time set, use the one just calculated now from
  2069. //start time
  2070. //$this->current_start_time = $start;
  2071. //$this->current_stop_time = $now;
  2072. //error_log('New LP - Setting '.$stop.' seconds - now '.$this->current_stop_time,0);
  2073. //}
  2074. }*/
  2075. }
  2076. }
  2077. /**
  2078. * Write objectives to DB. This method is separate from write_to_db() because otherwise
  2079. * objectives are lost as a side effect to AJAX and session concurrent access
  2080. * @return boolean True or false on error
  2081. */
  2082. function write_objectives_to_db()
  2083. {
  2084. if($this->debug>0){error_log('New LP - In learnpathItem::write_objectives_to_db()',0);}
  2085. if(is_array($this->objectives) && count($this->objectives)>0){
  2086. //save objectives
  2087. $tbl = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  2088. $sql = "SELECT id FROM $tbl " .
  2089. "WHERE lp_item_id = ".$this->db_id." " .
  2090. "AND lp_view_id = ".$this->view_id." " .
  2091. "AND view_count = ".$this->attempt_id;
  2092. $res = api_sql_query($sql,__FILE__,__LINE__);
  2093. if(Database::num_rows($res)>0){
  2094. $row = Database::fetch_array($res);
  2095. $lp_iv_id = $row[0];
  2096. if($this->debug>2){error_log('New LP - In learnpathItem::write_to_db() - Got item_view_id '.$lp_iv_id.', now checking objectives ',0);}
  2097. foreach($this->objectives as $index => $objective){
  2098. $iva_table = Database::get_course_table(TABLE_LP_IV_OBJECTIVE);
  2099. $iva_sql = "SELECT id FROM $iva_table " .
  2100. "WHERE lp_iv_id = $lp_iv_id " .
  2101. //"AND order_id = $index";
  2102. //also check for the objective ID as it must be unique for this SCO view
  2103. "AND objective_id = '".Database::escape_string($objective[0])."'";
  2104. $iva_res = api_sql_query($iva_sql,__FILE__,__LINE__);
  2105. //id(0), type(1), time(2), weighting(3),correct_responses(4),student_response(5),result(6),latency(7)
  2106. if(Database::num_rows($iva_res)>0){
  2107. //update (or don't)
  2108. $iva_row = Database::fetch_array($iva_res);
  2109. $iva_id = $iva_row[0];
  2110. $ivau_sql = "UPDATE $iva_table " .
  2111. "SET objective_id = '".Database::escape_string($objective[0])."'," .
  2112. "status = '".Database::escape_string($objective[1])."'," .
  2113. "score_raw = '".Database::escape_string($objective[2])."'," .
  2114. "score_min = '".Database::escape_string($objective[4])."'," .
  2115. "score_max = '".Database::escape_string($objective[3])."' " .
  2116. "WHERE id = $iva_id";
  2117. $ivau_res = api_sql_query($ivau_sql,__FILE__,__LINE__);
  2118. //error_log($ivau_sql,0);
  2119. }else{
  2120. //insert new one
  2121. $ivai_sql = "INSERT INTO $iva_table " .
  2122. "(lp_iv_id, order_id, objective_id, status, score_raw, score_min, score_max )" .
  2123. "VALUES" .
  2124. "(".$lp_iv_id.", ".$index.",'".Database::escape_string($objective[0])."','".Database::escape_string($objective[1])."'," .
  2125. "'".Database::escape_string($objective[2])."','".Database::escape_string($objective[4])."','".Database::escape_string($objective[3])."')";
  2126. $ivai_res = api_sql_query($ivai_sql,__FILE__,__LINE__);
  2127. //error_log($ivai_sql);
  2128. }
  2129. }
  2130. }
  2131. }
  2132. else
  2133. {
  2134. //error_log('no objective to save: '.print_r($this->objectives,1));
  2135. }
  2136. }
  2137. /**
  2138. * Writes the current data to the database
  2139. * @return boolean Query result
  2140. */
  2141. function write_to_db()
  2142. {
  2143. if($this->debug>0){error_log('New LP - In learnpathItem::write_to_db()',0);}
  2144. $mode = $this->get_lesson_mode();
  2145. $credit = $this->get_credit();
  2146. $my_verified_status=$this->get_status(false);
  2147. $item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  2148. $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.'" ;';
  2149. $rs_verified=api_sql_query($sql_verified,__FILE__,__LINE__);
  2150. $row_verified=Database::fetch_array($rs_verified);
  2151. $my_case_completed=array('completed','passed','browsed','failed');//added by isaac flores
  2152. if (in_array($sql_verified['status'],$my_case_completed)) {
  2153. $save=false;
  2154. } else {
  2155. $save=true;
  2156. }
  2157. if (($save===false && $this->type == 'sco') ||(($this->type == 'sco') && ($credit == 'no-credit' OR $mode == 'review' OR $mode == 'browse')))
  2158. {
  2159. //this info shouldn't be saved as the credit or lesson mode info prevent it
  2160. if($this->debug>1){error_log('New LP - In learnpathItem::write_to_db() - credit('.$credit.') or lesson_mode('.$mode.') prevent recording!',0);}
  2161. } else {
  2162. //check the row exists
  2163. $inserted = false;
  2164. // this a special case for multiple attempts and Dokeos exercises
  2165. if ($this->type == 'quiz' && $this->get_prevent_reinit()==0 && $this->get_status()=='completed') {
  2166. // we force the item to be restarted
  2167. $this->restart();
  2168. $sql = "INSERT INTO $item_view_table " .
  2169. "(total_time, " .
  2170. "start_time, " .
  2171. "score, " .
  2172. "status, " .
  2173. "max_score, ".
  2174. "lp_item_id, " .
  2175. "lp_view_id, " .
  2176. "view_count, " .
  2177. "suspend_data, " .
  2178. //"max_time_allowed," .
  2179. "lesson_location)" .
  2180. "VALUES" .
  2181. "(".$this->get_total_time()."," .
  2182. "".$this->current_start_time."," .
  2183. "".$this->get_score()."," .
  2184. "'".$this->get_status(false)."'," .
  2185. "'".$this->get_max()."'," .
  2186. "".$this->db_id."," .
  2187. "".$this->view_id."," .
  2188. "".$this->get_attempt_id()."," .
  2189. "'".Database::escape_string($this->current_data)."'," .
  2190. //"'".$this->get_max_time_allowed()."'," .
  2191. "'".$this->lesson_location."')";
  2192. if($this->debug>2){error_log('New LP - In learnpathItem::write_to_db() - Inserting into item_view forced: '.$sql,0);}
  2193. $res = api_sql_query($sql,__FILE__,__LINE__);
  2194. $this->db_item_view_id = Database::get_last_insert_id();
  2195. $inserted = true;
  2196. }
  2197. $item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  2198. $check = "SELECT * FROM $item_view_table " .
  2199. "WHERE lp_item_id = ".$this->db_id. " " .
  2200. "AND lp_view_id = ".$this->view_id. " ".
  2201. "AND view_count = ".$this->get_attempt_id();
  2202. if($this->debug>2){error_log('New LP - In learnpathItem::write_to_db() - Querying item_view: '.$check,0);}
  2203. $check_res = api_sql_query($check);
  2204. //depending on what we want (really), we'll update or insert a new row
  2205. //now save into DB
  2206. $res = 0;
  2207. if( $inserted==false && Database::num_rows($check_res)<1){
  2208. /*$my_status = '';
  2209. if ($this->type!=TOOL_QUIZ) {
  2210. $my_status = $this->get_status(false);
  2211. }*/
  2212. $sql = "INSERT INTO $item_view_table " .
  2213. "(total_time, " .
  2214. "start_time, " .
  2215. "score, " .
  2216. "status, " .
  2217. "max_score, ".
  2218. "lp_item_id, " .
  2219. "lp_view_id, " .
  2220. "view_count, " .
  2221. "suspend_data, " .
  2222. //"max_time_allowed," .
  2223. "lesson_location)" .
  2224. "VALUES" .
  2225. "(".$this->get_total_time()."," .
  2226. "".$this->current_start_time."," .
  2227. "".$this->get_score()."," .
  2228. "'".$this->get_status(false)."'," .
  2229. "'".$this->get_max()."'," .
  2230. "".$this->db_id."," .
  2231. "".$this->view_id."," .
  2232. "".$this->get_attempt_id()."," .
  2233. "'".Database::escape_string($this->current_data)."'," .
  2234. //"'".$this->get_max_time_allowed()."'," .
  2235. "'".$this->lesson_location."')";
  2236. if($this->debug>2){error_log('New LP - In learnpathItem::write_to_db() - Inserting into item_view: '.$sql,0);}
  2237. $res = api_sql_query($sql,__FILE__,__LINE__);
  2238. $this->db_item_view_id = Database::get_last_insert_id();
  2239. } else {
  2240. $sql = '';
  2241. if($this->type=='hotpotatoes') {
  2242. //make an exception for HotPotatoes, don't update the score
  2243. //because it has been saved outside of this tool
  2244. $sql = "UPDATE $item_view_table " .
  2245. "SET total_time = ".$this->get_total_time().", " .
  2246. " start_time = ".$this->get_current_start_time().", " .
  2247. " score = ".$this->get_score().", " .
  2248. " status = '".$this->get_status(false)."'," .
  2249. " max_score = '".$this->get_max()."'," .
  2250. " suspend_data = '".Database::escape_string($this->current_data)."'," .
  2251. " lesson_location = '".$this->lesson_location."' " .
  2252. "WHERE lp_item_id = ".$this->db_id." " .
  2253. "AND lp_view_id = ".$this->view_id." " .
  2254. "AND view_count = ".$this->attempt_id;
  2255. } else {
  2256. //for all other content types...
  2257. if ($this->type=='quiz') {
  2258. $my_status = ' ';
  2259. $total_time = ' ';
  2260. if (!empty($_REQUEST['exeId'])) {
  2261. $TBL_TRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  2262. $safe_exe_id = Database::escape_string($_REQUEST['exeId']);
  2263. $sql = 'SELECT start_date,exe_date FROM ' . $TBL_TRACK_EXERCICES . ' WHERE exe_id = '.(int)$safe_exe_id;
  2264. $res = api_sql_query($sql,__FILE__,__LINE__);
  2265. $row_dates = Database::fetch_array($res);
  2266. $time_start_date = convert_mysql_date($row_dates['start_date']);
  2267. $time_exe_date = convert_mysql_date($row_dates['exe_date']);
  2268. $mytime = ((int)$time_exe_date-(int)$time_start_date);
  2269. $total_time =" total_time = ".$mytime.", ";
  2270. }
  2271. } else {
  2272. $my_type_lp=learnpath::get_type_static($this->lp_id);
  2273. // this is a array containing values finished
  2274. $case_completed=array('completed','passed','browsed');
  2275. //is not multiple attempts
  2276. if ($this->get_prevent_reinit()==1) {
  2277. // process of status verified into data base
  2278. $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.'" ;';
  2279. $rs_verified=api_sql_query($sql_verified,__FILE__,__LINE__);
  2280. $row_verified=Database::fetch_array($rs_verified);
  2281. //get type lp: 1=lp dokeos and 2=scorm
  2282. // if not is completed or passed or browsed and learning path is scorm
  2283. if(!in_array($this->get_status(false),$case_completed) && $my_type_lp==2 ) {//&& $this->type!='dir'
  2284. $total_time =" total_time = total_time +".$this->get_total_time().", ";
  2285. $my_status = " status = '".$this->get_status(false)."' ,";
  2286. } else {
  2287. //verified into data base
  2288. if (!in_array($row_verified['status'],$case_completed) && $my_type_lp==2 ) { //&& $this->type!='dir'
  2289. $total_time =" total_time = total_time +".$this->get_total_time().", ";
  2290. $my_status = " status = '".$this->get_status(false)."' ,";
  2291. } elseif (in_array($row_verified['status'],$case_completed) && $my_type_lp==2 && $this->type!='sco' ) {//&& $this->type!='dir'
  2292. $total_time =" total_time = total_time +".$this->get_total_time().", ";
  2293. $my_status = " status = '".$this->get_status(false)."' ,";
  2294. }else {
  2295. //&& !in_array($row_verified['status'],$case_completed)
  2296. //is lp dokeos
  2297. if ($my_type_lp==1 && $this->type!='chapter') {
  2298. $total_time =" total_time = total_time + ".$this->get_total_time().", ";
  2299. $my_status = " status = '".$this->get_status(false)."' ,";
  2300. }
  2301. }
  2302. }
  2303. } else {
  2304. // is multiple attempts
  2305. if (in_array($this->get_status(false),$case_completed) && $my_type_lp==2) {
  2306. //reset zero new attempt ?
  2307. $my_status = " status = '".$this->get_status(false)."' ,";
  2308. } elseif (!in_array($this->get_status(false),$case_completed) && $my_type_lp==2){
  2309. $total_time =" total_time = ".$this->get_total_time().", ";
  2310. $my_status = " status = '".$this->get_status(false)."' ,";
  2311. } else {
  2312. //is dokeos LP
  2313. $total_time =" total_time = total_time +".$this->get_total_time().", ";
  2314. $my_status = " status = '".$this->get_status(false)."' ,";
  2315. }
  2316. //code added by isaac flores
  2317. //this code line fix the problem of wrong status
  2318. if ( $my_type_lp==2) {
  2319. //verify current status in multiples attempts
  2320. $sql_status='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.'" ';
  2321. $rs_status=Database::query($sql_status,__FILE__,__LINE__);
  2322. $current_status=Database::result($rs_status,0,'status');
  2323. if (in_array($current_status,$case_completed)) {
  2324. $my_status='';
  2325. $total_time='';
  2326. } else {
  2327. $total_time =" total_time = total_time +".$this->get_total_time().", ";
  2328. }
  2329. }
  2330. }
  2331. /*if ($my_type_lp==1 && !in_array($row_verified['status'],$case_completed)) {
  2332. $total_time =" total_time = total_time + ".$this->get_total_time().", ";
  2333. }*/
  2334. }
  2335. $sql = "UPDATE $item_view_table " .
  2336. "SET " .$total_time.
  2337. " start_time = ".$this->get_current_start_time().", " .
  2338. " score = ".$this->get_score().", " .
  2339. $my_status.
  2340. " max_score = '".$this->get_max()."'," .
  2341. " suspend_data = '".Database::escape_string($this->current_data)."'," .
  2342. //" max_time_allowed = '".$this->get_max_time_allowed()."'," .
  2343. " lesson_location = '".$this->lesson_location."' " .
  2344. "WHERE lp_item_id = ".$this->db_id." " .
  2345. "AND lp_view_id = ".$this->view_id." " .
  2346. "AND view_count = ".$this->attempt_id;
  2347. $this->current_start_time = time();
  2348. }
  2349. if($this->debug>2){error_log('New LP - In learnpathItem::write_to_db() - Updating item_view: '.$sql,0);}
  2350. $res = api_sql_query($sql,__FILE__,__LINE__);
  2351. }
  2352. //if(!$res)
  2353. //{
  2354. // $this->error = 'Could not update item_view table...'.mysql_error();
  2355. //}
  2356. if(is_array($this->interactions) && count($this->interactions)>0){
  2357. //save interactions
  2358. $tbl = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  2359. $sql = "SELECT id FROM $tbl " .
  2360. "WHERE lp_item_id = ".$this->db_id." " .
  2361. "AND lp_view_id = ".$this->view_id." " .
  2362. "AND view_count = ".$this->attempt_id;
  2363. $res = api_sql_query($sql,__FILE__,__LINE__);
  2364. if(Database::num_rows($res)>0){
  2365. $row = Database::fetch_array($res);
  2366. $lp_iv_id = $row[0];
  2367. if($this->debug>2){error_log('New LP - In learnpathItem::write_to_db() - Got item_view_id '.$lp_iv_id.', now checking interactions ',0);}
  2368. foreach($this->interactions as $index => $interaction){
  2369. $correct_resp = '';
  2370. if(is_array($interaction[4]) && !empty($interaction[4][0]) ){
  2371. foreach($interaction[4] as $resp){
  2372. $correct_resp .= $resp.',';
  2373. }
  2374. $correct_resp = substr($correct_resp,0,strlen($correct_resp)-1);
  2375. }
  2376. $iva_table = Database::get_course_table(TABLE_LP_IV_INTERACTION);
  2377. $iva_sql = "SELECT id FROM $iva_table " .
  2378. "WHERE lp_iv_id = $lp_iv_id " .
  2379. // "AND order_id = $index";
  2380. //also check for the interaction ID as it must be unique for this SCO view
  2381. "AND (order_id = $index " .
  2382. "OR interaction_id = '".Database::escape_string($interaction[0])."')";
  2383. $iva_res = api_sql_query($iva_sql,__FILE__,__LINE__);
  2384. //id(0), type(1), time(2), weighting(3),correct_responses(4),student_response(5),result(6),latency(7)
  2385. if(Database::num_rows($iva_res)>0){
  2386. //update (or don't)
  2387. $iva_row = Database::fetch_array($iva_res);
  2388. $iva_id = $iva_row[0];
  2389. $ivau_sql = "UPDATE $iva_table " .
  2390. "SET interaction_id = '".Database::escape_string($interaction[0])."'," .
  2391. "interaction_type = '".Database::escape_string($interaction[1])."'," .
  2392. "weighting = '".Database::escape_string($interaction[3])."'," .
  2393. "completion_time = '".Database::escape_string($interaction[2])."'," .
  2394. "correct_responses = '".Database::escape_string($correct_resp)."'," .
  2395. "student_response = '".Database::escape_string($interaction[5])."'," .
  2396. "result = '".Database::escape_string($interaction[6])."'," .
  2397. "latency = '".Database::escape_string($interaction[7])."'" .
  2398. "WHERE id = $iva_id";
  2399. $ivau_res = api_sql_query($ivau_sql,__FILE__,__LINE__);
  2400. } else {
  2401. //insert new one
  2402. $ivai_sql = "INSERT INTO $iva_table " .
  2403. "(order_id, lp_iv_id, interaction_id, interaction_type, " .
  2404. "weighting, completion_time, correct_responses, " .
  2405. "student_response, result, latency)" .
  2406. "VALUES" .
  2407. "(".$index.",".$lp_iv_id.",'".Database::escape_string($interaction[0])."','".Database::escape_string($interaction[1])."'," .
  2408. "'".Database::escape_string($interaction[3])."','".Database::escape_string($interaction[2])."','".Database::escape_string($correct_resp)."'," .
  2409. "'".Database::escape_string($interaction[5])."','".Database::escape_string($interaction[6])."','".Database::escape_string($interaction[7])."'" .
  2410. ")";
  2411. $ivai_res = api_sql_query($ivai_sql,__FILE__,__LINE__);
  2412. }
  2413. }
  2414. }
  2415. }
  2416. }
  2417. if($this->debug>2){error_log('New LP - End of learnpathItem::write_to_db()',0);}
  2418. return true;
  2419. }
  2420. }
  2421. ?>