aicc_api.php 27 KB

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