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