lp_ajax_switch_item.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This script contains the server part of the xajax interaction process. The client part is located
  5. * in lp_api.php or other api's.
  6. * This is a first attempt at using xajax and AJAX in general, so the code might be a bit unsettling.
  7. * @package chamilo.learnpath
  8. * @author Yannick Warnier <ywarnier@beeznest.org>
  9. */
  10. /**
  11. * Code
  12. */
  13. // Flag to allow for anonymous user - needs to be set before global.inc.php
  14. $use_anonymous = true;
  15. // Name of the language file that needs to be included
  16. $language_file[] = 'learnpath';
  17. //require_once '../inc/global.inc.php';
  18. $app['template.show_footer'] = false;
  19. $app['template.show_header'] = false;
  20. $app['default_layout'] = 'default/layout/blank.tpl';
  21. require_once 'learnpath.class.php';
  22. require_once 'scorm.class.php';
  23. require_once 'aicc.class.php';
  24. require_once 'learnpathItem.class.php';
  25. require_once 'scormItem.class.php';
  26. require_once 'aiccItem.class.php';
  27. /**
  28. * Get one item's details
  29. * @param integer LP ID
  30. * @param integer user ID
  31. * @param integer View ID
  32. * @param integer Current item ID
  33. * @param integer New item ID
  34. */
  35. function switch_item_details($lp_id, $user_id, $view_id, $current_item, $next_item)
  36. {
  37. $debug = 0;
  38. $return = '';
  39. if ($debug > 0) {
  40. error_log('In xajax_switch_item_details('.$lp_id.','.$user_id.','.$view_id.','.$current_item.','.$next_item.')', 0);
  41. }
  42. //$objResponse = new xajaxResponse();
  43. /* $item_id may be one of:
  44. * -'next'
  45. * -'previous'
  46. * -'first'
  47. * -'last'
  48. * - a real item ID
  49. */
  50. $mylp = '';
  51. if (isset($_SESSION['lpobject'])) {
  52. if ($debug > 1) {
  53. error_log('$_SESSION[lpobject] is set', 0);
  54. }
  55. $oLP = unserialize($_SESSION['lpobject']);
  56. if (!is_object($oLP)) {
  57. if ($debug > 1) {
  58. error_log(print_r($oLP, true), 0);
  59. }
  60. if ($debug > 2) {
  61. error_log('Building new lp', 0);
  62. }
  63. unset($oLP);
  64. $code = api_get_course_id();
  65. $mylp = new learnpath($code, $lp_id, $user_id);
  66. } else {
  67. if ($debug > 1) {
  68. error_log('Reusing session lp', 0);
  69. }
  70. $mylp = $oLP;
  71. }
  72. } else {
  73. if ($debug > 1) {
  74. error_log('$_SESSION[lpobject] is NOT set', 0);
  75. }
  76. }
  77. $check_attempts = $mylp->check_item_attempts($next_item);
  78. $new_item_id = 0;
  79. switch ($next_item) {
  80. case 'next':
  81. $mylp->set_current_item($current_item);
  82. $mylp->next();
  83. $new_item_id = $mylp->get_current_item_id();
  84. if ($debug > 1) {
  85. error_log('In {next} - next item is '.$new_item_id.'(current: '.$current_item.')', 0);
  86. }
  87. break;
  88. case 'previous':
  89. $mylp->set_current_item($current_item);
  90. $mylp->previous();
  91. $new_item_id = $mylp->get_current_item_id();
  92. if ($debug > 1) {
  93. error_log('In {previous} - next item is '.$new_item_id.'(current: '.$current_item.')', 0);
  94. }
  95. break;
  96. case 'first':
  97. $mylp->set_current_item($current_item);
  98. $mylp->first();
  99. $new_item_id = $mylp->get_current_item_id();
  100. if ($debug > 1) {
  101. error_log('In {first} - next item is '.$new_item_id.'(current: '.$current_item.')', 0);
  102. }
  103. break;
  104. case 'last':
  105. break;
  106. default:
  107. // Should be filtered to check it's not hacked.
  108. if ($next_item == $current_item) {
  109. // If we're opening the same item again.
  110. $mylp->items[$current_item]->restart();
  111. }
  112. $new_item_id = $next_item;
  113. $mylp->set_current_item($new_item_id);
  114. if ($debug > 1) {
  115. error_log('In {default} - next item is '.$new_item_id.'(current: '.$current_item.')', 0);
  116. }
  117. break;
  118. }
  119. if ($check_attempts) {
  120. $mylp->start_current_item(true);
  121. }
  122. if ($check_attempts == true && $mylp->force_commit) {
  123. $mylp->save_current();
  124. }
  125. if (is_object($mylp->items[$new_item_id])) {
  126. $mylpi = $mylp->items[$new_item_id];
  127. } else {
  128. if ($debug > 1) {
  129. error_log('In switch_item_details - generating new item object', 0);
  130. }
  131. $mylpi = new learnpathItem($new_item_id, $user_id);
  132. $mylpi->set_lp_view($view_id);
  133. }
  134. /*
  135. * now get what's needed by the SCORM API:
  136. * -score
  137. * -max
  138. * -min
  139. * -lesson_status
  140. * -session_time
  141. * -suspend_data
  142. */
  143. $myscore = $mylpi->get_score();
  144. $mymax = $mylpi->get_max();
  145. if ($mymax === '') {
  146. $mymax = "''";
  147. }
  148. $mymin = $mylpi->get_min();
  149. $mylesson_status = $mylpi->get_status();
  150. $mylesson_location = $mylpi->get_lesson_location();
  151. $mytotal_time = $mylpi->get_scorm_time('js');
  152. $mymastery_score = $mylpi->get_mastery_score();
  153. $mymax_time_allowed = $mylpi->get_max_time_allowed();
  154. $mylaunch_data = $mylpi->get_launch_data();
  155. $mysession_time = $mylpi->get_total_time();
  156. $mysuspend_data = $mylpi->get_suspend_data();
  157. $myic = $mylpi->get_interactions_count();
  158. $myistring = '';
  159. for ($i = 0; $i < $myic; $i++) {
  160. $myistring .= ",[".$i.",'','','','','','','']";
  161. }
  162. if (!empty($myistring)) {
  163. $myistring = substr($myistring, 1);
  164. }
  165. /*
  166. * The following lines should reinitialize the values for the SCO
  167. * However, due to many complications, we are now relying more on the
  168. * LMSInitialize() call and its underlying lp_ajax_initialize.php call
  169. * so this code is technically deprecated (but the change of item_id should
  170. * remain). However, due to numerous technical issues with SCORM, we prefer
  171. * leaving it as a double-lock security. If removing, please test carefully
  172. * with both SCORM and proper learning path tracking.
  173. */
  174. $return .=
  175. "olms.score=".$myscore.";".
  176. "olms.max=".$mymax.";".
  177. "olms.min=".$mymin.";".
  178. "olms.lesson_status='".$mylesson_status."';".
  179. "olms.lesson_location='".$mylesson_location."';".
  180. "olms.session_time='".$mysession_time."';".
  181. "olms.suspend_data='".$mysuspend_data."';".
  182. "olms.total_time = '".$mytotal_time."';".
  183. "olms.mastery_score = '".$mymastery_score."';".
  184. "olms.max_time_allowed = '".$mymax_time_allowed."';".
  185. "olms.launch_data = '".$mylaunch_data."';".
  186. "olms.interactions = new Array(".$myistring.");".
  187. "olms.item_objectives = new Array();".
  188. "olms.G_lastError = 0;".
  189. "olms.G_LastErrorMessage = 'No error';";
  190. /*
  191. * and re-initialise the rest
  192. * -lms_lp_id
  193. * -lms_item_id
  194. * -lms_old_item_id
  195. * -lms_new_item_id
  196. * -lms_initialized
  197. * -lms_progress_bar_mode
  198. * -lms_view_id
  199. * -lms_user_id
  200. */
  201. $mytotal = $mylp->get_total_items_count_without_chapters();
  202. $mycomplete = $mylp->get_complete_items_count();
  203. $myprogress_mode = $mylp->get_progress_bar_mode();
  204. $myprogress_mode = ($myprogress_mode == '' ? '%' : $myprogress_mode);
  205. $mynext = $mylp->get_next_item_id();
  206. $myprevious = $mylp->get_previous_item_id();
  207. $myitemtype = $mylpi->get_type();
  208. $mylesson_mode = $mylpi->get_lesson_mode();
  209. $mycredit = $mylpi->get_credit();
  210. $mylaunch_data = $mylpi->get_launch_data();
  211. $myinteractions_count = $mylpi->get_interactions_count();
  212. $myobjectives_count = $mylpi->get_objectives_count();
  213. $mycore_exit = $mylpi->get_core_exit();
  214. $return .=
  215. //"saved_lesson_status='not attempted';" .
  216. "olms.lms_lp_id=".$lp_id.";".
  217. "olms.lms_item_id=".$new_item_id.";".
  218. "olms.lms_old_item_id=0;".
  219. //"lms_been_synchronized=0;" .
  220. "olms.lms_initialized=0;".
  221. //"lms_total_lessons=".$mytotal.";" .
  222. //"lms_complete_lessons=".$mycomplete.";" .
  223. //"lms_progress_bar_mode='".$myprogress_mode."';" .
  224. "olms.lms_view_id=".$view_id.";".
  225. "olms.lms_user_id=".$user_id.";".
  226. "olms.next_item=".$new_item_id.";".// This one is very important to replace possible literal strings.
  227. "olms.lms_next_item=".$mynext.";".
  228. "olms.lms_previous_item=".$myprevious.";".
  229. "olms.lms_item_type = '".$myitemtype."';".
  230. "olms.lms_item_credit = '".$mycredit."';".
  231. "olms.lms_item_lesson_mode = '".$mylesson_mode."';".
  232. "olms.lms_item_launch_data = '".$mylaunch_data."';".
  233. "olms.lms_item_interactions_count = '".$myinteractions_count."';".
  234. "olms.lms_item_objectives_count = '".$myinteractions_count."';".
  235. "olms.lms_item_core_exit = '".$mycore_exit."';".
  236. "olms.asset_timer = 0;";
  237. $return .= "update_toc('unhighlight','".$current_item."');".
  238. "update_toc('highlight','".$new_item_id."');".
  239. "update_toc('$mylesson_status','".$new_item_id."');".
  240. "update_progress_bar('$mycomplete','$mytotal','$myprogress_mode');";
  241. $mylp->set_error_msg('');
  242. $mylp->prerequisites_match(); // Check the prerequisites are all complete.
  243. if ($debug > 1) {
  244. error_log('Prereq_match() returned '.htmlentities($mylp->error), 0);
  245. error_log($return);
  246. }
  247. $_SESSION['scorm_item_id'] = $new_item_id; // Save the new item ID for the exercise tool to use.
  248. $_SESSION['lpobject'] = serialize($mylp);
  249. return $return;
  250. }
  251. echo switch_item_details($_REQUEST['lid'], $_REQUEST['uid'], $_REQUEST['vid'], $_REQUEST['iid'], $_REQUEST['next']);
  252. exit;