aicc_api.php 27 KB

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