lp_ajax_initialize.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This script contains the server part of the xajax interaction process.
  5. * This script, in particular, enables the process of SCO's initialization. It
  6. * resets the JavaScript values for each SCO to the current LMS status.
  7. *
  8. * @package chamilo.learnpath
  9. *
  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 __DIR__.'/../inc/global.inc.php';
  15. api_protect_course_script();
  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. * @return string
  26. */
  27. function initialize_item($lp_id, $user_id, $view_id, $next_item)
  28. {
  29. $debug = 0;
  30. $return = '';
  31. if ($debug) {
  32. error_log('In initialize_item('.$lp_id.','.$user_id.','.$view_id.','.$next_item.')');
  33. }
  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. $mylp->set_current_item($next_item);
  43. if ($debug) {
  44. error_log('In initialize_item() - new item is '.$next_item);
  45. }
  46. $mylp->start_current_item(true);
  47. if (is_object($mylp->items[$next_item])) {
  48. if ($debug) {
  49. error_log('In initialize_item - recovering existing item object '.$next_item, 0);
  50. }
  51. $mylpi = $mylp->items[$next_item];
  52. } else {
  53. if ($debug) {
  54. error_log('In initialize_item - generating new item object '.$next_item, 0);
  55. }
  56. $mylpi = new learnpathItem($next_item, $user_id);
  57. }
  58. if ($mylpi) {
  59. $mylpi->set_lp_view($view_id);
  60. }
  61. /*
  62. * now get what's needed by the SCORM API:
  63. * -score
  64. * -max
  65. * -min
  66. * -lesson_status
  67. * -session_time
  68. * -suspend_data
  69. */
  70. $myscore = $mylpi->get_score();
  71. $mymax = $mylpi->get_max();
  72. if ($mymax === '') {
  73. $mymax = "''";
  74. }
  75. $mymin = $mylpi->get_min();
  76. $mylesson_status = $mylpi->get_status();
  77. $mytotal_time = $mylpi->get_scorm_time('js', null, true);
  78. $mymastery_score = $mylpi->get_mastery_score();
  79. $mymax_time_allowed = $mylpi->get_max_time_allowed();
  80. $mylaunch_data = $mylpi->get_launch_data();
  81. $mysession_time = $mylpi->get_total_time();
  82. $mysuspend_data = $mylpi->get_suspend_data();
  83. $mylesson_location = $mylpi->get_lesson_location();
  84. $myic = $mylpi->get_interactions_count();
  85. $myistring = '';
  86. for ($i = 0; $i < $myic; $i++) {
  87. $myistring .= ",[".$i.",'','','','','','','']";
  88. }
  89. if (!empty($myistring)) {
  90. $myistring = substr($myistring, 1);
  91. }
  92. // Obtention des donnees d'objectifs
  93. $mycoursedb = Database::get_course_table(TABLE_LP_IV_OBJECTIVE);
  94. $course_id = api_get_course_int_id();
  95. $mylp_iv_id = $mylpi->db_item_view_id;
  96. $phpobjectives = [];
  97. if (!empty($mylp_iv_id)) {
  98. $sql = "SELECT objective_id, status, score_raw, score_max, score_min
  99. FROM $mycoursedb
  100. WHERE lp_iv_id = $mylp_iv_id AND c_id = $course_id
  101. ORDER BY id ASC;";
  102. $res = Database::query($sql);
  103. while ($row = Database::fetch_row($res)) {
  104. $phpobjectives[] = $row;
  105. }
  106. }
  107. $myobjectives = json_encode($phpobjectives);
  108. $return .=
  109. "olms.score=".$myscore.";".
  110. "olms.max=".$mymax.";".
  111. "olms.min=".$mymin.";".
  112. "olms.lesson_status='".$mylesson_status."';".
  113. "olms.lesson_location='".$mylesson_location."';".
  114. "olms.session_time='".$mysession_time."';".
  115. "olms.suspend_data='".$mysuspend_data."';".
  116. "olms.total_time = '".$mytotal_time."';".
  117. "olms.mastery_score = '".$mymastery_score."';".
  118. "olms.max_time_allowed = '".$mymax_time_allowed."';".
  119. "olms.launch_data = '".$mylaunch_data."';".
  120. "olms.interactions = new Array(".$myistring.");".
  121. //"olms.item_objectives = new Array();" .
  122. "olms.item_objectives = ".$myobjectives.";".
  123. "olms.G_lastError = 0;".
  124. "olms.G_LastErrorMessage = 'No error';".
  125. "olms.finishSignalReceived = 0;";
  126. /*
  127. * and re-initialise the rest (proper to the LMS)
  128. * -lms_lp_id
  129. * -lms_item_id
  130. * -lms_old_item_id
  131. * -lms_new_item_id
  132. * -lms_initialized
  133. * -lms_progress_bar_mode
  134. * -lms_view_id
  135. * -lms_user_id
  136. */
  137. $mynext = $mylp->get_next_item_id();
  138. $myprevious = $mylp->get_previous_item_id();
  139. $myitemtype = $mylpi->get_type();
  140. $mylesson_mode = $mylpi->get_lesson_mode();
  141. $mycredit = $mylpi->get_credit();
  142. $mylaunch_data = $mylpi->get_launch_data();
  143. $myinteractions_count = $mylpi->get_interactions_count();
  144. $mycore_exit = $mylpi->get_core_exit();
  145. $return .=
  146. "olms.lms_lp_id=".$lp_id.";".
  147. "olms.lms_item_id=".$next_item.";".
  148. "olms.lms_old_item_id=0;".
  149. "olms.lms_initialized=0;".
  150. "olms.lms_view_id=".$view_id.";".
  151. "olms.lms_user_id=".$user_id.";".
  152. "olms.next_item=".$next_item.";".// This one is very important to replace possible literal strings.
  153. "olms.lms_next_item=".$mynext.";".
  154. "olms.lms_previous_item=".$myprevious.";".
  155. "olms.lms_item_type = '".$myitemtype."';".
  156. "olms.lms_item_credit = '".$mycredit."';".
  157. "olms.lms_item_lesson_mode = '".$mylesson_mode."';".
  158. "olms.lms_item_launch_data = '".$mylaunch_data."';".
  159. "olms.lms_item_interactions_count = '".$myinteractions_count."';".
  160. "olms.lms_item_objectives_count = '".$myinteractions_count."';".
  161. "olms.lms_item_core_exit = '".$mycore_exit."';".
  162. "olms.asset_timer = 0;";
  163. $mylp->set_error_msg('');
  164. $mylp->prerequisites_match(); // Check the prerequisites are all complete.
  165. if ($debug) {
  166. error_log('Prereq_match() returned '.htmlentities($mylp->error), 0);
  167. error_log("return = $return ");
  168. error_log("mylp->lp_view_session_id: ".$mylp->lp_view_session_id);
  169. }
  170. return $return;
  171. }
  172. echo initialize_item(
  173. $_POST['lid'],
  174. $_POST['uid'],
  175. $_POST['vid'],
  176. $_POST['iid']
  177. );