lp_ajax_initialize.php 6.7 KB

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