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