learnpathItem.class.php 189 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class learnpathItem
  5. * lp_item defines items belonging to a learnpath. Each item has a name,
  6. * a score, a use time and additional information that enables tracking a user's
  7. * progress in a learning path.
  8. *
  9. * @package chamilo.learnpath
  10. *
  11. * @author Yannick Warnier <ywarnier@beeznest.org>
  12. */
  13. class learnpathItem
  14. {
  15. const DEBUG = 0; // Logging parameter.
  16. public $iId;
  17. public $attempt_id; // Also called "objectives" SCORM-wise.
  18. public $audio; // The path to an audio file (stored in document/audio/).
  19. public $children = []; // Contains the ids of children items.
  20. public $condition; // If this item has a special condition embedded.
  21. public $current_score;
  22. public $current_start_time;
  23. public $current_stop_time;
  24. public $current_data = '';
  25. public $db_id;
  26. public $db_item_view_id = '';
  27. public $description = '';
  28. public $file;
  29. /**
  30. * At the moment, interactions are just an array of arrays with a structure
  31. * of 8 text fields: id(0), type(1), time(2), weighting(3),
  32. * correct_responses(4), student_response(5), result(6), latency(7).
  33. */
  34. public $interactions = [];
  35. public $interactions_count = 0;
  36. public $objectives = [];
  37. public $objectives_count = 0;
  38. public $launch_data = '';
  39. public $lesson_location = '';
  40. public $level = 0;
  41. public $core_exit = '';
  42. //var $location; // Only set this for SCORM?
  43. public $lp_id;
  44. public $max_score;
  45. public $mastery_score;
  46. public $min_score;
  47. public $max_time_allowed = '';
  48. public $name;
  49. public $next;
  50. public $parent;
  51. public $path; // In some cases the exo_id = exercise_id in courseDb exercices table.
  52. public $possible_status = [
  53. 'not attempted',
  54. 'incomplete',
  55. 'completed',
  56. 'passed',
  57. 'failed',
  58. 'browsed',
  59. ];
  60. public $prereq_string = '';
  61. public $prereq_alert = '';
  62. public $prereqs = [];
  63. public $previous;
  64. public $prevent_reinit = 1; // 0 = multiple attempts 1 = one attempt
  65. public $seriousgame_mode;
  66. public $ref;
  67. public $save_on_close = true;
  68. public $search_did = null;
  69. public $status;
  70. public $title;
  71. /**
  72. * Type attribute can contain one of
  73. * link|student_publication|dir|quiz|document|forum|thread.
  74. */
  75. public $type;
  76. public $view_id;
  77. public $oldTotalTime;
  78. //var used if absolute session time mode is used
  79. private $last_scorm_session_time = 0;
  80. private $prerequisiteMaxScore;
  81. private $prerequisiteMinScore;
  82. /**
  83. * Prepares the learning path item for later launch.
  84. * Don't forget to use set_lp_view() if applicable after creating the item.
  85. * Setting an lp_view will finalise the item_view data collection.
  86. *
  87. * @param int $id Learning path item ID
  88. * @param int $user_id User ID
  89. * @param int $course_id Course int id
  90. * @param null|array $item_content An array with the contents of the item
  91. */
  92. public function __construct(
  93. $id,
  94. $user_id = 0,
  95. $course_id = 0,
  96. $item_content = null
  97. ) {
  98. $items_table = Database::get_course_table(TABLE_LP_ITEM);
  99. // Get items table.
  100. if (!isset($user_id)) {
  101. $user_id = api_get_user_id();
  102. }
  103. if (self::DEBUG > 0) {
  104. error_log("learnpathItem constructor: id: $id user_id: $user_id course_id: $course_id");
  105. error_log("item_content: ".print_r($item_content, 1));
  106. }
  107. $id = (int) $id;
  108. if (empty($item_content)) {
  109. if (empty($course_id)) {
  110. $course_id = api_get_course_int_id();
  111. } else {
  112. $course_id = (int) $course_id;
  113. }
  114. $sql = "SELECT * FROM $items_table
  115. WHERE iid = $id";
  116. $res = Database::query($sql);
  117. if (Database::num_rows($res) < 1) {
  118. $this->error = 'Could not find given learnpath item in learnpath_item table';
  119. }
  120. $row = Database::fetch_array($res);
  121. } else {
  122. $row = $item_content;
  123. }
  124. $this->lp_id = $row['lp_id'];
  125. $this->iId = $row['iid'];
  126. $this->max_score = $row['max_score'];
  127. $this->min_score = $row['min_score'];
  128. $this->name = $row['title'];
  129. $this->type = $row['item_type'];
  130. $this->ref = $row['ref'];
  131. $this->title = $row['title'];
  132. $this->description = $row['description'];
  133. $this->path = $row['path'];
  134. $this->mastery_score = $row['mastery_score'];
  135. $this->parent = $row['parent_item_id'];
  136. $this->next = $row['next_item_id'];
  137. $this->previous = $row['previous_item_id'];
  138. $this->display_order = $row['display_order'];
  139. $this->prereq_string = $row['prerequisite'];
  140. $this->max_time_allowed = $row['max_time_allowed'];
  141. $this->setPrerequisiteMaxScore($row['prerequisite_max_score']);
  142. $this->setPrerequisiteMinScore($row['prerequisite_min_score']);
  143. $this->oldTotalTime = 0;
  144. if (isset($row['launch_data'])) {
  145. $this->launch_data = $row['launch_data'];
  146. }
  147. $this->save_on_close = true;
  148. $this->db_id = $id;
  149. // Load children list
  150. if (!empty($this->lp_id)) {
  151. $sql = "SELECT iid FROM $items_table
  152. WHERE
  153. c_id = $course_id AND
  154. lp_id = ".$this->lp_id." AND
  155. parent_item_id = $id";
  156. $res = Database::query($sql);
  157. if (Database::num_rows($res) > 0) {
  158. while ($row = Database::fetch_assoc($res)) {
  159. $this->children[] = $row['iid'];
  160. }
  161. }
  162. // Get search_did.
  163. if (api_get_setting('search_enabled') == 'true') {
  164. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  165. $sql = 'SELECT *
  166. FROM %s
  167. WHERE
  168. course_code=\'%s\' AND
  169. tool_id=\'%s\' AND
  170. ref_id_high_level=%s AND
  171. ref_id_second_level=%d
  172. LIMIT 1';
  173. // TODO: Verify if it's possible to assume the actual course instead
  174. // of getting it from db.
  175. $sql = sprintf(
  176. $sql,
  177. $tbl_se_ref,
  178. api_get_course_id(),
  179. TOOL_LEARNPATH,
  180. $this->lp_id,
  181. $id
  182. );
  183. $res = Database::query($sql);
  184. if (Database::num_rows($res) > 0) {
  185. $se_ref = Database::fetch_array($res);
  186. $this->search_did = (int) $se_ref['search_did'];
  187. }
  188. }
  189. }
  190. $this->seriousgame_mode = 0;
  191. $this->audio = $row['audio'];
  192. if (self::DEBUG > 0) {
  193. error_log(
  194. 'New LP - End of learnpathItem constructor for item '.$id,
  195. 0
  196. );
  197. }
  198. }
  199. /**
  200. * Adds a child to the current item.
  201. *
  202. * @param int $item The child item ID
  203. */
  204. public function add_child($item)
  205. {
  206. if (self::DEBUG > 0) {
  207. error_log('learnpathItem::add_child()', 0);
  208. }
  209. if (!empty($item)) {
  210. // Do not check in DB as we expect the call to come from the
  211. // learnpath class which should be aware of any fake.
  212. $this->children[] = $item;
  213. }
  214. }
  215. /**
  216. * Adds an interaction to the current item.
  217. *
  218. * @param int $index Index (order ID) of the interaction inside this item
  219. * @param array $params Array of parameters:
  220. * id(0), type(1), time(2), weighting(3), correct_responses(4),
  221. * student_response(5), result(6), latency(7)
  222. */
  223. public function add_interaction($index, $params)
  224. {
  225. $this->interactions[$index] = $params;
  226. // Take the current maximum index to generate the interactions_count.
  227. if (($index + 1) > $this->interactions_count) {
  228. $this->interactions_count = $index + 1;
  229. }
  230. }
  231. /**
  232. * Adds an objective to the current item.
  233. *
  234. * @param array Array of parameters:
  235. * id(0), status(1), score_raw(2), score_max(3), score_min(4)
  236. */
  237. public function add_objective($index, $params)
  238. {
  239. if (empty($params[0])) {
  240. return null;
  241. }
  242. $this->objectives[$index] = $params;
  243. // Take the current maximum index to generate the objectives_count.
  244. if ((count($this->objectives) + 1) > $this->objectives_count) {
  245. $this->objectives_count = (count($this->objectives) + 1);
  246. }
  247. }
  248. /**
  249. * Closes/stops the item viewing. Finalises runtime values.
  250. * If required, save to DB.
  251. *
  252. * @return bool True on success, false otherwise
  253. */
  254. public function close()
  255. {
  256. if (self::DEBUG > 0) {
  257. error_log('learnpathItem::close()', 0);
  258. }
  259. $this->current_stop_time = time();
  260. $type = $this->get_type();
  261. if ($type != 'sco') {
  262. if ($type == TOOL_QUIZ || $type == TOOL_HOTPOTATOES) {
  263. $this->get_status(
  264. true,
  265. true
  266. ); // Update status (second option forces the update).
  267. } else {
  268. $this->status = $this->possible_status[2];
  269. }
  270. }
  271. if ($this->save_on_close) {
  272. $this->save();
  273. }
  274. return true;
  275. }
  276. /**
  277. * Deletes all traces of this item in the database.
  278. *
  279. * @return bool true. Doesn't check for errors yet.
  280. */
  281. public function delete()
  282. {
  283. if (self::DEBUG > 0) {
  284. error_log('learnpath_item::delete() for item '.$this->db_id, 0);
  285. }
  286. $lp_item_view = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  287. $lp_item = Database::get_course_table(TABLE_LP_ITEM);
  288. $course_id = api_get_course_int_id();
  289. $sql = "DELETE FROM $lp_item_view
  290. WHERE c_id = $course_id AND lp_item_id = ".$this->db_id;
  291. if (self::DEBUG > 0) {
  292. error_log('Deleting from lp_item_view: '.$sql, 0);
  293. }
  294. Database::query($sql);
  295. $sql = "SELECT * FROM $lp_item
  296. WHERE iid = ".$this->db_id;
  297. $res_sel = Database::query($sql);
  298. if (Database::num_rows($res_sel) < 1) {
  299. return false;
  300. }
  301. $sql = "DELETE FROM $lp_item
  302. WHERE iid = ".$this->db_id;
  303. Database::query($sql);
  304. if (self::DEBUG > 0) {
  305. error_log('Deleting from lp_item: '.$sql);
  306. }
  307. if (api_get_setting('search_enabled') == 'true') {
  308. if (!is_null($this->search_did)) {
  309. $di = new ChamiloIndexer();
  310. $di->remove_document($this->search_did);
  311. }
  312. }
  313. return true;
  314. }
  315. /**
  316. * Drops a child from the children array.
  317. *
  318. * @param string $item index of child item to drop
  319. */
  320. public function drop_child($item)
  321. {
  322. if (self::DEBUG > 0) {
  323. error_log('learnpathItem::drop_child()', 0);
  324. }
  325. if (!empty($item)) {
  326. foreach ($this->children as $index => $child) {
  327. if ($child == $item) {
  328. $this->children[$index] = null;
  329. }
  330. }
  331. }
  332. }
  333. /**
  334. * Gets the current attempt_id for this user on this item.
  335. *
  336. * @return int attempt_id for this item view by this user or 1 if none defined
  337. */
  338. public function get_attempt_id()
  339. {
  340. if (self::DEBUG > 0) {
  341. error_log(
  342. 'learnpathItem::get_attempt_id() on item '.$this->db_id,
  343. 0
  344. );
  345. }
  346. $res = 1;
  347. if (!empty($this->attempt_id)) {
  348. $res = (int) $this->attempt_id;
  349. }
  350. if (self::DEBUG > 0) {
  351. error_log(
  352. 'New LP - End of learnpathItem::get_attempt_id() on item '.
  353. $this->db_id.' - Returning '.$res,
  354. 0
  355. );
  356. }
  357. return $res;
  358. }
  359. /**
  360. * Gets a list of the item's children.
  361. *
  362. * @return array Array of children items IDs
  363. */
  364. public function get_children()
  365. {
  366. if (self::DEBUG > 0) {
  367. error_log('learnpathItem::get_children()', 0);
  368. }
  369. $list = [];
  370. foreach ($this->children as $child) {
  371. if (!empty($child)) {
  372. $list[] = $child;
  373. }
  374. }
  375. return $list;
  376. }
  377. /**
  378. * Gets the core_exit value from the database.
  379. */
  380. public function get_core_exit()
  381. {
  382. return $this->core_exit;
  383. }
  384. /**
  385. * Gets the credit information (rather scorm-stuff) based on current status
  386. * and reinit autorization. Credit tells the sco(content) if Chamilo will
  387. * record the data it is sent (credit) or not (no-credit).
  388. *
  389. * @return string 'credit' or 'no-credit'. Defaults to 'credit'
  390. * Because if we don't know enough about this item, it's probably because
  391. * it was never used before.
  392. */
  393. public function get_credit()
  394. {
  395. if (self::DEBUG > 1) {
  396. error_log('learnpathItem::get_credit()', 0);
  397. }
  398. $credit = 'credit';
  399. // Now check the value of prevent_reinit (if it's 0, return credit as
  400. // the default was).
  401. // If prevent_reinit == 1 (or more).
  402. if ($this->get_prevent_reinit() != 0) {
  403. // If status is not attempted or incomplete, credit anyway.
  404. // Otherwise:
  405. // Check the status in the database rather than in the object, as
  406. // checking in the object would always return "no-credit" when we
  407. // want to set it to completed.
  408. $status = $this->get_status(true);
  409. if (self::DEBUG > 2) {
  410. error_log(
  411. 'learnpathItem::get_credit() - get_prevent_reinit!=0 and '.
  412. 'status is '.$status,
  413. 0
  414. );
  415. }
  416. //0=not attempted - 1 = incomplete
  417. if ($status != $this->possible_status[0] &&
  418. $status != $this->possible_status[1]
  419. ) {
  420. $credit = 'no-credit';
  421. }
  422. }
  423. if (self::DEBUG > 1) {
  424. error_log("learnpathItem::get_credit() returns: $credit");
  425. }
  426. return $credit;
  427. }
  428. /**
  429. * Gets the current start time property.
  430. *
  431. * @return int Current start time, or current time if none
  432. */
  433. public function get_current_start_time()
  434. {
  435. if (self::DEBUG > 0) {
  436. error_log('learnpathItem::get_current_start_time()', 0);
  437. }
  438. if (empty($this->current_start_time)) {
  439. return time();
  440. } else {
  441. return $this->current_start_time;
  442. }
  443. }
  444. /**
  445. * Gets the item's description.
  446. *
  447. * @return string Description
  448. */
  449. public function get_description()
  450. {
  451. if (self::DEBUG > 0) {
  452. error_log('learnpathItem::get_description()', 0);
  453. }
  454. if (empty($this->description)) {
  455. return '';
  456. }
  457. return $this->description;
  458. }
  459. /**
  460. * Gets the file path from the course's root directory, no matter what
  461. * tool it is from.
  462. *
  463. * @param string $path_to_scorm_dir
  464. *
  465. * @return string The file path, or an empty string if there is no file
  466. * attached, or '-1' if the file must be replaced by an error page
  467. */
  468. public function get_file_path($path_to_scorm_dir = '')
  469. {
  470. $course_id = api_get_course_int_id();
  471. if (self::DEBUG > 0) {
  472. error_log('learnpathItem::get_file_path()', 0);
  473. }
  474. $path = $this->get_path();
  475. $type = $this->get_type();
  476. if (empty($path)) {
  477. if ($type == 'dir') {
  478. return '';
  479. } else {
  480. return '-1';
  481. }
  482. } elseif ($path == strval(intval($path))) {
  483. // The path is numeric, so it is a reference to a Chamilo object.
  484. switch ($type) {
  485. case 'dir':
  486. return '';
  487. case TOOL_DOCUMENT:
  488. $table_doc = Database::get_course_table(TABLE_DOCUMENT);
  489. $sql = 'SELECT path
  490. FROM '.$table_doc.'
  491. WHERE
  492. c_id = '.$course_id.' AND
  493. iid = '.$path;
  494. $res = Database::query($sql);
  495. $row = Database::fetch_array($res);
  496. $real_path = 'document'.$row['path'];
  497. return $real_path;
  498. case TOOL_STUDENTPUBLICATION:
  499. case TOOL_QUIZ:
  500. case TOOL_FORUM:
  501. case TOOL_THREAD:
  502. case TOOL_LINK:
  503. default:
  504. return '-1';
  505. }
  506. } else {
  507. if (!empty($path_to_scorm_dir)) {
  508. $path = $path_to_scorm_dir.$path;
  509. }
  510. return $path;
  511. }
  512. }
  513. /**
  514. * Gets the DB ID.
  515. *
  516. * @return int Database ID for the current item
  517. */
  518. public function get_id()
  519. {
  520. if (self::DEBUG > 1) {
  521. error_log('learnpathItem::get_id()', 0);
  522. }
  523. if (!empty($this->db_id)) {
  524. return $this->db_id;
  525. }
  526. // TODO: Check this return value is valid for children classes (SCORM?).
  527. return 0;
  528. }
  529. /**
  530. * Loads the interactions into the item object, from the database.
  531. * If object interactions exist, they will be overwritten by this function,
  532. * using the database elements only.
  533. */
  534. public function load_interactions()
  535. {
  536. $this->interactions = [];
  537. $course_id = api_get_course_int_id();
  538. $tbl = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  539. $sql = "SELECT id FROM $tbl
  540. WHERE
  541. c_id = $course_id AND
  542. lp_item_id = ".$this->db_id." AND
  543. lp_view_id = ".$this->view_id." AND
  544. view_count = ".$this->attempt_id;
  545. $res = Database::query($sql);
  546. if (Database::num_rows($res) > 0) {
  547. $row = Database::fetch_array($res);
  548. $lp_iv_id = $row[0];
  549. $iva_table = Database::get_course_table(TABLE_LP_IV_INTERACTION);
  550. $sql = "SELECT * FROM $iva_table
  551. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id ";
  552. $res_sql = Database::query($sql);
  553. while ($row = Database::fetch_array($res_sql)) {
  554. $this->interactions[$row['interaction_id']] = [
  555. $row['interaction_id'],
  556. $row['interaction_type'],
  557. $row['weighting'],
  558. $row['completion_time'],
  559. $row['correct_responses'],
  560. $row['student_responses'],
  561. $row['result'],
  562. $row['latency'],
  563. ];
  564. }
  565. }
  566. }
  567. /**
  568. * Gets the current count of interactions recorded in the database.
  569. *
  570. * @param bool $checkdb Whether to count from database or not (defaults to no)
  571. *
  572. * @return int The current number of interactions recorder
  573. */
  574. public function get_interactions_count($checkdb = false)
  575. {
  576. if (self::DEBUG > 1) {
  577. error_log('learnpathItem::get_interactions_count()', 0);
  578. }
  579. $return = 0;
  580. $course_id = api_get_course_int_id();
  581. if ($checkdb) {
  582. $tbl = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  583. $sql = "SELECT iid FROM $tbl
  584. WHERE
  585. c_id = $course_id AND
  586. lp_item_id = ".$this->db_id." AND
  587. lp_view_id = ".$this->view_id." AND
  588. view_count = ".$this->get_attempt_id();
  589. $res = Database::query($sql);
  590. if (Database::num_rows($res) > 0) {
  591. $row = Database::fetch_array($res);
  592. $lp_iv_id = $row[0];
  593. $iva_table = Database::get_course_table(
  594. TABLE_LP_IV_INTERACTION
  595. );
  596. $sql = "SELECT count(id) as mycount
  597. FROM $iva_table
  598. WHERE c_id = $course_id AND lp_iv_id = $lp_iv_id ";
  599. $res_sql = Database::query($sql);
  600. if (Database::num_rows($res_sql) > 0) {
  601. $row = Database::fetch_array($res_sql);
  602. $return = $row['mycount'];
  603. }
  604. }
  605. } else {
  606. if (!empty($this->interactions_count)) {
  607. $return = $this->interactions_count;
  608. }
  609. }
  610. return $return;
  611. }
  612. /**
  613. * Gets the JavaScript array content to fill the interactions array.
  614. *
  615. * @param bool $checkdb Whether to check directly into the database (default no)
  616. *
  617. * @return string An empty string if no interaction, a JS array definition otherwise
  618. */
  619. public function get_interactions_js_array($checkdb = false)
  620. {
  621. $return = '';
  622. if ($checkdb) {
  623. $this->load_interactions(true);
  624. }
  625. foreach ($this->interactions as $id => $in) {
  626. $return .= "[
  627. '$id',
  628. '".$in[1]."',
  629. '".$in[2]."',
  630. '".$in[3]."',
  631. '".$in[4]."',
  632. '".$in[5]."',
  633. '".$in[6]."',
  634. '".$in[7]."'],";
  635. }
  636. if (!empty($return)) {
  637. $return = substr($return, 0, -1);
  638. }
  639. return $return;
  640. }
  641. /**
  642. * Gets the current count of objectives recorded in the database.
  643. *
  644. * @return int The current number of objectives recorder
  645. */
  646. public function get_objectives_count()
  647. {
  648. if (self::DEBUG > 1) {
  649. error_log('learnpathItem::get_objectives_count()', 0);
  650. }
  651. $res = 0;
  652. if (!empty($this->objectives_count)) {
  653. $res = $this->objectives_count;
  654. }
  655. return $res;
  656. }
  657. /**
  658. * Gets the launch_data field found in imsmanifests (this is SCORM- or
  659. * AICC-related, really).
  660. *
  661. * @return string Launch data as found in imsmanifest and stored in
  662. * Chamilo (read only). Defaults to ''.
  663. */
  664. public function get_launch_data()
  665. {
  666. if (self::DEBUG > 0) {
  667. error_log('learnpathItem::get_launch_data()', 0);
  668. }
  669. if (!empty($this->launch_data)) {
  670. return str_replace(
  671. ["\r", "\n", "'"],
  672. ['\r', '\n', "\\'"],
  673. $this->launch_data
  674. );
  675. }
  676. return '';
  677. }
  678. /**
  679. * Gets the lesson location.
  680. *
  681. * @return string lesson location as recorded by the SCORM and AICC
  682. * elements. Defaults to ''
  683. */
  684. public function get_lesson_location()
  685. {
  686. if (self::DEBUG > 0) {
  687. error_log('learnpathItem::get_lesson_location()', 0);
  688. }
  689. if (!empty($this->lesson_location)) {
  690. return str_replace(
  691. ["\r", "\n", "'"],
  692. ['\r', '\n', "\\'"],
  693. $this->lesson_location
  694. );
  695. } else {
  696. return '';
  697. }
  698. }
  699. /**
  700. * Gets the lesson_mode (scorm feature, but might be used by aicc as well
  701. * as chamilo paths).
  702. *
  703. * The "browse" mode is not supported yet (because there is no such way of
  704. * seeing a sco in Chamilo)
  705. *
  706. * @return string 'browse','normal' or 'review'. Defaults to 'normal'
  707. */
  708. public function get_lesson_mode()
  709. {
  710. $mode = 'normal';
  711. if ($this->get_prevent_reinit() != 0) {
  712. // If prevent_reinit == 0
  713. $my_status = $this->get_status();
  714. if ($my_status != $this->possible_status[0] && $my_status != $this->possible_status[1]) {
  715. $mode = 'review';
  716. }
  717. }
  718. return $mode;
  719. }
  720. /**
  721. * Gets the depth level.
  722. *
  723. * @return int Level. Defaults to 0
  724. */
  725. public function get_level()
  726. {
  727. if (self::DEBUG > 0) {
  728. error_log('learnpathItem::get_level()', 0);
  729. }
  730. if (empty($this->level)) {
  731. return 0;
  732. }
  733. return $this->level;
  734. }
  735. /**
  736. * Gets the mastery score.
  737. */
  738. public function get_mastery_score()
  739. {
  740. if (self::DEBUG > 0) {
  741. error_log('learnpathItem::get_mastery_score()', 0);
  742. }
  743. if (isset($this->mastery_score)) {
  744. return $this->mastery_score;
  745. } else {
  746. return -1;
  747. }
  748. }
  749. /**
  750. * Gets the maximum (score).
  751. *
  752. * @return int Maximum score. Defaults to 100 if nothing else is defined
  753. */
  754. public function get_max()
  755. {
  756. if (self::DEBUG > 0) {
  757. error_log('learnpathItem::get_max()', 0);
  758. }
  759. if ($this->type == 'sco') {
  760. if (isset($this->view_max_score) &&
  761. !empty($this->view_max_score) &&
  762. $this->view_max_score > 0
  763. ) {
  764. return $this->view_max_score;
  765. } elseif (isset($this->view_max_score) &&
  766. $this->view_max_score === ''
  767. ) {
  768. return $this->view_max_score;
  769. } else {
  770. if (!empty($this->max_score)) {
  771. return $this->max_score;
  772. } else {
  773. return 100;
  774. }
  775. }
  776. } else {
  777. if (!empty($this->max_score)) {
  778. return $this->max_score;
  779. } else {
  780. return 100;
  781. }
  782. }
  783. }
  784. /**
  785. * Gets the maximum time allowed for this user in this attempt on this item.
  786. *
  787. * @return string Time string in SCORM format
  788. * (HH:MM:SS or HH:MM:SS.SS or HHHH:MM:SS.SS)
  789. */
  790. public function get_max_time_allowed()
  791. {
  792. if (self::DEBUG > 0) {
  793. error_log('learnpathItem::get_max_time_allowed()', 0);
  794. }
  795. if (!empty($this->max_time_allowed)) {
  796. return $this->max_time_allowed;
  797. } else {
  798. return '';
  799. }
  800. }
  801. /**
  802. * Gets the minimum (score).
  803. *
  804. * @return int Minimum score. Defaults to 0
  805. */
  806. public function get_min()
  807. {
  808. if (self::DEBUG > 0) {
  809. error_log('learnpathItem::get_min()', 0);
  810. }
  811. if (!empty($this->min_score)) {
  812. return $this->min_score;
  813. } else {
  814. return 0;
  815. }
  816. }
  817. /**
  818. * Gets the parent ID.
  819. *
  820. * @return int Parent ID. Defaults to null
  821. */
  822. public function get_parent()
  823. {
  824. if (self::DEBUG > 0) {
  825. error_log('learnpathItem::get_parent()', 0);
  826. }
  827. if (!empty($this->parent)) {
  828. return $this->parent;
  829. }
  830. // TODO: Check this return value is valid for children classes (SCORM?).
  831. return null;
  832. }
  833. /**
  834. * Gets the path attribute.
  835. *
  836. * @return string Path. Defaults to ''
  837. */
  838. public function get_path()
  839. {
  840. if (self::DEBUG > 0) {
  841. error_log('learnpathItem::get_path()', 0);
  842. }
  843. if (empty($this->path)) {
  844. return '';
  845. }
  846. return $this->path;
  847. }
  848. /**
  849. * Gets the prerequisites string.
  850. *
  851. * @return string empty string or prerequisites string if defined
  852. */
  853. public function get_prereq_string()
  854. {
  855. if (self::DEBUG > 0) {
  856. error_log('learnpathItem::get_prereq_string()', 0);
  857. }
  858. if (!empty($this->prereq_string)) {
  859. return $this->prereq_string;
  860. } else {
  861. return '';
  862. }
  863. }
  864. /**
  865. * Gets the prevent_reinit attribute value (and sets it if not set already).
  866. *
  867. * @return int 1 or 0 (defaults to 1)
  868. */
  869. public function get_prevent_reinit()
  870. {
  871. if (self::DEBUG > 2) {
  872. error_log('learnpathItem::get_prevent_reinit()', 0);
  873. }
  874. if (!isset($this->prevent_reinit)) {
  875. if (!empty($this->lp_id)) {
  876. $table = Database::get_course_table(TABLE_LP_MAIN);
  877. $sql = "SELECT prevent_reinit
  878. FROM $table
  879. WHERE iid = ".$this->lp_id;
  880. $res = Database::query($sql);
  881. if (Database::num_rows($res) < 1) {
  882. $this->error = "Could not find parent learnpath in lp table";
  883. if (self::DEBUG > 2) {
  884. error_log(
  885. 'New LP - End of learnpathItem::get_prevent_reinit() - Returning false',
  886. 0
  887. );
  888. }
  889. return false;
  890. } else {
  891. $row = Database::fetch_array($res);
  892. $this->prevent_reinit = $row['prevent_reinit'];
  893. }
  894. } else {
  895. // Prevent reinit is always 1 by default - see learnpath.class.php
  896. $this->prevent_reinit = 1;
  897. }
  898. }
  899. if (self::DEBUG > 2) {
  900. error_log(
  901. 'New LP - End of learnpathItem::get_prevent_reinit() - Returned '.$this->prevent_reinit,
  902. 0
  903. );
  904. }
  905. return $this->prevent_reinit;
  906. }
  907. /**
  908. * Returns 1 if seriousgame_mode is activated, 0 otherwise.
  909. *
  910. * @return int (0 or 1)
  911. *
  912. * @deprecated seriousgame_mode seems not to be used
  913. *
  914. * @author ndiechburg <noel@cblue.be>
  915. */
  916. public function get_seriousgame_mode()
  917. {
  918. if (self::DEBUG > 2) {
  919. error_log('learnpathItem::get_seriousgame_mode()', 0);
  920. }
  921. if (!isset($this->seriousgame_mode)) {
  922. if (!empty($this->lp_id)) {
  923. $table = Database::get_course_table(TABLE_LP_MAIN);
  924. $sql = "SELECT seriousgame_mode
  925. FROM $table
  926. WHERE iid = ".$this->lp_id;
  927. $res = Database::query($sql);
  928. if (Database::num_rows($res) < 1) {
  929. $this->error = "Could not find parent learnpath in learnpath table";
  930. if (self::DEBUG > 2) {
  931. error_log(
  932. 'New LP - End of learnpathItem::get_seriousgame_mode() - Returning false',
  933. 0
  934. );
  935. }
  936. return false;
  937. } else {
  938. $row = Database::fetch_array($res);
  939. $this->seriousgame_mode = isset($row['seriousgame_mode']) ? $row['seriousgame_mode'] : 0;
  940. }
  941. } else {
  942. $this->seriousgame_mode = 0; //SeriousGame mode is always off by default
  943. }
  944. }
  945. if (self::DEBUG > 2) {
  946. error_log(
  947. 'New LP - End of learnpathItem::get_seriousgame_mode() - Returned '.$this->seriousgame_mode,
  948. 0
  949. );
  950. }
  951. return $this->seriousgame_mode;
  952. }
  953. /**
  954. * Gets the item's reference column.
  955. *
  956. * @return string The item's reference field (generally used for SCORM identifiers)
  957. */
  958. public function get_ref()
  959. {
  960. return $this->ref;
  961. }
  962. /**
  963. * Gets the list of included resources as a list of absolute or relative
  964. * paths of resources included in the current item. This allows for a
  965. * better SCORM export. The list will generally include pictures, flash
  966. * objects, java applets, or any other stuff included in the source of the
  967. * current item. The current item is expected to be an HTML file. If it
  968. * is not, then the function will return and empty list.
  969. *
  970. * @param string $type (one of the Chamilo tools) - optional (otherwise takes the current item's type)
  971. * @param string $abs_path absolute file path - optional (otherwise takes the current item's path)
  972. * @param int $recursivity level of recursivity we're in
  973. *
  974. * @return array List of file paths.
  975. * An additional field containing 'local' or 'remote' helps determine if
  976. * the file should be copied into the zip or just linked
  977. */
  978. public function get_resources_from_source(
  979. $type = null,
  980. $abs_path = null,
  981. $recursivity = 1
  982. ) {
  983. $max = 5;
  984. if ($recursivity > $max) {
  985. return [];
  986. }
  987. $type = empty($type) ? $this->get_type() : $type;
  988. if (!isset($abs_path)) {
  989. $path = $this->get_file_path();
  990. $abs_path = api_get_path(SYS_COURSE_PATH).api_get_course_path().'/'.$path;
  991. }
  992. $files_list = [];
  993. switch ($type) {
  994. case TOOL_DOCUMENT:
  995. case TOOL_QUIZ:
  996. case 'sco':
  997. // Get the document and, if HTML, open it.
  998. if (!is_file($abs_path)) {
  999. // The file could not be found.
  1000. return false;
  1001. }
  1002. // for now, read the whole file in one go (that's gonna be
  1003. // a problem when the file is too big).
  1004. $info = pathinfo($abs_path);
  1005. $ext = $info['extension'];
  1006. switch (strtolower($ext)) {
  1007. case 'html':
  1008. case 'htm':
  1009. case 'shtml':
  1010. case 'css':
  1011. $wantedAttributes = [
  1012. 'src',
  1013. 'url',
  1014. '@import',
  1015. 'href',
  1016. 'value',
  1017. ];
  1018. // Parse it for included resources.
  1019. $fileContent = file_get_contents($abs_path);
  1020. // Get an array of attributes from the HTML source.
  1021. $attributes = DocumentManager::parse_HTML_attributes(
  1022. $fileContent,
  1023. $wantedAttributes
  1024. );
  1025. // Look at 'src' attributes in this file
  1026. foreach ($wantedAttributes as $attr) {
  1027. if (isset($attributes[$attr])) {
  1028. // Find which kind of path these are (local or remote).
  1029. $sources = $attributes[$attr];
  1030. foreach ($sources as $source) {
  1031. // Skip what is obviously not a resource.
  1032. if (strpos($source, "+this.")) {
  1033. continue;
  1034. } // javascript code - will still work unaltered.
  1035. if (strpos($source, '.') === false) {
  1036. continue;
  1037. } // No dot, should not be an external file anyway.
  1038. if (strpos($source, 'mailto:')) {
  1039. continue;
  1040. } // mailto link.
  1041. if (strpos($source, ';') &&
  1042. !strpos($source, '&amp;')
  1043. ) {
  1044. continue;
  1045. } // Avoid code - that should help.
  1046. if ($attr == 'value') {
  1047. if (strpos($source, 'mp3file')) {
  1048. $files_list[] = [
  1049. substr(
  1050. $source,
  1051. 0,
  1052. strpos(
  1053. $source,
  1054. '.swf'
  1055. ) + 4
  1056. ),
  1057. 'local',
  1058. 'abs',
  1059. ];
  1060. $mp3file = substr(
  1061. $source,
  1062. strpos(
  1063. $source,
  1064. 'mp3file='
  1065. ) + 8
  1066. );
  1067. if (substr($mp3file, 0, 1) == '/') {
  1068. $files_list[] = [
  1069. $mp3file,
  1070. 'local',
  1071. 'abs',
  1072. ];
  1073. } else {
  1074. $files_list[] = [
  1075. $mp3file,
  1076. 'local',
  1077. 'rel',
  1078. ];
  1079. }
  1080. } elseif (strpos($source, 'flv=') === 0) {
  1081. $source = substr($source, 4);
  1082. if (strpos($source, '&') > 0) {
  1083. $source = substr(
  1084. $source,
  1085. 0,
  1086. strpos($source, '&')
  1087. );
  1088. }
  1089. if (strpos($source, '://') > 0) {
  1090. if (strpos($source, api_get_path(WEB_PATH)) !== false) {
  1091. // We found the current portal url.
  1092. $files_list[] = [
  1093. $source,
  1094. 'local',
  1095. 'url',
  1096. ];
  1097. } else {
  1098. // We didn't find any trace of current portal.
  1099. $files_list[] = [
  1100. $source,
  1101. 'remote',
  1102. 'url',
  1103. ];
  1104. }
  1105. } else {
  1106. $files_list[] = [
  1107. $source,
  1108. 'local',
  1109. 'abs',
  1110. ];
  1111. }
  1112. continue; // Skipping anything else to avoid two entries
  1113. //(while the others can have sub-files in their url, flv's can't).
  1114. }
  1115. }
  1116. if (strpos($source, '://') > 0) {
  1117. // Cut at '?' in a URL with params.
  1118. if (strpos($source, '?') > 0) {
  1119. $second_part = substr(
  1120. $source,
  1121. strpos($source, '?')
  1122. );
  1123. if (strpos($second_part, '://') > 0) {
  1124. // If the second part of the url contains a url too,
  1125. // treat the second one before cutting.
  1126. $pos1 = strpos(
  1127. $second_part,
  1128. '='
  1129. );
  1130. $pos2 = strpos(
  1131. $second_part,
  1132. '&'
  1133. );
  1134. $second_part = substr(
  1135. $second_part,
  1136. $pos1 + 1,
  1137. $pos2 - ($pos1 + 1)
  1138. );
  1139. if (strpos($second_part, api_get_path(WEB_PATH)) !== false) {
  1140. // We found the current portal url.
  1141. $files_list[] = [
  1142. $second_part,
  1143. 'local',
  1144. 'url',
  1145. ];
  1146. $in_files_list[] = self::get_resources_from_source(
  1147. TOOL_DOCUMENT,
  1148. $second_part,
  1149. $recursivity + 1
  1150. );
  1151. if (count($in_files_list) > 0) {
  1152. $files_list = array_merge(
  1153. $files_list,
  1154. $in_files_list
  1155. );
  1156. }
  1157. } else {
  1158. // We didn't find any trace of current portal.
  1159. $files_list[] = [
  1160. $second_part,
  1161. 'remote',
  1162. 'url',
  1163. ];
  1164. }
  1165. } elseif (strpos($second_part, '=') > 0) {
  1166. if (substr($second_part, 0, 1) === '/') {
  1167. // Link starts with a /,
  1168. // making it absolute (relative to DocumentRoot).
  1169. $files_list[] = [
  1170. $second_part,
  1171. 'local',
  1172. 'abs',
  1173. ];
  1174. $in_files_list[] = self::get_resources_from_source(
  1175. TOOL_DOCUMENT,
  1176. $second_part,
  1177. $recursivity + 1
  1178. );
  1179. if (count($in_files_list) > 0) {
  1180. $files_list = array_merge(
  1181. $files_list,
  1182. $in_files_list
  1183. );
  1184. }
  1185. } elseif (strstr($second_part, '..') === 0) {
  1186. // Link is relative but going back in the hierarchy.
  1187. $files_list[] = [
  1188. $second_part,
  1189. 'local',
  1190. 'rel',
  1191. ];
  1192. $dir = dirname(
  1193. $abs_path
  1194. );
  1195. $new_abs_path = realpath(
  1196. $dir.'/'.$second_part
  1197. );
  1198. $in_files_list[] = self::get_resources_from_source(
  1199. TOOL_DOCUMENT,
  1200. $new_abs_path,
  1201. $recursivity + 1
  1202. );
  1203. if (count($in_files_list) > 0) {
  1204. $files_list = array_merge(
  1205. $files_list,
  1206. $in_files_list
  1207. );
  1208. }
  1209. } else {
  1210. // No starting '/', making it relative to current document's path.
  1211. if (substr($second_part, 0, 2) == './') {
  1212. $second_part = substr(
  1213. $second_part,
  1214. 2
  1215. );
  1216. }
  1217. $files_list[] = [
  1218. $second_part,
  1219. 'local',
  1220. 'rel',
  1221. ];
  1222. $dir = dirname(
  1223. $abs_path
  1224. );
  1225. $new_abs_path = realpath(
  1226. $dir.'/'.$second_part
  1227. );
  1228. $in_files_list[] = self::get_resources_from_source(
  1229. TOOL_DOCUMENT,
  1230. $new_abs_path,
  1231. $recursivity + 1
  1232. );
  1233. if (count($in_files_list) > 0) {
  1234. $files_list = array_merge(
  1235. $files_list,
  1236. $in_files_list
  1237. );
  1238. }
  1239. }
  1240. }
  1241. // Leave that second part behind now.
  1242. $source = substr(
  1243. $source,
  1244. 0,
  1245. strpos($source, '?')
  1246. );
  1247. if (strpos($source, '://') > 0) {
  1248. if (strpos($source, api_get_path(WEB_PATH)) !== false) {
  1249. // We found the current portal url.
  1250. $files_list[] = [
  1251. $source,
  1252. 'local',
  1253. 'url',
  1254. ];
  1255. $in_files_list[] = self::get_resources_from_source(
  1256. TOOL_DOCUMENT,
  1257. $source,
  1258. $recursivity + 1
  1259. );
  1260. if (count($in_files_list) > 0) {
  1261. $files_list = array_merge(
  1262. $files_list,
  1263. $in_files_list
  1264. );
  1265. }
  1266. } else {
  1267. // We didn't find any trace of current portal.
  1268. $files_list[] = [
  1269. $source,
  1270. 'remote',
  1271. 'url',
  1272. ];
  1273. }
  1274. } else {
  1275. // No protocol found, make link local.
  1276. if (substr($source, 0, 1) === '/') {
  1277. // Link starts with a /, making it absolute (relative to DocumentRoot).
  1278. $files_list[] = [
  1279. $source,
  1280. 'local',
  1281. 'abs',
  1282. ];
  1283. $in_files_list[] = self::get_resources_from_source(
  1284. TOOL_DOCUMENT,
  1285. $source,
  1286. $recursivity + 1
  1287. );
  1288. if (count($in_files_list) > 0) {
  1289. $files_list = array_merge(
  1290. $files_list,
  1291. $in_files_list
  1292. );
  1293. }
  1294. } elseif (strstr($source, '..') === 0) {
  1295. // Link is relative but going back in the hierarchy.
  1296. $files_list[] = [
  1297. $source,
  1298. 'local',
  1299. 'rel',
  1300. ];
  1301. $dir = dirname(
  1302. $abs_path
  1303. );
  1304. $new_abs_path = realpath(
  1305. $dir.'/'.$source
  1306. );
  1307. $in_files_list[] = self::get_resources_from_source(
  1308. TOOL_DOCUMENT,
  1309. $new_abs_path,
  1310. $recursivity + 1
  1311. );
  1312. if (count($in_files_list) > 0) {
  1313. $files_list = array_merge(
  1314. $files_list,
  1315. $in_files_list
  1316. );
  1317. }
  1318. } else {
  1319. // No starting '/', making it relative to current document's path.
  1320. if (substr($source, 0, 2) == './') {
  1321. $source = substr(
  1322. $source,
  1323. 2
  1324. );
  1325. }
  1326. $files_list[] = [
  1327. $source,
  1328. 'local',
  1329. 'rel',
  1330. ];
  1331. $dir = dirname(
  1332. $abs_path
  1333. );
  1334. $new_abs_path = realpath(
  1335. $dir.'/'.$source
  1336. );
  1337. $in_files_list[] = self::get_resources_from_source(
  1338. TOOL_DOCUMENT,
  1339. $new_abs_path,
  1340. $recursivity + 1
  1341. );
  1342. if (count($in_files_list) > 0) {
  1343. $files_list = array_merge(
  1344. $files_list,
  1345. $in_files_list
  1346. );
  1347. }
  1348. }
  1349. }
  1350. }
  1351. // Found some protocol there.
  1352. if (strpos($source, api_get_path(WEB_PATH)) !== false) {
  1353. // We found the current portal url.
  1354. $files_list[] = [
  1355. $source,
  1356. 'local',
  1357. 'url',
  1358. ];
  1359. $in_files_list[] = self::get_resources_from_source(
  1360. TOOL_DOCUMENT,
  1361. $source,
  1362. $recursivity + 1
  1363. );
  1364. if (count($in_files_list) > 0) {
  1365. $files_list = array_merge(
  1366. $files_list,
  1367. $in_files_list
  1368. );
  1369. }
  1370. } else {
  1371. // We didn't find any trace of current portal.
  1372. $files_list[] = [
  1373. $source,
  1374. 'remote',
  1375. 'url',
  1376. ];
  1377. }
  1378. } else {
  1379. // No protocol found, make link local.
  1380. if (substr($source, 0, 1) === '/') {
  1381. // Link starts with a /, making it absolute (relative to DocumentRoot).
  1382. $files_list[] = [
  1383. $source,
  1384. 'local',
  1385. 'abs',
  1386. ];
  1387. $in_files_list[] = self::get_resources_from_source(
  1388. TOOL_DOCUMENT,
  1389. $source,
  1390. $recursivity + 1
  1391. );
  1392. if (count($in_files_list) > 0) {
  1393. $files_list = array_merge(
  1394. $files_list,
  1395. $in_files_list
  1396. );
  1397. }
  1398. } elseif (strstr($source, '..') === 0) {
  1399. // Link is relative but going back in the hierarchy.
  1400. $files_list[] = [
  1401. $source,
  1402. 'local',
  1403. 'rel',
  1404. ];
  1405. $dir = dirname($abs_path);
  1406. $new_abs_path = realpath(
  1407. $dir.'/'.$source
  1408. );
  1409. $in_files_list[] = self::get_resources_from_source(
  1410. TOOL_DOCUMENT,
  1411. $new_abs_path,
  1412. $recursivity + 1
  1413. );
  1414. if (count($in_files_list) > 0) {
  1415. $files_list = array_merge(
  1416. $files_list,
  1417. $in_files_list
  1418. );
  1419. }
  1420. } else {
  1421. // No starting '/', making it relative to current document's path.
  1422. if (strpos($source, 'width=') ||
  1423. strpos($source, 'autostart=')
  1424. ) {
  1425. continue;
  1426. }
  1427. if (substr($source, 0, 2) == './') {
  1428. $source = substr(
  1429. $source,
  1430. 2
  1431. );
  1432. }
  1433. $files_list[] = [
  1434. $source,
  1435. 'local',
  1436. 'rel',
  1437. ];
  1438. $dir = dirname($abs_path);
  1439. $new_abs_path = realpath(
  1440. $dir.'/'.$source
  1441. );
  1442. $in_files_list[] = self::get_resources_from_source(
  1443. TOOL_DOCUMENT,
  1444. $new_abs_path,
  1445. $recursivity + 1
  1446. );
  1447. if (count($in_files_list) > 0) {
  1448. $files_list = array_merge(
  1449. $files_list,
  1450. $in_files_list
  1451. );
  1452. }
  1453. }
  1454. }
  1455. }
  1456. }
  1457. }
  1458. break;
  1459. default:
  1460. break;
  1461. }
  1462. break;
  1463. default: // Ignore.
  1464. break;
  1465. }
  1466. $checked_files_list = [];
  1467. $checked_array_list = [];
  1468. foreach ($files_list as $idx => $file) {
  1469. if (!empty($file[0])) {
  1470. if (!in_array($file[0], $checked_files_list)) {
  1471. $checked_files_list[] = $files_list[$idx][0];
  1472. $checked_array_list[] = $files_list[$idx];
  1473. }
  1474. }
  1475. }
  1476. return $checked_array_list;
  1477. }
  1478. /**
  1479. * Gets the score.
  1480. *
  1481. * @return float The current score or 0 if no score set yet
  1482. */
  1483. public function get_score()
  1484. {
  1485. if (self::DEBUG > 0) {
  1486. error_log('learnpathItem::get_score()', 0);
  1487. }
  1488. $res = 0;
  1489. if (!empty($this->current_score)) {
  1490. $res = $this->current_score;
  1491. }
  1492. if (self::DEBUG > 1) {
  1493. error_log(
  1494. 'New LP - Out of learnpathItem::get_score() - returning '.$res,
  1495. 0
  1496. );
  1497. }
  1498. return $res;
  1499. }
  1500. /**
  1501. * Gets the item status.
  1502. *
  1503. * @param bool $check_db Do or don't check into the database for the
  1504. * latest value. Optional. Default is true
  1505. * @param bool $update_local Do or don't update the local attribute
  1506. * value with what's been found in DB
  1507. *
  1508. * @return string Current status or 'Not attempted' if no status set yet
  1509. */
  1510. public function get_status($check_db = true, $update_local = false)
  1511. {
  1512. $course_id = api_get_course_int_id();
  1513. $debug = self::DEBUG;
  1514. if ($debug > 0) {
  1515. error_log('learnpathItem::get_status() on item '.$this->db_id, 0);
  1516. }
  1517. if ($check_db) {
  1518. if ($debug > 2) {
  1519. error_log('learnpathItem::get_status(): checking db', 0);
  1520. }
  1521. if (!empty($this->db_item_view_id) && !empty($course_id)) {
  1522. $table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  1523. $sql = "SELECT status FROM $table
  1524. WHERE
  1525. c_id = $course_id AND
  1526. iid = '".$this->db_item_view_id."' AND
  1527. view_count = '".$this->get_attempt_id()."'";
  1528. if ($debug > 2) {
  1529. error_log(
  1530. 'learnpathItem::get_status() - Checking DB: '.$sql,
  1531. 0
  1532. );
  1533. }
  1534. $res = Database::query($sql);
  1535. if (Database::num_rows($res) == 1) {
  1536. $row = Database::fetch_array($res);
  1537. if ($update_local) {
  1538. if ($debug > 2) {
  1539. error_log(
  1540. 'learnpathItem::set_status() :'.$row['status'],
  1541. 0
  1542. );
  1543. }
  1544. $this->set_status($row['status']);
  1545. }
  1546. if ($debug > 2) {
  1547. error_log(
  1548. 'learnpathItem::get_status() - Returning db value '.$row['status'],
  1549. 0
  1550. );
  1551. }
  1552. return $row['status'];
  1553. }
  1554. }
  1555. } else {
  1556. if ($debug > 2) {
  1557. error_log(
  1558. 'learnpathItem::get_status() - in get_status: using attrib',
  1559. 0
  1560. );
  1561. }
  1562. if (!empty($this->status)) {
  1563. if ($debug > 2) {
  1564. error_log(
  1565. 'learnpathItem::get_status() - Returning attrib: '.$this->status,
  1566. 0
  1567. );
  1568. }
  1569. return $this->status;
  1570. }
  1571. }
  1572. if ($debug > 2) {
  1573. error_log(
  1574. 'learnpathItem::get_status() - Returning default '.$this->possible_status[0],
  1575. 0
  1576. );
  1577. }
  1578. return $this->possible_status[0];
  1579. }
  1580. /**
  1581. * Gets the suspend data.
  1582. */
  1583. public function get_suspend_data()
  1584. {
  1585. if (self::DEBUG > 0) {
  1586. error_log('learnpathItem::get_suspend_data()', 0);
  1587. }
  1588. // TODO: Improve cleaning of breaklines ... it works but is it really
  1589. // a beautiful way to do it ?
  1590. if (!empty($this->current_data)) {
  1591. return str_replace(
  1592. ["\r", "\n", "'"],
  1593. ['\r', '\n', "\\'"],
  1594. $this->current_data
  1595. );
  1596. } else {
  1597. return '';
  1598. }
  1599. }
  1600. /**
  1601. * @param string $origin
  1602. * @param string $time
  1603. *
  1604. * @return string
  1605. */
  1606. public static function getScormTimeFromParameter(
  1607. $origin = 'php',
  1608. $time = null
  1609. ) {
  1610. $h = get_lang('h');
  1611. if (!isset($time)) {
  1612. if ($origin == 'js') {
  1613. return '00 : 00: 00';
  1614. } else {
  1615. return '00 '.$h.' 00 \' 00"';
  1616. }
  1617. } else {
  1618. return api_format_time($time, $origin);
  1619. }
  1620. }
  1621. /**
  1622. * Gets the total time spent on this item view so far.
  1623. *
  1624. * @param string $origin Origin of the request. If coming from PHP,
  1625. * send formatted as xxhxx'xx", otherwise use scorm format 00:00:00
  1626. * @param int|null $given_time Given time is a default time to return formatted
  1627. * @param bool $query_db Whether to get the value from db or from memory
  1628. *
  1629. * @return string A string with the time in SCORM format
  1630. */
  1631. public function get_scorm_time(
  1632. $origin = 'php',
  1633. $given_time = null,
  1634. $query_db = false
  1635. ) {
  1636. $time = null;
  1637. $course_id = api_get_course_int_id();
  1638. if (!isset($given_time)) {
  1639. if (self::DEBUG > 2) {
  1640. error_log(
  1641. 'learnpathItem::get_scorm_time(): given time empty, current_start_time = '.$this->current_start_time,
  1642. 0
  1643. );
  1644. }
  1645. if ($query_db === true) {
  1646. $table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  1647. $sql = "SELECT start_time, total_time
  1648. FROM $table
  1649. WHERE
  1650. c_id = $course_id AND
  1651. iid = '".$this->db_item_view_id."' AND
  1652. view_count = '".$this->get_attempt_id()."'";
  1653. $res = Database::query($sql);
  1654. $row = Database::fetch_array($res);
  1655. $start = $row['start_time'];
  1656. $stop = $start + $row['total_time'];
  1657. } else {
  1658. $start = $this->current_start_time;
  1659. $stop = $this->current_stop_time;
  1660. }
  1661. if (!empty($start)) {
  1662. if (!empty($stop)) {
  1663. $time = $stop - $start;
  1664. } else {
  1665. $time = time() - $start;
  1666. }
  1667. }
  1668. } else {
  1669. $time = $given_time;
  1670. }
  1671. if (self::DEBUG > 2) {
  1672. error_log(
  1673. 'learnpathItem::get_scorm_time(): intermediate = '.$time,
  1674. 0
  1675. );
  1676. }
  1677. $time = api_format_time($time, $origin);
  1678. return $time;
  1679. }
  1680. /**
  1681. * Get the extra terms (tags) that identify this item.
  1682. *
  1683. * @return mixed
  1684. */
  1685. public function get_terms()
  1686. {
  1687. $table = Database::get_course_table(TABLE_LP_ITEM);
  1688. $sql = "SELECT * FROM $table
  1689. WHERE iid = ".intval($this->db_id);
  1690. $res = Database::query($sql);
  1691. $row = Database::fetch_array($res);
  1692. return $row['terms'];
  1693. }
  1694. /**
  1695. * Returns the item's title.
  1696. *
  1697. * @return string Title
  1698. */
  1699. public function get_title()
  1700. {
  1701. if (self::DEBUG > 0) {
  1702. error_log('learnpathItem::get_title()', 0);
  1703. }
  1704. if (empty($this->title)) {
  1705. return '';
  1706. }
  1707. return $this->title;
  1708. }
  1709. /**
  1710. * Returns the total time used to see that item.
  1711. *
  1712. * @return int Total time
  1713. */
  1714. public function get_total_time()
  1715. {
  1716. $debug = self::DEBUG;
  1717. if ($debug) {
  1718. error_log(
  1719. 'learnpathItem::get_total_time() for item '.$this->db_id.
  1720. ' - Initially, current_start_time = '.$this->current_start_time.
  1721. ' and current_stop_time = '.$this->current_stop_time,
  1722. 0
  1723. );
  1724. }
  1725. if ($this->current_start_time == 0) {
  1726. // Shouldn't be necessary thanks to the open() method.
  1727. if ($debug) {
  1728. error_log(
  1729. 'learnpathItem::get_total_time() - Current start time was empty',
  1730. 0
  1731. );
  1732. }
  1733. $this->current_start_time = time();
  1734. }
  1735. if (time() < $this->current_stop_time ||
  1736. $this->current_stop_time == 0
  1737. ) {
  1738. if ($debug) {
  1739. error_log(
  1740. 'learnpathItem::get_total_time() - Current stop time was '
  1741. .'greater than the current time or was empty',
  1742. 0
  1743. );
  1744. }
  1745. // If this case occurs, then we risk to write huge time data in db.
  1746. // In theory, stop time should be *always* updated here, but it
  1747. // might be used in some unknown goal.
  1748. $this->current_stop_time = time();
  1749. }
  1750. $time = $this->current_stop_time - $this->current_start_time;
  1751. if ($time < 0) {
  1752. if ($debug) {
  1753. error_log(
  1754. 'learnpathItem::get_total_time() - Time smaller than 0. Returning 0',
  1755. 0
  1756. );
  1757. }
  1758. return 0;
  1759. } else {
  1760. $time = $this->fixAbusiveTime($time);
  1761. if ($debug) {
  1762. error_log(
  1763. 'Current start time = '.$this->current_start_time.', current stop time = '.
  1764. $this->current_stop_time.' Returning '.$time."-----------\n"
  1765. );
  1766. }
  1767. return $time;
  1768. }
  1769. }
  1770. /**
  1771. * Sometimes time recorded for a learning path item is superior to the maximum allowed duration of the session.
  1772. * In this case, this session resets the time for that particular learning path item to 5 minutes
  1773. * (something more realistic, that is also used when leaving the portal without closing one's session).
  1774. *
  1775. * @param int $time
  1776. *
  1777. * @return int
  1778. */
  1779. public function fixAbusiveTime($time)
  1780. {
  1781. // Code based from Event::courseLogout
  1782. $sessionLifetime = api_get_configuration_value('session_lifetime');
  1783. // If session life time too big use 1 hour
  1784. if (empty($sessionLifetime) || $sessionLifetime > 86400) {
  1785. $sessionLifetime = 3600;
  1786. }
  1787. $fixedAddedMinute = 5 * 60; // Add only 5 minutes
  1788. if ($time > $sessionLifetime) {
  1789. error_log("fixAbusiveTime: Total time is too big: $time replaced with: $fixedAddedMinute");
  1790. error_log("item_id : ".$this->db_id." lp_item_view.iid: ".$this->db_item_view_id);
  1791. $time = $fixedAddedMinute;
  1792. }
  1793. return $time;
  1794. }
  1795. /**
  1796. * Gets the item type.
  1797. *
  1798. * @return string The item type (can be doc, dir, sco, asset)
  1799. */
  1800. public function get_type()
  1801. {
  1802. $res = 'asset';
  1803. if (!empty($this->type)) {
  1804. $res = $this->type;
  1805. }
  1806. if (self::DEBUG > 2) {
  1807. error_log(
  1808. 'learnpathItem::get_type() - Returning '.$res.' for item '.$this->db_id,
  1809. 0
  1810. );
  1811. }
  1812. return $res;
  1813. }
  1814. /**
  1815. * Gets the view count for this item.
  1816. *
  1817. * @return int Number of attempts or 0
  1818. */
  1819. public function get_view_count()
  1820. {
  1821. if (self::DEBUG > 0) {
  1822. error_log('learnpathItem::get_view_count()', 0);
  1823. }
  1824. if (!empty($this->attempt_id)) {
  1825. return $this->attempt_id;
  1826. } else {
  1827. return 0;
  1828. }
  1829. }
  1830. /**
  1831. * Tells if an item is done ('completed','passed','succeeded') or not.
  1832. *
  1833. * @return bool True if the item is done ('completed','passed','succeeded'),
  1834. * false otherwise
  1835. */
  1836. public function is_done()
  1837. {
  1838. $completedStatusList = [
  1839. 'completed',
  1840. 'passed',
  1841. 'succeeded',
  1842. 'failed',
  1843. ];
  1844. if ($this->status_is($completedStatusList)) {
  1845. if (self::DEBUG > 2) {
  1846. error_log(
  1847. 'learnpath::is_done() - Item '.$this->get_id(
  1848. ).' is complete',
  1849. 0
  1850. );
  1851. }
  1852. return true;
  1853. } else {
  1854. if (self::DEBUG > 2) {
  1855. error_log(
  1856. 'learnpath::is_done() - Item '.$this->get_id(
  1857. ).' is not complete',
  1858. 0
  1859. );
  1860. }
  1861. return false;
  1862. }
  1863. }
  1864. /**
  1865. * Tells if a restart is allowed (take it from $this->prevent_reinit and $this->status).
  1866. *
  1867. * @return int -1 if retaking the sco another time for credit is not allowed,
  1868. * 0 if it is not allowed but the item has to be finished
  1869. * 1 if it is allowed. Defaults to 1
  1870. */
  1871. public function isRestartAllowed()
  1872. {
  1873. if (self::DEBUG > 2) {
  1874. error_log('learnpathItem::isRestartAllowed()', 0);
  1875. }
  1876. $restart = 1;
  1877. $mystatus = $this->get_status(true);
  1878. if ($this->get_prevent_reinit() > 0) {
  1879. // If prevent_reinit == 1 (or more)
  1880. // If status is not attempted or incomplete, authorize retaking (of the same) anyway. Otherwise:
  1881. if ($mystatus != $this->possible_status[0] && $mystatus != $this->possible_status[1]) {
  1882. $restart = -1;
  1883. } else { //status incompleted or not attempted
  1884. $restart = 0;
  1885. }
  1886. } else {
  1887. if ($mystatus == $this->possible_status[0] || $mystatus == $this->possible_status[1]) {
  1888. $restart = -1;
  1889. }
  1890. }
  1891. if (self::DEBUG > 2) {
  1892. error_log(
  1893. 'New LP - End of learnpathItem::isRestartAllowed() - Returning '.$restart,
  1894. 0
  1895. );
  1896. }
  1897. return $restart;
  1898. }
  1899. /**
  1900. * Opens/launches the item. Initialises runtime values.
  1901. *
  1902. * @param bool $allow_new_attempt
  1903. *
  1904. * @return bool true on success, false on failure
  1905. */
  1906. public function open($allow_new_attempt = false)
  1907. {
  1908. if (self::DEBUG > 0) {
  1909. error_log('learnpathItem::open()', 0);
  1910. }
  1911. if ($this->prevent_reinit == 0) {
  1912. $this->current_score = 0;
  1913. $this->current_start_time = time();
  1914. // In this case, as we are opening the item, what is important to us
  1915. // is the database status, in order to know if this item has already
  1916. // been used in the past (rather than just loaded and modified by
  1917. // some javascript but not written in the database).
  1918. // If the database status is different from 'not attempted', we can
  1919. // consider this item has already been used, and as such we can
  1920. // open a new attempt. Otherwise, we'll just reuse the current
  1921. // attempt, which is generally created the first time the item is
  1922. // loaded (for example as part of the table of contents).
  1923. $stat = $this->get_status(true);
  1924. if ($allow_new_attempt && isset($stat) && ($stat != $this->possible_status[0])) {
  1925. $this->attempt_id = $this->attempt_id + 1; // Open a new attempt.
  1926. }
  1927. $this->status = $this->possible_status[1];
  1928. } else {
  1929. /*if ($this->current_start_time == 0) {
  1930. // Small exception for start time, to avoid amazing values.
  1931. $this->current_start_time = time();
  1932. }*/
  1933. // If we don't init start time here, the time is sometimes calculated from the last start time.
  1934. $this->current_start_time = time();
  1935. }
  1936. }
  1937. /**
  1938. * Outputs the item contents.
  1939. *
  1940. * @return string HTML file (displayable in an <iframe>) or empty string if no path defined
  1941. */
  1942. public function output()
  1943. {
  1944. if (self::DEBUG > 0) {
  1945. error_log('learnpathItem::output()', 0);
  1946. }
  1947. if (!empty($this->path) and is_file($this->path)) {
  1948. $output = '';
  1949. $output .= file_get_contents($this->path);
  1950. return $output;
  1951. }
  1952. return '';
  1953. }
  1954. /**
  1955. * Parses the prerequisites string with the AICC logic language.
  1956. *
  1957. * @param string $prereqs_string The prerequisites string as it figures in imsmanifest.xml
  1958. * @param array $items Array of items in the current learnpath object.
  1959. * Although we're in the learnpathItem object, it's necessary to have
  1960. * a list of all items to be able to check the current item's prerequisites
  1961. * @param array $refs_list list of references
  1962. * (the "ref" column in the lp_item table) that are strings used in the
  1963. * expression of prerequisites
  1964. * @param int $user_id The user ID. In some cases like Chamilo quizzes,
  1965. * it's necessary to have the user ID to query other tables (like the results of quizzes)
  1966. *
  1967. * @return bool True if the list of prerequisites given is entirely satisfied, false otherwise
  1968. */
  1969. public function parse_prereq($prereqs_string, $items, $refs_list, $user_id)
  1970. {
  1971. if (self::DEBUG > 0) {
  1972. error_log(
  1973. 'learnpathItem::parse_prereq() for learnpath '.$this->lp_id.' with string '.$prereqs_string,
  1974. 0
  1975. );
  1976. }
  1977. $course_id = api_get_course_int_id();
  1978. $sessionId = api_get_session_id();
  1979. // Deal with &, |, ~, =, <>, {}, ,, X*, () in reverse order.
  1980. $this->prereq_alert = '';
  1981. // First parse all parenthesis by using a sequential loop
  1982. // (looking for less-inclusives first).
  1983. if ($prereqs_string == '_true_') {
  1984. return true;
  1985. }
  1986. if ($prereqs_string == '_false_') {
  1987. if (empty($this->prereq_alert)) {
  1988. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  1989. }
  1990. return false;
  1991. }
  1992. while (strpos($prereqs_string, '(') !== false) {
  1993. // Remove any () set and replace with its value.
  1994. $matches = [];
  1995. $res = preg_match_all(
  1996. '/(\(([^\(\)]*)\))/',
  1997. $prereqs_string,
  1998. $matches
  1999. );
  2000. if ($res) {
  2001. foreach ($matches[2] as $id => $match) {
  2002. $str_res = $this->parse_prereq(
  2003. $match,
  2004. $items,
  2005. $refs_list,
  2006. $user_id
  2007. );
  2008. if ($str_res) {
  2009. $prereqs_string = str_replace(
  2010. $matches[1][$id],
  2011. '_true_',
  2012. $prereqs_string
  2013. );
  2014. } else {
  2015. $prereqs_string = str_replace(
  2016. $matches[1][$id],
  2017. '_false_',
  2018. $prereqs_string
  2019. );
  2020. }
  2021. }
  2022. }
  2023. }
  2024. // Parenthesis removed, now look for ORs as it is the lesser-priority
  2025. // binary operator (= always uses one text operand).
  2026. if (strpos($prereqs_string, '|') === false) {
  2027. if (self::DEBUG > 1) {
  2028. error_log('New LP - Didnt find any OR, looking for AND', 0);
  2029. }
  2030. if (strpos($prereqs_string, '&') !== false) {
  2031. $list = explode('&', $prereqs_string);
  2032. if (count($list) > 1) {
  2033. $andstatus = true;
  2034. foreach ($list as $condition) {
  2035. $andstatus = $andstatus && $this->parse_prereq(
  2036. $condition,
  2037. $items,
  2038. $refs_list,
  2039. $user_id
  2040. );
  2041. if (!$andstatus) {
  2042. if (self::DEBUG > 1) {
  2043. error_log(
  2044. 'New LP - One condition in AND was false, short-circuit',
  2045. 0
  2046. );
  2047. }
  2048. break;
  2049. }
  2050. }
  2051. if (empty($this->prereq_alert) && !$andstatus) {
  2052. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2053. }
  2054. return $andstatus;
  2055. } else {
  2056. if (isset($items[$refs_list[$list[0]]])) {
  2057. $status = $items[$refs_list[$list[0]]]->get_status(true);
  2058. $returnstatus = ($status == $this->possible_status[2]) || ($status == $this->possible_status[3]);
  2059. if (empty($this->prereq_alert) && !$returnstatus) {
  2060. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2061. }
  2062. return $returnstatus;
  2063. }
  2064. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2065. return false;
  2066. }
  2067. } else {
  2068. // No ORs found, now look for ANDs.
  2069. if (self::DEBUG > 1) {
  2070. error_log('New LP - Didnt find any AND, looking for =', 0);
  2071. }
  2072. if (strpos($prereqs_string, '=') !== false) {
  2073. if (self::DEBUG > 1) {
  2074. error_log('New LP - Found =, looking into it', 0);
  2075. }
  2076. // We assume '=' signs only appear when there's nothing else around.
  2077. $params = explode('=', $prereqs_string);
  2078. if (count($params) == 2) {
  2079. // Right number of operands.
  2080. if (isset($items[$refs_list[$params[0]]])) {
  2081. $status = $items[$refs_list[$params[0]]]->get_status(true);
  2082. $returnstatus = $status == $params[1];
  2083. if (empty($this->prereq_alert) && !$returnstatus) {
  2084. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2085. }
  2086. return $returnstatus;
  2087. }
  2088. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2089. return false;
  2090. }
  2091. } else {
  2092. // No ANDs found, look for <>
  2093. if (self::DEBUG > 1) {
  2094. error_log(
  2095. 'New LP - Didnt find any =, looking for <>',
  2096. 0
  2097. );
  2098. }
  2099. if (strpos($prereqs_string, '<>') !== false) {
  2100. if (self::DEBUG > 1) {
  2101. error_log('New LP - Found <>, looking into it', 0);
  2102. }
  2103. // We assume '<>' signs only appear when there's nothing else around.
  2104. $params = explode('<>', $prereqs_string);
  2105. if (count($params) == 2) {
  2106. // Right number of operands.
  2107. if (isset($items[$refs_list[$params[0]]])) {
  2108. $status = $items[$refs_list[$params[0]]]->get_status(true);
  2109. $returnstatus = $status != $params[1];
  2110. if (empty($this->prereq_alert) && !$returnstatus) {
  2111. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2112. }
  2113. return $returnstatus;
  2114. }
  2115. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2116. return false;
  2117. }
  2118. } else {
  2119. // No <> found, look for ~ (unary)
  2120. if (self::DEBUG > 1) {
  2121. error_log(
  2122. 'New LP - Didnt find any =, looking for ~',
  2123. 0
  2124. );
  2125. }
  2126. // Only remains: ~ and X*{}
  2127. if (strpos($prereqs_string, '~') !== false) {
  2128. // Found NOT.
  2129. if (self::DEBUG > 1) {
  2130. error_log(
  2131. 'New LP - Found ~, looking into it',
  2132. 0
  2133. );
  2134. }
  2135. $list = [];
  2136. $myres = preg_match(
  2137. '/~([^(\d+\*)\{]*)/',
  2138. $prereqs_string,
  2139. $list
  2140. );
  2141. if ($myres) {
  2142. $returnstatus = !$this->parse_prereq(
  2143. $list[1],
  2144. $items,
  2145. $refs_list,
  2146. $user_id
  2147. );
  2148. if (empty($this->prereq_alert) && !$returnstatus) {
  2149. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2150. }
  2151. return $returnstatus;
  2152. } else {
  2153. // Strange...
  2154. if (self::DEBUG > 1) {
  2155. error_log(
  2156. 'New LP - Found ~ but strange string: '.$prereqs_string,
  2157. 0
  2158. );
  2159. }
  2160. }
  2161. } else {
  2162. // Finally, look for sets/groups
  2163. if (self::DEBUG > 1) {
  2164. error_log(
  2165. 'New LP - Didnt find any ~, looking for groups',
  2166. 0
  2167. );
  2168. }
  2169. // Only groups here.
  2170. $groups = [];
  2171. $groups_there = preg_match_all(
  2172. '/((\d+\*)?\{([^\}]+)\}+)/',
  2173. $prereqs_string,
  2174. $groups
  2175. );
  2176. if ($groups_there) {
  2177. foreach ($groups[1] as $gr) {
  2178. // Only take the results that correspond to
  2179. // the big brackets-enclosed condition.
  2180. if (self::DEBUG > 1) {
  2181. error_log(
  2182. 'New LP - Dealing with group '.$gr,
  2183. 0
  2184. );
  2185. }
  2186. $multi = [];
  2187. $mycond = false;
  2188. if (preg_match(
  2189. '/(\d+)\*\{([^\}]+)\}/',
  2190. $gr,
  2191. $multi
  2192. )
  2193. ) {
  2194. if (self::DEBUG > 1) {
  2195. error_log(
  2196. 'New LP - Found multiplier '.$multi[0],
  2197. 0
  2198. );
  2199. }
  2200. $count = $multi[1];
  2201. $list = explode(',', $multi[2]);
  2202. $mytrue = 0;
  2203. foreach ($list as $cond) {
  2204. if (isset($items[$refs_list[$cond]])) {
  2205. $status = $items[$refs_list[$cond]]->get_status(true);
  2206. if ($status == $this->possible_status[2] ||
  2207. $status == $this->possible_status[3]
  2208. ) {
  2209. $mytrue++;
  2210. if (self::DEBUG > 1) {
  2211. error_log(
  2212. 'New LP - Found true item, counting.. ('.($mytrue).')',
  2213. 0
  2214. );
  2215. }
  2216. }
  2217. } else {
  2218. if (self::DEBUG > 1) {
  2219. error_log(
  2220. 'New LP - item '.$cond.' does not exist in items list',
  2221. 0
  2222. );
  2223. }
  2224. }
  2225. }
  2226. if ($mytrue >= $count) {
  2227. if (self::DEBUG > 1) {
  2228. error_log(
  2229. 'New LP - Got enough true results, return true',
  2230. 0
  2231. );
  2232. }
  2233. $mycond = true;
  2234. } else {
  2235. if (self::DEBUG > 1) {
  2236. error_log(
  2237. 'New LP - Not enough true results',
  2238. 0
  2239. );
  2240. }
  2241. }
  2242. } else {
  2243. if (self::DEBUG > 1) {
  2244. error_log(
  2245. 'New LP - No multiplier',
  2246. 0
  2247. );
  2248. }
  2249. $list = explode(',', $gr);
  2250. $mycond = true;
  2251. foreach ($list as $cond) {
  2252. if (isset($items[$refs_list[$cond]])) {
  2253. $status = $items[$refs_list[$cond]]->get_status(true);
  2254. if ($status == $this->possible_status[2] ||
  2255. $status == $this->possible_status[3]
  2256. ) {
  2257. $mycond = true;
  2258. if (self::DEBUG > 1) {
  2259. error_log(
  2260. 'New LP - Found true item',
  2261. 0
  2262. );
  2263. }
  2264. } else {
  2265. if (self::DEBUG > 1) {
  2266. error_log(
  2267. 'New LP - '.
  2268. ' Found false item, the set is not true, return false',
  2269. 0
  2270. );
  2271. }
  2272. $mycond = false;
  2273. break;
  2274. }
  2275. } else {
  2276. if (self::DEBUG > 1) {
  2277. error_log(
  2278. 'New LP - item '.$cond.' does not exist in items list',
  2279. 0
  2280. );
  2281. }
  2282. if (self::DEBUG > 1) {
  2283. error_log(
  2284. 'New LP - Found false item, the set is not true, return false',
  2285. 0
  2286. );
  2287. }
  2288. $mycond = false;
  2289. break;
  2290. }
  2291. }
  2292. }
  2293. if (!$mycond && empty($this->prereq_alert)) {
  2294. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2295. }
  2296. return $mycond;
  2297. }
  2298. } else {
  2299. // Nothing found there either. Now return the
  2300. // value of the corresponding resource completion status.
  2301. if (self::DEBUG > 1) {
  2302. error_log(
  2303. 'New LP - Didnt find any group, returning value for '.$prereqs_string,
  2304. 0
  2305. );
  2306. }
  2307. if (isset($refs_list[$prereqs_string]) &&
  2308. isset($items[$refs_list[$prereqs_string]])
  2309. ) {
  2310. /** @var learnpathItem $itemToCheck */
  2311. $itemToCheck = $items[$refs_list[$prereqs_string]];
  2312. if ($itemToCheck->type === 'quiz') {
  2313. // 1. Checking the status in current items.
  2314. $status = $itemToCheck->get_status(true);
  2315. $returnstatus = $status == $this->possible_status[2] || $status == $this->possible_status[3];
  2316. if (!$returnstatus) {
  2317. $explanation = sprintf(
  2318. get_lang('ItemXBlocksThisElement'),
  2319. $itemToCheck->get_title()
  2320. );
  2321. $this->prereq_alert = $explanation;
  2322. if (self::DEBUG > 1) {
  2323. error_log(
  2324. 'New LP - Prerequisite '.$prereqs_string.' not complete',
  2325. 0
  2326. );
  2327. }
  2328. } else {
  2329. if (self::DEBUG > 1) {
  2330. error_log(
  2331. 'New LP - Prerequisite '.$prereqs_string.' complete',
  2332. 0
  2333. );
  2334. }
  2335. }
  2336. // For one and first attempt.
  2337. if ($this->prevent_reinit == 1) {
  2338. // 2. If is completed we check the results in the DB of the quiz.
  2339. if ($returnstatus) {
  2340. //AND origin_lp_item_id = '.$user_id.'
  2341. $sql = 'SELECT exe_result, exe_weighting
  2342. FROM '.Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES).'
  2343. WHERE
  2344. exe_exo_id = '.$items[$refs_list[$prereqs_string]]->path.' AND
  2345. exe_user_id = '.$user_id.' AND
  2346. orig_lp_id = '.$this->lp_id.' AND
  2347. orig_lp_item_id = '.$prereqs_string.' AND
  2348. status <> "incomplete" AND
  2349. c_id = '.$course_id.'
  2350. ORDER BY exe_date DESC
  2351. LIMIT 0, 1';
  2352. $rs_quiz = Database::query($sql);
  2353. if ($quiz = Database::fetch_array($rs_quiz)) {
  2354. /** @var learnpathItem $myItemToCheck */
  2355. $myItemToCheck = $items[$refs_list[$this->get_id()]];
  2356. $minScore = $myItemToCheck->getPrerequisiteMinScore();
  2357. $maxScore = $myItemToCheck->getPrerequisiteMaxScore();
  2358. if (empty($minScore)) {
  2359. // Try with mastery_score
  2360. $masteryScoreAsMin = $myItemToCheck->get_mastery_score();
  2361. if (!empty($masteryScoreAsMin)) {
  2362. $minScore = $masteryScoreAsMin;
  2363. }
  2364. }
  2365. if (isset($minScore) && isset($minScore)) {
  2366. // Taking min/max prerequisites values see BT#5776
  2367. if ($quiz['exe_result'] >= $minScore &&
  2368. $quiz['exe_result'] <= $maxScore
  2369. ) {
  2370. $returnstatus = true;
  2371. } else {
  2372. $explanation = sprintf(
  2373. get_lang('YourResultAtXBlocksThisElement'),
  2374. $itemToCheck->get_title()
  2375. );
  2376. $this->prereq_alert = $explanation;
  2377. $returnstatus = false;
  2378. }
  2379. } else {
  2380. // Classic way
  2381. if ($quiz['exe_result'] >=
  2382. $items[$refs_list[$prereqs_string]]->get_mastery_score()
  2383. ) {
  2384. $returnstatus = true;
  2385. } else {
  2386. $explanation = sprintf(
  2387. get_lang('YourResultAtXBlocksThisElement'),
  2388. $itemToCheck->get_title()
  2389. );
  2390. $this->prereq_alert = $explanation;
  2391. $returnstatus = false;
  2392. }
  2393. }
  2394. } else {
  2395. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2396. $returnstatus = false;
  2397. }
  2398. }
  2399. } else {
  2400. // 3. For multiple attempts we check that there are minimum 1 item completed
  2401. // Checking in the database.
  2402. $sql = 'SELECT exe_result, exe_weighting
  2403. FROM '.Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES).'
  2404. WHERE
  2405. c_id = '.$course_id.' AND
  2406. exe_exo_id = '.$items[$refs_list[$prereqs_string]]->path.' AND
  2407. exe_user_id = '.$user_id.' AND
  2408. orig_lp_id = '.$this->lp_id.' AND
  2409. orig_lp_item_id = '.$prereqs_string;
  2410. $rs_quiz = Database::query($sql);
  2411. if (Database::num_rows($rs_quiz) > 0) {
  2412. while ($quiz = Database::fetch_array($rs_quiz)) {
  2413. /** @var learnpathItem $myItemToCheck */
  2414. $myItemToCheck = $items[$refs_list[$this->get_id()]];
  2415. $minScore = $myItemToCheck->getPrerequisiteMinScore();
  2416. $maxScore = $myItemToCheck->getPrerequisiteMaxScore();
  2417. if (empty($minScore)) {
  2418. // Try with mastery_score
  2419. $masteryScoreAsMin = $myItemToCheck->get_mastery_score();
  2420. if (!empty($masteryScoreAsMin)) {
  2421. $minScore = $masteryScoreAsMin;
  2422. }
  2423. }
  2424. if (isset($minScore) && isset($minScore)) {
  2425. // Taking min/max prerequisites values see BT#5776
  2426. if ($quiz['exe_result'] >= $minScore &&
  2427. $quiz['exe_result'] <= $maxScore
  2428. ) {
  2429. $returnstatus = true;
  2430. break;
  2431. } else {
  2432. $explanation = sprintf(
  2433. get_lang('YourResultAtXBlocksThisElement'),
  2434. $itemToCheck->get_title()
  2435. );
  2436. $this->prereq_alert = $explanation;
  2437. $returnstatus = false;
  2438. }
  2439. } else {
  2440. if ($quiz['exe_result'] >=
  2441. $items[$refs_list[$prereqs_string]]->get_mastery_score()
  2442. ) {
  2443. $returnstatus = true;
  2444. break;
  2445. } else {
  2446. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2447. $returnstatus = false;
  2448. }
  2449. }
  2450. }
  2451. } else {
  2452. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2453. $returnstatus = false;
  2454. }
  2455. }
  2456. return $returnstatus;
  2457. } elseif ($itemToCheck->type === 'student_publication') {
  2458. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  2459. $workId = $items[$refs_list[$prereqs_string]]->path;
  2460. $count = get_work_count_by_student($user_id, $workId);
  2461. if ($count >= 1) {
  2462. $returnstatus = true;
  2463. } else {
  2464. $returnstatus = false;
  2465. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2466. if (self::DEBUG > 1) {
  2467. error_log(
  2468. 'Student pub, prereq'.$prereqs_string.' not completed',
  2469. 0
  2470. );
  2471. }
  2472. }
  2473. return $returnstatus;
  2474. } else {
  2475. $status = $itemToCheck->get_status(false);
  2476. $returnstatus = $status == $this->possible_status[2] || $status == $this->possible_status[3];
  2477. if (!$returnstatus) {
  2478. $explanation = sprintf(
  2479. get_lang('ItemXBlocksThisElement'),
  2480. $itemToCheck->get_title()
  2481. );
  2482. $this->prereq_alert = $explanation;
  2483. if (self::DEBUG > 1) {
  2484. error_log(
  2485. 'New LP - Prerequisite '.$prereqs_string.' not complete',
  2486. 0
  2487. );
  2488. }
  2489. } else {
  2490. if (self::DEBUG > 1) {
  2491. error_log(
  2492. 'New LP - Prerequisite '.$prereqs_string.' complete',
  2493. 0
  2494. );
  2495. }
  2496. }
  2497. if ($returnstatus && $this->prevent_reinit == 1) {
  2498. // I would prefer check in the database.
  2499. $lp_item_view = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  2500. $lp_view = Database::get_course_table(TABLE_LP_VIEW);
  2501. $sql = 'SELECT iid FROM '.$lp_view.'
  2502. WHERE
  2503. c_id = '.$course_id.' AND
  2504. user_id = '.$user_id.' AND
  2505. lp_id = '.$this->lp_id.' AND
  2506. session_id = '.$sessionId.'
  2507. LIMIT 0, 1';
  2508. $rs_lp = Database::query($sql);
  2509. if (Database::num_rows($rs_lp)) {
  2510. $lp_id = Database::fetch_row($rs_lp);
  2511. $my_lp_id = $lp_id[0];
  2512. $sql = 'SELECT status FROM '.$lp_item_view.'
  2513. WHERE
  2514. c_id = '.$course_id.' AND
  2515. lp_view_id = '.$my_lp_id.' AND
  2516. lp_item_id = '.$refs_list[$prereqs_string].'
  2517. LIMIT 0, 1';
  2518. $rs_lp = Database::query($sql);
  2519. $status_array = Database::fetch_row($rs_lp);
  2520. $status = $status_array[0];
  2521. $returnstatus = $status == $this->possible_status[2] || $status == $this->possible_status[3];
  2522. if (!$returnstatus && empty($this->prereq_alert)) {
  2523. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2524. }
  2525. if (!$returnstatus) {
  2526. if (self::DEBUG > 1) {
  2527. error_log(
  2528. 'New LP - Prerequisite '.$prereqs_string.' not complete'
  2529. );
  2530. }
  2531. } else {
  2532. if (self::DEBUG > 1) {
  2533. error_log('New LP - Prerequisite '.$prereqs_string.' complete');
  2534. }
  2535. }
  2536. return $returnstatus;
  2537. }
  2538. } else {
  2539. return $returnstatus;
  2540. }
  2541. }
  2542. } else {
  2543. if (self::DEBUG > 1) {
  2544. error_log(
  2545. 'New LP - Could not find '.$prereqs_string.' in '.print_r(
  2546. $refs_list,
  2547. true
  2548. ),
  2549. 0
  2550. );
  2551. }
  2552. }
  2553. }
  2554. }
  2555. }
  2556. }
  2557. }
  2558. } else {
  2559. $list = explode("\|", $prereqs_string);
  2560. if (count($list) > 1) {
  2561. if (self::DEBUG > 1) {
  2562. error_log('New LP - Found OR, looking into it', 0);
  2563. }
  2564. $orstatus = false;
  2565. foreach ($list as $condition) {
  2566. if (self::DEBUG > 1) {
  2567. error_log(
  2568. 'New LP - Found OR, adding it ('.$condition.')',
  2569. 0
  2570. );
  2571. }
  2572. $orstatus = $orstatus || $this->parse_prereq(
  2573. $condition,
  2574. $items,
  2575. $refs_list,
  2576. $user_id
  2577. );
  2578. if ($orstatus) {
  2579. // Shortcircuit OR.
  2580. if (self::DEBUG > 1) {
  2581. error_log(
  2582. 'New LP - One condition in OR was true, short-circuit',
  2583. 0
  2584. );
  2585. }
  2586. break;
  2587. }
  2588. }
  2589. if (!$orstatus && empty($this->prereq_alert)) {
  2590. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2591. }
  2592. return $orstatus;
  2593. } else {
  2594. if (self::DEBUG > 1) {
  2595. error_log(
  2596. 'New LP - OR was found but only one elem present !?',
  2597. 0
  2598. );
  2599. }
  2600. if (isset($items[$refs_list[$list[0]]])) {
  2601. $status = $items[$refs_list[$list[0]]]->get_status(true);
  2602. $returnstatus = $status == 'completed' || $status == 'passed';
  2603. if (!$returnstatus && empty($this->prereq_alert)) {
  2604. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2605. }
  2606. return $returnstatus;
  2607. }
  2608. }
  2609. }
  2610. if (empty($this->prereq_alert)) {
  2611. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2612. }
  2613. if (self::DEBUG > 1) {
  2614. error_log(
  2615. 'New LP - End of parse_prereq. Error code is now '.$this->prereq_alert,
  2616. 0
  2617. );
  2618. }
  2619. return false;
  2620. }
  2621. /**
  2622. * Reinits all local values as the learnpath is restarted.
  2623. *
  2624. * @return bool True on success, false otherwise
  2625. */
  2626. public function restart()
  2627. {
  2628. if (self::DEBUG > 0) {
  2629. error_log('learnpathItem::restart()', 0);
  2630. }
  2631. $seriousGame = $this->get_seriousgame_mode();
  2632. //For serious game : We reuse same attempt_id
  2633. if ($seriousGame == 1 && $this->type == 'sco') {
  2634. // If this is a sco, Chamilo can't update the time without an
  2635. // explicit scorm call
  2636. $this->current_start_time = 0;
  2637. $this->current_stop_time = 0; //Those 0 value have this effect
  2638. $this->last_scorm_session_time = 0;
  2639. $this->save();
  2640. return true;
  2641. }
  2642. $this->save();
  2643. $allowed = $this->isRestartAllowed();
  2644. if ($allowed === -1) {
  2645. // Nothing allowed, do nothing.
  2646. } elseif ($allowed === 1) {
  2647. // Restart as new attempt is allowed, record a new attempt.
  2648. $this->attempt_id = $this->attempt_id + 1; // Simply reuse the previous attempt_id.
  2649. $this->current_score = 0;
  2650. $this->current_start_time = 0;
  2651. $this->current_stop_time = 0;
  2652. $this->current_data = '';
  2653. $this->status = $this->possible_status[0];
  2654. $this->interactions_count = 0;
  2655. $this->interactions = [];
  2656. $this->objectives_count = 0;
  2657. $this->objectives = [];
  2658. $this->lesson_location = '';
  2659. if ($this->type != TOOL_QUIZ) {
  2660. $this->write_to_db();
  2661. }
  2662. } else {
  2663. // Restart current element is allowed (because it's not finished yet),
  2664. // reinit current.
  2665. //$this->current_score = 0;
  2666. $this->current_start_time = 0;
  2667. $this->current_stop_time = 0;
  2668. $this->interactions_count = $this->get_interactions_count(true);
  2669. }
  2670. return true;
  2671. }
  2672. /**
  2673. * Saves data in the database.
  2674. *
  2675. * @param bool $from_outside Save from URL params (1) or from object attributes (0)
  2676. * @param bool $prereqs_complete The results of a check on prerequisites for this item.
  2677. * True if prerequisites are completed, false otherwise. Defaults to false. Only used if not sco or au
  2678. *
  2679. * @return bool True on success, false on failure
  2680. */
  2681. public function save($from_outside = true, $prereqs_complete = false)
  2682. {
  2683. $debug = self::DEBUG;
  2684. if ($debug) {
  2685. error_log('learnpathItem::save()', 0);
  2686. }
  2687. // First check if parameters passed via GET can be saved here
  2688. // in case it's a SCORM, we should get:
  2689. if ($this->type == 'sco' || $this->type == 'au') {
  2690. $status = $this->get_status(true);
  2691. if ($this->prevent_reinit == 1 &&
  2692. $status != $this->possible_status[0] && // not attempted
  2693. $status != $this->possible_status[1] //incomplete
  2694. ) {
  2695. if ($debug) {
  2696. error_log(
  2697. 'learnpathItem::save() - save reinit blocked by setting',
  2698. 0
  2699. );
  2700. }
  2701. // Do nothing because the status has already been set. Don't allow it to change.
  2702. // TODO: Check there isn't a special circumstance where this should be saved.
  2703. } else {
  2704. if ($debug) {
  2705. error_log(
  2706. 'learnpathItem::save() - SCORM save request received',
  2707. 0
  2708. );
  2709. }
  2710. // Get all new settings from the URL
  2711. if ($from_outside) {
  2712. if ($debug) {
  2713. error_log(
  2714. 'learnpathItem::save() - Getting item data from outside',
  2715. 0
  2716. );
  2717. }
  2718. foreach ($_GET as $param => $value) {
  2719. switch ($param) {
  2720. case 'score':
  2721. $this->set_score($value);
  2722. if ($debug) {
  2723. error_log(
  2724. 'learnpathItem::save() - setting score to '.$value,
  2725. 0
  2726. );
  2727. }
  2728. break;
  2729. case 'max':
  2730. $this->set_max_score($value);
  2731. if ($debug) {
  2732. error_log(
  2733. 'learnpathItem::save() - setting view_max_score to '.$value,
  2734. 0
  2735. );
  2736. }
  2737. break;
  2738. case 'min':
  2739. $this->min_score = $value;
  2740. if ($debug) {
  2741. error_log(
  2742. 'learnpathItem::save() - setting min_score to '.$value,
  2743. 0
  2744. );
  2745. }
  2746. break;
  2747. case 'lesson_status':
  2748. if (!empty($value)) {
  2749. $this->set_status($value);
  2750. if ($debug) {
  2751. error_log(
  2752. 'learnpathItem::save() - setting status to '.$value,
  2753. 0
  2754. );
  2755. }
  2756. }
  2757. break;
  2758. case 'time':
  2759. $this->set_time($value);
  2760. if ($debug) {
  2761. error_log(
  2762. 'learnpathItem::save() - setting time to '.$value,
  2763. 0
  2764. );
  2765. }
  2766. break;
  2767. case 'suspend_data':
  2768. $this->current_data = $value;
  2769. if ($debug) {
  2770. error_log(
  2771. 'learnpathItem::save() - setting suspend_data to '.$value,
  2772. 0
  2773. );
  2774. }
  2775. break;
  2776. case 'lesson_location':
  2777. $this->set_lesson_location($value);
  2778. if ($debug) {
  2779. error_log(
  2780. 'learnpathItem::save() - setting lesson_location to '.$value,
  2781. 0
  2782. );
  2783. }
  2784. break;
  2785. case 'core_exit':
  2786. $this->set_core_exit($value);
  2787. if ($debug) {
  2788. error_log(
  2789. 'learnpathItem::save() - setting core_exit to '.$value,
  2790. 0
  2791. );
  2792. }
  2793. break;
  2794. case 'interactions':
  2795. break;
  2796. case 'objectives':
  2797. break;
  2798. default:
  2799. // Ignore.
  2800. break;
  2801. }
  2802. }
  2803. } else {
  2804. if ($debug) {
  2805. error_log(
  2806. 'learnpathItem::save() - Using inside item status',
  2807. 0
  2808. );
  2809. }
  2810. // Do nothing, just let the local attributes be used.
  2811. }
  2812. }
  2813. } else {
  2814. // If not SCO, such messages should not be expected.
  2815. $type = strtolower($this->type);
  2816. if ($debug) {
  2817. error_log("type: $type");
  2818. }
  2819. switch ($type) {
  2820. case 'asset':
  2821. if ($prereqs_complete) {
  2822. $this->set_status($this->possible_status[2]);
  2823. }
  2824. break;
  2825. case TOOL_HOTPOTATOES:
  2826. break;
  2827. case TOOL_QUIZ:
  2828. return false;
  2829. break;
  2830. default:
  2831. // For now, everything that is not sco and not asset is set to
  2832. // completed when saved.
  2833. if ($prereqs_complete) {
  2834. $this->set_status($this->possible_status[2]);
  2835. }
  2836. break;
  2837. }
  2838. }
  2839. if ($debug) {
  2840. error_log(
  2841. 'New LP - End of learnpathItem::save() - Calling write_to_db()',
  2842. 0
  2843. );
  2844. }
  2845. return $this->write_to_db();
  2846. }
  2847. /**
  2848. * Sets the number of attempt_id to a given value.
  2849. *
  2850. * @param int $num The given value to set attempt_id to
  2851. *
  2852. * @return bool TRUE on success, FALSE otherwise
  2853. */
  2854. public function set_attempt_id($num)
  2855. {
  2856. if (self::DEBUG > 0) {
  2857. error_log('learnpathItem::set_attempt_id()', 0);
  2858. }
  2859. if ($num == strval(intval($num)) && $num >= 0) {
  2860. $this->attempt_id = $num;
  2861. return true;
  2862. }
  2863. return false;
  2864. }
  2865. /**
  2866. * Sets the core_exit value to the one given.
  2867. *
  2868. * @return bool $value True (always)
  2869. */
  2870. public function set_core_exit($value)
  2871. {
  2872. switch ($value) {
  2873. case '':
  2874. $this->core_exit = '';
  2875. break;
  2876. case 'suspend':
  2877. $this->core_exit = 'suspend';
  2878. break;
  2879. default:
  2880. $this->core_exit = 'none';
  2881. break;
  2882. }
  2883. return true;
  2884. }
  2885. /**
  2886. * Sets the item's description.
  2887. *
  2888. * @param string $string Description
  2889. */
  2890. public function set_description($string = '')
  2891. {
  2892. if (self::DEBUG > 0) {
  2893. error_log('learnpathItem::set_description()', 0);
  2894. }
  2895. if (!empty($string)) {
  2896. $this->description = $string;
  2897. }
  2898. }
  2899. /**
  2900. * Sets the lesson_location value.
  2901. *
  2902. * @param string $location lesson_location as provided by the SCO
  2903. *
  2904. * @return bool True on success, false otherwise
  2905. */
  2906. public function set_lesson_location($location)
  2907. {
  2908. if (self::DEBUG > 0) {
  2909. error_log('learnpathItem::set_lesson_location()', 0);
  2910. }
  2911. if (isset($location)) {
  2912. $this->lesson_location = $location;
  2913. return true;
  2914. }
  2915. return false;
  2916. }
  2917. /**
  2918. * Sets the item's depth level in the LP tree (0 is at root).
  2919. *
  2920. * @param int $int Level
  2921. */
  2922. public function set_level($int = 0)
  2923. {
  2924. if (self::DEBUG > 0) {
  2925. error_log('learnpathItem::set_level('.$int.')', 0);
  2926. }
  2927. if (!empty($int) && $int == strval(intval($int))) {
  2928. $this->level = $int;
  2929. }
  2930. }
  2931. /**
  2932. * Sets the lp_view id this item view is registered to.
  2933. *
  2934. * @param int $lp_view_id lp_view DB ID
  2935. * @param int $course_id
  2936. *
  2937. * @return bool
  2938. *
  2939. * @todo //todo insert into lp_item_view if lp_view not exists
  2940. */
  2941. public function set_lp_view($lp_view_id, $course_id = null)
  2942. {
  2943. $lp_view_id = intval($lp_view_id);
  2944. if (empty($course_id)) {
  2945. $course_id = api_get_course_int_id();
  2946. } else {
  2947. $course_id = intval($course_id);
  2948. }
  2949. $lpItemId = $this->get_id();
  2950. if (empty($lpItemId)) {
  2951. if (self::DEBUG > 0) {
  2952. error_log(
  2953. 'learnpathItem::set_lp_view('.$lp_view_id.') $lpItemId is empty',
  2954. 0
  2955. );
  2956. }
  2957. return false;
  2958. }
  2959. if (empty($lp_view_id)) {
  2960. if (self::DEBUG > 0) {
  2961. error_log(
  2962. 'learnpathItem::set_lp_view('.$lp_view_id.') $lp_view_id is empty',
  2963. 0
  2964. );
  2965. }
  2966. return false;
  2967. }
  2968. if (self::DEBUG > 0) {
  2969. error_log('learnpathItem::set_lp_view('.$lp_view_id.')', 0);
  2970. }
  2971. $this->view_id = $lp_view_id;
  2972. $item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  2973. // Get the lp_item_view with the highest view_count.
  2974. $sql = "SELECT * FROM $item_view_table
  2975. WHERE
  2976. c_id = $course_id AND
  2977. lp_item_id = ".$lpItemId." AND
  2978. lp_view_id = ".$lp_view_id."
  2979. ORDER BY view_count DESC";
  2980. if (self::DEBUG > 2) {
  2981. error_log(
  2982. 'learnpathItem::set_lp_view() - Querying lp_item_view: '.$sql,
  2983. 0
  2984. );
  2985. }
  2986. $res = Database::query($sql);
  2987. if (Database::num_rows($res) > 0) {
  2988. $row = Database::fetch_array($res);
  2989. $this->db_item_view_id = $row['iid'];
  2990. $this->attempt_id = $row['view_count'];
  2991. $this->current_score = $row['score'];
  2992. $this->current_data = $row['suspend_data'];
  2993. $this->view_max_score = $row['max_score'];
  2994. $this->status = $row['status'];
  2995. $this->current_start_time = $row['start_time'];
  2996. $this->current_stop_time = $this->current_start_time + $row['total_time'];
  2997. $this->lesson_location = $row['lesson_location'];
  2998. $this->core_exit = $row['core_exit'];
  2999. if (self::DEBUG > 2) {
  3000. error_log(
  3001. 'learnpathItem::set_lp_view() - Updated item object with database values',
  3002. 0
  3003. );
  3004. }
  3005. // Now get the number of interactions for this little guy.
  3006. $table = Database::get_course_table(TABLE_LP_IV_INTERACTION);
  3007. $sql = "SELECT * FROM $table
  3008. WHERE
  3009. c_id = $course_id AND
  3010. lp_iv_id = '".$this->db_item_view_id."'";
  3011. $res = Database::query($sql);
  3012. if ($res !== false) {
  3013. $this->interactions_count = Database::num_rows($res);
  3014. } else {
  3015. $this->interactions_count = 0;
  3016. }
  3017. // Now get the number of objectives for this little guy.
  3018. $table = Database::get_course_table(TABLE_LP_IV_OBJECTIVE);
  3019. $sql = "SELECT * FROM $table
  3020. WHERE
  3021. c_id = $course_id AND
  3022. lp_iv_id = '".$this->db_item_view_id."'";
  3023. $res = Database::query($sql);
  3024. if ($res !== false) {
  3025. $this->objectives_count = Database::num_rows($res);
  3026. } else {
  3027. $this->objectives_count = 0;
  3028. }
  3029. }
  3030. // End
  3031. if (self::DEBUG > 2) {
  3032. error_log('New LP - End of learnpathItem::set_lp_view()', 0);
  3033. }
  3034. return true;
  3035. }
  3036. /**
  3037. * Sets the path.
  3038. *
  3039. * @param string $string Path
  3040. */
  3041. public function set_path($string = '')
  3042. {
  3043. if (self::DEBUG > 0) {
  3044. error_log('learnpathItem::set_path()', 0);
  3045. }
  3046. if (!empty($string)) {
  3047. $this->path = $string;
  3048. }
  3049. }
  3050. /**
  3051. * Sets the prevent_reinit attribute.
  3052. * This is based on the LP value and is set at creation time for
  3053. * each learnpathItem. It is a (bad?) way of avoiding
  3054. * a reference to the LP when saving an item.
  3055. *
  3056. * @param int 1 for "prevent", 0 for "don't prevent"
  3057. * saving freshened values (new "not attempted" status etc)
  3058. */
  3059. public function set_prevent_reinit($prevent)
  3060. {
  3061. if (self::DEBUG > 0) {
  3062. error_log('learnpathItem::set_prevent_reinit()', 0);
  3063. }
  3064. if ($prevent) {
  3065. $this->prevent_reinit = 1;
  3066. } else {
  3067. $this->prevent_reinit = 0;
  3068. }
  3069. }
  3070. /**
  3071. * Sets the score value. If the mastery_score is set and the score reaches
  3072. * it, then set the status to 'passed'.
  3073. *
  3074. * @param float $score Score
  3075. *
  3076. * @return bool True on success, false otherwise
  3077. */
  3078. public function set_score($score)
  3079. {
  3080. $debug = self::DEBUG;
  3081. if ($debug > 0) {
  3082. error_log('learnpathItem::set_score('.$score.')', 0);
  3083. }
  3084. if (($this->max_score <= 0 || $score <= $this->max_score) && ($score >= $this->min_score)) {
  3085. $this->current_score = $score;
  3086. $masteryScore = $this->get_mastery_score();
  3087. $current_status = $this->get_status(false);
  3088. // Fixes bug when SCORM doesn't send a mastery score even if they sent a score!
  3089. if ($masteryScore == -1) {
  3090. $masteryScore = $this->max_score;
  3091. }
  3092. if ($debug > 0) {
  3093. error_log('get_mastery_score: '.$masteryScore);
  3094. error_log('current_status: '.$current_status);
  3095. error_log('current score : '.$this->current_score);
  3096. }
  3097. // If mastery_score is set AND the current score reaches the mastery
  3098. // score AND the current status is different from 'completed', then
  3099. // set it to 'passed'.
  3100. /*
  3101. if ($master != -1 && $this->current_score >= $master && $current_status != $this->possible_status[2]) {
  3102. if ($debug > 0) error_log('Status changed to: '.$this->possible_status[3]);
  3103. $this->set_status($this->possible_status[3]); //passed
  3104. } elseif ($master != -1 && $this->current_score < $master) {
  3105. if ($debug > 0) error_log('Status changed to: '.$this->possible_status[4]);
  3106. $this->set_status($this->possible_status[4]); //failed
  3107. }*/
  3108. return true;
  3109. }
  3110. return false;
  3111. }
  3112. /**
  3113. * Sets the maximum score for this item.
  3114. *
  3115. * @param int $score Maximum score - must be a decimal or an empty string
  3116. *
  3117. * @return bool True on success, false on error
  3118. */
  3119. public function set_max_score($score)
  3120. {
  3121. if (self::DEBUG > 0) {
  3122. error_log('learnpathItem::set_max_score('.$score.')', 0);
  3123. }
  3124. if (is_int($score) || $score == '') {
  3125. $this->view_max_score = $score;
  3126. if (self::DEBUG > 1) {
  3127. error_log(
  3128. 'learnpathItem::set_max_score() - '.
  3129. 'Updated object score of item '.$this->db_id.
  3130. ' to '.$this->view_max_score,
  3131. 0
  3132. );
  3133. }
  3134. return true;
  3135. }
  3136. return false;
  3137. }
  3138. /**
  3139. * Sets the status for this item.
  3140. *
  3141. * @param string $status Status - must be one of the values defined in $this->possible_status
  3142. * (this affects the status setting)
  3143. *
  3144. * @return bool True on success, false on error
  3145. */
  3146. public function set_status($status)
  3147. {
  3148. if (self::DEBUG > 0) {
  3149. error_log('learnpathItem::set_status('.$status.')', 0);
  3150. }
  3151. $found = false;
  3152. foreach ($this->possible_status as $possible) {
  3153. if (preg_match('/^'.$possible.'$/i', $status)) {
  3154. $found = true;
  3155. }
  3156. }
  3157. if ($found) {
  3158. $this->status = $status;
  3159. if (self::DEBUG > 1) {
  3160. error_log(
  3161. 'learnpathItem::set_status() - '.
  3162. 'Updated object status of item '.$this->db_id.
  3163. ' to '.$this->status,
  3164. 0
  3165. );
  3166. }
  3167. return true;
  3168. }
  3169. $this->status = $this->possible_status[0];
  3170. return false;
  3171. }
  3172. /**
  3173. * Set the (indexing) terms for this learnpath item.
  3174. *
  3175. * @param string $terms Terms, as a comma-split list
  3176. *
  3177. * @return bool Always return true
  3178. */
  3179. public function set_terms($terms)
  3180. {
  3181. global $charset;
  3182. $lp_item = Database::get_course_table(TABLE_LP_ITEM);
  3183. $a_terms = preg_split('/,/', $terms);
  3184. $i_terms = preg_split('/,/', $this->get_terms());
  3185. foreach ($i_terms as $term) {
  3186. if (!in_array($term, $a_terms)) {
  3187. array_push($a_terms, $term);
  3188. }
  3189. }
  3190. $new_terms = $a_terms;
  3191. $new_terms_string = implode(',', $new_terms);
  3192. // TODO: Validate csv string.
  3193. $terms = Database::escape_string(api_htmlentities($new_terms_string, ENT_QUOTES, $charset));
  3194. $sql = "UPDATE $lp_item
  3195. SET terms = '$terms'
  3196. WHERE iid=".$this->get_id();
  3197. Database::query($sql);
  3198. // Save it to search engine.
  3199. if (api_get_setting('search_enabled') == 'true') {
  3200. $di = new ChamiloIndexer();
  3201. $di->update_terms($this->get_search_did(), $new_terms, 'T');
  3202. }
  3203. return true;
  3204. }
  3205. /**
  3206. * Get the document ID from inside the text index database.
  3207. *
  3208. * @return int Search index database document ID
  3209. */
  3210. public function get_search_did()
  3211. {
  3212. return $this->search_did;
  3213. }
  3214. /**
  3215. * Sets the item viewing time in a usable form, given that SCORM packages
  3216. * often give it as 00:00:00.0000.
  3217. *
  3218. * @param string Time as given by SCORM
  3219. * @param string $format
  3220. */
  3221. public function set_time($scorm_time, $format = 'scorm')
  3222. {
  3223. $debug = self::DEBUG;
  3224. if ($debug) {
  3225. error_log("learnpathItem::set_time($scorm_time, $format)");
  3226. error_log("this->type: ".$this->type);
  3227. error_log("this->current_start_time: ".$this->current_start_time);
  3228. }
  3229. if ($scorm_time == '0' &&
  3230. $this->type != 'sco' &&
  3231. $this->current_start_time != 0
  3232. ) {
  3233. $myTime = time() - $this->current_start_time;
  3234. if ($myTime > 0) {
  3235. $this->update_time($myTime);
  3236. if ($debug) {
  3237. error_log('found asset - set time to '.$myTime);
  3238. }
  3239. } else {
  3240. if ($debug) {
  3241. error_log('Time not set');
  3242. }
  3243. }
  3244. } else {
  3245. switch ($format) {
  3246. case 'scorm':
  3247. $res = [];
  3248. if (preg_match(
  3249. '/^(\d{1,4}):(\d{2}):(\d{2})(\.\d{1,4})?/',
  3250. $scorm_time,
  3251. $res
  3252. )
  3253. ) {
  3254. $hour = $res[1];
  3255. $min = $res[2];
  3256. $sec = $res[3];
  3257. // Getting total number of seconds spent.
  3258. $totalSec = $hour * 3600 + $min * 60 + $sec;
  3259. if ($debug) {
  3260. error_log("totalSec : $totalSec");
  3261. error_log("Now calling to scorm_update_time()");
  3262. }
  3263. $this->scorm_update_time($totalSec);
  3264. }
  3265. break;
  3266. case 'int':
  3267. if ($debug) {
  3268. error_log("scorm_time = $scorm_time");
  3269. error_log("Now calling to scorm_update_time()");
  3270. }
  3271. $this->scorm_update_time($scorm_time);
  3272. break;
  3273. }
  3274. }
  3275. }
  3276. /**
  3277. * Sets the item's title.
  3278. *
  3279. * @param string $string Title
  3280. */
  3281. public function set_title($string = '')
  3282. {
  3283. if (self::DEBUG > 0) {
  3284. error_log('learnpathItem::set_title()', 0);
  3285. }
  3286. if (!empty($string)) {
  3287. $this->title = $string;
  3288. }
  3289. }
  3290. /**
  3291. * Sets the item's type.
  3292. *
  3293. * @param string $string Type
  3294. */
  3295. public function set_type($string = '')
  3296. {
  3297. if (self::DEBUG > 0) {
  3298. error_log('learnpathItem::set_type()', 0);
  3299. }
  3300. if (!empty($string)) {
  3301. $this->type = $string;
  3302. }
  3303. }
  3304. /**
  3305. * Checks if the current status is part of the list of status given.
  3306. *
  3307. * @param array $list An array of status to check for.
  3308. * If the current status is one of the strings, return true
  3309. *
  3310. * @return bool True if the status was one of the given strings,
  3311. * false otherwise
  3312. */
  3313. public function status_is($list = [])
  3314. {
  3315. if (self::DEBUG > 1) {
  3316. error_log(
  3317. 'learnpathItem::status_is('.print_r(
  3318. $list,
  3319. true
  3320. ).') on item '.$this->db_id,
  3321. 0
  3322. );
  3323. }
  3324. $currentStatus = $this->get_status(true);
  3325. if (empty($currentStatus)) {
  3326. return false;
  3327. }
  3328. $found = false;
  3329. foreach ($list as $status) {
  3330. if (preg_match('/^'.$status.'$/i', $currentStatus)) {
  3331. if (self::DEBUG > 2) {
  3332. error_log(
  3333. 'New LP - learnpathItem::status_is() - Found status '.
  3334. $status.' corresponding to current status',
  3335. 0
  3336. );
  3337. }
  3338. $found = true;
  3339. return $found;
  3340. }
  3341. }
  3342. if (self::DEBUG > 2) {
  3343. error_log(
  3344. 'New LP - learnpathItem::status_is() - Status '.
  3345. $currentStatus.' did not match request',
  3346. 0
  3347. );
  3348. }
  3349. return $found;
  3350. }
  3351. /**
  3352. * Updates the time info according to the given session_time.
  3353. *
  3354. * @param int $totalSec Time in seconds
  3355. */
  3356. public function update_time($totalSec = 0)
  3357. {
  3358. if (self::DEBUG > 0) {
  3359. error_log('learnpathItem::update_time('.$totalSec.')');
  3360. }
  3361. if ($totalSec >= 0) {
  3362. // Getting start time from finish time. The only problem in the calculation is it might be
  3363. // modified by the scripts processing time.
  3364. $now = time();
  3365. $start = $now - $totalSec;
  3366. $this->current_start_time = $start;
  3367. $this->current_stop_time = $now;
  3368. }
  3369. }
  3370. /**
  3371. * Special scorm update time function. This function will update time
  3372. * directly into db for scorm objects.
  3373. *
  3374. * @param int $total_sec Total number of seconds
  3375. */
  3376. public function scorm_update_time($total_sec = 0)
  3377. {
  3378. $debug = self::DEBUG;
  3379. if ($debug) {
  3380. error_log('learnpathItem::scorm_update_time()');
  3381. error_log("total_sec: $total_sec");
  3382. }
  3383. // Step 1 : get actual total time stored in db
  3384. $item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3385. $course_id = api_get_course_int_id();
  3386. $sql = 'SELECT total_time, status
  3387. FROM '.$item_view_table.'
  3388. WHERE
  3389. c_id = '.$course_id.' AND
  3390. lp_item_id = "'.$this->db_id.'" AND
  3391. lp_view_id = "'.$this->view_id.'" AND
  3392. view_count = "'.$this->get_attempt_id().'"';
  3393. $result = Database::query($sql);
  3394. $row = Database::fetch_array($result);
  3395. if (!isset($row['total_time'])) {
  3396. $total_time = 0;
  3397. } else {
  3398. $total_time = $row['total_time'];
  3399. }
  3400. if ($debug) {
  3401. error_log("Original total_time: $total_time");
  3402. }
  3403. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3404. $lp_id = (int) $this->lp_id;
  3405. $sql = "SELECT * FROM $lp_table WHERE iid = $lp_id";
  3406. $res = Database::query($sql);
  3407. $accumulateScormTime = 'false';
  3408. if (Database::num_rows($res) > 0) {
  3409. $row = Database::fetch_assoc($res);
  3410. $accumulateScormTime = $row['accumulate_scorm_time'];
  3411. }
  3412. // Step 2.1 : if normal mode total_time = total_time + total_sec
  3413. if ($this->type == 'sco' && $accumulateScormTime != 0) {
  3414. if ($debug) {
  3415. error_log("accumulateScormTime is on. total_time modified: $total_time + $total_sec");
  3416. }
  3417. $total_time += $total_sec;
  3418. } else {
  3419. // Step 2.2 : if not cumulative mode total_time = total_time - last_update + total_sec
  3420. $total_sec = $this->fixAbusiveTime($total_sec);
  3421. if ($debug) {
  3422. error_log("after fix abusive: $total_sec");
  3423. error_log("total_time: $total_time");
  3424. error_log("this->last_scorm_session_time: ".$this->last_scorm_session_time);
  3425. }
  3426. $total_time = $total_time - $this->last_scorm_session_time + $total_sec;
  3427. $this->last_scorm_session_time = $total_sec;
  3428. if ($total_time < 0) {
  3429. $total_time = $total_sec;
  3430. }
  3431. }
  3432. if ($debug) {
  3433. error_log("accumulate_scorm_time: $accumulateScormTime");
  3434. error_log("total_time modified: $total_time");
  3435. }
  3436. // Step 3 update db only if status != completed, passed, browsed or seriousgamemode not activated
  3437. // @todo complete
  3438. $case_completed = [
  3439. 'completed',
  3440. 'passed',
  3441. 'browsed',
  3442. 'failed',
  3443. ];
  3444. if ($this->seriousgame_mode != 1 ||
  3445. !in_array($row['status'], $case_completed)
  3446. ) {
  3447. $sql = "UPDATE $item_view_table
  3448. SET total_time = '$total_time'
  3449. WHERE
  3450. c_id = $course_id AND
  3451. lp_item_id = {$this->db_id} AND
  3452. lp_view_id = {$this->view_id} AND
  3453. view_count = {$this->get_attempt_id()}";
  3454. if ($debug) {
  3455. error_log('-------------total_time updated ------------------------');
  3456. error_log($sql);
  3457. error_log('-------------------------------------');
  3458. }
  3459. Database::query($sql);
  3460. }
  3461. }
  3462. /**
  3463. * Set the total_time to 0 into db.
  3464. */
  3465. public function scorm_init_time()
  3466. {
  3467. $table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3468. $course_id = api_get_course_int_id();
  3469. $sql = 'UPDATE '.$table.'
  3470. SET total_time = 0,
  3471. start_time = '.time().'
  3472. WHERE
  3473. c_id = '.$course_id.' AND
  3474. lp_item_id = "'.$this->db_id.'" AND
  3475. lp_view_id = "'.$this->view_id.'" AND
  3476. view_count = "'.$this->attempt_id.'"';
  3477. Database::query($sql);
  3478. }
  3479. /**
  3480. * Write objectives to DB. This method is separate from write_to_db() because otherwise
  3481. * objectives are lost as a side effect to AJAX and session concurrent access.
  3482. *
  3483. * @return bool True or false on error
  3484. */
  3485. public function write_objectives_to_db()
  3486. {
  3487. if (self::DEBUG > 0) {
  3488. error_log('learnpathItem::write_objectives_to_db()', 0);
  3489. }
  3490. $course_id = api_get_course_int_id();
  3491. if (is_array($this->objectives) && count($this->objectives) > 0) {
  3492. // Save objectives.
  3493. $tbl = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3494. $sql = "SELECT iid
  3495. FROM $tbl
  3496. WHERE
  3497. c_id = $course_id AND
  3498. lp_item_id = ".$this->db_id." AND
  3499. lp_view_id = ".$this->view_id." AND
  3500. view_count = ".$this->attempt_id;
  3501. $res = Database::query($sql);
  3502. if (Database::num_rows($res) > 0) {
  3503. $row = Database::fetch_array($res);
  3504. $lp_iv_id = $row[0];
  3505. if (self::DEBUG > 2) {
  3506. error_log(
  3507. 'learnpathItem::write_to_db() - Got item_view_id '.
  3508. $lp_iv_id.', now checking objectives ',
  3509. 0
  3510. );
  3511. }
  3512. foreach ($this->objectives as $index => $objective) {
  3513. $iva_table = Database::get_course_table(
  3514. TABLE_LP_IV_OBJECTIVE
  3515. );
  3516. $iva_sql = "SELECT iid FROM $iva_table
  3517. WHERE
  3518. c_id = $course_id AND
  3519. lp_iv_id = $lp_iv_id AND
  3520. objective_id = '".Database::escape_string($objective[0])."'";
  3521. $iva_res = Database::query($iva_sql);
  3522. // id(0), type(1), time(2), weighting(3),
  3523. // correct_responses(4), student_response(5),
  3524. // result(6), latency(7)
  3525. if (Database::num_rows($iva_res) > 0) {
  3526. // Update (or don't).
  3527. $iva_row = Database::fetch_array($iva_res);
  3528. $iva_id = $iva_row[0];
  3529. $ivau_sql = "UPDATE $iva_table ".
  3530. "SET objective_id = '".Database::escape_string($objective[0])."',".
  3531. "status = '".Database::escape_string($objective[1])."',".
  3532. "score_raw = '".Database::escape_string($objective[2])."',".
  3533. "score_min = '".Database::escape_string($objective[4])."',".
  3534. "score_max = '".Database::escape_string($objective[3])."' ".
  3535. "WHERE c_id = $course_id AND iid = $iva_id";
  3536. Database::query($ivau_sql);
  3537. } else {
  3538. // Insert new one.
  3539. $params = [
  3540. 'c_id' => $course_id,
  3541. 'lp_iv_id' => $lp_iv_id,
  3542. 'order_id' => $index,
  3543. 'objective_id' => $objective[0],
  3544. 'status' => $objective[1],
  3545. 'score_raw' => $objective[2],
  3546. 'score_min' => $objective[4],
  3547. 'score_max' => $objective[3],
  3548. ];
  3549. $insertId = Database::insert($iva_table, $params);
  3550. if ($insertId) {
  3551. $sql = "UPDATE $iva_table SET id = iid
  3552. WHERE iid = $insertId";
  3553. Database::query($sql);
  3554. }
  3555. }
  3556. }
  3557. }
  3558. }
  3559. }
  3560. /**
  3561. * Writes the current data to the database.
  3562. *
  3563. * @return bool Query result
  3564. */
  3565. public function write_to_db()
  3566. {
  3567. $debug = self::DEBUG;
  3568. if ($debug) {
  3569. error_log('learnpathItem::write_to_db()', 0);
  3570. }
  3571. // Check the session visibility.
  3572. if (!api_is_allowed_to_session_edit()) {
  3573. if ($debug) {
  3574. error_log('return false api_is_allowed_to_session_edit');
  3575. }
  3576. return false;
  3577. }
  3578. $course_id = api_get_course_int_id();
  3579. $mode = $this->get_lesson_mode();
  3580. $credit = $this->get_credit();
  3581. $total_time = ' ';
  3582. $my_status = ' ';
  3583. $item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3584. $sql = 'SELECT status, total_time FROM '.$item_view_table.'
  3585. WHERE
  3586. c_id = '.$course_id.' AND
  3587. lp_item_id="'.$this->db_id.'" AND
  3588. lp_view_id="'.$this->view_id.'" AND
  3589. view_count="'.$this->get_attempt_id().'" ';
  3590. $rs_verified = Database::query($sql);
  3591. $row_verified = Database::fetch_array($rs_verified);
  3592. $my_case_completed = [
  3593. 'completed',
  3594. 'passed',
  3595. 'browsed',
  3596. 'failed',
  3597. ];
  3598. $oldTotalTime = $row_verified['total_time'];
  3599. $this->oldTotalTime = $oldTotalTime;
  3600. $save = true;
  3601. if (isset($row_verified) && isset($row_verified['status'])) {
  3602. if (in_array($row_verified['status'], $my_case_completed)) {
  3603. $save = false;
  3604. }
  3605. }
  3606. if ((($save === false && $this->type == 'sco') ||
  3607. ($this->type == 'sco' && ($credit == 'no-credit' || $mode == 'review' || $mode == 'browse'))) &&
  3608. ($this->seriousgame_mode != 1 && $this->type == 'sco')
  3609. ) {
  3610. if ($debug) {
  3611. error_log(
  3612. "This info shouldn't be saved as the credit or lesson mode info prevent it"
  3613. );
  3614. error_log(
  3615. 'learnpathItem::write_to_db() - credit('.$credit.') or'.
  3616. ' lesson_mode('.$mode.') prevent recording!',
  3617. 0
  3618. );
  3619. }
  3620. } else {
  3621. // Check the row exists.
  3622. $inserted = false;
  3623. // This a special case for multiple attempts and Chamilo exercises.
  3624. if ($this->type == 'quiz' &&
  3625. $this->get_prevent_reinit() == 0 &&
  3626. $this->get_status() == 'completed'
  3627. ) {
  3628. // We force the item to be restarted.
  3629. $this->restart();
  3630. $params = [
  3631. "c_id" => $course_id,
  3632. "total_time" => $this->get_total_time(),
  3633. "start_time" => $this->current_start_time,
  3634. "score" => $this->get_score(),
  3635. "status" => $this->get_status(false),
  3636. "max_score" => $this->get_max(),
  3637. "lp_item_id" => $this->db_id,
  3638. "lp_view_id" => $this->view_id,
  3639. "view_count" => $this->get_attempt_id(),
  3640. "suspend_data" => $this->current_data,
  3641. //"max_time_allowed" => ,
  3642. "lesson_location" => $this->lesson_location,
  3643. ];
  3644. if ($debug) {
  3645. error_log(
  3646. 'learnpathItem::write_to_db() - Inserting into item_view forced: '.print_r($params, 1),
  3647. 0
  3648. );
  3649. }
  3650. $this->db_item_view_id = Database::insert($item_view_table, $params);
  3651. if ($this->db_item_view_id) {
  3652. $sql = "UPDATE $item_view_table SET id = iid
  3653. WHERE iid = ".$this->db_item_view_id;
  3654. Database::query($sql);
  3655. $inserted = true;
  3656. }
  3657. }
  3658. $item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3659. $sql = "SELECT * FROM $item_view_table
  3660. WHERE
  3661. c_id = $course_id AND
  3662. lp_item_id = ".$this->db_id." AND
  3663. lp_view_id = ".$this->view_id." AND
  3664. view_count = ".intval($this->get_attempt_id());
  3665. if ($debug) {
  3666. error_log(
  3667. 'learnpathItem::write_to_db() - Querying item_view: '.$sql,
  3668. 0
  3669. );
  3670. }
  3671. $check_res = Database::query($sql);
  3672. // Depending on what we want (really), we'll update or insert a new row
  3673. // now save into DB.
  3674. if (!$inserted && Database::num_rows($check_res) < 1) {
  3675. $params = [
  3676. "c_id" => $course_id,
  3677. "total_time" => $this->get_total_time(),
  3678. "start_time" => $this->current_start_time,
  3679. "score" => $this->get_score(),
  3680. "status" => $this->get_status(false),
  3681. "max_score" => $this->get_max(),
  3682. "lp_item_id" => $this->db_id,
  3683. "lp_view_id" => $this->view_id,
  3684. "view_count" => $this->get_attempt_id(),
  3685. "suspend_data" => $this->current_data,
  3686. //"max_time_allowed" => ,$this->get_max_time_allowed()
  3687. "lesson_location" => $this->lesson_location,
  3688. ];
  3689. if ($debug) {
  3690. error_log(
  3691. 'learnpathItem::write_to_db() - Inserting into item_view forced: '.print_r($params, 1),
  3692. 0
  3693. );
  3694. }
  3695. $this->db_item_view_id = Database::insert($item_view_table, $params);
  3696. if ($this->db_item_view_id) {
  3697. $sql = "UPDATE $item_view_table SET id = iid
  3698. WHERE iid = ".$this->db_item_view_id;
  3699. Database::query($sql);
  3700. }
  3701. } else {
  3702. if ($this->type == 'hotpotatoes') {
  3703. $params = [
  3704. 'total_time' => $this->get_total_time(),
  3705. 'start_time' => $this->get_current_start_time(),
  3706. 'score' => $this->get_score(),
  3707. 'status' => $this->get_status(false),
  3708. 'max_score' => $this->get_max(),
  3709. 'suspend_data' => $this->current_data,
  3710. 'lesson_location' => $this->lesson_location,
  3711. ];
  3712. $where = [
  3713. 'c_id = ? AND lp_item_id = ? AND lp_view_id = ? AND view_count = ?' => [
  3714. $course_id,
  3715. $this->db_id,
  3716. $this->view_id,
  3717. $this->get_attempt_id(),
  3718. ],
  3719. ];
  3720. Database::update($item_view_table, $params, $where);
  3721. } else {
  3722. // For all other content types...
  3723. if ($this->type == 'quiz') {
  3724. if ($debug) {
  3725. error_log("item is quiz:");
  3726. }
  3727. $my_status = ' ';
  3728. $total_time = ' ';
  3729. if (!empty($_REQUEST['exeId'])) {
  3730. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  3731. $exeId = (int) $_REQUEST['exeId'];
  3732. $sql = "SELECT exe_duration
  3733. FROM $table
  3734. WHERE exe_id = $exeId";
  3735. if ($debug) {
  3736. error_log($sql);
  3737. }
  3738. $res = Database::query($sql);
  3739. $exeRow = Database::fetch_array($res);
  3740. $duration = $exeRow['exe_duration'];
  3741. $total_time = " total_time = ".$duration.", ";
  3742. if ($debug) {
  3743. error_log("quiz: $total_time");
  3744. }
  3745. }
  3746. } else {
  3747. $my_type_lp = learnpath::get_type_static($this->lp_id);
  3748. // This is a array containing values finished
  3749. $case_completed = [
  3750. 'completed',
  3751. 'passed',
  3752. 'browsed',
  3753. 'failed',
  3754. ];
  3755. // Is not multiple attempts
  3756. if ($this->seriousgame_mode == 1 && $this->type == 'sco') {
  3757. $total_time = " total_time = total_time +".$this->get_total_time().", ";
  3758. $my_status = " status = '".$this->get_status(false)."' ,";
  3759. if ($debug) {
  3760. error_log("seriousgame_mode time changed: $total_time");
  3761. }
  3762. } elseif ($this->get_prevent_reinit() == 1) {
  3763. // Process of status verified into data base.
  3764. $sql = 'SELECT status FROM '.$item_view_table.'
  3765. WHERE
  3766. c_id = '.$course_id.' AND
  3767. lp_item_id="'.$this->db_id.'" AND
  3768. lp_view_id="'.$this->view_id.'" AND
  3769. view_count="'.$this->get_attempt_id().'"
  3770. ';
  3771. $rs_verified = Database::query($sql);
  3772. $row_verified = Database::fetch_array($rs_verified);
  3773. // Get type lp: 1=lp dokeos and 2=scorm.
  3774. // If not is completed or passed or browsed and learning path is scorm.
  3775. if (!in_array($this->get_status(false), $case_completed) &&
  3776. $my_type_lp == 2
  3777. ) {
  3778. $total_time = " total_time = total_time +".$this->get_total_time().", ";
  3779. $my_status = " status = '".$this->get_status(false)."' ,";
  3780. if ($debug) {
  3781. error_log("get_prevent_reinit = 1 time changed: $total_time");
  3782. }
  3783. } else {
  3784. // Verified into database.
  3785. if (!in_array($row_verified['status'], $case_completed) &&
  3786. $my_type_lp == 2
  3787. ) {
  3788. $total_time = " total_time = total_time +".$this->get_total_time().", ";
  3789. $my_status = " status = '".$this->get_status(false)."' ,";
  3790. if ($debug) {
  3791. error_log("total_time time changed case 1: $total_time");
  3792. }
  3793. } elseif (in_array($row_verified['status'], $case_completed) &&
  3794. $my_type_lp == 2 && $this->type != 'sco'
  3795. ) {
  3796. $total_time = " total_time = total_time +".$this->get_total_time().", ";
  3797. $my_status = " status = '".$this->get_status(false)."' ,";
  3798. if ($debug) {
  3799. error_log("total_time time changed case 2: $total_time");
  3800. }
  3801. } else {
  3802. if (($my_type_lp == 3 && $this->type == 'au') ||
  3803. ($my_type_lp == 1 && $this->type != 'dir')) {
  3804. // Is AICC or Chamilo LP
  3805. $total_time = " total_time = total_time + ".$this->get_total_time().", ";
  3806. $my_status = " status = '".$this->get_status(false)."' ,";
  3807. if ($debug) {
  3808. error_log("total_time time changed case 3: $total_time");
  3809. }
  3810. }
  3811. }
  3812. }
  3813. } else {
  3814. // Multiple attempts are allowed.
  3815. if (in_array($this->get_status(false), $case_completed) && $my_type_lp == 2) {
  3816. // Reset zero new attempt ?
  3817. $my_status = " status = '".$this->get_status(false)."' ,";
  3818. if ($debug) {
  3819. error_log("total_time time changed Multiple attempt case 1: $total_time");
  3820. }
  3821. } elseif (!in_array($this->get_status(false), $case_completed) && $my_type_lp == 2) {
  3822. $total_time = " total_time = ".$this->get_total_time().", ";
  3823. $my_status = " status = '".$this->get_status(false)."' ,";
  3824. if ($debug) {
  3825. error_log("total_time time changed Multiple attempt case 2: $total_time");
  3826. }
  3827. } else {
  3828. // It is chamilo LP.
  3829. $total_time = " total_time = total_time +".$this->get_total_time().", ";
  3830. $my_status = " status = '".$this->get_status(false)."' ,";
  3831. if ($debug) {
  3832. error_log("total_time time changed Multiple attempt case 3: $total_time");
  3833. }
  3834. }
  3835. // This code line fixes the problem of wrong status.
  3836. if ($my_type_lp == 2) {
  3837. // Verify current status in multiples attempts.
  3838. $sql = 'SELECT status FROM '.$item_view_table.'
  3839. WHERE
  3840. c_id = '.$course_id.' AND
  3841. lp_item_id="'.$this->db_id.'" AND
  3842. lp_view_id="'.$this->view_id.'" AND
  3843. view_count="'.$this->get_attempt_id().'" ';
  3844. $rs_status = Database::query($sql);
  3845. $current_status = Database::result(
  3846. $rs_status,
  3847. 0,
  3848. 'status'
  3849. );
  3850. if (in_array($current_status, $case_completed)) {
  3851. $my_status = '';
  3852. $total_time = '';
  3853. } else {
  3854. $total_time = " total_time = total_time + ".$this->get_total_time().", ";
  3855. }
  3856. if ($debug) {
  3857. error_log("total_time time my_type_lp: $total_time");
  3858. }
  3859. }
  3860. }
  3861. }
  3862. if ($this->type == 'sco') {
  3863. //IF scorm scorm_update_time has already updated total_time in db
  3864. //" . //start_time = ".$this->get_current_start_time().", " . //scorm_init_time does it
  3865. ////" max_time_allowed = '".$this->get_max_time_allowed()."'," .
  3866. $sql = "UPDATE $item_view_table SET
  3867. score = ".$this->get_score().",
  3868. $my_status
  3869. max_score = '".$this->get_max()."',
  3870. suspend_data = '".Database::escape_string($this->current_data)."',
  3871. lesson_location = '".$this->lesson_location."'
  3872. WHERE
  3873. c_id = $course_id AND
  3874. lp_item_id = ".$this->db_id." AND
  3875. lp_view_id = ".$this->view_id." AND
  3876. view_count = ".$this->get_attempt_id();
  3877. } else {
  3878. //" max_time_allowed = '".$this->get_max_time_allowed()."'," .
  3879. $sql = "UPDATE $item_view_table SET
  3880. $total_time
  3881. start_time = ".$this->get_current_start_time().",
  3882. score = ".$this->get_score().",
  3883. $my_status
  3884. max_score = '".$this->get_max()."',
  3885. suspend_data = '".Database::escape_string($this->current_data)."',
  3886. lesson_location = '".$this->lesson_location."'
  3887. WHERE
  3888. c_id = $course_id AND
  3889. lp_item_id = ".$this->db_id." AND
  3890. lp_view_id = ".$this->view_id." AND
  3891. view_count = ".$this->get_attempt_id();
  3892. }
  3893. $this->current_start_time = time();
  3894. }
  3895. if ($debug) {
  3896. error_log('-------------------------------------------');
  3897. error_log('learnpathItem::write_to_db() - Updating item_view:');
  3898. error_log($sql);
  3899. error_log('-------------------------------------------');
  3900. }
  3901. Database::query($sql);
  3902. }
  3903. if (is_array($this->interactions) &&
  3904. count($this->interactions) > 0
  3905. ) {
  3906. // Save interactions.
  3907. $tbl = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3908. $sql = "SELECT iid FROM $tbl
  3909. WHERE
  3910. c_id = $course_id AND
  3911. lp_item_id = ".$this->db_id." AND
  3912. lp_view_id = ".$this->view_id." AND
  3913. view_count = ".$this->get_attempt_id();
  3914. $res = Database::query($sql);
  3915. if (Database::num_rows($res) > 0) {
  3916. $row = Database::fetch_array($res);
  3917. $lp_iv_id = $row[0];
  3918. if ($debug) {
  3919. error_log(
  3920. 'learnpathItem::write_to_db() - Got item_view_id '.
  3921. $lp_iv_id.', now checking interactions ',
  3922. 0
  3923. );
  3924. }
  3925. foreach ($this->interactions as $index => $interaction) {
  3926. $correct_resp = '';
  3927. if (is_array($interaction[4]) && !empty($interaction[4][0])) {
  3928. foreach ($interaction[4] as $resp) {
  3929. $correct_resp .= $resp.',';
  3930. }
  3931. $correct_resp = substr(
  3932. $correct_resp,
  3933. 0,
  3934. strlen($correct_resp) - 1
  3935. );
  3936. }
  3937. $iva_table = Database::get_course_table(
  3938. TABLE_LP_IV_INTERACTION
  3939. );
  3940. //also check for the interaction ID as it must be unique for this SCO view
  3941. $iva_sql = "SELECT iid FROM $iva_table
  3942. WHERE
  3943. c_id = $course_id AND
  3944. lp_iv_id = $lp_iv_id AND
  3945. (
  3946. order_id = $index OR
  3947. interaction_id = '".Database::escape_string($interaction[0])."'
  3948. )
  3949. ";
  3950. $iva_res = Database::query($iva_sql);
  3951. $interaction[0] = isset($interaction[0]) ? $interaction[0] : '';
  3952. $interaction[1] = isset($interaction[1]) ? $interaction[1] : '';
  3953. $interaction[2] = isset($interaction[2]) ? $interaction[2] : '';
  3954. $interaction[3] = isset($interaction[3]) ? $interaction[3] : '';
  3955. $interaction[4] = isset($interaction[4]) ? $interaction[4] : '';
  3956. $interaction[5] = isset($interaction[5]) ? $interaction[5] : '';
  3957. $interaction[6] = isset($interaction[6]) ? $interaction[6] : '';
  3958. $interaction[7] = isset($interaction[7]) ? $interaction[7] : '';
  3959. // id(0), type(1), time(2), weighting(3), correct_responses(4), student_response(5), result(6), latency(7)
  3960. if (Database::num_rows($iva_res) > 0) {
  3961. // Update (or don't).
  3962. $iva_row = Database::fetch_array($iva_res);
  3963. $iva_id = $iva_row[0];
  3964. // Insert new one.
  3965. $params = [
  3966. 'interaction_id' => $interaction[0],
  3967. 'interaction_type' => $interaction[1],
  3968. 'weighting' => $interaction[3],
  3969. 'completion_time' => $interaction[2],
  3970. 'correct_responses' => $correct_resp,
  3971. 'student_response' => $interaction[5],
  3972. 'result' => $interaction[6],
  3973. 'latency' => $interaction[7],
  3974. ];
  3975. Database::update(
  3976. $iva_table,
  3977. $params,
  3978. [
  3979. 'c_id = ? AND iid = ?' => [
  3980. $course_id,
  3981. $iva_id,
  3982. ],
  3983. ]
  3984. );
  3985. } else {
  3986. // Insert new one.
  3987. $params = [
  3988. 'c_id' => $course_id,
  3989. 'order_id' => $index,
  3990. 'lp_iv_id' => $lp_iv_id,
  3991. 'interaction_id' => $interaction[0],
  3992. 'interaction_type' => $interaction[1],
  3993. 'weighting' => $interaction[3],
  3994. 'completion_time' => $interaction[2],
  3995. 'correct_responses' => $correct_resp,
  3996. 'student_response' => $interaction[5],
  3997. 'result' => $interaction[6],
  3998. 'latency' => $interaction[7],
  3999. ];
  4000. $insertId = Database::insert($iva_table, $params);
  4001. if ($insertId) {
  4002. $sql = "UPDATE $iva_table SET id = iid
  4003. WHERE iid = $insertId";
  4004. Database::query($sql);
  4005. }
  4006. }
  4007. }
  4008. }
  4009. }
  4010. }
  4011. if ($debug) {
  4012. error_log('End of learnpathItem::write_to_db()', 0);
  4013. }
  4014. return true;
  4015. }
  4016. /**
  4017. * Adds an audio file attached to the current item (store on disk and in db).
  4018. *
  4019. * @return bool|null|string
  4020. */
  4021. public function add_audio()
  4022. {
  4023. $course_info = api_get_course_info();
  4024. $filepath = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/document/';
  4025. if (!is_dir($filepath.'audio')) {
  4026. mkdir(
  4027. $filepath.'audio',
  4028. api_get_permissions_for_new_directories()
  4029. );
  4030. $audio_id = add_document(
  4031. $course_info,
  4032. '/audio',
  4033. 'folder',
  4034. 0,
  4035. 'audio'
  4036. );
  4037. api_item_property_update(
  4038. $course_info,
  4039. TOOL_DOCUMENT,
  4040. $audio_id,
  4041. 'FolderCreated',
  4042. api_get_user_id(),
  4043. null,
  4044. null,
  4045. null,
  4046. null,
  4047. api_get_session_id()
  4048. );
  4049. api_item_property_update(
  4050. $course_info,
  4051. TOOL_DOCUMENT,
  4052. $audio_id,
  4053. 'invisible',
  4054. api_get_user_id(),
  4055. null,
  4056. null,
  4057. null,
  4058. null,
  4059. api_get_session_id()
  4060. );
  4061. }
  4062. $key = 'file';
  4063. if (!isset($_FILES[$key]['name']) || !isset($_FILES[$key]['tmp_name'])) {
  4064. return false;
  4065. }
  4066. $result = DocumentManager::upload_document(
  4067. $_FILES,
  4068. '/audio',
  4069. null,
  4070. null,
  4071. 0,
  4072. 'rename',
  4073. false,
  4074. false
  4075. );
  4076. $file_path = null;
  4077. if ($result) {
  4078. $file_path = basename($result['path']);
  4079. // Store the mp3 file in the lp_item table.
  4080. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  4081. $sql = "UPDATE $tbl_lp_item SET
  4082. audio = '".Database::escape_string($file_path)."'
  4083. WHERE iid = ".intval($this->db_id);
  4084. Database::query($sql);
  4085. }
  4086. return $file_path;
  4087. }
  4088. /**
  4089. * Adds an audio file to the current item, using a file already in documents.
  4090. *
  4091. * @param int $doc_id
  4092. *
  4093. * @return string
  4094. */
  4095. public function add_audio_from_documents($doc_id)
  4096. {
  4097. $course_info = api_get_course_info();
  4098. $document_data = DocumentManager::get_document_data_by_id(
  4099. $doc_id,
  4100. $course_info['code']
  4101. );
  4102. $file_path = '';
  4103. if (!empty($document_data)) {
  4104. $file_path = basename($document_data['path']);
  4105. // Store the mp3 file in the lp_item table.
  4106. $table = Database::get_course_table(TABLE_LP_ITEM);
  4107. $sql = "UPDATE $table SET
  4108. audio = '".Database::escape_string($file_path)."'
  4109. WHERE iid = ".intval($this->db_id);
  4110. Database::query($sql);
  4111. }
  4112. return $file_path;
  4113. }
  4114. /**
  4115. * Removes the relation between the current item and an audio file. The file
  4116. * is only removed from the lp_item table, but remains in the document table
  4117. * and directory.
  4118. *
  4119. * @return bool
  4120. */
  4121. public function remove_audio()
  4122. {
  4123. if (empty($this->db_id)) {
  4124. return false;
  4125. }
  4126. $table = Database::get_course_table(TABLE_LP_ITEM);
  4127. $sql = "UPDATE $table SET
  4128. audio = ''
  4129. WHERE iid IN (".$this->db_id.")";
  4130. Database::query($sql);
  4131. }
  4132. /**
  4133. * Transform the SCORM status to a string that can be translated by Chamilo
  4134. * in different user languages.
  4135. *
  4136. * @param $status
  4137. * @param bool $decorate
  4138. * @param string $type classic|simple
  4139. *
  4140. * @return array|string
  4141. */
  4142. public static function humanize_status($status, $decorate = true, $type = 'classic')
  4143. {
  4144. $statusList = [
  4145. 'completed' => 'ScormCompstatus',
  4146. 'incomplete' => 'ScormIncomplete',
  4147. 'failed' => 'ScormFailed',
  4148. 'passed' => 'ScormPassed',
  4149. 'browsed' => 'ScormBrowsed',
  4150. 'not attempted' => 'ScormNotAttempted',
  4151. ];
  4152. $myLessonStatus = get_lang($statusList[$status]);
  4153. switch ($status) {
  4154. case 'completed':
  4155. case 'browsed':
  4156. $classStatus = 'info';
  4157. break;
  4158. case 'incomplete':
  4159. $classStatus = 'warning';
  4160. break;
  4161. case 'passed':
  4162. $classStatus = 'success';
  4163. break;
  4164. case 'failed':
  4165. $classStatus = 'important';
  4166. break;
  4167. default:
  4168. $classStatus = 'default';
  4169. break;
  4170. }
  4171. if ($type == 'simple') {
  4172. if (in_array($status, ['failed', 'passed', 'browsed'])) {
  4173. $myLessonStatus = get_lang('ScormIncomplete');
  4174. $classStatus = 'warning';
  4175. }
  4176. }
  4177. if ($decorate) {
  4178. return Display::label($myLessonStatus, $classStatus);
  4179. } else {
  4180. return $myLessonStatus;
  4181. }
  4182. }
  4183. /**
  4184. * @return float
  4185. */
  4186. public function getPrerequisiteMaxScore()
  4187. {
  4188. return $this->prerequisiteMaxScore;
  4189. }
  4190. /**
  4191. * @param float $prerequisiteMaxScore
  4192. */
  4193. public function setPrerequisiteMaxScore($prerequisiteMaxScore)
  4194. {
  4195. $this->prerequisiteMaxScore = $prerequisiteMaxScore;
  4196. }
  4197. /**
  4198. * @return float
  4199. */
  4200. public function getPrerequisiteMinScore()
  4201. {
  4202. return $this->prerequisiteMinScore;
  4203. }
  4204. /**
  4205. * @param float $prerequisiteMinScore
  4206. */
  4207. public function setPrerequisiteMinScore($prerequisiteMinScore)
  4208. {
  4209. $this->prerequisiteMinScore = $prerequisiteMinScore;
  4210. }
  4211. /**
  4212. * Check if this LP item has a created thread in the basis course from the forum of its LP.
  4213. *
  4214. * @param int $lpCourseId The course ID
  4215. *
  4216. * @return bool
  4217. */
  4218. public function lpItemHasThread($lpCourseId)
  4219. {
  4220. $forumThreadTable = Database::get_course_table(TABLE_FORUM_THREAD);
  4221. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  4222. $fakeFrom = "
  4223. $forumThreadTable ft
  4224. INNER JOIN $itemProperty ip
  4225. ON (ft.thread_id = ip.ref AND ft.c_id = ip.c_id)
  4226. ";
  4227. $resultData = Database::select(
  4228. 'COUNT(ft.iid) AS qty',
  4229. $fakeFrom,
  4230. [
  4231. 'where' => [
  4232. 'ip.visibility != ? AND ' => 2,
  4233. 'ip.tool = ? AND ' => TOOL_FORUM_THREAD,
  4234. 'ft.c_id = ? AND ' => intval($lpCourseId),
  4235. '(ft.lp_item_id = ? OR (ft.thread_title = ? AND ft.lp_item_id = ?))' => [
  4236. intval($this->db_id),
  4237. "{$this->title} - {$this->db_id}",
  4238. intval($this->db_id),
  4239. ],
  4240. ],
  4241. ],
  4242. 'first'
  4243. );
  4244. if ($resultData['qty'] > 0) {
  4245. return true;
  4246. }
  4247. return false;
  4248. }
  4249. /**
  4250. * Get the forum thread info.
  4251. *
  4252. * @param int $lpCourseId The course ID from the learning path
  4253. * @param int $lpSessionId Optional. The session ID from the learning path
  4254. *
  4255. * @return bool
  4256. */
  4257. public function getForumThread($lpCourseId, $lpSessionId = 0)
  4258. {
  4259. $lpSessionId = (int) $lpSessionId;
  4260. $forumThreadTable = Database::get_course_table(TABLE_FORUM_THREAD);
  4261. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  4262. $fakeFrom = "$forumThreadTable ft
  4263. INNER JOIN $itemProperty ip ";
  4264. if ($lpSessionId == 0) {
  4265. $fakeFrom .= "
  4266. ON (
  4267. ft.thread_id = ip.ref AND ft.c_id = ip.c_id AND (
  4268. ft.session_id = ip.session_id OR ip.session_id IS NULL
  4269. )
  4270. )
  4271. ";
  4272. } else {
  4273. $fakeFrom .= "
  4274. ON (
  4275. ft.thread_id = ip.ref AND ft.c_id = ip.c_id AND ft.session_id = ip.session_id
  4276. )
  4277. ";
  4278. }
  4279. $resultData = Database::select(
  4280. 'ft.*',
  4281. $fakeFrom,
  4282. [
  4283. 'where' => [
  4284. 'ip.visibility != ? AND ' => 2,
  4285. 'ip.tool = ? AND ' => TOOL_FORUM_THREAD,
  4286. 'ft.session_id = ? AND ' => $lpSessionId,
  4287. 'ft.c_id = ? AND ' => intval($lpCourseId),
  4288. '(ft.lp_item_id = ? OR (ft.thread_title = ? AND ft.lp_item_id = ?))' => [
  4289. intval($this->db_id),
  4290. "{$this->title} - {$this->db_id}",
  4291. intval($this->db_id),
  4292. ],
  4293. ],
  4294. ],
  4295. 'first'
  4296. );
  4297. if (empty($resultData)) {
  4298. return false;
  4299. }
  4300. return $resultData;
  4301. }
  4302. /**
  4303. * Create a forum thread for this learning path item.
  4304. *
  4305. * @param int $currentForumId The forum ID to add the new thread
  4306. *
  4307. * @return int The forum thread if was created. Otherwise return false
  4308. */
  4309. public function createForumThread($currentForumId)
  4310. {
  4311. require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php';
  4312. $em = Database::getManager();
  4313. $threadRepo = $em->getRepository('ChamiloCourseBundle:CForumThread');
  4314. $forumThread = $threadRepo->findOneBy([
  4315. 'threadTitle' => "{$this->title} - {$this->db_id}",
  4316. 'forumId' => (int) $currentForumId,
  4317. ]);
  4318. if (!$forumThread) {
  4319. $forumInfo = get_forum_information($currentForumId);
  4320. store_thread(
  4321. $forumInfo,
  4322. [
  4323. 'forum_id' => intval($currentForumId),
  4324. 'thread_id' => 0,
  4325. 'gradebook' => 0,
  4326. 'post_title' => "{$this->name} - {$this->db_id}",
  4327. 'post_text' => $this->description,
  4328. 'category_id' => 1,
  4329. 'numeric_calification' => 0,
  4330. 'calification_notebook_title' => 0,
  4331. 'weight_calification' => 0.00,
  4332. 'thread_peer_qualify' => 0,
  4333. 'lp_item_id' => $this->db_id,
  4334. ],
  4335. [],
  4336. false
  4337. );
  4338. return;
  4339. }
  4340. $forumThread->setLpItemId($this->db_id);
  4341. $em->persist($forumThread);
  4342. $em->flush();
  4343. }
  4344. /**
  4345. * Allow dissociate a forum to this LP item.
  4346. *
  4347. * @param int $threadIid The thread id
  4348. *
  4349. * @return bool
  4350. */
  4351. public function dissociateForumThread($threadIid)
  4352. {
  4353. $threadIid = (int) $threadIid;
  4354. $em = Database::getManager();
  4355. $forumThread = $em->find('ChamiloCourseBundle:CForumThread', $threadIid);
  4356. if (!$forumThread) {
  4357. return false;
  4358. }
  4359. $forumThread->setThreadTitle("{$this->get_title()} - {$this->db_id}");
  4360. $forumThread->setLpItemId(0);
  4361. $em->persist($forumThread);
  4362. $em->flush();
  4363. return true;
  4364. }
  4365. /**
  4366. * @return int
  4367. */
  4368. public function getLastScormSessionTime()
  4369. {
  4370. return $this->last_scorm_session_time;
  4371. }
  4372. /**
  4373. * @return int
  4374. */
  4375. public function getIid()
  4376. {
  4377. return $this->iId;
  4378. }
  4379. }