learnpathItem.class.php 110 KB

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