learnpathItem.class.php 109 KB

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