[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/quiz/ -> override_form.php (source)

   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 $CFG, $DB;
  82  
  83          $cm = $this->cm;
  84          $mform = $this->_form;
  85  
  86          $mform->addElement('header', 'override', get_string('override', 'quiz'));
  87  
  88          if ($this->groupmode) {
  89              // Group override.
  90              if ($this->groupid) {
  91                  // There is already a groupid, so freeze the selector.
  92                  $groupchoices = array();
  93                  $groupchoices[$this->groupid] = groups_get_group_name($this->groupid);
  94                  $mform->addElement('select', 'groupid',
  95                          get_string('overridegroup', 'quiz'), $groupchoices);
  96                  $mform->freeze('groupid');
  97              } else {
  98                  // Prepare the list of groups.
  99                  $groups = groups_get_all_groups($cm->course);
 100                  if (empty($groups)) {
 101                      // Generate an error.
 102                      $link = new moodle_url('/mod/quiz/overrides.php', array('cmid'=>$cm->id));
 103                      print_error('groupsnone', 'quiz', $link);
 104                  }
 105  
 106                  $groupchoices = array();
 107                  foreach ($groups as $group) {
 108                      $groupchoices[$group->id] = $group->name;
 109                  }
 110                  unset($groups);
 111  
 112                  if (count($groupchoices) == 0) {
 113                      $groupchoices[0] = get_string('none');
 114                  }
 115  
 116                  $mform->addElement('select', 'groupid',
 117                          get_string('overridegroup', 'quiz'), $groupchoices);
 118                  $mform->addRule('groupid', get_string('required'), 'required', null, 'client');
 119              }
 120          } else {
 121              // User override.
 122              if ($this->userid) {
 123                  // There is already a userid, so freeze the selector.
 124                  $user = $DB->get_record('user', array('id'=>$this->userid));
 125                  $userchoices = array();
 126                  $userchoices[$this->userid] = fullname($user);
 127                  $mform->addElement('select', 'userid',
 128                          get_string('overrideuser', 'quiz'), $userchoices);
 129                  $mform->freeze('userid');
 130              } else {
 131                  // Prepare the list of users.
 132                  $users = array();
 133                  list($sort, $sortparams) = users_order_by_sql('u');
 134                  if (!empty($sortparams)) {
 135                      throw new coding_exception('users_order_by_sql returned some query parameters. ' .
 136                              'This is unexpected, and a problem because there is no way to pass these ' .
 137                              'parameters to get_users_by_capability. See MDL-34657.');
 138                  }
 139                  $users = get_users_by_capability($this->context, 'mod/quiz:attempt',
 140                          'u.id, u.email, ' . get_all_user_name_fields(true, 'u'),
 141                          $sort, '', '', '', '', false, true);
 142  
 143                  // Filter users based on any fixed restrictions (groups, profile).
 144                  $info = new \core_availability\info_module($cm);
 145                  $users = $info->filter_user_list($users);
 146  
 147                  if (empty($users)) {
 148                      // Generate an error.
 149                      $link = new moodle_url('/mod/quiz/overrides.php', array('cmid'=>$cm->id));
 150                      print_error('usersnone', 'quiz', $link);
 151                  }
 152  
 153                  $userchoices = array();
 154                  $canviewemail = in_array('email', get_extra_user_fields($this->context));
 155                  foreach ($users as $id => $user) {
 156                      if (empty($invalidusers[$id]) || (!empty($override) &&
 157                              $id == $override->userid)) {
 158                          if ($canviewemail) {
 159                              $userchoices[$id] = fullname($user) . ', ' . $user->email;
 160                          } else {
 161                              $userchoices[$id] = fullname($user);
 162                          }
 163                      }
 164                  }
 165                  unset($users);
 166  
 167                  if (count($userchoices) == 0) {
 168                      $userchoices[0] = get_string('none');
 169                  }
 170                  $mform->addElement('searchableselector', 'userid',
 171                          get_string('overrideuser', 'quiz'), $userchoices);
 172                  $mform->addRule('userid', get_string('required'), 'required', null, 'client');
 173              }
 174          }
 175  
 176          // Password.
 177          // This field has to be above the date and timelimit fields,
 178          // otherwise browsers will clear it when those fields are changed.
 179          $mform->addElement('passwordunmask', 'password', get_string('requirepassword', 'quiz'));
 180          $mform->setType('password', PARAM_TEXT);
 181          $mform->addHelpButton('password', 'requirepassword', 'quiz');
 182          $mform->setDefault('password', $this->quiz->password);
 183  
 184          // Open and close dates.
 185          $mform->addElement('date_time_selector', 'timeopen',
 186                  get_string('quizopen', 'quiz'), mod_quiz_mod_form::$datefieldoptions);
 187          $mform->setDefault('timeopen', $this->quiz->timeopen);
 188  
 189          $mform->addElement('date_time_selector', 'timeclose',
 190                  get_string('quizclose', 'quiz'), mod_quiz_mod_form::$datefieldoptions);
 191          $mform->setDefault('timeclose', $this->quiz->timeclose);
 192  
 193          // Time limit.
 194          $mform->addElement('duration', 'timelimit',
 195                  get_string('timelimit', 'quiz'), array('optional' => true));
 196          $mform->addHelpButton('timelimit', 'timelimit', 'quiz');
 197          $mform->setDefault('timelimit', $this->quiz->timelimit);
 198  
 199          // Number of attempts.
 200          $attemptoptions = array('0' => get_string('unlimited'));
 201          for ($i = 1; $i <= QUIZ_MAX_ATTEMPT_OPTION; $i++) {
 202              $attemptoptions[$i] = $i;
 203          }
 204          $mform->addElement('select', 'attempts',
 205                  get_string('attemptsallowed', 'quiz'), $attemptoptions);
 206          $mform->setDefault('attempts', $this->quiz->attempts);
 207  
 208          // Submit buttons.
 209          $mform->addElement('submit', 'resetbutton',
 210                  get_string('reverttodefaults', 'quiz'));
 211  
 212          $buttonarray = array();
 213          $buttonarray[] = $mform->createElement('submit', 'submitbutton',
 214                  get_string('save', 'quiz'));
 215          $buttonarray[] = $mform->createElement('submit', 'againbutton',
 216                  get_string('saveoverrideandstay', 'quiz'));
 217          $buttonarray[] = $mform->createElement('cancel');
 218  
 219          $mform->addGroup($buttonarray, 'buttonbar', '', array(' '), false);
 220          $mform->closeHeaderBefore('buttonbar');
 221  
 222      }
 223  
 224      public function validation($data, $files) {
 225          global $COURSE, $DB;
 226          $errors = parent::validation($data, $files);
 227  
 228          $mform =& $this->_form;
 229          $quiz = $this->quiz;
 230  
 231          if ($mform->elementExists('userid')) {
 232              if (empty($data['userid'])) {
 233                  $errors['userid'] = get_string('required');
 234              }
 235          }
 236  
 237          if ($mform->elementExists('groupid')) {
 238              if (empty($data['groupid'])) {
 239                  $errors['groupid'] = get_string('required');
 240              }
 241          }
 242  
 243          // Ensure that the dates make sense.
 244          if (!empty($data['timeopen']) && !empty($data['timeclose'])) {
 245              if ($data['timeclose'] < $data['timeopen'] ) {
 246                  $errors['timeclose'] = get_string('closebeforeopen', 'quiz');
 247              }
 248          }
 249  
 250          // Ensure that at least one quiz setting was changed.
 251          $changed = false;
 252          $keys = array('timeopen', 'timeclose', 'timelimit', 'attempts', 'password');
 253          foreach ($keys as $key) {
 254              if ($data[$key] != $quiz->{$key}) {
 255                  $changed = true;
 256                  break;
 257              }
 258          }
 259          if (!$changed) {
 260              $errors['timeopen'] = get_string('nooverridedata', 'quiz');
 261          }
 262  
 263          return $errors;
 264      }
 265  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1