lp_ajax_switch_item.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. *
  9. * @package chamilo.learnpath
  10. *
  11. * @author Yannick Warnier <ywarnier@beeznest.org>
  12. */
  13. // Flag to allow for anonymous user - needs to be set before global.inc.php
  14. $use_anonymous = true;
  15. require_once __DIR__.'/../inc/global.inc.php';
  16. /**
  17. * Get one item's details.
  18. *
  19. * @param int LP ID
  20. * @param int user ID
  21. * @param int View ID
  22. * @param int Current item ID
  23. * @param int New item ID
  24. */
  25. function switch_item_details($lp_id, $user_id, $view_id, $current_item, $next_item)
  26. {
  27. $debug = 0;
  28. $return = '';
  29. if ($debug > 0) {
  30. error_log(
  31. 'In xajax_switch_item_details('.$lp_id.','.$user_id.','.$view_id.','.$current_item.','.$next_item.')',
  32. 0
  33. );
  34. }
  35. //$objResponse = new xajaxResponse();
  36. /*$item_id may be one of:
  37. * -'next'
  38. * -'previous'
  39. * -'first'
  40. * -'last'
  41. * - a real item ID
  42. */
  43. $mylp = learnpath::getLpFromSession(api_get_course_id(), $lp_id, $user_id);
  44. $new_item_id = 0;
  45. switch ($next_item) {
  46. case 'next':
  47. $mylp->set_current_item($current_item);
  48. $mylp->next();
  49. $new_item_id = $mylp->get_current_item_id();
  50. if ($debug > 1) {
  51. error_log('In {next} - next item is '.$new_item_id.'(current: '.$current_item.')');
  52. }
  53. break;
  54. case 'previous':
  55. $mylp->set_current_item($current_item);
  56. $mylp->previous();
  57. $new_item_id = $mylp->get_current_item_id();
  58. if ($debug > 1) {
  59. error_log('In {previous} - next item is '.$new_item_id.'(current: '.$current_item.')');
  60. }
  61. break;
  62. case 'first':
  63. $mylp->set_current_item($current_item);
  64. $mylp->first();
  65. $new_item_id = $mylp->get_current_item_id();
  66. if ($debug > 1) {
  67. error_log('In {first} - next item is '.$new_item_id.'(current: '.$current_item.')');
  68. }
  69. break;
  70. case 'last':
  71. break;
  72. default:
  73. // Should be filtered to check it's not hacked.
  74. if ($next_item == $current_item) {
  75. // If we're opening the same item again.
  76. $mylp->items[$current_item]->restart();
  77. }
  78. $new_item_id = $next_item;
  79. $mylp->set_current_item($new_item_id);
  80. if ($debug > 1) {
  81. error_log('In {default} - next item is '.$new_item_id.'(current: '.$current_item.')');
  82. }
  83. break;
  84. }
  85. $mylp->start_current_item(true);
  86. if ($mylp->force_commit) {
  87. $mylp->save_current();
  88. }
  89. if (is_object($mylp->items[$new_item_id])) {
  90. $mylpi = $mylp->items[$new_item_id];
  91. } else {
  92. if ($debug > 1) {
  93. error_log('In switch_item_details - generating new item object', 0);
  94. }
  95. $mylpi = new learnpathItem($new_item_id, $user_id);
  96. $mylpi->set_lp_view($view_id);
  97. }
  98. /*
  99. * now get what's needed by the SCORM API:
  100. * -score
  101. * -max
  102. * -min
  103. * -lesson_status
  104. * -session_time
  105. * -suspend_data
  106. */
  107. $myscore = $mylpi->get_score();
  108. $mymax = $mylpi->get_max();
  109. if ($mymax === '') {
  110. $mymax = "''";
  111. }
  112. $mymin = $mylpi->get_min();
  113. $mylesson_status = $mylpi->get_status();
  114. $mylesson_location = $mylpi->get_lesson_location();
  115. $mytotal_time = $mylpi->get_scorm_time('js');
  116. $mymastery_score = $mylpi->get_mastery_score();
  117. $mymax_time_allowed = $mylpi->get_max_time_allowed();
  118. $mylaunch_data = $mylpi->get_launch_data();
  119. /*
  120. if ($mylpi->get_type() == 'asset') {
  121. // Temporary measure to save completion of an asset. Later on,
  122. // Chamilo should trigger something on unload, maybe...
  123. // (even though that would mean the last item cannot be completed)
  124. $mylesson_status = 'completed';
  125. $mylpi->set_status('completed');
  126. $mylpi->save();
  127. }
  128. */
  129. $mysession_time = $mylpi->get_total_time();
  130. $mysuspend_data = $mylpi->get_suspend_data();
  131. $mylesson_location = $mylpi->get_lesson_location();
  132. $myic = $mylpi->get_interactions_count();
  133. $myistring = '';
  134. for ($i = 0; $i < $myic; $i++) {
  135. $myistring .= ",[".$i.",'','','','','','','']";
  136. }
  137. if (!empty($myistring)) {
  138. $myistring = substr($myistring, 1);
  139. }
  140. /*
  141. * The following lines should reinitialize the values for the SCO
  142. * However, due to many complications, we are now relying more on the
  143. * LMSInitialize() call and its underlying lp_ajax_initialize.php call
  144. * so this code is technically deprecated (but the change of item_id should
  145. * remain). However, due to numerous technical issues with SCORM, we prefer
  146. * leaving it as a double-lock security. If removing, please test carefully
  147. * with both SCORM and proper learning path tracking.
  148. */
  149. $return .=
  150. "olms.score=".$myscore.";".
  151. "olms.max=".$mymax.";".
  152. "olms.min=".$mymin.";".
  153. "olms.lesson_status='".$mylesson_status."';".
  154. "olms.lesson_location='".$mylesson_location."';".
  155. "olms.session_time='".$mysession_time."';".
  156. "olms.suspend_data='".$mysuspend_data."';".
  157. "olms.total_time = '".$mytotal_time."';".
  158. "olms.mastery_score = '".$mymastery_score."';".
  159. "olms.max_time_allowed = '".$mymax_time_allowed."';".
  160. "olms.launch_data = '".$mylaunch_data."';".
  161. "olms.interactions = new Array(".$myistring.");".
  162. "olms.item_objectives = new Array();".
  163. "olms.G_lastError = 0;".
  164. "olms.G_LastErrorMessage = 'No error';".
  165. "olms.finishSignalReceived = 0;";
  166. /*
  167. * and re-initialise the rest
  168. * -lms_lp_id
  169. * -lms_item_id
  170. * -lms_old_item_id
  171. * -lms_new_item_id
  172. * -lms_initialized
  173. * -lms_progress_bar_mode
  174. * -lms_view_id
  175. * -lms_user_id
  176. */
  177. $mytotal = $mylp->getTotalItemsCountWithoutDirs();
  178. $mycomplete = $mylp->get_complete_items_count();
  179. $myprogress_mode = $mylp->get_progress_bar_mode();
  180. $myprogress_mode = ($myprogress_mode == '' ? '%' : $myprogress_mode);
  181. $mynext = $mylp->get_next_item_id();
  182. $myprevious = $mylp->get_previous_item_id();
  183. $myitemtype = $mylpi->get_type();
  184. $mylesson_mode = $mylpi->get_lesson_mode();
  185. $mycredit = $mylpi->get_credit();
  186. $mylaunch_data = $mylpi->get_launch_data();
  187. $myinteractions_count = $mylpi->get_interactions_count();
  188. //$myobjectives_count = $mylpi->get_objectives_count();
  189. $mycore_exit = $mylpi->get_core_exit();
  190. $return .=
  191. //"saved_lesson_status='not attempted';" .
  192. "olms.lms_lp_id=".$lp_id.";".
  193. "olms.lms_item_id=".$new_item_id.";".
  194. "olms.lms_old_item_id=0;".
  195. //"lms_been_synchronized=0;" .
  196. "olms.lms_initialized=0;".
  197. //"lms_total_lessons=".$mytotal.";" .
  198. //"lms_complete_lessons=".$mycomplete.";" .
  199. //"lms_progress_bar_mode='".$myprogress_mode."';" .
  200. "olms.lms_view_id=".$view_id.";".
  201. "olms.lms_user_id=".$user_id.";".
  202. "olms.next_item=".$new_item_id.";".// This one is very important to replace possible literal strings.
  203. "olms.lms_next_item=".$mynext.";".
  204. "olms.lms_previous_item=".$myprevious.";".
  205. "olms.lms_item_type = '".$myitemtype."';".
  206. "olms.lms_item_credit = '".$mycredit."';".
  207. "olms.lms_item_lesson_mode = '".$mylesson_mode."';".
  208. "olms.lms_item_launch_data = '".$mylaunch_data."';".
  209. "olms.lms_item_interactions_count = '".$myinteractions_count."';".
  210. "olms.lms_item_objectives_count = '".$myinteractions_count."';".
  211. "olms.lms_item_core_exit = '".$mycore_exit."';".
  212. "olms.asset_timer = 0;";
  213. $updateMinTime = '';
  214. if (Tracking::minimumTimeAvailable(api_get_session_id(), api_get_course_int_id())) {
  215. $timeLp = $mylp->getAccumulateWorkTime();
  216. $timeTotalCourse = $mylp->getAccumulateWorkTimeTotalCourse();
  217. // Minimum connection percentage
  218. $perc = 100;
  219. // Time from the course
  220. $tc = $timeTotalCourse;
  221. $sessionId = api_get_session_id();
  222. if (!empty($sessionId) && $sessionId != 0) {
  223. /*$sql = "SELECT hours, perc FROM plugin_licences_course_session WHERE session_id = $sessionId";
  224. $res = Database::query($sql);
  225. if (Database::num_rows($res) > 0) {
  226. $aux = Database::fetch_assoc($res);
  227. $perc = $aux['perc'];
  228. $tc = $aux['hours'] * 60;
  229. }*/
  230. }
  231. // Percentage of the learning paths
  232. $pl = 0;
  233. if (!empty($timeTotalCourse)) {
  234. $pl = $timeLp / $timeTotalCourse;
  235. }
  236. // Minimum time for each learning path
  237. $time_total = intval($pl * $tc * $perc / 100) * 60;
  238. //$time_total = $mylp->getAccumulateWorkTime() * 60;
  239. /*$lpTime = Tracking::get_time_spent_in_lp(
  240. $user_id,
  241. api_get_course_id(),
  242. [$lp_id],
  243. api_get_session_id()
  244. );*/
  245. $lpTimeList = Tracking::getCalculateTime($user_id, api_get_course_int_id(), api_get_session_id());
  246. $lpTime = isset($lpTimeList[TOOL_LEARNPATH][$lp_id]) ? $lpTimeList[TOOL_LEARNPATH][$lp_id] : 0;
  247. if ($lpTime >= $time_total) {
  248. $time_spent = $time_total;
  249. } else {
  250. $time_spent = $lpTime;
  251. }
  252. $hour = (intval($lpTime / 3600)) < 10 ? '0'.intval($lpTime / 3600) : intval($lpTime / 3600);
  253. $minute = date('i', $lpTime);
  254. $second = date('s', $lpTime);
  255. $updateMinTime = "update_time_bar('$time_spent','$time_total','%');".
  256. "update_chronometer('$hour','$minute','$second');";
  257. }
  258. $return .=
  259. "update_toc('unhighlight','".$current_item."');".
  260. "update_toc('highlight','".$new_item_id."');".
  261. "update_toc('$mylesson_status','".$new_item_id."');".
  262. "update_progress_bar('$mycomplete','$mytotal','$myprogress_mode');".
  263. $updateMinTime
  264. ;
  265. $return .= 'updateGamificationValues(); ';
  266. // Ofaj
  267. $return .= "$('#item-parent-names').load('"
  268. .api_get_path(WEB_AJAX_PATH)
  269. ."lp.ajax.php?a=get_parent_names&new_item=$new_item_id');";
  270. $mylp->set_error_msg('');
  271. $mylp->prerequisites_match(); // Check the prerequisites are all complete.
  272. if ($debug > 1) {
  273. error_log('Prereq_match() returned '.htmlentities($mylp->error), 0);
  274. }
  275. // Save the new item ID for the exercise tool to use.
  276. Session::write('scorm_item_id', $new_item_id);
  277. Session::write('lpobject', serialize($mylp));
  278. Session::write('oLP', $mylp);
  279. return $return;
  280. }
  281. echo switch_item_details(
  282. $_REQUEST['lid'],
  283. $_REQUEST['uid'],
  284. $_REQUEST['vid'],
  285. $_REQUEST['iid'],
  286. $_REQUEST['next']
  287. );