Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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 handles editing and creation of quiz overrides
  19   *
  20   * @package   mod_quiz
  21   * @copyright 2010 Matt Petro
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  
  26  require_once(__DIR__ . '/../../config.php');
  27  require_once($CFG->dirroot.'/mod/quiz/lib.php');
  28  require_once($CFG->dirroot.'/mod/quiz/locallib.php');
  29  require_once($CFG->dirroot.'/mod/quiz/override_form.php');
  30  
  31  
  32  $cmid = optional_param('cmid', 0, PARAM_INT);
  33  $overrideid = optional_param('id', 0, PARAM_INT);
  34  $action = optional_param('action', null, PARAM_ALPHA);
  35  $reset = optional_param('reset', false, PARAM_BOOL);
  36  
  37  $override = null;
  38  if ($overrideid) {
  39  
  40      if (! $override = $DB->get_record('quiz_overrides', array('id' => $overrideid))) {
  41          print_error('invalidoverrideid', 'quiz');
  42      }
  43      if (! $quiz = $DB->get_record('quiz', array('id' => $override->quiz))) {
  44          print_error('invalidcoursemodule');
  45      }
  46      list($course, $cm) = get_course_and_cm_from_instance($quiz, 'quiz');
  47  
  48  } else if ($cmid) {
  49      list($course, $cm) = get_course_and_cm_from_cmid($cmid, 'quiz');
  50      $quiz = $DB->get_record('quiz', array('id' => $cm->instance), '*', MUST_EXIST);
  51  
  52  } else {
  53      print_error('invalidcoursemodule');
  54  }
  55  $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
  56  
  57  $url = new moodle_url('/mod/quiz/overrideedit.php');
  58  if ($action) {
  59      $url->param('action', $action);
  60  }
  61  if ($overrideid) {
  62      $url->param('id', $overrideid);
  63  } else {
  64      $url->param('cmid', $cmid);
  65  }
  66  
  67  $PAGE->set_url($url);
  68  
  69  require_login($course, false, $cm);
  70  
  71  $context = context_module::instance($cm->id);
  72  
  73  // Add or edit an override.
  74  require_capability('mod/quiz:manageoverrides', $context);
  75  
  76  if ($overrideid) {
  77      // Editing an override.
  78      $data = clone $override;
  79  
  80      if ($override->groupid) {
  81          if (!groups_group_visible($override->groupid, $course, $cm)) {
  82              print_error('invalidoverrideid', 'quiz');
  83          }
  84      } else {
  85          if (!groups_user_groups_visible($course, $override->userid, $cm)) {
  86              print_error('invalidoverrideid', 'quiz');
  87          }
  88      }
  89  } else {
  90      // Creating a new override.
  91      $data = new stdClass();
  92  }
  93  
  94  // Merge quiz defaults with data.
  95  $keys = array('timeopen', 'timeclose', 'timelimit', 'attempts', 'password');
  96  foreach ($keys as $key) {
  97      if (!isset($data->{$key}) || $reset) {
  98          $data->{$key} = $quiz->{$key};
  99      }
 100  }
 101  
 102  // If we are duplicating an override, then clear the user/group and override id
 103  // since they will change.
 104  if ($action === 'duplicate') {
 105      $override->id = null;
 106      $override->userid = null;
 107      $override->groupid = null;
 108  }
 109  
 110  // True if group-based override.
 111  $groupmode = !empty($data->groupid) || ($action === 'addgroup' && empty($overrideid));
 112  
 113  $overridelisturl = new moodle_url('/mod/quiz/overrides.php', array('cmid'=>$cm->id));
 114  if (!$groupmode) {
 115      $overridelisturl->param('mode', 'user');
 116  }
 117  
 118  // Setup the form.
 119  $mform = new quiz_override_form($url, $cm, $quiz, $context, $groupmode, $override);
 120  $mform->set_data($data);
 121  
 122  if ($mform->is_cancelled()) {
 123      redirect($overridelisturl);
 124  
 125  } else if (optional_param('resetbutton', 0, PARAM_ALPHA)) {
 126      $url->param('reset', true);
 127      redirect($url);
 128  
 129  } else if ($fromform = $mform->get_data()) {
 130      // Process the data.
 131      $fromform->quiz = $quiz->id;
 132  
 133      // Replace unchanged values with null.
 134      foreach ($keys as $key) {
 135          if ($fromform->{$key} == $quiz->{$key}) {
 136              $fromform->{$key} = null;
 137          }
 138      }
 139  
 140      // See if we are replacing an existing override.
 141      $userorgroupchanged = false;
 142      if (empty($override->id)) {
 143          $userorgroupchanged = true;
 144      } else if (!empty($fromform->userid)) {
 145          $userorgroupchanged = $fromform->userid !== $override->userid;
 146      } else {
 147          $userorgroupchanged = $fromform->groupid !== $override->groupid;
 148      }
 149  
 150      if ($userorgroupchanged) {
 151          $conditions = array(
 152                  'quiz' => $quiz->id,
 153                  'userid' => empty($fromform->userid)? null : $fromform->userid,
 154                  'groupid' => empty($fromform->groupid)? null : $fromform->groupid);
 155          if ($oldoverride = $DB->get_record('quiz_overrides', $conditions)) {
 156              // There is an old override, so we merge any new settings on top of
 157              // the older override.
 158              foreach ($keys as $key) {
 159                  if (is_null($fromform->{$key})) {
 160                      $fromform->{$key} = $oldoverride->{$key};
 161                  }
 162              }
 163              // Set the course module id before calling quiz_delete_override().
 164              $quiz->cmid = $cm->id;
 165              quiz_delete_override($quiz, $oldoverride->id);
 166          }
 167      }
 168  
 169      // Set the common parameters for one of the events we may be triggering.
 170      $params = array(
 171          'context' => $context,
 172          'other' => array(
 173              'quizid' => $quiz->id
 174          )
 175      );
 176      if (!empty($override->id)) {
 177          $fromform->id = $override->id;
 178          $DB->update_record('quiz_overrides', $fromform);
 179          $cachekey = $groupmode ? "{$fromform->quiz}_g_{$fromform->groupid}" : "{$fromform->quiz}_u_{$fromform->userid}";
 180          cache::make('mod_quiz', 'overrides')->delete($cachekey);
 181  
 182          // Determine which override updated event to fire.
 183          $params['objectid'] = $override->id;
 184          if (!$groupmode) {
 185              $params['relateduserid'] = $fromform->userid;
 186              $event = \mod_quiz\event\user_override_updated::create($params);
 187          } else {
 188              $params['other']['groupid'] = $fromform->groupid;
 189              $event = \mod_quiz\event\group_override_updated::create($params);
 190          }
 191  
 192          // Trigger the override updated event.
 193          $event->trigger();
 194      } else {
 195          unset($fromform->id);
 196          $fromform->id = $DB->insert_record('quiz_overrides', $fromform);
 197          $cachekey = $groupmode ? "{$fromform->quiz}_g_{$fromform->groupid}" : "{$fromform->quiz}_u_{$fromform->userid}";
 198          cache::make('mod_quiz', 'overrides')->delete($cachekey);
 199  
 200          // Determine which override created event to fire.
 201          $params['objectid'] = $fromform->id;
 202          if (!$groupmode) {
 203              $params['relateduserid'] = $fromform->userid;
 204              $event = \mod_quiz\event\user_override_created::create($params);
 205          } else {
 206              $params['other']['groupid'] = $fromform->groupid;
 207              $event = \mod_quiz\event\group_override_created::create($params);
 208          }
 209  
 210          // Trigger the override created event.
 211          $event->trigger();
 212      }
 213  
 214      quiz_update_open_attempts(array('quizid'=>$quiz->id));
 215      if ($groupmode) {
 216          // Priorities may have shifted, so we need to update all of the calendar events for group overrides.
 217          quiz_update_events($quiz);
 218      } else {
 219          // User override. We only need to update the calendar event for this user override.
 220          quiz_update_events($quiz, $fromform);
 221      }
 222  
 223      if (!empty($fromform->submitbutton)) {
 224          redirect($overridelisturl);
 225      }
 226  
 227      // The user pressed the 'again' button, so redirect back to this page.
 228      $url->remove_params('cmid');
 229      $url->param('action', 'duplicate');
 230      $url->param('id', $fromform->id);
 231      redirect($url);
 232  
 233  }
 234  
 235  // Print the form.
 236  $pagetitle = get_string('editoverride', 'quiz');
 237  $PAGE->navbar->add($pagetitle);
 238  $PAGE->set_pagelayout('admin');
 239  $PAGE->set_title($pagetitle);
 240  $PAGE->set_heading($course->fullname);
 241  echo $OUTPUT->header();
 242  echo $OUTPUT->heading(format_string($quiz->name, true, array('context' => $context)));
 243  
 244  $mform->display();
 245  
 246  echo $OUTPUT->footer();