lp_ajax_initialize.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 '../inc/global.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 = '';
  46. $lpobject = Session::read('lpobject');
  47. if (isset($lpobject)) {
  48. $oLP = unserialize($lpobject);
  49. if ($debug) error_log("lpobject was set");
  50. if (!is_object($oLP)) {
  51. unset($oLP);
  52. $code = api_get_course_id();
  53. $mylp = new learnpath($code, $lp_id, $user_id);
  54. if ($debug) error_log("Creating learnpath");
  55. } else {
  56. $mylp = $oLP;
  57. if ($debug) error_log("Loading learnpath from unserialize");
  58. }
  59. } else {
  60. if ($debug) {
  61. error_log("lpobject was not set");
  62. }
  63. }
  64. $check_attempts = $mylp->check_item_attempts($next_item);
  65. if (!$check_attempts) {
  66. return false;
  67. }
  68. $mylp->set_current_item($next_item);
  69. if ($debug > 1) { error_log('In initialize_item() - new item is '.$next_item, 0); }
  70. $mylp->start_current_item(true);
  71. if (is_object($mylp->items[$next_item])) {
  72. if ($debug > 1) { error_log('In initialize_item - recovering existing item object '.$next_item, 0); }
  73. $mylpi = $mylp->items[$next_item];
  74. } else {
  75. if ($debug > 1) { error_log('In initialize_item - generating new item object '.$next_item, 0); }
  76. $mylpi = new learnpathItem($next_item, $user_id);
  77. }
  78. if ($mylpi) {
  79. $mylpi->set_lp_view($view_id);
  80. }
  81. /*
  82. * now get what's needed by the SCORM API:
  83. * -score
  84. * -max
  85. * -min
  86. * -lesson_status
  87. * -session_time
  88. * -suspend_data
  89. */
  90. $myscore = $mylpi->get_score();
  91. $mymax = $mylpi->get_max();
  92. if ($mymax === '') { $mymax = "''"; }
  93. $mymin = $mylpi->get_min();
  94. $mylesson_status = $mylpi->get_status();
  95. $mytotal_time = $mylpi->get_scorm_time('js', null, true);
  96. $mymastery_score = $mylpi->get_mastery_score();
  97. $mymax_time_allowed = $mylpi->get_max_time_allowed();
  98. $mylaunch_data = $mylpi->get_launch_data();
  99. $mysession_time = $mylpi->get_total_time();
  100. $mysuspend_data = $mylpi->get_suspend_data();
  101. $mylesson_location = $mylpi->get_lesson_location();
  102. $myic = $mylpi->get_interactions_count();
  103. $myistring = '';
  104. for ($i = 0; $i < $myic; $i++) {
  105. $myistring .= ",[".$i.",'','','','','','','']";
  106. }
  107. if (!empty($myistring)) {
  108. $myistring = substr($myistring, 1);
  109. }
  110. // Obtention des donnees d'objectifs
  111. $mycoursedb = Database::get_course_table(TABLE_LP_IV_OBJECTIVE);
  112. $course_id = api_get_course_int_id();
  113. $mylp_iv_id = $mylpi->db_item_view_id;
  114. $phpobjectives = array();
  115. if (!empty($mylp_iv_id)) {
  116. $sql = "SELECT objective_id, status, score_raw, score_max, score_min
  117. FROM $mycoursedb
  118. WHERE lp_iv_id = $mylp_iv_id AND c_id = $course_id
  119. ORDER BY id ASC;";
  120. $res = Database::query($sql);
  121. while ($row = Database::fetch_row($res)) {
  122. $phpobjectives[] = $row;
  123. }
  124. }
  125. $myobjectives = json_encode($phpobjectives);
  126. $return .=
  127. "olms.score=".$myscore.";" .
  128. "olms.max=".$mymax.";" .
  129. "olms.min=".$mymin.";" .
  130. "olms.lesson_status='".$mylesson_status."';" .
  131. "olms.lesson_location='".$mylesson_location."';" .
  132. "olms.session_time='".$mysession_time."';" .
  133. "olms.suspend_data='".$mysuspend_data."';" .
  134. "olms.total_time = '".$mytotal_time."';" .
  135. "olms.mastery_score = '".$mymastery_score."';" .
  136. "olms.max_time_allowed = '".$mymax_time_allowed."';" .
  137. "olms.launch_data = '".$mylaunch_data."';" .
  138. "olms.interactions = new Array(".$myistring.");" .
  139. //"olms.item_objectives = new Array();" .
  140. "olms.item_objectives = ".$myobjectives.";" .
  141. "olms.G_lastError = 0;" .
  142. "olms.G_LastErrorMessage = 'No error';" ;
  143. /*
  144. * and re-initialise the rest (proper to the LMS)
  145. * -lms_lp_id
  146. * -lms_item_id
  147. * -lms_old_item_id
  148. * -lms_new_item_id
  149. * -lms_initialized
  150. * -lms_progress_bar_mode
  151. * -lms_view_id
  152. * -lms_user_id
  153. */
  154. $mytotal = $mylp->get_total_items_count_without_chapters();
  155. $mycomplete = $mylp->get_complete_items_count();
  156. $myprogress_mode = $mylp->get_progress_bar_mode();
  157. $myprogress_mode = ($myprogress_mode == '' ? '%' : $myprogress_mode);
  158. $mynext = $mylp->get_next_item_id();
  159. $myprevious = $mylp->get_previous_item_id();
  160. $myitemtype = $mylpi->get_type();
  161. $mylesson_mode = $mylpi->get_lesson_mode();
  162. $mycredit = $mylpi->get_credit();
  163. $mylaunch_data = $mylpi->get_launch_data();
  164. $myinteractions_count = $mylpi->get_interactions_count();
  165. $myobjectives_count = $mylpi->get_objectives_count();
  166. $mycore_exit = $mylpi->get_core_exit();
  167. $return .=
  168. "olms.lms_lp_id=".$lp_id.";" .
  169. "olms.lms_item_id=".$next_item.";" .
  170. "olms.lms_old_item_id=0;" .
  171. "olms.lms_initialized=0;" .
  172. "olms.lms_view_id=".$view_id.";" .
  173. "olms.lms_user_id=".$user_id.";" .
  174. "olms.next_item=".$next_item.";" . // This one is very important to replace possible literal strings.
  175. "olms.lms_next_item=".$mynext.";" .
  176. "olms.lms_previous_item=".$myprevious.";" .
  177. "olms.lms_item_type = '".$myitemtype."';" .
  178. "olms.lms_item_credit = '".$mycredit."';" .
  179. "olms.lms_item_lesson_mode = '".$mylesson_mode."';" .
  180. "olms.lms_item_launch_data = '".$mylaunch_data."';" .
  181. "olms.lms_item_interactions_count = '".$myinteractions_count."';" .
  182. "olms.lms_item_objectives_count = '".$myinteractions_count."';" .
  183. "olms.lms_item_core_exit = '".$mycore_exit."';" .
  184. "olms.asset_timer = 0;";
  185. $mylp->set_error_msg('');
  186. $mylp->prerequisites_match(); // Check the prerequisites are all complete.
  187. if ($debug > 1) { error_log('Prereq_match() returned '.htmlentities($mylp->error), 0); }
  188. if ($debug > 1) { error_log("return = $return "); }
  189. return $return;
  190. }
  191. echo initialize_item($_POST['lid'], $_POST['uid'], $_POST['vid'], $_POST['iid']);
  192. exit;