learnpathItem.class.php 99 KB

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