lp_ajax_switch_item.php 8.9 KB

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