lp_ajax_initialize.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 __DIR__.'/../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. {
  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 (is_object($mylp->items[$next_item])) {
  38. if ($debug > 1) { error_log('In initialize_item - recovering existing item object '.$next_item, 0); }
  39. $mylpi = $mylp->items[$next_item];
  40. } else {
  41. if ($debug > 1) { error_log('In initialize_item - generating new item object '.$next_item, 0); }
  42. $mylpi = new learnpathItem($next_item, $user_id);
  43. }
  44. if ($mylpi) {
  45. $mylpi->set_lp_view($view_id);
  46. }
  47. /*
  48. * now get what's needed by the SCORM API:
  49. * -score
  50. * -max
  51. * -min
  52. * -lesson_status
  53. * -session_time
  54. * -suspend_data
  55. */
  56. $myscore = $mylpi->get_score();
  57. $mymax = $mylpi->get_max();
  58. if ($mymax === '') {
  59. $mymax = "''";
  60. }
  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->getTotalItemsCountWithoutDirs();
  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. echo initialize_item($_POST['lid'], $_POST['uid'], $_POST['vid'], $_POST['iid']);