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   * Settings form for overrides in the quiz module.
  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  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once($CFG->libdir . '/formslib.php');
  29  require_once($CFG->dirroot . '/mod/quiz/mod_form.php');
  30  
  31  
  32  /**
  33   * Form for editing settings overrides.
  34   *
  35   * @copyright  2010 Matt Petro
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class quiz_override_form extends moodleform {
  39  
  40      /** @var object course module object. */
  41      protected $cm;
  42  
  43      /** @var object the quiz settings object. */
  44      protected $quiz;
  45  
  46      /** @var context the quiz context. */
  47      protected $context;
  48  
  49      /** @var bool editing group override (true) or user override (false). */
  50      protected $groupmode;
  51  
  52      /** @var int groupid, if provided. */
  53      protected $groupid;
  54  
  55      /** @var int userid, if provided. */
  56      protected $userid;
  57  
  58      /**
  59       * Constructor.
  60       * @param moodle_url $submiturl the form action URL.
  61       * @param object course module object.
  62       * @param object the quiz settings object.
  63       * @param context the quiz context.
  64       * @param bool editing group override (true) or user override (false).
  65       * @param object $override the override being edited, if it already exists.
  66       */
  67      public function __construct($submiturl, $cm, $quiz, $context, $groupmode, $override) {
  68  
  69          $this->cm = $cm;
  70          $this->quiz = $quiz;
  71          $this->context = $context;
  72          $this->groupmode = $groupmode;
  73          $this->groupid = empty($override->groupid) ? 0 : $override->groupid;
  74          $this->userid = empty($override->userid) ? 0 : $override->userid;
  75  
  76          parent::__construct($submiturl, null, 'post');
  77  
  78      }
  79  
  80      protected function definition() {
  81          global $DB;
  82  
  83          $cm = $this->cm;
  84          $mform = $this->_form;
  85  
  86          $mform->addElement('header', 'override', get_string('override', 'quiz'));
  87  
  88          $quizgroupmode = groups_get_activity_groupmode($cm);
  89          $accessallgroups = ($quizgroupmode == NOGROUPS) || has_capability('moodle/site:accessallgroups', $this->context);
  90  
  91          if ($this->groupmode) {
  92              // Group override.
  93              if ($this->groupid) {
  94                  // There is already a groupid, so freeze the selector.
  95                  $groupchoices = array();
  96                  $groupchoices[$this->groupid] = groups_get_group_name($this->groupid);
  97                  $mform->addElement('select', 'groupid',
  98                          get_string('overridegroup', 'quiz'), $groupchoices);
  99                  $mform->freeze('groupid');
 100              } else {
 101                  // Prepare the list of groups.
 102                  // Only include the groups the current can access.
 103                  $groups = $accessallgroups ? groups_get_all_groups($cm->course) : groups_get_activity_allowed_groups($cm);
 104                  if (empty($groups)) {
 105                      // Generate an error.
 106                      $link = new moodle_url('/mod/quiz/overrides.php', array('cmid'=>$cm->id));
 107                      print_error('groupsnone', 'quiz', $link);
 108                  }
 109  
 110                  $groupchoices = array();
 111                  foreach ($groups as $group) {
 112                      $groupchoices[$group->id] = $group->name;
 113                  }
 114                  unset($groups);
 115  
 116                  if (count($groupchoices) == 0) {
 117                      $groupchoices[0] = get_string('none');
 118                  }
 119  
 120                  $mform->addElement('select', 'groupid',
 121                          get_string('overridegroup', 'quiz'), $groupchoices);
 122                  $mform->addRule('groupid', get_string('required'), 'required', null, 'client');
 123              }
 124          } else {
 125              // User override.
 126              // TODO Does not support custom user profile fields (MDL-70456).
 127              $userfieldsapi = \core_user\fields::for_identity($this->context, false)->with_userpic()->with_name();
 128              $extrauserfields = $userfieldsapi->get_required_fields([\core_user\fields::PURPOSE_IDENTITY]);
 129              if ($this->userid) {
 130                  // There is already a userid, so freeze the selector.
 131                  $user = $DB->get_record('user', ['id' => $this->userid]);
 132                  $userchoices = array();
 133                  $userchoices[$this->userid] = $this->display_user_name($user, $extrauserfields);
 134                  $mform->addElement('select', 'userid',
 135                          get_string('overrideuser', 'quiz'), $userchoices);
 136                  $mform->freeze('userid');
 137              } else {
 138                  // Prepare the list of users.
 139                  $users = array();
 140                  list($sort, $sortparams) = users_order_by_sql('u');
 141                  if (!empty($sortparams)) {
 142                      throw new coding_exception('users_order_by_sql returned some query parameters. ' .
 143                              'This is unexpected, and a problem because there is no way to pass these ' .
 144                              'parameters to get_users_by_capability. See MDL-34657.');
 145                  }
 146  
 147                  // Get the list of appropriate users, depending on whether and how groups are used.
 148                  $userfields = $userfieldsapi->get_sql('u', false, '', 'userid', false)->selects;
 149                  if ($accessallgroups) {
 150                      $users = get_users_by_capability($this->context, 'mod/quiz:attempt',
 151                              $userfields, $sort);
 152                  } else if ($groups = groups_get_activity_allowed_groups($cm)) {
 153                      $users = get_users_by_capability($this->context, 'mod/quiz:attempt',
 154                              $userfields, $sort, '', '', array_keys($groups));
 155                  }
 156  
 157                  // Filter users based on any fixed restrictions (groups, profile).
 158                  $info = new \core_availability\info_module($cm);
 159                  $users = $info->filter_user_list($users);
 160  
 161                  if (empty($users)) {
 162                      // Generate an error.
 163                      $link = new moodle_url('/mod/quiz/overrides.php', array('cmid'=>$cm->id));
 164                      print_error('usersnone', 'quiz', $link);
 165                  }
 166  
 167                  $userchoices = [];
 168                  foreach ($users as $id => $user) {
 169                      $userchoices[$id] = $this->display_user_name($user, $extrauserfields);
 170                  }
 171                  unset($users);
 172  
 173                  $mform->addElement('searchableselector', 'userid',
 174                          get_string('overrideuser', 'quiz'), $userchoices);
 175                  $mform->addRule('userid', get_string('required'), 'required', null, 'client');
 176              }
 177          }
 178  
 179          // Password.
 180          // This field has to be above the date and timelimit fields,
 181          // otherwise browsers will clear it when those fields are changed.
 182          $mform->addElement('passwordunmask', 'password', get_string('requirepassword', 'quiz'));
 183          $mform->setType('password', PARAM_TEXT);
 184          $mform->addHelpButton('password', 'requirepassword', 'quiz');
 185          $mform->setDefault('password', $this->quiz->password);
 186  
 187          // Open and close dates.
 188          $mform->addElement('date_time_selector', 'timeopen',
 189                  get_string('quizopen', 'quiz'), mod_quiz_mod_form::$datefieldoptions);
 190          $mform->setDefault('timeopen', $this->quiz->timeopen);
 191  
 192          $mform->addElement('date_time_selector', 'timeclose',
 193                  get_string('quizclose', 'quiz'), mod_quiz_mod_form::$datefieldoptions);
 194          $mform->setDefault('timeclose', $this->quiz->timeclose);
 195  
 196          // Time limit.
 197          $mform->addElement('duration', 'timelimit',
 198                  get_string('timelimit', 'quiz'), array('optional' => true));
 199          $mform->addHelpButton('timelimit', 'timelimit', 'quiz');
 200          $mform->setDefault('timelimit', $this->quiz->timelimit);
 201  
 202          // Number of attempts.
 203          $attemptoptions = array('0' => get_string('unlimited'));
 204          for ($i = 1; $i <= QUIZ_MAX_ATTEMPT_OPTION; $i++) {
 205              $attemptoptions[$i] = $i;
 206          }
 207          $mform->addElement('select', 'attempts',
 208                  get_string('attemptsallowed', 'quiz'), $attemptoptions);
 209          $mform->addHelpButton('attempts', 'attempts', 'quiz');
 210          $mform->setDefault('attempts', $this->quiz->attempts);
 211  
 212          // Submit buttons.
 213          $mform->addElement('submit', 'resetbutton',
 214                  get_string('reverttodefaults', 'quiz'));
 215  
 216          $buttonarray = array();
 217          $buttonarray[] = $mform->createElement('submit', 'submitbutton',
 218                  get_string('save', 'quiz'));
 219          $buttonarray[] = $mform->createElement('submit', 'againbutton',
 220                  get_string('saveoverrideandstay', 'quiz'));
 221          $buttonarray[] = $mform->createElement('cancel');
 222  
 223          $mform->addGroup($buttonarray, 'buttonbar', '', array(' '), false);
 224          $mform->closeHeaderBefore('buttonbar');
 225      }
 226  
 227      /**
 228       * Get a user's name and identity ready to display.
 229       *
 230       * @param stdClass $user a user object.
 231       * @param array $extrauserfields (identity fields in user table only from the user_fields API)
 232       * @return string User's name, with extra info, for display.
 233       */
 234      protected function display_user_name(stdClass $user, array $extrauserfields) {
 235          $username = fullname($user);
 236          $namefields = [];
 237          foreach ($extrauserfields as $field) {
 238              if (isset($user->$field) && $user->$field !== '') {
 239                  $namefields[] = s($user->$field);
 240              }
 241          }
 242          if ($namefields) {
 243              $username .= ' (' . implode(', ', $namefields) . ')';
 244          }
 245          return $username;
 246      }
 247  
 248      public function validation($data, $files) {
 249          $errors = parent::validation($data, $files);
 250  
 251          $mform =& $this->_form;
 252          $quiz = $this->quiz;
 253  
 254          if ($mform->elementExists('userid')) {
 255              if (empty($data['userid'])) {
 256                  $errors['userid'] = get_string('required');
 257              }
 258          }
 259  
 260          if ($mform->elementExists('groupid')) {
 261              if (empty($data['groupid'])) {
 262                  $errors['groupid'] = get_string('required');
 263              }
 264          }
 265  
 266          // Ensure that the dates make sense.
 267          if (!empty($data['timeopen']) && !empty($data['timeclose'])) {
 268              if ($data['timeclose'] < $data['timeopen'] ) {
 269                  $errors['timeclose'] = get_string('closebeforeopen', 'quiz');
 270              }
 271          }
 272  
 273          // Ensure that at least one quiz setting was changed.
 274          $changed = false;
 275          $keys = array('timeopen', 'timeclose', 'timelimit', 'attempts', 'password');
 276          foreach ($keys as $key) {
 277              if ($data[$key] != $quiz->{$key}) {
 278                  $changed = true;
 279                  break;
 280              }
 281          }
 282          if (!$changed) {
 283              $errors['timeopen'] = get_string('nooverridedata', 'quiz');
 284          }
 285  
 286          return $errors;
 287      }
 288  }