lp_ajax_switch_item.php 9.5 KB

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