lp_ajax_initialize.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 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. /**
  12. * Code
  13. */
  14. // Flag to allow for anonymous user - needs to be set before global.inc.php
  15. use \ChamiloSession as Session;
  16. $use_anonymous = true;
  17. // Name of the language file that needs to be included.
  18. $language_file[] = 'learnpath';
  19. require_once 'back_compat.inc.php';
  20. require_once 'learnpath.class.php';
  21. require_once 'scorm.class.php';
  22. require_once 'aicc.class.php';
  23. require_once 'learnpathItem.class.php';
  24. require_once 'scormItem.class.php';
  25. require_once 'aiccItem.class.php';
  26. /**
  27. * Get one item's details
  28. * @param integer LP ID
  29. * @param integer user ID
  30. * @param integer View ID
  31. * @param integer Current item ID
  32. * @param integer New item ID
  33. */
  34. function initialize_item($lp_id, $user_id, $view_id, $next_item) {
  35. global $debug;
  36. $return = '';
  37. if ($debug > 0) { error_log('In initialize_item('.$lp_id.','.$user_id.','.$view_id.','.$next_item.')', 0); }
  38. /*$item_id may be one of:
  39. * -'next'
  40. * -'previous'
  41. * -'first'
  42. * -'last'
  43. * - a real item ID
  44. */
  45. $mylp = learnpath::getLpFromSession(api_get_course_id(), $lp_id, $user_id);
  46. $mylp->set_current_item($next_item);
  47. if ($debug > 1) { error_log('In initialize_item() - new item is '.$next_item, 0); }
  48. $mylp->start_current_item(true);
  49. if (is_object($mylp->items[$next_item])) {
  50. if ($debug > 1) { error_log('In initialize_item - recovering existing item object '.$next_item, 0); }
  51. $mylpi = $mylp->items[$next_item];
  52. } else {
  53. if ($debug > 1) { error_log('In initialize_item - generating new item object '.$next_item, 0); }
  54. $mylpi = new learnpathItem($next_item, $user_id);
  55. }
  56. if ($mylpi) {
  57. $mylpi->set_lp_view($view_id);
  58. }
  59. /*
  60. * now get what's needed by the SCORM API:
  61. * -score
  62. * -max
  63. * -min
  64. * -lesson_status
  65. * -session_time
  66. * -suspend_data
  67. */
  68. $myscore = $mylpi->get_score();
  69. $mymax = $mylpi->get_max();
  70. if ($mymax === '') { $mymax = "''"; }
  71. $mymin = $mylpi->get_min();
  72. $mylesson_status = $mylpi->get_status();
  73. $mytotal_time = $mylpi->get_scorm_time('js', null, true);
  74. $mymastery_score = $mylpi->get_mastery_score();
  75. $mymax_time_allowed = $mylpi->get_max_time_allowed();
  76. $mylaunch_data = $mylpi->get_launch_data();
  77. $mysession_time = $mylpi->get_total_time();
  78. $mysuspend_data = $mylpi->get_suspend_data();
  79. $mylesson_location = $mylpi->get_lesson_location();
  80. $myic = $mylpi->get_interactions_count();
  81. $myistring = '';
  82. for ($i = 0; $i < $myic; $i++) {
  83. $myistring .= ",[".$i.",'','','','','','','']";
  84. }
  85. if (!empty($myistring)) {
  86. $myistring = substr($myistring, 1);
  87. }
  88. // Obtention des donnees d'objectifs
  89. $mycoursedb = Database::get_course_table(TABLE_LP_IV_OBJECTIVE);
  90. $course_id = api_get_course_int_id();
  91. $mylp_iv_id = $mylpi->db_item_view_id;
  92. $phpobjectives = array();
  93. if (!empty($mylp_iv_id)) {
  94. $sql = "SELECT objective_id, status, score_raw, score_max, score_min
  95. FROM $mycoursedb
  96. WHERE lp_iv_id = $mylp_iv_id AND c_id = $course_id
  97. ORDER BY id ASC;";
  98. $res = Database::query($sql);
  99. while ($row = Database::fetch_row($res)) {
  100. $phpobjectives[] = $row;
  101. }
  102. }
  103. $myobjectives = json_encode($phpobjectives);
  104. $return .=
  105. "olms.score=".$myscore.";" .
  106. "olms.max=".$mymax.";" .
  107. "olms.min=".$mymin.";" .
  108. "olms.lesson_status='".$mylesson_status."';" .
  109. "olms.lesson_location='".$mylesson_location."';" .
  110. "olms.session_time='".$mysession_time."';" .
  111. "olms.suspend_data='".$mysuspend_data."';" .
  112. "olms.total_time = '".$mytotal_time."';" .
  113. "olms.mastery_score = '".$mymastery_score."';" .
  114. "olms.max_time_allowed = '".$mymax_time_allowed."';" .
  115. "olms.launch_data = '".$mylaunch_data."';" .
  116. "olms.interactions = new Array(".$myistring.");" .
  117. //"olms.item_objectives = new Array();" .
  118. "olms.item_objectives = ".$myobjectives.";" .
  119. "olms.G_lastError = 0;" .
  120. "olms.G_LastErrorMessage = 'No error';".
  121. "olms.finishSignalReceived = 0;";
  122. /*
  123. * and re-initialise the rest (proper to the LMS)
  124. * -lms_lp_id
  125. * -lms_item_id
  126. * -lms_old_item_id
  127. * -lms_new_item_id
  128. * -lms_initialized
  129. * -lms_progress_bar_mode
  130. * -lms_view_id
  131. * -lms_user_id
  132. */
  133. $mytotal = $mylp->get_total_items_count_without_chapters();
  134. $mycomplete = $mylp->get_complete_items_count();
  135. $myprogress_mode = $mylp->get_progress_bar_mode();
  136. $myprogress_mode = ($myprogress_mode == '' ? '%' : $myprogress_mode);
  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. $myobjectives_count = $mylpi->get_objectives_count();
  145. $mycore_exit = $mylpi->get_core_exit();
  146. $return .=
  147. "olms.lms_lp_id=".$lp_id.";" .
  148. "olms.lms_item_id=".$next_item.";" .
  149. "olms.lms_old_item_id=0;" .
  150. "olms.lms_initialized=0;" .
  151. "olms.lms_view_id=".$view_id.";" .
  152. "olms.lms_user_id=".$user_id.";" .
  153. "olms.next_item=".$next_item.";" . // This one is very important to replace possible literal strings.
  154. "olms.lms_next_item=".$mynext.";" .
  155. "olms.lms_previous_item=".$myprevious.";" .
  156. "olms.lms_item_type = '".$myitemtype."';" .
  157. "olms.lms_item_credit = '".$mycredit."';" .
  158. "olms.lms_item_lesson_mode = '".$mylesson_mode."';" .
  159. "olms.lms_item_launch_data = '".$mylaunch_data."';" .
  160. "olms.lms_item_interactions_count = '".$myinteractions_count."';" .
  161. "olms.lms_item_objectives_count = '".$myinteractions_count."';" .
  162. "olms.lms_item_core_exit = '".$mycore_exit."';" .
  163. "olms.asset_timer = 0;";
  164. $mylp->set_error_msg('');
  165. $mylp->prerequisites_match(); // Check the prerequisites are all complete.
  166. if ($debug > 1) { error_log('Prereq_match() returned '.htmlentities($mylp->error), 0); }
  167. if ($debug > 1) { error_log("return = $return "); }
  168. return $return;
  169. }
  170. echo initialize_item($_POST['lid'], $_POST['uid'], $_POST['vid'], $_POST['iid']);