lp_ajax_switch_item.php 8.9 KB

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