lp_comm.server.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. <?php //$id: $
  2. /**
  3. * This script contains the server part of the xajax interaction process. The client part is located
  4. * in lp_api.php or other api's.
  5. * This is a first attempt at using xajax and AJAX in general, so the code might be a bit unsettling.
  6. * @package dokeos.learnpath
  7. * @author Yannick Warnier <ywarnier@beeznest.org>
  8. */
  9. /**
  10. * Script
  11. */
  12. //flag to allow for anonymous user - needs to be set before global.inc.php
  13. $use_anonymous = true;
  14. // name of the language file that needs to be included
  15. $language_file[] = 'learnpath';
  16. require_once('back_compat.inc.php');
  17. /**
  18. * Backup an item's values into the javascript API as "old" values (so we still have them at hand)
  19. * @param integer Learnpath ID
  20. * @param integer User ID
  21. * @param integer View ID
  22. * @param integer Item ID
  23. * @param double Current score
  24. * @param double Maximum score
  25. * @param double Minimum score
  26. * @param string Lesson status
  27. * @param string Session time
  28. * @param string Suspend data
  29. * @param string Lesson location
  30. */
  31. function backup_item_details($lp_id,$user_id,$view_id,$item_id,$score=-1,$max=-1,$min=-1,$status='',$time='',$suspend='',$location='')
  32. {
  33. $objResponse = new xajaxResponse();
  34. $objResponse->addScript(
  35. "old_score=".$score.";" .
  36. "old_max=".$max.";" .
  37. "old_min=".$min.";" .
  38. "old_lesson_status='".$status."';" .
  39. "old_session_time='".$time."';" .
  40. "lms_old_item_id='".$item_id."';" .
  41. "old_suspend_data='".$suspend."';" .
  42. "old_lesson_location='".$location."';");
  43. //$objResponse->addAlert('data for item '.$item_id.', user '.$user_id.' backed up');
  44. return $objResponse;
  45. }
  46. /**
  47. * Writes an item's new values into the database and returns the operation result
  48. * @param integer Learnpath ID
  49. * @param integer User ID
  50. * @param integer View ID
  51. * @param integer Item ID
  52. * @param double Current score
  53. * @param double Maximum score
  54. * @param double Minimum score
  55. * @param string Lesson status
  56. * @param string Session time
  57. * @param string Suspend data
  58. * @param string Lesson location
  59. * @param string Core exit SCORM string
  60. */
  61. function save_item($lp_id,$user_id,$view_id,$item_id,$score=-1,$max=-1,$min=-1,$status='',$time=0,$suspend='',$location='',$interactions=array(),$core_exit='none')
  62. {
  63. global $_configuration;
  64. $debug=0;
  65. if($debug>0){error_log('In xajax_save_item('.$lp_id.','.$user_id.','.$view_id.','.$item_id.','.$score.','.$max.','.$min.','.$status.','.$time.',"'.$suspend.'","'.$location.'","'.(count($interactions)>0?$interactions[0]:'').'","'.$core_exit.'")',0);}
  66. $objResponse = new xajaxResponse();
  67. require_once('learnpath.class.php');
  68. require_once('scorm.class.php');
  69. require_once('aicc.class.php');
  70. require_once('learnpathItem.class.php');
  71. require_once('scormItem.class.php');
  72. require_once('aiccItem.class.php');
  73. $mylp = '';
  74. if(isset($_SESSION['lpobject']))
  75. {
  76. if($debug>1){error_log('////$_SESSION[lpobject] is set',0);}
  77. $oLP =& unserialize($_SESSION['lpobject']);
  78. if(!is_object($oLP)){
  79. if($debug>2){error_log(print_r($oLP,true),0);}
  80. if($debug>2){error_log('////Building new lp',0);}
  81. unset($oLP);
  82. $code = api_get_course_id();
  83. $mylp = & new learnpath($code,$lp_id,$user_id);
  84. }else{
  85. if($debug>2){error_log('////Reusing session lp',0);}
  86. $mylp = & $oLP;
  87. }
  88. }
  89. //$objResponse->addAlert(api_get_path(REL_CODE_PATH).'newscorm/learnpathItem.class.php');
  90. $prereq_check = $mylp->prerequisites_match($item_id);
  91. if($prereq_check === true) //launch the prerequisites check and set error if needed
  92. {
  93. $mylpi =& $mylp->items[$item_id];
  94. //$mylpi->set_lp_view($view_id);
  95. if($score!=-1){
  96. $mylpi->set_score($score);
  97. }
  98. if($max!=-1){
  99. $mylpi->max_score=$max;
  100. }
  101. if($min!=-1){
  102. $mylpi->min_score=$min;
  103. }
  104. if($status!='')
  105. {
  106. if($debug>1){error_log('Calling set_status('.$status.') from xajax',0);}
  107. $mylpi->set_status($status);
  108. if($debug>1){error_log('Done calling set_status from xajax',0);}
  109. }
  110. if($time!='')
  111. {
  112. //if big integer, then it's a timestamp, otherwise it's normal scorm time
  113. if($time == intval(strval($time)) && $time>1000000){
  114. $real_time = time() - $time;
  115. //$real_time += $mylpi->get_total_time();
  116. $mylpi->set_time($real_time,'int');
  117. }else{
  118. $mylpi->set_time($time);
  119. }
  120. }
  121. if($suspend!='')
  122. {
  123. $mylpi->current_data = $suspend;//escapetxt($suspend);
  124. }
  125. if($location!='')
  126. {
  127. $mylpi->set_lesson_location($location);
  128. }
  129. //deal with interactions provided in arrays in the following format
  130. //id(0), type(1), time(2), weighting(3),correct_responses(4),student_response(5),result(6),latency(7)
  131. if(is_array($interactions) && count($interactions)>0){
  132. foreach($interactions as $index=>$interaction){
  133. $mylpi->add_interaction($index,$interactions[$index]);
  134. }
  135. }
  136. $mylpi->set_core_exit($core_exit);
  137. $mylp->save_item($item_id,false);
  138. }
  139. $mystatus = $mylpi->get_status(false);
  140. $mytotal = $mylp->get_total_items_count_without_chapters();
  141. $mycomplete = $mylp->get_complete_items_count();
  142. $myprogress_mode = $mylp->get_progress_bar_mode();
  143. $myprogress_mode = ($myprogress_mode==''?'%':$myprogress_mode);
  144. //$mylpi->write_to_db();
  145. $_SESSION['lpobject'] = serialize($mylp);
  146. if($mylpi->get_type()!='sco')
  147. { //if this object's JS status has not been updated by the SCORM API, update now
  148. $objResponse->addScript("lesson_status='".$mystatus."';");
  149. }
  150. $objResponse->addScript("update_toc('".$mystatus."','".$item_id."');");
  151. $update_list = $mylp->get_update_queue();
  152. foreach($update_list as $my_upd_id => $my_upd_status)
  153. {
  154. if($my_upd_id != $item_id){ //only update the status from other items (i.e. parents and brothers), do not update current as we just did it already
  155. $objResponse->addScript("update_toc('".$my_upd_status."','".$my_upd_id."');");
  156. }
  157. }
  158. $objResponse->addScript("update_progress_bar('$mycomplete','$mytotal','$myprogress_mode');");
  159. if($debug>0){
  160. $objResponse->addScript("logit_lms('Saved data for item ".$item_id.", user ".$user_id." (status=".$mystatus.")',2)");
  161. if($debug>1){error_log('End of xajax_save_item()',0);}
  162. }
  163. if($_configuration['tracking_enabled'] && !isset($_SESSION['login_as']))
  164. { // if $_SESSION['login_as'] is set, then the user is an admin logged as the user
  165. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  166. $sql_last_connection="SELECT login_id, login_date FROM $tbl_track_login WHERE login_user_id='".api_get_user_id()."' ORDER BY login_date DESC LIMIT 0,1";
  167. $q_last_connection=api_sql_query($sql_last_connection);
  168. if(Database::num_rows($q_last_connection) > 0)
  169. {
  170. $row = Database::fetch_array($q_last_connection);
  171. $i_id_last_connection=$row['login_id'];
  172. $s_sql_update_logout_date="UPDATE $tbl_track_login SET logout_date=NOW() WHERE login_id='$i_id_last_connection'";
  173. api_sql_query($s_sql_update_logout_date);
  174. }
  175. }
  176. return $objResponse;
  177. }
  178. /**
  179. * Writes an item's new values into the database and returns the operation result
  180. * @param integer Learnpath ID
  181. * @param integer User ID
  182. * @param integer View ID
  183. * @param integer Item ID
  184. * @param array Objectives array
  185. */
  186. function save_objectives($lp_id,$user_id,$view_id,$item_id,$objectives=array())
  187. {
  188. global $_configuration;
  189. $debug=0;
  190. if($debug>0){error_log('In xajax_save_objectives('.$lp_id.','.$user_id.','.$view_id.','.$item_id.',"'.(count($objectives)>0?count($objectives):'').'")',0);}
  191. $objResponse = new xajaxResponse();
  192. require_once('learnpath.class.php');
  193. require_once('scorm.class.php');
  194. require_once('aicc.class.php');
  195. require_once('learnpathItem.class.php');
  196. require_once('scormItem.class.php');
  197. require_once('aiccItem.class.php');
  198. $mylp = '';
  199. if(isset($_SESSION['lpobject']))
  200. {
  201. if($debug>1){error_log('////$_SESSION[lpobject] is set',0);}
  202. $oLP =& unserialize($_SESSION['lpobject']);
  203. if(!is_object($oLP)){
  204. if($debug>2){error_log(print_r($oLP,true),0);}
  205. if($debug>2){error_log('////Building new lp',0);}
  206. unset($oLP);
  207. $code = api_get_course_id();
  208. $mylp = & new learnpath($code,$lp_id,$user_id);
  209. }else{
  210. if($debug>2){error_log('////Reusing session lp',0);}
  211. $mylp = & $oLP;
  212. }
  213. }
  214. $mylpi =& $mylp->items[$item_id];
  215. //error_log(__FILE__.' '.__LINE__.' '.print_r($objectives,true),0);
  216. if(is_array($objectives) && count($objectives)>0){
  217. foreach($objectives as $index=>$objective){
  218. //error_log(__FILE__.' '.__LINE__.' '.$objectives[$index][0],0);
  219. $mylpi->add_objective($index,$objectives[$index]);
  220. $mylpi->write_objectives_to_db();
  221. }
  222. }
  223. return $objResponse;
  224. }
  225. /**
  226. * Get one item's details
  227. * @param integer LP ID
  228. * @param integer user ID
  229. * @param integer View ID
  230. * @param integer Current item ID
  231. * @param integer New item ID
  232. */
  233. function switch_item_details($lp_id,$user_id,$view_id,$current_item,$next_item)
  234. {
  235. $debug=0;
  236. if($debug>0){error_log('In xajax_switch_item_details('.$lp_id.','.$user_id.','.$view_id.','.$current_item.','.$next_item.')',0);}
  237. $objResponse = new xajaxResponse();
  238. /*$item_id may be one of:
  239. * -'next'
  240. * -'previous'
  241. * -'first'
  242. * -'last'
  243. * - a real item ID
  244. */
  245. require_once('learnpath.class.php');
  246. require_once('scorm.class.php');
  247. require_once('aicc.class.php');
  248. require_once('learnpathItem.class.php');
  249. require_once('scormItem.class.php');
  250. require_once('aiccItem.class.php');
  251. $mylp = '';
  252. if(isset($_SESSION['lpobject']))
  253. {
  254. if($debug>1){error_log('////$_SESSION[lpobject] is set',0);}
  255. $oLP =& unserialize($_SESSION['lpobject']);
  256. if(!is_object($oLP)){
  257. if($debug>1){error_log(print_r($oLP,true),0);}
  258. if($debug>2){error_log('////Building new lp',0);}
  259. unset($oLP);
  260. $code = api_get_course_id();
  261. $mylp = & new learnpath($code,$lp_id,$user_id);
  262. }else{
  263. if($debug>1){error_log('////Reusing session lp',0);}
  264. $mylp = & $oLP;
  265. }
  266. }
  267. $new_item_id = 0;
  268. switch($next_item){
  269. case 'next':
  270. $mylp->set_current_item($current_item);
  271. $mylp->next();
  272. $new_item_id = $mylp->get_current_item_id();
  273. if($debug>1){error_log('In {next} - next item is '.$new_item_id.'(current: '.$current_item.')',0);}
  274. break;
  275. case 'previous':
  276. $mylp->set_current_item($current_item);
  277. $mylp->previous();
  278. $new_item_id = $mylp->get_current_item_id();
  279. if($debug>1){error_log('In {previous} - next item is '.$new_item_id.'(current: '.$current_item.')',0);}
  280. break;
  281. case 'first':
  282. $mylp->set_current_item($current_item);
  283. $mylp->first();
  284. $new_item_id = $mylp->get_current_item_id();
  285. if($debug>1){error_log('In {first} - next item is '.$new_item_id.'(current: '.$current_item.')',0);}
  286. break;
  287. case 'last':
  288. break;
  289. default:
  290. //should be filtered to check it's not hacked
  291. if($next_item == $current_item){
  292. //if we're opening the same item again
  293. $mylp->items[$current_item]->restart();
  294. }
  295. $new_item_id = $next_item;
  296. $mylp->set_current_item($new_item_id);
  297. if($debug>1){error_log('In {default} - next item is '.$new_item_id.'(current: '.$current_item.')',0);}
  298. break;
  299. }
  300. $mylp->start_current_item(true);
  301. if($mylp->force_commit){
  302. $mylp->save_current();
  303. }
  304. //$objResponse->addAlert(api_get_path(REL_CODE_PATH).'newscorm/learnpathItem.class.php');
  305. if(is_object($mylp->items[$new_item_id])){
  306. $mylpi = & $mylp->items[$new_item_id];
  307. }else{
  308. if($debug>1){error_log('In switch_item_details - generating new item object',0);}
  309. $mylpi =& new learnpathItem($new_item_id,$user_id);
  310. $mylpi->set_lp_view($view_id);
  311. }
  312. /*
  313. * now get what's needed by the SCORM API:
  314. * -score
  315. * -max
  316. * -min
  317. * -lesson_status
  318. * -session_time
  319. * -suspend_data
  320. */
  321. $myscore = $mylpi->get_score();
  322. $mymax = $mylpi->get_max();
  323. $mymin = $mylpi->get_min();
  324. $mylesson_status = $mylpi->get_status();
  325. $mylesson_location = $mylpi->get_lesson_location();
  326. $mytotal_time = $mylpi->get_scorm_time('js');
  327. $mymastery_score = $mylpi->get_mastery_score();
  328. $mymax_time_allowed = $mylpi->get_max_time_allowed();
  329. $mylaunch_data = $mylpi->get_launch_data();
  330. /*
  331. if($mylpi->get_type() == 'asset'){
  332. //temporary measure to save completion of an asset. Later on, Dokeos should trigger something on unload, maybe... (even though that would mean the last item cannot be completed)
  333. $mylesson_status = 'completed';
  334. $mylpi->set_status('completed');
  335. $mylpi->save();
  336. }
  337. */
  338. $mysession_time = $mylpi->get_total_time();
  339. $mysuspend_data = $mylpi->get_suspend_data();
  340. $mylesson_location = $mylpi->get_lesson_location();
  341. $objResponse->addScript(
  342. "score=".$myscore.";" .
  343. "max=".$mymax.";" .
  344. "min=".$mymin.";" .
  345. "lesson_status='".$mylesson_status."';" .
  346. "lesson_location='".$mylesson_location."';" .
  347. "session_time='".$mysession_time."';" .
  348. "suspend_data='".$mysuspend_data."';" .
  349. "lesson_location='".$mylesson_location."';" .
  350. "total_time = '".$mytotal_time."';" .
  351. "mastery_score = '".$mymastery_score."';" .
  352. "max_time_allowed = '".$mymax_time_allowed."';" .
  353. "launch_data = '".$mylaunch_data."';" .
  354. "interactions = new Array();" .
  355. "item_objectives = new Array();" .
  356. "G_lastError = 0;" .
  357. "G_LastErrorMessage = 'No error';");
  358. /*
  359. * and re-initialise the rest
  360. * -saved_lesson_status = 'not attempted'
  361. * -lms_lp_id
  362. * -lms_item_id
  363. * -lms_old_item_id
  364. * -lms_new_item_id
  365. * -lms_been_synchronized
  366. * -lms_initialized
  367. * -lms_total_lessons
  368. * -lms_complete_lessons
  369. * -lms_progress_bar_mode
  370. * -lms_view_id
  371. * -lms_user_id
  372. */
  373. $mytotal = $mylp->get_total_items_count_without_chapters();
  374. $mycomplete = $mylp->get_complete_items_count();
  375. $myprogress_mode = $mylp->get_progress_bar_mode();
  376. $myprogress_mode = ($myprogress_mode==''?'%':$myprogress_mode);
  377. $mynext = $mylp->get_next_item_id();
  378. $myprevious = $mylp->get_previous_item_id();
  379. $myitemtype = $mylpi->get_type();
  380. $mylesson_mode = $mylpi->get_lesson_mode();
  381. $mycredit = $mylpi->get_credit();
  382. $mylaunch_data = $mylpi->get_launch_data();
  383. $myinteractions_count = $mylpi->get_interactions_count();
  384. $myobjectives_count = $mylpi->get_objectives_count();
  385. $mycore_exit = $mylpi->get_core_exit();
  386. $objResponse->addScript(
  387. "saved_lesson_status='not attempted';" .
  388. "lms_lp_id=".$lp_id.";" .
  389. "lms_item_id=".$new_item_id.";" .
  390. "lms_old_item_id=0;" .
  391. "lms_been_synchronized=0;" .
  392. "lms_initialized=0;" .
  393. "lms_total_lessons=".$mytotal.";" .
  394. "lms_complete_lessons=".$mycomplete.";" .
  395. "lms_progress_bar_mod='".$myprogress_mode."';" .
  396. "lms_view_id=".$view_id.";" .
  397. "lms_user_id=".$user_id.";" .
  398. "next_item=".$new_item_id.";" . //this one is very important to replace possible literal strings
  399. "lms_next_item=".$mynext.";" .
  400. "lms_previous_item=".$myprevious.";" .
  401. "lms_item_type = '".$myitemtype."';" .
  402. "lms_item_credit = '".$mycredit."';" .
  403. "lms_item_lesson_mode = '".$mylesson_mode."';" .
  404. "lms_item_launch_data = '".$mylaunch_data."';" .
  405. "lms_item_interactions_count = '".$myinteractions_count."';" .
  406. "lms_item_objectives_count = '".$myinteractions_count."';" .
  407. "lms_item_core_exit = '".$mycore_exit."';" .
  408. "asset_timer = 0;"
  409. );
  410. $objResponse->addScript("update_toc('unhighlight','".$current_item."');");
  411. $objResponse->addScript("update_toc('highlight','".$new_item_id."');");
  412. $objResponse->addScript("update_toc('$mylesson_status','".$new_item_id."');");
  413. $objResponse->addScript("update_progress_bar('$mycomplete','$mytotal','$myprogress_mode');");
  414. $mylp->set_error_msg('');
  415. $mylp->prerequisites_match(); //check the prerequisites are all complete
  416. if($debug>1){error_log('Prereq_match() returned '.htmlentities($mylp->error),0);}
  417. $objResponse->addScript("update_message_frame('".str_replace("'","\'",htmlentities($mylp->error))."');");
  418. $_SESSION['scorm_item_id'] = $new_item_id;//Save the new item ID for the exercise tool to use
  419. $_SESSION['lpobject'] = serialize($mylp);
  420. return $objResponse;
  421. }
  422. /**
  423. * Start a timer and hand it back to the JS by assigning the current time (of start) to
  424. * var asset_timer
  425. */
  426. function start_timer()
  427. {
  428. $objResponse = new xajaxResponse();
  429. $time = time();
  430. $objResponse->addScript("asset_timer='$time';asset_timer_total=0;");
  431. return $objResponse;
  432. }
  433. require('lp_comm.common.php');
  434. $xajax->processRequests();
  435. ?>