lp_comm.server.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. $debug=0;
  64. 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);}
  65. $objResponse = new xajaxResponse();
  66. require_once('learnpath.class.php');
  67. require_once('scorm.class.php');
  68. require_once('aicc.class.php');
  69. require_once('learnpathItem.class.php');
  70. require_once('scormItem.class.php');
  71. require_once('aiccItem.class.php');
  72. $mylp = '';
  73. if(isset($_SESSION['lpobject']))
  74. {
  75. if($debug>1){error_log('////$_SESSION[lpobject] is set',0);}
  76. $oLP =& unserialize($_SESSION['lpobject']);
  77. if(!is_object($oLP)){
  78. if($debug>2){error_log(print_r($oLP,true),0);}
  79. if($debug>2){error_log('////Building new lp',0);}
  80. unset($oLP);
  81. $code = api_get_course_id();
  82. $mylp = & new learnpath($code,$lp_id,$user_id);
  83. }else{
  84. if($debug>2){error_log('////Reusing session lp',0);}
  85. $mylp = & $oLP;
  86. }
  87. }
  88. //$objResponse->addAlert(api_get_path(REL_CLARO_PATH).'newscorm/learnpathItem.class.php');
  89. $prereq_check = $mylp->prerequisites_match($item_id);
  90. if($prereq_check === true) //launch the prerequisites check and set error if needed
  91. {
  92. $mylpi =& $mylp->items[$item_id];
  93. //$mylpi->set_lp_view($view_id);
  94. if($score!=-1){
  95. $mylpi->set_score($score);
  96. }
  97. if($max!=-1){
  98. $mylpi->max_score=$max;
  99. }
  100. if($min!=-1){
  101. $mylpi->min_score=$min;
  102. }
  103. if($status!='')
  104. {
  105. if($debug>1){error_log('Calling set_status('.$status.') from xajax',0);}
  106. $mylpi->set_status($status);
  107. if($debug>1){error_log('Done calling set_status from xajax',0);}
  108. }
  109. if($time!='')
  110. {
  111. //if big integer, then it's a timestamp, otherwise it's normal scorm time
  112. if($time == intval(strval($time)) && $time>1000000){
  113. $real_time = time() - $time;
  114. //$real_time += $mylpi->get_total_time();
  115. $mylpi->set_time($real_time,'int');
  116. }else{
  117. $mylpi->set_time($time);
  118. }
  119. }
  120. if($suspend!='')
  121. {
  122. $mylpi->current_data = $suspend;//escapetxt($suspend);
  123. }
  124. if($location!='')
  125. {
  126. $mylpi->set_lesson_location($location);
  127. }
  128. //deal with interactions provided in arrays in the following format
  129. //id(0), type(1), time(2), weighting(3),correct_responses(4),student_response(5),result(6),latency(7)
  130. if(is_array($interactions) && count($interactions)>0){
  131. foreach($interactions as $index=>$interaction){
  132. $mylpi->add_interaction($index,$interactions[$index]);
  133. }
  134. }
  135. $mylpi->set_core_exit($core_exit);
  136. $mylp->save_item($item_id,false);
  137. }
  138. $mytotal = $mylp->get_total_items_count_without_chapters();
  139. $mycomplete = $mylp->get_complete_items_count();
  140. $myprogress_mode = $mylp->get_progress_bar_mode();
  141. $myprogress_mode = ($myprogress_mode==''?'%':$myprogress_mode);
  142. //$mylpi->write_to_db();
  143. $_SESSION['lpobject'] = serialize($mylp);
  144. $objResponse->addScript("update_toc('".$status."','".$item_id."');");
  145. $update_list = $mylp->get_update_queue();
  146. foreach($update_list as $my_upd_id => $my_upd_status)
  147. {
  148. 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
  149. $objResponse->addScript("update_toc('".$my_upd_status."','".$my_upd_id."');");
  150. }
  151. }
  152. $objResponse->addScript("update_progress_bar('$mycomplete','$mytotal','$myprogress_mode');");
  153. if($debug>0){
  154. $objResponse->addScript("logit_lms('Saved data for item ".$item_id.", user ".$user_id." (status=".$status.")',2)");
  155. if($debug>1){error_log('End of xajax_save_item()',0);}
  156. }
  157. return $objResponse;
  158. }
  159. /**
  160. * Get one item's details
  161. * @param integer LP ID
  162. * @param integer user ID
  163. * @param integer View ID
  164. * @param integer Current item ID
  165. * @param integer New item ID
  166. */
  167. function switch_item_details($lp_id,$user_id,$view_id,$current_item,$next_item)
  168. {
  169. $debug=0;
  170. if($debug>0){error_log('In xajax_switch_item_details('.$lp_id.','.$user_id.','.$view_id.','.$current_item.','.$next_item.')',0);}
  171. $objResponse = new xajaxResponse();
  172. /*$item_id may be one of:
  173. * -'next'
  174. * -'previous'
  175. * -'first'
  176. * -'last'
  177. * - a real item ID
  178. */
  179. require_once('learnpath.class.php');
  180. require_once('scorm.class.php');
  181. require_once('aicc.class.php');
  182. require_once('learnpathItem.class.php');
  183. require_once('scormItem.class.php');
  184. require_once('aiccItem.class.php');
  185. $mylp = '';
  186. if(isset($_SESSION['lpobject']))
  187. {
  188. if($debug>1){error_log('////$_SESSION[lpobject] is set',0);}
  189. $oLP =& unserialize($_SESSION['lpobject']);
  190. if(!is_object($oLP)){
  191. if($debug>1){error_log(print_r($oLP,true),0);}
  192. if($debug>2){error_log('////Building new lp',0);}
  193. unset($oLP);
  194. $code = api_get_course_id();
  195. $mylp = & new learnpath($code,$lp_id,$user_id);
  196. }else{
  197. if($debug>1){error_log('////Reusing session lp',0);}
  198. $mylp = & $oLP;
  199. }
  200. }
  201. $new_item_id = 0;
  202. switch($next_item){
  203. case 'next':
  204. $mylp->set_current_item($current_item);
  205. $mylp->next();
  206. $new_item_id = $mylp->get_current_item_id();
  207. if($debug>1){error_log('In {next} - next item is '.$new_item_id.'(current: '.$current_item.')',0);}
  208. break;
  209. case 'previous':
  210. $mylp->set_current_item($current_item);
  211. $mylp->previous();
  212. $new_item_id = $mylp->get_current_item_id();
  213. if($debug>1){error_log('In {previous} - next item is '.$new_item_id.'(current: '.$current_item.')',0);}
  214. break;
  215. case 'first':
  216. $mylp->set_current_item($current_item);
  217. $mylp->first();
  218. $new_item_id = $mylp->get_current_item_id();
  219. if($debug>1){error_log('In {first} - next item is '.$new_item_id.'(current: '.$current_item.')',0);}
  220. break;
  221. case 'last':
  222. break;
  223. default:
  224. //should be filtered to check it's not hacked
  225. if($next_item == $current_item){
  226. //if we're opening the same item again
  227. $mylp->items[$current_item]->restart();
  228. }
  229. $new_item_id = $next_item;
  230. $mylp->set_current_item($new_item_id);
  231. if($debug>1){error_log('In {default} - next item is '.$new_item_id.'(current: '.$current_item.')',0);}
  232. break;
  233. }
  234. $mylp->start_current_item(true);
  235. if($mylp->force_commit){
  236. $mylp->save_current();
  237. }
  238. //$objResponse->addAlert(api_get_path(REL_CLARO_PATH).'newscorm/learnpathItem.class.php');
  239. if(is_object($mylp->items[$new_item_id])){
  240. $mylpi = & $mylp->items[$new_item_id];
  241. }else{
  242. if($debug>1){error_log('In switch_item_details - generating new item object',0);}
  243. $mylpi =& new learnpathItem($new_item_id,$user_id);
  244. $mylpi->set_lp_view($view_id);
  245. }
  246. /*
  247. * now get what's needed by the SCORM API:
  248. * -score
  249. * -max
  250. * -min
  251. * -lesson_status
  252. * -session_time
  253. * -suspend_data
  254. */
  255. $myscore = $mylpi->get_score();
  256. $mymax = $mylpi->get_max();
  257. $mymin = $mylpi->get_min();
  258. $mylesson_status = $mylpi->get_status();
  259. $mylesson_location = $mylpi->get_lesson_location();
  260. $mytotal_time = $mylpi->get_scorm_time('js');
  261. $mymastery_score = $mylpi->get_mastery_score();
  262. $mymax_time_allowed = $mylpi->get_max_time_allowed();
  263. $mylaunch_data = $mylpi->get_launch_data();
  264. /*
  265. if($mylpi->get_type() == 'asset'){
  266. //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)
  267. $mylesson_status = 'completed';
  268. $mylpi->set_status('completed');
  269. $mylpi->save();
  270. }
  271. */
  272. $mysession_time = $mylpi->get_total_time();
  273. $mysuspend_data = $mylpi->get_suspend_data();
  274. $mylesson_location = $mylpi->get_lesson_location();
  275. $objResponse->addScript(
  276. "score=".$myscore.";" .
  277. "max=".$mymax.";" .
  278. "min=".$mymin.";" .
  279. "lesson_status='".$mylesson_status."';" .
  280. "lesson_location='".$mylesson_location."';" .
  281. "session_time='".$mysession_time."';" .
  282. "suspend_data='".$mysuspend_data."';" .
  283. "lesson_location='".$mylesson_location."';" .
  284. "total_time = '".$mytotal_time."';" .
  285. "mastery_score = '".$mymastery_score."';" .
  286. "max_time_allowed = '".$mymax_time_allowed."';" .
  287. "launch_data = '".$mylaunch_data."';" .
  288. "interactions = new Array();" .
  289. "G_lastError = 0;" .
  290. "G_LastErrorMessage = 'No error';");
  291. /*
  292. * and re-initialise the rest
  293. * -saved_lesson_status = 'not attempted'
  294. * -lms_lp_id
  295. * -lms_item_id
  296. * -lms_old_item_id
  297. * -lms_new_item_id
  298. * -lms_been_synchronized
  299. * -lms_initialized
  300. * -lms_total_lessons
  301. * -lms_complete_lessons
  302. * -lms_progress_bar_mode
  303. * -lms_view_id
  304. * -lms_user_id
  305. */
  306. $mytotal = $mylp->get_total_items_count_without_chapters();
  307. $mycomplete = $mylp->get_complete_items_count();
  308. $myprogress_mode = $mylp->get_progress_bar_mode();
  309. $myprogress_mode = ($myprogress_mode==''?'%':$myprogress_mode);
  310. $mynext = $mylp->get_next_item_id();
  311. $myprevious = $mylp->get_previous_item_id();
  312. $myitemtype = $mylpi->get_type();
  313. $mylesson_mode = $mylpi->get_lesson_mode();
  314. $mycredit = $mylpi->get_credit();
  315. $mylaunch_data = $mylpi->get_launch_data();
  316. $myinteractions_count = $mylpi->get_interactions_count();
  317. $mycore_exit = $mylpi->get_core_exit();
  318. $objResponse->addScript(
  319. "saved_lesson_status='not attempted';" .
  320. "lms_lp_id=".$lp_id.";" .
  321. "lms_item_id=".$new_item_id.";" .
  322. "lms_old_item_id=0;" .
  323. "lms_been_synchronized=0;" .
  324. "lms_initialized=0;" .
  325. "lms_total_lessons=".$mytotal.";" .
  326. "lms_complete_lessons=".$mycomplete.";" .
  327. "lms_progress_bar_mod='".$myprogress_mode."';" .
  328. "lms_view_id=".$view_id.";" .
  329. "lms_user_id=".$user_id.";" .
  330. "next_item=".$new_item_id.";" . //this one is very important to replace possible literal strings
  331. "lms_next_item=".$mynext.";" .
  332. "lms_previous_item=".$myprevious.";" .
  333. "lms_item_type = '".$myitemtype."';" .
  334. "lms_item_credit = '".$mycredit."';" .
  335. "lms_item_lesson_mode = '".$mylesson_mode."';" .
  336. "lms_item_launch_data = '".$mylaunch_data."';" .
  337. "lms_item_interactions_count = '".$myinteractions_count."';" .
  338. "lms_item_core_exit = '".$mycore_exit."';" .
  339. "asset_timer = 0;"
  340. );
  341. $objResponse->addScript("update_toc('unhighlight','".$current_item."');");
  342. $objResponse->addScript("update_toc('highlight','".$new_item_id."');");
  343. $objResponse->addScript("update_toc('$mylesson_status','".$new_item_id."');");
  344. $objResponse->addScript("update_progress_bar('$mycomplete','$mytotal','$myprogress_mode');");
  345. $mylp->set_error_msg('');
  346. $mylp->prerequisites_match(); //check the prerequisites are all complete
  347. if($debug>1){error_log('Prereq_match() returned '.htmlentities($mylp->error),0);}
  348. $objResponse->addScript("update_message_frame('".str_replace("'","\'",htmlentities($mylp->error))."');");
  349. $_SESSION['scorm_item_id'] = $new_item_id;//Save the new item ID for the exercise tool to use
  350. $_SESSION['lpobject'] = serialize($mylp);
  351. return $objResponse;
  352. }
  353. /**
  354. * Start a timer and hand it back to the JS by assigning the current time (of start) to
  355. * var asset_timer
  356. */
  357. function start_timer()
  358. {
  359. $objResponse = new xajaxResponse();
  360. $time = time();
  361. $objResponse->addScript("asset_timer='$time';asset_timer_total=0;");
  362. return $objResponse;
  363. }
  364. require('lp_comm.common.php');
  365. $xajax->processRequests();
  366. ?>