learnpathItem.class.php 190 KB

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