lp_comm.server.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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($max!=-1)
  96. {
  97. $mylpi->max_score=$max;
  98. }
  99. if($min!=-1)
  100. {
  101. $mylpi->min_score=$min;
  102. }
  103. if($score!=-1)
  104. {
  105. $mylpi->set_score($score);
  106. }
  107. if($status!='')
  108. {
  109. if($debug>1){error_log('Calling set_status('.$status.') from xajax',0);}
  110. $mylpi->set_status($status);
  111. if($debug>1){error_log('Done calling set_status from xajax',0);}
  112. }
  113. if($time!='')
  114. {
  115. //if big integer, then it's a timestamp, otherwise it's normal scorm time
  116. if($time == intval(strval($time)) && $time>1000000){
  117. $real_time = time() - $time;
  118. //$real_time += $mylpi->get_total_time();
  119. $mylpi->set_time($real_time,'int');
  120. }else{
  121. $mylpi->set_time($time);
  122. }
  123. }
  124. if($suspend!='')
  125. {
  126. $mylpi->current_data = $suspend;//escapetxt($suspend);
  127. }
  128. if($location!='')
  129. {
  130. $mylpi->set_lesson_location($location);
  131. }
  132. //deal with interactions provided in arrays in the following format
  133. //id(0), type(1), time(2), weighting(3),correct_responses(4),student_response(5),result(6),latency(7)
  134. if(is_array($interactions) && count($interactions)>0){
  135. foreach($interactions as $index=>$interaction){
  136. $mylpi->add_interaction($index,$interactions[$index]);
  137. }
  138. }
  139. $mylpi->set_core_exit($core_exit);
  140. $mylp->save_item($item_id,false);
  141. }else{
  142. return $objResponse;
  143. }
  144. $mystatus = $mylpi->get_status(false);
  145. $mytotal = $mylp->get_total_items_count_without_chapters();
  146. $mycomplete = $mylp->get_complete_items_count();
  147. $myprogress_mode = $mylp->get_progress_bar_mode();
  148. $myprogress_mode = ($myprogress_mode==''?'%':$myprogress_mode);
  149. //$mylpi->write_to_db();
  150. $_SESSION['lpobject'] = serialize($mylp);
  151. if($mylpi->get_type()!='sco')
  152. { //if this object's JS status has not been updated by the SCORM API, update now
  153. $objResponse->addScript("lesson_status='".$mystatus."';");
  154. }
  155. $objResponse->addScript("update_toc('".$mystatus."','".$item_id."');");
  156. $update_list = $mylp->get_update_queue();
  157. foreach($update_list as $my_upd_id => $my_upd_status)
  158. {
  159. 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
  160. $objResponse->addScript("update_toc('".$my_upd_status."','".$my_upd_id."');");
  161. }
  162. }
  163. $objResponse->addScript("update_progress_bar('$mycomplete','$mytotal','$myprogress_mode');");
  164. if($debug>0){
  165. $objResponse->addScript("logit_lms('Saved data for item ".$item_id.", user ".$user_id." (status=".$mystatus.")',2)");
  166. if($debug>1){error_log('End of xajax_save_item()',0);}
  167. }
  168. if($_configuration['tracking_enabled'] && !isset($_SESSION['login_as']))
  169. { // if $_SESSION['login_as'] is set, then the user is an admin logged as the user
  170. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  171. $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";
  172. $q_last_connection=api_sql_query($sql_last_connection);
  173. if(Database::num_rows($q_last_connection) > 0)
  174. {
  175. $row = Database::fetch_array($q_last_connection);
  176. $i_id_last_connection=$row['login_id'];
  177. $s_sql_update_logout_date="UPDATE $tbl_track_login SET logout_date=NOW() WHERE login_id='$i_id_last_connection'";
  178. api_sql_query($s_sql_update_logout_date);
  179. }
  180. }
  181. return $objResponse;
  182. }
  183. /**
  184. * Writes an item's new values into the database and returns the operation result
  185. * @param integer Learnpath ID
  186. * @param integer User ID
  187. * @param integer View ID
  188. * @param integer Item ID
  189. * @param array Objectives array
  190. */
  191. function save_objectives($lp_id,$user_id,$view_id,$item_id,$objectives=array())
  192. {
  193. global $_configuration;
  194. $debug=0;
  195. if($debug>0){error_log('In xajax_save_objectives('.$lp_id.','.$user_id.','.$view_id.','.$item_id.',"'.(count($objectives)>0?count($objectives):'').'")',0);}
  196. $objResponse = new xajaxResponse();
  197. require_once('learnpath.class.php');
  198. require_once('scorm.class.php');
  199. require_once('aicc.class.php');
  200. require_once('learnpathItem.class.php');
  201. require_once('scormItem.class.php');
  202. require_once('aiccItem.class.php');
  203. $mylp = '';
  204. if(isset($_SESSION['lpobject']))
  205. {
  206. if($debug>1){error_log('////$_SESSION[lpobject] is set',0);}
  207. $oLP =& unserialize($_SESSION['lpobject']);
  208. if(!is_object($oLP)){
  209. if($debug>2){error_log(print_r($oLP,true),0);}
  210. if($debug>2){error_log('////Building new lp',0);}
  211. unset($oLP);
  212. $code = api_get_course_id();
  213. $mylp = & new learnpath($code,$lp_id,$user_id);
  214. }else{
  215. if($debug>2){error_log('////Reusing session lp',0);}
  216. $mylp = & $oLP;
  217. }
  218. }
  219. $mylpi =& $mylp->items[$item_id];
  220. //error_log(__FILE__.' '.__LINE__.' '.print_r($objectives,true),0);
  221. if(is_array($objectives) && count($objectives)>0){
  222. foreach($objectives as $index=>$objective){
  223. //error_log(__FILE__.' '.__LINE__.' '.$objectives[$index][0],0);
  224. $mylpi->add_objective($index,$objectives[$index]);
  225. }
  226. $mylpi->write_objectives_to_db();
  227. }
  228. return $objResponse;
  229. }
  230. /**
  231. * Get one item's details
  232. * @param integer LP ID
  233. * @param integer user ID
  234. * @param integer View ID
  235. * @param integer Current item ID
  236. * @param integer New item ID
  237. */
  238. function switch_item_details($lp_id,$user_id,$view_id,$current_item,$next_item)
  239. {
  240. $debug=0;
  241. if($debug>0){error_log('In xajax_switch_item_details('.$lp_id.','.$user_id.','.$view_id.','.$current_item.','.$next_item.')',0);}
  242. $objResponse = new xajaxResponse();
  243. /*$item_id may be one of:
  244. * -'next'
  245. * -'previous'
  246. * -'first'
  247. * -'last'
  248. * - a real item ID
  249. */
  250. require_once('learnpath.class.php');
  251. require_once('scorm.class.php');
  252. require_once('aicc.class.php');
  253. require_once('learnpathItem.class.php');
  254. require_once('scormItem.class.php');
  255. require_once('aiccItem.class.php');
  256. $mylp = '';
  257. if(isset($_SESSION['lpobject']))
  258. {
  259. if($debug>1){error_log('////$_SESSION[lpobject] is set',0);}
  260. $oLP =& unserialize($_SESSION['lpobject']);
  261. if(!is_object($oLP)){
  262. if($debug>1){error_log(print_r($oLP,true),0);}
  263. if($debug>2){error_log('////Building new lp',0);}
  264. unset($oLP);
  265. $code = api_get_course_id();
  266. $mylp = & new learnpath($code,$lp_id,$user_id);
  267. }else{
  268. if($debug>1){error_log('////Reusing session lp',0);}
  269. $mylp = & $oLP;
  270. }
  271. }
  272. $new_item_id = 0;
  273. switch($next_item){
  274. case 'next':
  275. $mylp->set_current_item($current_item);
  276. $mylp->next();
  277. $new_item_id = $mylp->get_current_item_id();
  278. if($debug>1){error_log('In {next} - next item is '.$new_item_id.'(current: '.$current_item.')',0);}
  279. break;
  280. case 'previous':
  281. $mylp->set_current_item($current_item);
  282. $mylp->previous();
  283. $new_item_id = $mylp->get_current_item_id();
  284. if($debug>1){error_log('In {previous} - next item is '.$new_item_id.'(current: '.$current_item.')',0);}
  285. break;
  286. case 'first':
  287. $mylp->set_current_item($current_item);
  288. $mylp->first();
  289. $new_item_id = $mylp->get_current_item_id();
  290. if($debug>1){error_log('In {first} - next item is '.$new_item_id.'(current: '.$current_item.')',0);}
  291. break;
  292. case 'last':
  293. break;
  294. default:
  295. //should be filtered to check it's not hacked
  296. if($next_item == $current_item){
  297. //if we're opening the same item again
  298. $mylp->items[$current_item]->restart();
  299. }
  300. $new_item_id = $next_item;
  301. $mylp->set_current_item($new_item_id);
  302. if($debug>1){error_log('In {default} - next item is '.$new_item_id.'(current: '.$current_item.')',0);}
  303. break;
  304. }
  305. $mylp->start_current_item(true);
  306. if($mylp->force_commit){
  307. $mylp->save_current();
  308. }
  309. //$objResponse->addAlert(api_get_path(REL_CODE_PATH).'newscorm/learnpathItem.class.php');
  310. if(is_object($mylp->items[$new_item_id])){
  311. $mylpi = & $mylp->items[$new_item_id];
  312. }else{
  313. if($debug>1){error_log('In switch_item_details - generating new item object',0);}
  314. $mylpi =& new learnpathItem($new_item_id,$user_id);
  315. $mylpi->set_lp_view($view_id);
  316. }
  317. /*
  318. * now get what's needed by the SCORM API:
  319. * -score
  320. * -max
  321. * -min
  322. * -lesson_status
  323. * -session_time
  324. * -suspend_data
  325. */
  326. $myscore = $mylpi->get_score();
  327. $mymax = $mylpi->get_max();
  328. $mymin = $mylpi->get_min();
  329. $mylesson_status = $mylpi->get_status();
  330. $mylesson_location = $mylpi->get_lesson_location();
  331. $mytotal_time = $mylpi->get_scorm_time('js');
  332. $mymastery_score = $mylpi->get_mastery_score();
  333. $mymax_time_allowed = $mylpi->get_max_time_allowed();
  334. $mylaunch_data = $mylpi->get_launch_data();
  335. /*
  336. if($mylpi->get_type() == 'asset'){
  337. //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)
  338. $mylesson_status = 'completed';
  339. $mylpi->set_status('completed');
  340. $mylpi->save();
  341. }
  342. */
  343. $mysession_time = $mylpi->get_total_time();
  344. $mysuspend_data = $mylpi->get_suspend_data();
  345. $mylesson_location = $mylpi->get_lesson_location();
  346. $objResponse->addScript(
  347. "score=".$myscore.";" .
  348. "max=".$mymax.";" .
  349. "min=".$mymin.";" .
  350. "lesson_status='".$mylesson_status."';" .
  351. "lesson_location='".$mylesson_location."';" .
  352. "session_time='".$mysession_time."';" .
  353. "suspend_data='".$mysuspend_data."';" .
  354. "lesson_location='".$mylesson_location."';" .
  355. "total_time = '".$mytotal_time."';" .
  356. "mastery_score = '".$mymastery_score."';" .
  357. "max_time_allowed = '".$mymax_time_allowed."';" .
  358. "launch_data = '".$mylaunch_data."';" .
  359. "interactions = new Array();" .
  360. "item_objectives = new Array();" .
  361. "G_lastError = 0;" .
  362. "G_LastErrorMessage = 'No error';");
  363. /*
  364. * and re-initialise the rest
  365. * -saved_lesson_status = 'not attempted'
  366. * -lms_lp_id
  367. * -lms_item_id
  368. * -lms_old_item_id
  369. * -lms_new_item_id
  370. * -lms_been_synchronized
  371. * -lms_initialized
  372. * -lms_total_lessons
  373. * -lms_complete_lessons
  374. * -lms_progress_bar_mode
  375. * -lms_view_id
  376. * -lms_user_id
  377. */
  378. $mytotal = $mylp->get_total_items_count_without_chapters();
  379. $mycomplete = $mylp->get_complete_items_count();
  380. $myprogress_mode = $mylp->get_progress_bar_mode();
  381. $myprogress_mode = ($myprogress_mode==''?'%':$myprogress_mode);
  382. $mynext = $mylp->get_next_item_id();
  383. $myprevious = $mylp->get_previous_item_id();
  384. $myitemtype = $mylpi->get_type();
  385. $mylesson_mode = $mylpi->get_lesson_mode();
  386. $mycredit = $mylpi->get_credit();
  387. $mylaunch_data = $mylpi->get_launch_data();
  388. $myinteractions_count = $mylpi->get_interactions_count();
  389. $myobjectives_count = $mylpi->get_objectives_count();
  390. $mycore_exit = $mylpi->get_core_exit();
  391. $objResponse->addScript(
  392. "saved_lesson_status='not attempted';" .
  393. "lms_lp_id=".$lp_id.";" .
  394. "lms_item_id=".$new_item_id.";" .
  395. "lms_old_item_id=0;" .
  396. "lms_been_synchronized=0;" .
  397. "lms_initialized=0;" .
  398. "lms_total_lessons=".$mytotal.";" .
  399. "lms_complete_lessons=".$mycomplete.";" .
  400. "lms_progress_bar_mod='".$myprogress_mode."';" .
  401. "lms_view_id=".$view_id.";" .
  402. "lms_user_id=".$user_id.";" .
  403. "next_item=".$new_item_id.";" . //this one is very important to replace possible literal strings
  404. "lms_next_item=".$mynext.";" .
  405. "lms_previous_item=".$myprevious.";" .
  406. "lms_item_type = '".$myitemtype."';" .
  407. "lms_item_credit = '".$mycredit."';" .
  408. "lms_item_lesson_mode = '".$mylesson_mode."';" .
  409. "lms_item_launch_data = '".$mylaunch_data."';" .
  410. "lms_item_interactions_count = '".$myinteractions_count."';" .
  411. "lms_item_objectives_count = '".$myinteractions_count."';" .
  412. "lms_item_core_exit = '".$mycore_exit."';" .
  413. "asset_timer = 0;"
  414. );
  415. $objResponse->addScript("update_toc('unhighlight','".$current_item."');");
  416. $objResponse->addScript("update_toc('highlight','".$new_item_id."');");
  417. $objResponse->addScript("update_toc('$mylesson_status','".$new_item_id."');");
  418. $objResponse->addScript("update_progress_bar('$mycomplete','$mytotal','$myprogress_mode');");
  419. $mylp->set_error_msg('');
  420. $mylp->prerequisites_match(); //check the prerequisites are all complete
  421. if($debug>1){error_log('Prereq_match() returned '.htmlentities($mylp->error),0);}
  422. $objResponse->addScript("update_message_frame('".str_replace("'","\'",htmlentities($mylp->error))."');");
  423. $_SESSION['scorm_item_id'] = $new_item_id;//Save the new item ID for the exercise tool to use
  424. $_SESSION['lpobject'] = serialize($mylp);
  425. return $objResponse;
  426. }
  427. /**
  428. * Start a timer and hand it back to the JS by assigning the current time (of start) to
  429. * var asset_timer
  430. */
  431. function start_timer()
  432. {
  433. $objResponse = new xajaxResponse();
  434. $time = time();
  435. $objResponse->addScript("asset_timer='$time';asset_timer_total=0;");
  436. return $objResponse;
  437. }
  438. require('lp_comm.common.php');
  439. $xajax->processRequests();
  440. ?>