| 1 | 
           efrain | 
           1 | 
           <?php
  | 
        
        
            | 
            | 
           2 | 
           // This file is part of Moodle - http://moodle.org/
  | 
        
        
            | 
            | 
           3 | 
           //
  | 
        
        
            | 
            | 
           4 | 
           // Moodle is free software: you can redistribute it and/or modify
  | 
        
        
            | 
            | 
           5 | 
           // it under the terms of the GNU General Public License as published by
  | 
        
        
            | 
            | 
           6 | 
           // the Free Software Foundation, either version 3 of the License, or
  | 
        
        
            | 
            | 
           7 | 
           // (at your option) any later version.
  | 
        
        
            | 
            | 
           8 | 
           //
  | 
        
        
            | 
            | 
           9 | 
           // Moodle is distributed in the hope that it will be useful,
  | 
        
        
            | 
            | 
           10 | 
           // but WITHOUT ANY WARRANTY; without even the implied warranty of
  | 
        
        
            | 
            | 
           11 | 
           // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  | 
        
        
            | 
            | 
           12 | 
           // GNU General Public License for more details.
  | 
        
        
            | 
            | 
           13 | 
           //
  | 
        
        
            | 
            | 
           14 | 
           // You should have received a copy of the GNU General Public License
  | 
        
        
            | 
            | 
           15 | 
           // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  | 
        
        
            | 
            | 
           16 | 
              | 
        
        
            | 
            | 
           17 | 
           /**
  | 
        
        
            | 
            | 
           18 | 
            * This script displays a particular page of a quiz attempt that is in progress.
  | 
        
        
            | 
            | 
           19 | 
            *
  | 
        
        
            | 
            | 
           20 | 
            * @package   mod_quiz
  | 
        
        
            | 
            | 
           21 | 
            * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
  | 
        
        
            | 
            | 
           22 | 
            * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           23 | 
            */
  | 
        
        
            | 
            | 
           24 | 
              | 
        
        
            | 
            | 
           25 | 
           use mod_quiz\output\navigation_panel_attempt;
  | 
        
        
            | 
            | 
           26 | 
           use mod_quiz\output\renderer;
  | 
        
        
            | 
            | 
           27 | 
           use mod_quiz\quiz_attempt;
  | 
        
        
            | 
            | 
           28 | 
              | 
        
        
            | 
            | 
           29 | 
           require_once(__DIR__ . '/../../config.php');
  | 
        
        
            | 
            | 
           30 | 
           require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  | 
        
        
            | 
            | 
           31 | 
              | 
        
        
            | 
            | 
           32 | 
           // Look for old-style URLs, such as may be in the logs, and redirect them to startattemtp.php.
  | 
        
        
            | 
            | 
           33 | 
           if ($id = optional_param('id', 0, PARAM_INT)) {
  | 
        
        
            | 
            | 
           34 | 
               redirect($CFG->wwwroot . '/mod/quiz/startattempt.php?cmid=' . $id . '&sesskey=' . sesskey());
  | 
        
        
            | 
            | 
           35 | 
           } else if ($qid = optional_param('q', 0, PARAM_INT)) {
  | 
        
        
            | 
            | 
           36 | 
               if (!$cm = get_coursemodule_from_instance('quiz', $qid)) {
  | 
        
        
            | 
            | 
           37 | 
                   throw new \moodle_exception('invalidquizid', 'quiz');
  | 
        
        
            | 
            | 
           38 | 
               }
  | 
        
        
            | 
            | 
           39 | 
               redirect(new moodle_url('/mod/quiz/startattempt.php',
  | 
        
        
            | 
            | 
           40 | 
                       ['cmid' => $cm->id, 'sesskey' => sesskey()]));
  | 
        
        
            | 
            | 
           41 | 
           }
  | 
        
        
            | 
            | 
           42 | 
              | 
        
        
            | 
            | 
           43 | 
           // Get submitted parameters.
  | 
        
        
            | 
            | 
           44 | 
           $attemptid = required_param('attempt', PARAM_INT);
  | 
        
        
            | 
            | 
           45 | 
           $page = optional_param('page', 0, PARAM_INT);
  | 
        
        
            | 
            | 
           46 | 
           $cmid = optional_param('cmid', null, PARAM_INT);
  | 
        
        
            | 
            | 
           47 | 
              | 
        
        
            | 
            | 
           48 | 
           $attemptobj = quiz_create_attempt_handling_errors($attemptid, $cmid);
  | 
        
        
            | 
            | 
           49 | 
           $page = $attemptobj->force_page_number_into_range($page);
  | 
        
        
            | 
            | 
           50 | 
           $PAGE->set_url($attemptobj->attempt_url(null, $page));
  | 
        
        
            | 
            | 
           51 | 
           // During quiz attempts, the browser back/forwards buttons should force a reload.
  | 
        
        
            | 
            | 
           52 | 
           $PAGE->set_cacheable(false);
  | 
        
        
            | 
            | 
           53 | 
              | 
        
        
            | 
            | 
           54 | 
           $PAGE->set_secondary_active_tab("modulepage");
  | 
        
        
            | 
            | 
           55 | 
              | 
        
        
            | 
            | 
           56 | 
           // Check login.
  | 
        
        
            | 
            | 
           57 | 
           require_login($attemptobj->get_course(), false, $attemptobj->get_cm());
  | 
        
        
            | 
            | 
           58 | 
              | 
        
        
            | 
            | 
           59 | 
           // Check that this attempt belongs to this user.
  | 
        
        
            | 
            | 
           60 | 
           if ($attemptobj->get_userid() != $USER->id) {
  | 
        
        
            | 
            | 
           61 | 
               if ($attemptobj->has_capability('mod/quiz:viewreports')) {
  | 
        
        
            | 
            | 
           62 | 
                   redirect($attemptobj->review_url(null, $page));
  | 
        
        
            | 
            | 
           63 | 
               } else {
  | 
        
        
            | 
            | 
           64 | 
                   throw new moodle_exception('notyourattempt', 'quiz', $attemptobj->view_url());
  | 
        
        
            | 
            | 
           65 | 
               }
  | 
        
        
            | 
            | 
           66 | 
           }
  | 
        
        
            | 
            | 
           67 | 
              | 
        
        
            | 
            | 
           68 | 
           // Check capabilities and block settings.
  | 
        
        
            | 
            | 
           69 | 
           if (!$attemptobj->is_preview_user()) {
  | 
        
        
            | 
            | 
           70 | 
               $attemptobj->require_capability('mod/quiz:attempt');
  | 
        
        
            | 
            | 
           71 | 
               if (empty($attemptobj->get_quiz()->showblocks)) {
  | 
        
        
            | 
            | 
           72 | 
                   $PAGE->blocks->show_only_fake_blocks();
  | 
        
        
            | 
            | 
           73 | 
               }
  | 
        
        
            | 
            | 
           74 | 
              | 
        
        
            | 
            | 
           75 | 
           } else {
  | 
        
        
            | 
            | 
           76 | 
               navigation_node::override_active_url($attemptobj->start_attempt_url());
  | 
        
        
            | 
            | 
           77 | 
           }
  | 
        
        
            | 
            | 
           78 | 
              | 
        
        
            | 
            | 
           79 | 
           // If the attempt is already closed, send them to the review page.
  | 
        
        
            | 
            | 
           80 | 
           if ($attemptobj->is_finished()) {
  | 
        
        
            | 
            | 
           81 | 
               redirect($attemptobj->review_url(null, $page));
  | 
        
        
            | 
            | 
           82 | 
           } else if ($attemptobj->get_state() == quiz_attempt::OVERDUE) {
  | 
        
        
            | 
            | 
           83 | 
               redirect($attemptobj->summary_url());
  | 
        
        
            | 
            | 
           84 | 
           }
  | 
        
        
            | 
            | 
           85 | 
              | 
        
        
            | 
            | 
           86 | 
           // Check the access rules.
  | 
        
        
            | 
            | 
           87 | 
           $accessmanager = $attemptobj->get_access_manager(time());
  | 
        
        
            | 
            | 
           88 | 
           $accessmanager->setup_attempt_page($PAGE);
  | 
        
        
            | 
            | 
           89 | 
           /** @var renderer $output */
  | 
        
        
            | 
            | 
           90 | 
           $output = $PAGE->get_renderer('mod_quiz');
  | 
        
        
            | 
            | 
           91 | 
           $messages = $accessmanager->prevent_access();
  | 
        
        
            | 
            | 
           92 | 
           if (!$attemptobj->is_preview_user() && $messages) {
  | 
        
        
            | 
            | 
           93 | 
               throw new \moodle_exception('attempterror', 'quiz', $attemptobj->view_url(),
  | 
        
        
            | 
            | 
           94 | 
                       $output->access_messages($messages));
  | 
        
        
            | 
            | 
           95 | 
           }
  | 
        
        
            | 
            | 
           96 | 
           if ($accessmanager->is_preflight_check_required($attemptobj->get_attemptid())) {
  | 
        
        
            | 
            | 
           97 | 
               redirect($attemptobj->start_attempt_url(null, $page));
  | 
        
        
            | 
            | 
           98 | 
           }
  | 
        
        
            | 
            | 
           99 | 
              | 
        
        
            | 
            | 
           100 | 
           // Set up auto-save if required.
  | 
        
        
            | 
            | 
           101 | 
           $autosaveperiod = get_config('quiz', 'autosaveperiod');
  | 
        
        
            | 
            | 
           102 | 
           if ($autosaveperiod) {
  | 
        
        
            | 
            | 
           103 | 
               $PAGE->requires->string_for_js('strftimedatetimeshortaccurate', 'langconfig');
  | 
        
        
            | 
            | 
           104 | 
               $PAGE->requires->string_for_js('lastautosave', 'quiz');
  | 
        
        
            | 
            | 
           105 | 
               $PAGE->requires->yui_module('moodle-mod_quiz-autosave',
  | 
        
        
            | 
            | 
           106 | 
                       'M.mod_quiz.autosave.init', [$autosaveperiod]);
  | 
        
        
            | 
            | 
           107 | 
           }
  | 
        
        
            | 
            | 
           108 | 
              | 
        
        
            | 
            | 
           109 | 
           // Log this page view.
  | 
        
        
            | 
            | 
           110 | 
           $attemptobj->fire_attempt_viewed_event();
  | 
        
        
            | 
            | 
           111 | 
              | 
        
        
            | 
            | 
           112 | 
           // Get the list of questions needed by this page.
  | 
        
        
            | 
            | 
           113 | 
           $slots = $attemptobj->get_slots($page);
  | 
        
        
            | 
            | 
           114 | 
              | 
        
        
            | 
            | 
           115 | 
           // Check.
  | 
        
        
            | 
            | 
           116 | 
           if (empty($slots)) {
  | 
        
        
            | 
            | 
           117 | 
               throw new moodle_exception('noquestionsfound', 'quiz', $attemptobj->view_url());
  | 
        
        
            | 
            | 
           118 | 
           }
  | 
        
        
            | 
            | 
           119 | 
              | 
        
        
            | 
            | 
           120 | 
           // Update attempt page, redirecting the user if $page is not valid.
  | 
        
        
            | 
            | 
           121 | 
           if (!$attemptobj->set_currentpage($page)) {
  | 
        
        
            | 
            | 
           122 | 
               redirect($attemptobj->start_attempt_url(null, $attemptobj->get_currentpage()));
  | 
        
        
            | 
            | 
           123 | 
           }
  | 
        
        
            | 
            | 
           124 | 
              | 
        
        
            | 
            | 
           125 | 
           if ($attemptobj->is_own_preview()) {
  | 
        
        
            | 
            | 
           126 | 
               $attemptobj->update_questions_to_new_version_if_changed();
  | 
        
        
            | 
            | 
           127 | 
           }
  | 
        
        
            | 
            | 
           128 | 
              | 
        
        
            | 
            | 
           129 | 
           // Initialise the JavaScript.
  | 
        
        
            | 
            | 
           130 | 
           $headtags = $attemptobj->get_html_head_contributions($page);
  | 
        
        
            | 
            | 
           131 | 
           $PAGE->requires->js_init_call('M.mod_quiz.init_attempt_form', null, false, quiz_get_js_module());
  | 
        
        
            | 
            | 
           132 | 
           \core\session\manager::keepalive(); // Try to prevent sessions expiring during quiz attempts.
  | 
        
        
            | 
            | 
           133 | 
              | 
        
        
            | 
            | 
           134 | 
           // Arrange for the navigation to be displayed in the first region on the page.
  | 
        
        
            | 
            | 
           135 | 
           $navbc = $attemptobj->get_navigation_panel($output, navigation_panel_attempt::class, $page);
  | 
        
        
            | 
            | 
           136 | 
           $regions = $PAGE->blocks->get_regions();
  | 
        
        
            | 
            | 
           137 | 
           $PAGE->blocks->add_fake_block($navbc, reset($regions));
  | 
        
        
            | 
            | 
           138 | 
              | 
        
        
            | 
            | 
           139 | 
           $headtags = $attemptobj->get_html_head_contributions($page);
  | 
        
        
            | 
            | 
           140 | 
           $PAGE->set_title($attemptobj->attempt_page_title($page));
  | 
        
        
            | 
            | 
           141 | 
           $PAGE->add_body_class('limitedwidth');
  | 
        
        
            | 
            | 
           142 | 
           $PAGE->set_heading($attemptobj->get_course()->fullname);
  | 
        
        
           | 1441 | 
           ariadna | 
           143 | 
           if ($PAGE->pagelayout === 'secure') {
  | 
        
        
            | 
            | 
           144 | 
               // Show the activity header (but only the name) in the secure layout on quiz pages.
  | 
        
        
            | 
            | 
           145 | 
               $PAGE->activityheader->set_attrs([
  | 
        
        
            | 
            | 
           146 | 
                   'description' => '',
  | 
        
        
            | 
            | 
           147 | 
               ]);
  | 
        
        
            | 
            | 
           148 | 
           } else {
  | 
        
        
            | 
            | 
           149 | 
               $PAGE->activityheader->disable();
  | 
        
        
            | 
            | 
           150 | 
           }
  | 
        
        
           | 1 | 
           efrain | 
           151 | 
           if ($attemptobj->is_last_page($page)) {
  | 
        
        
            | 
            | 
           152 | 
               $nextpage = -1;
  | 
        
        
            | 
            | 
           153 | 
           } else {
  | 
        
        
            | 
            | 
           154 | 
               $nextpage = $page + 1;
  | 
        
        
            | 
            | 
           155 | 
           }
  | 
        
        
            | 
            | 
           156 | 
              | 
        
        
            | 
            | 
           157 | 
           echo $output->attempt_page($attemptobj, $page, $accessmanager, $messages, $slots, $id, $nextpage);
  |