MDL-22388 Added some checks to kill these scripts dead with an unequivocal notice...
[moodle.git] / mod / quiz / mod_form.php
1 <?php
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 ///////////////////////////////////////////////////////////////////////////
26 if (!defined('MOODLE_INTERNAL')) {
27     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
28 }
30 require_once($CFG->dirroot . '/course/moodleform_mod.php');
31 require_once($CFG->dirroot . '/mod/quiz/locallib.php');
33 /**
34  * Settings form for the quiz module.
35  *
36  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
37  * @package quiz
38  */
39 class mod_quiz_mod_form extends moodleform_mod {
40     var $_feedbacks;
42     function definition() {
44         global $COURSE, $CFG, $DB, $PAGE;
45         $quizconfig = get_config('quiz');
46         $mform    =& $this->_form;
48 //-------------------------------------------------------------------------------
49         $mform->addElement('header', 'general', get_string('general', 'form'));
51     /// Name.
52         $mform->addElement('text', 'name', get_string('name'), array('size'=>'64'));
53         if (!empty($CFG->formatstringstriptags)) {
54             $mform->setType('name', PARAM_TEXT);
55         } else {
56             $mform->setType('name', PARAM_CLEAN);
57         }
58         $mform->addRule('name', null, 'required', null, 'client');
60     /// Introduction.
61         $this->add_intro_editor(false, get_string('introduction', 'quiz'));
63     /// Open and close dates.
64         $mform->addElement('date_time_selector', 'timeopen', get_string('quizopen', 'quiz'), array('optional' => true));
65         $mform->setHelpButton('timeopen', array('timeopen', get_string('quizopen', 'quiz'), 'quiz'));
67         $mform->addElement('date_time_selector', 'timeclose', get_string('quizclose', 'quiz'), array('optional' => true));
68         $mform->setHelpButton('timeclose', array('timeopen', get_string('quizclose', 'quiz'), 'quiz'));
70     /// Time limit.
71         $mform->addElement('duration', 'timelimit', get_string('timelimit', 'quiz'), array('optional' => true));
72         $mform->setHelpButton('timelimit', array('timelimit', get_string('quiztimer','quiz'), 'quiz'));
73         $mform->setAdvanced('timelimit', $quizconfig->timelimit_adv);
74         $mform->setDefault('timelimit', $quizconfig->timelimit);
76     /// Number of attempts.
77         $attemptoptions = array('0' => get_string('unlimited'));
78         for ($i = 1; $i <= QUIZ_MAX_ATTEMPT_OPTION; $i++) {
79             $attemptoptions[$i] = $i;
80         }
81         $mform->addElement('select', 'attempts', get_string('attemptsallowed', 'quiz'), $attemptoptions);
82         $mform->setHelpButton('attempts', array('attempts', get_string('attemptsallowed','quiz'), 'quiz'));
83         $mform->setAdvanced('attempts', $quizconfig->attempts_adv);
84         $mform->setDefault('attempts', $quizconfig->attempts);
86     /// Grading method.
87         $mform->addElement('select', 'grademethod', get_string('grademethod', 'quiz'), quiz_get_grading_options());
88         $mform->setHelpButton('grademethod', array('grademethod', get_string('grademethod','quiz'), 'quiz'));
89         $mform->setAdvanced('grademethod', $quizconfig->grademethod_adv);
90         $mform->setDefault('grademethod', $quizconfig->grademethod);
91         $mform->disabledIf('grademethod', 'attempts', 'eq', 1);
93 //-------------------------------------------------------------------------------
94         $mform->addElement('header', 'layouthdr', get_string('layout', 'quiz'));
96     /// Shuffle questions.
97         $shuffleoptions = array(0 => get_string('asshownoneditscreen', 'quiz'), 1 => get_string('shuffledrandomly', 'quiz'));
98         $mform->addElement('select', 'shufflequestions', get_string('questionorder', 'quiz'), $shuffleoptions, array('id' => 'id_shufflequestions'));
99         $mform->setHelpButton('shufflequestions', array('shufflequestions', get_string('shufflequestions','quiz'), 'quiz'));
100         $mform->setAdvanced('shufflequestions', $quizconfig->shufflequestions_adv);
101         $mform->setDefault('shufflequestions', $quizconfig->shufflequestions);
103     /// Questions per page.
104         $pageoptions = array();
105         $pageoptions[0] = get_string('neverallononepage', 'quiz');
106         $pageoptions[1] = get_string('everyquestion', 'quiz');
107         for ($i = 2; $i <= QUIZ_MAX_QPP_OPTION; ++$i) {
108             $pageoptions[$i] = get_string('everynquestions', 'quiz', $i);
109         }
111         $pagegroup = array();
112         $pagegroup[] = &$mform->createElement('select', 'questionsperpage', get_string('newpage', 'quiz'), $pageoptions, array('id' => 'id_questionsperpage'));
113         $mform->setDefault('questionsperpage', $quizconfig->questionsperpage);
115         if (!empty($this->_cm)) {
116             $pagegroup[] = &$mform->createElement('checkbox', 'repaginatenow', '', get_string('repaginatenow', 'quiz'), array('id' => 'id_repaginatenow'));
117             $mform->disabledIf('repaginatenow', 'shufflequestions', 'eq', 1);
118             $PAGE->requires->yui2_lib('event');
119             $PAGE->requires->js('/mod/quiz/edit.js');
120         }
122         $mform->addGroup($pagegroup, 'questionsperpagegrp', get_string('newpage', 'quiz'), null, false);
123         $mform->setHelpButton('questionsperpagegrp', array('questionsperpage', get_string('newpageevery', 'quiz'), 'quiz'));
124         $mform->setAdvanced('questionsperpagegrp', $quizconfig->questionsperpage_adv);
126 //-------------------------------------------------------------------------------
127         $mform->addElement('header', 'interactionhdr', get_string('questionbehaviour', 'quiz'));
129     /// Shuffle within questions.
130         $mform->addElement('selectyesno', 'shuffleanswers', get_string('shufflewithin', 'quiz'));
131         $mform->setHelpButton('shuffleanswers', array('shufflewithin', get_string('shufflewithin','quiz'), 'quiz'));
132         $mform->setAdvanced('shuffleanswers', $quizconfig->shuffleanswers_adv);
133         $mform->setDefault('shuffleanswers', $quizconfig->shuffleanswers);
135     /// Adaptive mode.
136         $mform->addElement('selectyesno', 'adaptive', get_string('adaptive', 'quiz'));
137         $mform->setHelpButton('adaptive', array('adaptive', get_string('adaptive','quiz'), 'quiz'));
138         $mform->setAdvanced('adaptive', $quizconfig->optionflags_adv);
139         $mform->setDefault('adaptive', $quizconfig->optionflags & QUESTION_ADAPTIVE);
141     /// Apply penalties.
142         $mform->addElement('selectyesno', 'penaltyscheme', get_string('penaltyscheme', 'quiz'));
143         $mform->setHelpButton('penaltyscheme', array('penaltyscheme', get_string('penaltyscheme','quiz'), 'quiz'));
144         $mform->setAdvanced('penaltyscheme', $quizconfig->penaltyscheme_adv);
145         $mform->setDefault('penaltyscheme', $quizconfig->penaltyscheme);
146         $mform->disabledIf('penaltyscheme', 'adaptive', 'neq', 1);
148     /// Each attempt builds on last.
149         $mform->addElement('selectyesno', 'attemptonlast', get_string('eachattemptbuildsonthelast', 'quiz'));
150         $mform->setHelpButton('attemptonlast', array('repeatattempts', get_string('eachattemptbuildsonthelast', 'quiz'), 'quiz'));
151         $mform->setAdvanced('attemptonlast', $quizconfig->attemptonlast_adv);
152         $mform->setDefault('attemptonlast', $quizconfig->attemptonlast);
153         $mform->disabledIf('attemptonlast', 'attempts', 'eq', 1);
155 //-------------------------------------------------------------------------------
156         $mform->addElement('header', 'reviewoptionshdr', get_string('reviewoptionsheading', 'quiz'));
157         $mform->setHelpButton('reviewoptionshdr', array('reviewoptions', get_string('reviewoptionsheading','quiz'), 'quiz'));
158         $mform->setAdvanced('reviewoptionshdr', $quizconfig->review_adv);
160     /// Review options.
161         $immediatelyoptionsgrp=array();
162         $immediatelyoptionsgrp[] = &$mform->createElement('checkbox', 'responsesimmediately', '', get_string('responses', 'quiz'));
163         $immediatelyoptionsgrp[] = &$mform->createElement('checkbox', 'answersimmediately', '', get_string('answers', 'quiz'));
164         $immediatelyoptionsgrp[] = &$mform->createElement('checkbox', 'feedbackimmediately', '', get_string('feedback', 'quiz'));
165         $immediatelyoptionsgrp[] = &$mform->createElement('checkbox', 'generalfeedbackimmediately', '', get_string('generalfeedback', 'quiz'));
166         $immediatelyoptionsgrp[] = &$mform->createElement('checkbox', 'scoreimmediately', '', get_string('scores', 'quiz'));
167         $immediatelyoptionsgrp[] = &$mform->createElement('checkbox', 'overallfeedbackimmediately', '', get_string('overallfeedback', 'quiz'));
168         $mform->addGroup($immediatelyoptionsgrp, 'immediatelyoptionsgrp', get_string('reviewimmediately', 'quiz'), null, false);
169         $mform->setDefault('responsesimmediately', $quizconfig->review & QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_IMMEDIATELY);
170         $mform->setDefault('answersimmediately', $quizconfig->review & QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_IMMEDIATELY);
171         $mform->setDefault('feedbackimmediately', $quizconfig->review & QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_IMMEDIATELY);
172         $mform->setDefault('generalfeedbackimmediately', $quizconfig->review & QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_IMMEDIATELY);
173         $mform->setDefault('scoreimmediately', $quizconfig->review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_IMMEDIATELY);
174         $mform->setDefault('overallfeedbackimmediately', $quizconfig->review & QUIZ_REVIEW_OVERALLFEEDBACK & QUIZ_REVIEW_IMMEDIATELY);
176         $openoptionsgrp=array();
177         $openoptionsgrp[] = &$mform->createElement('checkbox', 'responsesopen', '', get_string('responses', 'quiz'));
178         $openoptionsgrp[] = &$mform->createElement('checkbox', 'answersopen', '', get_string('answers', 'quiz'));
179         $openoptionsgrp[] = &$mform->createElement('checkbox', 'feedbackopen', '', get_string('feedback', 'quiz'));
180         $openoptionsgrp[] = &$mform->createElement('checkbox', 'generalfeedbackopen', '', get_string('generalfeedback', 'quiz'));
181         $openoptionsgrp[] = &$mform->createElement('checkbox', 'scoreopen', '', get_string('scores', 'quiz'));
182         $openoptionsgrp[] = &$mform->createElement('checkbox', 'overallfeedbackopen', '', get_string('overallfeedback', 'quiz'));
183         $mform->addGroup($openoptionsgrp, 'openoptionsgrp', get_string('reviewopen', 'quiz'), array(' '), false);
184         $mform->setDefault('responsesopen', $quizconfig->review & QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_OPEN);
185         $mform->setDefault('answersopen', $quizconfig->review & QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_OPEN);
186         $mform->setDefault('feedbackopen', $quizconfig->review & QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_OPEN);
187         $mform->setDefault('generalfeedbackopen', $quizconfig->review & QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_OPEN);
188         $mform->setDefault('scoreopen', $quizconfig->review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_OPEN);
189         $mform->setDefault('overallfeedbackopen', $quizconfig->review & QUIZ_REVIEW_OVERALLFEEDBACK & QUIZ_REVIEW_OPEN);
191         $closedoptionsgrp=array();
192         $closedoptionsgrp[] = &$mform->createElement('checkbox', 'responsesclosed', '', get_string('responses', 'quiz'));
193         $closedoptionsgrp[] = &$mform->createElement('checkbox', 'answersclosed', '', get_string('answers', 'quiz'));
194         $closedoptionsgrp[] = &$mform->createElement('checkbox', 'feedbackclosed', '', get_string('feedback', 'quiz'));
195         $closedoptionsgrp[] = &$mform->createElement('checkbox', 'generalfeedbackclosed', '', get_string('generalfeedback', 'quiz'));
196         $closedoptionsgrp[] = &$mform->createElement('checkbox', 'scoreclosed', '', get_string('scores', 'quiz'));
197         $closedoptionsgrp[] = &$mform->createElement('checkbox', 'overallfeedbackclosed', '', get_string('overallfeedback', 'quiz'));
198         $mform->addGroup($closedoptionsgrp, 'closedoptionsgrp', get_string('reviewclosed', 'quiz'), array(' '), false);
199         $mform->setDefault('responsesclosed', $quizconfig->review & QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_CLOSED);
200         $mform->setDefault('answersclosed', $quizconfig->review & QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_CLOSED);
201         $mform->setDefault('feedbackclosed', $quizconfig->review & QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_CLOSED);
202         $mform->setDefault('generalfeedbackclosed', $quizconfig->review & QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_CLOSED);
203         $mform->setDefault('scoreclosed', $quizconfig->review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_CLOSED);
204         $mform->setDefault('overallfeedbackclosed', $quizconfig->review & QUIZ_REVIEW_OVERALLFEEDBACK & QUIZ_REVIEW_CLOSED);
205         $mform->disabledIf('closedoptionsgrp', 'timeclose[enabled]');
207 //-------------------------------------------------------------------------------
208         $mform->addElement('header', 'display', get_string('display', 'form'));
210     /// Show user picture.
211         $mform->addElement('selectyesno', 'showuserpicture', get_string('showuserpicture', 'quiz'));
212         $mform->setHelpButton('showuserpicture', array('showuserpicture', get_string('showuserpicture', 'quiz'), 'quiz'));
213         $mform->setAdvanced('showuserpicture', $quizconfig->showuserpicture_adv);
214         $mform->setDefault('showuserpicture', $quizconfig->showuserpicture);
216     /// Overall decimal points.
217         $options = array();
218         for ($i = 0; $i <= QUIZ_MAX_DECIMAL_OPTION; $i++) {
219             $options[$i] = $i;
220         }
221         $mform->addElement('select', 'decimalpoints', get_string('decimalplaces', 'quiz'), $options);
222         $mform->setHelpButton('decimalpoints', array('decimalpoints', get_string('decimalplaces','quiz'), 'quiz'));
223         $mform->setAdvanced('decimalpoints', $quizconfig->decimalpoints_adv);
224         $mform->setDefault('decimalpoints', $quizconfig->decimalpoints);
226     /// Question decimal points.
227         $options = array(-1 => get_string('sameasoverall', 'quiz'));
228         for ($i = 0; $i <= QUIZ_MAX_Q_DECIMAL_OPTION; $i++) {
229             $options[$i] = $i;
230         }
231         $mform->addElement('select', 'questiondecimalpoints', get_string('decimalplacesquestion', 'quiz'), $options);
232         $mform->setHelpButton('questiondecimalpoints', array('decimalplacesquestion', get_string('decimalplacesquestion','quiz'), 'quiz'));
233         $mform->setAdvanced('questiondecimalpoints', $quizconfig->questiondecimalpoints_adv);
234         $mform->setDefault('questiondecimalpoints', $quizconfig->questiondecimalpoints);
236 //-------------------------------------------------------------------------------
237         $mform->addElement('header', 'security', get_string('extraattemptrestrictions', 'quiz'));
239     /// Enforced time delay between quiz attempts.
240         $mform->addElement('passwordunmask', 'quizpassword', get_string('requirepassword', 'quiz'));
241         $mform->setType('quizpassword', PARAM_TEXT);
242         $mform->setHelpButton('quizpassword', array('requirepassword', get_string('requirepassword', 'quiz'), 'quiz'));
243         $mform->setAdvanced('quizpassword', $quizconfig->password_adv);
244         $mform->setDefault('quizpassword', $quizconfig->password);
246     /// IP address.
247         $mform->addElement('text', 'subnet', get_string('requiresubnet', 'quiz'));
248         $mform->setType('subnet', PARAM_TEXT);
249         $mform->setHelpButton('subnet', array('requiresubnet', get_string('requiresubnet', 'quiz'), 'quiz'));
250         $mform->setAdvanced('subnet', $quizconfig->subnet_adv);
251         $mform->setDefault('subnet', $quizconfig->subnet);
253     /// Enforced time delay between quiz attempts.
254         $mform->addElement('duration', 'delay1', get_string('delay1st2nd', 'quiz'), array('optional' => true));
255         $mform->setHelpButton('delay1', array('timedelay1', get_string('delay1st2nd', 'quiz'), 'quiz'));
256         $mform->setAdvanced('delay1', $quizconfig->delay1_adv);
257         $mform->setDefault('delay1', $quizconfig->delay1);
258         $mform->disabledIf('delay1', 'attempts', 'eq', 1);
260         $mform->addElement('duration', 'delay2', get_string('delaylater', 'quiz'), array('optional' => true));
261         $mform->setHelpButton('delay2', array('timedelay2', get_string('delaylater', 'quiz'), 'quiz'));
262         $mform->setAdvanced('delay2', $quizconfig->delay2_adv);
263         $mform->setDefault('delay2', $quizconfig->delay2);
264         $mform->disabledIf('delay2', 'attempts', 'eq', 1);
265         $mform->disabledIf('delay2', 'attempts', 'eq', 2);
267     /// 'Secure' window.
268         $options = array(
269                     0 => get_string('none', 'quiz'),
270                     1 => get_string('popupwithjavascriptsupport', 'quiz'));
271         if (!empty($CFG->enablesafebrowserintegration)) {
272             $options[2] = get_string('requiresafeexambrowser', 'quiz');
273         }
274         $mform->addElement('select', 'popup', get_string('browsersecurity', 'quiz'), $options);
275         $mform->setHelpButton('popup', array('browsersecurity', get_string('browsersecurity', 'quiz'), 'quiz'));
276         $mform->setAdvanced('popup', $quizconfig->popup_adv);
277         $mform->setDefault('popup', $quizconfig->popup);
279 //-------------------------------------------------------------------------------
280         $mform->addElement('header', 'overallfeedbackhdr', get_string('overallfeedback', 'quiz'));
281         $mform->setHelpButton('overallfeedbackhdr', array('overallfeedback', get_string('overallfeedback', 'quiz'), 'quiz'));
283         $mform->addElement('hidden', 'grade', $quizconfig->maximumgrade);
284         $mform->setType('grade', PARAM_RAW);
285         if (empty($this->_cm)) {
286             $needwarning = $quizconfig->maximumgrade == 0;
287         } else {
288             $quizgrade = $DB->get_field('quiz', 'grade', array('id' => $this->_instance));
289             $needwarning = $quizgrade == 0;
290         }
291         if ($needwarning) {
292             $mform->addElement('static', 'nogradewarning', '', get_string('nogradewarning', 'quiz'));
293         }
295         $mform->addElement('static', 'gradeboundarystatic1', get_string('gradeboundary', 'quiz'), '100%');
297         $repeatarray = array();
298         $repeatarray[] = &MoodleQuickForm::createElement('text', 'feedbacktext', get_string('feedback', 'quiz'), array('size' => 50));
299         $mform->setType('feedbacktext', PARAM_RAW);
300         $repeatarray[] = &MoodleQuickForm::createElement('text', 'feedbackboundaries', get_string('gradeboundary', 'quiz'), array('size' => 10));
301         $mform->setType('feedbackboundaries', PARAM_NOTAGS);
303         if (!empty($this->_instance)) {
304             $this->_feedbacks = $DB->get_records('quiz_feedback', array('quizid'=>$this->_instance), 'mingrade DESC');
305         } else {
306             $this->_feedbacks = array();
307         }
308         $numfeedbacks = max(count($this->_feedbacks) * 1.5, 5);
310         $nextel=$this->repeat_elements($repeatarray, $numfeedbacks - 1,
311                 array(), 'boundary_repeats', 'boundary_add_fields', 3,
312                 get_string('addmoreoverallfeedbacks', 'quiz'), true);
314         // Put some extra elements in before the button
315         $insertEl = &MoodleQuickForm::createElement('text', "feedbacktext[$nextel]", get_string('feedback', 'quiz'), array('size' => 50));
316         $mform->insertElementBefore($insertEl, 'boundary_add_fields');
318         $insertEl = &MoodleQuickForm::createElement('static', 'gradeboundarystatic2', get_string('gradeboundary', 'quiz'), '0%');
319         $mform->insertElementBefore($insertEl, 'boundary_add_fields');
321         // Add the disabledif rules. We cannot do this using the $repeatoptions parameter to
322         // repeat_elements becuase we don't want to dissable the first feedbacktext.
323         for ($i = 0; $i < $nextel; $i++) {
324             $mform->disabledIf('feedbackboundaries[' . $i . ']', 'grade', 'eq', 0);
325             $mform->disabledIf('feedbacktext[' . ($i + 1) . ']', 'grade', 'eq', 0);
326         }
328 //-------------------------------------------------------------------------------
329         $this->standard_coursemodule_elements();
331 //-------------------------------------------------------------------------------
332         // buttons
333         $this->add_action_buttons();
334     }
336     function data_preprocessing(&$default_values){
337         if (isset($default_values['grade'])) {
338             $default_values['grade'] = $default_values['grade'] + 0; // Convert to a real number, so we don't get 0.0000.
339         }
341         if (count($this->_feedbacks)) {
342             $key = 0;
343             foreach ($this->_feedbacks as $feedback){
344                 $default_values['feedbacktext['.$key.']'] = $feedback->feedbacktext;
345                 if ($feedback->mingrade > 0) {
346                     $default_values['feedbackboundaries['.$key.']'] = (100.0 * $feedback->mingrade / $default_values['grade']) . '%';
347                 }
348                 $key++;
349             }
350         }
352         if (isset($default_values['timelimit'])) {
353             $default_values['timelimitenable'] = $default_values['timelimit'] > 0;
354         }
356         if (isset($default_values['review'])){
357             $review = (int)$default_values['review'];
358             unset($default_values['review']);
360             $default_values['responsesimmediately'] = $review & QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_IMMEDIATELY;
361             $default_values['answersimmediately'] = $review & QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_IMMEDIATELY;
362             $default_values['feedbackimmediately'] = $review & QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_IMMEDIATELY;
363             $default_values['generalfeedbackimmediately'] = $review & QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_IMMEDIATELY;
364             $default_values['scoreimmediately'] = $review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_IMMEDIATELY;
365             $default_values['overallfeedbackimmediately'] = $review & QUIZ_REVIEW_OVERALLFEEDBACK & QUIZ_REVIEW_IMMEDIATELY;
367             $default_values['responsesopen'] = $review & QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_OPEN;
368             $default_values['answersopen'] = $review & QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_OPEN;
369             $default_values['feedbackopen'] = $review & QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_OPEN;
370             $default_values['generalfeedbackopen'] = $review & QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_OPEN;
371             $default_values['scoreopen'] = $review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_OPEN;
372             $default_values['overallfeedbackopen'] = $review & QUIZ_REVIEW_OVERALLFEEDBACK & QUIZ_REVIEW_OPEN;
374             $default_values['responsesclosed'] = $review & QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_CLOSED;
375             $default_values['answersclosed'] = $review & QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_CLOSED;
376             $default_values['feedbackclosed'] = $review & QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_CLOSED;
377             $default_values['generalfeedbackclosed'] = $review & QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_CLOSED;
378             $default_values['scoreclosed'] = $review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_CLOSED;
379             $default_values['overallfeedbackclosed'] = $review & QUIZ_REVIEW_OVERALLFEEDBACK & QUIZ_REVIEW_CLOSED;
380         }
382         if (isset($default_values['optionflags'])){
383             $default_values['adaptive'] = $default_values['optionflags'] & QUESTION_ADAPTIVE;
384             unset($default_values['optionflags']);
385         }
387         // Password field - different in form to stop browsers that remember passwords
388         // getting confused.
389         if (isset($default_values['password'])) {
390             $default_values['quizpassword'] = $default_values['password'];
391             unset($default_values['password']);
392         }
393     }
395     function validation($data, $files) {
396         $errors = parent::validation($data, $files);
398         // Check open and close times are consistent.
399         if ($data['timeopen'] != 0 && $data['timeclose'] != 0 && $data['timeclose'] < $data['timeopen']) {
400             $errors['timeclose'] = get_string('closebeforeopen', 'quiz');
401         }
403         // Check the boundary value is a number or a percentage, and in range.
404         $i = 0;
405         while (!empty($data['feedbackboundaries'][$i] )) {
406             $boundary = trim($data['feedbackboundaries'][$i]);
407             if (strlen($boundary) > 0 && $boundary[strlen($boundary) - 1] == '%') {
408                 $boundary = trim(substr($boundary, 0, -1));
409                 if (is_numeric($boundary)) {
410                     $boundary = $boundary * $data['grade'] / 100.0;
411                 } else {
412                     $errors["feedbackboundaries[$i]"] = get_string('feedbackerrorboundaryformat', 'quiz', $i + 1);
413                 }
414             }
415             if (is_numeric($boundary) && $boundary <= 0 || $boundary >= $data['grade'] ) {
416                 $errors["feedbackboundaries[$i]"] = get_string('feedbackerrorboundaryoutofrange', 'quiz', $i + 1);
417             }
418             if (is_numeric($boundary) && $i > 0 && $boundary >= $data['feedbackboundaries'][$i - 1]) {
419                 $errors["feedbackboundaries[$i]"] = get_string('feedbackerrororder', 'quiz', $i + 1);
420             }
421             $data['feedbackboundaries'][$i] = $boundary;
422             $i += 1;
423         }
424         $numboundaries = $i;
426         // Check there is nothing in the remaining unused fields.
427         if (!empty($data['feedbackboundaries'])) {
428             for ($i = $numboundaries; $i < count($data['feedbackboundaries']); $i += 1) {
429                 if (!empty($data['feedbackboundaries'][$i] ) && trim($data['feedbackboundaries'][$i] ) != '') {
430                     $errors["feedbackboundaries[$i]"] = get_string('feedbackerrorjunkinboundary', 'quiz', $i + 1);
431                 }
432             }
433         }
434         for ($i = $numboundaries + 1; $i < count($data['feedbacktext']); $i += 1) {
435             if (!empty($data['feedbacktext'][$i] ) && trim($data['feedbacktext'][$i] ) != '') {
436                 $errors["feedbacktext[$i]"] = get_string('feedbackerrorjunkinfeedback', 'quiz', $i + 1);
437             }
438         }
440         return $errors;
441     }