learnpathItem.class.php 104 KB

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