2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Settings form for overrides in the quiz module.
21 * @copyright 2010 Matt Petro
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
28 require_once($CFG->libdir
. '/formslib.php');
29 require_once($CFG->dirroot
. '/mod/quiz/mod_form.php');
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. */
43 /** @var object the quiz settings object. */
46 /** @var context the quiz context. */
49 /** @var bool editing group override (true) or user override (false). */
52 /** @var int groupid, if provided. */
55 /** @var int userid, if provided. */
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) {
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() {
84 $mform = $this->_form
;
86 $mform->addElement('header', 'override', get_string('override', 'quiz'));
88 if ($this->groupmode
) {
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');
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
;
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');
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');
131 // Prepare the list of users.
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 $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);
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);
148 // Generate an error.
149 $link = new moodle_url('/mod/quiz/overrides.php', array('cmid'=>$cm->id
));
150 print_error('usersnone', 'quiz', $link);
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
)) {
159 $userchoices[$id] = fullname($user) . ', ' . $user->email
;
161 $userchoices[$id] = fullname($user);
167 $mform->addElement('searchableselector', 'userid',
168 get_string('overrideuser', 'quiz'), $userchoices);
169 $mform->addRule('userid', get_string('required'), 'required', null, 'client');
174 // This field has to be above the date and timelimit fields,
175 // otherwise browsers will clear it when those fields are changed.
176 $mform->addElement('passwordunmask', 'password', get_string('requirepassword', 'quiz'));
177 $mform->setType('password', PARAM_TEXT
);
178 $mform->addHelpButton('password', 'requirepassword', 'quiz');
179 $mform->setDefault('password', $this->quiz
->password
);
181 // Open and close dates.
182 $mform->addElement('date_time_selector', 'timeopen',
183 get_string('quizopen', 'quiz'), mod_quiz_mod_form
::$datefieldoptions);
184 $mform->setDefault('timeopen', $this->quiz
->timeopen
);
186 $mform->addElement('date_time_selector', 'timeclose',
187 get_string('quizclose', 'quiz'), mod_quiz_mod_form
::$datefieldoptions);
188 $mform->setDefault('timeclose', $this->quiz
->timeclose
);
191 $mform->addElement('duration', 'timelimit',
192 get_string('timelimit', 'quiz'), array('optional' => true));
193 $mform->addHelpButton('timelimit', 'timelimit', 'quiz');
194 $mform->setDefault('timelimit', $this->quiz
->timelimit
);
196 // Number of attempts.
197 $attemptoptions = array('0' => get_string('unlimited'));
198 for ($i = 1; $i <= QUIZ_MAX_ATTEMPT_OPTION
; $i++
) {
199 $attemptoptions[$i] = $i;
201 $mform->addElement('select', 'attempts',
202 get_string('attemptsallowed', 'quiz'), $attemptoptions);
203 $mform->addHelpButton('attempts', 'attempts', 'quiz');
204 $mform->setDefault('attempts', $this->quiz
->attempts
);
207 $mform->addElement('submit', 'resetbutton',
208 get_string('reverttodefaults', 'quiz'));
210 $buttonarray = array();
211 $buttonarray[] = $mform->createElement('submit', 'submitbutton',
212 get_string('save', 'quiz'));
213 $buttonarray[] = $mform->createElement('submit', 'againbutton',
214 get_string('saveoverrideandstay', 'quiz'));
215 $buttonarray[] = $mform->createElement('cancel');
217 $mform->addGroup($buttonarray, 'buttonbar', '', array(' '), false);
218 $mform->closeHeaderBefore('buttonbar');
222 public function validation($data, $files) {
224 $errors = parent
::validation($data, $files);
226 $mform =& $this->_form
;
229 if ($mform->elementExists('userid')) {
230 if (empty($data['userid'])) {
231 $errors['userid'] = get_string('required');
235 if ($mform->elementExists('groupid')) {
236 if (empty($data['groupid'])) {
237 $errors['groupid'] = get_string('required');
241 // Ensure that the dates make sense.
242 if (!empty($data['timeopen']) && !empty($data['timeclose'])) {
243 if ($data['timeclose'] < $data['timeopen'] ) {
244 $errors['timeclose'] = get_string('closebeforeopen', 'quiz');
248 // Ensure that at least one quiz setting was changed.
250 $keys = array('timeopen', 'timeclose', 'timelimit', 'attempts', 'password');
251 foreach ($keys as $key) {
252 if ($data[$key] != $quiz->{$key}) {
258 $errors['timeopen'] = get_string('nooverridedata', 'quiz');