aicc_api.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * API event handler functions for AICC / CMIv4 in API communication mode
  6. *
  7. * @author Denes Nagy <darkden@freemail.hu>
  8. * @author Yannick Warnier <ywarnier@beeznest.org>
  9. * @version v 1.0
  10. * @access public
  11. * @package chamilo.learnpath
  12. * @license GNU/GPL
  13. */
  14. /**
  15. * This script is divided into three sections.
  16. * The first section (below) is the initialisation part.
  17. * The second section is the AICC object part
  18. * The third section defines the event handlers for Chamilo's internal messaging
  19. * and frames refresh
  20. *
  21. * This script implements the API messaging for AICC. The HACP messaging is
  22. * made by another set of scripts.
  23. */
  24. // Flag to allow for anonymous user - needs to be set before global.inc.php.
  25. $use_anonymous = true;
  26. // Load common libraries using a compatibility script to bridge between 1.6 and 1.8.
  27. require_once __DIR__.'/../inc/global.inc.php';
  28. // Is this needed? This is probabaly done in the header file.
  29. $file = $_SESSION['file'];
  30. $oLP = unserialize(Session::read('lpobject'));
  31. $oItem = $oLP->items[$oLP->current];
  32. if (!is_object($oItem)) {
  33. error_log('New LP - scorm_api - Could not load oItem item', 0);
  34. exit;
  35. }
  36. $autocomplete_when_80pct = 0;
  37. /* JavaScript Functions */
  38. ?>var scorm_logs=<?php echo (empty($oLP->scorm_debug) ? '0' : '3'); ?>; //debug log level for SCORM. 0 = none, 1=light, 2=a lot, 3=all - displays logs in log frame
  39. var lms_logs=0; //debug log level for LMS actions. 0=none, 1=light, 2=a lot, 3=all
  40. //logit_lms('scormfunctions.php included',0);
  41. function APIobject() {
  42. this.LMSInitialize=LMSInitialize;
  43. this.LMSGetValue=LMSGetValue;
  44. this.LMSSetValue=LMSSetValue;
  45. this.LMSCommit=LMSCommit;
  46. this.LMSFinish=LMSFinish;
  47. this.LMSGetLastError=LMSGetLastError;
  48. this.LMSGetErrorString=LMSGetErrorString;
  49. this.LMSGetDiagnostic=LMSGetDiagnostic;
  50. }
  51. // It is not sure that the scos use the above declarations.
  52. API = new APIobject(); //for scorm 1.2
  53. var G_NoError = 0;
  54. var G_GeneralException = 101;
  55. var G_ServerBusy = 102;
  56. var G_InvalidArgumentError = 201;
  57. var G_ElementCannotHaveChildren = 202;
  58. var G_ElementIsNotAnArray = 203;
  59. var G_NotInitialized = 301;
  60. var G_NotImplementedError = 401;
  61. var G_InvalidSetValue = 402;
  62. var G_ElementIsReadOnly = 403;
  63. var G_ElementIsWriteOnly = 404;
  64. var G_IncorrectDataType = 405;
  65. var G_LastError = G_NoError ;
  66. var commit = false ;
  67. // Strictly SCORM variables.
  68. var score=<?php echo $oItem->get_score(); ?>;
  69. var max=<?php echo $oItem->get_max(); ?>;
  70. var min=<?php echo $oItem->get_min(); ?>;
  71. var lesson_status='<?php echo $oItem->get_status(); ?>';
  72. var session_time='<?php echo $oItem->get_scorm_time('js'); ?>';
  73. var suspend_data = '<?php echo $oItem->get_suspend_data(); ?>';
  74. var lesson_location = '<?php echo $oItem->get_lesson_location(); ?>';
  75. var total_time = '<?php echo $oItem->get_scorm_time('js'); ?>';
  76. // Chamilo internal variables.
  77. var saved_lesson_status = 'not attempted';
  78. var lms_lp_id = <?php echo $oLP->get_id(); ?>;
  79. var lms_item_id = <?php echo $oItem->get_id(); ?>;
  80. //var lms_new_item_id = 0; //temporary value (only there between a load_item() and a LMSInitialize())
  81. var lms_been_synchronized = 0;
  82. var lms_initialized = 0;
  83. var lms_total_lessons = <?php echo $oLP->get_total_items_count(); ?>;
  84. var lms_complete_lessons = <?php echo $oLP->get_complete_items_count(); ?>;
  85. var lms_progress_bar_mode = '<?php echo $oLP->progress_bar_mode; ?>';
  86. if(lms_progress_bar_mode == ''){lms_progress_bar_mode='%';}
  87. var lms_view_id = '<?php echo $oLP->get_view(); ?>';
  88. if(lms_view_id == ''){ lms_view_id = 1;}
  89. var lms_user_id = '<?php echo $_user['user_id']; ?>';
  90. var lms_next_item = '<?php echo $oLP->get_next_item_id(); ?>';
  91. var lms_previous_item = '<?php echo $oLP->get_previous_item_id(); ?>';
  92. var lms_lp_type = '<?php echo $oLP->get_type(); ?>';
  93. var lms_item_type = '<?php echo $oItem->get_type(); ?>';
  94. // Backup for old values.
  95. var old_score = 0;
  96. var old_max = 0;
  97. var old_min = 0;
  98. var old_lesson_status = '';
  99. var old_session_time = '';
  100. var old_suspend_data = '';
  101. var lms_old_item_id = 0;
  102. function LMSInitialize() { //this is the initialize function of all APIobjects
  103. logit_scorm('LMSInitialise()',0);
  104. lms_initialized=1;
  105. return('true');
  106. }
  107. function LMSGetValue(param) {
  108. //logit_scorm("LMSGetValue('"+param+"')",1);
  109. var result='';
  110. if(param=='cmi.core._children' || param=='cmi.core_children'){
  111. result='entry, exit, lesson_status, student_id, student_name, lesson_location, total_time, credit, lesson_mode, score, session_time';
  112. }else if(param == 'cmi.core.entry'){
  113. result='';
  114. }else if(param == 'cmi.core.exit'){
  115. result='';
  116. }else if(param == 'cmi.core.lesson_status'){
  117. if(lesson_status != '') {
  118. result=lesson_status;
  119. }
  120. else{
  121. result='not attempted';
  122. }
  123. }else if(param == 'cmi.core.student_id'){
  124. result='<?php echo $_user['user_id']; ?>';
  125. }else if(param == 'cmi.core.student_name'){
  126. <?php
  127. $who = addslashes(api_get_person_name($_user['firstName'], $_user['lastName']));
  128. echo "result='$who';";
  129. ?>
  130. }else if(param == 'cmi.core.lesson_location'){
  131. result=lesson_location;
  132. }else if(param == 'cmi.core.total_time'){
  133. result=total_time;
  134. }else if(param == 'cmi.core.score._children'){
  135. result='raw,min,max';
  136. }else if(param == 'cmi.core.score.raw'){
  137. result=score;
  138. }else if(param == 'cmi.core.score.max'){
  139. result=max;
  140. }else if(param == 'cmi.core.score.min'){
  141. result=min;
  142. }else if(param == 'cmi.core.score'){
  143. result=score;
  144. }else if(param == 'cmi.core.credit'){
  145. result='no-credit';
  146. }else if(param == 'cmi.core.lesson_mode'){
  147. result='normal';
  148. }else if(param == 'cmi.suspend_data'){
  149. result='<?php echo $oItem->get_suspend_data(); ?>';
  150. }else if(param == 'cmi.launch_data'){
  151. result='';
  152. }else if(param == 'cmi.objectives._count'){
  153. result='<?php echo $oItem->get_view_count(); ?>';
  154. }
  155. /*
  156. // Switch not working??? WTF???
  157. switch(param) {
  158. case 'cmi.core._children' :
  159. result='entry, exit, lesson_status, student_id, student_name, lesson_location, total_time, credit, lesson_mode, score, session_time';
  160. break;
  161. case 'cmi.core_children' :
  162. result='entry, exit, lesson_status, student_id, student_name, lesson_location, total_time, credit, lesson_mode, score, session_time';
  163. break;
  164. case 'cmi.core.entry' :
  165. result='';
  166. break;
  167. case 'cmi.core.exit' :
  168. result='';
  169. break;
  170. case 'cmi.core.lesson_status' :
  171. if(lesson_status != '') {
  172. result=lesson_status;
  173. }
  174. else{
  175. result='not attempted';
  176. }
  177. break;
  178. case 'cmi.core.student_id' :
  179. result='<?php echo $_user['user_id']; ?>';
  180. break;
  181. case 'cmi.core.student_name' :
  182. <?php
  183. $who = addslashes(api_get_person_name($_user['firstName'], $_user['lastName']));
  184. echo "result='$who';";
  185. ?> break;
  186. case 'cmi.core.lesson_location' :
  187. result='';
  188. break;
  189. case 'cmi.core.total_time' :
  190. result=total_time;
  191. break;
  192. case 'cmi.core.score._children' :
  193. result='raw,min,max';
  194. break;
  195. case 'cmi.core.score.raw' :
  196. result=score;
  197. break;
  198. case 'cmi.core.score.max' :
  199. result=max;
  200. break;
  201. case 'cmi.core.score.min' :
  202. result=min;
  203. break;
  204. case 'cmi.core.score' :
  205. result=score;
  206. break;
  207. case 'cmi.core.credit' :
  208. result='no-credit';
  209. break;
  210. case 'cmi.core.lesson_mode' :
  211. result='normal';
  212. break;
  213. case 'cmi.suspend_data' :
  214. result='<?php echo $oItem->get_suspend_data(); ?>';
  215. break;
  216. case 'cmi.launch_data' :
  217. result='';
  218. break;
  219. case 'cmi.objectives._count':
  220. result='<?php echo $oItem->get_view_count(); ?>';
  221. break;
  222. default :
  223. result='';
  224. break;
  225. }
  226. */
  227. logit_scorm("LMSGetValue('"+param+"') returned '"+result+"'",1);
  228. return result;
  229. }
  230. function LMSSetValue(param, val) {
  231. logit_scorm("LMSSetValue('"+param+"','"+val+"')",0);
  232. switch(param) {
  233. case 'cmi.core.score.raw' : score= val ; break;
  234. case 'cmi.core.score.max' : max = val; break;
  235. case 'cmi.core.score.min' : min = val; break;
  236. case 'cmi.core.lesson_location' : lesson_location = val;break;
  237. case 'cmi.core.lesson_status' :
  238. saved_lesson_status = lesson_status;
  239. lesson_status = val;
  240. <?php if ($oLP->mode != 'fullscreen') { ?>
  241. //var update = update_toc(lesson_status,lms_item_id);
  242. <?php } ?>
  243. break;
  244. case 'cmi.completion_status' : lesson_status = val; break; //1.3
  245. case 'cmi.core.session_time' : session_time = val; break;
  246. case 'cmi.score.scaled' : score = val ; break; //1.3
  247. case 'cmi.success_status' : success_status = val; break; //1.3
  248. case 'cmi.suspend_data' : suspend_data = val; break;
  249. }
  250. //var update = update_toc();
  251. //var update_progress = update_progress_bar();
  252. <?php
  253. if ($oLP->force_commit == 1) {
  254. echo " var mycommit = LMSCommit('force');";
  255. }
  256. ?>
  257. return(true);
  258. }
  259. function savedata(origin) { //origin can be 'commit', 'finish' or 'terminate'
  260. <?php if ($autocomplete_when_80pct) { ?>
  261. if( ( lesson_status == 'incomplete') && (score >= (0.8*max) ) ){
  262. lesson_status = 'completed';
  263. }
  264. <?php }?>
  265. param = 'id='+lms_item_id+'&origin='+origin+'&score='+score+'&max='+max+'&min='+min+'&lesson_status='+lesson_status+'&time='+session_time+'&suspend_data='+suspend_data;
  266. url="http://<?php
  267. $self = api_get_self();
  268. $url = $_SERVER['HTTP_HOST'].$self;
  269. $url = substr($url, 0, -14); // 14 is the length of this file's name (/scorm_api.php).
  270. echo $url;
  271. ?>/lp_controller.php?<?php echo api_get_cidreq(); ?>&action=save&lp_id=<?php echo $oLP->get_id(); ?>&" + param + "";
  272. logit_lms('saving data (status='+lesson_status+')',1);
  273. xajax_save_item(lms_lp_id, lms_user_id, lms_view_id, lms_item_id, score, max, min, lesson_status, session_time, suspend_data, lesson_location);
  274. //xajax_update_pgs();
  275. //xajax_update_toc();
  276. }
  277. function LMSCommit(val) {
  278. logit_scorm('LMSCommit()',0);
  279. commit = true ;
  280. savedata('commit');
  281. return('true');
  282. }
  283. function LMSFinish(val) {
  284. if ( !commit ) {
  285. logit_scorm('LMSFinish() (no LMSCommit())',1);
  286. }
  287. if ( commit ) {
  288. logit_scorm('LMSFinish() called',1);
  289. savedata('finish');
  290. }
  291. return('true');
  292. }
  293. function LMSGetLastError() {
  294. logit_scorm('LMSGetLastError()',1);
  295. return(G_LastError);
  296. }
  297. function LMSGetErrorString(errCode){
  298. logit_scorm('LMSGetErrorString()',1);
  299. return('No error !');
  300. }
  301. function LMSGetDiagnostic(errCode){
  302. logit_scorm('LMSGetDiagnostic()',1);
  303. return(API.LMSGetLastError());
  304. }
  305. <?php
  306. /**
  307. * Chamilo-specific code that deals with event handling and inter-frames
  308. * messaging/refreshing.
  309. * Note that from now on, the Chamilo JS code in this library will act as
  310. * a controller, of the MVC pattern, and receive all requests for frame
  311. * updates, then redispatch to any frame concerned.
  312. */
  313. ?>
  314. /**
  315. * Defining the AJAX-object class to be made available from other frames.
  316. */
  317. function XAJAXobject() {
  318. this.xajax_switch_item_details=xajax_switch_item_details;
  319. this.switch_item=switch_item;
  320. }
  321. //it is not sure that the scos use the above declarations
  322. oXAJAX = new XAJAXobject();
  323. oxajax = new XAJAXobject();
  324. /**
  325. * Cross-browser event handling by Scott Andrew
  326. * @param element Element that needs an event attached
  327. * @param string Event type (load, unload, click, keyDown, ...)
  328. * @param string Function name (the event handler)
  329. * @param string used in addEventListener
  330. */
  331. function addEvent(elm, evType, fn, useCapture){
  332. if(elm.addEventListener){
  333. elm.addEventListener(evType, fn, useCapture);
  334. return true;
  335. }else if (elm.attachEvent){
  336. var r = elm.attachEvent('on' + evType, fn);
  337. }else{
  338. elm['on'+evType] = fn;
  339. }
  340. }
  341. /**
  342. * Add listeners to the page objects. This has to be defined for
  343. * the current context as it acts on objects that should exist
  344. * on the page
  345. */
  346. function addListeners(){
  347. //exit if the browser doesn't support ID or tag retrieval
  348. logit_lms('Entering addListeners()',2);
  349. if(!document.getElementsByTagName){
  350. logit_lms("getElementsByTagName not available",2);
  351. return;
  352. }
  353. if(!document.getElementById){
  354. logit_lms("getElementById not available",2);
  355. return;
  356. }
  357. //assign event handlers to objects
  358. if(lms_lp_type==1 || lms_item_type=='asset'){
  359. logit_lms('Dokeos LP or asset',2);
  360. // If this path is a Chamilo learnpath, then start manual save
  361. // when something is loaded in there.
  362. var myelem = document.getElementById('content_id');
  363. if(!myelem){logit_lms("Impossible to find content_id element in document",2);}
  364. addEvent(myelem,'unload',chamilo_save_asset,false);
  365. logit_lms('Added event listener on content_id for unload',2);
  366. }
  367. logit_lms('Quitting addListeners()',2);
  368. }
  369. /**
  370. * Load an item into the content frame:
  371. * - making sure the previous item status have been saved
  372. * - first updating the current item ID (to save the right item)
  373. * - updating the frame src
  374. */
  375. function load_item(item_id,url){
  376. if(document.getElementById('content_id'))
  377. {
  378. logit_lms('Loading item '+item_id,2);
  379. var cont_f = document.getElementById('content_id');
  380. if(cont_f.src){
  381. lms_old_item_id = lms_item_id;
  382. var lms_new_item_id = item_id;
  383. //load new content page into content frame
  384. if(lms_lp_type==1 || lms_item_type=='asset'){
  385. chamilo_save_asset();
  386. }
  387. cont_f.src = url;
  388. update_toc('unhighlight',lms_old_item_id);
  389. update_toc('highlight',item_id);
  390. /* legacy code
  391. lms_been_synchronized = 0;
  392. lms_initialized = 0;
  393. if(lms_lp_type==1 || lms_item_type=='asset'){
  394. lms_item_id = lms_new_item_id;
  395. }*/
  396. return true;
  397. }
  398. logit_lms('cont_f.src has no properties',0);
  399. }
  400. logit_lms('content_id has no properties',0);
  401. return false;
  402. }
  403. /**
  404. * Save a Chamilo learnpath item's time and mark as completed upon
  405. * leaving it
  406. */
  407. function chamilo_save_asset(){
  408. //var linkparams = 'id='+lms_item_id+'&score='+score+'&max='+max+'&min='+min+'&lesson_status='+lesson_status+'&time='+session_time+'&suspend_data='+suspend_data;
  409. //var url = "<?php echo api_get_path(WEB_CODE_PATH).'lp/lp_controller.php'; ?>?action=save&" + linkparams + "";
  410. logit_lms('chamilo_save_asset: '+url,0);
  411. //frames["message_name"].src = url;
  412. xajax_save_item(lms_lp_id, lms_user_id, lms_view_id, lms_item_id, score, max, min, lesson_status, session_time, suspend_data, lesson_location);
  413. }
  414. /**
  415. * Logs information about SCORM messages into the log frame
  416. * @param string Message to log
  417. * @param integer Priority (0 for top priority, 3 for lowest)
  418. */
  419. function logit_scorm(message,priority) {
  420. if (scorm_logs) {
  421. log_in_log("SCORM: " + message);
  422. }
  423. return false;
  424. /*if(frames["lp_log_name"] && scorm_logs>priority){
  425. frames["lp_log_name"].document.getElementById("log_content").innerHTML += "AICC: " + message + "<br/>";
  426. }*/
  427. }
  428. function log_in_log(message) {
  429. var ua = $.browser;
  430. if (ua.mozilla) {
  431. console.log(message);
  432. } else {
  433. if (window.console) {
  434. window.console.log(message);
  435. }
  436. }
  437. }
  438. /**
  439. * Logs information about LMS activity into the log frame
  440. * @param string Message to log
  441. * @param integer Priority (0 for top priority, 3 for lowest)
  442. */
  443. function logit_lms(message,priority) {
  444. /*
  445. if(frames["lp_log_name"] && lms_logs>priority){
  446. frames["lp_log_name"].document.getElementById("log_content").innerHTML += "LMS: " + message + "<br/>";
  447. }*/
  448. if (scorm_logs) {
  449. log_in_log("LMS: " + message);
  450. }
  451. }
  452. /**
  453. * update the Table Of Contents frame, by changing CSS styles, mostly
  454. * @param string Action to be taken
  455. * @param integer Item id to update
  456. */
  457. function update_toc(update_action,update_id)
  458. {
  459. <?php if ($oLP->mode != 'fullscreen') { ?>
  460. var myframe = frames["toc_name"];
  461. var myelem = myframe.document.getElementById("toc_"+update_id);
  462. var myelemimg = myframe.document.getElementById("toc_img_"+update_id);
  463. logit_lms('update_toc('+update_action+','+update_id+')',2);
  464. if(update_id != 0){
  465. switch(update_action){
  466. case 'unhighlight':
  467. myelem.className = "scorm_item";
  468. break;
  469. case 'highlight':
  470. myelem.className = "scorm_item_highlight";
  471. break;
  472. case 'not attempted':
  473. if(myelemimg.src != '../img/notattempted.gif'){
  474. myelemimg.src = "../img/notattempted.gif";
  475. myelemimg.alt = "not attempted";
  476. }
  477. break;
  478. case 'incomplete':
  479. if(myelemimg.src != '../img/incomplete.png'){
  480. myelemimg.src = "../img/incomplete.png";
  481. myelemimg.alt = "incomplete";
  482. }
  483. break;
  484. case 'completed':
  485. if(myelemimg.src != '../img/completed.png'){
  486. myelemimg.src = "../img/completed.png";
  487. myelemimg.alt = "completed";
  488. }
  489. break;
  490. case 'failed':
  491. if(myelemimg.src != '../img/failed.png'){
  492. myelemimg.src = "../img/failed.png";
  493. myelemimg.alt = "failed";
  494. }
  495. break;
  496. case 'passed':
  497. if(myelemimg.src != '../img/completed.png' && myelemimg.alt != 'passed'){
  498. myelemimg.src = "../img/completed.png";
  499. myelemimg.alt = "passed";
  500. }
  501. break;
  502. case 'browsed':
  503. if(myelemimg.src != '../img/completed.png' && myelemimg.alt != 'browsed'){
  504. myelemimg.src = "../img/completed.png";
  505. myelemimg.alt = "browsed";
  506. }
  507. break;
  508. default:
  509. logit_lms('Update action unknown',2);
  510. break;
  511. }
  512. }
  513. return true;
  514. <?php } ?>
  515. return true;
  516. }
  517. /**
  518. * Updates the progress bar with the new status. Prevents the need of a page refresh and flickering
  519. * @param integer Number of completed items
  520. * @param integer Number of items in total
  521. * @param string Display mode (absolute 'abs' or percentage '%').Defaults to %
  522. */
  523. function update_progress_bar(nbr_complete, nbr_total, mode)
  524. {
  525. logit_lms('update_progress_bar('+nbr_complete+','+nbr_total+','+mode+')',2);
  526. logit_lms('could update with data: '+lms_lp_id+','+lms_view_id+','+lms_user_id,2);
  527. var myframe = frames["nav_name"];
  528. if(myframe){
  529. if(mode == ''){mode='%';}
  530. if(nbr_total == 0){nbr_total=1;}
  531. var percentage = (nbr_complete/nbr_total)*100;
  532. percentage = Math.round(percentage);
  533. var progress_bar = $("#progress_bar_value");
  534. progress_bar.css('width', percentage +"%");
  535. /*
  536. var pr_text = myframe.document.getElementById('progress_text');
  537. var pr_full = myframe.document.getElementById('progress_img_full');
  538. var pr_empty = myframe.document.getElementById('progress_img_empty');
  539. pr_full.width = percentage;
  540. pr_empty.width = 100-percentage;
  541. */
  542. var mytext = '';
  543. switch(mode){
  544. case 'abs':
  545. mytext = nbr_complete + '/' + nbr_total;
  546. break;
  547. case '%':
  548. default:
  549. mytext = percentage + '%';
  550. break;
  551. }
  552. pr_text.innerHTML = mytext;
  553. }
  554. return true;
  555. }
  556. function update_stats_page()
  557. {
  558. var myframe = document.getElementById('content_id');
  559. var mysrc = myframe.location.href;
  560. if(mysrc == 'lp_controller.php?action=stats'){
  561. if(myframe && myframe.src){
  562. var mysrc = myframe.src;
  563. myframe.src = mysrc;
  564. }
  565. // = mysrc; //refresh page
  566. }
  567. return true;
  568. }
  569. /**
  570. * Updates the message frame with the given string
  571. */
  572. function update_message_frame(msg_msg)
  573. {
  574. if(msg_msg==null){msg_msg='';}
  575. var msg_f = frames["message_name"];
  576. if(!msg_f.document || !msg_f.document.getElementById('msg_div_id')){
  577. logit_lms('In update_message_frame() - message frame has no document property',0);
  578. }else{
  579. logit_lms('In update_message_frame() - updating frame',0);
  580. msg_f.document.getElementById('msg_div_id').innerHTML= msg_msg;
  581. }
  582. }
  583. /**
  584. * Function that handles the saving of an item and switching from an item to another.
  585. * Once called, this function should be able to do the whole process of (1) saving the
  586. * current item, (2) refresh all the values inside the SCORM API object, (3) open the
  587. * new item into the content_id frame, (4) refresh the table of contents, (5) refresh
  588. * the progress bar (completion), (6) refresh the message frame
  589. * @param integer Chamilo ID for the current item
  590. * @param string This parameter can be a string specifying the next
  591. * item (like 'next', 'previous', 'first' or 'last') or the id to the next item
  592. */
  593. function switch_item(current_item, next_item){
  594. /*
  595. if(!current_item){
  596. logit_lms('In switch - no current_item defined',0);
  597. }
  598. if(!next_item){
  599. logit_lms('In switch - no next_item defined',0);
  600. }
  601. */
  602. if(lms_item_id == next_item){
  603. return; //nothing to switch
  604. }
  605. //(1) save the current item
  606. logit_lms('Called switch_item with params '+lms_item_id+' and '+next_item+'',0);
  607. xajax_save_item(lms_lp_id, lms_user_id, lms_view_id, lms_item_id, score, max, min, lesson_status, session_time, suspend_data, lesson_location);
  608. //(2) Refresh all the values inside this SCORM API object - use AJAX
  609. xajax_switch_item_details(lms_lp_id,lms_user_id,lms_view_id,lms_item_id,next_item);
  610. //(3) open the new item in the content_id frame
  611. var cont_f = document.getElementById('content_id');
  612. if(!cont_f){logit_lms('In switch - content frame not found',0);return false;}
  613. switch(next_item){
  614. case 'next':
  615. next_item = lms_next_item;
  616. break;
  617. case 'previous':
  618. next_item = lms_previous_item;
  619. break;
  620. default:
  621. break;
  622. }
  623. cont_f.src = 'lp_controller.php?action=content&lp_id='+lms_lp_id+'&item_id='+next_item;
  624. //(4) refresh table of contents
  625. /*
  626. var toc_f = document.getElementById('toc_id');
  627. if(!toc_f){logit_lms('In switch - toc frame not found',0);return false;}
  628. var myrefresh = toc_f.src;
  629. toc_f.src = myrefresh;
  630. */
  631. //(5) refresh the progress bar
  632. /*
  633. var prg_f = document.getElementById('nav_id');
  634. if(!prg_f){logit_lms('In switch - navigation frame not found',0);return false;}
  635. var myrefresh = prg_f.src;
  636. prg_f.src = myrefresh;
  637. */
  638. //(6) refresh the message box (included in switch_item_details)
  639. return true;
  640. }
  641. /**
  642. * Save a specific item (with its interactions, if any) into the LMS through
  643. * an AJAX call. Originally, we used the xajax library. Now we use jQuery.
  644. * Because of the need to pass an array, we have to build the parameters
  645. * manually into GET[]
  646. */
  647. function xajax_save_item(lms_lp_id, lms_user_id, lms_view_id, lms_item_id, score, max, min, lesson_status, session_time, suspend_data, lesson_location, interactions, lms_item_core_exit) {
  648. params='';
  649. params += 'lid='+lms_lp_id+'&uid='+lms_user_id+'&vid='+lms_view_id;
  650. params += '&iid='+lms_item_id+'&s='+score+'&max='+max+'&min='+min;
  651. params += '&status='+lesson_status+'&t='+session_time;
  652. params += '&suspend='+suspend_data+'&loc='+lesson_location;
  653. params += '&core_exit='+lms_item_core_exit;
  654. interact_string = '';
  655. for (i in interactions){
  656. interact_string += '&interact['+i+']=';
  657. interact_temp = '[';
  658. for (j in interactions[i]) {
  659. interact_temp += interactions[i][j]+',';
  660. }
  661. interact_temp = interact_temp.substr(0,(interact_temp.length-2)) + ']';
  662. interact_string += encodeURIComponent(interact_temp);
  663. }
  664. //interact_string = encodeURIComponent(interact_string.substr(0,(interact_string.length-1)));
  665. params += interact_string;
  666. /*params = {
  667. 'lid': lms_lp_id,
  668. 'uid': lms_user_id,
  669. 'vid': lms_view_id,
  670. 'iid': lms_item_id,
  671. 's': score,
  672. 'max': max,
  673. 'min': min,
  674. 'status': lesson_status,
  675. 't': session_time,
  676. 'suspend': suspend_data,
  677. 'loc': lesson_location,
  678. 'interact': interac_string,
  679. 'core_exit': lms_item_core_exit
  680. }
  681. */
  682. $.ajax({
  683. type:"POST",
  684. data: params,
  685. url: "lp_ajax_save_item.php",
  686. dataType: "script",
  687. async: false
  688. }
  689. );
  690. }
  691. /**
  692. * Starts the timer with the server clock time.
  693. * Originally, we used the xajax library. Now we use jQuery
  694. */
  695. function xajax_start_timer() {
  696. $.ajax({
  697. type: "GET",
  698. url: "lp_ajax_start_timer.php",
  699. dataType: "script",
  700. async: false
  701. });
  702. }
  703. /**
  704. * Save a specific item's objectives into the LMS through
  705. * an AJAX call. Originally, we used the xajax library. Now we use jQuery
  706. */
  707. function xajax_save_objectives(lms_lp_id,lms_user_id,lms_view_id,lms_item_id,item_objectives) {
  708. params='';
  709. params += 'lid='+lms_lp_id+'&uid='+lms_user_id+'&vid='+lms_view_id;
  710. params += '&iid='+lms_item_id;
  711. obj_string = '';
  712. for (i in item_objectives){
  713. obj_string += '&objectives['+i+']=';
  714. obj_temp = '[';
  715. for (j in item_objectives[i]) {
  716. obj_temp += item_objectives[i][j]+',';
  717. }
  718. obj_temp = obj_temp.substr(0,(obj_temp.length-2)) + ']';
  719. obj_string += encodeURIComponent(obj_temp);
  720. }
  721. params += obj_string;
  722. $.ajax({
  723. type: "POST",
  724. data: params,
  725. url: "lp_ajax_save_objectives.php",
  726. dataType: "script",
  727. async: false
  728. });
  729. }
  730. /**
  731. * Switch between two items through
  732. * an AJAX call. Originally, we used the xajax library. Now we use jQuery
  733. */
  734. function xajax_switch_item_details(lms_lp_id,lms_user_id,lms_view_id,lms_item_id,next_item) {
  735. params = {
  736. 'lid': lms_lp_id,
  737. 'uid': lms_user_id,
  738. 'vid': lms_view_id,
  739. 'iid': lms_item_id,
  740. 'next': next_item
  741. }
  742. $.ajax({
  743. type: "POST",
  744. data: params,
  745. url: "lp_ajax_switch_item.php",
  746. dataType: "script",
  747. async: false
  748. });
  749. }
  750. addEvent(window,'load',addListeners,false);