lp_ajax_initialize.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. // Flag to allow for anonymous user - needs to be set before global.inc.php.
  12. $use_anonymous = true;
  13. // Name of the language file that needs to be included.
  14. $language_file[] = 'learnpath';
  15. require_once 'back_compat.inc.php';
  16. /**
  17. * Get one item's details
  18. * @param integer LP ID
  19. * @param integer user ID
  20. * @param integer View ID
  21. * @param integer Current item ID
  22. * @param integer New item ID
  23. */
  24. function initialize_item($lp_id, $user_id, $view_id, $next_item) {
  25. $debug = 0;
  26. $return = '';
  27. if ($debug > 0) { error_log('In initialize_item('.$lp_id.','.$user_id.','.$view_id.','.$next_item.')', 0); }
  28. //$objResponse = new xajaxResponse();
  29. /*$item_id may be one of:
  30. * -'next'
  31. * -'previous'
  32. * -'first'
  33. * -'last'
  34. * - a real item ID
  35. */
  36. require_once 'learnpath.class.php';
  37. require_once 'scorm.class.php';
  38. require_once 'aicc.class.php';
  39. require_once 'learnpathItem.class.php';
  40. require_once 'scormItem.class.php';
  41. require_once 'aiccItem.class.php';
  42. $mylp = '';
  43. if (isset($_SESSION['lpobject'])) {
  44. if ($debug > 1) { error_log('////$_SESSION[lpobject] is set', 0); }
  45. $oLP =& unserialize($_SESSION['lpobject']);
  46. if (!is_object($oLP)) {
  47. if ($debug > 1) { error_log(print_r($oLP,true), 0); }
  48. if ($debug > 2) { error_log('////Building new lp', 0); }
  49. unset($oLP);
  50. $code = api_get_course_id();
  51. $mylp = & new learnpath($code,$lp_id,$user_id);
  52. } else {
  53. if ($debug > 1) { error_log('////Reusing session lp', 0); }
  54. $mylp = & $oLP;
  55. }
  56. }
  57. $mylp->set_current_item($next_item);
  58. if ($debug > 1) { error_log('In initialize_item() - new item is '.$next_item, 0); }
  59. $mylp->start_current_item(true);
  60. /*
  61. if ($mylp->force_commit) {
  62. $mylp->save_current();
  63. }
  64. */
  65. //$objResponse->addAlert(api_get_path(REL_CODE_PATH).'newscorm/learnpathItem.class.php');
  66. if (is_object($mylp->items[$next_item])) {
  67. if ($debug > 1) { error_log('In initialize_item - recovering existing item object '.$next_item, 0); }
  68. $mylpi = & $mylp->items[$next_item];
  69. } else {
  70. if ($debug > 1) { error_log('In initialize_item - generating new item object '.$next_item, 0); }
  71. $mylpi =& new learnpathItem($next_item, $user_id);
  72. $mylpi->set_lp_view($view_id);
  73. }
  74. /*
  75. * now get what's needed by the SCORM API:
  76. * -score
  77. * -max
  78. * -min
  79. * -lesson_status
  80. * -session_time
  81. * -suspend_data
  82. */
  83. $myscore = $mylpi->get_score();
  84. $mymax = $mylpi->get_max();
  85. if ($mymax === '') { $mymax = "''"; }
  86. $mymin = $mylpi->get_min();
  87. $mylesson_status = $mylpi->get_status();
  88. $mylesson_location = $mylpi->get_lesson_location();
  89. $mytotal_time = $mylpi->get_scorm_time('js', null, true);
  90. $mymastery_score = $mylpi->get_mastery_score();
  91. $mymax_time_allowed = $mylpi->get_max_time_allowed();
  92. $mylaunch_data = $mylpi->get_launch_data();
  93. $mysession_time = $mylpi->get_total_time();
  94. $mysuspend_data = $mylpi->get_suspend_data();
  95. $mylesson_location = $mylpi->get_lesson_location();
  96. $myic = $mylpi->get_interactions_count();
  97. $myistring = '';
  98. for ($i = 0; $i < $myic; $i++) {
  99. $myistring .= ",[".$i.",'','','','','','','']";
  100. }
  101. if (!empty($myistring)) {
  102. $myistring = substr($myistring, 1);
  103. }
  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.G_lastError = 0;" .
  119. "olms.G_LastErrorMessage = 'No error';" ;
  120. /*
  121. * and re-initialise the rest (proper to the LMS)
  122. * -lms_lp_id
  123. * -lms_item_id
  124. * -lms_old_item_id
  125. * -lms_new_item_id
  126. * -lms_initialized
  127. * -lms_progress_bar_mode
  128. * -lms_view_id
  129. * -lms_user_id
  130. */
  131. $mytotal = $mylp->get_total_items_count_without_chapters();
  132. $mycomplete = $mylp->get_complete_items_count();
  133. $myprogress_mode = $mylp->get_progress_bar_mode();
  134. $myprogress_mode = ($myprogress_mode == '' ? '%' : $myprogress_mode);
  135. $mynext = $mylp->get_next_item_id();
  136. $myprevious = $mylp->get_previous_item_id();
  137. $myitemtype = $mylpi->get_type();
  138. $mylesson_mode = $mylpi->get_lesson_mode();
  139. $mycredit = $mylpi->get_credit();
  140. $mylaunch_data = $mylpi->get_launch_data();
  141. $myinteractions_count = $mylpi->get_interactions_count();
  142. $myobjectives_count = $mylpi->get_objectives_count();
  143. $mycore_exit = $mylpi->get_core_exit();
  144. $return .=
  145. "olms.lms_lp_id=".$lp_id.";" .
  146. "olms.lms_item_id=".$next_item.";" .
  147. "olms.lms_old_item_id=0;" .
  148. "olms.lms_initialized=0;" .
  149. "olms.lms_view_id=".$view_id.";" .
  150. "olms.lms_user_id=".$user_id.";" .
  151. "olms.next_item=".$next_item.";" . // This one is very important to replace possible literal strings.
  152. "olms.lms_next_item=".$mynext.";" .
  153. "olms.lms_previous_item=".$myprevious.";" .
  154. "olms.lms_item_type = '".$myitemtype."';" .
  155. "olms.lms_item_credit = '".$mycredit."';" .
  156. "olms.lms_item_lesson_mode = '".$mylesson_mode."';" .
  157. "olms.lms_item_launch_data = '".$mylaunch_data."';" .
  158. "olms.lms_item_interactions_count = '".$myinteractions_count."';" .
  159. "olms.lms_item_objectives_count = '".$myinteractions_count."';" .
  160. "olms.lms_item_core_exit = '".$mycore_exit."';" .
  161. "olms.asset_timer = 0;";
  162. $mylp->set_error_msg('');
  163. $mylp->prerequisites_match(); // Check the prerequisites are all complete.
  164. if ($debug > 1) { error_log('Prereq_match() returned '.htmlentities($mylp->error), 0); }
  165. //$_SESSION['scorm_item_id'] = $new_item_id; // Save the new item ID for the exercise tool to use/
  166. //$_SESSION['lpobject'] = serialize($mylp);
  167. return $return;
  168. }
  169. echo initialize_item($_POST['lid'], $_POST['uid'], $_POST['vid'], $_POST['iid']);