learnpathItem.class.php 98 KB

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