MDL-28653 question output. Add a class to the main div based on question state.
[moodle.git] / mod / quiz / override_form.php
blobc5e12f4dca27437cd8b34fed340fd4b1d2d615d6
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 protected $cm; // course module object
41 protected $quiz; // quiz object
42 protected $context; // context object
43 protected $groupmode; // editing group override (true) or user override (false)
44 protected $groupid; // groupid, if provided
45 protected $userid; // userid, if provided
47 public function __construct($submiturl, $cm, $quiz, $context, $groupmode, $override) {
49 $this->cm = $cm;
50 $this->quiz = $quiz;
51 $this->context = $context;
52 $this->groupmode = $groupmode;
53 $this->groupid = empty($override->groupid) ? 0 : $override->groupid;
54 $this->userid = empty($override->userid) ? 0 : $override->userid;
56 parent::__construct($submiturl, null, 'post');
60 protected function definition() {
61 global $CFG, $USER, $DB;
63 $cm = $this->cm;
64 $mform = $this->_form;
66 $mform->addElement('header', 'override', get_string('override', 'quiz'));
68 if ($this->groupmode) {
69 // group override
70 if ($this->groupid) {
71 // There is already a groupid, so freeze the selector
72 $groupchoices = array();
73 $groupchoices[$this->groupid] = groups_get_group_name($this->groupid);
74 $mform->addElement('select', 'groupid',
75 get_string('overridegroup', 'quiz'), $groupchoices);
76 $mform->freeze('groupid');
77 } else {
78 // Prepare the list of groups
79 $groups = groups_get_all_groups($cm->course);
80 if (empty($groups)) {
81 // generate an error
82 $link = new moodle_url('/mod/quiz/overrides.php', array('cmid'=>$cm->id));
83 print_error('groupsnone', 'quiz', $link);
86 $groupchoices = array();
87 foreach ($groups as $group) {
88 $groupchoices[$group->id] = $group->name;
90 unset($groups);
92 if (count($groupchoices) == 0) {
93 $groupchoices[0] = get_string('none');
96 $mform->addElement('select', 'groupid',
97 get_string('overridegroup', 'quiz'), $groupchoices);
98 $mform->addRule('groupid', get_string('required'), 'required', null, 'client');
100 } else {
101 //user override
102 if ($this->userid) {
103 // There is already a userid, so freeze the selector
104 $user = $DB->get_record('user', array('id'=>$this->userid));
105 $userchoices = array();
106 $userchoices[$this->userid] = fullname($user);
107 $mform->addElement('select', 'userid',
108 get_string('overrideuser', 'quiz'), $userchoices);
109 $mform->freeze('userid');
110 } else {
111 // Prepare the list of users
112 $users = array();
113 if (!empty($CFG->enablegroupmembersonly) && $cm->groupmembersonly) {
114 // only users from the grouping
115 $groups = groups_get_all_groups($cm->course, 0, $cm->groupingid);
116 if (!empty($groups)) {
117 $users = get_users_by_capability($this->context, 'mod/quiz:attempt',
118 'u.id, u.firstname, u.lastname, u.email',
119 'firstname ASC, lastname ASC', '', '', array_keys($groups),
120 '', false, true);
122 } else {
123 $users = get_users_by_capability($this->context, 'mod/quiz:attempt',
124 'u.id, u.firstname, u.lastname, u.email' ,
125 'firstname ASC, lastname ASC', '', '', '', '', false, true);
127 if (empty($users)) {
128 // generate an error
129 $link = new moodle_url('/mod/quiz/overrides.php', array('cmid'=>$cm->id));
130 print_error('usersnone', 'quiz', $link);
133 $userchoices = array();
134 foreach ($users as $id => $user) {
135 if (empty($invalidusers[$id]) || (!empty($override) &&
136 $id == $override->userid)) {
137 $userchoices[$id] = fullname($user) . ', ' . $user->email;
140 unset($users);
142 if (count($userchoices) == 0) {
143 $userchoices[0] = get_string('none');
145 $mform->addElement('searchableselector', 'userid',
146 get_string('overrideuser', 'quiz'), $userchoices);
147 $mform->addRule('userid', get_string('required'), 'required', null, 'client');
151 // Password
152 // This field has to be above the date and timelimit fields,
153 // otherwise browsers will clear it when those fields are changed
154 $mform->addElement('passwordunmask', 'password', get_string('requirepassword', 'quiz'));
155 $mform->setType('password', PARAM_TEXT);
156 $mform->addHelpButton('password', 'requirepassword', 'quiz');
157 $mform->setDefault('password', $this->quiz->password);
159 // Open and close dates.
160 $mform->addElement('date_time_selector', 'timeopen',
161 get_string('quizopen', 'quiz'), array('optional' => true));
162 $mform->setDefault('timeopen', $this->quiz->timeopen);
164 $mform->addElement('date_time_selector', 'timeclose',
165 get_string('quizclose', 'quiz'), array('optional' => true));
166 $mform->setDefault('timeclose', $this->quiz->timeclose);
168 // Time limit.
169 $mform->addElement('duration', 'timelimit',
170 get_string('timelimit', 'quiz'), array('optional' => true));
171 $mform->addHelpButton('timelimit', 'timelimit', 'quiz');
172 $mform->setDefault('timelimit', $this->quiz->timelimit);
174 // Number of attempts.
175 $attemptoptions = array('0' => get_string('unlimited'));
176 for ($i = 1; $i <= QUIZ_MAX_ATTEMPT_OPTION; $i++) {
177 $attemptoptions[$i] = $i;
179 $mform->addElement('select', 'attempts',
180 get_string('attemptsallowed', 'quiz'), $attemptoptions);
181 $mform->setDefault('attempts', $this->quiz->attempts);
183 // Submit buttons
184 $mform->addElement('submit', 'resetbutton',
185 get_string('reverttodefaults', 'quiz'));
187 $buttonarray = array();
188 $buttonarray[] = $mform->createElement('submit', 'submitbutton',
189 get_string('save', 'quiz'));
190 $buttonarray[] = $mform->createElement('submit', 'againbutton',
191 get_string('saveoverrideandstay', 'quiz'));
192 $buttonarray[] = $mform->createElement('cancel');
194 $mform->addGroup($buttonarray, 'buttonbar', '', array(' '), false);
195 $mform->closeHeaderBefore('buttonbar');
199 // form verification
200 public function validation($data, $files) {
201 global $COURSE, $DB;
202 $errors = parent::validation($data, $files);
204 $mform =& $this->_form;
205 $quiz = $this->quiz;
207 if ($mform->elementExists('userid')) {
208 if (empty($data['userid'])) {
209 $errors['userid'] = get_string('required');
213 if ($mform->elementExists('groupid')) {
214 if (empty($data['groupid'])) {
215 $errors['groupid'] = get_string('required');
219 // Ensure that the dates make sense
220 if (!empty($data['timeopen']) && !empty($data['timeclose'])) {
221 if ($data['timeclose'] < $data['timeopen'] ) {
222 $errors['timeclose'] = get_string('closebeforeopen', 'quiz');
226 // Ensure that at least one quiz setting was changed
227 $changed = false;
228 $keys = array('timeopen', 'timeclose', 'timelimit', 'attempts', 'password');
229 foreach ($keys as $key) {
230 if ($data[$key] != $quiz->{$key}) {
231 $changed = true;
232 break;
235 if (!$changed) {
236 $errors['timeopen'] = get_string('nooverridedata', 'quiz');
239 return $errors;