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