Merge branch 'MDL-39155' of https://github.com/jacks92/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('select', 'showuserpicture', get_string('showuserpicture', 'quiz'), array(
254                 QUIZ_SHOWIMAGE_NONE => get_string('shownoimage', 'quiz'),
255                 QUIZ_SHOWIMAGE_SMALL => get_string('showsmallimage', 'quiz'),
256                 QUIZ_SHOWIMAGE_LARGE => get_string('showlargeimage', 'quiz')));
257         $mform->addHelpButton('showuserpicture', 'showuserpicture', 'quiz');
258         $mform->setAdvanced('showuserpicture', $quizconfig->showuserpicture_adv);
259         $mform->setDefault('showuserpicture', $quizconfig->showuserpicture);
261         // Overall decimal points.
262         $options = array();
263         for ($i = 0; $i <= QUIZ_MAX_DECIMAL_OPTION; $i++) {
264             $options[$i] = $i;
265         }
266         $mform->addElement('select', 'decimalpoints', get_string('decimalplaces', 'quiz'),
267                 $options);
268         $mform->addHelpButton('decimalpoints', 'decimalplaces', 'quiz');
269         $mform->setAdvanced('decimalpoints', $quizconfig->decimalpoints_adv);
270         $mform->setDefault('decimalpoints', $quizconfig->decimalpoints);
272         // Question decimal points.
273         $options = array(-1 => get_string('sameasoverall', 'quiz'));
274         for ($i = 0; $i <= QUIZ_MAX_Q_DECIMAL_OPTION; $i++) {
275             $options[$i] = $i;
276         }
277         $mform->addElement('select', 'questiondecimalpoints',
278                 get_string('decimalplacesquestion', 'quiz'), $options);
279         $mform->addHelpButton('questiondecimalpoints', 'decimalplacesquestion', 'quiz');
280         $mform->setAdvanced('questiondecimalpoints', $quizconfig->questiondecimalpoints_adv);
281         $mform->setDefault('questiondecimalpoints', $quizconfig->questiondecimalpoints);
283         // Show blocks during quiz attempt.
284         $mform->addElement('selectyesno', 'showblocks', get_string('showblocks', 'quiz'));
285         $mform->addHelpButton('showblocks', 'showblocks', 'quiz');
286         $mform->setAdvanced('showblocks', $quizconfig->showblocks_adv);
287         $mform->setDefault('showblocks', $quizconfig->showblocks);
289         // -------------------------------------------------------------------------------
290         $mform->addElement('header', 'security', get_string('extraattemptrestrictions', 'quiz'));
292         // Require password to begin quiz attempt.
293         $mform->addElement('passwordunmask', 'quizpassword', get_string('requirepassword', 'quiz'));
294         $mform->setType('quizpassword', PARAM_TEXT);
295         $mform->addHelpButton('quizpassword', 'requirepassword', 'quiz');
296         $mform->setAdvanced('quizpassword', $quizconfig->password_adv);
297         $mform->setDefault('quizpassword', $quizconfig->password);
299         // IP address.
300         $mform->addElement('text', 'subnet', get_string('requiresubnet', 'quiz'));
301         $mform->setType('subnet', PARAM_TEXT);
302         $mform->addHelpButton('subnet', 'requiresubnet', 'quiz');
303         $mform->setAdvanced('subnet', $quizconfig->subnet_adv);
304         $mform->setDefault('subnet', $quizconfig->subnet);
306         // Enforced time delay between quiz attempts.
307         $mform->addElement('duration', 'delay1', get_string('delay1st2nd', 'quiz'),
308                 array('optional' => true));
309         $mform->addHelpButton('delay1', 'delay1st2nd', 'quiz');
310         $mform->setAdvanced('delay1', $quizconfig->delay1_adv);
311         $mform->setDefault('delay1', $quizconfig->delay1);
312         $mform->disabledIf('delay1', 'attempts', 'eq', 1);
314         $mform->addElement('duration', 'delay2', get_string('delaylater', 'quiz'),
315                 array('optional' => true));
316         $mform->addHelpButton('delay2', 'delaylater', 'quiz');
317         $mform->setAdvanced('delay2', $quizconfig->delay2_adv);
318         $mform->setDefault('delay2', $quizconfig->delay2);
319         $mform->disabledIf('delay2', 'attempts', 'eq', 1);
320         $mform->disabledIf('delay2', 'attempts', 'eq', 2);
322         // Browser security choices.
323         $mform->addElement('select', 'browsersecurity', get_string('browsersecurity', 'quiz'),
324                 quiz_access_manager::get_browser_security_choices());
325         $mform->addHelpButton('browsersecurity', 'browsersecurity', 'quiz');
326         $mform->setAdvanced('browsersecurity', $quizconfig->browsersecurity_adv);
327         $mform->setDefault('browsersecurity', $quizconfig->browsersecurity);
329         // Any other rule plugins.
330         quiz_access_manager::add_settings_form_fields($this, $mform);
332         // -------------------------------------------------------------------------------
333         $mform->addElement('header', 'overallfeedbackhdr', get_string('overallfeedback', 'quiz'));
334         $mform->addHelpButton('overallfeedbackhdr', 'overallfeedback', 'quiz');
336         if (isset($this->current->grade)) {
337             $needwarning = $this->current->grade === 0;
338         } else {
339             $needwarning = $quizconfig->maximumgrade == 0;
340         }
341         if ($needwarning) {
342             $mform->addElement('static', 'nogradewarning', '',
343                     get_string('nogradewarning', 'quiz'));
344         }
346         $mform->addElement('static', 'gradeboundarystatic1',
347                 get_string('gradeboundary', 'quiz'), '100%');
349         $repeatarray = array();
350         $repeatedoptions = array();
351         $repeatarray[] = $mform->createElement('editor', 'feedbacktext',
352                 get_string('feedback', 'quiz'), array('rows' => 3), array('maxfiles' => EDITOR_UNLIMITED_FILES,
353                         'noclean' => true, 'context' => $this->context, 'collapsed' => 1));
354         $repeatarray[] = $mform->createElement('text', 'feedbackboundaries',
355                 get_string('gradeboundary', 'quiz'), array('size' => 10));
356         $repeatedoptions['feedbacktext']['type'] = PARAM_RAW;
357         $repeatedoptions['feedbackboundaries']['type'] = PARAM_RAW;
359         if (!empty($this->_instance)) {
360             $this->_feedbacks = $DB->get_records('quiz_feedback',
361                     array('quizid' => $this->_instance), 'mingrade DESC');
362         } else {
363             $this->_feedbacks = array();
364         }
365         $numfeedbacks = max(count($this->_feedbacks) * 1.5, 5);
367         $nextel = $this->repeat_elements($repeatarray, $numfeedbacks - 1,
368                 $repeatedoptions, 'boundary_repeats', 'boundary_add_fields', 3,
369                 get_string('addmoreoverallfeedbacks', 'quiz'), true);
371         // Put some extra elements in before the button.
372         $mform->insertElementBefore($mform->createElement('editor',
373                 "feedbacktext[$nextel]", get_string('feedback', 'quiz'), array('rows' => 3),
374                 array('maxfiles' => EDITOR_UNLIMITED_FILES, 'noclean' => true,
375                       'context' => $this->context, 'collapsed' => 1)),
376                 'boundary_add_fields');
377         $mform->insertElementBefore($mform->createElement('static',
378                 'gradeboundarystatic2', get_string('gradeboundary', 'quiz'), '0%'),
379                 'boundary_add_fields');
381         // Add the disabledif rules. We cannot do this using the $repeatoptions parameter to
382         // repeat_elements because we don't want to dissable the first feedbacktext.
383         for ($i = 0; $i < $nextel; $i++) {
384             $mform->disabledIf('feedbackboundaries[' . $i . ']', 'grade', 'eq', 0);
385             $mform->disabledIf('feedbacktext[' . ($i + 1) . ']', 'grade', 'eq', 0);
386         }
388         // -------------------------------------------------------------------------------
389         $this->standard_coursemodule_elements();
391         // Check and act on whether setting outcomes is considered an advanced setting.
392         $mform->setAdvanced('modoutcomes', !empty($quizconfig->outcomes_adv));
394         // The standard_coursemodule_elements method sets this to 100, but the
395         // quiz has its own setting, so use that.
396         $mform->setDefault('grade', $quizconfig->maximumgrade);
398         // -------------------------------------------------------------------------------
399         $this->add_action_buttons();
400     }
402     protected function add_review_options_group($mform, $quizconfig, $whenname,
403             $when, $withhelp = false) {
404         global $OUTPUT;
406         $group = array();
407         foreach (self::$reviewfields as $field => $string) {
408             list($identifier, $component) = $string;
410             $label = get_string($identifier, $component);
411             if ($withhelp) {
412                 $label .= ' ' . $OUTPUT->help_icon($identifier, $component);
413             }
415             $group[] = $mform->createElement('checkbox', $field . $whenname, '', $label);
416         }
417         $mform->addGroup($group, $whenname . 'optionsgrp',
418                 get_string('review' . $whenname, 'quiz'), null, false);
420         foreach (self::$reviewfields as $field => $notused) {
421             $cfgfield = 'review' . $field;
422             if ($quizconfig->$cfgfield & $when) {
423                 $mform->setDefault($field . $whenname, 1);
424             } else {
425                 $mform->setDefault($field . $whenname, 0);
426             }
427         }
429         $mform->disabledIf('correctness' . $whenname, 'attempt' . $whenname);
430         $mform->disabledIf('specificfeedback' . $whenname, 'attempt' . $whenname);
431         $mform->disabledIf('generalfeedback' . $whenname, 'attempt' . $whenname);
432         $mform->disabledIf('rightanswer' . $whenname, 'attempt' . $whenname);
433     }
435     protected function preprocessing_review_settings(&$toform, $whenname, $when) {
436         foreach (self::$reviewfields as $field => $notused) {
437             $fieldname = 'review' . $field;
438             if (array_key_exists($fieldname, $toform)) {
439                 $toform[$field . $whenname] = $toform[$fieldname] & $when;
440             }
441         }
442     }
444     public function data_preprocessing(&$toform) {
445         if (isset($toform['grade'])) {
446             // Convert to a real number, so we don't get 0.0000.
447             $toform['grade'] = $toform['grade'] + 0;
448         }
450         if (count($this->_feedbacks)) {
451             $key = 0;
452             foreach ($this->_feedbacks as $feedback) {
453                 $draftid = file_get_submitted_draft_itemid('feedbacktext['.$key.']');
454                 $toform['feedbacktext['.$key.']']['text'] = file_prepare_draft_area(
455                     $draftid,               // Draftid.
456                     $this->context->id,     // Context.
457                     'mod_quiz',             // Component.
458                     'feedback',             // Filarea.
459                     !empty($feedback->id) ? (int) $feedback->id : null, // Itemid.
460                     null,
461                     $feedback->feedbacktext // Text.
462                 );
463                 $toform['feedbacktext['.$key.']']['format'] = $feedback->feedbacktextformat;
464                 $toform['feedbacktext['.$key.']']['itemid'] = $draftid;
466                 if ($toform['grade'] == 0) {
467                     // When a quiz is un-graded, there can only be one lot of
468                     // feedback. If the quiz previously had a maximum grade and
469                     // several lots of feedback, we must now avoid putting text
470                     // into input boxes that are disabled, but which the
471                     // validation will insist are blank.
472                     break;
473                 }
475                 if ($feedback->mingrade > 0) {
476                     $toform['feedbackboundaries['.$key.']'] =
477                             (100.0 * $feedback->mingrade / $toform['grade']) . '%';
478                 }
479                 $key++;
480             }
481         }
483         if (isset($toform['timelimit'])) {
484             $toform['timelimitenable'] = $toform['timelimit'] > 0;
485         }
487         $this->preprocessing_review_settings($toform, 'during',
488                 mod_quiz_display_options::DURING);
489         $this->preprocessing_review_settings($toform, 'immediately',
490                 mod_quiz_display_options::IMMEDIATELY_AFTER);
491         $this->preprocessing_review_settings($toform, 'open',
492                 mod_quiz_display_options::LATER_WHILE_OPEN);
493         $this->preprocessing_review_settings($toform, 'closed',
494                 mod_quiz_display_options::AFTER_CLOSE);
495         $toform['attemptduring'] = true;
496         $toform['overallfeedbackduring'] = false;
498         // Password field - different in form to stop browsers that remember
499         // passwords from getting confused.
500         if (isset($toform['password'])) {
501             $toform['quizpassword'] = $toform['password'];
502             unset($toform['password']);
503         }
505         // Load any settings belonging to the access rules.
506         if (!empty($toform['instance'])) {
507             $accesssettings = quiz_access_manager::load_settings($toform['instance']);
508             foreach ($accesssettings as $name => $value) {
509                 $toform[$name] = $value;
510             }
511         }
512     }
514     public function validation($data, $files) {
515         $errors = parent::validation($data, $files);
517         // Check open and close times are consistent.
518         if ($data['timeopen'] != 0 && $data['timeclose'] != 0 &&
519                 $data['timeclose'] < $data['timeopen']) {
520             $errors['timeclose'] = get_string('closebeforeopen', 'quiz');
521         }
523         // Check that the grace period is not too short.
524         if ($data['overduehandling'] == 'graceperiod') {
525             $graceperiodmin = get_config('quiz', 'graceperiodmin');
526             if ($data['graceperiod'] <= $graceperiodmin) {
527                 $errors['graceperiod'] = get_string('graceperiodtoosmall', 'quiz', format_time($graceperiodmin));
528             }
529         }
531         // Check the boundary value is a number or a percentage, and in range.
532         $i = 0;
533         while (!empty($data['feedbackboundaries'][$i] )) {
534             $boundary = trim($data['feedbackboundaries'][$i]);
535             if (strlen($boundary) > 0) {
536                 if ($boundary[strlen($boundary) - 1] == '%') {
537                     $boundary = trim(substr($boundary, 0, -1));
538                     if (is_numeric($boundary)) {
539                         $boundary = $boundary * $data['grade'] / 100.0;
540                     } else {
541                         $errors["feedbackboundaries[$i]"] =
542                                 get_string('feedbackerrorboundaryformat', 'quiz', $i + 1);
543                     }
544                 } else if (!is_numeric($boundary)) {
545                     $errors["feedbackboundaries[$i]"] =
546                             get_string('feedbackerrorboundaryformat', 'quiz', $i + 1);
547                 }
548             }
549             if (is_numeric($boundary) && $boundary <= 0 || $boundary >= $data['grade'] ) {
550                 $errors["feedbackboundaries[$i]"] =
551                         get_string('feedbackerrorboundaryoutofrange', 'quiz', $i + 1);
552             }
553             if (is_numeric($boundary) && $i > 0 &&
554                     $boundary >= $data['feedbackboundaries'][$i - 1]) {
555                 $errors["feedbackboundaries[$i]"] =
556                         get_string('feedbackerrororder', 'quiz', $i + 1);
557             }
558             $data['feedbackboundaries'][$i] = $boundary;
559             $i += 1;
560         }
561         $numboundaries = $i;
563         // Check there is nothing in the remaining unused fields.
564         if (!empty($data['feedbackboundaries'])) {
565             for ($i = $numboundaries; $i < count($data['feedbackboundaries']); $i += 1) {
566                 if (!empty($data['feedbackboundaries'][$i] ) &&
567                         trim($data['feedbackboundaries'][$i] ) != '') {
568                     $errors["feedbackboundaries[$i]"] =
569                             get_string('feedbackerrorjunkinboundary', 'quiz', $i + 1);
570                 }
571             }
572         }
573         for ($i = $numboundaries + 1; $i < count($data['feedbacktext']); $i += 1) {
574             if (!empty($data['feedbacktext'][$i]['text']) &&
575                     trim($data['feedbacktext'][$i]['text'] ) != '') {
576                 $errors["feedbacktext[$i]"] =
577                         get_string('feedbackerrorjunkinfeedback', 'quiz', $i + 1);
578             }
579         }
581         // Any other rule plugins.
582         $errors = quiz_access_manager::validate_settings_form_fields($errors, $data, $files, $this);
584         return $errors;
585     }