MDL-37961 webservice: PARAM_BOOL with PARAM_DEFAULT accepts boolean default
[moodle.git] / mod / quiz / override_form.php
blob80976ecef61dad700726d15ad661cc59d1992f0f
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.
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/>.
17 /**
18 * Settings form for overrides in the quiz module.
20 * @package mod
21 * @subpackage quiz
22 * @copyright 2010 Matt Petro
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->libdir . '/formslib.php');
32 /**
33 * Form for editing settings overrides.
35 * @copyright 2010 Matt Petro
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class quiz_override_form extends moodleform {
40 /** @var object course module object. */
41 protected $cm;
43 /** @var object the quiz settings object. */
44 protected $quiz;
46 /** @var context the quiz context. */
47 protected $context;
49 /** @var bool editing group override (true) or user override (false). */
50 protected $groupmode;
52 /** @var int groupid, if provided. */
53 protected $groupid;
55 /** @var int userid, if provided. */
56 protected $userid;
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.
67 public function __construct($submiturl, $cm, $quiz, $context, $groupmode, $override) {
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;
76 parent::__construct($submiturl, null, 'post');
80 protected function definition() {
81 global $CFG, $DB;
83 $cm = $this->cm;
84 $mform = $this->_form;
86 $mform->addElement('header', 'override', get_string('override', 'quiz'));
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);
106 $groupchoices = array();
107 foreach ($groups as $group) {
108 $groupchoices[$group->id] = $group->name;
110 unset($groups);
112 if (count($groupchoices) == 0) {
113 $groupchoices[0] = get_string('none');
116 $mform->addElement('select', 'groupid',
117 get_string('overridegroup', 'quiz'), $groupchoices);
118 $mform->addRule('groupid', get_string('required'), 'required', null, 'client');
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.');
139 if (!empty($CFG->enablegroupmembersonly) && $cm->groupmembersonly) {
140 // Only users from the grouping.
141 $groups = groups_get_all_groups($cm->course, 0, $cm->groupingid);
142 if (!empty($groups)) {
143 $users = get_users_by_capability($this->context, 'mod/quiz:attempt',
144 'u.id, u.firstname, u.lastname, u.email',
145 $sort, '', '', array_keys($groups),
146 '', false, true);
148 } else {
149 $users = get_users_by_capability($this->context, 'mod/quiz:attempt',
150 'u.id, u.firstname, u.lastname, u.email' ,
151 $sort, '', '', '', '', false, true);
153 if (empty($users)) {
154 // Generate an error.
155 $link = new moodle_url('/mod/quiz/overrides.php', array('cmid'=>$cm->id));
156 print_error('usersnone', 'quiz', $link);
159 $userchoices = array();
160 foreach ($users as $id => $user) {
161 if (empty($invalidusers[$id]) || (!empty($override) &&
162 $id == $override->userid)) {
163 $userchoices[$id] = fullname($user) . ', ' . $user->email;
166 unset($users);
168 if (count($userchoices) == 0) {
169 $userchoices[0] = get_string('none');
171 $mform->addElement('searchableselector', 'userid',
172 get_string('overrideuser', 'quiz'), $userchoices);
173 $mform->addRule('userid', get_string('required'), 'required', null, 'client');
177 // Password.
178 // This field has to be above the date and timelimit fields,
179 // otherwise browsers will clear it when those fields are changed.
180 $mform->addElement('passwordunmask', 'password', get_string('requirepassword', 'quiz'));
181 $mform->setType('password', PARAM_TEXT);
182 $mform->addHelpButton('password', 'requirepassword', 'quiz');
183 $mform->setDefault('password', $this->quiz->password);
185 // Open and close dates.
186 $mform->addElement('date_time_selector', 'timeopen',
187 get_string('quizopen', 'quiz'), array('optional' => true));
188 $mform->setDefault('timeopen', $this->quiz->timeopen);
190 $mform->addElement('date_time_selector', 'timeclose',
191 get_string('quizclose', 'quiz'), array('optional' => true));
192 $mform->setDefault('timeclose', $this->quiz->timeclose);
194 // Time limit.
195 $mform->addElement('duration', 'timelimit',
196 get_string('timelimit', 'quiz'), array('optional' => true));
197 $mform->addHelpButton('timelimit', 'timelimit', 'quiz');
198 $mform->setDefault('timelimit', $this->quiz->timelimit);
200 // Number of attempts.
201 $attemptoptions = array('0' => get_string('unlimited'));
202 for ($i = 1; $i <= QUIZ_MAX_ATTEMPT_OPTION; $i++) {
203 $attemptoptions[$i] = $i;
205 $mform->addElement('select', 'attempts',
206 get_string('attemptsallowed', 'quiz'), $attemptoptions);
207 $mform->setDefault('attempts', $this->quiz->attempts);
209 // Submit buttons.
210 $mform->addElement('submit', 'resetbutton',
211 get_string('reverttodefaults', 'quiz'));
213 $buttonarray = array();
214 $buttonarray[] = $mform->createElement('submit', 'submitbutton',
215 get_string('save', 'quiz'));
216 $buttonarray[] = $mform->createElement('submit', 'againbutton',
217 get_string('saveoverrideandstay', 'quiz'));
218 $buttonarray[] = $mform->createElement('cancel');
220 $mform->addGroup($buttonarray, 'buttonbar', '', array(' '), false);
221 $mform->closeHeaderBefore('buttonbar');
225 public function validation($data, $files) {
226 global $COURSE, $DB;
227 $errors = parent::validation($data, $files);
229 $mform =& $this->_form;
230 $quiz = $this->quiz;
232 if ($mform->elementExists('userid')) {
233 if (empty($data['userid'])) {
234 $errors['userid'] = get_string('required');
238 if ($mform->elementExists('groupid')) {
239 if (empty($data['groupid'])) {
240 $errors['groupid'] = get_string('required');
244 // Ensure that the dates make sense.
245 if (!empty($data['timeopen']) && !empty($data['timeclose'])) {
246 if ($data['timeclose'] < $data['timeopen'] ) {
247 $errors['timeclose'] = get_string('closebeforeopen', 'quiz');
251 // Ensure that at least one quiz setting was changed.
252 $changed = false;
253 $keys = array('timeopen', 'timeclose', 'timelimit', 'attempts', 'password');
254 foreach ($keys as $key) {
255 if ($data[$key] != $quiz->{$key}) {
256 $changed = true;
257 break;
260 if (!$changed) {
261 $errors['timeopen'] = get_string('nooverridedata', 'quiz');
264 return $errors;