learnpathItem.class.php 183 KB

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