learnpathItem.class.php 67 KB

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