lp_ajax_switch_item.php 8.8 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. //$objResponse->addAlert(api_get_path(REL_CODE_PATH).'newscorm/learnpathItem.class.php');
  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->get_total_items_count_without_chapters();
  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. $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. );