See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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 * 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 if ($this->userid) { 127 // There is already a userid, so freeze the selector. 128 $user = $DB->get_record('user', array('id'=>$this->userid)); 129 $userchoices = array(); 130 $userchoices[$this->userid] = fullname($user); 131 $mform->addElement('select', 'userid', 132 get_string('overrideuser', 'quiz'), $userchoices); 133 $mform->freeze('userid'); 134 } else { 135 // Prepare the list of users. 136 $users = array(); 137 list($sort, $sortparams) = users_order_by_sql('u'); 138 if (!empty($sortparams)) { 139 throw new coding_exception('users_order_by_sql returned some query parameters. ' . 140 'This is unexpected, and a problem because there is no way to pass these ' . 141 'parameters to get_users_by_capability. See MDL-34657.'); 142 } 143 144 // Get the list of appropriate users, depending on whether and how groups are used. 145 if ($accessallgroups) { 146 $users = get_users_by_capability($this->context, 'mod/quiz:attempt', 147 'u.id, u.email, ' . get_all_user_name_fields(true, 'u'), 148 $sort); 149 } else if ($groups = groups_get_activity_allowed_groups($cm)) { 150 $users = get_users_by_capability($this->context, 'mod/quiz:attempt', 151 'u.id, u.email, ' . get_all_user_name_fields(true, 'u'), 152 $sort, '', '', array_keys($groups)); 153 } 154 155 // Filter users based on any fixed restrictions (groups, profile). 156 $info = new \core_availability\info_module($cm); 157 $users = $info->filter_user_list($users); 158 159 if (empty($users)) { 160 // Generate an error. 161 $link = new moodle_url('/mod/quiz/overrides.php', array('cmid'=>$cm->id)); 162 print_error('usersnone', 'quiz', $link); 163 } 164 165 $userchoices = array(); 166 $canviewemail = in_array('email', get_extra_user_fields($this->context)); 167 foreach ($users as $id => $user) { 168 if (empty($invalidusers[$id]) || (!empty($override) && 169 $id == $override->userid)) { 170 if ($canviewemail) { 171 $userchoices[$id] = fullname($user) . ', ' . $user->email; 172 } else { 173 $userchoices[$id] = fullname($user); 174 } 175 } 176 } 177 unset($users); 178 179 $mform->addElement('searchableselector', 'userid', 180 get_string('overrideuser', 'quiz'), $userchoices); 181 $mform->addRule('userid', get_string('required'), 'required', null, 'client'); 182 } 183 } 184 185 // Password. 186 // This field has to be above the date and timelimit fields, 187 // otherwise browsers will clear it when those fields are changed. 188 $mform->addElement('passwordunmask', 'password', get_string('requirepassword', 'quiz')); 189 $mform->setType('password', PARAM_TEXT); 190 $mform->addHelpButton('password', 'requirepassword', 'quiz'); 191 $mform->setDefault('password', $this->quiz->password); 192 193 // Open and close dates. 194 $mform->addElement('date_time_selector', 'timeopen', 195 get_string('quizopen', 'quiz'), mod_quiz_mod_form::$datefieldoptions); 196 $mform->setDefault('timeopen', $this->quiz->timeopen); 197 198 $mform->addElement('date_time_selector', 'timeclose', 199 get_string('quizclose', 'quiz'), mod_quiz_mod_form::$datefieldoptions); 200 $mform->setDefault('timeclose', $this->quiz->timeclose); 201 202 // Time limit. 203 $mform->addElement('duration', 'timelimit', 204 get_string('timelimit', 'quiz'), array('optional' => true)); 205 $mform->addHelpButton('timelimit', 'timelimit', 'quiz'); 206 $mform->setDefault('timelimit', $this->quiz->timelimit); 207 208 // Number of attempts. 209 $attemptoptions = array('0' => get_string('unlimited')); 210 for ($i = 1; $i <= QUIZ_MAX_ATTEMPT_OPTION; $i++) { 211 $attemptoptions[$i] = $i; 212 } 213 $mform->addElement('select', 'attempts', 214 get_string('attemptsallowed', 'quiz'), $attemptoptions); 215 $mform->addHelpButton('attempts', 'attempts', 'quiz'); 216 $mform->setDefault('attempts', $this->quiz->attempts); 217 218 // Submit buttons. 219 $mform->addElement('submit', 'resetbutton', 220 get_string('reverttodefaults', 'quiz')); 221 222 $buttonarray = array(); 223 $buttonarray[] = $mform->createElement('submit', 'submitbutton', 224 get_string('save', 'quiz')); 225 $buttonarray[] = $mform->createElement('submit', 'againbutton', 226 get_string('saveoverrideandstay', 'quiz')); 227 $buttonarray[] = $mform->createElement('cancel'); 228 229 $mform->addGroup($buttonarray, 'buttonbar', '', array(' '), false); 230 $mform->closeHeaderBefore('buttonbar'); 231 232 } 233 234 public function validation($data, $files) { 235 $errors = parent::validation($data, $files); 236 237 $mform =& $this->_form; 238 $quiz = $this->quiz; 239 240 if ($mform->elementExists('userid')) { 241 if (empty($data['userid'])) { 242 $errors['userid'] = get_string('required'); 243 } 244 } 245 246 if ($mform->elementExists('groupid')) { 247 if (empty($data['groupid'])) { 248 $errors['groupid'] = get_string('required'); 249 } 250 } 251 252 // Ensure that the dates make sense. 253 if (!empty($data['timeopen']) && !empty($data['timeclose'])) { 254 if ($data['timeclose'] < $data['timeopen'] ) { 255 $errors['timeclose'] = get_string('closebeforeopen', 'quiz'); 256 } 257 } 258 259 // Ensure that at least one quiz setting was changed. 260 $changed = false; 261 $keys = array('timeopen', 'timeclose', 'timelimit', 'attempts', 'password'); 262 foreach ($keys as $key) { 263 if ($data[$key] != $quiz->{$key}) { 264 $changed = true; 265 break; 266 } 267 } 268 if (!$changed) { 269 $errors['timeopen'] = get_string('nooverridedata', 'quiz'); 270 } 271 272 return $errors; 273 } 274 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body