exercise_result.php 9.0 KB

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