learnpathItem.class.php 184 KB

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