MDL-20636 Fix GIFT unit test
[moodle.git] / mod / quiz / mod_form.php
CommitLineData
83192608 1<?php
a23f0aaf 2
ba643847
TH
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Defines the quiz module ettings form.
20 *
21 * @package mod
22 * @subpackage quiz
23 * @copyright 2006 Jamie Pratt
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
84e628a0 26
25302dee 27
a17b297d 28defined('MOODLE_INTERNAL') || die();
bfebaf64 29
84e628a0 30require_once($CFG->dirroot . '/course/moodleform_mod.php');
31require_once($CFG->dirroot . '/mod/quiz/locallib.php');
32
25302dee 33
84e628a0 34/**
35 * Settings form for the quiz module.
3268cf99 36 *
ba643847
TH
37 * @copyright 2006 Jamie Pratt
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
84e628a0 39 */
f07b9627 40class mod_quiz_mod_form extends moodleform_mod {
25302dee
TH
41 protected $_feedbacks;
42 protected static $reviewfields = array(); // Initialised in the constructor.
43
f2557823 44 public function __construct($current, $section, $cm, $course) {
25302dee
TH
45 self::$reviewfields = array(
46 'attempt' => get_string('theattempt', 'quiz'),
47 'correctness' => get_string('whethercorrect', 'question'),
48 'marks' => get_string('marks', 'question'),
49 'specificfeedback' => get_string('specificfeedback', 'question'),
50 'generalfeedback' => get_string('generalfeedback', 'question'),
51 'rightanswer' => get_string('rightanswer', 'question'),
52 'overallfeedback' => get_string('overallfeedback', 'quiz'),
53 );
f2557823 54 parent::__construct($current, $section, $cm, $course);
25302dee 55 }
a23f0aaf 56
2ee60b49 57 function definition() {
25ddb7ef 58 global $COURSE, $CFG, $DB, $PAGE;
e2249afe 59 $quizconfig = get_config('quiz');
25302dee 60 $mform = $this->_form;
a23f0aaf 61
a23f0aaf 62//-------------------------------------------------------------------------------
63 $mform->addElement('header', 'general', get_string('general', 'form'));
64
84e628a0 65 /// Name.
26de8d35 66 $mform->addElement('text', 'name', get_string('name'), array('size'=>'64'));
8eb1d25f 67 if (!empty($CFG->formatstringstriptags)) {
68 $mform->setType('name', PARAM_TEXT);
69 } else {
b8ea3041 70 $mform->setType('name', PARAM_CLEANHTML);
8eb1d25f 71 }
99ebfea2 72 $mform->addRule('name', null, 'required', null, 'client');
a23f0aaf 73
84e628a0 74 /// Introduction.
0cd1affc 75 $this->add_intro_editor(false, get_string('introduction', 'quiz'));
a23f0aaf 76
84e628a0 77 /// Open and close dates.
25302dee
TH
78 $mform->addElement('date_time_selector', 'timeopen', get_string('quizopen', 'quiz'),
79 array('optional' => true, 'step' => 1));
f2557823 80 $mform->addHelpButton('timeopen', 'quizopenclose', 'quiz');
25302dee
TH
81
82 $mform->addElement('date_time_selector', 'timeclose', get_string('quizclose', 'quiz'),
83 array('optional' => true, 'step' => 1));
a23f0aaf 84
84e628a0 85 /// Time limit.
86 $mform->addElement('duration', 'timelimit', get_string('timelimit', 'quiz'), array('optional' => true));
7292c11f 87 $mform->addHelpButton('timelimit', 'timelimit', 'quiz');
adf8f3f9 88 $mform->setAdvanced('timelimit', $quizconfig->timelimit_adv);
e2249afe 89 $mform->setDefault('timelimit', $quizconfig->timelimit);
a23f0aaf 90
84e628a0 91 /// Number of attempts.
92 $attemptoptions = array('0' => get_string('unlimited'));
93 for ($i = 1; $i <= QUIZ_MAX_ATTEMPT_OPTION; $i++) {
94 $attemptoptions[$i] = $i;
a23f0aaf 95 }
84e628a0 96 $mform->addElement('select', 'attempts', get_string('attemptsallowed', 'quiz'), $attemptoptions);
adf8f3f9 97 $mform->setAdvanced('attempts', $quizconfig->attempts_adv);
84e628a0 98 $mform->setDefault('attempts', $quizconfig->attempts);
a23f0aaf 99
84e628a0 100 /// Grading method.
101 $mform->addElement('select', 'grademethod', get_string('grademethod', 'quiz'), quiz_get_grading_options());
7292c11f 102 $mform->addHelpButton('grademethod', 'grademethod', 'quiz');
adf8f3f9 103 $mform->setAdvanced('grademethod', $quizconfig->grademethod_adv);
84e628a0 104 $mform->setDefault('grademethod', $quizconfig->grademethod);
105 $mform->disabledIf('grademethod', 'attempts', 'eq', 1);
106
a23f0aaf 107//-------------------------------------------------------------------------------
84e628a0 108 $mform->addElement('header', 'layouthdr', get_string('layout', 'quiz'));
109
110 /// Shuffle questions.
eeab18f0 111 $shuffleoptions = array(0 => get_string('asshownoneditscreen', 'quiz'), 1 => get_string('shuffledrandomly', 'quiz'));
112 $mform->addElement('select', 'shufflequestions', get_string('questionorder', 'quiz'), $shuffleoptions, array('id' => 'id_shufflequestions'));
adf8f3f9 113 $mform->setAdvanced('shufflequestions', $quizconfig->shufflequestions_adv);
84e628a0 114 $mform->setDefault('shufflequestions', $quizconfig->shufflequestions);
115
116 /// Questions per page.
eeab18f0 117 $pageoptions = array();
118 $pageoptions[0] = get_string('neverallononepage', 'quiz');
119 $pageoptions[1] = get_string('everyquestion', 'quiz');
84e628a0 120 for ($i = 2; $i <= QUIZ_MAX_QPP_OPTION; ++$i) {
eeab18f0 121 $pageoptions[$i] = get_string('everynquestions', 'quiz', $i);
a23f0aaf 122 }
eeab18f0 123
124 $pagegroup = array();
125 $pagegroup[] = &$mform->createElement('select', 'questionsperpage', get_string('newpage', 'quiz'), $pageoptions, array('id' => 'id_questionsperpage'));
e2249afe 126 $mform->setDefault('questionsperpage', $quizconfig->questionsperpage);
a23f0aaf 127
eeab18f0 128 if (!empty($this->_cm)) {
129 $pagegroup[] = &$mform->createElement('checkbox', 'repaginatenow', '', get_string('repaginatenow', 'quiz'), array('id' => 'id_repaginatenow'));
130 $mform->disabledIf('repaginatenow', 'shufflequestions', 'eq', 1);
f44b10ed 131 $PAGE->requires->yui2_lib('event');
9dec75db 132 $PAGE->requires->js('/mod/quiz/edit.js');
a13d4fbd 133 $PAGE->requires->js_init_call('quiz_settings_init');
eeab18f0 134 }
135
136 $mform->addGroup($pagegroup, 'questionsperpagegrp', get_string('newpage', 'quiz'), null, false);
7292c11f 137 $mform->addHelpButton('questionsperpagegrp', 'newpage', 'quiz');
adf8f3f9 138 $mform->setAdvanced('questionsperpagegrp', $quizconfig->questionsperpage_adv);
eeab18f0 139
84e628a0 140//-------------------------------------------------------------------------------
141 $mform->addElement('header', 'interactionhdr', get_string('questionbehaviour', 'quiz'));
a23f0aaf 142
84e628a0 143 /// Shuffle within questions.
144 $mform->addElement('selectyesno', 'shuffleanswers', get_string('shufflewithin', 'quiz'));
7292c11f 145 $mform->addHelpButton('shuffleanswers', 'shufflewithin', 'quiz');
adf8f3f9 146 $mform->setAdvanced('shuffleanswers', $quizconfig->shuffleanswers_adv);
e2249afe 147 $mform->setDefault('shuffleanswers', $quizconfig->shuffleanswers);
a23f0aaf 148
25302dee 149 /// How questions behave (question behaviour).
f2557823
TH
150 if (!empty($this->current->preferredbehaviour)) {
151 $currentbehaviour = $this->current->preferredbehaviour;
25302dee
TH
152 } else {
153 $currentbehaviour = '';
154 }
155 $behaviours = question_engine::get_behaviour_options($currentbehaviour);
156 $mform->addElement('select', 'preferredbehaviour', get_string('howquestionsbehave', 'question'), $behaviours);
f2557823 157 $mform->addHelpButton('preferredbehaviour', 'howquestionsbehave', 'question');
25302dee
TH
158 $mform->setAdvanced('preferredbehaviour', $CFG->quiz_fix_preferredbehaviour);
159 $mform->setDefault('preferredbehaviour', $CFG->quiz_preferredbehaviour);
a23f0aaf 160
84e628a0 161 /// Each attempt builds on last.
162 $mform->addElement('selectyesno', 'attemptonlast', get_string('eachattemptbuildsonthelast', 'quiz'));
7292c11f 163 $mform->addHelpButton('attemptonlast', 'eachattemptbuildsonthelast', 'quiz');
adf8f3f9 164 $mform->setAdvanced('attemptonlast', $quizconfig->attemptonlast_adv);
84e628a0 165 $mform->setDefault('attemptonlast', $quizconfig->attemptonlast);
166 $mform->disabledIf('attemptonlast', 'attempts', 'eq', 1);
a23f0aaf 167
168//-------------------------------------------------------------------------------
e2833e87 169 $mform->addElement('header', 'reviewoptionshdr', get_string('reviewoptionsheading', 'quiz'));
7292c11f 170 $mform->addHelpButton('reviewoptionshdr', 'reviewoptionsheading', 'quiz');
adf8f3f9 171 $mform->setAdvanced('reviewoptionshdr', $quizconfig->review_adv);
a23f0aaf 172
84e628a0 173 /// Review options.
14936199
TH
174 $this->add_review_options_group($mform, $quizconfig, 'during', mod_quiz_display_options::DURING);
175 $this->add_review_options_group($mform, $quizconfig, 'immediately', mod_quiz_display_options::IMMEDIATELY_AFTER);
176 $this->add_review_options_group($mform, $quizconfig, 'open', mod_quiz_display_options::LATER_WHILE_OPEN);
177 $this->add_review_options_group($mform, $quizconfig, 'closed', mod_quiz_display_options::AFTER_CLOSE);
25302dee
TH
178
179 foreach ($behaviours as $behaviour => $notused) {
180 $unusedoptions = question_engine::get_behaviour_unused_display_options($behaviour);
181 foreach ($unusedoptions as $unusedoption) {
182 $mform->disabledIf($unusedoption . 'during', 'preferredbehaviour',
183 'eq', $behaviour);
184 }
185 }
186 $mform->disabledIf('attemptduring', 'preferredbehaviour',
187 'neq', 'wontmatch');
188 $mform->disabledIf('overallfeedbackduring', 'preferredbehaviour',
189 'neq', 'wontmatch');
a23f0aaf 190
191//-------------------------------------------------------------------------------
84e628a0 192 $mform->addElement('header', 'display', get_string('display', 'form'));
a23f0aaf 193
84e628a0 194 /// Show user picture.
195 $mform->addElement('selectyesno', 'showuserpicture', get_string('showuserpicture', 'quiz'));
7292c11f 196 $mform->addHelpButton('showuserpicture', 'showuserpicture', 'quiz');
adf8f3f9 197 $mform->setAdvanced('showuserpicture', $quizconfig->showuserpicture_adv);
84e628a0 198 $mform->setDefault('showuserpicture', $quizconfig->showuserpicture);
199
200 /// Overall decimal points.
201 $options = array();
202 for ($i = 0; $i <= QUIZ_MAX_DECIMAL_OPTION; $i++) {
203 $options[$i] = $i;
204 }
205 $mform->addElement('select', 'decimalpoints', get_string('decimalplaces', 'quiz'), $options);
7292c11f 206 $mform->addHelpButton('decimalpoints', 'decimalplaces', 'quiz');
adf8f3f9 207 $mform->setAdvanced('decimalpoints', $quizconfig->decimalpoints_adv);
84e628a0 208 $mform->setDefault('decimalpoints', $quizconfig->decimalpoints);
a23f0aaf 209
84e628a0 210 /// Question decimal points.
211 $options = array(-1 => get_string('sameasoverall', 'quiz'));
212 for ($i = 0; $i <= QUIZ_MAX_Q_DECIMAL_OPTION; $i++) {
213 $options[$i] = $i;
214 }
215 $mform->addElement('select', 'questiondecimalpoints', get_string('decimalplacesquestion', 'quiz'), $options);
7292c11f 216 $mform->addHelpButton('questiondecimalpoints', 'decimalplacesquestion','quiz');
adf8f3f9 217 $mform->setAdvanced('questiondecimalpoints', $quizconfig->questiondecimalpoints_adv);
84e628a0 218 $mform->setDefault('questiondecimalpoints', $quizconfig->questiondecimalpoints);
219
56ed242b
SH
220 // Show blocks during quiz attempt
221 $mform->addElement('selectyesno', 'showblocks', get_string('showblocks', 'quiz'));
7292c11f 222 $mform->addHelpButton('showblocks', 'showblocks', 'quiz');
56ed242b
SH
223 $mform->setAdvanced('showblocks', $quizconfig->showblocks_adv);
224 $mform->setDefault('showblocks', $quizconfig->showblocks);
225
84e628a0 226//-------------------------------------------------------------------------------
227 $mform->addElement('header', 'security', get_string('extraattemptrestrictions', 'quiz'));
228
229 /// Enforced time delay between quiz attempts.
230 $mform->addElement('passwordunmask', 'quizpassword', get_string('requirepassword', 'quiz'));
2ee60b49 231 $mform->setType('quizpassword', PARAM_TEXT);
7292c11f 232 $mform->addHelpButton('quizpassword', 'requirepassword', 'quiz');
adf8f3f9 233 $mform->setAdvanced('quizpassword', $quizconfig->password_adv);
e2249afe 234 $mform->setDefault('quizpassword', $quizconfig->password);
a23f0aaf 235
84e628a0 236 /// IP address.
237 $mform->addElement('text', 'subnet', get_string('requiresubnet', 'quiz'));
2ee60b49 238 $mform->setType('subnet', PARAM_TEXT);
7292c11f 239 $mform->addHelpButton('subnet', 'requiresubnet', 'quiz');
adf8f3f9 240 $mform->setAdvanced('subnet', $quizconfig->subnet_adv);
e2249afe 241 $mform->setDefault('subnet', $quizconfig->subnet);
a23f0aaf 242
84e628a0 243 /// Enforced time delay between quiz attempts.
244 $mform->addElement('duration', 'delay1', get_string('delay1st2nd', 'quiz'), array('optional' => true));
7292c11f 245 $mform->addHelpButton('delay1', 'delay1st2nd', 'quiz');
adf8f3f9 246 $mform->setAdvanced('delay1', $quizconfig->delay1_adv);
84e628a0 247 $mform->setDefault('delay1', $quizconfig->delay1);
248 $mform->disabledIf('delay1', 'attempts', 'eq', 1);
249
250 $mform->addElement('duration', 'delay2', get_string('delaylater', 'quiz'), array('optional' => true));
7292c11f 251 $mform->addHelpButton('delay2', 'delaylater', 'quiz');
adf8f3f9 252 $mform->setAdvanced('delay2', $quizconfig->delay2_adv);
84e628a0 253 $mform->setDefault('delay2', $quizconfig->delay2);
254 $mform->disabledIf('delay2', 'attempts', 'eq', 1);
255 $mform->disabledIf('delay2', 'attempts', 'eq', 2);
256
257 /// 'Secure' window.
7d4dfc48 258 $options = array(
259 0 => get_string('none', 'quiz'),
260 1 => get_string('popupwithjavascriptsupport', 'quiz'));
05d5801a 261 if (!empty($CFG->enablesafebrowserintegration)) {
7d4dfc48 262 $options[2] = get_string('requiresafeexambrowser', 'quiz');
263 }
264 $mform->addElement('select', 'popup', get_string('browsersecurity', 'quiz'), $options);
7292c11f 265 $mform->addHelpButton('popup', 'browsersecurity', 'quiz');
adf8f3f9 266 $mform->setAdvanced('popup', $quizconfig->popup_adv);
84e628a0 267 $mform->setDefault('popup', $quizconfig->popup);
268
a23f0aaf 269//-------------------------------------------------------------------------------
08224df1 270 $mform->addElement('header', 'overallfeedbackhdr', get_string('overallfeedback', 'quiz'));
7292c11f 271 $mform->addHelpButton('overallfeedbackhdr', 'overallfeedback', 'quiz');
a23f0aaf 272
e0b7cfcb 273 $mform->addElement('hidden', 'grade', $quizconfig->maximumgrade);
d18e0fe6 274 $mform->setType('grade', PARAM_RAW);
f2557823
TH
275
276 if (isset($this->current->grade)) {
277 $needwarning = $this->current->grade === 0;
e0b7cfcb 278 } else {
f2557823 279 $needwarning = $quizconfig->maximumgrade == 0;
e0b7cfcb 280 }
281 if ($needwarning) {
282 $mform->addElement('static', 'nogradewarning', '', get_string('nogradewarning', 'quiz'));
283 }
284
2ee60b49 285 $mform->addElement('static', 'gradeboundarystatic1', get_string('gradeboundary', 'quiz'), '100%');
a23f0aaf 286
e0b7cfcb 287 $repeatarray = array();
25302dee 288 $repeatarray[] = MoodleQuickForm::createElement('editor', 'feedbacktext', get_string('feedback', 'quiz'), null, array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'noclean'=>true, 'context'=>$this->context));
e0b7cfcb 289 $mform->setType('feedbacktext', PARAM_RAW);
25302dee 290 $repeatarray[] = MoodleQuickForm::createElement('text', 'feedbackboundaries', get_string('gradeboundary', 'quiz'), array('size' => 10));
e0b7cfcb 291 $mform->setType('feedbackboundaries', PARAM_NOTAGS);
a23f0aaf 292
293 if (!empty($this->_instance)) {
c18269c7 294 $this->_feedbacks = $DB->get_records('quiz_feedback', array('quizid'=>$this->_instance), 'mingrade DESC');
a23f0aaf 295 } else {
296 $this->_feedbacks = array();
297 }
298 $numfeedbacks = max(count($this->_feedbacks) * 1.5, 5);
ebff6e2c 299
84e628a0 300 $nextel=$this->repeat_elements($repeatarray, $numfeedbacks - 1,
6f3b54c8 301 array(), 'boundary_repeats', 'boundary_add_fields', 3,
302 get_string('addmoreoverallfeedbacks', 'quiz'), true);
a23f0aaf 303
e0b7cfcb 304 // Put some extra elements in before the button
25302dee 305 $insertEl = MoodleQuickForm::createElement('editor', "feedbacktext[$nextel]", get_string('feedback', 'quiz'), null, array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'noclean'=>true, 'context'=>$this->context));
a23f0aaf 306 $mform->insertElementBefore($insertEl, 'boundary_add_fields');
a23f0aaf 307
25302dee 308 $insertEl = MoodleQuickForm::createElement('static', 'gradeboundarystatic2', get_string('gradeboundary', 'quiz'), '0%');
a23f0aaf 309 $mform->insertElementBefore($insertEl, 'boundary_add_fields');
310
e0b7cfcb 311 // Add the disabledif rules. We cannot do this using the $repeatoptions parameter to
312 // repeat_elements becuase we don't want to dissable the first feedbacktext.
313 for ($i = 0; $i < $nextel; $i++) {
314 $mform->disabledIf('feedbackboundaries[' . $i . ']', 'grade', 'eq', 0);
315 $mform->disabledIf('feedbacktext[' . ($i + 1) . ']', 'grade', 'eq', 0);
316 }
317
84e628a0 318//-------------------------------------------------------------------------------
42f103be 319 $this->standard_coursemodule_elements();
84e628a0 320
a23f0aaf 321//-------------------------------------------------------------------------------
322 // buttons
323 $this->add_action_buttons();
2ee60b49 324 }
a23f0aaf 325
14936199 326 protected function add_review_options_group($mform, $quizconfig, $whenname, $when) {
25302dee
TH
327 $group = array();
328 foreach (self::$reviewfields as $field => $label) {
329 $group[] = $mform->createElement('checkbox', $field . $whenname, '', $label);
330 }
331 $mform->addGroup($group, $whenname . 'optionsgrp', get_string('review' . $whenname, 'quiz'), null, false);
332
333 foreach (self::$reviewfields as $field => $notused) {
14936199
TH
334 $cfgfield = 'review' . $field;
335 if ($quizconfig->$cfgfield & $when) {
25302dee
TH
336 $mform->setDefault($field . $whenname, 1);
337 } else {
338 $mform->setDefault($field . $whenname, 0);
339 }
340 }
341
342 $mform->disabledIf('correctness' . $whenname, 'attempt' . $whenname);
343 $mform->disabledIf('specificfeedback' . $whenname, 'attempt' . $whenname);
344 $mform->disabledIf('generalfeedback' . $whenname, 'attempt' . $whenname);
345 $mform->disabledIf('rightanswer' . $whenname, 'attempt' . $whenname);
346 }
347
348 protected function preprocessing_review_settings(&$toform, $whenname, $when) {
349 foreach (self::$reviewfields as $field => $notused) {
350 $fieldname = 'review' . $field;
351 if (array_key_exists($fieldname, $toform)) {
352 $toform[$field . $whenname] = $toform[$fieldname] & $when;
353 }
354 }
355 }
356
357 function data_preprocessing(&$toform) {
358 if (isset($toform['grade'])) {
359 $toform['grade'] = $toform['grade'] + 0; // Convert to a real number, so we don't get 0.0000.
e0b7cfcb 360 }
361
a23f0aaf 362 if (count($this->_feedbacks)) {
363 $key = 0;
364 foreach ($this->_feedbacks as $feedback){
fe6ce234 365 $draftid = file_get_submitted_draft_itemid('feedbacktext['.$key.']');
25302dee 366 $toform['feedbacktext['.$key.']']['text'] = file_prepare_draft_area(
fe6ce234
DC
367 $draftid, // draftid
368 $this->context->id, // context
369 'mod_quiz', // component
370 'feedback', // filarea
371 !empty($feedback->id)?(int)$feedback->id:null, // itemid
372 null,
373 $feedback->feedbacktext // text
374 );
25302dee
TH
375 $toform['feedbacktext['.$key.']']['format'] = $feedback->feedbacktextformat;
376 $toform['feedbacktext['.$key.']']['itemid'] = $draftid;
fe6ce234 377
a23f0aaf 378 if ($feedback->mingrade > 0) {
167f1562 379 $toform['feedbackboundaries['.$key.']'] = (100.0 * $feedback->mingrade / $toform['grade']) . '%';
a23f0aaf 380 }
381 $key++;
382 }
a23f0aaf 383 }
298daa21 384
25302dee
TH
385 if (isset($toform['timelimit'])) {
386 $toform['timelimitenable'] = $toform['timelimit'] > 0;
a23f0aaf 387 }
388
25302dee
TH
389 $this->preprocessing_review_settings($toform, 'during', mod_quiz_display_options::DURING);
390 $this->preprocessing_review_settings($toform, 'immediately', mod_quiz_display_options::IMMEDIATELY_AFTER);
391 $this->preprocessing_review_settings($toform, 'open', mod_quiz_display_options::LATER_WHILE_OPEN);
392 $this->preprocessing_review_settings($toform, 'closed', mod_quiz_display_options::AFTER_CLOSE);
393 $toform['attemptduring'] = true;
394 $toform['overallfeedbackduring'] = false;
395
396 // Password field - different in form to stop browsers that remember
397 // passwords from getting confused.
398 if (isset($toform['password'])) {
399 $toform['quizpassword'] = $toform['password'];
400 unset($toform['password']);
ab0a8ff2 401 }
2ee60b49 402 }
a23f0aaf 403
a78890d5 404 function validation($data, $files) {
405 $errors = parent::validation($data, $files);
60243313 406
a23f0aaf 407 // Check open and close times are consistent.
408 if ($data['timeopen'] != 0 && $data['timeclose'] != 0 && $data['timeclose'] < $data['timeopen']) {
409 $errors['timeclose'] = get_string('closebeforeopen', 'quiz');
410 }
411
412 // Check the boundary value is a number or a percentage, and in range.
413 $i = 0;
414 while (!empty($data['feedbackboundaries'][$i] )) {
415 $boundary = trim($data['feedbackboundaries'][$i]);
416 if (strlen($boundary) > 0 && $boundary[strlen($boundary) - 1] == '%') {
417 $boundary = trim(substr($boundary, 0, -1));
418 if (is_numeric($boundary)) {
419 $boundary = $boundary * $data['grade'] / 100.0;
420 } else {
421 $errors["feedbackboundaries[$i]"] = get_string('feedbackerrorboundaryformat', 'quiz', $i + 1);
422 }
423 }
424 if (is_numeric($boundary) && $boundary <= 0 || $boundary >= $data['grade'] ) {
425 $errors["feedbackboundaries[$i]"] = get_string('feedbackerrorboundaryoutofrange', 'quiz', $i + 1);
426 }
427 if (is_numeric($boundary) && $i > 0 && $boundary >= $data['feedbackboundaries'][$i - 1]) {
428 $errors["feedbackboundaries[$i]"] = get_string('feedbackerrororder', 'quiz', $i + 1);
429 }
430 $data['feedbackboundaries'][$i] = $boundary;
431 $i += 1;
432 }
433 $numboundaries = $i;
434
435 // Check there is nothing in the remaining unused fields.
e0b7cfcb 436 if (!empty($data['feedbackboundaries'])) {
437 for ($i = $numboundaries; $i < count($data['feedbackboundaries']); $i += 1) {
438 if (!empty($data['feedbackboundaries'][$i] ) && trim($data['feedbackboundaries'][$i] ) != '') {
439 $errors["feedbackboundaries[$i]"] = get_string('feedbackerrorjunkinboundary', 'quiz', $i + 1);
440 }
a23f0aaf 441 }
442 }
e0b7cfcb 443 for ($i = $numboundaries + 1; $i < count($data['feedbacktext']); $i += 1) {
fe6ce234 444 if (!empty($data['feedbacktext'][$i]['text']) && trim($data['feedbacktext'][$i]['text'] ) != '') {
a23f0aaf 445 $errors["feedbacktext[$i]"] = get_string('feedbackerrorjunkinfeedback', 'quiz', $i + 1);
446 }
447 }
60243313 448
32648682 449 return $errors;
2ee60b49 450 }
a23f0aaf 451}