learnpathItem.class.php 102 KB

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