learnpathItem.class.php 187 KB

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