Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 401 and 402] [Versions 401 and 403]

   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 page prints a review of a particular quiz attempt
  19   *
  20   * It is used either by the student whose attempts this is, after the attempt,
  21   * or by a teacher reviewing another's attempt during or afterwards.
  22   *
  23   * @package   mod_quiz
  24   * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
  25   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  
  29  require_once(__DIR__ . '/../../config.php');
  30  require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  31  require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
  32  
  33  $attemptid = required_param('attempt', PARAM_INT);
  34  $page      = optional_param('page', 0, PARAM_INT);
  35  $showall   = optional_param('showall', null, PARAM_BOOL);
  36  $cmid      = optional_param('cmid', null, PARAM_INT);
  37  
  38  $url = new moodle_url('/mod/quiz/review.php', array('attempt'=>$attemptid));
  39  if ($page !== 0) {
  40      $url->param('page', $page);
  41  } else if ($showall) {
  42      $url->param('showall', $showall);
  43  }
  44  $PAGE->set_url($url);
  45  $PAGE->set_secondary_active_tab("modulepage");
  46  
  47  $attemptobj = quiz_create_attempt_handling_errors($attemptid, $cmid);
  48  $attemptobj->preload_all_attempt_step_users();
  49  $page = $attemptobj->force_page_number_into_range($page);
  50  
  51  // Now we can validate the params better, re-genrate the page URL.
  52  if ($showall === null) {
  53      $showall = $page == 0 && $attemptobj->get_default_show_all('review');
  54  }
  55  $PAGE->set_url($attemptobj->review_url(null, $page, $showall));
  56  
  57  // Check login.
  58  require_login($attemptobj->get_course(), false, $attemptobj->get_cm());
  59  $attemptobj->check_review_capability();
  60  
  61  // Create an object to manage all the other (non-roles) access rules.
  62  $accessmanager = $attemptobj->get_access_manager(time());
  63  $accessmanager->setup_attempt_page($PAGE);
  64  
  65  $options = $attemptobj->get_display_options(true);
  66  
  67  // Check permissions - warning there is similar code in reviewquestion.php and
  68  // quiz_attempt::check_file_access. If you change on, change them all.
  69  if ($attemptobj->is_own_attempt()) {
  70      if (!$attemptobj->is_finished()) {
  71          redirect($attemptobj->attempt_url(null, $page));
  72  
  73      } else if (!$options->attempt) {
  74          $accessmanager->back_to_view_page($PAGE->get_renderer('mod_quiz'),
  75                  $attemptobj->cannot_review_message());
  76      }
  77  
  78  } else if (!$attemptobj->is_review_allowed()) {
  79      throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'noreviewattempt');
  80  }
  81  
  82  // Load the questions and states needed by this page.
  83  if ($showall) {
  84      $questionids = $attemptobj->get_slots();
  85  } else {
  86      $questionids = $attemptobj->get_slots($page);
  87  }
  88  
  89  // Save the flag states, if they are being changed.
  90  if ($options->flags == question_display_options::EDITABLE && optional_param('savingflags', false,
  91          PARAM_BOOL)) {
  92      require_sesskey();
  93      $attemptobj->save_question_flags();
  94      redirect($attemptobj->review_url(null, $page, $showall));
  95  }
  96  
  97  // Work out appropriate title and whether blocks should be shown.
  98  if ($attemptobj->is_own_preview()) {
  99      navigation_node::override_active_url($attemptobj->start_attempt_url());
 100  
 101  } else {
 102      if (empty($attemptobj->get_quiz()->showblocks) && !$attemptobj->is_preview_user()) {
 103          $PAGE->blocks->show_only_fake_blocks();
 104      }
 105  }
 106  
 107  // Set up the page header.
 108  $headtags = $attemptobj->get_html_head_contributions($page, $showall);
 109  $PAGE->set_title($attemptobj->review_page_title($page, $showall));
 110  $PAGE->set_heading($attemptobj->get_course()->fullname);
 111  $PAGE->activityheader->disable();
 112  
 113  // Summary table start. ============================================================================
 114  
 115  // Work out some time-related things.
 116  $attempt = $attemptobj->get_attempt();
 117  $quiz = $attemptobj->get_quiz();
 118  $overtime = 0;
 119  
 120  if ($attempt->state == quiz_attempt::FINISHED) {
 121      if ($timetaken = ($attempt->timefinish - $attempt->timestart)) {
 122          if ($quiz->timelimit && $timetaken > ($quiz->timelimit + 60)) {
 123              $overtime = $timetaken - $quiz->timelimit;
 124              $overtime = format_time($overtime);
 125          }
 126          $timetaken = format_time($timetaken);
 127      } else {
 128          $timetaken = "-";
 129      }
 130  } else {
 131      $timetaken = get_string('unfinished', 'quiz');
 132  }
 133  
 134  // Prepare summary informat about the whole attempt.
 135  $summarydata = array();
 136  if (!$attemptobj->get_quiz()->showuserpicture && $attemptobj->get_userid() != $USER->id) {
 137      // If showuserpicture is true, the picture is shown elsewhere, so don't repeat it.
 138      $student = $DB->get_record('user', array('id' => $attemptobj->get_userid()));
 139      $userpicture = new user_picture($student);
 140      $userpicture->courseid = $attemptobj->get_courseid();
 141      $summarydata['user'] = array(
 142          'title'   => $userpicture,
 143          'content' => new action_link(new moodle_url('/user/view.php', array(
 144                                  'id' => $student->id, 'course' => $attemptobj->get_courseid())),
 145                            fullname($student, true)),
 146      );
 147  }
 148  
 149  if ($attemptobj->has_capability('mod/quiz:viewreports')) {
 150      $attemptlist = $attemptobj->links_to_other_attempts($attemptobj->review_url(null, $page,
 151              $showall));
 152      if ($attemptlist) {
 153          $summarydata['attemptlist'] = array(
 154              'title'   => get_string('attempts', 'quiz'),
 155              'content' => $attemptlist,
 156          );
 157      }
 158  }
 159  
 160  // Timing information.
 161  $summarydata['startedon'] = array(
 162      'title'   => get_string('startedon', 'quiz'),
 163      'content' => userdate($attempt->timestart),
 164  );
 165  
 166  $summarydata['state'] = array(
 167      'title'   => get_string('attemptstate', 'quiz'),
 168      'content' => quiz_attempt::state_name($attempt->state),
 169  );
 170  
 171  if ($attempt->state == quiz_attempt::FINISHED) {
 172      $summarydata['completedon'] = array(
 173          'title'   => get_string('completedon', 'quiz'),
 174          'content' => userdate($attempt->timefinish),
 175      );
 176      $summarydata['timetaken'] = array(
 177          'title'   => get_string('timetaken', 'quiz'),
 178          'content' => $timetaken,
 179      );
 180  }
 181  
 182  if (!empty($overtime)) {
 183      $summarydata['overdue'] = array(
 184          'title'   => get_string('overdue', 'quiz'),
 185          'content' => $overtime,
 186      );
 187  }
 188  
 189  // Show marks (if the user is allowed to see marks at the moment).
 190  $grade = quiz_rescale_grade($attempt->sumgrades, $quiz, false);
 191  if ($options->marks >= question_display_options::MARK_AND_MAX && quiz_has_grades($quiz)) {
 192  
 193      if ($attempt->state != quiz_attempt::FINISHED) {
 194          // Cannot display grade.
 195  
 196      } else if (is_null($grade)) {
 197          $summarydata['grade'] = array(
 198              'title'   => get_string('grade', 'quiz'),
 199              'content' => quiz_format_grade($quiz, $grade),
 200          );
 201  
 202      } else {
 203          // Show raw marks only if they are different from the grade (like on the view page).
 204          if ($quiz->grade != $quiz->sumgrades) {
 205              $a = new stdClass();
 206              $a->grade = quiz_format_grade($quiz, $attempt->sumgrades);
 207              $a->maxgrade = quiz_format_grade($quiz, $quiz->sumgrades);
 208              $summarydata['marks'] = array(
 209                  'title'   => get_string('marks', 'quiz'),
 210                  'content' => get_string('outofshort', 'quiz', $a),
 211              );
 212          }
 213  
 214          // Now the scaled grade.
 215          $a = new stdClass();
 216          $a->grade = html_writer::tag('b', quiz_format_grade($quiz, $grade));
 217          $a->maxgrade = quiz_format_grade($quiz, $quiz->grade);
 218          if ($quiz->grade != 100) {
 219              // Show the percentage using the configured number of decimal places,
 220              // but without trailing zeroes.
 221              $a->percent = html_writer::tag('b', format_float(
 222                      $attempt->sumgrades * 100 / $quiz->sumgrades,
 223                      $quiz->decimalpoints, true, true));
 224              $formattedgrade = get_string('outofpercent', 'quiz', $a);
 225          } else {
 226              $formattedgrade = get_string('outof', 'quiz', $a);
 227          }
 228          $summarydata['grade'] = array(
 229              'title'   => get_string('grade', 'quiz'),
 230              'content' => $formattedgrade,
 231          );
 232      }
 233  }
 234  
 235  // Any additional summary data from the behaviour.
 236  $summarydata = array_merge($summarydata, $attemptobj->get_additional_summary_data($options));
 237  
 238  // Feedback if there is any, and the user is allowed to see it now.
 239  $feedback = $attemptobj->get_overall_feedback($grade);
 240  if ($options->overallfeedback && $feedback) {
 241      $summarydata['feedback'] = array(
 242          'title'   => get_string('feedback', 'quiz'),
 243          'content' => $feedback,
 244      );
 245  }
 246  
 247  // Summary table end. ==============================================================================
 248  
 249  if ($showall) {
 250      $slots = $attemptobj->get_slots();
 251      $lastpage = true;
 252  } else {
 253      $slots = $attemptobj->get_slots($page);
 254      $lastpage = $attemptobj->is_last_page($page);
 255  }
 256  
 257  $output = $PAGE->get_renderer('mod_quiz');
 258  
 259  // Arrange for the navigation to be displayed.
 260  $navbc = $attemptobj->get_navigation_panel($output, 'quiz_review_nav_panel', $page, $showall);
 261  $regions = $PAGE->blocks->get_regions();
 262  $PAGE->blocks->add_fake_block($navbc, reset($regions));
 263  
 264  echo $output->review_page($attemptobj, $slots, $page, $showall, $lastpage, $options, $summarydata);
 265  
 266  // Trigger an event for this review.
 267  $attemptobj->fire_attempt_reviewed_event();