exercise_result.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. *
  11. * @package chamilo.exercise
  12. *
  13. * @author Olivier Brouckaert, main author
  14. * @author Roan Embrechts, some refactoring
  15. * @author Julio Montoya switchable fill in blank option added
  16. *
  17. * @todo split more code up in functions, move functions to library?
  18. */
  19. $debug = false;
  20. require_once __DIR__.'/../inc/global.inc.php';
  21. $this_section = SECTION_COURSES;
  22. /* ACCESS RIGHTS */
  23. api_protect_course_script(true);
  24. if ($debug) {
  25. error_log('Entering exercise_result.php: '.print_r($_POST, 1));
  26. }
  27. $origin = api_get_origin();
  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']) ? (int) $_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'].'&'.api_get_cidreq());
  43. exit;
  44. }
  45. api_not_allowed(true);
  46. }
  47. if (api_is_in_gradebook()) {
  48. $interbreadcrumb[] = [
  49. 'url' => Category::getUrl(),
  50. 'name' => get_lang('ToolGradebook'),
  51. ];
  52. }
  53. $nameTools = get_lang('Exercises');
  54. $interbreadcrumb[] = [
  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. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_JS_PATH).'annotation/js/annotation.js"></script>';
  61. if (api_get_configuration_value('quiz_prevent_copy_paste')) {
  62. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_JS_PATH).'jquery.nocopypaste.js"></script>';
  63. }
  64. if (!in_array($origin, ['learnpath', 'embeddable'])) {
  65. // So we are not in learnpath tool
  66. Display::display_header($nameTools, get_lang('Exercise'));
  67. } else {
  68. $htmlHeadXtra[] = "
  69. <style>
  70. body { background: none;}
  71. </style>
  72. ";
  73. Display::display_reduced_header();
  74. }
  75. // I'm in a preview mode as course admin. Display the action menu.
  76. if (api_is_course_admin() && !in_array($origin, ['learnpath', 'embeddable'])) {
  77. echo '<div class="actions">';
  78. echo '<a href="admin.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id.'">'.
  79. Display::return_icon('back.png', get_lang('GoBackToQuestionList'), [], 32).'</a>';
  80. echo '<a href="exercise_admin.php?'.api_get_cidreq().'&modifyExercise=yes&exerciseId='.$objExercise->id.'">'.
  81. Display::return_icon('edit.png', get_lang('ModifyExercise'), [], 32).'</a>';
  82. echo '</div>';
  83. }
  84. $feedback_type = $objExercise->feedback_type;
  85. $exercise_stat_info = $objExercise->get_stat_track_exercise_info_by_exe_id($exe_id);
  86. if (!empty($exercise_stat_info['data_tracking'])) {
  87. $question_list = explode(',', $exercise_stat_info['data_tracking']);
  88. }
  89. $learnpath_id = isset($exercise_stat_info['orig_lp_id']) ? $exercise_stat_info['orig_lp_id'] : 0;
  90. $learnpath_item_id = isset($exercise_stat_info['orig_lp_item_id']) ? $exercise_stat_info['orig_lp_item_id'] : 0;
  91. $learnpath_item_view_id = isset($exercise_stat_info['orig_lp_item_view_id']) ? $exercise_stat_info['orig_lp_item_view_id'] : 0;
  92. if ($origin == 'learnpath') {
  93. ?>
  94. <form method="GET" action="exercise.php?<?php echo api_get_cidreq(); ?>">
  95. <input type="hidden" name="origin" value="<?php echo $origin; ?>" />
  96. <input type="hidden" name="learnpath_id" value="<?php echo $learnpath_id; ?>" />
  97. <input type="hidden" name="learnpath_item_id" value="<?php echo $learnpath_item_id; ?>"/>
  98. <input type="hidden" name="learnpath_item_view_id" value="<?php echo $learnpath_item_view_id; ?>" />
  99. <?php
  100. }
  101. $i = $total_score = $max_score = 0;
  102. $remainingMessage = '';
  103. $attemptButton = '';
  104. if ($origin !== 'embeddable') {
  105. $attemptButton = Display::toolbarButton(
  106. get_lang('AnotherAttempt'),
  107. api_get_path(WEB_CODE_PATH).'exercise/overview.php?'.api_get_cidreq().'&'.http_build_query([
  108. 'exerciseId' => $objExercise->id,
  109. 'learnpath_id' => $learnpath_id,
  110. 'learnpath_item_id' => $learnpath_item_id,
  111. 'learnpath_item_view_id' => $learnpath_item_view_id,
  112. ]),
  113. 'pencil-square-o',
  114. 'info'
  115. );
  116. }
  117. // We check if the user attempts before sending to the exercise_result.php
  118. if ($objExercise->selectAttempts() > 0) {
  119. $attempt_count = Event::get_attempt_count(
  120. api_get_user_id(),
  121. $objExercise->id,
  122. $learnpath_id,
  123. $learnpath_item_id,
  124. $learnpath_item_view_id
  125. );
  126. if ($attempt_count >= $objExercise->selectAttempts()) {
  127. echo Display::return_message(
  128. sprintf(get_lang('ReachedMaxAttempts'), $objExercise->selectTitle(), $objExercise->selectAttempts()),
  129. 'warning',
  130. false
  131. );
  132. if (!in_array($origin, ['learnpath', 'embeddable'])) {
  133. //we are not in learnpath tool
  134. Display::display_footer();
  135. }
  136. exit;
  137. } else {
  138. $attempt_count++;
  139. $remainingAttempts = $objExercise->selectAttempts() - $attempt_count;
  140. if ($remainingAttempts) {
  141. $attemptMessage = sprintf(get_lang('RemainingXAttempts'), $remainingAttempts);
  142. $remainingMessage = sprintf("<p>%s</p> %s", $attemptMessage, $attemptButton);
  143. }
  144. }
  145. } else {
  146. $remainingMessage = $attemptButton ? "<p>$attemptButton</p>" : '';
  147. }
  148. $total_score = 0;
  149. if (!empty($exercise_stat_info)) {
  150. $total_score = $exercise_stat_info['exe_result'];
  151. }
  152. $max_score = $objExercise->get_max_score();
  153. echo Display::return_message(get_lang('Saved').'<br />', 'normal', false);
  154. // Display and save questions
  155. ExerciseLib::displayQuestionListByAttempt(
  156. $objExercise,
  157. $exe_id,
  158. true,
  159. $remainingMessage
  160. );
  161. //Unset session for clock time
  162. ExerciseLib::exercise_time_control_delete(
  163. $objExercise->id,
  164. $learnpath_id,
  165. $learnpath_item_id
  166. );
  167. ExerciseLib::delete_chat_exercise_session($exe_id);
  168. if (!in_array($origin, ['learnpath', 'embeddable'])) {
  169. echo '<div class="question-return">';
  170. echo Display::url(
  171. get_lang('ReturnToCourseHomepage'),
  172. api_get_course_url(),
  173. ['class' => 'btn btn-primary']
  174. );
  175. echo '</div>';
  176. if (api_is_allowed_to_session_edit()) {
  177. Session::erase('objExercise');
  178. Session::erase('exe_id');
  179. Session::erase('calculatedAnswerId');
  180. Session::erase('duration_time_previous');
  181. Session::erase('duration_time');
  182. }
  183. Display::display_footer();
  184. } elseif ($origin == 'embeddable') {
  185. if (api_is_allowed_to_session_edit()) {
  186. Session::erase('objExercise');
  187. Session::erase('exe_id');
  188. Session::erase('calculatedAnswerId');
  189. Session::erase('duration_time_previous');
  190. Session::erase('duration_time');
  191. }
  192. Session::write('attempt_remaining', $remainingMessage);
  193. Display::display_reduced_footer();
  194. } else {
  195. $lp_mode = Session::read('lp_mode');
  196. $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;
  197. $href = $lp_mode == 'fullscreen' ? ' window.opener.location.href="'.$url.'" ' : ' top.location.href="'.$url.'"';
  198. if (api_is_allowed_to_session_edit()) {
  199. Session::erase('objExercise');
  200. Session::erase('exe_id');
  201. Session::erase('calculatedAnswerId');
  202. Session::erase('duration_time_previous');
  203. Session::erase('duration_time');
  204. }
  205. Session::write('attempt_remaining', $remainingMessage);
  206. // Record the results in the learning path, using the SCORM interface (API)
  207. echo "<script>window.parent.API.void_save_asset('$total_score', '$max_score', 0, 'completed');</script>";
  208. echo '<script type="text/javascript">'.$href.'</script>';
  209. Display::display_reduced_footer();
  210. }