lp_ajax_switch_item.php 9.3 KB

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