learnpathItem.class.php 100 KB

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