exercise_result.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Exercise result
  6. * This script gets information from the script "exercise_submit.php",
  7. * through the session, and calculates the score of the student for
  8. * that exercise.
  9. * Then it shows the results on the screen.
  10. * @package chamilo.exercise
  11. * @author Olivier Brouckaert, main author
  12. * @author Roan Embrechts, some refactoring
  13. * @author Julio Montoya switchable fill in blank option added
  14. *
  15. * @todo split more code up in functions, move functions to library?
  16. */
  17. $debug = false;
  18. require_once __DIR__.'/../inc/global.inc.php';
  19. $this_section = SECTION_COURSES;
  20. /* ACCESS RIGHTS */
  21. api_protect_course_script(true);
  22. if ($debug) {
  23. error_log('Entering exercise_result.php: '.print_r($_POST, 1));
  24. }
  25. // general parameters passed via POST/GET
  26. if (empty($origin)) {
  27. $origin = Security::remove_XSS($_REQUEST['origin']);
  28. }
  29. /** @var Exercise $objExercise */
  30. if (empty($objExercise)) {
  31. $objExercise = Session::read('objExercise');
  32. }
  33. if (empty($remind_list)) {
  34. $remind_list = isset($_REQUEST['remind_list']) ? $_REQUEST['remind_list'] : null;
  35. }
  36. $exe_id = isset($_REQUEST['exe_id']) ? intval($_REQUEST['exe_id']) : 0;
  37. if (empty($objExercise)) {
  38. // Redirect to the exercise overview
  39. // Check if the exe_id exists
  40. $objExercise = new Exercise();
  41. $exercise_stat_info = $objExercise->get_stat_track_exercise_info_by_exe_id($exe_id);
  42. if (!empty($exercise_stat_info) && isset($exercise_stat_info['exe_exo_id'])) {
  43. header("Location: overview.php?exerciseId=".$exercise_stat_info['exe_exo_id']);
  44. exit;
  45. }
  46. api_not_allowed();
  47. }
  48. if (api_is_in_gradebook()) {
  49. $interbreadcrumb[] = array(
  50. 'url' => Category::getUrl(),
  51. 'name' => get_lang('ToolGradebook'),
  52. );
  53. }
  54. $nameTools = get_lang('Exercises');
  55. $interbreadcrumb[] = array(
  56. "url" => "exercise.php?".api_get_cidreq(),
  57. "name" => get_lang('Exercises'),
  58. );
  59. $htmlHeadXtra[] = api_get_css('js/hotspot/css/hotspot.css');
  60. $htmlHeadXtra[] = api_get_js('js/hotspot/js/hotspot.js');
  61. if ($origin != 'learnpath') {
  62. // So we are not in learnpath tool
  63. Display::display_header($nameTools, get_lang('Exercise'));
  64. } else {
  65. $htmlHeadXtra[] = "
  66. <style>
  67. body { background: none;}
  68. </style>
  69. ";
  70. Display::display_reduced_header();
  71. }
  72. /* DISPLAY AND MAIN PROCESS */
  73. // I'm in a preview mode as course admin. Display the action menu.
  74. if (api_is_course_admin() && $origin != 'learnpath') {
  75. echo '<div class="actions">';
  76. echo '<a href="admin.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id.'">'.
  77. Display::return_icon('back.png', get_lang('GoBackToQuestionList'), array(), 32).'</a>';
  78. echo '<a href="exercise_admin.php?'.api_get_cidreq().'&modifyExercise=yes&exerciseId='.$objExercise->id.'">'.
  79. Display::return_icon('edit.png', get_lang('ModifyExercise'), array(), 32).'</a>';
  80. echo '</div>';
  81. }
  82. $feedback_type = $objExercise->feedback_type;
  83. $exercise_stat_info = $objExercise->get_stat_track_exercise_info_by_exe_id($exe_id);
  84. if (!empty($exercise_stat_info['data_tracking'])) {
  85. $question_list = explode(',', $exercise_stat_info['data_tracking']);
  86. }
  87. $learnpath_id = isset($exercise_stat_info['orig_lp_id']) ? $exercise_stat_info['orig_lp_id'] : 0;
  88. $learnpath_item_id = isset($exercise_stat_info['orig_lp_item_id']) ? $exercise_stat_info['orig_lp_item_id'] : 0;
  89. $learnpath_item_view_id = isset($exercise_stat_info['orig_lp_item_view_id']) ? $exercise_stat_info['orig_lp_item_view_id'] : 0;
  90. if ($origin == 'learnpath') {
  91. ?>
  92. <form method="GET" action="exercise.php?<?php echo api_get_cidreq() ?>">
  93. <input type="hidden" name="origin" value="<?php echo $origin; ?>" />
  94. <input type="hidden" name="learnpath_id" value="<?php echo $learnpath_id; ?>" />
  95. <input type="hidden" name="learnpath_item_id" value="<?php echo $learnpath_item_id; ?>" />
  96. <input type="hidden" name="learnpath_item_view_id" value="<?php echo $learnpath_item_view_id; ?>" />
  97. <?php
  98. }
  99. $i = $total_score = $max_score = 0;
  100. $remainingMessage = '';
  101. // We check if the user attempts before sending to the exercise_result.php
  102. if ($objExercise->selectAttempts() > 0) {
  103. $attempt_count = Event::get_attempt_count(
  104. api_get_user_id(),
  105. $objExercise->id,
  106. $learnpath_id,
  107. $learnpath_item_id,
  108. $learnpath_item_view_id
  109. );
  110. if ($attempt_count >= $objExercise->selectAttempts()) {
  111. echo Display::return_message(
  112. sprintf(get_lang('ReachedMaxAttempts'), $objExercise->selectTitle(), $objExercise->selectAttempts()),
  113. 'warning',
  114. false
  115. );
  116. if ($origin != 'learnpath') {
  117. //we are not in learnpath tool
  118. Display::display_footer();
  119. }
  120. exit;
  121. } else {
  122. $attempt_count++;
  123. $remainingAttempts = $objExercise->selectAttempts() - $attempt_count;
  124. if ($remainingAttempts) {
  125. $attemptButton = Display::toolbarButton(
  126. get_lang('AnotherAttempt'),
  127. api_get_path(WEB_CODE_PATH).'exercise/overview.php?'.api_get_cidreq().'&'.http_build_query([
  128. 'exerciseId' => $objExercise->id,
  129. 'learnpath_id' => $learnpath_id,
  130. 'learnpath_item_id' => $learnpath_item_id
  131. ]),
  132. 'pencil-square-o',
  133. 'info'
  134. );
  135. $attemptMessage = sprintf(get_lang('RemainingXAttempts'), $remainingAttempts);
  136. $remainingMessage = sprintf("<p>%s</p> %s", $attemptMessage, $attemptButton);
  137. }
  138. }
  139. }
  140. $total_score = 0;
  141. if (!empty($exercise_stat_info)) {
  142. $total_score = $exercise_stat_info['exe_result'];
  143. }
  144. $max_score = $objExercise->get_max_score();
  145. echo Display::return_message(get_lang('Saved').'<br />', 'normal', false);
  146. // Display and save questions
  147. ExerciseLib::displayQuestionListByAttempt(
  148. $objExercise,
  149. $exe_id,
  150. true,
  151. $remainingMessage
  152. );
  153. //Unset session for clock time
  154. ExerciseLib::exercise_time_control_delete(
  155. $objExercise->id,
  156. $learnpath_id,
  157. $learnpath_item_id
  158. );
  159. ExerciseLib::delete_chat_exercise_session($exe_id);
  160. if ($origin != 'learnpath') {
  161. echo '<div class="question-return">';
  162. echo Display::url(
  163. get_lang('ReturnToCourseHomepage'),
  164. api_get_course_url(),
  165. array('class' => 'btn btn-primary')
  166. );
  167. echo '</div>';
  168. if (api_is_allowed_to_session_edit()) {
  169. Session::erase('objExercise');
  170. Session::erase('exe_id');
  171. }
  172. Display::display_footer();
  173. } else {
  174. $lp_mode = Session::read('lp_mode');
  175. $url = '../lp/lp_controller.php?'.api_get_cidreq().'&action=view&lp_id='.$learnpath_id.'&lp_item_id='.$learnpath_item_id.'&exeId='.$exercise_stat_info['exe_id'].'&fb_type='.$objExercise->feedback_type.'#atoc_'.$learnpath_item_id;
  176. $href = $lp_mode == 'fullscreen' ? ' window.opener.location.href="'.$url.'" ' : ' top.location.href="'.$url.'"';
  177. if (api_is_allowed_to_session_edit()) {
  178. Session::erase('objExercise');
  179. Session::erase('exe_id');
  180. }
  181. Session::write('attempt_remaining', $remainingMessage);
  182. // Record the results in the learning path, using the SCORM interface (API)
  183. echo "<script>window.parent.API.void_save_asset('$total_score', '$max_score', 0, 'completed');</script>";
  184. echo '<script type="text/javascript">'.$href.'</script>';
  185. echo '</body></html>';
  186. }