learnpathItem.class.php 183 KB

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