MDL-18910 moving modedit features to modname_supports()
[moodle.git] / mod / quiz / mod_form.php
1 <?php // $Id$
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 require_once($CFG->dirroot . '/course/moodleform_mod.php');
27 require_once($CFG->dirroot . '/mod/quiz/locallib.php');
29 /**
30  * Settings form for the quiz module.
31  * 
32  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
33  * @package quiz
34  */
35 class mod_quiz_mod_form extends moodleform_mod {
36     var $_feedbacks;
38     function definition() {
40         global $COURSE, $CFG, $DB;
41         $quizconfig = get_config('quiz');
42         $mform    =& $this->_form;
44 //-------------------------------------------------------------------------------
45         $mform->addElement('header', 'general', get_string('general', 'form'));
47     /// Name.
48         $mform->addElement('text', 'name', get_string('name'), array('size'=>'64'));
49         if (!empty($CFG->formatstringstriptags)) {
50             $mform->setType('name', PARAM_TEXT);
51         } else {
52             $mform->setType('name', PARAM_CLEAN);
53         }
54         $mform->addRule('name', null, 'required', null, 'client');
56     /// Introduction.
57         $mform->addElement('htmleditor', 'intro', get_string('introduction', 'quiz'));
58         $mform->setType('intro', PARAM_RAW);
59         $mform->setHelpButton('intro', array('richtext2', get_string('helprichtext')));
61     /// Open and close dates.
62         $mform->addElement('date_time_selector', 'timeopen', get_string('quizopen', 'quiz'), array('optional' => true));
63         $mform->setHelpButton('timeopen', array('timeopen', get_string('quizopen', 'quiz'), 'quiz'));
65         $mform->addElement('date_time_selector', 'timeclose', get_string('quizclose', 'quiz'), array('optional' => true));
66         $mform->setHelpButton('timeclose', array('timeopen', get_string('quizclose', 'quiz'), 'quiz'));
68     /// Time limit.
69         $mform->addElement('duration', 'timelimit', get_string('timelimit', 'quiz'), array('optional' => true));
70         $mform->setHelpButton('timelimit', array('timelimit', get_string('quiztimer','quiz'), 'quiz'));
71         $mform->setAdvanced('timelimit', $quizconfig->fix_timelimit);
72         $mform->setDefault('timelimit', $quizconfig->timelimit);
74     /// Number of attempts.
75         $attemptoptions = array('0' => get_string('unlimited'));
76         for ($i = 1; $i <= QUIZ_MAX_ATTEMPT_OPTION; $i++) {
77             $attemptoptions[$i] = $i;
78         }
79         $mform->addElement('select', 'attempts', get_string('attemptsallowed', 'quiz'), $attemptoptions);
80         $mform->setHelpButton('attempts', array('attempts', get_string('attemptsallowed','quiz'), 'quiz'));
81         $mform->setAdvanced('attempts', $quizconfig->fix_attempts);
82         $mform->setDefault('attempts', $quizconfig->attempts);
84     /// Grading method.
85         $mform->addElement('select', 'grademethod', get_string('grademethod', 'quiz'), quiz_get_grading_options());
86         $mform->setHelpButton('grademethod', array('grademethod', get_string('grademethod','quiz'), 'quiz'));
87         $mform->setAdvanced('grademethod', $quizconfig->fix_grademethod);
88         $mform->setDefault('grademethod', $quizconfig->grademethod);
89         $mform->disabledIf('grademethod', 'attempts', 'eq', 1);
91 //-------------------------------------------------------------------------------
92         $mform->addElement('header', 'layouthdr', get_string('layout', 'quiz'));
94     /// Shuffle questions.
95         $shuffleoptions = array(0 => get_string('asshownoneditscreen', 'quiz'), 1 => get_string('shuffledrandomly', 'quiz'));
96         $mform->addElement('select', 'shufflequestions', get_string('questionorder', 'quiz'), $shuffleoptions, array('id' => 'id_shufflequestions'));
97         $mform->setHelpButton('shufflequestions', array('shufflequestions', get_string('shufflequestions','quiz'), 'quiz'));
98         $mform->setAdvanced('shufflequestions', $quizconfig->fix_shufflequestions);
99         $mform->setDefault('shufflequestions', $quizconfig->shufflequestions);
101     /// Questions per page.
102         $pageoptions = array();
103         $pageoptions[0] = get_string('neverallononepage', 'quiz');
104         $pageoptions[1] = get_string('everyquestion', 'quiz');
105         for ($i = 2; $i <= QUIZ_MAX_QPP_OPTION; ++$i) {
106             $pageoptions[$i] = get_string('everynquestions', 'quiz', $i);
107         }
109         $pagegroup = array();
110         $pagegroup[] = &$mform->createElement('select', 'questionsperpage', get_string('newpage', 'quiz'), $pageoptions, array('id' => 'id_questionsperpage'));
111         $mform->setDefault('questionsperpage', $quizconfig->questionsperpage);
113         if (!empty($this->_cm)) {
114             $pagegroup[] = &$mform->createElement('checkbox', 'repaginatenow', '', get_string('repaginatenow', 'quiz'), array('id' => 'id_repaginatenow'));
115             $mform->disabledIf('repaginatenow', 'shufflequestions', 'eq', 1);
116             require_js(array('yui_yahoo', 'yui_dom', 'yui_event'));
117             require_js('mod/quiz/edit.js');
118         }
120         $mform->addGroup($pagegroup, 'questionsperpagegrp', get_string('newpage', 'quiz'), null, false);
121         $mform->setHelpButton('questionsperpagegrp', array('questionsperpage', get_string('newpageevery', 'quiz'), 'quiz'));
122         $mform->setAdvanced('questionsperpagegrp', $quizconfig->fix_questionsperpage);
124 //-------------------------------------------------------------------------------
125         $mform->addElement('header', 'interactionhdr', get_string('questionbehaviour', 'quiz'));
127     /// Shuffle within questions.
128         $mform->addElement('selectyesno', 'shuffleanswers', get_string('shufflewithin', 'quiz'));
129         $mform->setHelpButton('shuffleanswers', array('shufflewithin', get_string('shufflewithin','quiz'), 'quiz'));
130         $mform->setAdvanced('shuffleanswers', $quizconfig->fix_shuffleanswers);
131         $mform->setDefault('shuffleanswers', $quizconfig->shuffleanswers);
133     /// Adaptive mode.
134         $mform->addElement('selectyesno', 'adaptive', get_string('adaptive', 'quiz'));
135         $mform->setHelpButton('adaptive', array('adaptive', get_string('adaptive','quiz'), 'quiz'));
136         $mform->setAdvanced('adaptive', $quizconfig->fix_optionflags);
137         $mform->setDefault('adaptive', $quizconfig->optionflags & QUESTION_ADAPTIVE);
139     /// Apply penalties.
140         $mform->addElement('selectyesno', 'penaltyscheme', get_string('penaltyscheme', 'quiz'));
141         $mform->setHelpButton('penaltyscheme', array('penaltyscheme', get_string('penaltyscheme','quiz'), 'quiz'));
142         $mform->setAdvanced('penaltyscheme', $quizconfig->fix_penaltyscheme);
143         $mform->setDefault('penaltyscheme', $quizconfig->penaltyscheme);
144         $mform->disabledIf('penaltyscheme', 'adaptive', 'neq', 1);
146     /// Each attempt builds on last.
147         $mform->addElement('selectyesno', 'attemptonlast', get_string('eachattemptbuildsonthelast', 'quiz'));
148         $mform->setHelpButton('attemptonlast', array('repeatattempts', get_string('eachattemptbuildsonthelast', 'quiz'), 'quiz'));
149         $mform->setAdvanced('attemptonlast', $quizconfig->fix_attemptonlast);
150         $mform->setDefault('attemptonlast', $quizconfig->attemptonlast);
151         $mform->disabledIf('attemptonlast', 'attempts', 'eq', 1);
153 //-------------------------------------------------------------------------------
154         $mform->addElement('header', 'reviewoptionshdr', get_string('reviewoptionsheading', 'quiz'));
155         $mform->setHelpButton('reviewoptionshdr', array('reviewoptions', get_string('reviewoptionsheading','quiz'), 'quiz'));
156         $mform->setAdvanced('reviewoptionshdr', $quizconfig->fix_review);
158     /// Review options.
159         $immediatelyoptionsgrp=array();
160         $immediatelyoptionsgrp[] = &$mform->createElement('checkbox', 'responsesimmediately', '', get_string('responses', 'quiz'));
161         $immediatelyoptionsgrp[] = &$mform->createElement('checkbox', 'answersimmediately', '', get_string('answers', 'quiz'));
162         $immediatelyoptionsgrp[] = &$mform->createElement('checkbox', 'feedbackimmediately', '', get_string('feedback', 'quiz'));
163         $immediatelyoptionsgrp[] = &$mform->createElement('checkbox', 'generalfeedbackimmediately', '', get_string('generalfeedback', 'quiz'));
164         $immediatelyoptionsgrp[] = &$mform->createElement('checkbox', 'scoreimmediately', '', get_string('scores', 'quiz'));
165         $immediatelyoptionsgrp[] = &$mform->createElement('checkbox', 'overallfeedbackimmediately', '', get_string('overallfeedback', 'quiz'));
166         $mform->addGroup($immediatelyoptionsgrp, 'immediatelyoptionsgrp', get_string('reviewimmediately', 'quiz'), null, false);
167         $mform->setDefault('responsesimmediately', $quizconfig->review & QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_IMMEDIATELY);
168         $mform->setDefault('answersimmediately', $quizconfig->review & QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_IMMEDIATELY);
169         $mform->setDefault('feedbackimmediately', $quizconfig->review & QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_IMMEDIATELY);
170         $mform->setDefault('generalfeedbackimmediately', $quizconfig->review & QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_IMMEDIATELY);
171         $mform->setDefault('scoreimmediately', $quizconfig->review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_IMMEDIATELY);
172         $mform->setDefault('overallfeedbackimmediately', $quizconfig->review & QUIZ_REVIEW_OVERALLFEEDBACK & QUIZ_REVIEW_IMMEDIATELY);
174         $openoptionsgrp=array();
175         $openoptionsgrp[] = &$mform->createElement('checkbox', 'responsesopen', '', get_string('responses', 'quiz'));
176         $openoptionsgrp[] = &$mform->createElement('checkbox', 'answersopen', '', get_string('answers', 'quiz'));
177         $openoptionsgrp[] = &$mform->createElement('checkbox', 'feedbackopen', '', get_string('feedback', 'quiz'));
178         $openoptionsgrp[] = &$mform->createElement('checkbox', 'generalfeedbackopen', '', get_string('generalfeedback', 'quiz'));
179         $openoptionsgrp[] = &$mform->createElement('checkbox', 'scoreopen', '', get_string('scores', 'quiz'));
180         $openoptionsgrp[] = &$mform->createElement('checkbox', 'overallfeedbackopen', '', get_string('overallfeedback', 'quiz'));
181         $mform->addGroup($openoptionsgrp, 'openoptionsgrp', get_string('reviewopen', 'quiz'), array(' '), false);
182         $mform->setDefault('responsesopen', $quizconfig->review & QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_OPEN);
183         $mform->setDefault('answersopen', $quizconfig->review & QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_OPEN);
184         $mform->setDefault('feedbackopen', $quizconfig->review & QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_OPEN);
185         $mform->setDefault('generalfeedbackopen', $quizconfig->review & QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_OPEN);
186         $mform->setDefault('scoreopen', $quizconfig->review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_OPEN);
187         $mform->setDefault('overallfeedbackopen', $quizconfig->review & QUIZ_REVIEW_OVERALLFEEDBACK & QUIZ_REVIEW_OPEN);
189         $closedoptionsgrp=array();
190         $closedoptionsgrp[] = &$mform->createElement('checkbox', 'responsesclosed', '', get_string('responses', 'quiz'));
191         $closedoptionsgrp[] = &$mform->createElement('checkbox', 'answersclosed', '', get_string('answers', 'quiz'));
192         $closedoptionsgrp[] = &$mform->createElement('checkbox', 'feedbackclosed', '', get_string('feedback', 'quiz'));
193         $closedoptionsgrp[] = &$mform->createElement('checkbox', 'generalfeedbackclosed', '', get_string('generalfeedback', 'quiz'));
194         $closedoptionsgrp[] = &$mform->createElement('checkbox', 'scoreclosed', '', get_string('scores', 'quiz'));
195         $closedoptionsgrp[] = &$mform->createElement('checkbox', 'overallfeedbackclosed', '', get_string('overallfeedback', 'quiz'));
196         $mform->addGroup($closedoptionsgrp, 'closedoptionsgrp', get_string('reviewclosed', 'quiz'), array(' '), false);
197         $mform->setDefault('responsesclosed', $quizconfig->review & QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_CLOSED);
198         $mform->setDefault('answersclosed', $quizconfig->review & QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_CLOSED);
199         $mform->setDefault('feedbackclosed', $quizconfig->review & QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_CLOSED);
200         $mform->setDefault('generalfeedbackclosed', $quizconfig->review & QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_CLOSED);
201         $mform->setDefault('scoreclosed', $quizconfig->review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_CLOSED);
202         $mform->setDefault('overallfeedbackclosed', $quizconfig->review & QUIZ_REVIEW_OVERALLFEEDBACK & QUIZ_REVIEW_CLOSED);
203         $mform->disabledIf('closedoptionsgrp', 'timeclose[enabled]');
205 //-------------------------------------------------------------------------------
206         $mform->addElement('header', 'display', get_string('display', 'form'));
208     /// Show user picture.
209         $mform->addElement('selectyesno', 'showuserpicture', get_string('showuserpicture', 'quiz'));
210         $mform->setHelpButton('showuserpicture', array('showuserpicture', get_string('showuserpicture', 'quiz'), 'quiz'));
211         $mform->setAdvanced('showuserpicture', $quizconfig->fix_showuserpicture);
212         $mform->setDefault('showuserpicture', $quizconfig->showuserpicture);
214     /// Overall decimal points.
215         $options = array();
216         for ($i = 0; $i <= QUIZ_MAX_DECIMAL_OPTION; $i++) {
217             $options[$i] = $i;
218         }
219         $mform->addElement('select', 'decimalpoints', get_string('decimalplaces', 'quiz'), $options);
220         $mform->setHelpButton('decimalpoints', array('decimalpoints', get_string('decimalplaces','quiz'), 'quiz'));
221         $mform->setAdvanced('decimalpoints', $quizconfig->fix_decimalpoints);
222         $mform->setDefault('decimalpoints', $quizconfig->decimalpoints);
224     /// Question decimal points.
225         $options = array(-1 => get_string('sameasoverall', 'quiz'));
226         for ($i = 0; $i <= QUIZ_MAX_Q_DECIMAL_OPTION; $i++) {
227             $options[$i] = $i;
228         }
229         $mform->addElement('select', 'questiondecimalpoints', get_string('decimalplacesquestion', 'quiz'), $options);
230         $mform->setHelpButton('questiondecimalpoints', array('decimalplacesquestion', get_string('decimalplacesquestion','quiz'), 'quiz'));
231         $mform->setAdvanced('questiondecimalpoints', $quizconfig->fix_questiondecimalpoints);
232         $mform->setDefault('questiondecimalpoints', $quizconfig->questiondecimalpoints);
234 //-------------------------------------------------------------------------------
235         $mform->addElement('header', 'security', get_string('extraattemptrestrictions', 'quiz'));
237     /// Enforced time delay between quiz attempts.
238         $mform->addElement('passwordunmask', 'quizpassword', get_string('requirepassword', 'quiz'));
239         $mform->setType('quizpassword', PARAM_TEXT);
240         $mform->setHelpButton('quizpassword', array('requirepassword', get_string('requirepassword', 'quiz'), 'quiz'));
241         $mform->setAdvanced('quizpassword', $quizconfig->fix_password);
242         $mform->setDefault('quizpassword', $quizconfig->password);
244     /// IP address.
245         $mform->addElement('text', 'subnet', get_string('requiresubnet', 'quiz'));
246         $mform->setType('subnet', PARAM_TEXT);
247         $mform->setHelpButton('subnet', array('requiresubnet', get_string('requiresubnet', 'quiz'), 'quiz'));
248         $mform->setAdvanced('subnet', $quizconfig->fix_subnet);
249         $mform->setDefault('subnet', $quizconfig->subnet);
251     /// Enforced time delay between quiz attempts.
252         $mform->addElement('duration', 'delay1', get_string('delay1st2nd', 'quiz'), array('optional' => true));
253         $mform->setHelpButton('delay1', array('timedelay1', get_string('delay1st2nd', 'quiz'), 'quiz'));
254         $mform->setAdvanced('delay1', $quizconfig->fix_delay1);
255         $mform->setDefault('delay1', $quizconfig->delay1);
256         $mform->disabledIf('delay1', 'attempts', 'eq', 1);
258         $mform->addElement('duration', 'delay2', get_string('delaylater', 'quiz'), array('optional' => true));
259         $mform->setHelpButton('delay2', array('timedelay2', get_string('delaylater', 'quiz'), 'quiz'));
260         $mform->setAdvanced('delay2', $quizconfig->fix_delay2);
261         $mform->setDefault('delay2', $quizconfig->delay2);
262         $mform->disabledIf('delay2', 'attempts', 'eq', 1);
263         $mform->disabledIf('delay2', 'attempts', 'eq', 2);
265     /// 'Secure' window.
266         $mform->addElement('selectyesno', 'popup', get_string('showinsecurepopup', 'quiz'));
267         $mform->setHelpButton('popup', array('popup', get_string('showinsecurepopup', 'quiz'), 'quiz'));
268         $mform->setAdvanced('popup', $quizconfig->fix_popup);
269         $mform->setDefault('popup', $quizconfig->popup);
271 //-------------------------------------------------------------------------------
272         $mform->addElement('header', 'overallfeedbackhdr', get_string('overallfeedback', 'quiz'));
273         $mform->setHelpButton('overallfeedbackhdr', array('overallfeedback', get_string('overallfeedback', 'quiz'), 'quiz'));
275         $mform->addElement('hidden', 'grade', $quizconfig->maximumgrade);
276         if (empty($this->_cm)) {
277             $needwarning = $quizconfig->maximumgrade == 0;
278         } else {
279             $quizgrade = $DB->get_field('quiz', 'grade', array('id' => $this->_instance));
280             $needwarning = $quizgrade == 0;
281         }
282         if ($needwarning) {
283             $mform->addElement('static', 'nogradewarning', '', get_string('nogradewarning', 'quiz'));
284         }
286         $mform->addElement('static', 'gradeboundarystatic1', get_string('gradeboundary', 'quiz'), '100%');
288         $repeatarray = array();
289         $repeatarray[] = &MoodleQuickForm::createElement('text', 'feedbacktext', get_string('feedback', 'quiz'), array('size' => 50));
290         $mform->setType('feedbacktext', PARAM_RAW);
291         $repeatarray[] = &MoodleQuickForm::createElement('text', 'feedbackboundaries', get_string('gradeboundary', 'quiz'), array('size' => 10));
292         $mform->setType('feedbackboundaries', PARAM_NOTAGS);
294         if (!empty($this->_instance)) {
295             $this->_feedbacks = $DB->get_records('quiz_feedback', array('quizid'=>$this->_instance), 'mingrade DESC');
296         } else {
297             $this->_feedbacks = array();
298         }
299         $numfeedbacks = max(count($this->_feedbacks) * 1.5, 5);
301         $nextel=$this->repeat_elements($repeatarray, $numfeedbacks - 1,
302                 array(), 'boundary_repeats', 'boundary_add_fields', 3,
303                 get_string('addmoreoverallfeedbacks', 'quiz'), true);
305         // Put some extra elements in before the button
306         $insertEl = &MoodleQuickForm::createElement('text', "feedbacktext[$nextel]", get_string('feedback', 'quiz'), array('size' => 50));
307         $mform->insertElementBefore($insertEl, 'boundary_add_fields');
309         $insertEl = &MoodleQuickForm::createElement('static', 'gradeboundarystatic2', get_string('gradeboundary', 'quiz'), '0%');
310         $mform->insertElementBefore($insertEl, 'boundary_add_fields');
312         // Add the disabledif rules. We cannot do this using the $repeatoptions parameter to
313         // repeat_elements becuase we don't want to dissable the first feedbacktext.
314         for ($i = 0; $i < $nextel; $i++) {
315             $mform->disabledIf('feedbackboundaries[' . $i . ']', 'grade', 'eq', 0);
316             $mform->disabledIf('feedbacktext[' . ($i + 1) . ']', 'grade', 'eq', 0);
317         }
319 //-------------------------------------------------------------------------------
320         $this->standard_coursemodule_elements();
322 //-------------------------------------------------------------------------------
323         // buttons
324         $this->add_action_buttons();
325     }
327     function data_preprocessing(&$default_values){
328         if (isset($default_values['grade'])) {
329             $default_values['grade'] = $default_values['grade'] + 0; // Convert to a real number, so we don't get 0.0000.
330         }
332         if (count($this->_feedbacks)) {
333             $key = 0;
334             foreach ($this->_feedbacks as $feedback){
335                 $default_values['feedbacktext['.$key.']'] = $feedback->feedbacktext;
336                 if ($feedback->mingrade > 0) {
337                     $default_values['feedbackboundaries['.$key.']'] = (100.0 * $feedback->mingrade / $default_values['grade']) . '%';
338                 }
339                 $key++;
340             }
341         }
343         if (isset($default_values['timelimit'])) {
344             $default_values['timelimitenable'] = $default_values['timelimit'] > 0;
345         }
347         if (isset($default_values['review'])){
348             $review = (int)$default_values['review'];
349             unset($default_values['review']);
351             $default_values['responsesimmediately'] = $review & QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_IMMEDIATELY;
352             $default_values['answersimmediately'] = $review & QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_IMMEDIATELY;
353             $default_values['feedbackimmediately'] = $review & QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_IMMEDIATELY;
354             $default_values['generalfeedbackimmediately'] = $review & QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_IMMEDIATELY;
355             $default_values['scoreimmediately'] = $review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_IMMEDIATELY;
356             $default_values['overallfeedbackimmediately'] = $review & QUIZ_REVIEW_OVERALLFEEDBACK & QUIZ_REVIEW_IMMEDIATELY;
358             $default_values['responsesopen'] = $review & QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_OPEN;
359             $default_values['answersopen'] = $review & QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_OPEN;
360             $default_values['feedbackopen'] = $review & QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_OPEN;
361             $default_values['generalfeedbackopen'] = $review & QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_OPEN;
362             $default_values['scoreopen'] = $review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_OPEN;
363             $default_values['overallfeedbackopen'] = $review & QUIZ_REVIEW_OVERALLFEEDBACK & QUIZ_REVIEW_OPEN;
365             $default_values['responsesclosed'] = $review & QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_CLOSED;
366             $default_values['answersclosed'] = $review & QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_CLOSED;
367             $default_values['feedbackclosed'] = $review & QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_CLOSED;
368             $default_values['generalfeedbackclosed'] = $review & QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_CLOSED;
369             $default_values['scoreclosed'] = $review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_CLOSED;
370             $default_values['overallfeedbackclosed'] = $review & QUIZ_REVIEW_OVERALLFEEDBACK & QUIZ_REVIEW_CLOSED;
371         }
373         if (isset($default_values['optionflags'])){
374             $default_values['adaptive'] = $default_values['optionflags'] & QUESTION_ADAPTIVE;
375             unset($default_values['optionflags']);
376         }
378         // Password field - different in form to stop browsers that remember passwords
379         // getting confused.
380         if (isset($default_values['password'])) {
381             $default_values['quizpassword'] = $default_values['password'];
382             unset($default_values['password']);
383         }
384     }
386     function validation($data, $files) {
387         $errors = parent::validation($data, $files);
389         // Check open and close times are consistent.
390         if ($data['timeopen'] != 0 && $data['timeclose'] != 0 && $data['timeclose'] < $data['timeopen']) {
391             $errors['timeclose'] = get_string('closebeforeopen', 'quiz');
392         }
394         // Check the boundary value is a number or a percentage, and in range.
395         $i = 0;
396         while (!empty($data['feedbackboundaries'][$i] )) {
397             $boundary = trim($data['feedbackboundaries'][$i]);
398             if (strlen($boundary) > 0 && $boundary[strlen($boundary) - 1] == '%') {
399                 $boundary = trim(substr($boundary, 0, -1));
400                 if (is_numeric($boundary)) {
401                     $boundary = $boundary * $data['grade'] / 100.0;
402                 } else {
403                     $errors["feedbackboundaries[$i]"] = get_string('feedbackerrorboundaryformat', 'quiz', $i + 1);
404                 }
405             }
406             if (is_numeric($boundary) && $boundary <= 0 || $boundary >= $data['grade'] ) {
407                 $errors["feedbackboundaries[$i]"] = get_string('feedbackerrorboundaryoutofrange', 'quiz', $i + 1);
408             }
409             if (is_numeric($boundary) && $i > 0 && $boundary >= $data['feedbackboundaries'][$i - 1]) {
410                 $errors["feedbackboundaries[$i]"] = get_string('feedbackerrororder', 'quiz', $i + 1);
411             }
412             $data['feedbackboundaries'][$i] = $boundary;
413             $i += 1;
414         }
415         $numboundaries = $i;
417         // Check there is nothing in the remaining unused fields.
418         if (!empty($data['feedbackboundaries'])) {
419             for ($i = $numboundaries; $i < count($data['feedbackboundaries']); $i += 1) {
420                 if (!empty($data['feedbackboundaries'][$i] ) && trim($data['feedbackboundaries'][$i] ) != '') {
421                     $errors["feedbackboundaries[$i]"] = get_string('feedbackerrorjunkinboundary', 'quiz', $i + 1);
422                 }
423             }
424         }
425         for ($i = $numboundaries + 1; $i < count($data['feedbacktext']); $i += 1) {
426             if (!empty($data['feedbacktext'][$i] ) && trim($data['feedbacktext'][$i] ) != '') {
427                 $errors["feedbacktext[$i]"] = get_string('feedbackerrorjunkinfeedback', 'quiz', $i + 1);
428             }
429         }
431         return $errors;
432     }
435 ?>