aicc_api.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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 '../inc/global.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. $file = $_SESSION['file'];
  34. $oLP = unserialize($_SESSION['lpobject']);
  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. //var update = update_toc(lesson_status,lms_item_id);
  246. <?php } ?>
  247. break;
  248. case 'cmi.completion_status' : lesson_status = val; break; //1.3
  249. case 'cmi.core.session_time' : session_time = val; break;
  250. case 'cmi.score.scaled' : score = val ; break; //1.3
  251. case 'cmi.success_status' : success_status = val; break; //1.3
  252. case 'cmi.suspend_data' : suspend_data = val; break;
  253. }
  254. //var update = update_toc();
  255. //var update_progress = update_progress_bar();
  256. <?php
  257. if ($oLP->force_commit == 1) {
  258. echo " var mycommit = LMSCommit('force');";
  259. }
  260. ?>
  261. return(true);
  262. }
  263. function savedata(origin) { //origin can be 'commit', 'finish' or 'terminate'
  264. <?php if ($autocomplete_when_80pct) { ?>
  265. if( ( lesson_status == 'incomplete') && (score >= (0.8*max) ) ){
  266. lesson_status = 'completed';
  267. }
  268. <?php }?>
  269. param = 'id='+lms_item_id+'&origin='+origin+'&score='+score+'&max='+max+'&min='+min+'&lesson_status='+lesson_status+'&time='+session_time+'&suspend_data='+suspend_data;
  270. url="http://<?php
  271. $self = api_get_self();
  272. $url = $_SERVER['HTTP_HOST'].$self;
  273. $url = substr($url, 0, -14); // 14 is the length of this file's name (/scorm_api.php).
  274. echo $url;
  275. ?>/lp_controller.php?cidReq=<?php echo api_get_course_id();?>&action=save&lp_id=<?php echo $oLP->get_id();?>&" + param + "";
  276. logit_lms('saving data (status='+lesson_status+')',1);
  277. 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);
  278. //xajax_update_pgs();
  279. //xajax_update_toc();
  280. }
  281. function LMSCommit(val) {
  282. logit_scorm('LMSCommit()',0);
  283. commit = true ;
  284. savedata('commit');
  285. return('true');
  286. }
  287. function LMSFinish(val) {
  288. if ( !commit ) {
  289. logit_scorm('LMSFinish() (no LMSCommit())',1);
  290. }
  291. if ( commit ) {
  292. logit_scorm('LMSFinish() called',1);
  293. savedata('finish');
  294. }
  295. return('true');
  296. }
  297. function LMSGetLastError() {
  298. logit_scorm('LMSGetLastError()',1);
  299. return(G_LastError);
  300. }
  301. function LMSGetErrorString(errCode){
  302. logit_scorm('LMSGetErrorString()',1);
  303. return('No error !');
  304. }
  305. function LMSGetDiagnostic(errCode){
  306. logit_scorm('LMSGetDiagnostic()',1);
  307. return(API.LMSGetLastError());
  308. }
  309. <?php
  310. /**
  311. * Chamilo-specific code that deals with event handling and inter-frames
  312. * messaging/refreshing.
  313. * Note that from now on, the Chamilo JS code in this library will act as
  314. * a controller, of the MVC pattern, and receive all requests for frame
  315. * updates, then redispatch to any frame concerned.
  316. */
  317. ?>
  318. /**
  319. * Defining the AJAX-object class to be made available from other frames.
  320. */
  321. function XAJAXobject() {
  322. this.xajax_switch_item_details=xajax_switch_item_details;
  323. this.switch_item=switch_item;
  324. }
  325. //it is not sure that the scos use the above declarations
  326. oXAJAX = new XAJAXobject();
  327. oxajax = new XAJAXobject();
  328. /**
  329. * Cross-browser event handling by Scott Andrew
  330. * @param element Element that needs an event attached
  331. * @param string Event type (load, unload, click, keyDown, ...)
  332. * @param string Function name (the event handler)
  333. * @param string used in addEventListener
  334. */
  335. function addEvent(elm, evType, fn, useCapture){
  336. if(elm.addEventListener){
  337. elm.addEventListener(evType, fn, useCapture);
  338. return true;
  339. }else if (elm.attachEvent){
  340. var r = elm.attachEvent('on' + evType, fn);
  341. }else{
  342. elm['on'+evType] = fn;
  343. }
  344. }
  345. /**
  346. * Add listeners to the page objects. This has to be defined for
  347. * the current context as it acts on objects that should exist
  348. * on the page
  349. */
  350. function addListeners(){
  351. //exit if the browser doesn't support ID or tag retrieval
  352. logit_lms('Entering addListeners()',2);
  353. if(!document.getElementsByTagName){
  354. logit_lms("getElementsByTagName not available",2);
  355. return;
  356. }
  357. if(!document.getElementById){
  358. logit_lms("getElementById not available",2);
  359. return;
  360. }
  361. //assign event handlers to objects
  362. if(lms_lp_type==1 || lms_item_type=='asset'){
  363. logit_lms('Dokeos LP or asset',2);
  364. // If this path is a Chamilo learnpath, then start manual save
  365. // when something is loaded in there.
  366. var myelem = document.getElementById('content_id');
  367. if(!myelem){logit_lms("Impossible to find content_id element in document",2);}
  368. addEvent(myelem,'unload',chamilo_save_asset,false);
  369. logit_lms('Added event listener on content_id for unload',2);
  370. }
  371. logit_lms('Quitting addListeners()',2);
  372. }
  373. /**
  374. * Load an item into the content frame:
  375. * - making sure the previous item status have been saved
  376. * - first updating the current item ID (to save the right item)
  377. * - updating the frame src
  378. */
  379. function load_item(item_id,url){
  380. if(document.getElementById('content_id'))
  381. {
  382. logit_lms('Loading item '+item_id,2);
  383. var cont_f = document.getElementById('content_id');
  384. if(cont_f.src){
  385. lms_old_item_id = lms_item_id;
  386. var lms_new_item_id = item_id;
  387. //load new content page into content frame
  388. if(lms_lp_type==1 || lms_item_type=='asset'){
  389. chamilo_save_asset();
  390. }
  391. cont_f.src = url;
  392. update_toc('unhighlight',lms_old_item_id);
  393. update_toc('highlight',item_id);
  394. /* legacy code
  395. lms_been_synchronized = 0;
  396. lms_initialized = 0;
  397. if(lms_lp_type==1 || lms_item_type=='asset'){
  398. lms_item_id = lms_new_item_id;
  399. }*/
  400. return true;
  401. }
  402. logit_lms('cont_f.src has no properties',0);
  403. }
  404. logit_lms('content_id has no properties',0);
  405. return false;
  406. }
  407. /**
  408. * Save a Chamilo learnpath item's time and mark as completed upon
  409. * leaving it
  410. */
  411. function chamilo_save_asset(){
  412. //var linkparams = 'id='+lms_item_id+'&score='+score+'&max='+max+'&min='+min+'&lesson_status='+lesson_status+'&time='+session_time+'&suspend_data='+suspend_data;
  413. //var url = "<?php echo api_get_path(WEB_CODE_PATH).'newscorm/lp_controller.php'; ?>?action=save&" + linkparams + "";
  414. logit_lms('chamilo_save_asset: '+url,0);
  415. //frames["message_name"].src = url;
  416. 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);
  417. }
  418. /**
  419. * Logs information about SCORM messages into the log frame
  420. * @param string Message to log
  421. * @param integer Priority (0 for top priority, 3 for lowest)
  422. */
  423. function logit_scorm(message,priority) {
  424. if (scorm_logs) {
  425. log_in_log("SCORM: " + message);
  426. }
  427. return false;
  428. /*if(frames["lp_log_name"] && scorm_logs>priority){
  429. frames["lp_log_name"].document.getElementById("log_content").innerHTML += "AICC: " + message + "<br/>";
  430. }*/
  431. }
  432. function log_in_log(message) {
  433. var ua = $.browser;
  434. if (ua.mozilla) {
  435. console.log(message);
  436. } else {
  437. if (window.console) {
  438. window.console.log(message);
  439. }
  440. }
  441. }
  442. /**
  443. * Logs information about LMS activity into the log frame
  444. * @param string Message to log
  445. * @param integer Priority (0 for top priority, 3 for lowest)
  446. */
  447. function logit_lms(message,priority) {
  448. /*
  449. if(frames["lp_log_name"] && lms_logs>priority){
  450. frames["lp_log_name"].document.getElementById("log_content").innerHTML += "LMS: " + message + "<br/>";
  451. }*/
  452. if (scorm_logs) {
  453. log_in_log("LMS: " + message);
  454. }
  455. }
  456. /**
  457. * update the Table Of Contents frame, by changing CSS styles, mostly
  458. * @param string Action to be taken
  459. * @param integer Item id to update
  460. */
  461. function update_toc(update_action,update_id)
  462. {
  463. <?php if ($oLP->mode != 'fullscreen') { ?>
  464. var myframe = frames["toc_name"];
  465. var myelem = myframe.document.getElementById("toc_"+update_id);
  466. var myelemimg = myframe.document.getElementById("toc_img_"+update_id);
  467. logit_lms('update_toc('+update_action+','+update_id+')',2);
  468. if(update_id != 0){
  469. switch(update_action){
  470. case 'unhighlight':
  471. myelem.className = "scorm_item";
  472. break;
  473. case 'highlight':
  474. myelem.className = "scorm_item_highlight";
  475. break;
  476. case 'not attempted':
  477. if(myelemimg.src != '../img/notattempted.gif'){
  478. myelemimg.src = "../img/notattempted.gif";
  479. myelemimg.alt = "not attempted";
  480. }
  481. break;
  482. case 'incomplete':
  483. if(myelemimg.src != '../img/incomplete.png'){
  484. myelemimg.src = "../img/incomplete.png";
  485. myelemimg.alt = "incomplete";
  486. }
  487. break;
  488. case 'completed':
  489. if(myelemimg.src != '../img/completed.png'){
  490. myelemimg.src = "../img/completed.png";
  491. myelemimg.alt = "completed";
  492. }
  493. break;
  494. case 'failed':
  495. if(myelemimg.src != '../img/failed.png'){
  496. myelemimg.src = "../img/failed.png";
  497. myelemimg.alt = "failed";
  498. }
  499. break;
  500. case 'passed':
  501. if(myelemimg.src != '../img/completed.png' && myelemimg.alt != 'passed'){
  502. myelemimg.src = "../img/completed.png";
  503. myelemimg.alt = "passed";
  504. }
  505. break;
  506. case 'browsed':
  507. if(myelemimg.src != '../img/completed.png' && myelemimg.alt != 'browsed'){
  508. myelemimg.src = "../img/completed.png";
  509. myelemimg.alt = "browsed";
  510. }
  511. break;
  512. default:
  513. logit_lms('Update action unknown',2);
  514. break;
  515. }
  516. }
  517. return true;
  518. <?php } ?>
  519. return true;
  520. }
  521. /**
  522. * Updates the progress bar with the new status. Prevents the need of a page refresh and flickering
  523. * @param integer Number of completed items
  524. * @param integer Number of items in total
  525. * @param string Display mode (absolute 'abs' or percentage '%').Defaults to %
  526. */
  527. function update_progress_bar(nbr_complete, nbr_total, mode)
  528. {
  529. logit_lms('update_progress_bar('+nbr_complete+','+nbr_total+','+mode+')',2);
  530. logit_lms('could update with data: '+lms_lp_id+','+lms_view_id+','+lms_user_id,2);
  531. var myframe = frames["nav_name"];
  532. if(myframe){
  533. if(mode == ''){mode='%';}
  534. if(nbr_total == 0){nbr_total=1;}
  535. var percentage = (nbr_complete/nbr_total)*100;
  536. percentage = Math.round(percentage);
  537. var progress_bar = $("#progress_bar_value");
  538. progress_bar.css('width', percentage +"%");
  539. /*
  540. var pr_text = myframe.document.getElementById('progress_text');
  541. var pr_full = myframe.document.getElementById('progress_img_full');
  542. var pr_empty = myframe.document.getElementById('progress_img_empty');
  543. pr_full.width = percentage;
  544. pr_empty.width = 100-percentage;
  545. */
  546. var mytext = '';
  547. switch(mode){
  548. case 'abs':
  549. mytext = nbr_complete + '/' + nbr_total;
  550. break;
  551. case '%':
  552. default:
  553. mytext = percentage + '%';
  554. break;
  555. }
  556. pr_text.innerHTML = mytext;
  557. }
  558. return true;
  559. }
  560. function update_stats_page()
  561. {
  562. var myframe = document.getElementById('content_id');
  563. var mysrc = myframe.location.href;
  564. if(mysrc == 'lp_controller.php?action=stats'){
  565. if(myframe && myframe.src){
  566. var mysrc = myframe.src;
  567. myframe.src = mysrc;
  568. }
  569. // = mysrc; //refresh page
  570. }
  571. return true;
  572. }
  573. /**
  574. * Updates the message frame with the given string
  575. */
  576. function update_message_frame(msg_msg)
  577. {
  578. if(msg_msg==null){msg_msg='';}
  579. var msg_f = frames["message_name"];
  580. if(!msg_f.document || !msg_f.document.getElementById('msg_div_id')){
  581. logit_lms('In update_message_frame() - message frame has no document property',0);
  582. }else{
  583. logit_lms('In update_message_frame() - updating frame',0);
  584. msg_f.document.getElementById('msg_div_id').innerHTML= msg_msg;
  585. }
  586. }
  587. /**
  588. * Function that handles the saving of an item and switching from an item to another.
  589. * Once called, this function should be able to do the whole process of (1) saving the
  590. * current item, (2) refresh all the values inside the SCORM API object, (3) open the
  591. * new item into the content_id frame, (4) refresh the table of contents, (5) refresh
  592. * the progress bar (completion), (6) refresh the message frame
  593. * @param integer Chamilo ID for the current item
  594. * @param string This parameter can be a string specifying the next
  595. * item (like 'next', 'previous', 'first' or 'last') or the id to the next item
  596. */
  597. function switch_item(current_item, next_item){
  598. /*
  599. if(!current_item){
  600. logit_lms('In switch - no current_item defined',0);
  601. }
  602. if(!next_item){
  603. logit_lms('In switch - no next_item defined',0);
  604. }
  605. */
  606. if(lms_item_id == next_item){
  607. return; //nothing to switch
  608. }
  609. //(1) save the current item
  610. logit_lms('Called switch_item with params '+lms_item_id+' and '+next_item+'',0);
  611. 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);
  612. //(2) Refresh all the values inside this SCORM API object - use AJAX
  613. xajax_switch_item_details(lms_lp_id,lms_user_id,lms_view_id,lms_item_id,next_item);
  614. //(3) open the new item in the content_id frame
  615. var cont_f = document.getElementById('content_id');
  616. if(!cont_f){logit_lms('In switch - content frame not found',0);return false;}
  617. switch(next_item){
  618. case 'next':
  619. next_item = lms_next_item;
  620. break;
  621. case 'previous':
  622. next_item = lms_previous_item;
  623. break;
  624. default:
  625. break;
  626. }
  627. cont_f.src = 'lp_controller.php?action=content&lp_id='+lms_lp_id+'&item_id='+next_item;
  628. //(4) refresh table of contents
  629. /*
  630. var toc_f = document.getElementById('toc_id');
  631. if(!toc_f){logit_lms('In switch - toc frame not found',0);return false;}
  632. var myrefresh = toc_f.src;
  633. toc_f.src = myrefresh;
  634. */
  635. //(5) refresh the progress bar
  636. /*
  637. var prg_f = document.getElementById('nav_id');
  638. if(!prg_f){logit_lms('In switch - navigation frame not found',0);return false;}
  639. var myrefresh = prg_f.src;
  640. prg_f.src = myrefresh;
  641. */
  642. //(6) refresh the message box (included in switch_item_details)
  643. return true;
  644. }
  645. /**
  646. * Save a specific item (with its interactions, if any) into the LMS through
  647. * an AJAX call. Originally, we used the xajax library. Now we use jQuery.
  648. * Because of the need to pass an array, we have to build the parameters
  649. * manually into GET[]
  650. */
  651. 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) {
  652. params='';
  653. params += 'lid='+lms_lp_id+'&uid='+lms_user_id+'&vid='+lms_view_id;
  654. params += '&iid='+lms_item_id+'&s='+score+'&max='+max+'&min='+min;
  655. params += '&status='+lesson_status+'&t='+session_time;
  656. params += '&suspend='+suspend_data+'&loc='+lesson_location;
  657. params += '&core_exit='+lms_item_core_exit;
  658. interact_string = '';
  659. for (i in interactions){
  660. interact_string += '&interact['+i+']=';
  661. interact_temp = '[';
  662. for (j in interactions[i]) {
  663. interact_temp += interactions[i][j]+',';
  664. }
  665. interact_temp = interact_temp.substr(0,(interact_temp.length-2)) + ']';
  666. interact_string += encodeURIComponent(interact_temp);
  667. }
  668. //interact_string = encodeURIComponent(interact_string.substr(0,(interact_string.length-1)));
  669. params += interact_string;
  670. /*params = {
  671. 'lid': lms_lp_id,
  672. 'uid': lms_user_id,
  673. 'vid': lms_view_id,
  674. 'iid': lms_item_id,
  675. 's': score,
  676. 'max': max,
  677. 'min': min,
  678. 'status': lesson_status,
  679. 't': session_time,
  680. 'suspend': suspend_data,
  681. 'loc': lesson_location,
  682. 'interact': interac_string,
  683. 'core_exit': lms_item_core_exit
  684. }
  685. */
  686. $.ajax({
  687. type:"POST",
  688. data: params,
  689. url: "lp_ajax_save_item.php",
  690. dataType: "script",
  691. async: false
  692. }
  693. );
  694. }
  695. /**
  696. * Starts the timer with the server clock time.
  697. * Originally, we used the xajax library. Now we use jQuery
  698. */
  699. function xajax_start_timer() {
  700. $.ajax({
  701. type: "GET",
  702. url: "lp_ajax_start_timer.php",
  703. dataType: "script",
  704. async: false
  705. });
  706. }
  707. /**
  708. * Save a specific item's objectives into the LMS through
  709. * an AJAX call. Originally, we used the xajax library. Now we use jQuery
  710. */
  711. function xajax_save_objectives(lms_lp_id,lms_user_id,lms_view_id,lms_item_id,item_objectives) {
  712. params='';
  713. params += 'lid='+lms_lp_id+'&uid='+lms_user_id+'&vid='+lms_view_id;
  714. params += '&iid='+lms_item_id;
  715. obj_string = '';
  716. for (i in item_objectives){
  717. obj_string += '&objectives['+i+']=';
  718. obj_temp = '[';
  719. for (j in item_objectives[i]) {
  720. obj_temp += item_objectives[i][j]+',';
  721. }
  722. obj_temp = obj_temp.substr(0,(obj_temp.length-2)) + ']';
  723. obj_string += encodeURIComponent(obj_temp);
  724. }
  725. params += obj_string;
  726. $.ajax({
  727. type: "POST",
  728. data: params,
  729. url: "lp_ajax_save_objectives.php",
  730. dataType: "script",
  731. async: false
  732. });
  733. }
  734. /**
  735. * Switch between two items through
  736. * an AJAX call. Originally, we used the xajax library. Now we use jQuery
  737. */
  738. function xajax_switch_item_details(lms_lp_id,lms_user_id,lms_view_id,lms_item_id,next_item) {
  739. params = {
  740. 'lid': lms_lp_id,
  741. 'uid': lms_user_id,
  742. 'vid': lms_view_id,
  743. 'iid': lms_item_id,
  744. 'next': next_item
  745. }
  746. $.ajax({
  747. type: "POST",
  748. data: params,
  749. url: "lp_ajax_switch_item.php",
  750. dataType: "script",
  751. async: false
  752. });
  753. }
  754. addEvent(window,'load',addListeners,false);