aicc_api.php 27 KB

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