learnpathItem.class.php 96 KB

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