learnpathItem.class.php 193 KB

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