lp_ajax_initialize.php 7.5 KB

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