aicc_api.php 27 KB

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