learnpathItem.class.php 192 KB

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