learnpathItem.class.php 188 KB

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