learnpathItem.class.php 190 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737
  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 array|null $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. if (!Tracking::minimunTimeAvailable(api_get_session_id(), api_get_course_int_id())) {
  1788. $fixedAddedMinute = 5 * 60; // Add only 5 minutes
  1789. if ($time > $sessionLifetime) {
  1790. error_log("fixAbusiveTime: Total time is too big: $time replaced with: $fixedAddedMinute");
  1791. error_log("item_id : ".$this->db_id." lp_item_view.iid: ".$this->db_item_view_id);
  1792. $time = $fixedAddedMinute;
  1793. }
  1794. return $time;
  1795. } else {
  1796. // Calulate minimum and accumulated time
  1797. $user_id = api_get_user_id();
  1798. $myLP = learnpath::getLpFromSession(api_get_course_id(), $this->lp_id, $user_id);
  1799. $timeLp = $myLP->getAccumulateWorkTime();
  1800. $timeTotalCourse = $myLP->getAccumulateWorkTimeTotalCourse();
  1801. /*
  1802. $timeLp = $_SESSION['oLP']->getAccumulateWorkTime();
  1803. $timeTotalCourse = $_SESSION['oLP']->getAccumulateWorkTimeTotalCourse();
  1804. */
  1805. // Minimum connection percentage
  1806. $perc = 100;
  1807. // Time from the course
  1808. $tc = $timeTotalCourse;
  1809. /*if (!empty($sessionId) && $sessionId != 0) {
  1810. $sql = "SELECT hours, perc FROM plugin_licences_course_session WHERE session_id = $sessionId";
  1811. $res = Database::query($sql);
  1812. if (Database::num_rows($res) > 0) {
  1813. $aux = Database::fetch_assoc($res);
  1814. $perc = $aux['perc'];
  1815. $tc = $aux['hours'] * 60;
  1816. }
  1817. }*/
  1818. // Percentage of the learning paths
  1819. $pl = 0;
  1820. if (!empty($timeTotalCourse)) {
  1821. $pl = $timeLp / $timeTotalCourse;
  1822. }
  1823. // Minimum time for each learning path
  1824. $accumulateWorkTime = ($pl * $tc * $perc / 100);
  1825. $time_seg = intval($accumulateWorkTime * 60);
  1826. if ($time_seg < $sessionLifetime) {
  1827. $sessionLifetime = $time_seg;
  1828. }
  1829. if ($time > $sessionLifetime) {
  1830. $fixedAddedMinute = $time_seg + mt_rand(0, 300);
  1831. if (self::DEBUG > 2) {
  1832. error_log("Total time is too big: $time replaced with: $fixedAddedMinute");
  1833. }
  1834. $time = $fixedAddedMinute;
  1835. }
  1836. return $time;
  1837. }
  1838. }
  1839. /**
  1840. * Gets the item type.
  1841. *
  1842. * @return string The item type (can be doc, dir, sco, asset)
  1843. */
  1844. public function get_type()
  1845. {
  1846. $res = 'asset';
  1847. if (!empty($this->type)) {
  1848. $res = $this->type;
  1849. }
  1850. if (self::DEBUG > 2) {
  1851. error_log(
  1852. 'learnpathItem::get_type() - Returning '.$res.' for item '.$this->db_id,
  1853. 0
  1854. );
  1855. }
  1856. return $res;
  1857. }
  1858. /**
  1859. * Gets the view count for this item.
  1860. *
  1861. * @return int Number of attempts or 0
  1862. */
  1863. public function get_view_count()
  1864. {
  1865. if (self::DEBUG > 0) {
  1866. error_log('learnpathItem::get_view_count()', 0);
  1867. }
  1868. if (!empty($this->attempt_id)) {
  1869. return $this->attempt_id;
  1870. } else {
  1871. return 0;
  1872. }
  1873. }
  1874. /**
  1875. * Tells if an item is done ('completed','passed','succeeded') or not.
  1876. *
  1877. * @return bool True if the item is done ('completed','passed','succeeded'),
  1878. * false otherwise
  1879. */
  1880. public function is_done()
  1881. {
  1882. $completedStatusList = [
  1883. 'completed',
  1884. 'passed',
  1885. 'succeeded',
  1886. 'failed',
  1887. ];
  1888. if ($this->status_is($completedStatusList)) {
  1889. if (self::DEBUG > 2) {
  1890. error_log(
  1891. 'learnpath::is_done() - Item '.$this->get_id(
  1892. ).' is complete',
  1893. 0
  1894. );
  1895. }
  1896. return true;
  1897. } else {
  1898. if (self::DEBUG > 2) {
  1899. error_log(
  1900. 'learnpath::is_done() - Item '.$this->get_id(
  1901. ).' is not complete',
  1902. 0
  1903. );
  1904. }
  1905. return false;
  1906. }
  1907. }
  1908. /**
  1909. * Tells if a restart is allowed (take it from $this->prevent_reinit and $this->status).
  1910. *
  1911. * @return int -1 if retaking the sco another time for credit is not allowed,
  1912. * 0 if it is not allowed but the item has to be finished
  1913. * 1 if it is allowed. Defaults to 1
  1914. */
  1915. public function isRestartAllowed()
  1916. {
  1917. if (self::DEBUG > 2) {
  1918. error_log('learnpathItem::isRestartAllowed()', 0);
  1919. }
  1920. $restart = 1;
  1921. $mystatus = $this->get_status(true);
  1922. if ($this->get_prevent_reinit() > 0) {
  1923. // If prevent_reinit == 1 (or more)
  1924. // If status is not attempted or incomplete, authorize retaking (of the same) anyway. Otherwise:
  1925. if ($mystatus != $this->possible_status[0] && $mystatus != $this->possible_status[1]) {
  1926. $restart = -1;
  1927. } else { //status incompleted or not attempted
  1928. $restart = 0;
  1929. }
  1930. } else {
  1931. if ($mystatus == $this->possible_status[0] || $mystatus == $this->possible_status[1]) {
  1932. $restart = -1;
  1933. }
  1934. }
  1935. if (self::DEBUG > 2) {
  1936. error_log(
  1937. 'New LP - End of learnpathItem::isRestartAllowed() - Returning '.$restart,
  1938. 0
  1939. );
  1940. }
  1941. return $restart;
  1942. }
  1943. /**
  1944. * Opens/launches the item. Initialises runtime values.
  1945. *
  1946. * @param bool $allow_new_attempt
  1947. *
  1948. * @return bool true on success, false on failure
  1949. */
  1950. public function open($allow_new_attempt = false)
  1951. {
  1952. if (self::DEBUG > 0) {
  1953. error_log('learnpathItem::open()', 0);
  1954. }
  1955. if ($this->prevent_reinit == 0) {
  1956. $this->current_score = 0;
  1957. $this->current_start_time = time();
  1958. // In this case, as we are opening the item, what is important to us
  1959. // is the database status, in order to know if this item has already
  1960. // been used in the past (rather than just loaded and modified by
  1961. // some javascript but not written in the database).
  1962. // If the database status is different from 'not attempted', we can
  1963. // consider this item has already been used, and as such we can
  1964. // open a new attempt. Otherwise, we'll just reuse the current
  1965. // attempt, which is generally created the first time the item is
  1966. // loaded (for example as part of the table of contents).
  1967. $stat = $this->get_status(true);
  1968. if ($allow_new_attempt && isset($stat) && ($stat != $this->possible_status[0])) {
  1969. $this->attempt_id = $this->attempt_id + 1; // Open a new attempt.
  1970. }
  1971. $this->status = $this->possible_status[1];
  1972. } else {
  1973. /*if ($this->current_start_time == 0) {
  1974. // Small exception for start time, to avoid amazing values.
  1975. $this->current_start_time = time();
  1976. }*/
  1977. // If we don't init start time here, the time is sometimes calculated from the last start time.
  1978. $this->current_start_time = time();
  1979. }
  1980. }
  1981. /**
  1982. * Outputs the item contents.
  1983. *
  1984. * @return string HTML file (displayable in an <iframe>) or empty string if no path defined
  1985. */
  1986. public function output()
  1987. {
  1988. if (self::DEBUG > 0) {
  1989. error_log('learnpathItem::output()', 0);
  1990. }
  1991. if (!empty($this->path) and is_file($this->path)) {
  1992. $output = '';
  1993. $output .= file_get_contents($this->path);
  1994. return $output;
  1995. }
  1996. return '';
  1997. }
  1998. /**
  1999. * Parses the prerequisites string with the AICC logic language.
  2000. *
  2001. * @param string $prereqs_string The prerequisites string as it figures in imsmanifest.xml
  2002. * @param array $items Array of items in the current learnpath object.
  2003. * Although we're in the learnpathItem object, it's necessary to have
  2004. * a list of all items to be able to check the current item's prerequisites
  2005. * @param array $refs_list list of references
  2006. * (the "ref" column in the lp_item table) that are strings used in the
  2007. * expression of prerequisites
  2008. * @param int $user_id The user ID. In some cases like Chamilo quizzes,
  2009. * it's necessary to have the user ID to query other tables (like the results of quizzes)
  2010. *
  2011. * @return bool True if the list of prerequisites given is entirely satisfied, false otherwise
  2012. */
  2013. public function parse_prereq($prereqs_string, $items, $refs_list, $user_id)
  2014. {
  2015. $debug = self::DEBUG;
  2016. if ($debug > 0) {
  2017. error_log(
  2018. 'learnpathItem::parse_prereq() for learnpath '.$this->lp_id.' with string '.$prereqs_string,
  2019. 0
  2020. );
  2021. }
  2022. $course_id = api_get_course_int_id();
  2023. $sessionId = api_get_session_id();
  2024. // Deal with &, |, ~, =, <>, {}, ,, X*, () in reverse order.
  2025. $this->prereq_alert = '';
  2026. // First parse all parenthesis by using a sequential loop
  2027. // (looking for less-inclusives first).
  2028. if ($prereqs_string == '_true_') {
  2029. return true;
  2030. }
  2031. if ($prereqs_string == '_false_') {
  2032. if (empty($this->prereq_alert)) {
  2033. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2034. }
  2035. return false;
  2036. }
  2037. while (strpos($prereqs_string, '(') !== false) {
  2038. // Remove any () set and replace with its value.
  2039. $matches = [];
  2040. $res = preg_match_all(
  2041. '/(\(([^\(\)]*)\))/',
  2042. $prereqs_string,
  2043. $matches
  2044. );
  2045. if ($res) {
  2046. foreach ($matches[2] as $id => $match) {
  2047. $str_res = $this->parse_prereq(
  2048. $match,
  2049. $items,
  2050. $refs_list,
  2051. $user_id
  2052. );
  2053. if ($str_res) {
  2054. $prereqs_string = str_replace(
  2055. $matches[1][$id],
  2056. '_true_',
  2057. $prereqs_string
  2058. );
  2059. } else {
  2060. $prereqs_string = str_replace(
  2061. $matches[1][$id],
  2062. '_false_',
  2063. $prereqs_string
  2064. );
  2065. }
  2066. }
  2067. }
  2068. }
  2069. // Parenthesis removed, now look for ORs as it is the lesser-priority
  2070. // binary operator (= always uses one text operand).
  2071. if (strpos($prereqs_string, '|') === false) {
  2072. if ($debug) {
  2073. error_log('New LP - Didnt find any OR, looking for AND', 0);
  2074. }
  2075. if (strpos($prereqs_string, '&') !== false) {
  2076. $list = explode('&', $prereqs_string);
  2077. if (count($list) > 1) {
  2078. $andstatus = true;
  2079. foreach ($list as $condition) {
  2080. $andstatus = $andstatus && $this->parse_prereq(
  2081. $condition,
  2082. $items,
  2083. $refs_list,
  2084. $user_id
  2085. );
  2086. if (!$andstatus) {
  2087. if ($debug) {
  2088. error_log(
  2089. 'New LP - One condition in AND was false, short-circuit',
  2090. 0
  2091. );
  2092. }
  2093. break;
  2094. }
  2095. }
  2096. if (empty($this->prereq_alert) && !$andstatus) {
  2097. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2098. }
  2099. return $andstatus;
  2100. } else {
  2101. if (isset($items[$refs_list[$list[0]]])) {
  2102. $status = $items[$refs_list[$list[0]]]->get_status(true);
  2103. $returnstatus = ($status == $this->possible_status[2]) || ($status == $this->possible_status[3]);
  2104. if (empty($this->prereq_alert) && !$returnstatus) {
  2105. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2106. }
  2107. return $returnstatus;
  2108. }
  2109. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2110. return false;
  2111. }
  2112. } else {
  2113. // No ORs found, now look for ANDs.
  2114. if ($debug) {
  2115. error_log('New LP - Didnt find any AND, looking for =', 0);
  2116. }
  2117. if (strpos($prereqs_string, '=') !== false) {
  2118. if ($debug) {
  2119. error_log('New LP - Found =, looking into it', 0);
  2120. }
  2121. // We assume '=' signs only appear when there's nothing else around.
  2122. $params = explode('=', $prereqs_string);
  2123. if (count($params) == 2) {
  2124. // Right number of operands.
  2125. if (isset($items[$refs_list[$params[0]]])) {
  2126. $status = $items[$refs_list[$params[0]]]->get_status(true);
  2127. $returnstatus = $status == $params[1];
  2128. if (empty($this->prereq_alert) && !$returnstatus) {
  2129. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2130. }
  2131. return $returnstatus;
  2132. }
  2133. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2134. return false;
  2135. }
  2136. } else {
  2137. // No ANDs found, look for <>
  2138. if ($debug) {
  2139. error_log(
  2140. 'New LP - Didnt find any =, looking for <>',
  2141. 0
  2142. );
  2143. }
  2144. if (strpos($prereqs_string, '<>') !== false) {
  2145. if ($debug) {
  2146. error_log('New LP - Found <>, looking into it', 0);
  2147. }
  2148. // We assume '<>' signs only appear when there's nothing else around.
  2149. $params = explode('<>', $prereqs_string);
  2150. if (count($params) == 2) {
  2151. // Right number of operands.
  2152. if (isset($items[$refs_list[$params[0]]])) {
  2153. $status = $items[$refs_list[$params[0]]]->get_status(true);
  2154. $returnstatus = $status != $params[1];
  2155. if (empty($this->prereq_alert) && !$returnstatus) {
  2156. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2157. }
  2158. return $returnstatus;
  2159. }
  2160. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2161. return false;
  2162. }
  2163. } else {
  2164. // No <> found, look for ~ (unary)
  2165. if ($debug) {
  2166. error_log(
  2167. 'New LP - Didnt find any =, looking for ~',
  2168. 0
  2169. );
  2170. }
  2171. // Only remains: ~ and X*{}
  2172. if (strpos($prereqs_string, '~') !== false) {
  2173. // Found NOT.
  2174. if ($debug) {
  2175. error_log(
  2176. 'New LP - Found ~, looking into it',
  2177. 0
  2178. );
  2179. }
  2180. $list = [];
  2181. $myres = preg_match(
  2182. '/~([^(\d+\*)\{]*)/',
  2183. $prereqs_string,
  2184. $list
  2185. );
  2186. if ($myres) {
  2187. $returnstatus = !$this->parse_prereq(
  2188. $list[1],
  2189. $items,
  2190. $refs_list,
  2191. $user_id
  2192. );
  2193. if (empty($this->prereq_alert) && !$returnstatus) {
  2194. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2195. }
  2196. return $returnstatus;
  2197. } else {
  2198. // Strange...
  2199. if ($debug) {
  2200. error_log(
  2201. 'New LP - Found ~ but strange string: '.$prereqs_string,
  2202. 0
  2203. );
  2204. }
  2205. }
  2206. } else {
  2207. // Finally, look for sets/groups
  2208. if ($debug) {
  2209. error_log(
  2210. 'New LP - Didnt find any ~, looking for groups',
  2211. 0
  2212. );
  2213. }
  2214. // Only groups here.
  2215. $groups = [];
  2216. $groups_there = preg_match_all(
  2217. '/((\d+\*)?\{([^\}]+)\}+)/',
  2218. $prereqs_string,
  2219. $groups
  2220. );
  2221. if ($groups_there) {
  2222. foreach ($groups[1] as $gr) {
  2223. // Only take the results that correspond to
  2224. // the big brackets-enclosed condition.
  2225. if ($debug) {
  2226. error_log(
  2227. 'New LP - Dealing with group '.$gr,
  2228. 0
  2229. );
  2230. }
  2231. $multi = [];
  2232. $mycond = false;
  2233. if (preg_match(
  2234. '/(\d+)\*\{([^\}]+)\}/',
  2235. $gr,
  2236. $multi
  2237. )
  2238. ) {
  2239. if ($debug) {
  2240. error_log(
  2241. 'New LP - Found multiplier '.$multi[0],
  2242. 0
  2243. );
  2244. }
  2245. $count = $multi[1];
  2246. $list = explode(',', $multi[2]);
  2247. $mytrue = 0;
  2248. foreach ($list as $cond) {
  2249. if (isset($items[$refs_list[$cond]])) {
  2250. $status = $items[$refs_list[$cond]]->get_status(true);
  2251. if ($status == $this->possible_status[2] ||
  2252. $status == $this->possible_status[3]
  2253. ) {
  2254. $mytrue++;
  2255. if ($debug) {
  2256. error_log(
  2257. 'New LP - Found true item, counting.. ('.($mytrue).')',
  2258. 0
  2259. );
  2260. }
  2261. }
  2262. } else {
  2263. if ($debug) {
  2264. error_log(
  2265. 'New LP - item '.$cond.' does not exist in items list',
  2266. 0
  2267. );
  2268. }
  2269. }
  2270. }
  2271. if ($mytrue >= $count) {
  2272. if ($debug) {
  2273. error_log(
  2274. 'New LP - Got enough true results, return true',
  2275. 0
  2276. );
  2277. }
  2278. $mycond = true;
  2279. } else {
  2280. if ($debug) {
  2281. error_log(
  2282. 'New LP - Not enough true results',
  2283. 0
  2284. );
  2285. }
  2286. }
  2287. } else {
  2288. if ($debug) {
  2289. error_log(
  2290. 'New LP - No multiplier',
  2291. 0
  2292. );
  2293. }
  2294. $list = explode(',', $gr);
  2295. $mycond = true;
  2296. foreach ($list as $cond) {
  2297. if (isset($items[$refs_list[$cond]])) {
  2298. $status = $items[$refs_list[$cond]]->get_status(true);
  2299. if ($status == $this->possible_status[2] ||
  2300. $status == $this->possible_status[3]
  2301. ) {
  2302. $mycond = true;
  2303. if ($debug) {
  2304. error_log(
  2305. 'New LP - Found true item',
  2306. 0
  2307. );
  2308. }
  2309. } else {
  2310. if ($debug) {
  2311. error_log(
  2312. 'New LP - '.
  2313. ' Found false item, the set is not true, return false',
  2314. 0
  2315. );
  2316. }
  2317. $mycond = false;
  2318. break;
  2319. }
  2320. } else {
  2321. if ($debug) {
  2322. error_log(
  2323. 'New LP - item '.$cond.' does not exist in items list',
  2324. 0
  2325. );
  2326. }
  2327. if ($debug) {
  2328. error_log(
  2329. 'New LP - Found false item, the set is not true, return false',
  2330. 0
  2331. );
  2332. }
  2333. $mycond = false;
  2334. break;
  2335. }
  2336. }
  2337. }
  2338. if (!$mycond && empty($this->prereq_alert)) {
  2339. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2340. }
  2341. return $mycond;
  2342. }
  2343. } else {
  2344. // Nothing found there either. Now return the
  2345. // value of the corresponding resource completion status.
  2346. if ($debug) {
  2347. error_log(
  2348. 'New LP - Didnt find any group, returning value for '.$prereqs_string,
  2349. 0
  2350. );
  2351. }
  2352. if (isset($refs_list[$prereqs_string]) &&
  2353. isset($items[$refs_list[$prereqs_string]])
  2354. ) {
  2355. /** @var learnpathItem $itemToCheck */
  2356. $itemToCheck = $items[$refs_list[$prereqs_string]];
  2357. if ($itemToCheck->type === 'quiz') {
  2358. // 1. Checking the status in current items.
  2359. $status = $itemToCheck->get_status(true);
  2360. $returnstatus = $status == $this->possible_status[2] || $status == $this->possible_status[3];
  2361. if (!$returnstatus) {
  2362. $explanation = sprintf(
  2363. get_lang('ItemXBlocksThisElement'),
  2364. $itemToCheck->get_title()
  2365. );
  2366. $this->prereq_alert = $explanation;
  2367. if ($debug) {
  2368. error_log(
  2369. 'New LP - Prerequisite '.$prereqs_string.' not complete',
  2370. 0
  2371. );
  2372. }
  2373. } else {
  2374. if ($debug) {
  2375. error_log(
  2376. 'New LP - Prerequisite '.$prereqs_string.' complete',
  2377. 0
  2378. );
  2379. }
  2380. }
  2381. // For one and first attempt.
  2382. if ($this->prevent_reinit == 1) {
  2383. // 2. If is completed we check the results in the DB of the quiz.
  2384. if ($returnstatus) {
  2385. //AND origin_lp_item_id = '.$user_id.'
  2386. $sql = 'SELECT exe_result, exe_weighting
  2387. FROM '.Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES).'
  2388. WHERE
  2389. exe_exo_id = '.$items[$refs_list[$prereqs_string]]->path.' AND
  2390. exe_user_id = '.$user_id.' AND
  2391. orig_lp_id = '.$this->lp_id.' AND
  2392. orig_lp_item_id = '.$prereqs_string.' AND
  2393. status <> "incomplete" AND
  2394. c_id = '.$course_id.'
  2395. ORDER BY exe_date DESC
  2396. LIMIT 0, 1';
  2397. $rs_quiz = Database::query($sql);
  2398. if ($quiz = Database::fetch_array($rs_quiz)) {
  2399. /** @var learnpathItem $myItemToCheck */
  2400. $myItemToCheck = $items[$refs_list[$this->get_id()]];
  2401. $minScore = $myItemToCheck->getPrerequisiteMinScore();
  2402. $maxScore = $myItemToCheck->getPrerequisiteMaxScore();
  2403. /*if (empty($minScore)) {
  2404. // Try with mastery_score
  2405. $masteryScoreAsMin = $myItemToCheck->get_mastery_score();
  2406. if (!empty($masteryScoreAsMin)) {
  2407. $minScore = $masteryScoreAsMin;
  2408. }
  2409. }*/
  2410. if (isset($minScore) && isset($minScore)) {
  2411. // Taking min/max prerequisites values see BT#5776
  2412. if ($quiz['exe_result'] >= $minScore &&
  2413. $quiz['exe_result'] <= $maxScore
  2414. ) {
  2415. $returnstatus = true;
  2416. } else {
  2417. $explanation = sprintf(
  2418. get_lang('YourResultAtXBlocksThisElement'),
  2419. $itemToCheck->get_title()
  2420. );
  2421. $this->prereq_alert = $explanation;
  2422. $returnstatus = false;
  2423. }
  2424. } else {
  2425. // Classic way
  2426. if ($quiz['exe_result'] >=
  2427. $items[$refs_list[$prereqs_string]]->get_mastery_score()
  2428. ) {
  2429. $returnstatus = true;
  2430. } else {
  2431. $explanation = sprintf(
  2432. get_lang('YourResultAtXBlocksThisElement'),
  2433. $itemToCheck->get_title()
  2434. );
  2435. $this->prereq_alert = $explanation;
  2436. $returnstatus = false;
  2437. }
  2438. }
  2439. } else {
  2440. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2441. $returnstatus = false;
  2442. }
  2443. }
  2444. } else {
  2445. // 3. For multiple attempts we check that there are minimum 1 item completed
  2446. // Checking in the database.
  2447. $sql = 'SELECT exe_result, exe_weighting
  2448. FROM '.Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES).'
  2449. WHERE
  2450. c_id = '.$course_id.' AND
  2451. exe_exo_id = '.$items[$refs_list[$prereqs_string]]->path.' AND
  2452. exe_user_id = '.$user_id.' AND
  2453. orig_lp_id = '.$this->lp_id.' AND
  2454. orig_lp_item_id = '.$prereqs_string;
  2455. $rs_quiz = Database::query($sql);
  2456. if (Database::num_rows($rs_quiz) > 0) {
  2457. while ($quiz = Database::fetch_array($rs_quiz)) {
  2458. /** @var learnpathItem $myItemToCheck */
  2459. $myItemToCheck = $items[$refs_list[$this->get_id()]];
  2460. $minScore = $myItemToCheck->getPrerequisiteMinScore();
  2461. $maxScore = $myItemToCheck->getPrerequisiteMaxScore();
  2462. if (empty($minScore)) {
  2463. // Try with mastery_score
  2464. $masteryScoreAsMin = $myItemToCheck->get_mastery_score();
  2465. if (!empty($masteryScoreAsMin)) {
  2466. $minScore = $masteryScoreAsMin;
  2467. }
  2468. }
  2469. if (isset($minScore) && isset($minScore)) {
  2470. // Taking min/max prerequisites values see BT#5776
  2471. if ($quiz['exe_result'] >= $minScore &&
  2472. $quiz['exe_result'] <= $maxScore
  2473. ) {
  2474. $returnstatus = true;
  2475. break;
  2476. } else {
  2477. $explanation = sprintf(
  2478. get_lang('YourResultAtXBlocksThisElement'),
  2479. $itemToCheck->get_title()
  2480. );
  2481. $this->prereq_alert = $explanation;
  2482. $returnstatus = false;
  2483. }
  2484. } else {
  2485. if ($quiz['exe_result'] >=
  2486. $items[$refs_list[$prereqs_string]]->get_mastery_score()
  2487. ) {
  2488. $returnstatus = true;
  2489. break;
  2490. } else {
  2491. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2492. $returnstatus = false;
  2493. }
  2494. }
  2495. }
  2496. } else {
  2497. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2498. $returnstatus = false;
  2499. }
  2500. }
  2501. return $returnstatus;
  2502. } elseif ($itemToCheck->type === 'student_publication') {
  2503. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  2504. $workId = $items[$refs_list[$prereqs_string]]->path;
  2505. $count = get_work_count_by_student($user_id, $workId);
  2506. if ($count >= 1) {
  2507. $returnstatus = true;
  2508. } else {
  2509. $returnstatus = false;
  2510. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2511. if (self::DEBUG > 1) {
  2512. error_log(
  2513. 'Student pub, prereq'.$prereqs_string.' not completed',
  2514. 0
  2515. );
  2516. }
  2517. }
  2518. return $returnstatus;
  2519. } else {
  2520. $status = $itemToCheck->get_status(false);
  2521. $returnstatus = $status == $this->possible_status[2] || $status == $this->possible_status[3];
  2522. if (!$returnstatus) {
  2523. $explanation = sprintf(
  2524. get_lang('ItemXBlocksThisElement'),
  2525. $itemToCheck->get_title()
  2526. );
  2527. $this->prereq_alert = $explanation;
  2528. if (self::DEBUG > 1) {
  2529. error_log(
  2530. 'New LP - Prerequisite '.$prereqs_string.' not complete',
  2531. 0
  2532. );
  2533. }
  2534. } else {
  2535. if (self::DEBUG > 1) {
  2536. error_log(
  2537. 'New LP - Prerequisite '.$prereqs_string.' complete',
  2538. 0
  2539. );
  2540. }
  2541. }
  2542. if ($returnstatus && $this->prevent_reinit == 1) {
  2543. // I would prefer check in the database.
  2544. $lp_item_view = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  2545. $lp_view = Database::get_course_table(TABLE_LP_VIEW);
  2546. $sql = 'SELECT iid FROM '.$lp_view.'
  2547. WHERE
  2548. c_id = '.$course_id.' AND
  2549. user_id = '.$user_id.' AND
  2550. lp_id = '.$this->lp_id.' AND
  2551. session_id = '.$sessionId.'
  2552. LIMIT 0, 1';
  2553. $rs_lp = Database::query($sql);
  2554. if (Database::num_rows($rs_lp)) {
  2555. $lp_id = Database::fetch_row($rs_lp);
  2556. $my_lp_id = $lp_id[0];
  2557. $sql = 'SELECT status FROM '.$lp_item_view.'
  2558. WHERE
  2559. c_id = '.$course_id.' AND
  2560. lp_view_id = '.$my_lp_id.' AND
  2561. lp_item_id = '.$refs_list[$prereqs_string].'
  2562. LIMIT 0, 1';
  2563. $rs_lp = Database::query($sql);
  2564. $status_array = Database::fetch_row($rs_lp);
  2565. $status = $status_array[0];
  2566. $returnstatus = $status == $this->possible_status[2] || $status == $this->possible_status[3];
  2567. if (!$returnstatus && empty($this->prereq_alert)) {
  2568. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2569. }
  2570. if (!$returnstatus) {
  2571. if (self::DEBUG > 1) {
  2572. error_log(
  2573. 'New LP - Prerequisite '.$prereqs_string.' not complete'
  2574. );
  2575. }
  2576. } else {
  2577. if (self::DEBUG > 1) {
  2578. error_log('New LP - Prerequisite '.$prereqs_string.' complete');
  2579. }
  2580. }
  2581. return $returnstatus;
  2582. }
  2583. } else {
  2584. return $returnstatus;
  2585. }
  2586. }
  2587. } else {
  2588. if (self::DEBUG > 1) {
  2589. error_log(
  2590. 'New LP - Could not find '.$prereqs_string.' in '.print_r(
  2591. $refs_list,
  2592. true
  2593. ),
  2594. 0
  2595. );
  2596. }
  2597. }
  2598. }
  2599. }
  2600. }
  2601. }
  2602. }
  2603. } else {
  2604. $list = explode("\|", $prereqs_string);
  2605. if (count($list) > 1) {
  2606. if (self::DEBUG > 1) {
  2607. error_log('New LP - Found OR, looking into it', 0);
  2608. }
  2609. $orstatus = false;
  2610. foreach ($list as $condition) {
  2611. if (self::DEBUG > 1) {
  2612. error_log(
  2613. 'New LP - Found OR, adding it ('.$condition.')',
  2614. 0
  2615. );
  2616. }
  2617. $orstatus = $orstatus || $this->parse_prereq(
  2618. $condition,
  2619. $items,
  2620. $refs_list,
  2621. $user_id
  2622. );
  2623. if ($orstatus) {
  2624. // Shortcircuit OR.
  2625. if (self::DEBUG > 1) {
  2626. error_log(
  2627. 'New LP - One condition in OR was true, short-circuit',
  2628. 0
  2629. );
  2630. }
  2631. break;
  2632. }
  2633. }
  2634. if (!$orstatus && empty($this->prereq_alert)) {
  2635. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2636. }
  2637. return $orstatus;
  2638. } else {
  2639. if (self::DEBUG > 1) {
  2640. error_log(
  2641. 'New LP - OR was found but only one elem present !?',
  2642. 0
  2643. );
  2644. }
  2645. if (isset($items[$refs_list[$list[0]]])) {
  2646. $status = $items[$refs_list[$list[0]]]->get_status(true);
  2647. $returnstatus = $status == 'completed' || $status == 'passed';
  2648. if (!$returnstatus && empty($this->prereq_alert)) {
  2649. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2650. }
  2651. return $returnstatus;
  2652. }
  2653. }
  2654. }
  2655. if (empty($this->prereq_alert)) {
  2656. $this->prereq_alert = get_lang('LearnpathPrereqNotCompleted');
  2657. }
  2658. if (self::DEBUG > 1) {
  2659. error_log(
  2660. 'New LP - End of parse_prereq. Error code is now '.$this->prereq_alert,
  2661. 0
  2662. );
  2663. }
  2664. return false;
  2665. }
  2666. /**
  2667. * Reinits all local values as the learnpath is restarted.
  2668. *
  2669. * @return bool True on success, false otherwise
  2670. */
  2671. public function restart()
  2672. {
  2673. if (self::DEBUG > 0) {
  2674. error_log('learnpathItem::restart()', 0);
  2675. }
  2676. $seriousGame = $this->get_seriousgame_mode();
  2677. //For serious game : We reuse same attempt_id
  2678. if ($seriousGame == 1 && $this->type == 'sco') {
  2679. // If this is a sco, Chamilo can't update the time without an
  2680. // explicit scorm call
  2681. $this->current_start_time = 0;
  2682. $this->current_stop_time = 0; //Those 0 value have this effect
  2683. $this->last_scorm_session_time = 0;
  2684. $this->save();
  2685. return true;
  2686. }
  2687. $this->save();
  2688. $allowed = $this->isRestartAllowed();
  2689. if ($allowed === -1) {
  2690. // Nothing allowed, do nothing.
  2691. } elseif ($allowed === 1) {
  2692. // Restart as new attempt is allowed, record a new attempt.
  2693. $this->attempt_id = $this->attempt_id + 1; // Simply reuse the previous attempt_id.
  2694. $this->current_score = 0;
  2695. $this->current_start_time = 0;
  2696. $this->current_stop_time = 0;
  2697. $this->current_data = '';
  2698. $this->status = $this->possible_status[0];
  2699. $this->interactions_count = 0;
  2700. $this->interactions = [];
  2701. $this->objectives_count = 0;
  2702. $this->objectives = [];
  2703. $this->lesson_location = '';
  2704. if ($this->type != TOOL_QUIZ) {
  2705. $this->write_to_db();
  2706. }
  2707. } else {
  2708. // Restart current element is allowed (because it's not finished yet),
  2709. // reinit current.
  2710. //$this->current_score = 0;
  2711. $this->current_start_time = 0;
  2712. $this->current_stop_time = 0;
  2713. $this->interactions_count = $this->get_interactions_count(true);
  2714. }
  2715. return true;
  2716. }
  2717. /**
  2718. * Saves data in the database.
  2719. *
  2720. * @param bool $from_outside Save from URL params (1) or from object attributes (0)
  2721. * @param bool $prereqs_complete The results of a check on prerequisites for this item.
  2722. * True if prerequisites are completed, false otherwise. Defaults to false. Only used if not sco or au
  2723. *
  2724. * @return bool True on success, false on failure
  2725. */
  2726. public function save($from_outside = true, $prereqs_complete = false)
  2727. {
  2728. $debug = self::DEBUG;
  2729. if ($debug) {
  2730. error_log('learnpathItem::save()', 0);
  2731. }
  2732. // First check if parameters passed via GET can be saved here
  2733. // in case it's a SCORM, we should get:
  2734. if ($this->type == 'sco' || $this->type == 'au') {
  2735. $status = $this->get_status(true);
  2736. if ($this->prevent_reinit == 1 &&
  2737. $status != $this->possible_status[0] && // not attempted
  2738. $status != $this->possible_status[1] //incomplete
  2739. ) {
  2740. if ($debug) {
  2741. error_log(
  2742. 'learnpathItem::save() - save reinit blocked by setting',
  2743. 0
  2744. );
  2745. }
  2746. // Do nothing because the status has already been set. Don't allow it to change.
  2747. // TODO: Check there isn't a special circumstance where this should be saved.
  2748. } else {
  2749. if ($debug) {
  2750. error_log(
  2751. 'learnpathItem::save() - SCORM save request received',
  2752. 0
  2753. );
  2754. }
  2755. // Get all new settings from the URL
  2756. if ($from_outside) {
  2757. if ($debug) {
  2758. error_log(
  2759. 'learnpathItem::save() - Getting item data from outside',
  2760. 0
  2761. );
  2762. }
  2763. foreach ($_GET as $param => $value) {
  2764. switch ($param) {
  2765. case 'score':
  2766. $this->set_score($value);
  2767. if ($debug) {
  2768. error_log(
  2769. 'learnpathItem::save() - setting score to '.$value,
  2770. 0
  2771. );
  2772. }
  2773. break;
  2774. case 'max':
  2775. $this->set_max_score($value);
  2776. if ($debug) {
  2777. error_log(
  2778. 'learnpathItem::save() - setting view_max_score to '.$value,
  2779. 0
  2780. );
  2781. }
  2782. break;
  2783. case 'min':
  2784. $this->min_score = $value;
  2785. if ($debug) {
  2786. error_log(
  2787. 'learnpathItem::save() - setting min_score to '.$value,
  2788. 0
  2789. );
  2790. }
  2791. break;
  2792. case 'lesson_status':
  2793. if (!empty($value)) {
  2794. $this->set_status($value);
  2795. if ($debug) {
  2796. error_log(
  2797. 'learnpathItem::save() - setting status to '.$value,
  2798. 0
  2799. );
  2800. }
  2801. }
  2802. break;
  2803. case 'time':
  2804. $this->set_time($value);
  2805. if ($debug) {
  2806. error_log(
  2807. 'learnpathItem::save() - setting time to '.$value,
  2808. 0
  2809. );
  2810. }
  2811. break;
  2812. case 'suspend_data':
  2813. $this->current_data = $value;
  2814. if ($debug) {
  2815. error_log(
  2816. 'learnpathItem::save() - setting suspend_data to '.$value,
  2817. 0
  2818. );
  2819. }
  2820. break;
  2821. case 'lesson_location':
  2822. $this->set_lesson_location($value);
  2823. if ($debug) {
  2824. error_log(
  2825. 'learnpathItem::save() - setting lesson_location to '.$value,
  2826. 0
  2827. );
  2828. }
  2829. break;
  2830. case 'core_exit':
  2831. $this->set_core_exit($value);
  2832. if ($debug) {
  2833. error_log(
  2834. 'learnpathItem::save() - setting core_exit to '.$value,
  2835. 0
  2836. );
  2837. }
  2838. break;
  2839. case 'interactions':
  2840. break;
  2841. case 'objectives':
  2842. break;
  2843. default:
  2844. // Ignore.
  2845. break;
  2846. }
  2847. }
  2848. } else {
  2849. if ($debug) {
  2850. error_log(
  2851. 'learnpathItem::save() - Using inside item status',
  2852. 0
  2853. );
  2854. }
  2855. // Do nothing, just let the local attributes be used.
  2856. }
  2857. }
  2858. } else {
  2859. // If not SCO, such messages should not be expected.
  2860. $type = strtolower($this->type);
  2861. if ($debug) {
  2862. error_log("type: $type");
  2863. }
  2864. switch ($type) {
  2865. case 'asset':
  2866. if ($prereqs_complete) {
  2867. $this->set_status($this->possible_status[2]);
  2868. }
  2869. break;
  2870. case TOOL_HOTPOTATOES:
  2871. break;
  2872. case TOOL_QUIZ:
  2873. return false;
  2874. break;
  2875. default:
  2876. // For now, everything that is not sco and not asset is set to
  2877. // completed when saved.
  2878. if ($prereqs_complete) {
  2879. $this->set_status($this->possible_status[2]);
  2880. }
  2881. break;
  2882. }
  2883. }
  2884. if ($debug) {
  2885. error_log(
  2886. 'New LP - End of learnpathItem::save() - Calling write_to_db()',
  2887. 0
  2888. );
  2889. }
  2890. return $this->write_to_db();
  2891. }
  2892. /**
  2893. * Sets the number of attempt_id to a given value.
  2894. *
  2895. * @param int $num The given value to set attempt_id to
  2896. *
  2897. * @return bool TRUE on success, FALSE otherwise
  2898. */
  2899. public function set_attempt_id($num)
  2900. {
  2901. if (self::DEBUG > 0) {
  2902. error_log('learnpathItem::set_attempt_id()', 0);
  2903. }
  2904. if ($num == strval(intval($num)) && $num >= 0) {
  2905. $this->attempt_id = $num;
  2906. return true;
  2907. }
  2908. return false;
  2909. }
  2910. /**
  2911. * Sets the core_exit value to the one given.
  2912. *
  2913. * @return bool $value True (always)
  2914. */
  2915. public function set_core_exit($value)
  2916. {
  2917. switch ($value) {
  2918. case '':
  2919. $this->core_exit = '';
  2920. break;
  2921. case 'suspend':
  2922. $this->core_exit = 'suspend';
  2923. break;
  2924. default:
  2925. $this->core_exit = 'none';
  2926. break;
  2927. }
  2928. return true;
  2929. }
  2930. /**
  2931. * Sets the item's description.
  2932. *
  2933. * @param string $string Description
  2934. */
  2935. public function set_description($string = '')
  2936. {
  2937. if (self::DEBUG > 0) {
  2938. error_log('learnpathItem::set_description()', 0);
  2939. }
  2940. if (!empty($string)) {
  2941. $this->description = $string;
  2942. }
  2943. }
  2944. /**
  2945. * Sets the lesson_location value.
  2946. *
  2947. * @param string $location lesson_location as provided by the SCO
  2948. *
  2949. * @return bool True on success, false otherwise
  2950. */
  2951. public function set_lesson_location($location)
  2952. {
  2953. if (self::DEBUG > 0) {
  2954. error_log('learnpathItem::set_lesson_location()', 0);
  2955. }
  2956. if (isset($location)) {
  2957. $this->lesson_location = $location;
  2958. return true;
  2959. }
  2960. return false;
  2961. }
  2962. /**
  2963. * Sets the item's depth level in the LP tree (0 is at root).
  2964. *
  2965. * @param int $int Level
  2966. */
  2967. public function set_level($int = 0)
  2968. {
  2969. if (self::DEBUG > 0) {
  2970. error_log('learnpathItem::set_level('.$int.')', 0);
  2971. }
  2972. if (!empty($int) && $int == strval(intval($int))) {
  2973. $this->level = $int;
  2974. }
  2975. }
  2976. /**
  2977. * Sets the lp_view id this item view is registered to.
  2978. *
  2979. * @param int $lp_view_id lp_view DB ID
  2980. * @param int $course_id
  2981. *
  2982. * @return bool
  2983. *
  2984. * @todo //todo insert into lp_item_view if lp_view not exists
  2985. */
  2986. public function set_lp_view($lp_view_id, $course_id = null)
  2987. {
  2988. $lp_view_id = intval($lp_view_id);
  2989. if (empty($course_id)) {
  2990. $course_id = api_get_course_int_id();
  2991. } else {
  2992. $course_id = intval($course_id);
  2993. }
  2994. $lpItemId = $this->get_id();
  2995. if (empty($lpItemId)) {
  2996. if (self::DEBUG > 0) {
  2997. error_log(
  2998. 'learnpathItem::set_lp_view('.$lp_view_id.') $lpItemId is empty',
  2999. 0
  3000. );
  3001. }
  3002. return false;
  3003. }
  3004. if (empty($lp_view_id)) {
  3005. if (self::DEBUG > 0) {
  3006. error_log(
  3007. 'learnpathItem::set_lp_view('.$lp_view_id.') $lp_view_id is empty',
  3008. 0
  3009. );
  3010. }
  3011. return false;
  3012. }
  3013. if (self::DEBUG > 0) {
  3014. error_log('learnpathItem::set_lp_view('.$lp_view_id.')', 0);
  3015. }
  3016. $this->view_id = $lp_view_id;
  3017. $item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3018. // Get the lp_item_view with the highest view_count.
  3019. $sql = "SELECT * FROM $item_view_table
  3020. WHERE
  3021. c_id = $course_id AND
  3022. lp_item_id = ".$lpItemId." AND
  3023. lp_view_id = ".$lp_view_id."
  3024. ORDER BY view_count DESC";
  3025. if (self::DEBUG > 2) {
  3026. error_log(
  3027. 'learnpathItem::set_lp_view() - Querying lp_item_view: '.$sql,
  3028. 0
  3029. );
  3030. }
  3031. $res = Database::query($sql);
  3032. if (Database::num_rows($res) > 0) {
  3033. $row = Database::fetch_array($res);
  3034. $this->db_item_view_id = $row['iid'];
  3035. $this->attempt_id = $row['view_count'];
  3036. $this->current_score = $row['score'];
  3037. $this->current_data = $row['suspend_data'];
  3038. $this->view_max_score = $row['max_score'];
  3039. $this->status = $row['status'];
  3040. $this->current_start_time = $row['start_time'];
  3041. $this->current_stop_time = $this->current_start_time + $row['total_time'];
  3042. $this->lesson_location = $row['lesson_location'];
  3043. $this->core_exit = $row['core_exit'];
  3044. if (self::DEBUG > 2) {
  3045. error_log(
  3046. 'learnpathItem::set_lp_view() - Updated item object with database values',
  3047. 0
  3048. );
  3049. }
  3050. // Now get the number of interactions for this little guy.
  3051. $table = Database::get_course_table(TABLE_LP_IV_INTERACTION);
  3052. $sql = "SELECT * FROM $table
  3053. WHERE
  3054. c_id = $course_id AND
  3055. lp_iv_id = '".$this->db_item_view_id."'";
  3056. $res = Database::query($sql);
  3057. if ($res !== false) {
  3058. $this->interactions_count = Database::num_rows($res);
  3059. } else {
  3060. $this->interactions_count = 0;
  3061. }
  3062. // Now get the number of objectives for this little guy.
  3063. $table = Database::get_course_table(TABLE_LP_IV_OBJECTIVE);
  3064. $sql = "SELECT * FROM $table
  3065. WHERE
  3066. c_id = $course_id AND
  3067. lp_iv_id = '".$this->db_item_view_id."'";
  3068. $res = Database::query($sql);
  3069. if ($res !== false) {
  3070. $this->objectives_count = Database::num_rows($res);
  3071. } else {
  3072. $this->objectives_count = 0;
  3073. }
  3074. }
  3075. // End
  3076. if (self::DEBUG > 2) {
  3077. error_log('New LP - End of learnpathItem::set_lp_view()', 0);
  3078. }
  3079. return true;
  3080. }
  3081. /**
  3082. * Sets the path.
  3083. *
  3084. * @param string $string Path
  3085. */
  3086. public function set_path($string = '')
  3087. {
  3088. if (self::DEBUG > 0) {
  3089. error_log('learnpathItem::set_path()', 0);
  3090. }
  3091. if (!empty($string)) {
  3092. $this->path = $string;
  3093. }
  3094. }
  3095. /**
  3096. * Sets the prevent_reinit attribute.
  3097. * This is based on the LP value and is set at creation time for
  3098. * each learnpathItem. It is a (bad?) way of avoiding
  3099. * a reference to the LP when saving an item.
  3100. *
  3101. * @param int 1 for "prevent", 0 for "don't prevent"
  3102. * saving freshened values (new "not attempted" status etc)
  3103. */
  3104. public function set_prevent_reinit($prevent)
  3105. {
  3106. if (self::DEBUG > 0) {
  3107. error_log('learnpathItem::set_prevent_reinit()', 0);
  3108. }
  3109. if ($prevent) {
  3110. $this->prevent_reinit = 1;
  3111. } else {
  3112. $this->prevent_reinit = 0;
  3113. }
  3114. }
  3115. /**
  3116. * Sets the score value. If the mastery_score is set and the score reaches
  3117. * it, then set the status to 'passed'.
  3118. *
  3119. * @param float $score Score
  3120. *
  3121. * @return bool True on success, false otherwise
  3122. */
  3123. public function set_score($score)
  3124. {
  3125. $debug = self::DEBUG;
  3126. if ($debug > 0) {
  3127. error_log('learnpathItem::set_score('.$score.')', 0);
  3128. }
  3129. if (($this->max_score <= 0 || $score <= $this->max_score) && ($score >= $this->min_score)) {
  3130. $this->current_score = $score;
  3131. $masteryScore = $this->get_mastery_score();
  3132. $current_status = $this->get_status(false);
  3133. // Fixes bug when SCORM doesn't send a mastery score even if they sent a score!
  3134. if ($masteryScore == -1) {
  3135. $masteryScore = $this->max_score;
  3136. }
  3137. if ($debug > 0) {
  3138. error_log('get_mastery_score: '.$masteryScore);
  3139. error_log('current_status: '.$current_status);
  3140. error_log('current score : '.$this->current_score);
  3141. }
  3142. // If mastery_score is set AND the current score reaches the mastery
  3143. // score AND the current status is different from 'completed', then
  3144. // set it to 'passed'.
  3145. /*
  3146. if ($master != -1 && $this->current_score >= $master && $current_status != $this->possible_status[2]) {
  3147. if ($debug > 0) error_log('Status changed to: '.$this->possible_status[3]);
  3148. $this->set_status($this->possible_status[3]); //passed
  3149. } elseif ($master != -1 && $this->current_score < $master) {
  3150. if ($debug > 0) error_log('Status changed to: '.$this->possible_status[4]);
  3151. $this->set_status($this->possible_status[4]); //failed
  3152. }*/
  3153. return true;
  3154. }
  3155. return false;
  3156. }
  3157. /**
  3158. * Sets the maximum score for this item.
  3159. *
  3160. * @param int $score Maximum score - must be a decimal or an empty string
  3161. *
  3162. * @return bool True on success, false on error
  3163. */
  3164. public function set_max_score($score)
  3165. {
  3166. if (self::DEBUG > 0) {
  3167. error_log('learnpathItem::set_max_score('.$score.')', 0);
  3168. }
  3169. if (is_int($score) || $score == '') {
  3170. $this->view_max_score = $score;
  3171. if (self::DEBUG > 1) {
  3172. error_log(
  3173. 'learnpathItem::set_max_score() - '.
  3174. 'Updated object score of item '.$this->db_id.
  3175. ' to '.$this->view_max_score,
  3176. 0
  3177. );
  3178. }
  3179. return true;
  3180. }
  3181. return false;
  3182. }
  3183. /**
  3184. * Sets the status for this item.
  3185. *
  3186. * @param string $status Status - must be one of the values defined in $this->possible_status
  3187. * (this affects the status setting)
  3188. *
  3189. * @return bool True on success, false on error
  3190. */
  3191. public function set_status($status)
  3192. {
  3193. if (self::DEBUG > 0) {
  3194. error_log('learnpathItem::set_status('.$status.')', 0);
  3195. }
  3196. $found = false;
  3197. foreach ($this->possible_status as $possible) {
  3198. if (preg_match('/^'.$possible.'$/i', $status)) {
  3199. $found = true;
  3200. }
  3201. }
  3202. if ($found) {
  3203. $this->status = $status;
  3204. if (self::DEBUG > 1) {
  3205. error_log(
  3206. 'learnpathItem::set_status() - '.
  3207. 'Updated object status of item '.$this->db_id.
  3208. ' to '.$this->status,
  3209. 0
  3210. );
  3211. }
  3212. return true;
  3213. }
  3214. $this->status = $this->possible_status[0];
  3215. return false;
  3216. }
  3217. /**
  3218. * Set the (indexing) terms for this learnpath item.
  3219. *
  3220. * @param string $terms Terms, as a comma-split list
  3221. *
  3222. * @return bool Always return true
  3223. */
  3224. public function set_terms($terms)
  3225. {
  3226. global $charset;
  3227. $lp_item = Database::get_course_table(TABLE_LP_ITEM);
  3228. $a_terms = preg_split('/,/', $terms);
  3229. $i_terms = preg_split('/,/', $this->get_terms());
  3230. foreach ($i_terms as $term) {
  3231. if (!in_array($term, $a_terms)) {
  3232. array_push($a_terms, $term);
  3233. }
  3234. }
  3235. $new_terms = $a_terms;
  3236. $new_terms_string = implode(',', $new_terms);
  3237. // TODO: Validate csv string.
  3238. $terms = Database::escape_string(api_htmlentities($new_terms_string, ENT_QUOTES, $charset));
  3239. $sql = "UPDATE $lp_item
  3240. SET terms = '$terms'
  3241. WHERE iid=".$this->get_id();
  3242. Database::query($sql);
  3243. // Save it to search engine.
  3244. if (api_get_setting('search_enabled') == 'true') {
  3245. $di = new ChamiloIndexer();
  3246. $di->update_terms($this->get_search_did(), $new_terms, 'T');
  3247. }
  3248. return true;
  3249. }
  3250. /**
  3251. * Get the document ID from inside the text index database.
  3252. *
  3253. * @return int Search index database document ID
  3254. */
  3255. public function get_search_did()
  3256. {
  3257. return $this->search_did;
  3258. }
  3259. /**
  3260. * Sets the item viewing time in a usable form, given that SCORM packages
  3261. * often give it as 00:00:00.0000.
  3262. *
  3263. * @param string Time as given by SCORM
  3264. * @param string $format
  3265. */
  3266. public function set_time($scorm_time, $format = 'scorm')
  3267. {
  3268. $debug = self::DEBUG;
  3269. if ($debug) {
  3270. error_log("learnpathItem::set_time($scorm_time, $format)");
  3271. error_log("this->type: ".$this->type);
  3272. error_log("this->current_start_time: ".$this->current_start_time);
  3273. }
  3274. if ($scorm_time == '0' &&
  3275. $this->type != 'sco' &&
  3276. $this->current_start_time != 0
  3277. ) {
  3278. $myTime = time() - $this->current_start_time;
  3279. if ($myTime > 0) {
  3280. $this->update_time($myTime);
  3281. if ($debug) {
  3282. error_log('found asset - set time to '.$myTime);
  3283. }
  3284. } else {
  3285. if ($debug) {
  3286. error_log('Time not set');
  3287. }
  3288. }
  3289. } else {
  3290. switch ($format) {
  3291. case 'scorm':
  3292. $res = [];
  3293. if (preg_match(
  3294. '/^(\d{1,4}):(\d{2}):(\d{2})(\.\d{1,4})?/',
  3295. $scorm_time,
  3296. $res
  3297. )
  3298. ) {
  3299. $hour = $res[1];
  3300. $min = $res[2];
  3301. $sec = $res[3];
  3302. // Getting total number of seconds spent.
  3303. $totalSec = $hour * 3600 + $min * 60 + $sec;
  3304. if ($debug) {
  3305. error_log("totalSec : $totalSec");
  3306. error_log("Now calling to scorm_update_time()");
  3307. }
  3308. $this->scorm_update_time($totalSec);
  3309. }
  3310. break;
  3311. case 'int':
  3312. if ($debug) {
  3313. error_log("scorm_time = $scorm_time");
  3314. error_log("Now calling to scorm_update_time()");
  3315. }
  3316. $this->scorm_update_time($scorm_time);
  3317. break;
  3318. }
  3319. }
  3320. }
  3321. /**
  3322. * Sets the item's title.
  3323. *
  3324. * @param string $string Title
  3325. */
  3326. public function set_title($string = '')
  3327. {
  3328. if (self::DEBUG > 0) {
  3329. error_log('learnpathItem::set_title()', 0);
  3330. }
  3331. if (!empty($string)) {
  3332. $this->title = $string;
  3333. }
  3334. }
  3335. /**
  3336. * Sets the item's type.
  3337. *
  3338. * @param string $string Type
  3339. */
  3340. public function set_type($string = '')
  3341. {
  3342. if (self::DEBUG > 0) {
  3343. error_log('learnpathItem::set_type()', 0);
  3344. }
  3345. if (!empty($string)) {
  3346. $this->type = $string;
  3347. }
  3348. }
  3349. /**
  3350. * Checks if the current status is part of the list of status given.
  3351. *
  3352. * @param array $list An array of status to check for.
  3353. * If the current status is one of the strings, return true
  3354. *
  3355. * @return bool True if the status was one of the given strings,
  3356. * false otherwise
  3357. */
  3358. public function status_is($list = [])
  3359. {
  3360. if (self::DEBUG > 1) {
  3361. error_log(
  3362. 'learnpathItem::status_is('.print_r(
  3363. $list,
  3364. true
  3365. ).') on item '.$this->db_id,
  3366. 0
  3367. );
  3368. }
  3369. $currentStatus = $this->get_status(true);
  3370. if (empty($currentStatus)) {
  3371. return false;
  3372. }
  3373. $found = false;
  3374. foreach ($list as $status) {
  3375. if (preg_match('/^'.$status.'$/i', $currentStatus)) {
  3376. if (self::DEBUG > 2) {
  3377. error_log(
  3378. 'New LP - learnpathItem::status_is() - Found status '.
  3379. $status.' corresponding to current status',
  3380. 0
  3381. );
  3382. }
  3383. $found = true;
  3384. return $found;
  3385. }
  3386. }
  3387. if (self::DEBUG > 2) {
  3388. error_log(
  3389. 'New LP - learnpathItem::status_is() - Status '.
  3390. $currentStatus.' did not match request',
  3391. 0
  3392. );
  3393. }
  3394. return $found;
  3395. }
  3396. /**
  3397. * Updates the time info according to the given session_time.
  3398. *
  3399. * @param int $totalSec Time in seconds
  3400. */
  3401. public function update_time($totalSec = 0)
  3402. {
  3403. if (self::DEBUG > 0) {
  3404. error_log('learnpathItem::update_time('.$totalSec.')');
  3405. }
  3406. if ($totalSec >= 0) {
  3407. // Getting start time from finish time. The only problem in the calculation is it might be
  3408. // modified by the scripts processing time.
  3409. $now = time();
  3410. $start = $now - $totalSec;
  3411. $this->current_start_time = $start;
  3412. $this->current_stop_time = $now;
  3413. }
  3414. }
  3415. /**
  3416. * Special scorm update time function. This function will update time
  3417. * directly into db for scorm objects.
  3418. *
  3419. * @param int $total_sec Total number of seconds
  3420. */
  3421. public function scorm_update_time($total_sec = 0)
  3422. {
  3423. $debug = self::DEBUG;
  3424. if ($debug) {
  3425. error_log('learnpathItem::scorm_update_time()');
  3426. error_log("total_sec: $total_sec");
  3427. }
  3428. // Step 1 : get actual total time stored in db
  3429. $item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3430. $course_id = api_get_course_int_id();
  3431. $sql = 'SELECT total_time, status
  3432. FROM '.$item_view_table.'
  3433. WHERE
  3434. c_id = '.$course_id.' AND
  3435. lp_item_id = "'.$this->db_id.'" AND
  3436. lp_view_id = "'.$this->view_id.'" AND
  3437. view_count = "'.$this->get_attempt_id().'"';
  3438. $result = Database::query($sql);
  3439. $row = Database::fetch_array($result);
  3440. if (!isset($row['total_time'])) {
  3441. $total_time = 0;
  3442. } else {
  3443. $total_time = $row['total_time'];
  3444. }
  3445. if ($debug) {
  3446. error_log("Original total_time: $total_time");
  3447. }
  3448. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  3449. $lp_id = (int) $this->lp_id;
  3450. $sql = "SELECT * FROM $lp_table WHERE iid = $lp_id";
  3451. $res = Database::query($sql);
  3452. $accumulateScormTime = 'false';
  3453. if (Database::num_rows($res) > 0) {
  3454. $row = Database::fetch_assoc($res);
  3455. $accumulateScormTime = $row['accumulate_scorm_time'];
  3456. }
  3457. // Step 2.1 : if normal mode total_time = total_time + total_sec
  3458. if ($this->type == 'sco' && $accumulateScormTime != 0) {
  3459. if ($debug) {
  3460. error_log("accumulateScormTime is on. total_time modified: $total_time + $total_sec");
  3461. }
  3462. $total_time += $total_sec;
  3463. } else {
  3464. // Step 2.2 : if not cumulative mode total_time = total_time - last_update + total_sec
  3465. $total_sec = $this->fixAbusiveTime($total_sec);
  3466. if ($debug) {
  3467. error_log("after fix abusive: $total_sec");
  3468. error_log("total_time: $total_time");
  3469. error_log("this->last_scorm_session_time: ".$this->last_scorm_session_time);
  3470. }
  3471. $total_time = $total_time - $this->last_scorm_session_time + $total_sec;
  3472. $this->last_scorm_session_time = $total_sec;
  3473. if ($total_time < 0) {
  3474. $total_time = $total_sec;
  3475. }
  3476. }
  3477. if ($debug) {
  3478. error_log("accumulate_scorm_time: $accumulateScormTime");
  3479. error_log("total_time modified: $total_time");
  3480. }
  3481. // Step 3 update db only if status != completed, passed, browsed or seriousgamemode not activated
  3482. // @todo complete
  3483. $case_completed = [
  3484. 'completed',
  3485. 'passed',
  3486. 'browsed',
  3487. 'failed',
  3488. ];
  3489. if ($this->seriousgame_mode != 1 ||
  3490. !in_array($row['status'], $case_completed)
  3491. ) {
  3492. $sql = "UPDATE $item_view_table
  3493. SET total_time = '$total_time'
  3494. WHERE
  3495. c_id = $course_id AND
  3496. lp_item_id = {$this->db_id} AND
  3497. lp_view_id = {$this->view_id} AND
  3498. view_count = {$this->get_attempt_id()}";
  3499. if ($debug) {
  3500. error_log('-------------total_time updated ------------------------');
  3501. error_log($sql);
  3502. error_log('-------------------------------------');
  3503. }
  3504. Database::query($sql);
  3505. }
  3506. }
  3507. /**
  3508. * Set the total_time to 0 into db.
  3509. */
  3510. public function scorm_init_time()
  3511. {
  3512. $table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3513. $course_id = api_get_course_int_id();
  3514. $sql = 'UPDATE '.$table.'
  3515. SET total_time = 0,
  3516. start_time = '.time().'
  3517. WHERE
  3518. c_id = '.$course_id.' AND
  3519. lp_item_id = "'.$this->db_id.'" AND
  3520. lp_view_id = "'.$this->view_id.'" AND
  3521. view_count = "'.$this->attempt_id.'"';
  3522. Database::query($sql);
  3523. }
  3524. /**
  3525. * Write objectives to DB. This method is separate from write_to_db() because otherwise
  3526. * objectives are lost as a side effect to AJAX and session concurrent access.
  3527. *
  3528. * @return bool True or false on error
  3529. */
  3530. public function write_objectives_to_db()
  3531. {
  3532. if (self::DEBUG > 0) {
  3533. error_log('learnpathItem::write_objectives_to_db()', 0);
  3534. }
  3535. $course_id = api_get_course_int_id();
  3536. if (is_array($this->objectives) && count($this->objectives) > 0) {
  3537. // Save objectives.
  3538. $tbl = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3539. $sql = "SELECT iid
  3540. FROM $tbl
  3541. WHERE
  3542. c_id = $course_id AND
  3543. lp_item_id = ".$this->db_id." AND
  3544. lp_view_id = ".$this->view_id." AND
  3545. view_count = ".$this->attempt_id;
  3546. $res = Database::query($sql);
  3547. if (Database::num_rows($res) > 0) {
  3548. $row = Database::fetch_array($res);
  3549. $lp_iv_id = $row[0];
  3550. if (self::DEBUG > 2) {
  3551. error_log(
  3552. 'learnpathItem::write_to_db() - Got item_view_id '.
  3553. $lp_iv_id.', now checking objectives ',
  3554. 0
  3555. );
  3556. }
  3557. foreach ($this->objectives as $index => $objective) {
  3558. $iva_table = Database::get_course_table(
  3559. TABLE_LP_IV_OBJECTIVE
  3560. );
  3561. $iva_sql = "SELECT iid FROM $iva_table
  3562. WHERE
  3563. c_id = $course_id AND
  3564. lp_iv_id = $lp_iv_id AND
  3565. objective_id = '".Database::escape_string($objective[0])."'";
  3566. $iva_res = Database::query($iva_sql);
  3567. // id(0), type(1), time(2), weighting(3),
  3568. // correct_responses(4), student_response(5),
  3569. // result(6), latency(7)
  3570. if (Database::num_rows($iva_res) > 0) {
  3571. // Update (or don't).
  3572. $iva_row = Database::fetch_array($iva_res);
  3573. $iva_id = $iva_row[0];
  3574. $ivau_sql = "UPDATE $iva_table ".
  3575. "SET objective_id = '".Database::escape_string($objective[0])."',".
  3576. "status = '".Database::escape_string($objective[1])."',".
  3577. "score_raw = '".Database::escape_string($objective[2])."',".
  3578. "score_min = '".Database::escape_string($objective[4])."',".
  3579. "score_max = '".Database::escape_string($objective[3])."' ".
  3580. "WHERE c_id = $course_id AND iid = $iva_id";
  3581. Database::query($ivau_sql);
  3582. } else {
  3583. // Insert new one.
  3584. $params = [
  3585. 'c_id' => $course_id,
  3586. 'lp_iv_id' => $lp_iv_id,
  3587. 'order_id' => $index,
  3588. 'objective_id' => $objective[0],
  3589. 'status' => $objective[1],
  3590. 'score_raw' => $objective[2],
  3591. 'score_min' => $objective[4],
  3592. 'score_max' => $objective[3],
  3593. ];
  3594. $insertId = Database::insert($iva_table, $params);
  3595. if ($insertId) {
  3596. $sql = "UPDATE $iva_table SET id = iid
  3597. WHERE iid = $insertId";
  3598. Database::query($sql);
  3599. }
  3600. }
  3601. }
  3602. }
  3603. }
  3604. }
  3605. /**
  3606. * Writes the current data to the database.
  3607. *
  3608. * @return bool Query result
  3609. */
  3610. public function write_to_db()
  3611. {
  3612. $debug = self::DEBUG;
  3613. if ($debug) {
  3614. error_log('learnpathItem::write_to_db()', 0);
  3615. }
  3616. // Check the session visibility.
  3617. if (!api_is_allowed_to_session_edit()) {
  3618. if ($debug) {
  3619. error_log('return false api_is_allowed_to_session_edit');
  3620. }
  3621. return false;
  3622. }
  3623. $course_id = api_get_course_int_id();
  3624. $mode = $this->get_lesson_mode();
  3625. $credit = $this->get_credit();
  3626. $total_time = ' ';
  3627. $my_status = ' ';
  3628. $item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3629. $sql = 'SELECT status, total_time FROM '.$item_view_table.'
  3630. WHERE
  3631. c_id = '.$course_id.' AND
  3632. lp_item_id="'.$this->db_id.'" AND
  3633. lp_view_id="'.$this->view_id.'" AND
  3634. view_count="'.$this->get_attempt_id().'" ';
  3635. $rs_verified = Database::query($sql);
  3636. $row_verified = Database::fetch_array($rs_verified);
  3637. $my_case_completed = [
  3638. 'completed',
  3639. 'passed',
  3640. 'browsed',
  3641. 'failed',
  3642. ];
  3643. $oldTotalTime = $row_verified['total_time'];
  3644. $this->oldTotalTime = $oldTotalTime;
  3645. $save = true;
  3646. if (isset($row_verified) && isset($row_verified['status'])) {
  3647. if (in_array($row_verified['status'], $my_case_completed)) {
  3648. $save = false;
  3649. }
  3650. }
  3651. if ((($save === false && $this->type == 'sco') ||
  3652. ($this->type == 'sco' && ($credit == 'no-credit' || $mode == 'review' || $mode == 'browse'))) &&
  3653. ($this->seriousgame_mode != 1 && $this->type == 'sco')
  3654. ) {
  3655. if ($debug) {
  3656. error_log(
  3657. "This info shouldn't be saved as the credit or lesson mode info prevent it"
  3658. );
  3659. error_log(
  3660. 'learnpathItem::write_to_db() - credit('.$credit.') or'.
  3661. ' lesson_mode('.$mode.') prevent recording!',
  3662. 0
  3663. );
  3664. }
  3665. } else {
  3666. // Check the row exists.
  3667. $inserted = false;
  3668. // This a special case for multiple attempts and Chamilo exercises.
  3669. if ($this->type == 'quiz' &&
  3670. $this->get_prevent_reinit() == 0 &&
  3671. $this->get_status() == 'completed'
  3672. ) {
  3673. // We force the item to be restarted.
  3674. $this->restart();
  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" => ,
  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. $inserted = true;
  3701. }
  3702. }
  3703. $item_view_table = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3704. $sql = "SELECT * FROM $item_view_table
  3705. WHERE
  3706. c_id = $course_id AND
  3707. lp_item_id = ".$this->db_id." AND
  3708. lp_view_id = ".$this->view_id." AND
  3709. view_count = ".intval($this->get_attempt_id());
  3710. if ($debug) {
  3711. error_log(
  3712. 'learnpathItem::write_to_db() - Querying item_view: '.$sql,
  3713. 0
  3714. );
  3715. }
  3716. $check_res = Database::query($sql);
  3717. // Depending on what we want (really), we'll update or insert a new row
  3718. // now save into DB.
  3719. if (!$inserted && Database::num_rows($check_res) < 1) {
  3720. $params = [
  3721. "c_id" => $course_id,
  3722. "total_time" => $this->get_total_time(),
  3723. "start_time" => $this->current_start_time,
  3724. "score" => $this->get_score(),
  3725. "status" => $this->get_status(false),
  3726. "max_score" => $this->get_max(),
  3727. "lp_item_id" => $this->db_id,
  3728. "lp_view_id" => $this->view_id,
  3729. "view_count" => $this->get_attempt_id(),
  3730. "suspend_data" => $this->current_data,
  3731. //"max_time_allowed" => ,$this->get_max_time_allowed()
  3732. "lesson_location" => $this->lesson_location,
  3733. ];
  3734. if ($debug) {
  3735. error_log(
  3736. 'learnpathItem::write_to_db() - Inserting into item_view forced: '.print_r($params, 1),
  3737. 0
  3738. );
  3739. }
  3740. $this->db_item_view_id = Database::insert($item_view_table, $params);
  3741. if ($this->db_item_view_id) {
  3742. $sql = "UPDATE $item_view_table SET id = iid
  3743. WHERE iid = ".$this->db_item_view_id;
  3744. Database::query($sql);
  3745. }
  3746. } else {
  3747. if ($this->type == 'hotpotatoes') {
  3748. $params = [
  3749. 'total_time' => $this->get_total_time(),
  3750. 'start_time' => $this->get_current_start_time(),
  3751. 'score' => $this->get_score(),
  3752. 'status' => $this->get_status(false),
  3753. 'max_score' => $this->get_max(),
  3754. 'suspend_data' => $this->current_data,
  3755. 'lesson_location' => $this->lesson_location,
  3756. ];
  3757. $where = [
  3758. 'c_id = ? AND lp_item_id = ? AND lp_view_id = ? AND view_count = ?' => [
  3759. $course_id,
  3760. $this->db_id,
  3761. $this->view_id,
  3762. $this->get_attempt_id(),
  3763. ],
  3764. ];
  3765. Database::update($item_view_table, $params, $where);
  3766. } else {
  3767. // For all other content types...
  3768. if ($this->type == 'quiz') {
  3769. if ($debug) {
  3770. error_log("item is quiz:");
  3771. }
  3772. $my_status = ' ';
  3773. $total_time = ' ';
  3774. if (!empty($_REQUEST['exeId'])) {
  3775. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
  3776. $exeId = (int) $_REQUEST['exeId'];
  3777. $sql = "SELECT exe_duration
  3778. FROM $table
  3779. WHERE exe_id = $exeId";
  3780. if ($debug) {
  3781. error_log($sql);
  3782. }
  3783. $res = Database::query($sql);
  3784. $exeRow = Database::fetch_array($res);
  3785. $duration = $exeRow['exe_duration'];
  3786. $total_time = " total_time = ".$duration.", ";
  3787. if ($debug) {
  3788. error_log("quiz: $total_time");
  3789. }
  3790. }
  3791. } else {
  3792. $my_type_lp = learnpath::get_type_static($this->lp_id);
  3793. // This is a array containing values finished
  3794. $case_completed = [
  3795. 'completed',
  3796. 'passed',
  3797. 'browsed',
  3798. 'failed',
  3799. ];
  3800. // Is not multiple attempts
  3801. if ($this->seriousgame_mode == 1 && $this->type == 'sco') {
  3802. $total_time = " total_time = total_time +".$this->get_total_time().", ";
  3803. $my_status = " status = '".$this->get_status(false)."' ,";
  3804. if ($debug) {
  3805. error_log("seriousgame_mode time changed: $total_time");
  3806. }
  3807. } elseif ($this->get_prevent_reinit() == 1) {
  3808. // Process of status verified into data base.
  3809. $sql = 'SELECT status FROM '.$item_view_table.'
  3810. WHERE
  3811. c_id = '.$course_id.' AND
  3812. lp_item_id="'.$this->db_id.'" AND
  3813. lp_view_id="'.$this->view_id.'" AND
  3814. view_count="'.$this->get_attempt_id().'"
  3815. ';
  3816. $rs_verified = Database::query($sql);
  3817. $row_verified = Database::fetch_array($rs_verified);
  3818. // Get type lp: 1=lp dokeos and 2=scorm.
  3819. // If not is completed or passed or browsed and learning path is scorm.
  3820. if (!in_array($this->get_status(false), $case_completed) &&
  3821. $my_type_lp == 2
  3822. ) {
  3823. $total_time = " total_time = total_time +".$this->get_total_time().", ";
  3824. $my_status = " status = '".$this->get_status(false)."' ,";
  3825. if ($debug) {
  3826. error_log("get_prevent_reinit = 1 time changed: $total_time");
  3827. }
  3828. } else {
  3829. // Verified into database.
  3830. if (!in_array($row_verified['status'], $case_completed) &&
  3831. $my_type_lp == 2
  3832. ) {
  3833. $total_time = " total_time = total_time +".$this->get_total_time().", ";
  3834. $my_status = " status = '".$this->get_status(false)."' ,";
  3835. if ($debug) {
  3836. error_log("total_time time changed case 1: $total_time");
  3837. }
  3838. } elseif (in_array($row_verified['status'], $case_completed) &&
  3839. $my_type_lp == 2 && $this->type != 'sco'
  3840. ) {
  3841. $total_time = " total_time = total_time +".$this->get_total_time().", ";
  3842. $my_status = " status = '".$this->get_status(false)."' ,";
  3843. if ($debug) {
  3844. error_log("total_time time changed case 2: $total_time");
  3845. }
  3846. } else {
  3847. if (($my_type_lp == 3 && $this->type == 'au') ||
  3848. ($my_type_lp == 1 && $this->type != 'dir')) {
  3849. // Is AICC or Chamilo LP
  3850. $total_time = " total_time = total_time + ".$this->get_total_time().", ";
  3851. $my_status = " status = '".$this->get_status(false)."' ,";
  3852. if ($debug) {
  3853. error_log("total_time time changed case 3: $total_time");
  3854. }
  3855. }
  3856. }
  3857. }
  3858. } else {
  3859. // Multiple attempts are allowed.
  3860. if (in_array($this->get_status(false), $case_completed) && $my_type_lp == 2) {
  3861. // Reset zero new attempt ?
  3862. $my_status = " status = '".$this->get_status(false)."' ,";
  3863. if ($debug) {
  3864. error_log("total_time time changed Multiple attempt case 1: $total_time");
  3865. }
  3866. } elseif (!in_array($this->get_status(false), $case_completed) && $my_type_lp == 2) {
  3867. $total_time = " total_time = ".$this->get_total_time().", ";
  3868. $my_status = " status = '".$this->get_status(false)."' ,";
  3869. if ($debug) {
  3870. error_log("total_time time changed Multiple attempt case 2: $total_time");
  3871. }
  3872. } else {
  3873. // It is chamilo LP.
  3874. $total_time = " total_time = total_time +".$this->get_total_time().", ";
  3875. $my_status = " status = '".$this->get_status(false)."' ,";
  3876. if ($debug) {
  3877. error_log("total_time time changed Multiple attempt case 3: $total_time");
  3878. }
  3879. }
  3880. // This code line fixes the problem of wrong status.
  3881. if ($my_type_lp == 2) {
  3882. // Verify current status in multiples attempts.
  3883. $sql = 'SELECT status FROM '.$item_view_table.'
  3884. WHERE
  3885. c_id = '.$course_id.' AND
  3886. lp_item_id="'.$this->db_id.'" AND
  3887. lp_view_id="'.$this->view_id.'" AND
  3888. view_count="'.$this->get_attempt_id().'" ';
  3889. $rs_status = Database::query($sql);
  3890. $current_status = Database::result(
  3891. $rs_status,
  3892. 0,
  3893. 'status'
  3894. );
  3895. if (in_array($current_status, $case_completed)) {
  3896. $my_status = '';
  3897. $total_time = '';
  3898. } else {
  3899. $total_time = " total_time = total_time + ".$this->get_total_time().", ";
  3900. }
  3901. if ($debug) {
  3902. error_log("total_time time my_type_lp: $total_time");
  3903. }
  3904. }
  3905. }
  3906. }
  3907. if ($this->type == 'sco') {
  3908. //IF scorm scorm_update_time has already updated total_time in db
  3909. //" . //start_time = ".$this->get_current_start_time().", " . //scorm_init_time does it
  3910. ////" max_time_allowed = '".$this->get_max_time_allowed()."'," .
  3911. $sql = "UPDATE $item_view_table SET
  3912. score = ".$this->get_score().",
  3913. $my_status
  3914. max_score = '".$this->get_max()."',
  3915. suspend_data = '".Database::escape_string($this->current_data)."',
  3916. lesson_location = '".$this->lesson_location."'
  3917. WHERE
  3918. c_id = $course_id AND
  3919. lp_item_id = ".$this->db_id." AND
  3920. lp_view_id = ".$this->view_id." AND
  3921. view_count = ".$this->get_attempt_id();
  3922. } else {
  3923. //" max_time_allowed = '".$this->get_max_time_allowed()."'," .
  3924. $sql = "UPDATE $item_view_table SET
  3925. $total_time
  3926. start_time = ".$this->get_current_start_time().",
  3927. score = ".$this->get_score().",
  3928. $my_status
  3929. max_score = '".$this->get_max()."',
  3930. suspend_data = '".Database::escape_string($this->current_data)."',
  3931. lesson_location = '".$this->lesson_location."'
  3932. WHERE
  3933. c_id = $course_id AND
  3934. lp_item_id = ".$this->db_id." AND
  3935. lp_view_id = ".$this->view_id." AND
  3936. view_count = ".$this->get_attempt_id();
  3937. }
  3938. $this->current_start_time = time();
  3939. }
  3940. if ($debug) {
  3941. error_log('-------------------------------------------');
  3942. error_log('learnpathItem::write_to_db() - Updating item_view:');
  3943. error_log($sql);
  3944. error_log('-------------------------------------------');
  3945. }
  3946. Database::query($sql);
  3947. }
  3948. if (is_array($this->interactions) &&
  3949. count($this->interactions) > 0
  3950. ) {
  3951. // Save interactions.
  3952. $tbl = Database::get_course_table(TABLE_LP_ITEM_VIEW);
  3953. $sql = "SELECT iid FROM $tbl
  3954. WHERE
  3955. c_id = $course_id AND
  3956. lp_item_id = ".$this->db_id." AND
  3957. lp_view_id = ".$this->view_id." AND
  3958. view_count = ".$this->get_attempt_id();
  3959. $res = Database::query($sql);
  3960. if (Database::num_rows($res) > 0) {
  3961. $row = Database::fetch_array($res);
  3962. $lp_iv_id = $row[0];
  3963. if ($debug) {
  3964. error_log(
  3965. 'learnpathItem::write_to_db() - Got item_view_id '.
  3966. $lp_iv_id.', now checking interactions ',
  3967. 0
  3968. );
  3969. }
  3970. foreach ($this->interactions as $index => $interaction) {
  3971. $correct_resp = '';
  3972. if (is_array($interaction[4]) && !empty($interaction[4][0])) {
  3973. foreach ($interaction[4] as $resp) {
  3974. $correct_resp .= $resp.',';
  3975. }
  3976. $correct_resp = substr(
  3977. $correct_resp,
  3978. 0,
  3979. strlen($correct_resp) - 1
  3980. );
  3981. }
  3982. $iva_table = Database::get_course_table(
  3983. TABLE_LP_IV_INTERACTION
  3984. );
  3985. //also check for the interaction ID as it must be unique for this SCO view
  3986. $iva_sql = "SELECT iid FROM $iva_table
  3987. WHERE
  3988. c_id = $course_id AND
  3989. lp_iv_id = $lp_iv_id AND
  3990. (
  3991. order_id = $index OR
  3992. interaction_id = '".Database::escape_string($interaction[0])."'
  3993. )
  3994. ";
  3995. $iva_res = Database::query($iva_sql);
  3996. $interaction[0] = isset($interaction[0]) ? $interaction[0] : '';
  3997. $interaction[1] = isset($interaction[1]) ? $interaction[1] : '';
  3998. $interaction[2] = isset($interaction[2]) ? $interaction[2] : '';
  3999. $interaction[3] = isset($interaction[3]) ? $interaction[3] : '';
  4000. $interaction[4] = isset($interaction[4]) ? $interaction[4] : '';
  4001. $interaction[5] = isset($interaction[5]) ? $interaction[5] : '';
  4002. $interaction[6] = isset($interaction[6]) ? $interaction[6] : '';
  4003. $interaction[7] = isset($interaction[7]) ? $interaction[7] : '';
  4004. // id(0), type(1), time(2), weighting(3), correct_responses(4), student_response(5), result(6), latency(7)
  4005. if (Database::num_rows($iva_res) > 0) {
  4006. // Update (or don't).
  4007. $iva_row = Database::fetch_array($iva_res);
  4008. $iva_id = $iva_row[0];
  4009. // Insert new one.
  4010. $params = [
  4011. 'interaction_id' => $interaction[0],
  4012. 'interaction_type' => $interaction[1],
  4013. 'weighting' => $interaction[3],
  4014. 'completion_time' => $interaction[2],
  4015. 'correct_responses' => $correct_resp,
  4016. 'student_response' => $interaction[5],
  4017. 'result' => $interaction[6],
  4018. 'latency' => $interaction[7],
  4019. ];
  4020. Database::update(
  4021. $iva_table,
  4022. $params,
  4023. [
  4024. 'c_id = ? AND iid = ?' => [
  4025. $course_id,
  4026. $iva_id,
  4027. ],
  4028. ]
  4029. );
  4030. } else {
  4031. // Insert new one.
  4032. $params = [
  4033. 'c_id' => $course_id,
  4034. 'order_id' => $index,
  4035. 'lp_iv_id' => $lp_iv_id,
  4036. 'interaction_id' => $interaction[0],
  4037. 'interaction_type' => $interaction[1],
  4038. 'weighting' => $interaction[3],
  4039. 'completion_time' => $interaction[2],
  4040. 'correct_responses' => $correct_resp,
  4041. 'student_response' => $interaction[5],
  4042. 'result' => $interaction[6],
  4043. 'latency' => $interaction[7],
  4044. ];
  4045. $insertId = Database::insert($iva_table, $params);
  4046. if ($insertId) {
  4047. $sql = "UPDATE $iva_table SET id = iid
  4048. WHERE iid = $insertId";
  4049. Database::query($sql);
  4050. }
  4051. }
  4052. }
  4053. }
  4054. }
  4055. }
  4056. if ($debug) {
  4057. error_log('End of learnpathItem::write_to_db()', 0);
  4058. }
  4059. return true;
  4060. }
  4061. /**
  4062. * Adds an audio file attached to the current item (store on disk and in db).
  4063. *
  4064. * @return bool|string|null
  4065. */
  4066. public function add_audio()
  4067. {
  4068. $course_info = api_get_course_info();
  4069. $filepath = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/document/';
  4070. if (!is_dir($filepath.'audio')) {
  4071. mkdir(
  4072. $filepath.'audio',
  4073. api_get_permissions_for_new_directories()
  4074. );
  4075. $audio_id = add_document(
  4076. $course_info,
  4077. '/audio',
  4078. 'folder',
  4079. 0,
  4080. 'audio'
  4081. );
  4082. api_item_property_update(
  4083. $course_info,
  4084. TOOL_DOCUMENT,
  4085. $audio_id,
  4086. 'FolderCreated',
  4087. api_get_user_id(),
  4088. null,
  4089. null,
  4090. null,
  4091. null,
  4092. api_get_session_id()
  4093. );
  4094. api_item_property_update(
  4095. $course_info,
  4096. TOOL_DOCUMENT,
  4097. $audio_id,
  4098. 'invisible',
  4099. api_get_user_id(),
  4100. null,
  4101. null,
  4102. null,
  4103. null,
  4104. api_get_session_id()
  4105. );
  4106. }
  4107. $key = 'file';
  4108. if (!isset($_FILES[$key]['name']) || !isset($_FILES[$key]['tmp_name'])) {
  4109. return false;
  4110. }
  4111. $result = DocumentManager::upload_document(
  4112. $_FILES,
  4113. '/audio',
  4114. null,
  4115. null,
  4116. 0,
  4117. 'rename',
  4118. false,
  4119. false
  4120. );
  4121. $file_path = null;
  4122. if ($result) {
  4123. $file_path = basename($result['path']);
  4124. // Store the mp3 file in the lp_item table.
  4125. $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
  4126. $sql = "UPDATE $tbl_lp_item SET
  4127. audio = '".Database::escape_string($file_path)."'
  4128. WHERE iid = ".intval($this->db_id);
  4129. Database::query($sql);
  4130. }
  4131. return $file_path;
  4132. }
  4133. /**
  4134. * Adds an audio file to the current item, using a file already in documents.
  4135. *
  4136. * @param int $doc_id
  4137. *
  4138. * @return string
  4139. */
  4140. public function add_audio_from_documents($doc_id)
  4141. {
  4142. $course_info = api_get_course_info();
  4143. $document_data = DocumentManager::get_document_data_by_id(
  4144. $doc_id,
  4145. $course_info['code']
  4146. );
  4147. $file_path = '';
  4148. if (!empty($document_data)) {
  4149. $file_path = substr($document_data['path'], 6);
  4150. $file_path = ltrim($file_path, "/");
  4151. // Store the mp3 file in the lp_item table.
  4152. $table = Database::get_course_table(TABLE_LP_ITEM);
  4153. $sql = "UPDATE $table SET
  4154. audio = '".Database::escape_string($file_path)."'
  4155. WHERE iid = ".intval($this->db_id);
  4156. Database::query($sql);
  4157. }
  4158. return $file_path;
  4159. }
  4160. /**
  4161. * Removes the relation between the current item and an audio file. The file
  4162. * is only removed from the lp_item table, but remains in the document table
  4163. * and directory.
  4164. *
  4165. * @return bool
  4166. */
  4167. public function remove_audio()
  4168. {
  4169. if (empty($this->db_id)) {
  4170. return false;
  4171. }
  4172. $table = Database::get_course_table(TABLE_LP_ITEM);
  4173. $sql = "UPDATE $table SET
  4174. audio = ''
  4175. WHERE iid IN (".$this->db_id.")";
  4176. Database::query($sql);
  4177. }
  4178. /**
  4179. * Transform the SCORM status to a string that can be translated by Chamilo
  4180. * in different user languages.
  4181. *
  4182. * @param $status
  4183. * @param bool $decorate
  4184. * @param string $type classic|simple
  4185. *
  4186. * @return array|string
  4187. */
  4188. public static function humanize_status($status, $decorate = true, $type = 'classic')
  4189. {
  4190. $statusList = [
  4191. 'completed' => 'ScormCompstatus',
  4192. 'incomplete' => 'ScormIncomplete',
  4193. 'failed' => 'ScormFailed',
  4194. 'passed' => 'ScormPassed',
  4195. 'browsed' => 'ScormBrowsed',
  4196. 'not attempted' => 'ScormNotAttempted',
  4197. ];
  4198. $myLessonStatus = get_lang($statusList[$status]);
  4199. switch ($status) {
  4200. case 'completed':
  4201. case 'browsed':
  4202. $classStatus = 'info';
  4203. break;
  4204. case 'incomplete':
  4205. $classStatus = 'warning';
  4206. break;
  4207. case 'passed':
  4208. $classStatus = 'success';
  4209. break;
  4210. case 'failed':
  4211. $classStatus = 'important';
  4212. break;
  4213. default:
  4214. $classStatus = 'default';
  4215. break;
  4216. }
  4217. if ($type == 'simple') {
  4218. if (in_array($status, ['failed', 'passed', 'browsed'])) {
  4219. $myLessonStatus = get_lang('ScormIncomplete');
  4220. $classStatus = 'warning';
  4221. }
  4222. }
  4223. if ($decorate) {
  4224. return Display::label($myLessonStatus, $classStatus);
  4225. } else {
  4226. return $myLessonStatus;
  4227. }
  4228. }
  4229. /**
  4230. * @return float
  4231. */
  4232. public function getPrerequisiteMaxScore()
  4233. {
  4234. return $this->prerequisiteMaxScore;
  4235. }
  4236. /**
  4237. * @param float $prerequisiteMaxScore
  4238. */
  4239. public function setPrerequisiteMaxScore($prerequisiteMaxScore)
  4240. {
  4241. $this->prerequisiteMaxScore = $prerequisiteMaxScore;
  4242. }
  4243. /**
  4244. * @return float
  4245. */
  4246. public function getPrerequisiteMinScore()
  4247. {
  4248. return $this->prerequisiteMinScore;
  4249. }
  4250. /**
  4251. * @param float $prerequisiteMinScore
  4252. */
  4253. public function setPrerequisiteMinScore($prerequisiteMinScore)
  4254. {
  4255. $this->prerequisiteMinScore = $prerequisiteMinScore;
  4256. }
  4257. /**
  4258. * Check if this LP item has a created thread in the basis course from the forum of its LP.
  4259. *
  4260. * @param int $lpCourseId The course ID
  4261. *
  4262. * @return bool
  4263. */
  4264. public function lpItemHasThread($lpCourseId)
  4265. {
  4266. $forumThreadTable = Database::get_course_table(TABLE_FORUM_THREAD);
  4267. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  4268. $fakeFrom = "
  4269. $forumThreadTable ft
  4270. INNER JOIN $itemProperty ip
  4271. ON (ft.thread_id = ip.ref AND ft.c_id = ip.c_id)
  4272. ";
  4273. $resultData = Database::select(
  4274. 'COUNT(ft.iid) AS qty',
  4275. $fakeFrom,
  4276. [
  4277. 'where' => [
  4278. 'ip.visibility != ? AND ' => 2,
  4279. 'ip.tool = ? AND ' => TOOL_FORUM_THREAD,
  4280. 'ft.c_id = ? AND ' => intval($lpCourseId),
  4281. '(ft.lp_item_id = ? OR (ft.thread_title = ? AND ft.lp_item_id = ?))' => [
  4282. intval($this->db_id),
  4283. "{$this->title} - {$this->db_id}",
  4284. intval($this->db_id),
  4285. ],
  4286. ],
  4287. ],
  4288. 'first'
  4289. );
  4290. if ($resultData['qty'] > 0) {
  4291. return true;
  4292. }
  4293. return false;
  4294. }
  4295. /**
  4296. * Get the forum thread info.
  4297. *
  4298. * @param int $lpCourseId The course ID from the learning path
  4299. * @param int $lpSessionId Optional. The session ID from the learning path
  4300. *
  4301. * @return bool
  4302. */
  4303. public function getForumThread($lpCourseId, $lpSessionId = 0)
  4304. {
  4305. $lpSessionId = (int) $lpSessionId;
  4306. $forumThreadTable = Database::get_course_table(TABLE_FORUM_THREAD);
  4307. $itemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  4308. $fakeFrom = "$forumThreadTable ft
  4309. INNER JOIN $itemProperty ip ";
  4310. if ($lpSessionId == 0) {
  4311. $fakeFrom .= "
  4312. ON (
  4313. ft.thread_id = ip.ref AND ft.c_id = ip.c_id AND (
  4314. ft.session_id = ip.session_id OR ip.session_id IS NULL
  4315. )
  4316. )
  4317. ";
  4318. } else {
  4319. $fakeFrom .= "
  4320. ON (
  4321. ft.thread_id = ip.ref AND ft.c_id = ip.c_id AND ft.session_id = ip.session_id
  4322. )
  4323. ";
  4324. }
  4325. $resultData = Database::select(
  4326. 'ft.*',
  4327. $fakeFrom,
  4328. [
  4329. 'where' => [
  4330. 'ip.visibility != ? AND ' => 2,
  4331. 'ip.tool = ? AND ' => TOOL_FORUM_THREAD,
  4332. 'ft.session_id = ? AND ' => $lpSessionId,
  4333. 'ft.c_id = ? AND ' => intval($lpCourseId),
  4334. '(ft.lp_item_id = ? OR (ft.thread_title = ? AND ft.lp_item_id = ?))' => [
  4335. intval($this->db_id),
  4336. "{$this->title} - {$this->db_id}",
  4337. intval($this->db_id),
  4338. ],
  4339. ],
  4340. ],
  4341. 'first'
  4342. );
  4343. if (empty($resultData)) {
  4344. return false;
  4345. }
  4346. return $resultData;
  4347. }
  4348. /**
  4349. * Create a forum thread for this learning path item.
  4350. *
  4351. * @param int $currentForumId The forum ID to add the new thread
  4352. *
  4353. * @return int The forum thread if was created. Otherwise return false
  4354. */
  4355. public function createForumThread($currentForumId)
  4356. {
  4357. require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php';
  4358. $em = Database::getManager();
  4359. $threadRepo = $em->getRepository('ChamiloCourseBundle:CForumThread');
  4360. $forumThread = $threadRepo->findOneBy([
  4361. 'threadTitle' => "{$this->title} - {$this->db_id}",
  4362. 'forumId' => (int) $currentForumId,
  4363. ]);
  4364. if (!$forumThread) {
  4365. $forumInfo = get_forum_information($currentForumId);
  4366. store_thread(
  4367. $forumInfo,
  4368. [
  4369. 'forum_id' => intval($currentForumId),
  4370. 'thread_id' => 0,
  4371. 'gradebook' => 0,
  4372. 'post_title' => "{$this->name} - {$this->db_id}",
  4373. 'post_text' => $this->description,
  4374. 'category_id' => 1,
  4375. 'numeric_calification' => 0,
  4376. 'calification_notebook_title' => 0,
  4377. 'weight_calification' => 0.00,
  4378. 'thread_peer_qualify' => 0,
  4379. 'lp_item_id' => $this->db_id,
  4380. ],
  4381. [],
  4382. false
  4383. );
  4384. return;
  4385. }
  4386. $forumThread->setLpItemId($this->db_id);
  4387. $em->persist($forumThread);
  4388. $em->flush();
  4389. }
  4390. /**
  4391. * Allow dissociate a forum to this LP item.
  4392. *
  4393. * @param int $threadIid The thread id
  4394. *
  4395. * @return bool
  4396. */
  4397. public function dissociateForumThread($threadIid)
  4398. {
  4399. $threadIid = (int) $threadIid;
  4400. $em = Database::getManager();
  4401. $forumThread = $em->find('ChamiloCourseBundle:CForumThread', $threadIid);
  4402. if (!$forumThread) {
  4403. return false;
  4404. }
  4405. $forumThread->setThreadTitle("{$this->get_title()} - {$this->db_id}");
  4406. $forumThread->setLpItemId(0);
  4407. $em->persist($forumThread);
  4408. $em->flush();
  4409. return true;
  4410. }
  4411. /**
  4412. * @return int
  4413. */
  4414. public function getLastScormSessionTime()
  4415. {
  4416. return $this->last_scorm_session_time;
  4417. }
  4418. /**
  4419. * @return int
  4420. */
  4421. public function getIid()
  4422. {
  4423. return $this->iId;
  4424. }
  4425. }