learnpathItem.class.php 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  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','succeeded','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;
  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. /*
  123. if(is_array($this->interactions[$index]) && count($this->interactions[$index])>0){
  124. $this->interactions[$index] = $params;
  125. return false;
  126. }else{
  127. if(count($params)==8){//we rely on the calling script to provide parameters in the right order
  128. $this->interactions[$index] = $params;
  129. return true;
  130. }else{
  131. return false;
  132. }
  133. }
  134. */
  135. }
  136. /**
  137. * Closes/stops the item viewing. Finalises runtime values. If required, save to DB.
  138. * @return boolean True on success, false otherwise
  139. */
  140. function close()
  141. {
  142. if($this->debug>0){error_log('New LP - In learnpathItem::close()',0);}
  143. $this->current_stop_time = time();
  144. if($this->get_type() != 'sco'){
  145. $this->status = $this->possible_status[2];
  146. }
  147. if($this->save_on_close)
  148. {
  149. $this->save();
  150. }
  151. return true;
  152. }
  153. /**
  154. * Deletes all traces of this item in the database
  155. * @return boolean true. Doesn't check for errors yet.
  156. */
  157. function delete()
  158. {
  159. if($this->debug>0){error_log('New LP - In learnpath_item::delete() for item '.$this->db_id,0);}
  160. $lp_item_view = Database::get_course_table('lp_item_view');
  161. $lp_item = Database::get_course_table('lp_item');
  162. $sql_del_view = "DELETE FROM $lp_item_view WHERE item_id = ".$this->db_id;
  163. //error_log('New LP - Deleting from lp_item_view: '.$sql_del_view,0);
  164. $res_del_view = api_sql_query($sql_del_view);
  165. $sql_del_item = "DELETE FROM $lp_item WHERE id = ".$this->db_id;
  166. //error_log('New LP - Deleting from lp_item: '.$sql_del_view,0);
  167. $res_del_item = api_sql_query($sql_del_item);
  168. return true;
  169. }
  170. /**
  171. * Drops a child from the children array
  172. * @param string index of child item to drop
  173. * @return void
  174. */
  175. function drop_child($item)
  176. {
  177. if($this->debug>0){error_log('New LP - In learnpathItem::drop_child()',0);}
  178. if(!empty($item))
  179. {
  180. foreach($this->children as $index => $child)
  181. {
  182. if($child == $item){
  183. $this->children[$index] = null;
  184. }
  185. }
  186. }
  187. }
  188. /**
  189. * Gets the current attempt_id for this user on this item
  190. * @return integer The attempt_id for this item view by this user, or 1 if none defined
  191. */
  192. function get_attempt_id()
  193. {
  194. if($this->debug>0){error_log('New LP - In learnpathItem::get_attempt_id()',0);}
  195. if(!empty($this->attempt_id))
  196. {
  197. return $this->attempt_id;
  198. }
  199. return 1;
  200. }
  201. /**
  202. * Gets a list of the item's children
  203. * @return array Array of children items IDs
  204. */
  205. function get_children()
  206. {
  207. if($this->debug>0){error_log('New LP - In learnpathItem::get_children()',0);}
  208. $list = array();
  209. foreach($this->children as $child){
  210. if(!empty($child))
  211. {
  212. //error_log('New LP - Found '.$child,0);
  213. $list[] = $child;
  214. }
  215. }
  216. return $list;
  217. }
  218. /**
  219. * Gets the credit information (rather scorm-stuff) based on current status and reinit
  220. * autorization. Credit tells the sco(content) if Dokeos will record the data it is sent (credit) or not (no-credit)
  221. * @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.
  222. */
  223. function get_credit(){
  224. if(!empty($this->debug) && $this->debug>1){error_log('In learnpathItem::get_credit()',0);}
  225. $credit = 'credit';
  226. //now check the value of prevent_reinit (if it's 0, return credit as the default was)
  227. if($this->get_prevent_reinit() != 0){ //if prevent_reinit == 1 (or more)
  228. //if status is not attempted or incomplete, credit anyway. Otherwise:
  229. //check the status in the database rather than in the object, as checking in the object
  230. //would always return "no-credit" when we want to set it to completed
  231. $status = $this->get_status(true);
  232. if(!empty($this->debug) && $this->debug>2){error_log('In learnpathItem::get_credit() - get_prevent_reinit!=0 and status is '.$status,0);}
  233. if($status != $this->possible_status[0] AND $status != $this->possible_status[1]){
  234. $credit = 'no-credit';
  235. }
  236. }
  237. return $credit;
  238. }
  239. /**
  240. * Gets the current start time property
  241. * @return integer Current start time, or current time if none
  242. */
  243. function get_current_start_time()
  244. {
  245. if($this->debug>0){error_log('New LP - In learnpathItem::get_current_start_time()',0);}
  246. if(empty($this->current_start_time))
  247. {
  248. return time();
  249. }else{
  250. return $this->current_start_time;
  251. }
  252. }
  253. /**
  254. * Gets the item's description
  255. * @return string Description
  256. */
  257. function get_description(){
  258. if($this->debug>0){error_log('New LP - In learnpathItem::get_description()',0);}
  259. if(empty($this->description)){return '';}
  260. return $this->description;
  261. }
  262. /**
  263. * Gets the DB ID
  264. * @return integer Database ID for the current item
  265. */
  266. function get_id(){
  267. if($this->debug>1){error_log('New LP - In learnpathItem::get_id()',0);}
  268. if(!empty($this->db_id))
  269. {
  270. return $this->db_id;
  271. }
  272. //TODO check this return value is valid for children classes (SCORM?)
  273. return 0;
  274. }
  275. /**
  276. * Gets the current count of interactions recorded in the database
  277. * @return int The current number of interactions recorder
  278. */
  279. function get_interactions_count()
  280. {
  281. if($this->debug>1){error_log('New LP - In learnpathItem::get_interactions_count()',0);}
  282. $res = 0;
  283. if(!empty($this->interactions_count)){
  284. $res = $this->interactions_count;
  285. }
  286. return $res;
  287. }
  288. /**
  289. * Gets the launch_data field found in imsmanifests (this is SCORM- or AICC-related, really)
  290. * @return string Launch data as found in imsmanifest and stored in Dokeos (read only). Defaults to ''.
  291. */
  292. function get_launch_data(){
  293. if($this->debug>0){error_log('New LP - In learnpathItem::get_launch_data()',0);}
  294. if(!empty($this->launch_data)){
  295. return $this->launch_data;
  296. }
  297. return '';
  298. }
  299. /**
  300. * Gets the lesson location
  301. * @return string lesson location as recorded by the SCORM and AICC elements. Defaults to ''
  302. */
  303. function get_lesson_location(){
  304. if($this->debug>0){error_log('New LP - In learnpathItem::get_lesson_location()',0);}
  305. if(!empty($this->lesson_location)){return $this->lesson_location;}else{return '';}
  306. }
  307. /**
  308. * Gets the lesson_mode (scorm feature, but might be used by aicc as well as dokeos paths)
  309. *
  310. * The "browse" mode is not supported yet (because there is no such way of seeing a sco in Dokeos)
  311. * @return string 'browse','normal' or 'review'. Defaults to 'normal'
  312. */
  313. function get_lesson_mode(){
  314. $mode = 'normal';
  315. if($this->get_prevent_reinit() != 0){ //if prevent_reinit == 0
  316. if($this->status != $this->possible_status[0] AND $this->status != $this->possible_status[1]){
  317. $mode = 'review';
  318. }
  319. }
  320. }
  321. /**
  322. * Gets the depth level
  323. * @return int Level. Defaults to 0
  324. */
  325. function get_level(){
  326. if($this->debug>0){error_log('New LP - In learnpathItem::get_level()',0);}
  327. if(empty($this->level)){return 0;}
  328. return $this->level;
  329. }
  330. /**
  331. * Gets the maximum (score)
  332. * @return int Maximum score. Defaults to 100
  333. */
  334. function get_max(){
  335. if($this->debug>0){error_log('New LP - In learnpathItem::get_max()',0);}
  336. if(!empty($this->max_score)){return $this->max_score;}else{return 100;}
  337. }
  338. /**
  339. * Gets the minimum (score)
  340. * @return int Minimum score. Defaults to 0
  341. */
  342. function get_min(){
  343. if($this->debug>0){error_log('New LP - In learnpathItem::get_min()',0);}
  344. if(!empty($this->min_score)){return $this->min_score;}else{return 0;}
  345. }
  346. /**
  347. * Gets the parent ID
  348. * @return int Parent ID. Defaults to null
  349. */
  350. function get_parent(){
  351. if($this->debug>0){error_log('New LP - In learnpathItem::get_parent()',0);}
  352. if(!empty($this->parent))
  353. {
  354. return $this->parent;
  355. }
  356. //TODO check this return value is valid for children classes (SCORM?)
  357. return null;
  358. }
  359. /**
  360. * Gets the path attribute.
  361. * @return string Path. Defaults to ''
  362. */
  363. function get_path(){
  364. if($this->debug>0){error_log('New LP - In learnpathItem::get_path()',0);}
  365. if(empty($this->path)){return '';}
  366. return $this->path;
  367. }
  368. /**
  369. * Gets the prerequisites string
  370. * @return string Empty string or prerequisites string if defined. Defaults to
  371. */
  372. function get_prereq_string()
  373. {
  374. if($this->debug>0){error_log('New LP - In learnpathItem::get_prereq_string()',0);}
  375. if(!empty($this->prereq_string))
  376. {
  377. return $this->prereq_string;
  378. }else{
  379. return '';
  380. }
  381. }
  382. /**
  383. * Gets the prevent_reinit attribute value (and sets it if not set already)
  384. * @return int 1 or 0 (defaults to 1)
  385. */
  386. function get_prevent_reinit(){
  387. if($this->debug>2){error_log('New LP - In learnpathItem::get_prevent_reinit()',0);}
  388. if(!isset($this->prevent_reinit)){
  389. if(!empty($this->lp_id)){
  390. $db = Database::get_course_table('lp');
  391. $sql = "SELECT * FROM $db WHERE id = ".$this->lp_id;
  392. $res = mysql_query($sql);
  393. if(mysql_num_rows($res)<1)
  394. {
  395. $this->error = "Could not find parent learnpath in learnpath table";
  396. if($this->debug>2){error_log('New LP - End of learnpathItem::get_prevent_reinit() - Returning false',0);}
  397. return false;
  398. }else{
  399. $row = Database::fetch_array($res);
  400. $this->prevent_reinit = $row['prevent_reinit'];
  401. }
  402. }else{
  403. $this->prevent_reinit = 1;//prevent reinit is always 1 by default - see learnpath.class.php
  404. }
  405. }
  406. if($this->debug>2){error_log('New LP - End of learnpathItem::get_prevent_reinit() - Returned '.$this->prevent_reinit,0);}
  407. return $this->prevent_reinit;
  408. }
  409. /**
  410. * Gets the score
  411. * @return float The current score or 0 if no score set yet
  412. */
  413. function get_score(){
  414. if($this->debug>0){error_log('New LP - In learnpathItem::get_score()',0);}
  415. if(!empty($this->current_score))
  416. {
  417. return $this->current_score;
  418. }
  419. //TODO check this return value is valid for children classes (SCORM?)
  420. return 0;
  421. }
  422. /**
  423. * Gets the item status
  424. * @param boolean Do or don't check into the database for the latest value. Optional. Default is true
  425. * @param boolean Do or don't update the local attribute value with what's been found in DB
  426. * @return string Current status or 'Nnot attempted' if no status set yet
  427. */
  428. function get_status($check_db=true,$update_local=false)
  429. {
  430. if($this->debug>0){error_log('New LP - In learnpathItem::get_status()',0);}
  431. if($check_db)
  432. {
  433. if($this->debug>2){error_log('New LP - In learnpathItem::get_status(): checking db',0);}
  434. $table = Database::get_course_table('lp_item_view');
  435. $sql = "SELECT * FROM $table WHERE id = '".$this->db_item_view_id."' AND view_count = '".$this->get_attempt_id()."'";
  436. if($this->debug>2){error_log('New LP - In learnpathItem::get_status() - Checking DB: '.$sql,0);}
  437. $res = api_sql_query($sql);
  438. if(Database::num_rows($res)==1){
  439. $row = Database::fetch_array($res);
  440. if($update_local==true){
  441. $this->set_status($row['status']);
  442. }
  443. return $row['status'];
  444. }
  445. }
  446. else
  447. {
  448. if($this->debug>2){error_log('New LP - In learnpathItem::get_status() - in get_status: using attrib',0);}
  449. if(!empty($this->status))
  450. {
  451. return $this->status;
  452. }
  453. }
  454. return $this->possible_status[0];
  455. }
  456. /**
  457. * Gets the suspend data
  458. */
  459. function get_suspend_data()
  460. {
  461. if($this->debug>0){error_log('New LP - In learnpathItem::get_suspend_data()',0);}
  462. if(!empty($this->current_data)){return $this->current_data;}else{return '';}
  463. }
  464. /**
  465. * Gets the total time spent on this item view so far
  466. * @param string Origin of the request. If coming from PHP, send formatted as xxhxx'xx", otherwise use scorm format 00:00:00
  467. * @param integer Given time is a default time to return formatted
  468. */
  469. function get_scorm_time($origin='php',$given_time=null){
  470. //if($this->debug>0){error_log('New LP - In learnpathItem::get_scorm_time()',0);}
  471. $h = get_lang('h');
  472. if(!isset($given_time)){
  473. if(!empty($this->current_start_time)){
  474. if(!empty($this->current_stop_time)){
  475. $time = $this->current_stop_time - $this->current_start_time;
  476. }else{
  477. $time = time() - $this->current_start_time;
  478. }
  479. }else{
  480. if($origin == 'js'){
  481. return '00:00:00';
  482. }else{
  483. return '00'.$h.'00\'00"';
  484. }
  485. }
  486. }else{
  487. $time = $given_time;
  488. }
  489. $hours = $time/3600;
  490. $mins = ($time%3600)/60;
  491. $secs = ($time%60);
  492. if($origin == 'js'){
  493. $scorm_time = sprintf("%4d:%02d:%02d",$hours,$mins,$secs);
  494. }else{
  495. $scorm_time = sprintf("%4d$h%02d'%02d\"",$hours,$mins,$secs);
  496. }
  497. return $scorm_time;
  498. }
  499. /**
  500. * Returns the item's title
  501. * @return string Title
  502. */
  503. function get_title(){
  504. if($this->debug>0){error_log('New LP - In learnpathItem::get_title()',0);}
  505. if(empty($this->title)){return '';}
  506. return $this->title;
  507. }
  508. /**
  509. * Returns the total time used to see that item
  510. * @return integer Total time
  511. */
  512. function get_total_time(){
  513. if($this->debug>0){error_log('New LP - In learnpathItem::get_total_time()',0);}
  514. if($this->current_start_time == 0){ //shouldn't be necessary thanks to the open() method
  515. $this->current_start_time = time();
  516. }
  517. $time = $this->current_stop_time - $this->current_start_time;
  518. if($time < 0){
  519. return 0;
  520. }else{
  521. 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);}
  522. return $time;
  523. }
  524. }
  525. /**
  526. * Gets the item type
  527. * @return string The item type (can be doc, dir, sco, asset)
  528. */
  529. function get_type()
  530. {
  531. $res = 'asset';
  532. if($this->debug>0){error_log('New LP - In learnpathItem::get_type()',0);}
  533. if(!empty($this->type))
  534. {
  535. //error_log('In item::get_type() - returning '.$this->type,0);
  536. $res = $this->type;
  537. }
  538. if($this->debug>2){error_log('New LP - In learnpathItem::get_type() - Returning '.$res.' for item '.$this->get_id(),0);}
  539. return $res;
  540. }
  541. /**
  542. * Gets the view count for this item
  543. */
  544. function get_view_count(){
  545. if($this->debug>0){error_log('New LP - In learnpathItem::get_view_count()',0);}
  546. if(!empty($this->attempt_id)){
  547. return $this->attempt_id;
  548. }else{
  549. return 0;
  550. }
  551. }
  552. /**
  553. * Tells if an item is done ('completed','passed','succeeded') or not
  554. * @return bool True if the item is done ('completed','passed','succeeded'), false otherwise
  555. */
  556. function is_done(){
  557. if($this->status_is(array('completed','passed','succeeded'))){
  558. if($this->debug>2){error_log('New LP - In learnpath::is_done() - Item '.$this->get_id().' is complete',0);}
  559. return true;
  560. }else{
  561. if($this->debug>2){error_log('New LP - In learnpath::is_done() - Item '.$this->get_id().' is not complete',0);}
  562. return false;
  563. }
  564. }
  565. /**
  566. * Tells if a restart is allowed (take it from $this->prevent_reinit and $this->status)
  567. * @return integer -1 if retaking the sco another time for credit is not allowed,
  568. * 0 if it is not allowed but the item has to be finished
  569. * 1 if it is allowed. Defaults to 1
  570. */
  571. function is_restart_allowed()
  572. {
  573. if($this->debug>2){error_log('New LP - In learnpathItem::is_restart_allowed()',0);}
  574. $restart = 1;
  575. if($this->get_prevent_reinit() > 0){ //if prevent_reinit == 1 (or more)
  576. //if status is not attempted or incomplete, authorize retaking (of the same) anyway. Otherwise:
  577. if($this->status != $this->possible_status[0] AND $this->status != $this->possible_status[1]){
  578. $restart = -1;
  579. }else{
  580. $restart = 0;
  581. }
  582. }else{
  583. if($this->status == $this->possible_status[0] OR $this->status == $this->possible_status[1]){
  584. $restart = 0;
  585. }
  586. }
  587. if($this->debug>2){error_log('New LP - End of learnpathItem::is_restart_allowed() - Returning '.$restart,0);}
  588. return $restart;
  589. }
  590. /**
  591. * Opens/launches the item. Initialises runtime values.
  592. * @return boolean True on success, false on failure.
  593. */
  594. function open(){
  595. if($this->debug>0){error_log('New LP - In learnpathItem::open()',0);}
  596. if($this->prevent_reinit == 0){
  597. $this->current_score = 0;
  598. $this->current_start_time = time();
  599. $this->status = $this->possible_status[1];
  600. $this->attempt_id = $this->attempt_id + 1; //open a new attempt
  601. }else{
  602. if($this->current_start_time == 0){ //small exception for start time, to avoid amazing values
  603. $this->current_start_time = time();
  604. }
  605. //error_log('New LP - reinit blocked by setting',0);
  606. }
  607. }
  608. /**
  609. * Outputs the item contents
  610. * @return string HTML file (displayable in an <iframe>) or empty string if no path defined
  611. */
  612. function output()
  613. {
  614. if($this->debug>0){error_log('New LP - In learnpathItem::output()',0);}
  615. if(!empty($this->path) and is_file($this->path))
  616. {
  617. $output = '';
  618. $output .= file_get_contents($this->path);
  619. return $output;
  620. }
  621. return '';
  622. }
  623. /**
  624. * Parses the prerequisites string with the AICC logic language
  625. * @param string The prerequisites string as it figures in imsmanifest.xml
  626. * @param object Learnpath object pointer. We need it to get a list of other resources the prerequisites might be pointing to.
  627. * @return boolean True if the list of prerequisites given is entirely satisfied, false otherwise
  628. * @TODO //TODO if it's a Dokeos LP, use item ID instead of item REF for prereq!!!
  629. */
  630. function parse_prereq($prereqs_string, &$oLP){
  631. if($this->debug>0){error_log('New LP - In learnpathItem::parse_prereq() for item '.$this->lp_id.' with string '.$prereqs_string,0);}
  632. //deal with &, |, ~, =, <>, {}, ,, X*, () in reverse order
  633. $this->prereq_alert = '';
  634. // First parse all parenthesis by using a sequential loop (looking for less-inclusives first)
  635. if($prereqs_string == '_true_'){return true;}
  636. if($prereqs_string == '_false_'){
  637. if(empty($this->prereq_alert)){
  638. $this->prereq_alert = get_lang('_prereq_not_complete');
  639. }
  640. return false;
  641. }
  642. while(strpos($prereqs_string,'(')!==false){
  643. //remove any () set and replace with its value
  644. $matches = array();
  645. $res = preg_match_all('/(\(([^\(\)]*)\))/',$prereqs_string,$matches);
  646. if($res){
  647. foreach($matches[2] as $id=>$match){
  648. $str_res = $this->parse_prereq($match);
  649. if($str_res){
  650. $prereqs_string = str_replace($matches[1][$id],'_true_',$prereqs_string);
  651. }else{
  652. $prereqs_string = str_replace($matches[1][$id],'_false_',$prereqs_string);
  653. }
  654. }
  655. }
  656. }
  657. //parenthesis removed, now look for ORs as it is the lesser-priority binary operator (= always uses one text operand)
  658. if(strpos($prereqs_string,"|")===false){
  659. if($this->debug>1){error_log('New LP - Didnt find any OR, looking for AND',0);}
  660. if(strpos($prereqs_string,"&")!==false){
  661. $list = split("&",$prereqs_string);
  662. if(count($list)>1){
  663. $andstatus = true;
  664. foreach($list as $condition){
  665. $andstatus = $andstatus && $this->parse_prereq($condition);
  666. if($andstatus==false){
  667. if($this->debug>1){error_log('New LP - One condition in AND was false, short-circuit',0);}
  668. break;
  669. }
  670. }
  671. if(empty($this->prereq_alert) && !$andstatus){
  672. $this->prereq_alert = get_lang('_prereq_not_complete');
  673. }
  674. return $andstatus;
  675. }else{
  676. if(isset($oLP->items[$oLP->refs_list[$list[0]]])){
  677. $status = $oLP->items[$oLP->refs_list[$list[0]]]->get_status(true);
  678. $returnstatus = (($status == $this->possible_status[2]) OR ($status == $this->possible_status[3]));
  679. if(empty($this->prereq_alert) && !$returnstatus){
  680. $this->prereq_alert = get_lang('_prereq_not_complete');
  681. }
  682. return $returnstatus;
  683. }
  684. $this->prereq_alert = get_lang('_prereq_not_complete');
  685. return false;
  686. }
  687. }else{
  688. //no ORs found, now look for ANDs
  689. if($this->debug>1){error_log('New LP - Didnt find any AND, looking for =',0);}
  690. if(strpos($prereqs_string,"=")!==false){
  691. if($this->debug>1){error_log('New LP - Found =, looking into it',0);}
  692. //we assume '=' signs only appear when there's nothing else around
  693. $params = split('=',$prereqs_string);
  694. if(count($params) == 2){
  695. //right number of operands
  696. if(isset($oLP->items[$oLP->refs_list[$params[0]]])){
  697. $status = $oLP->items[$oLP->refs_list[$params[0]]]->get_status(true);
  698. $returnstatus = ($status == $params[1]);
  699. if(empty($this->prereq_alert) && !$returnstatus){
  700. $this->prereq_alert = get_lang('_prereq_not_complete');
  701. }
  702. return $returnstatus;
  703. }
  704. $this->prereq_alert = get_lang('_prereq_not_complete');
  705. return false;
  706. }
  707. }else{
  708. //No ANDs found, look for <>
  709. if($this->debug>1){error_log('New LP - Didnt find any =, looking for <>',0);}
  710. if(strpos($prereqs_string,"<>")!==false){
  711. if($this->debug>1){error_log('New LP - Found <>, looking into it',0);}
  712. //we assume '<>' signs only appear when there's nothing else around
  713. $params = split('<>',$prereqs_string);
  714. if(count($params) == 2){
  715. //right number of operands
  716. if(isset($oLP->items[$oLP->refs_list[$params[0]]])){
  717. $status = $oLP->items[$oLP->refs_list[$params[0]]]->get_status(true);
  718. $returnstatus = ($status != $params[1]);
  719. if(empty($this->prereq_alert) && !$returnstatus){
  720. $this->prereq_alert = get_lang('_prereq_not_complete');
  721. }
  722. return $returnstatus;
  723. }
  724. $this->prereq_alert = get_lang('_prereq_not_complete');
  725. return false;
  726. }
  727. }else{
  728. //No <> found, look for ~ (unary)
  729. if($this->debug>1){error_log('New LP - Didnt find any =, looking for ~',0);}
  730. //only remains: ~ and X*{}
  731. if(strpos($prereqs_string,"~")!==false){
  732. //found NOT
  733. if($this->debug>1){error_log('New LP - Found ~, looking into it',0);}
  734. $list = array();
  735. $myres = preg_match('/~([^(\d+\*)\{]*)/',$prereqs_string,$list);
  736. if($myres){
  737. $returnstatus = !$this->parse_prereq($list[1]);
  738. if(empty($this->prereq_alert) && !$returnstatus){
  739. $this->prereq_alert = get_lang('_prereq_not_complete');
  740. }
  741. return $returnstatus;
  742. }else{
  743. //strange...
  744. if($this->debug>1){error_log('New LP - Found ~ but strange string: '.$prereqs_string,0);}
  745. }
  746. }else{
  747. //Finally, look for sets/groups
  748. if($this->debug>1){error_log('New LP - Didnt find any ~, looking for groups',0);}
  749. //only groups here
  750. $groups = array();
  751. $groups_there = preg_match_all('/((\d+\*)?\{([^\}]+)\}+)/',$prereqs_string,$groups);
  752. if($groups_there){
  753. foreach($groups[1] as $gr) //only take the results that correspond to the big brackets-enclosed condition
  754. {
  755. if($this->debug>1){error_log('New LP - Dealing with group '.$gr,0);}
  756. $multi = array();
  757. $mycond = false;
  758. if(preg_match('/(\d+)\*\{([^\}]+)\}/',$gr,$multi)){
  759. if($this->debug>1){error_log('New LP - Found multiplier '.$multi[0],0);}
  760. $count = $multi[1];
  761. $list = split(',',$multi[2]);
  762. $mytrue = 0;
  763. foreach($list as $cond){
  764. if(isset($oLP->items[$oLP->refs_list[$cond]])){
  765. $status = $oLP->items[$oLP->refs_list[$cond]]->get_status(true);
  766. if (($status == $this->possible_status[2]) OR ($status == $this->possible_status[3])){
  767. $mytrue ++;
  768. if($this->debug>1){error_log('New LP - Found true item, counting.. ('.($mytrue).')',0);}
  769. }
  770. }else{
  771. if($this->debug>1){error_log('New LP - item '.$cond.' does not exist in items list',0);}
  772. }
  773. }
  774. if($mytrue >= $count){
  775. if($this->debug>1){error_log('New LP - Got enough true results, return true',0);}
  776. $mycond = true;
  777. }else{
  778. if($this->debug>1){error_log('New LP - Not enough true results',0);}
  779. }
  780. }
  781. else{
  782. if($this->debug>1){error_log('New LP - No multiplier',0);}
  783. $list = split(',',$gr);
  784. $mycond = true;
  785. foreach($list as $cond){
  786. if(isset($oLP->items[$oLP->refs_list[$cond]])){
  787. $status = $oLP->items[$oLP->refs_list[$cond]]->get_status(true);
  788. if (($status == $this->possible_status[2]) OR ($status == $this->possible_status[3])){
  789. $mycond = true;
  790. if($this->debug>1){error_log('New LP - Found true item',0);}
  791. }else{
  792. if($this->debug>1){error_log('New LP - Found false item, the set is not true, return false',0);}
  793. $mycond = false;
  794. break;
  795. }
  796. }else{
  797. if($this->debug>1){error_log('New LP - item '.$cond.' does not exist in items list',0);}
  798. if($this->debug>1){error_log('New LP - Found false item, the set is not true, return false',0);}
  799. $mycond = false;
  800. break;
  801. }
  802. }
  803. }
  804. if(!$mycond && empty($this->prereq_alert)){
  805. $this->prereq_alert = get_lang('_prereq_not_complete');
  806. }
  807. return $mycond;
  808. }
  809. }else{
  810. //Nothing found there either. Now return the value of the corresponding resource completion status
  811. if($this->debug>1){error_log('New LP - Didnt find any group, returning value for '.$prereqs_string,0);}
  812. if(isset($oLP->items[$oLP->refs_list[$prereqs_string]])){
  813. $status = $oLP->items[$oLP->refs_list[$prereqs_string]]->get_status(true);
  814. $returnstatus = (($status == $this->possible_status[2]) OR ($status == $this->possible_status[3]));
  815. if(!$returnstatus && empty($this->prereq_alert)){
  816. $this->prereq_alert = get_lang('_prereq_not_complete');
  817. }
  818. if(!$returnstatus){
  819. if($this->debug>1){error_log('New LP - Prerequisite '.$prereqs_string.' not complete',0);}
  820. }else{
  821. if($this->debug>1){error_log('New LP - Prerequisite '.$prereqs_string.' complete',0);}
  822. }
  823. return $returnstatus;
  824. }else{
  825. if($this->debug>1){error_log('New LP - Could not find '.$prereqs_string.' in '.print_r($oLP->refs_list,true),0);}
  826. }
  827. }
  828. }
  829. }
  830. }
  831. }
  832. }else{
  833. $list = split("\|",$prereqs_string);
  834. if(count($list)>1){
  835. if($this->debug>1){error_log('New LP - Found OR, looking into it',0);}
  836. $orstatus = false;
  837. foreach($list as $condition){
  838. if($this->debug>1){error_log('New LP - Found OR, adding it ('.$condition.')',0);}
  839. $orstatus = $orstatus || $this->parse_prereq($condition);
  840. if($orstatus == true){
  841. //shortcircuit OR
  842. if($this->debug>1){error_log('New LP - One condition in OR was true, short-circuit',0);}
  843. break;
  844. }
  845. }
  846. if(!$orstatus && empty($this->prereq_alert)){
  847. $this->prereq_alert = get_lang('_prereq_not_complete');
  848. }
  849. return $orstatus;
  850. }else{
  851. if($this->debug>1){error_log('New LP - OR was found but only one elem present !?',0);}
  852. if(isset($oLP->items[$oLP->refs_list[$list[0]]])){
  853. $status = $oLP->items[$oLP->refs_list[$list[0]]]->get_status(true);
  854. $returnstatus = (($status == 'completed') OR ($status == 'passed'));
  855. if(!$returnstatus && empty($this->prereq_alert)){
  856. $this->prereq_alert = get_lang('_prereq_not_complete');
  857. }
  858. return $returnstatus;
  859. }
  860. }
  861. }
  862. if(empty($this->prereq_alert)){
  863. $this->prereq_alert = get_lang('_prereq_not_complete');
  864. }
  865. if($this->debug>1){error_log('New LP - End of parse_prereq. Error code is now '.$this->prereq_alert,0);}
  866. return false;
  867. }
  868. /**
  869. * Reinits all local values as the learnpath is restarted
  870. * @return boolean True on success, false otherwise
  871. */
  872. function restart()
  873. {
  874. if($this->debug>0){error_log('New LP - In learnpathItem::restart()',0);}
  875. $this->save();
  876. $allowed = $this->is_restart_allowed();
  877. if($allowed === -1){
  878. //nothing allowed, do nothing
  879. }elseif($allowed === 1){
  880. //restart as new attempt is allowed, record a new attempt
  881. $this->attempt_id = $this->attempt_id + 1; //simply reuse the previous attempt_id
  882. $this->current_score = 0;
  883. $this->current_start_time = 0;
  884. $this->current_stop_time = 0;
  885. $this->current_data = '';
  886. $this->status = $this->possible_status[0];
  887. $this->interactions_count = 0;
  888. $this->lesson_location = '';
  889. }else{
  890. //restart current element is allowed (because it's not finished yet),
  891. // reinit current
  892. $this->current_score = 0;
  893. $this->current_start_time = 0;
  894. $this->current_stop_time = 0;
  895. $this->current_data = '';
  896. $this->status = $this->possible_status[0];
  897. }
  898. return true;
  899. }
  900. /**
  901. * Saves data in the database
  902. * @param boolean Save from URL params (1) or from object attributes (0)
  903. * @return boolean True on success, false on failure
  904. */
  905. function save($from_outside=true)
  906. {
  907. if($this->debug>0){error_log('New LP - In learnpathItem::save()',0);}
  908. //$item_view_table = Database::get_course_table(COURSEID,LEARNPATH_ITEM_VIEW_TABLE);
  909. $item_id = $this->get_id();
  910. //first check if parameters passed via GET can be saved here
  911. //in case it's a SCORM, we should get:
  912. if($this->type == 'sco'){
  913. $s = $this->get_status(true);
  914. if($this->prevent_reinit == 1 AND
  915. $s != $this->possible_status[0] AND $s != $this->possible_status[1]){
  916. if($this->debug>1){error_log('New LP - In learnpathItem::save() - save reinit blocked by setting',0);}
  917. //do nothing because the status has already been set. Don't allow it to change.
  918. //TODO check there isn't a special circumstance where this should be saved
  919. }else{
  920. if($this->debug>1){error_log('New LP - In learnpathItem::save() - SCORM save request received',0);}
  921. //get all new settings from the URL
  922. if($from_outside==true){
  923. if($this->debug>1){error_log('New LP - In learnpathItem::save() - Getting item data from outside',0);}
  924. foreach($_GET as $param => $value)
  925. {
  926. $value = mysql_real_escape_string($value);
  927. switch($param){
  928. case 'score':
  929. $this->set_score($value);
  930. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting score to '.$value,0);}
  931. break;
  932. case 'max':
  933. $this->max_score = $value;
  934. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting max_score to '.$value,0);}
  935. break;
  936. case 'min':
  937. $this->min_score = $value;
  938. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting min_score to '.$value,0);}
  939. break;
  940. case 'lesson_status':
  941. if(!empty($value)){
  942. $this->set_status($value);
  943. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting status to '.$value,0);}
  944. }
  945. break;
  946. case 'time':
  947. $this->set_time($value);
  948. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting time to '.$value,0);}
  949. break;
  950. case 'suspend_data':
  951. $this->current_data = $value;
  952. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting suspend_data to '.$value,0);}
  953. break;
  954. case 'lesson_location':
  955. $this->set_lesson_location($value);
  956. if($this->debug>2){error_log('New LP - In learnpathItem::save() - setting lesson_location to '.$value,0);}
  957. break;
  958. case 'interactions':
  959. //$interactions = unserialize($value);
  960. //foreach($interactions as $interaction){
  961. // ;
  962. //}
  963. break;
  964. /*
  965. case 'objectives._count':
  966. $this->attempt_id = $value;
  967. break;
  968. */
  969. default:
  970. //ignore
  971. break;
  972. }
  973. }
  974. }else{
  975. if($this->debug>1){error_log('New LP - In learnpathItem::save() - Using inside item status',0);}
  976. //do nothing, just let the local attributes be used
  977. }
  978. }
  979. }else{ //if not SCO, such messages should not be expected
  980. $type = strtolower($type);
  981. switch($type){
  982. case 'asset':
  983. $this->set_status($this->possible_status[2]);
  984. break;
  985. case 'hotpotatoes':
  986. $tbl_hotpot = Database::get_course_table(QUIZ_TABLE);
  987. $hotpot_sql = "SELECT * FROM $tbl_hotpot WHERE ...";
  988. $hotpot_res = api_sql_query($hotpot_sql,__FILE__,__LINE__);
  989. if(Database::num_rows($hotpot_res)>0){
  990. $hotpot_row = Database::fetch_array($hotpot_res);
  991. $this->min_score = $hotpot_row['min_score'];
  992. $this->max_score = $hotpot_row['max_score'];
  993. $this->set_score($hotpot_row['score']);
  994. //TODO move hardcoded quota value somewhere as variable
  995. if($hotpot_row['score']/$hotpot_row['max_score']>=0.70){
  996. $this->set_status($this->possible_status[3]);
  997. }else{
  998. $this->set_status($this->possible_status[4]);
  999. }
  1000. }else{
  1001. //if not found, set to incompleted as it probably means
  1002. //the user hasn't passed the test
  1003. $this->set_status($this->possible_status[2]);
  1004. }
  1005. break;
  1006. case TOOL_QUIZ:
  1007. $this->set_status($this->possible_status[2]);
  1008. break;
  1009. default:
  1010. //for now, everything that is not sco and not asset is set to
  1011. //completed when saved
  1012. $this->set_status($this->possible_status[2]);
  1013. break;
  1014. }
  1015. }
  1016. //$time = $this->time
  1017. if($this->debug>1){error_log('New LP - End of learnpathItem::save() - Calling write_to_db()',0);}
  1018. return $this->write_to_db();
  1019. }
  1020. /**
  1021. * Sets the number of attempt_id to a given value
  1022. * @param integer The given value to set attempt_id to
  1023. * @return boolean TRUE on success, FALSE otherwise
  1024. */
  1025. function set_attempt_id($num)
  1026. {
  1027. if($this->debug>0){error_log('New LP - In learnpathItem::set_attempt_id()',0);}
  1028. if($num == strval(intval($num)) && $num>=0){
  1029. $this->attempt_id = $num;
  1030. return true;
  1031. }
  1032. return false;
  1033. }
  1034. /**
  1035. * Sets the item's description
  1036. * @param string Description
  1037. */
  1038. function set_description($string=''){
  1039. if($this->debug>0){error_log('New LP - In learnpathItem::set_description()',0);}
  1040. if(!empty($string)){$this->description = $string;}
  1041. }
  1042. /**
  1043. * Sets the lesson_location value
  1044. * @param string lesson_location as provided by the SCO
  1045. * @return boolean True on success, false otherwise
  1046. */
  1047. function set_lesson_location($location)
  1048. {
  1049. if($this->debug>0){error_log('New LP - In learnpathItem::set_lesson_location()',0);}
  1050. if(isset($location)){
  1051. $this->lesson_location = mysql_real_escape_string($location);
  1052. return true;
  1053. }
  1054. return false;
  1055. }
  1056. /**
  1057. * Sets the item's depth level in the LP tree (0 is at root)
  1058. * @param integer Level
  1059. */
  1060. function set_level($int=0){
  1061. if($this->debug>0){error_log('New LP - In learnpathItem::set_level('.$int.')',0);}
  1062. if(!empty($int) AND $int == strval(intval($int))){$this->level = $int;}
  1063. }
  1064. /**
  1065. * Sets the lp_view id this item view is registered to
  1066. * @param integer lp_view DB ID
  1067. * @todo //todo insert into lp_item_view if lp_view not exists
  1068. */
  1069. function set_lp_view($lp_view_id)
  1070. {
  1071. if($this->debug>0){error_log('New LP - In learnpathItem::set_lp_view('.$lp_view_id.')',0);}
  1072. if(!empty($lp_view_id) and $lp_view_id = intval(strval($lp_view_id)))
  1073. {
  1074. $this->view_id = $lp_view_id;
  1075. $item_view_table = Database::get_course_table('lp_item_view');
  1076. //get the lp_item_view with the highest view_count
  1077. $sql = "SELECT * FROM $item_view_table WHERE lp_item_id = ".$this->get_id()." " .
  1078. " AND lp_view_id = ".$lp_view_id." ORDER BY view_count DESC";
  1079. if($this->debug>2){error_log('New LP - In learnpathItem::set_lp_view() - Querying lp_item_view: '.$sql,0);}
  1080. $res = api_sql_query($sql,__FILE__,__LINE__);
  1081. if(Database::num_rows($res)>0){
  1082. $row = Database::fetch_array($res);
  1083. $this->db_item_view_id = $row['id'];
  1084. $this->attempt_id = $row['view_count'];
  1085. $this->current_score = $row['score'];
  1086. $this->current_data = $row['suspend_data'];
  1087. $this->status = $row['status'];
  1088. $this->current_start_time = $row['start_time'];
  1089. $this->current_stop_time = $this->current_start_time + $row['total_time'];
  1090. $this->lesson_location = $row['lesson_location'];
  1091. if($this->debug>2){error_log('New LP - In learnpathItem::set_lp_view() - Updated item object with database values',0);}
  1092. //now get the number of interactions for this little guy
  1093. $item_view_interaction_table = Database::get_course_table('lp_iv_interaction');
  1094. $sql = "SELECT * FROM $item_view_interaction_table WHERE lp_iv_id = '".$this->db_item_view_id."'";
  1095. $res = api_sql_query($sql,__FILE__,__LINE__);
  1096. if($res !== false){
  1097. $this->interactions_count = Database::num_rows($res);
  1098. }else{
  1099. $this->interactions_count = 0;
  1100. }
  1101. }
  1102. }
  1103. //end
  1104. if($this->debug>2){error_log('New LP - End of learnpathItem::set_lp_view()',0);}
  1105. }
  1106. /**
  1107. * Sets the path
  1108. * @param string Path
  1109. */
  1110. function set_path($string=''){
  1111. if($this->debug>0){error_log('New LP - In learnpathItem::set_path()',0);}
  1112. if(!empty($string)){$this->path = $string;}
  1113. }
  1114. /**
  1115. * Sets the prevent_reinit attribute. This is based on the LP value and is set at creation time for
  1116. * each learnpathItem. It is a (bad?) way of avoiding a reference to the LP when saving an item.
  1117. * @param integer 1 for "prevent", 0 for "don't prevent" saving freshened values (new "not attempted" status etc)
  1118. */
  1119. function set_prevent_reinit($prevent){
  1120. if($this->debug>0){error_log('New LP - In learnpathItem::set_prevent_reinit()',0);}
  1121. if($prevent){
  1122. $this->prevent_reinit = 1;
  1123. }else{
  1124. $this->prevent_reinit = 0;
  1125. }
  1126. }
  1127. /**
  1128. * Sets the score value
  1129. * @param float Score
  1130. * @return boolean True on success, false otherwise
  1131. */
  1132. function set_score($score)
  1133. {
  1134. if($this->debug>0){error_log('New LP - In learnpathItem::set_score()',0);}
  1135. if(($score <= $this->max_score) && ($score >= $this->min_score))
  1136. {
  1137. $this->current_score = $score;
  1138. return true;
  1139. }
  1140. return false;
  1141. }
  1142. /**
  1143. * Sets the status for this item
  1144. * @param string Status - must be one of the values defined in $this->possible_status
  1145. * @return boolean True on success, false on error
  1146. */
  1147. function set_status($status)
  1148. {
  1149. if($this->debug>0){error_log('New LP - In learnpathItem::set_status('.$status.')',0);}
  1150. $found = false;
  1151. foreach($this->possible_status as $possible){
  1152. if(preg_match('/^'.$possible.'$/i',$status)){
  1153. $found = true;
  1154. }
  1155. }
  1156. //if(in_array($status,$this->possible_status))
  1157. if($found)
  1158. {
  1159. $this->status = mysql_real_escape_string($status);
  1160. if($this->debug>1){error_log('New LP - In learnpathItem::set_status() - Updated object status of item '.$this->db_id.' to '.$this->status,0);}
  1161. return true;
  1162. }
  1163. //error_log('New LP - '.$status.' was not in the possible status',0);
  1164. $this->status = $this->possible_status[0];
  1165. return false;
  1166. }
  1167. /**
  1168. * Sets the item viewing time in a usable form, given that SCORM packages often give it as 00:00:00.0000
  1169. * @param string Time as given by SCORM
  1170. */
  1171. function set_time($scorm_time)
  1172. {
  1173. if($this->debug>0){error_log('New LP - In learnpathItem::set_time('.$scorm_time.')',0);}
  1174. if($scorm_time == 0 and ($this->type=='asset' or $this->type=='doc') and $this->current_start_time!=0){
  1175. $my_time = time() - $this->current_start_time;
  1176. if($my_time > 0){
  1177. $this->update_time($my_time);
  1178. if($this->debug>-1){error_log('New LP - In learnpathItem::set_time('.$scorm_time.') - found asset - set time to '.$my_time,0);}
  1179. }
  1180. }else{
  1181. $res = array();
  1182. if(preg_match('/^(\d{1,4}):(\d{2}):(\d{2})(\.\d{1,4})?/',$scorm_time,$res)){
  1183. $time = time();
  1184. $hour = $res[1];
  1185. $min = $res[2];
  1186. $sec = $res[3];
  1187. //getting total number of seconds spent
  1188. $total_sec = $hour*3600 + $min*60 + $sec;
  1189. $this->update_time($total_sec);
  1190. }
  1191. }
  1192. }
  1193. /**
  1194. * Sets the item's title
  1195. * @param string Title
  1196. */
  1197. function set_title($string=''){
  1198. if($this->debug>0){error_log('New LP - In learnpathItem::set_title()',0);}
  1199. if(!empty($string)){$this->title = $string;}
  1200. }
  1201. /**
  1202. * Sets the item's type
  1203. * @param string Type
  1204. */
  1205. function set_type($string=''){
  1206. if($this->debug>0){error_log('New LP - In learnpathItem::set_type()',0);}
  1207. if(!empty($string)){$this->type = $string;}
  1208. }
  1209. /**
  1210. * Checks if the current status is part of the list of status given
  1211. * @param strings_array An array of status to check for. If the current status is one of the strings, return true
  1212. * @return boolean True if the status was one of the given strings, false otherwise
  1213. */
  1214. function status_is($list=array())
  1215. {
  1216. if($this->debug>1){error_log('New LP - In learnpathItem::status_is('.print_r($list,true).')',0);}
  1217. if(empty($this->status)){
  1218. return false;
  1219. }
  1220. $found = false;
  1221. foreach($list as $status)
  1222. {
  1223. if(preg_match('/^'.$status.'$/i',$this->status))
  1224. {
  1225. if($this->debug>2){error_log('New LP - learnpathItem::status_is() - Found status '.$status.' corresponding to current status',0);}
  1226. $found = true;
  1227. return $found;
  1228. }
  1229. }
  1230. if($this->debug>2){error_log('New LP - learnpathItem::status_is() - Status '.$this->status.' did not match request',0);}
  1231. return $found;
  1232. }
  1233. /**
  1234. * Updates the time info according to the given session_time
  1235. * @param integer Time in seconds
  1236. * //TODO @TODO make this method better by allowing better/multiple time slices
  1237. */
  1238. function update_time($total_sec=0){
  1239. if($this->debug>0){error_log('New LP - In learnpathItem::update_time('.$total_sec.')',0);}
  1240. if($total_sec>=0){
  1241. //getting start time from finish time. The only problem in the calculation is it might be
  1242. //modified by the scripts processing time
  1243. $now = time();
  1244. $start = $now-$total_sec;
  1245. $this->current_start_time = $start;
  1246. $this->current_stop_time = $now;
  1247. /*if(empty($this->current_start_time)){
  1248. $this->current_start_time = $start;
  1249. $this->current_stop_time = $now;
  1250. }else{
  1251. //if($this->current_stop_time != $this->current_start_time){
  1252. //if the stop time has already been set before to something else
  1253. //than the start time, add the given time to what's already been
  1254. //recorder.
  1255. //This is the SCORM way of doing things, because the time comes from
  1256. //core.session_time, not core.total_time
  1257. // UPDATE: adding time to previous time is only done on SCORM's finish()
  1258. // call, not normally, so for now ignore this section
  1259. // $this->current_stop_time = $this->current_stop_time + $stop;
  1260. // error_log('New LP - Adding '.$stop.' seconds - now '.$this->current_stop_time,0);
  1261. //}else{
  1262. //if no previous stop time set, use the one just calculated now from
  1263. //start time
  1264. //$this->current_start_time = $start;
  1265. //$this->current_stop_time = $now;
  1266. //error_log('New LP - Setting '.$stop.' seconds - now '.$this->current_stop_time,0);
  1267. //}
  1268. }*/
  1269. }
  1270. }
  1271. /**
  1272. * Writes the current data to the database
  1273. * @return boolean Query result
  1274. */
  1275. function write_to_db()
  1276. {
  1277. if($this->debug>0){error_log('New LP - In learnpathItem::write_to_db()',0);}
  1278. $mode = $this->get_lesson_mode();
  1279. $credit = $this->get_credit();
  1280. if($credit == 'no-credit' OR $mode == 'review' OR $mode == 'browse')
  1281. {
  1282. //this info shouldn't be saved as the credit or lesson mode info prevent it
  1283. if($this->debug>1){error_log('New LP - In learnpathItem::write_to_db() - credit('.$credit.') or lesson_mode('.$mode.') prevent recording!',0);}
  1284. }else{
  1285. //check the row exists
  1286. $item_view_table = Database::get_course_table('lp_item_view');
  1287. $check = "SELECT * FROM $item_view_table " .
  1288. "WHERE lp_item_id = ".$this->db_id. " " .
  1289. "AND lp_view_id = ".$this->view_id. " ".
  1290. "AND view_count = ".$this->get_attempt_id();
  1291. if($this->debug>2){error_log('New LP - In learnpathItem::write_to_db() - Querying item_view: '.$check,0);}
  1292. $check_res = api_sql_query($check);
  1293. //depending on what we want (really), we'll update or insert a new row
  1294. //now save into DB
  1295. if(Database::num_rows($check_res)<1){
  1296. $sql = "INSERT INTO $item_view_table " .
  1297. "(total_time, " .
  1298. "start_time, " .
  1299. "score, " .
  1300. "status, " .
  1301. "lp_item_id, " .
  1302. "lp_view_id, " .
  1303. "view_count, " .
  1304. "suspend_data, " .
  1305. "lesson_location)" .
  1306. "VALUES" .
  1307. "(".$this->get_total_time()."," .
  1308. "".$this->current_start_time."," .
  1309. "".$this->get_score()."," .
  1310. "'".$this->get_status(false)."'," .
  1311. "".$this->db_id."," .
  1312. "".$this->view_id."," .
  1313. "".$this->get_attempt_id()."," .
  1314. "'".$this->current_data."'," .
  1315. "'".$this->lesson_location."')";
  1316. if($this->debug>2){error_log('New LP - In learnpathItem::write_to_db() - Inserting into item_view: '.$sql,0);}
  1317. }else{
  1318. $sql = "UPDATE $item_view_table " .
  1319. "SET total_time = ".$this->get_total_time().", " .
  1320. " start_time = ".$this->get_current_start_time().", " .
  1321. " score = ".$this->get_score().", " .
  1322. " status = '".$this->get_status(false)."'," .
  1323. " suspend_data = '".$this->current_data."'," .
  1324. " lesson_location = '".$this->lesson_location."' " .
  1325. "WHERE lp_item_id = ".$this->db_id." " .
  1326. "AND lp_view_id = ".$this->view_id." " .
  1327. "AND view_count = ".$this->attempt_id;
  1328. if($this->debug>2){error_log('New LP - In learnpathItem::write_to_db() - Updating item_view: '.$sql,0);}
  1329. }
  1330. $res = api_sql_query($sql,__FILE__,__LINE__);
  1331. if(!$res)
  1332. {
  1333. $this->error = 'Could not update item_view table...'.mysql_error();
  1334. }
  1335. if(is_array($this->interactions) && count($this->interactions)>0){
  1336. //save interactions
  1337. $tbl = Database::get_course_table('lp_item_view');
  1338. $sql = "SELECT id FROM $tbl " .
  1339. "WHERE lp_item_id = ".$this->db_id." " .
  1340. "AND lp_view_id = ".$this->view_id." " .
  1341. "AND view_count = ".$this->attempt_id;
  1342. $res = api_sql_query($sql,__FILE__,__LINE__);
  1343. if(Database::num_rows($res)>0){
  1344. $row = Database::fetch_array($res);
  1345. $lp_iv_id = $row[0];
  1346. if($this->debug>2){error_log('New LP - In learnpathItem::write_to_db() - Got item_view_id '.$lp_iv_id.', now checking interactions ',0);}
  1347. foreach($this->interactions as $index => $interaction){
  1348. $correct_resp = '';
  1349. if(is_array($interaction[4]) && !empty($interactions[4][0]) ){
  1350. foreach($interactions[4] as $resp){
  1351. $correct_resp .= $resp.',';
  1352. }
  1353. $correct_resp = substr($correct_resp,0,strlen($correct_resp)-1);
  1354. }
  1355. $iva_table = Database::get_course_table('lp_iv_interaction');
  1356. $iva_sql = "SELECT id FROM $iva_table " .
  1357. "WHERE lp_iv_id = $lp_iv_id " .
  1358. "AND order_id = $index";
  1359. $iva_res = api_sql_query($iva_sql,__FILE__,__LINE__);
  1360. //id(0), type(1), time(2), weighting(3),correct_responses(4),student_response(5),result(6),latency(7)
  1361. if(Database::num_rows($iva_res)>0){
  1362. //update (or don't)
  1363. $iva_row = Database::fetch_array($iva_res);
  1364. $iva_id = $iva_row[0];
  1365. $ivau_sql = "UPDATE $iva_table " .
  1366. "SET interaction_id = '".$interaction[0]."'," .
  1367. "interaction_type = '".$interaction[1]."'," .
  1368. "weighting = '".$interaction[3]."'," .
  1369. "completion_time = '".$interaction[2]."'," .
  1370. "correct_responses = '".$correct_resp."'," .
  1371. "student_response = '".$interaction[5]."'," .
  1372. "result = '".$interaction[6]."'," .
  1373. "latency = '".$interaction[7]."'" .
  1374. "WHERE id = $iva_id";
  1375. $ivau_res = api_sql_query($ivau_sql,__FILE__,__LINE__);
  1376. }else{
  1377. //insert new one
  1378. $ivai_sql = "INSERT INTO $iva_table " .
  1379. "(order_id, lp_iv_id, interaction_id, interaction_type, " .
  1380. "weighting, completion_time, correct_responses, " .
  1381. "student_response, result, latency)" .
  1382. "VALUES" .
  1383. "(".$index.",".$lp_iv_id.",'".$interaction[0]."','".$interaction[1]."'," .
  1384. "'".$interaction[3]."','".$interaction[2]."','".$correct_resp."'," .
  1385. "'".$interaction[5]."','".$interaction[6]."','".$interaction[7]."'" .
  1386. ")";
  1387. $ivai_res = api_sql_query($ivai_sql,__FILE__,__LINE__);
  1388. }
  1389. }
  1390. }
  1391. }
  1392. }
  1393. if($this->debug>2){error_log('New LP - End of learnpathItem::write_to_db()',0);}
  1394. return true;
  1395. }
  1396. }
  1397. ?>