learnpathItem.class.php 151 KB

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