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