aicc_api.php 27 KB

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