exercise_result.php 7.2 KB

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