learnpathItem.class.php 186 KB

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