lp_ajax_switch_item.php 8.7 KB

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