lp_ajax_initialize.php 6.4 KB

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