learnpathItem.class.php 192 KB

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