exercise_result.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. $js = '<script>'.api_get_language_translate_html().'</script>';
  48. $htmlHeadXtra[] = $js;
  49. if (api_is_in_gradebook()) {
  50. $interbreadcrumb[] = [
  51. 'url' => Category::getUrl(),
  52. 'name' => get_lang('ToolGradebook'),
  53. ];
  54. }
  55. $nameTools = get_lang('Exercises');
  56. $interbreadcrumb[] = [
  57. 'url' => 'exercise.php?'.api_get_cidreq(),
  58. 'name' => get_lang('Exercises'),
  59. ];
  60. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_JS_PATH).'hotspot/js/hotspot.js"></script>';
  61. $htmlHeadXtra[] = '<link rel="stylesheet" href="'.api_get_path(WEB_LIBRARY_JS_PATH).'hotspot/css/hotspot.css">';
  62. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_JS_PATH).'annotation/js/annotation.js"></script>';
  63. if (api_get_configuration_value('quiz_prevent_copy_paste')) {
  64. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_JS_PATH).'jquery.nocopypaste.js"></script>';
  65. }
  66. if (!empty($objExercise->getResultAccess())) {
  67. $htmlHeadXtra[] = api_get_css(api_get_path(WEB_LIBRARY_PATH).'javascript/epiclock/renderers/minute/epiclock.minute.css');
  68. $htmlHeadXtra[] = api_get_js('epiclock/javascript/jquery.dateformat.min.js');
  69. $htmlHeadXtra[] = api_get_js('epiclock/javascript/jquery.epiclock.min.js');
  70. $htmlHeadXtra[] = api_get_js('epiclock/renderers/minute/epiclock.minute.js');
  71. }
  72. if (!in_array($origin, ['learnpath', 'embeddable'])) {
  73. // So we are not in learnpath tool
  74. Display::display_header($nameTools, get_lang('Exercise'));
  75. } else {
  76. $htmlHeadXtra[] = "
  77. <style>
  78. body { background: none;}
  79. </style>
  80. ";
  81. Display::display_reduced_header();
  82. }
  83. // I'm in a preview mode as course admin. Display the action menu.
  84. if (api_is_course_admin() && !in_array($origin, ['learnpath', 'embeddable'])) {
  85. echo '<div class="actions">';
  86. echo '<a href="admin.php?'.api_get_cidreq().'&exerciseId='.$objExercise->id.'">'.
  87. Display::return_icon('back.png', get_lang('GoBackToQuestionList'), [], 32).'</a>';
  88. echo '<a href="exercise_admin.php?'.api_get_cidreq().'&modifyExercise=yes&exerciseId='.$objExercise->id.'">'.
  89. Display::return_icon('edit.png', get_lang('ModifyExercise'), [], 32).'</a>';
  90. echo '</div>';
  91. }
  92. $feedback_type = $objExercise->getFeedbackType();
  93. $exercise_stat_info = $objExercise->get_stat_track_exercise_info_by_exe_id($exe_id);
  94. if (!empty($exercise_stat_info['data_tracking'])) {
  95. $question_list = explode(',', $exercise_stat_info['data_tracking']);
  96. }
  97. $learnpath_id = isset($exercise_stat_info['orig_lp_id']) ? $exercise_stat_info['orig_lp_id'] : 0;
  98. $learnpath_item_id = isset($exercise_stat_info['orig_lp_item_id']) ? $exercise_stat_info['orig_lp_item_id'] : 0;
  99. $learnpath_item_view_id = isset($exercise_stat_info['orig_lp_item_view_id']) ? $exercise_stat_info['orig_lp_item_view_id'] : 0;
  100. if ($origin == 'learnpath') {
  101. ?>
  102. <form method="GET" action="exercise.php?<?php echo api_get_cidreq(); ?>">
  103. <input type="hidden" name="origin" value="<?php echo $origin; ?>" />
  104. <input type="hidden" name="learnpath_id" value="<?php echo $learnpath_id; ?>" />
  105. <input type="hidden" name="learnpath_item_id" value="<?php echo $learnpath_item_id; ?>"/>
  106. <input type="hidden" name="learnpath_item_view_id" value="<?php echo $learnpath_item_view_id; ?>" />
  107. <?php
  108. }
  109. $i = $total_score = $max_score = 0;
  110. $remainingMessage = '';
  111. $attemptButton = '';
  112. if ($origin !== 'embeddable') {
  113. $attemptButton = Display::toolbarButton(
  114. get_lang('AnotherAttempt'),
  115. api_get_path(WEB_CODE_PATH).'exercise/overview.php?'.api_get_cidreq().'&'.http_build_query([
  116. 'exerciseId' => $objExercise->id,
  117. 'learnpath_id' => $learnpath_id,
  118. 'learnpath_item_id' => $learnpath_item_id,
  119. 'learnpath_item_view_id' => $learnpath_item_view_id,
  120. ]),
  121. 'pencil-square-o',
  122. 'info'
  123. );
  124. }
  125. // We check if the user attempts before sending to the exercise_result.php
  126. if ($objExercise->selectAttempts() > 0) {
  127. $attempt_count = Event::get_attempt_count(
  128. api_get_user_id(),
  129. $objExercise->id,
  130. $learnpath_id,
  131. $learnpath_item_id,
  132. $learnpath_item_view_id
  133. );
  134. if ($attempt_count >= $objExercise->selectAttempts()) {
  135. echo Display::return_message(
  136. sprintf(get_lang('ReachedMaxAttempts'), $objExercise->selectTitle(), $objExercise->selectAttempts()),
  137. 'warning',
  138. false
  139. );
  140. if (!in_array($origin, ['learnpath', 'embeddable'])) {
  141. //we are not in learnpath tool
  142. Display::display_footer();
  143. }
  144. exit;
  145. } else {
  146. $attempt_count++;
  147. $remainingAttempts = $objExercise->selectAttempts() - $attempt_count;
  148. if ($remainingAttempts) {
  149. $attemptMessage = sprintf(get_lang('RemainingXAttempts'), $remainingAttempts);
  150. $remainingMessage = sprintf("<p>%s</p> %s", $attemptMessage, $attemptButton);
  151. }
  152. }
  153. } else {
  154. $remainingMessage = $attemptButton ? "<p>$attemptButton</p>" : '';
  155. }
  156. $total_score = 0;
  157. if (!empty($exercise_stat_info)) {
  158. $total_score = $exercise_stat_info['exe_result'];
  159. }
  160. $max_score = $objExercise->get_max_score();
  161. if ($origin !== 'embeddable') {
  162. echo Display::return_message(get_lang('Saved').'<br />', 'normal', false);
  163. }
  164. if ($origin == 'embeddable') {
  165. showEmbeddableFinishButton();
  166. }
  167. // Display and save questions
  168. ExerciseLib::displayQuestionListByAttempt(
  169. $objExercise,
  170. $exe_id,
  171. true,
  172. $remainingMessage
  173. );
  174. //Unset session for clock time
  175. ExerciseLib::exercise_time_control_delete(
  176. $objExercise->id,
  177. $learnpath_id,
  178. $learnpath_item_id
  179. );
  180. ExerciseLib::delete_chat_exercise_session($exe_id);
  181. if (!in_array($origin, ['learnpath', 'embeddable'])) {
  182. echo '<div class="question-return">';
  183. echo Display::url(
  184. get_lang('ReturnToCourseHomepage'),
  185. api_get_course_url(),
  186. ['class' => 'btn btn-primary']
  187. );
  188. echo '</div>';
  189. if (api_is_allowed_to_session_edit()) {
  190. Session::erase('objExercise');
  191. Session::erase('exe_id');
  192. Session::erase('calculatedAnswerId');
  193. Session::erase('duration_time_previous');
  194. Session::erase('duration_time');
  195. }
  196. Display::display_footer();
  197. } elseif ($origin === 'embeddable') {
  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. showEmbeddableFinishButton();
  207. Display::display_reduced_footer();
  208. } else {
  209. $lp_mode = Session::read('lp_mode');
  210. $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->getFeedbackType().'#atoc_'.$learnpath_item_id;
  211. $href = $lp_mode === 'fullscreen' ? ' window.opener.location.href="'.$url.'" ' : ' top.location.href="'.$url.'"';
  212. if (api_is_allowed_to_session_edit()) {
  213. Session::erase('objExercise');
  214. Session::erase('exe_id');
  215. Session::erase('calculatedAnswerId');
  216. Session::erase('duration_time_previous');
  217. Session::erase('duration_time');
  218. }
  219. Session::write('attempt_remaining', $remainingMessage);
  220. // Record the results in the learning path, using the SCORM interface (API)
  221. echo "<script>window.parent.API.void_save_asset('$total_score', '$max_score', 0, 'completed');</script>";
  222. echo '<script type="text/javascript">'.$href.'</script>';
  223. Display::display_reduced_footer();
  224. }
  225. function showEmbeddableFinishButton()
  226. {
  227. echo '<script>
  228. $(function () {
  229. $(\'.btn-close-quiz\').on(\'click\', function () {
  230. window.parent.$(\'video:not(.skip), audio:not(.skip)\').get(0).play();
  231. });
  232. });
  233. </script>';
  234. echo Display::tag(
  235. 'p',
  236. Display::toolbarButton(
  237. get_lang('EndTest'),
  238. '#',
  239. 'times',
  240. 'warning',
  241. ['role' => 'button', 'class' => 'btn-close-quiz']
  242. ),
  243. ['class' => 'text-right']
  244. );
  245. }