MDL-37998 quiz settings: disableif rules should account for overrides.
[moodle.git] / mod / quiz / mod_form.php
CommitLineData
83192608 1<?php
ba643847
TH
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 * Defines the quiz module ettings form.
19 *
01773a6d 20 * @package mod_quiz
ba643847
TH
21 * @copyright 2006 Jamie Pratt
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
84e628a0 24
25302dee 25
a17b297d 26defined('MOODLE_INTERNAL') || die();
bfebaf64 27
84e628a0 28require_once($CFG->dirroot . '/course/moodleform_mod.php');
29require_once($CFG->dirroot . '/mod/quiz/locallib.php');
30
25302dee 31
84e628a0 32/**
33 * Settings form for the quiz module.
3268cf99 34 *
ba643847
TH
35 * @copyright 2006 Jamie Pratt
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
84e628a0 37 */
f07b9627 38class mod_quiz_mod_form extends moodleform_mod {
73465743
TH
39 /** @var array options to be used with date_time_selector fields in the quiz. */
40 public static $datefieldoptions = array('optional' => true, 'step' => 1);
41
25302dee
TH
42 protected $_feedbacks;
43 protected static $reviewfields = array(); // Initialised in the constructor.
44
358f3167
TH
45 /** @var int the max number of attempts allowed in any user or group override on this quiz. */
46 protected $maxattemptsanyoverride = null;
47
f2557823 48 public function __construct($current, $section, $cm, $course) {
25302dee 49 self::$reviewfields = array(
ce74dcc3
TH
50 'attempt' => array('theattempt', 'quiz'),
51 'correctness' => array('whethercorrect', 'question'),
52 'marks' => array('marks', 'quiz'),
53 'specificfeedback' => array('specificfeedback', 'question'),
54 'generalfeedback' => array('generalfeedback', 'question'),
55 'rightanswer' => array('rightanswer', 'question'),
56 'overallfeedback' => array('reviewoverallfeedback', 'quiz'),
25302dee 57 );
f2557823 58 parent::__construct($current, $section, $cm, $course);
25302dee 59 }
a23f0aaf 60
c7df5006 61 protected function definition() {
25ddb7ef 62 global $COURSE, $CFG, $DB, $PAGE;
e2249afe 63 $quizconfig = get_config('quiz');
25302dee 64 $mform = $this->_form;
a23f0aaf 65
9e83f3d1 66 // -------------------------------------------------------------------------------
a23f0aaf 67 $mform->addElement('header', 'general', get_string('general', 'form'));
68
aac80ff3 69 // Name.
26de8d35 70 $mform->addElement('text', 'name', get_string('name'), array('size'=>'64'));
8eb1d25f 71 if (!empty($CFG->formatstringstriptags)) {
72 $mform->setType('name', PARAM_TEXT);
73 } else {
b8ea3041 74 $mform->setType('name', PARAM_CLEANHTML);
8eb1d25f 75 }
99ebfea2 76 $mform->addRule('name', null, 'required', null, 'client');
a74cd331 77 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
a23f0aaf 78
aac80ff3 79 // Introduction.
0cd1affc 80 $this->add_intro_editor(false, get_string('introduction', 'quiz'));
a23f0aaf 81
9e83f3d1 82 // -------------------------------------------------------------------------------
84da4788
TH
83 $mform->addElement('header', 'timing', get_string('timing', 'quiz'));
84
aac80ff3 85 // Open and close dates.
25302dee 86 $mform->addElement('date_time_selector', 'timeopen', get_string('quizopen', 'quiz'),
73465743 87 self::$datefieldoptions);
f2557823 88 $mform->addHelpButton('timeopen', 'quizopenclose', 'quiz');
25302dee
TH
89
90 $mform->addElement('date_time_selector', 'timeclose', get_string('quizclose', 'quiz'),
73465743 91 self::$datefieldoptions);
a23f0aaf 92
aac80ff3
TH
93 // Time limit.
94 $mform->addElement('duration', 'timelimit', get_string('timelimit', 'quiz'),
95 array('optional' => true));
7292c11f 96 $mform->addHelpButton('timelimit', 'timelimit', 'quiz');
adf8f3f9 97 $mform->setAdvanced('timelimit', $quizconfig->timelimit_adv);
e2249afe 98 $mform->setDefault('timelimit', $quizconfig->timelimit);
a23f0aaf 99
87427d9c
TH
100 // What to do with overdue attempts.
101 $mform->addElement('select', 'overduehandling', get_string('overduehandling', 'quiz'),
102 quiz_get_overdue_handling_options());
d9552ab4 103 $mform->addHelpButton('overduehandling', 'overduehandling', 'quiz');
87427d9c
TH
104 $mform->setAdvanced('overduehandling', $quizconfig->overduehandling_adv);
105 $mform->setDefault('overduehandling', $quizconfig->overduehandling);
fdf26a0e 106 // TODO Formslib does OR logic on disableif, and we need AND logic here.
87427d9c
TH
107 // $mform->disabledIf('overduehandling', 'timelimit', 'eq', 0);
108 // $mform->disabledIf('overduehandling', 'timeclose', 'eq', 0);
109
110 // Grace period time.
111 $mform->addElement('duration', 'graceperiod', get_string('graceperiod', 'quiz'),
112 array('optional' => true));
113 $mform->addHelpButton('graceperiod', 'graceperiod', 'quiz');
114 $mform->setAdvanced('graceperiod', $quizconfig->graceperiod_adv);
115 $mform->setDefault('graceperiod', $quizconfig->graceperiod);
116 $mform->disabledIf('graceperiod', 'overduehandling', 'neq', 'graceperiod');
117
9e83f3d1
TH
118 // -------------------------------------------------------------------------------
119 // Grade settings.
84da4788
TH
120 $this->standard_grading_coursemodule_elements();
121
122 $mform->removeElement('grade');
123 $mform->addElement('hidden', 'grade', $quizconfig->maximumgrade);
61cca0b7 124 $mform->setType('grade', PARAM_FLOAT);
84da4788 125
aac80ff3 126 // Number of attempts.
84e628a0 127 $attemptoptions = array('0' => get_string('unlimited'));
128 for ($i = 1; $i <= QUIZ_MAX_ATTEMPT_OPTION; $i++) {
129 $attemptoptions[$i] = $i;
a23f0aaf 130 }
aac80ff3
TH
131 $mform->addElement('select', 'attempts', get_string('attemptsallowed', 'quiz'),
132 $attemptoptions);
adf8f3f9 133 $mform->setAdvanced('attempts', $quizconfig->attempts_adv);
84e628a0 134 $mform->setDefault('attempts', $quizconfig->attempts);
a23f0aaf 135
aac80ff3
TH
136 // Grading method.
137 $mform->addElement('select', 'grademethod', get_string('grademethod', 'quiz'),
138 quiz_get_grading_options());
7292c11f 139 $mform->addHelpButton('grademethod', 'grademethod', 'quiz');
adf8f3f9 140 $mform->setAdvanced('grademethod', $quizconfig->grademethod_adv);
84e628a0 141 $mform->setDefault('grademethod', $quizconfig->grademethod);
358f3167
TH
142 if ($this->get_max_attempts_for_any_override() < 2) {
143 $mform->disabledIf('grademethod', 'attempts', 'eq', 1);
144 }
84e628a0 145
9e83f3d1 146 // -------------------------------------------------------------------------------
84e628a0 147 $mform->addElement('header', 'layouthdr', get_string('layout', 'quiz'));
148
aac80ff3
TH
149 // Shuffle questions.
150 $shuffleoptions = array(
151 0 => get_string('asshownoneditscreen', 'quiz'),
152 1 => get_string('shuffledrandomly', 'quiz')
153 );
154 $mform->addElement('select', 'shufflequestions', get_string('questionorder', 'quiz'),
155 $shuffleoptions, array('id' => 'id_shufflequestions'));
adf8f3f9 156 $mform->setAdvanced('shufflequestions', $quizconfig->shufflequestions_adv);
84e628a0 157 $mform->setDefault('shufflequestions', $quizconfig->shufflequestions);
158
aac80ff3 159 // Questions per page.
eeab18f0 160 $pageoptions = array();
161 $pageoptions[0] = get_string('neverallononepage', 'quiz');
162 $pageoptions[1] = get_string('everyquestion', 'quiz');
84e628a0 163 for ($i = 2; $i <= QUIZ_MAX_QPP_OPTION; ++$i) {
eeab18f0 164 $pageoptions[$i] = get_string('everynquestions', 'quiz', $i);
a23f0aaf 165 }
eeab18f0 166
167 $pagegroup = array();
aac80ff3
TH
168 $pagegroup[] = $mform->createElement('select', 'questionsperpage',
169 get_string('newpage', 'quiz'), $pageoptions, array('id' => 'id_questionsperpage'));
e2249afe 170 $mform->setDefault('questionsperpage', $quizconfig->questionsperpage);
a23f0aaf 171
eeab18f0 172 if (!empty($this->_cm)) {
aac80ff3
TH
173 $pagegroup[] = $mform->createElement('checkbox', 'repaginatenow', '',
174 get_string('repaginatenow', 'quiz'), array('id' => 'id_repaginatenow'));
eeab18f0 175 $mform->disabledIf('repaginatenow', 'shufflequestions', 'eq', 1);
f387bb9b
PS
176
177 $PAGE->requires->js('/question/qengine.js');
178 $module = array(
179 'name' => 'mod_quiz_edit',
180 'fullpath' => '/mod/quiz/edit.js',
181 'requires' => array('yui2-dom', 'yui2-event', 'yui2-container'),
182 'strings' => array(),
183 'async' => false,
184 );
185 $PAGE->requires->js_init_call('quiz_settings_init', null, false, $module);
eeab18f0 186 }
187
aac80ff3
TH
188 $mform->addGroup($pagegroup, 'questionsperpagegrp',
189 get_string('newpage', 'quiz'), null, false);
7292c11f 190 $mform->addHelpButton('questionsperpagegrp', 'newpage', 'quiz');
adf8f3f9 191 $mform->setAdvanced('questionsperpagegrp', $quizconfig->questionsperpage_adv);
eeab18f0 192
9e83f3d1 193 // Navigation method.
33c8d37b
CF
194 $mform->addElement('select', 'navmethod', get_string('navmethod', 'quiz'),
195 quiz_get_navigation_options());
196 $mform->addHelpButton('navmethod', 'navmethod', 'quiz');
197 $mform->setAdvanced('navmethod', $quizconfig->navmethod_adv);
198 $mform->setDefault('navmethod', $quizconfig->navmethod);
199
9e83f3d1 200 // -------------------------------------------------------------------------------
84e628a0 201 $mform->addElement('header', 'interactionhdr', get_string('questionbehaviour', 'quiz'));
a23f0aaf 202
aac80ff3 203 // Shuffle within questions.
84e628a0 204 $mform->addElement('selectyesno', 'shuffleanswers', get_string('shufflewithin', 'quiz'));
7292c11f 205 $mform->addHelpButton('shuffleanswers', 'shufflewithin', 'quiz');
adf8f3f9 206 $mform->setAdvanced('shuffleanswers', $quizconfig->shuffleanswers_adv);
e2249afe 207 $mform->setDefault('shuffleanswers', $quizconfig->shuffleanswers);
a23f0aaf 208
aac80ff3 209 // How questions behave (question behaviour).
f2557823
TH
210 if (!empty($this->current->preferredbehaviour)) {
211 $currentbehaviour = $this->current->preferredbehaviour;
25302dee
TH
212 } else {
213 $currentbehaviour = '';
214 }
215 $behaviours = question_engine::get_behaviour_options($currentbehaviour);
aac80ff3
TH
216 $mform->addElement('select', 'preferredbehaviour',
217 get_string('howquestionsbehave', 'question'), $behaviours);
f2557823 218 $mform->addHelpButton('preferredbehaviour', 'howquestionsbehave', 'question');
bb28e3bc 219 $mform->setDefault('preferredbehaviour', $quizconfig->preferredbehaviour);
a23f0aaf 220
aac80ff3
TH
221 // Each attempt builds on last.
222 $mform->addElement('selectyesno', 'attemptonlast',
223 get_string('eachattemptbuildsonthelast', 'quiz'));
7292c11f 224 $mform->addHelpButton('attemptonlast', 'eachattemptbuildsonthelast', 'quiz');
adf8f3f9 225 $mform->setAdvanced('attemptonlast', $quizconfig->attemptonlast_adv);
84e628a0 226 $mform->setDefault('attemptonlast', $quizconfig->attemptonlast);
358f3167
TH
227 if ($this->get_max_attempts_for_any_override() < 2) {
228 $mform->disabledIf('attemptonlast', 'attempts', 'eq', 1);
229 }
a23f0aaf 230
9e83f3d1 231 // -------------------------------------------------------------------------------
aac80ff3
TH
232 $mform->addElement('header', 'reviewoptionshdr',
233 get_string('reviewoptionsheading', 'quiz'));
7292c11f 234 $mform->addHelpButton('reviewoptionshdr', 'reviewoptionsheading', 'quiz');
a23f0aaf 235
aac80ff3
TH
236 // Review options.
237 $this->add_review_options_group($mform, $quizconfig, 'during',
ce74dcc3 238 mod_quiz_display_options::DURING, true);
aac80ff3
TH
239 $this->add_review_options_group($mform, $quizconfig, 'immediately',
240 mod_quiz_display_options::IMMEDIATELY_AFTER);
241 $this->add_review_options_group($mform, $quizconfig, 'open',
242 mod_quiz_display_options::LATER_WHILE_OPEN);
243 $this->add_review_options_group($mform, $quizconfig, 'closed',
244 mod_quiz_display_options::AFTER_CLOSE);
25302dee
TH
245
246 foreach ($behaviours as $behaviour => $notused) {
247 $unusedoptions = question_engine::get_behaviour_unused_display_options($behaviour);
248 foreach ($unusedoptions as $unusedoption) {
249 $mform->disabledIf($unusedoption . 'during', 'preferredbehaviour',
250 'eq', $behaviour);
251 }
252 }
253 $mform->disabledIf('attemptduring', 'preferredbehaviour',
254 'neq', 'wontmatch');
255 $mform->disabledIf('overallfeedbackduring', 'preferredbehaviour',
256 'neq', 'wontmatch');
a23f0aaf 257
9e83f3d1 258 // -------------------------------------------------------------------------------
4dc30936 259 $mform->addElement('header', 'display', get_string('appearance'));
a23f0aaf 260
aac80ff3 261 // Show user picture.
9ec7a716
TH
262 $mform->addElement('select', 'showuserpicture', get_string('showuserpicture', 'quiz'),
263 quiz_get_user_image_options());
7292c11f 264 $mform->addHelpButton('showuserpicture', 'showuserpicture', 'quiz');
adf8f3f9 265 $mform->setAdvanced('showuserpicture', $quizconfig->showuserpicture_adv);
84e628a0 266 $mform->setDefault('showuserpicture', $quizconfig->showuserpicture);
267
aac80ff3 268 // Overall decimal points.
84e628a0 269 $options = array();
270 for ($i = 0; $i <= QUIZ_MAX_DECIMAL_OPTION; $i++) {
271 $options[$i] = $i;
272 }
aac80ff3
TH
273 $mform->addElement('select', 'decimalpoints', get_string('decimalplaces', 'quiz'),
274 $options);
7292c11f 275 $mform->addHelpButton('decimalpoints', 'decimalplaces', 'quiz');
adf8f3f9 276 $mform->setAdvanced('decimalpoints', $quizconfig->decimalpoints_adv);
84e628a0 277 $mform->setDefault('decimalpoints', $quizconfig->decimalpoints);
a23f0aaf 278
aac80ff3 279 // Question decimal points.
84e628a0 280 $options = array(-1 => get_string('sameasoverall', 'quiz'));
281 for ($i = 0; $i <= QUIZ_MAX_Q_DECIMAL_OPTION; $i++) {
282 $options[$i] = $i;
283 }
aac80ff3
TH
284 $mform->addElement('select', 'questiondecimalpoints',
285 get_string('decimalplacesquestion', 'quiz'), $options);
25a03faa 286 $mform->addHelpButton('questiondecimalpoints', 'decimalplacesquestion', 'quiz');
adf8f3f9 287 $mform->setAdvanced('questiondecimalpoints', $quizconfig->questiondecimalpoints_adv);
84e628a0 288 $mform->setDefault('questiondecimalpoints', $quizconfig->questiondecimalpoints);
289
9e83f3d1 290 // Show blocks during quiz attempt.
56ed242b 291 $mform->addElement('selectyesno', 'showblocks', get_string('showblocks', 'quiz'));
7292c11f 292 $mform->addHelpButton('showblocks', 'showblocks', 'quiz');
56ed242b
SH
293 $mform->setAdvanced('showblocks', $quizconfig->showblocks_adv);
294 $mform->setDefault('showblocks', $quizconfig->showblocks);
295
9e83f3d1 296 // -------------------------------------------------------------------------------
84e628a0 297 $mform->addElement('header', 'security', get_string('extraattemptrestrictions', 'quiz'));
298
7493a4bb 299 // Require password to begin quiz attempt.
84e628a0 300 $mform->addElement('passwordunmask', 'quizpassword', get_string('requirepassword', 'quiz'));
2ee60b49 301 $mform->setType('quizpassword', PARAM_TEXT);
7292c11f 302 $mform->addHelpButton('quizpassword', 'requirepassword', 'quiz');
adf8f3f9 303 $mform->setAdvanced('quizpassword', $quizconfig->password_adv);
e2249afe 304 $mform->setDefault('quizpassword', $quizconfig->password);
a23f0aaf 305
aac80ff3 306 // IP address.
84e628a0 307 $mform->addElement('text', 'subnet', get_string('requiresubnet', 'quiz'));
2ee60b49 308 $mform->setType('subnet', PARAM_TEXT);
7292c11f 309 $mform->addHelpButton('subnet', 'requiresubnet', 'quiz');
adf8f3f9 310 $mform->setAdvanced('subnet', $quizconfig->subnet_adv);
e2249afe 311 $mform->setDefault('subnet', $quizconfig->subnet);
a23f0aaf 312
aac80ff3
TH
313 // Enforced time delay between quiz attempts.
314 $mform->addElement('duration', 'delay1', get_string('delay1st2nd', 'quiz'),
315 array('optional' => true));
7292c11f 316 $mform->addHelpButton('delay1', 'delay1st2nd', 'quiz');
adf8f3f9 317 $mform->setAdvanced('delay1', $quizconfig->delay1_adv);
84e628a0 318 $mform->setDefault('delay1', $quizconfig->delay1);
358f3167
TH
319 if ($this->get_max_attempts_for_any_override() < 2) {
320 $mform->disabledIf('delay1', 'attempts', 'eq', 1);
321 }
84e628a0 322
aac80ff3
TH
323 $mform->addElement('duration', 'delay2', get_string('delaylater', 'quiz'),
324 array('optional' => true));
7292c11f 325 $mform->addHelpButton('delay2', 'delaylater', 'quiz');
adf8f3f9 326 $mform->setAdvanced('delay2', $quizconfig->delay2_adv);
84e628a0 327 $mform->setDefault('delay2', $quizconfig->delay2);
358f3167
TH
328 if ($this->get_max_attempts_for_any_override() < 3) {
329 $mform->disabledIf('delay2', 'attempts', 'eq', 1);
330 $mform->disabledIf('delay2', 'attempts', 'eq', 2);
331 }
84e628a0 332
9e83f3d1 333 // Browser security choices.
4344c5d5 334 $mform->addElement('select', 'browsersecurity', get_string('browsersecurity', 'quiz'),
1d9e1a3c 335 quiz_access_manager::get_browser_security_choices());
4344c5d5
TH
336 $mform->addHelpButton('browsersecurity', 'browsersecurity', 'quiz');
337 $mform->setAdvanced('browsersecurity', $quizconfig->browsersecurity_adv);
338 $mform->setDefault('browsersecurity', $quizconfig->browsersecurity);
84e628a0 339
b83c32d3
TH
340 // Any other rule plugins.
341 quiz_access_manager::add_settings_form_fields($this, $mform);
342
9e83f3d1 343 // -------------------------------------------------------------------------------
08224df1 344 $mform->addElement('header', 'overallfeedbackhdr', get_string('overallfeedback', 'quiz'));
7292c11f 345 $mform->addHelpButton('overallfeedbackhdr', 'overallfeedback', 'quiz');
a23f0aaf 346
f2557823
TH
347 if (isset($this->current->grade)) {
348 $needwarning = $this->current->grade === 0;
e0b7cfcb 349 } else {
f2557823 350 $needwarning = $quizconfig->maximumgrade == 0;
e0b7cfcb 351 }
352 if ($needwarning) {
aac80ff3
TH
353 $mform->addElement('static', 'nogradewarning', '',
354 get_string('nogradewarning', 'quiz'));
e0b7cfcb 355 }
356
aac80ff3
TH
357 $mform->addElement('static', 'gradeboundarystatic1',
358 get_string('gradeboundary', 'quiz'), '100%');
a23f0aaf 359
e0b7cfcb 360 $repeatarray = array();
d0e25622 361 $repeatedoptions = array();
fabbf439 362 $repeatarray[] = $mform->createElement('editor', 'feedbacktext',
e58b2c32 363 get_string('feedback', 'quiz'), array('rows' => 3), array('maxfiles' => EDITOR_UNLIMITED_FILES,
0c0dfa8f 364 'noclean' => true, 'context' => $this->context));
fabbf439 365 $repeatarray[] = $mform->createElement('text', 'feedbackboundaries',
aac80ff3 366 get_string('gradeboundary', 'quiz'), array('size' => 10));
d0e25622
TH
367 $repeatedoptions['feedbacktext']['type'] = PARAM_RAW;
368 $repeatedoptions['feedbackboundaries']['type'] = PARAM_RAW;
a23f0aaf 369
370 if (!empty($this->_instance)) {
d0e25622
TH
371 $this->_feedbacks = $DB->get_records('quiz_feedback',
372 array('quizid' => $this->_instance), 'mingrade DESC');
a23f0aaf 373 } else {
374 $this->_feedbacks = array();
375 }
376 $numfeedbacks = max(count($this->_feedbacks) * 1.5, 5);
ebff6e2c 377
d0e25622
TH
378 $nextel = $this->repeat_elements($repeatarray, $numfeedbacks - 1,
379 $repeatedoptions, 'boundary_repeats', 'boundary_add_fields', 3,
6f3b54c8 380 get_string('addmoreoverallfeedbacks', 'quiz'), true);
a23f0aaf 381
9e83f3d1 382 // Put some extra elements in before the button.
fabbf439 383 $mform->insertElementBefore($mform->createElement('editor',
e58b2c32 384 "feedbacktext[$nextel]", get_string('feedback', 'quiz'), array('rows' => 3),
d0e25622 385 array('maxfiles' => EDITOR_UNLIMITED_FILES, 'noclean' => true,
0c0dfa8f 386 'context' => $this->context)),
d0e25622 387 'boundary_add_fields');
fabbf439 388 $mform->insertElementBefore($mform->createElement('static',
d0e25622
TH
389 'gradeboundarystatic2', get_string('gradeboundary', 'quiz'), '0%'),
390 'boundary_add_fields');
a23f0aaf 391
e0b7cfcb 392 // Add the disabledif rules. We cannot do this using the $repeatoptions parameter to
81522abd 393 // repeat_elements because we don't want to dissable the first feedbacktext.
e0b7cfcb 394 for ($i = 0; $i < $nextel; $i++) {
395 $mform->disabledIf('feedbackboundaries[' . $i . ']', 'grade', 'eq', 0);
396 $mform->disabledIf('feedbacktext[' . ($i + 1) . ']', 'grade', 'eq', 0);
397 }
398
9e83f3d1 399 // -------------------------------------------------------------------------------
42f103be 400 $this->standard_coursemodule_elements();
84e628a0 401
d18f78b0
TL
402 // Check and act on whether setting outcomes is considered an advanced setting.
403 $mform->setAdvanced('modoutcomes', !empty($quizconfig->outcomes_adv));
404
f9e54712
TH
405 // The standard_coursemodule_elements method sets this to 100, but the
406 // quiz has its own setting, so use that.
407 $mform->setDefault('grade', $quizconfig->maximumgrade);
408
9e83f3d1 409 // -------------------------------------------------------------------------------
a23f0aaf 410 $this->add_action_buttons();
2ee60b49 411 }
a23f0aaf 412
ce74dcc3
TH
413 protected function add_review_options_group($mform, $quizconfig, $whenname,
414 $when, $withhelp = false) {
415 global $OUTPUT;
416
25302dee 417 $group = array();
ce74dcc3
TH
418 foreach (self::$reviewfields as $field => $string) {
419 list($identifier, $component) = $string;
420
421 $label = get_string($identifier, $component);
422 if ($withhelp) {
423 $label .= ' ' . $OUTPUT->help_icon($identifier, $component);
424 }
425
25302dee
TH
426 $group[] = $mform->createElement('checkbox', $field . $whenname, '', $label);
427 }
aac80ff3
TH
428 $mform->addGroup($group, $whenname . 'optionsgrp',
429 get_string('review' . $whenname, 'quiz'), null, false);
25302dee
TH
430
431 foreach (self::$reviewfields as $field => $notused) {
14936199
TH
432 $cfgfield = 'review' . $field;
433 if ($quizconfig->$cfgfield & $when) {
25302dee
TH
434 $mform->setDefault($field . $whenname, 1);
435 } else {
436 $mform->setDefault($field . $whenname, 0);
437 }
438 }
439
c5908481
TH
440 if ($whenname != 'during') {
441 $mform->disabledIf('correctness' . $whenname, 'attempt' . $whenname);
442 $mform->disabledIf('specificfeedback' . $whenname, 'attempt' . $whenname);
443 $mform->disabledIf('generalfeedback' . $whenname, 'attempt' . $whenname);
444 $mform->disabledIf('rightanswer' . $whenname, 'attempt' . $whenname);
445 }
25302dee
TH
446 }
447
448 protected function preprocessing_review_settings(&$toform, $whenname, $when) {
449 foreach (self::$reviewfields as $field => $notused) {
450 $fieldname = 'review' . $field;
451 if (array_key_exists($fieldname, $toform)) {
452 $toform[$field . $whenname] = $toform[$fieldname] & $when;
453 }
454 }
455 }
456
06cd11a9 457 public function data_preprocessing(&$toform) {
25302dee 458 if (isset($toform['grade'])) {
aac80ff3
TH
459 // Convert to a real number, so we don't get 0.0000.
460 $toform['grade'] = $toform['grade'] + 0;
e0b7cfcb 461 }
462
a23f0aaf 463 if (count($this->_feedbacks)) {
464 $key = 0;
aac80ff3 465 foreach ($this->_feedbacks as $feedback) {
fe6ce234 466 $draftid = file_get_submitted_draft_itemid('feedbacktext['.$key.']');
25302dee 467 $toform['feedbacktext['.$key.']']['text'] = file_prepare_draft_area(
9e83f3d1
TH
468 $draftid, // Draftid.
469 $this->context->id, // Context.
470 'mod_quiz', // Component.
471 'feedback', // Filarea.
472 !empty($feedback->id) ? (int) $feedback->id : null, // Itemid.
fe6ce234 473 null,
9e83f3d1 474 $feedback->feedbacktext // Text.
fe6ce234 475 );
25302dee
TH
476 $toform['feedbacktext['.$key.']']['format'] = $feedback->feedbacktextformat;
477 $toform['feedbacktext['.$key.']']['itemid'] = $draftid;
fe6ce234 478
c5c16a2c
TH
479 if ($toform['grade'] == 0) {
480 // When a quiz is un-graded, there can only be one lot of
481 // feedback. If the quiz previously had a maximum grade and
482 // several lots of feedback, we must now avoid putting text
483 // into input boxes that are disabled, but which the
484 // validation will insist are blank.
485 break;
486 }
487
a23f0aaf 488 if ($feedback->mingrade > 0) {
aac80ff3
TH
489 $toform['feedbackboundaries['.$key.']'] =
490 (100.0 * $feedback->mingrade / $toform['grade']) . '%';
a23f0aaf 491 }
492 $key++;
493 }
a23f0aaf 494 }
298daa21 495
25302dee
TH
496 if (isset($toform['timelimit'])) {
497 $toform['timelimitenable'] = $toform['timelimit'] > 0;
a23f0aaf 498 }
499
aac80ff3
TH
500 $this->preprocessing_review_settings($toform, 'during',
501 mod_quiz_display_options::DURING);
502 $this->preprocessing_review_settings($toform, 'immediately',
503 mod_quiz_display_options::IMMEDIATELY_AFTER);
504 $this->preprocessing_review_settings($toform, 'open',
505 mod_quiz_display_options::LATER_WHILE_OPEN);
506 $this->preprocessing_review_settings($toform, 'closed',
507 mod_quiz_display_options::AFTER_CLOSE);
25302dee
TH
508 $toform['attemptduring'] = true;
509 $toform['overallfeedbackduring'] = false;
510
511 // Password field - different in form to stop browsers that remember
512 // passwords from getting confused.
513 if (isset($toform['password'])) {
514 $toform['quizpassword'] = $toform['password'];
515 unset($toform['password']);
ab0a8ff2 516 }
c18ba64c
TH
517
518 // Load any settings belonging to the access rules.
519 if (!empty($toform['instance'])) {
520 $accesssettings = quiz_access_manager::load_settings($toform['instance']);
521 foreach ($accesssettings as $name => $value) {
522 $toform[$name] = $value;
523 }
524 }
2ee60b49 525 }
a23f0aaf 526
c7df5006 527 public function validation($data, $files) {
a78890d5 528 $errors = parent::validation($data, $files);
60243313 529
a23f0aaf 530 // Check open and close times are consistent.
aac80ff3
TH
531 if ($data['timeopen'] != 0 && $data['timeclose'] != 0 &&
532 $data['timeclose'] < $data['timeopen']) {
a23f0aaf 533 $errors['timeclose'] = get_string('closebeforeopen', 'quiz');
534 }
535
a6866c7e
TH
536 // Check that the grace period is not too short.
537 if ($data['overduehandling'] == 'graceperiod') {
538 $graceperiodmin = get_config('quiz', 'graceperiodmin');
539 if ($data['graceperiod'] <= $graceperiodmin) {
540 $errors['graceperiod'] = get_string('graceperiodtoosmall', 'quiz', format_time($graceperiodmin));
541 }
542 }
543
a23f0aaf 544 // Check the boundary value is a number or a percentage, and in range.
545 $i = 0;
546 while (!empty($data['feedbackboundaries'][$i] )) {
547 $boundary = trim($data['feedbackboundaries'][$i]);
26efb488
JR
548 if (strlen($boundary) > 0) {
549 if ($boundary[strlen($boundary) - 1] == '%') {
550 $boundary = trim(substr($boundary, 0, -1));
551 if (is_numeric($boundary)) {
552 $boundary = $boundary * $data['grade'] / 100.0;
553 } else {
554 $errors["feedbackboundaries[$i]"] =
555 get_string('feedbackerrorboundaryformat', 'quiz', $i + 1);
556 }
557 } else if (!is_numeric($boundary)) {
aac80ff3
TH
558 $errors["feedbackboundaries[$i]"] =
559 get_string('feedbackerrorboundaryformat', 'quiz', $i + 1);
a23f0aaf 560 }
561 }
562 if (is_numeric($boundary) && $boundary <= 0 || $boundary >= $data['grade'] ) {
aac80ff3
TH
563 $errors["feedbackboundaries[$i]"] =
564 get_string('feedbackerrorboundaryoutofrange', 'quiz', $i + 1);
a23f0aaf 565 }
aac80ff3
TH
566 if (is_numeric($boundary) && $i > 0 &&
567 $boundary >= $data['feedbackboundaries'][$i - 1]) {
568 $errors["feedbackboundaries[$i]"] =
569 get_string('feedbackerrororder', 'quiz', $i + 1);
a23f0aaf 570 }
571 $data['feedbackboundaries'][$i] = $boundary;
572 $i += 1;
573 }
574 $numboundaries = $i;
575
576 // Check there is nothing in the remaining unused fields.
e0b7cfcb 577 if (!empty($data['feedbackboundaries'])) {
578 for ($i = $numboundaries; $i < count($data['feedbackboundaries']); $i += 1) {
aac80ff3
TH
579 if (!empty($data['feedbackboundaries'][$i] ) &&
580 trim($data['feedbackboundaries'][$i] ) != '') {
581 $errors["feedbackboundaries[$i]"] =
582 get_string('feedbackerrorjunkinboundary', 'quiz', $i + 1);
e0b7cfcb 583 }
a23f0aaf 584 }
585 }
e0b7cfcb 586 for ($i = $numboundaries + 1; $i < count($data['feedbacktext']); $i += 1) {
aac80ff3
TH
587 if (!empty($data['feedbacktext'][$i]['text']) &&
588 trim($data['feedbacktext'][$i]['text'] ) != '') {
589 $errors["feedbacktext[$i]"] =
590 get_string('feedbackerrorjunkinfeedback', 'quiz', $i + 1);
a23f0aaf 591 }
592 }
60243313 593
d823a63d
TH
594 // Any other rule plugins.
595 $errors = quiz_access_manager::validate_settings_form_fields($errors, $data, $files, $this);
596
32648682 597 return $errors;
2ee60b49 598 }
349c9303
RM
599
600 /**
601 * Display module-specific activity completion rules.
602 * Part of the API defined by moodleform_mod
603 * @return array Array of string IDs of added items, empty array if none
604 */
605 public function add_completion_rules() {
606 $mform = $this->_form;
607 $items = array();
608
609 $group = array();
610 $group[] = $mform->createElement('advcheckbox', 'completionpass', null, get_string('completionpass', 'quiz'),
611 array('group' => 'cpass'));
612
613 $group[] = $mform->createElement('advcheckbox', 'completionattemptsexhausted', null,
614 get_string('completionattemptsexhausted', 'quiz'),
615 array('group' => 'cattempts'));
616 $mform->disabledIf('completionattemptsexhausted', 'completionpass', 'notchecked');
617 $mform->addGroup($group, 'completionpassgroup', get_string('completionpass', 'quiz'), ' &nbsp; ', false);
618 $mform->addHelpButton('completionpassgroup', 'completionpass', 'quiz');
619 $items[] = 'completionpassgroup';
620 return $items;
621 }
622
623 /**
624 * Called during validation. Indicates whether a module-specific completion rule is selected.
625 *
626 * @param array $data Input data (not yet validated)
627 * @return bool True if one or more rules is enabled, false if none are.
628 */
629 public function completion_rule_enabled($data) {
630 return !empty($data['completionattemptsexhausted']) || !empty($data['completionpass']);
631 }
358f3167
TH
632
633 /**
634 * Get the maximum number of attempts that anyone might have due to a user
635 * or group override. Used to decide whether disabledIf rules should be applied.
636 * @return int the number of attempts allowed. For the purpose of this method,
637 * unlimited is returned as 1000, not 0.
638 */
639 public function get_max_attempts_for_any_override() {
640 global $DB;
641
642 if (empty($this->_instance)) {
643 // Quiz not created yet, so no overrides.
644 return 1;
645 }
646
647 if ($this->maxattemptsanyoverride === null) {
648 $this->maxattemptsanyoverride = $DB->get_field_sql("
649 SELECT MAX(CASE WHEN attempts = 0 THEN 1000 ELSE attempts END)
650 FROM {quiz_overrides}
651 WHERE quiz = ?",
652 array($this->_instance));
653 if ($this->maxattemptsanyoverride < 1) {
654 // This happens when no override alters the number of attempts.
655 $this->maxattemptsanyoverride = 1;
656 }
657 }
658
659 return $this->maxattemptsanyoverride;
660 }
a23f0aaf 661}