formslib: MDL-15827 disabledif option to repeat_elements did not work if you depend...
[moodle.git] / mod / quiz / mod_form.php
CommitLineData
44888f69 1<?php // $Id$
a23f0aaf 2
84e628a0 3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.org //
9// //
10// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
11// //
12// This program is free software; you can redistribute it and/or modify //
13// it under the terms of the GNU General Public License as published by //
14// the Free Software Foundation; either version 2 of the License, or //
15// (at your option) any later version. //
16// //
17// This program is distributed in the hope that it will be useful, //
18// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20// GNU General Public License for more details: //
21// //
22// http://www.gnu.org/copyleft/gpl.html //
23// //
24///////////////////////////////////////////////////////////////////////////
25
26require_once($CFG->dirroot . '/course/moodleform_mod.php');
27require_once($CFG->dirroot . '/mod/quiz/locallib.php');
28
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 */
f07b9627 35class mod_quiz_mod_form extends moodleform_mod {
a23f0aaf 36 var $_feedbacks;
37
2ee60b49 38 function definition() {
a23f0aaf 39
c18269c7 40 global $COURSE, $CFG, $DB;
e2249afe 41 $quizconfig = get_config('quiz');
2ee60b49 42 $mform =& $this->_form;
a23f0aaf 43
a23f0aaf 44//-------------------------------------------------------------------------------
45 $mform->addElement('header', 'general', get_string('general', 'form'));
46
84e628a0 47 /// Name.
26de8d35 48 $mform->addElement('text', 'name', get_string('name'), array('size'=>'64'));
8eb1d25f 49 if (!empty($CFG->formatstringstriptags)) {
50 $mform->setType('name', PARAM_TEXT);
51 } else {
52 $mform->setType('name', PARAM_CLEAN);
53 }
99ebfea2 54 $mform->addRule('name', null, 'required', null, 'client');
a23f0aaf 55
84e628a0 56 /// Introduction.
57 $mform->addElement('htmleditor', 'intro', get_string('introduction', 'quiz'));
2ee60b49 58 $mform->setType('intro', PARAM_RAW);
16851b22 59 $mform->setHelpButton('intro', array('richtext2', get_string('helprichtext')));
a23f0aaf 60
84e628a0 61 /// Open and close dates.
62 $mform->addElement('date_time_selector', 'timeopen', get_string('quizopen', 'quiz'), array('optional' => true));
8289cdf3 63 $mform->setHelpButton('timeopen', array('timeopen', get_string('quizopen', 'quiz'), 'quiz'));
a23f0aaf 64
84e628a0 65 $mform->addElement('date_time_selector', 'timeclose', get_string('quizclose', 'quiz'), array('optional' => true));
8289cdf3 66 $mform->setHelpButton('timeclose', array('timeopen', get_string('quizclose', 'quiz'), 'quiz'));
a23f0aaf 67
84e628a0 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);
e2249afe 72 $mform->setDefault('timelimit', $quizconfig->timelimit);
a23f0aaf 73
84e628a0 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;
a23f0aaf 78 }
84e628a0 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);
a23f0aaf 83
84e628a0 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);
90
91 $mform->addElement('hidden', 'grade', $quizconfig->maximumgrade);
a23f0aaf 92
93//-------------------------------------------------------------------------------
84e628a0 94 $mform->addElement('header', 'layouthdr', get_string('layout', 'quiz'));
95
96 /// Shuffle questions.
eeab18f0 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'));
84e628a0 99 $mform->setHelpButton('shufflequestions', array('shufflequestions', get_string('shufflequestions','quiz'), 'quiz'));
100 $mform->setAdvanced('shufflequestions', $quizconfig->fix_shufflequestions);
101 $mform->setDefault('shufflequestions', $quizconfig->shufflequestions);
102
103 /// Questions per page.
eeab18f0 104 $pageoptions = array();
105 $pageoptions[0] = get_string('neverallononepage', 'quiz');
106 $pageoptions[1] = get_string('everyquestion', 'quiz');
84e628a0 107 for ($i = 2; $i <= QUIZ_MAX_QPP_OPTION; ++$i) {
eeab18f0 108 $pageoptions[$i] = get_string('everynquestions', 'quiz', $i);
a23f0aaf 109 }
eeab18f0 110
111 $pagegroup = array();
112 $pagegroup[] = &$mform->createElement('select', 'questionsperpage', get_string('newpage', 'quiz'), $pageoptions, array('id' => 'id_questionsperpage'));
e2249afe 113 $mform->setDefault('questionsperpage', $quizconfig->questionsperpage);
a23f0aaf 114
eeab18f0 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 require_js(array('yui_yahoo', 'yui_dom', 'yui_event'));
119 require_js('mod/quiz/edit.js');
120 }
121
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->fix_questionsperpage);
125
84e628a0 126//-------------------------------------------------------------------------------
127 $mform->addElement('header', 'interactionhdr', get_string('questionbehaviour', 'quiz'));
a23f0aaf 128
84e628a0 129 /// Shuffle within questions.
130 $mform->addElement('selectyesno', 'shuffleanswers', get_string('shufflewithin', 'quiz'));
131 $mform->setHelpButton('shuffleanswers', array('shufflewithin', get_string('shufflewithin','quiz'), 'quiz'));
e2249afe 132 $mform->setAdvanced('shuffleanswers', $quizconfig->fix_shuffleanswers);
133 $mform->setDefault('shuffleanswers', $quizconfig->shuffleanswers);
a23f0aaf 134
84e628a0 135 /// Adaptive mode.
136 $mform->addElement('selectyesno', 'adaptive', get_string('adaptive', 'quiz'));
137 $mform->setHelpButton('adaptive', array('adaptive', get_string('adaptive','quiz'), 'quiz'));
e2249afe 138 $mform->setAdvanced('adaptive', $quizconfig->fix_optionflags);
139 $mform->setDefault('adaptive', $quizconfig->optionflags & QUESTION_ADAPTIVE);
a23f0aaf 140
84e628a0 141 /// Apply penalties.
142 $mform->addElement('selectyesno', 'penaltyscheme', get_string('penaltyscheme', 'quiz'));
143 $mform->setHelpButton('penaltyscheme', array('penaltyscheme', get_string('penaltyscheme','quiz'), 'quiz'));
e2249afe 144 $mform->setAdvanced('penaltyscheme', $quizconfig->fix_penaltyscheme);
145 $mform->setDefault('penaltyscheme', $quizconfig->penaltyscheme);
84e628a0 146 $mform->disabledIf('penaltyscheme', 'adaptive', 'neq', 1);
a23f0aaf 147
84e628a0 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->fix_attemptonlast);
152 $mform->setDefault('attemptonlast', $quizconfig->attemptonlast);
153 $mform->disabledIf('attemptonlast', 'attempts', 'eq', 1);
a23f0aaf 154
155//-------------------------------------------------------------------------------
e2833e87 156 $mform->addElement('header', 'reviewoptionshdr', get_string('reviewoptionsheading', 'quiz'));
157 $mform->setHelpButton('reviewoptionshdr', array('reviewoptions', get_string('reviewoptionsheading','quiz'), 'quiz'));
e2249afe 158 $mform->setAdvanced('reviewoptionshdr', $quizconfig->fix_review);
a23f0aaf 159
84e628a0 160 /// Review options.
2ee60b49 161 $immediatelyoptionsgrp=array();
a23f0aaf 162 $immediatelyoptionsgrp[] = &$mform->createElement('checkbox', 'responsesimmediately', '', get_string('responses', 'quiz'));
a23f0aaf 163 $immediatelyoptionsgrp[] = &$mform->createElement('checkbox', 'answersimmediately', '', get_string('answers', 'quiz'));
00719c02 164 $immediatelyoptionsgrp[] = &$mform->createElement('checkbox', 'feedbackimmediately', '', get_string('feedback', 'quiz'));
a23f0aaf 165 $immediatelyoptionsgrp[] = &$mform->createElement('checkbox', 'generalfeedbackimmediately', '', get_string('generalfeedback', 'quiz'));
00719c02 166 $immediatelyoptionsgrp[] = &$mform->createElement('checkbox', 'scoreimmediately', '', get_string('scores', 'quiz'));
167 $immediatelyoptionsgrp[] = &$mform->createElement('checkbox', 'overallfeedbackimmediately', '', get_string('overallfeedback', 'quiz'));
84e628a0 168 $mform->addGroup($immediatelyoptionsgrp, 'immediatelyoptionsgrp', get_string('reviewimmediately', 'quiz'), null, false);
e2249afe 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);
a23f0aaf 175
2ee60b49 176 $openoptionsgrp=array();
a23f0aaf 177 $openoptionsgrp[] = &$mform->createElement('checkbox', 'responsesopen', '', get_string('responses', 'quiz'));
a23f0aaf 178 $openoptionsgrp[] = &$mform->createElement('checkbox', 'answersopen', '', get_string('answers', 'quiz'));
00719c02 179 $openoptionsgrp[] = &$mform->createElement('checkbox', 'feedbackopen', '', get_string('feedback', 'quiz'));
a23f0aaf 180 $openoptionsgrp[] = &$mform->createElement('checkbox', 'generalfeedbackopen', '', get_string('generalfeedback', 'quiz'));
00719c02 181 $openoptionsgrp[] = &$mform->createElement('checkbox', 'scoreopen', '', get_string('scores', 'quiz'));
182 $openoptionsgrp[] = &$mform->createElement('checkbox', 'overallfeedbackopen', '', get_string('overallfeedback', 'quiz'));
84e628a0 183 $mform->addGroup($openoptionsgrp, 'openoptionsgrp', get_string('reviewopen', 'quiz'), array(' '), false);
e2249afe 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);
a23f0aaf 190
2ee60b49 191 $closedoptionsgrp=array();
a23f0aaf 192 $closedoptionsgrp[] = &$mform->createElement('checkbox', 'responsesclosed', '', get_string('responses', 'quiz'));
a23f0aaf 193 $closedoptionsgrp[] = &$mform->createElement('checkbox', 'answersclosed', '', get_string('answers', 'quiz'));
00719c02 194 $closedoptionsgrp[] = &$mform->createElement('checkbox', 'feedbackclosed', '', get_string('feedback', 'quiz'));
a23f0aaf 195 $closedoptionsgrp[] = &$mform->createElement('checkbox', 'generalfeedbackclosed', '', get_string('generalfeedback', 'quiz'));
00719c02 196 $closedoptionsgrp[] = &$mform->createElement('checkbox', 'scoreclosed', '', get_string('scores', 'quiz'));
197 $closedoptionsgrp[] = &$mform->createElement('checkbox', 'overallfeedbackclosed', '', get_string('overallfeedback', 'quiz'));
84e628a0 198 $mform->addGroup($closedoptionsgrp, 'closedoptionsgrp', get_string('reviewclosed', 'quiz'), array(' '), false);
e2249afe 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);
84e628a0 205 $mform->disabledIf('closedoptionsgrp', 'timeclose[enabled]');
a23f0aaf 206
207//-------------------------------------------------------------------------------
84e628a0 208 $mform->addElement('header', 'display', get_string('display', 'form'));
a23f0aaf 209
84e628a0 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->fix_showuserpicture);
214 $mform->setDefault('showuserpicture', $quizconfig->showuserpicture);
215
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->fix_decimalpoints);
224 $mform->setDefault('decimalpoints', $quizconfig->decimalpoints);
a23f0aaf 225
84e628a0 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->fix_questiondecimalpoints);
234 $mform->setDefault('questiondecimalpoints', $quizconfig->questiondecimalpoints);
235
236//-------------------------------------------------------------------------------
237 $mform->addElement('header', 'security', get_string('extraattemptrestrictions', 'quiz'));
238
239 /// Enforced time delay between quiz attempts.
240 $mform->addElement('passwordunmask', 'quizpassword', get_string('requirepassword', 'quiz'));
2ee60b49 241 $mform->setType('quizpassword', PARAM_TEXT);
84e628a0 242 $mform->setHelpButton('quizpassword', array('requirepassword', get_string('requirepassword', 'quiz'), 'quiz'));
e2249afe 243 $mform->setAdvanced('quizpassword', $quizconfig->fix_password);
244 $mform->setDefault('quizpassword', $quizconfig->password);
a23f0aaf 245
84e628a0 246 /// IP address.
247 $mform->addElement('text', 'subnet', get_string('requiresubnet', 'quiz'));
2ee60b49 248 $mform->setType('subnet', PARAM_TEXT);
84e628a0 249 $mform->setHelpButton('subnet', array('requiresubnet', get_string('requiresubnet', 'quiz'), 'quiz'));
e2249afe 250 $mform->setAdvanced('subnet', $quizconfig->fix_subnet);
251 $mform->setDefault('subnet', $quizconfig->subnet);
a23f0aaf 252
84e628a0 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->fix_delay1);
257 $mform->setDefault('delay1', $quizconfig->delay1);
258 $mform->disabledIf('delay1', 'attempts', 'eq', 1);
259
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->fix_delay2);
263 $mform->setDefault('delay2', $quizconfig->delay2);
264 $mform->disabledIf('delay2', 'attempts', 'eq', 1);
265 $mform->disabledIf('delay2', 'attempts', 'eq', 2);
266
267 /// 'Secure' window.
268 $mform->addElement('selectyesno', 'popup', get_string('showinsecurepopup', 'quiz'));
269 $mform->setHelpButton('popup', array('popup', get_string('showinsecurepopup', 'quiz'), 'quiz'));
270 $mform->setAdvanced('popup', $quizconfig->fix_popup);
271 $mform->setDefault('popup', $quizconfig->popup);
272
a23f0aaf 273//-------------------------------------------------------------------------------
08224df1 274 $mform->addElement('header', 'overallfeedbackhdr', get_string('overallfeedback', 'quiz'));
2ee60b49 275 $mform->setHelpButton('overallfeedbackhdr', array('overallfeedback', get_string('overallfeedback', 'quiz'), 'quiz'));
a23f0aaf 276
2ee60b49 277 $mform->addElement('static', 'gradeboundarystatic1', get_string('gradeboundary', 'quiz'), '100%');
a23f0aaf 278
279 $repeatarray=array();
a462602a 280 $repeatarray[] = &MoodleQuickForm::createElement('text', 'feedbacktext', get_string('feedback', 'quiz'), array('size' => 50));
281 $repeatarray[] = &MoodleQuickForm::createElement('text', 'feedbackboundaries', get_string('gradeboundary', 'quiz'), array('size' => 10));
a23f0aaf 282
283 if (!empty($this->_instance)) {
c18269c7 284 $this->_feedbacks = $DB->get_records('quiz_feedback', array('quizid'=>$this->_instance), 'mingrade DESC');
a23f0aaf 285 } else {
286 $this->_feedbacks = array();
287 }
288 $numfeedbacks = max(count($this->_feedbacks) * 1.5, 5);
ebff6e2c 289
de266537 290 $mform->setType('feedbacktext', PARAM_RAW);
3a298174 291 $mform->setType('feedbackboundaries', PARAM_NOTAGS);
ebff6e2c 292
84e628a0 293 $nextel=$this->repeat_elements($repeatarray, $numfeedbacks - 1,
6f3b54c8 294 array(), 'boundary_repeats', 'boundary_add_fields', 3,
295 get_string('addmoreoverallfeedbacks', 'quiz'), true);
a23f0aaf 296
297 //put some extra elements in before the button
a462602a 298 $insertEl = &MoodleQuickForm::createElement('text', "feedbacktext[$nextel]", get_string('feedback', 'quiz'), array('size' => 50));
a23f0aaf 299 $mform->insertElementBefore($insertEl, 'boundary_add_fields');
a23f0aaf 300
301 $insertEl = &MoodleQuickForm::createElement('static', 'gradeboundarystatic2', get_string('gradeboundary', 'quiz'), '0%');
302 $mform->insertElementBefore($insertEl, 'boundary_add_fields');
303
84e628a0 304//-------------------------------------------------------------------------------
305 $features = new stdClass;
306 $features->groups = true;
307 $features->groupings = true;
308 $features->groupmembersonly = true;
309 $this->standard_coursemodule_elements($features);
310
a23f0aaf 311//-------------------------------------------------------------------------------
312 // buttons
313 $this->add_action_buttons();
2ee60b49 314 }
a23f0aaf 315
2ee60b49 316 function data_preprocessing(&$default_values){
a23f0aaf 317 if (count($this->_feedbacks)) {
318 $key = 0;
319 foreach ($this->_feedbacks as $feedback){
320 $default_values['feedbacktext['.$key.']'] = $feedback->feedbacktext;
321 if ($feedback->mingrade > 0) {
322 $default_values['feedbackboundaries['.$key.']'] = (100.0 * $feedback->mingrade / $default_values['grade']) . '%';
323 }
324 $key++;
325 }
a23f0aaf 326 }
298daa21 327
328 if (isset($default_values['timelimit'])) {
329 $default_values['timelimitenable'] = $default_values['timelimit'] > 0;
a23f0aaf 330 }
331
332 if (isset($default_values['review'])){
333 $review = (int)$default_values['review'];
334 unset($default_values['review']);
335
336 $default_values['responsesimmediately'] = $review & QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_IMMEDIATELY;
a23f0aaf 337 $default_values['answersimmediately'] = $review & QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_IMMEDIATELY;
00719c02 338 $default_values['feedbackimmediately'] = $review & QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_IMMEDIATELY;
a23f0aaf 339 $default_values['generalfeedbackimmediately'] = $review & QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_IMMEDIATELY;
00719c02 340 $default_values['scoreimmediately'] = $review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_IMMEDIATELY;
341 $default_values['overallfeedbackimmediately'] = $review & QUIZ_REVIEW_OVERALLFEEDBACK & QUIZ_REVIEW_IMMEDIATELY;
a23f0aaf 342
343 $default_values['responsesopen'] = $review & QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_OPEN;
a23f0aaf 344 $default_values['answersopen'] = $review & QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_OPEN;
00719c02 345 $default_values['feedbackopen'] = $review & QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_OPEN;
a23f0aaf 346 $default_values['generalfeedbackopen'] = $review & QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_OPEN;
00719c02 347 $default_values['scoreopen'] = $review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_OPEN;
348 $default_values['overallfeedbackopen'] = $review & QUIZ_REVIEW_OVERALLFEEDBACK & QUIZ_REVIEW_OPEN;
a23f0aaf 349
350 $default_values['responsesclosed'] = $review & QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_CLOSED;
a23f0aaf 351 $default_values['answersclosed'] = $review & QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_CLOSED;
00719c02 352 $default_values['feedbackclosed'] = $review & QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_CLOSED;
a23f0aaf 353 $default_values['generalfeedbackclosed'] = $review & QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_CLOSED;
00719c02 354 $default_values['scoreclosed'] = $review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_CLOSED;
355 $default_values['overallfeedbackclosed'] = $review & QUIZ_REVIEW_OVERALLFEEDBACK & QUIZ_REVIEW_CLOSED;
a23f0aaf 356 }
357
358 if (isset($default_values['optionflags'])){
359 $default_values['adaptive'] = $default_values['optionflags'] & QUESTION_ADAPTIVE;
360 unset($default_values['optionflags']);
361 }
ab0a8ff2 362
363 // Password field - different in form to stop browsers that remember passwords
364 // getting confused.
2ee60b49 365 if (isset($default_values['password'])) {
ab0a8ff2 366 $default_values['quizpassword'] = $default_values['password'];
367 unset($default_values['password']);
368 }
2ee60b49 369 }
a23f0aaf 370
a78890d5 371 function validation($data, $files) {
372 $errors = parent::validation($data, $files);
60243313 373
a23f0aaf 374 // Check open and close times are consistent.
375 if ($data['timeopen'] != 0 && $data['timeclose'] != 0 && $data['timeclose'] < $data['timeopen']) {
376 $errors['timeclose'] = get_string('closebeforeopen', 'quiz');
377 }
378
379 // Check the boundary value is a number or a percentage, and in range.
380 $i = 0;
381 while (!empty($data['feedbackboundaries'][$i] )) {
382 $boundary = trim($data['feedbackboundaries'][$i]);
383 if (strlen($boundary) > 0 && $boundary[strlen($boundary) - 1] == '%') {
384 $boundary = trim(substr($boundary, 0, -1));
385 if (is_numeric($boundary)) {
386 $boundary = $boundary * $data['grade'] / 100.0;
387 } else {
388 $errors["feedbackboundaries[$i]"] = get_string('feedbackerrorboundaryformat', 'quiz', $i + 1);
389 }
390 }
391 if (is_numeric($boundary) && $boundary <= 0 || $boundary >= $data['grade'] ) {
392 $errors["feedbackboundaries[$i]"] = get_string('feedbackerrorboundaryoutofrange', 'quiz', $i + 1);
393 }
394 if (is_numeric($boundary) && $i > 0 && $boundary >= $data['feedbackboundaries'][$i - 1]) {
395 $errors["feedbackboundaries[$i]"] = get_string('feedbackerrororder', 'quiz', $i + 1);
396 }
397 $data['feedbackboundaries'][$i] = $boundary;
398 $i += 1;
399 }
400 $numboundaries = $i;
401
402 // Check there is nothing in the remaining unused fields.
403 for ($i = $numboundaries; $i < count($data['feedbackboundaries'] ); $i += 1) {
404 if (!empty($data['feedbackboundaries'][$i] ) && trim($data['feedbackboundaries'][$i] ) != '') {
405 $errors["feedbackboundaries[$i]"] = get_string('feedbackerrorjunkinboundary', 'quiz', $i + 1);
406 }
407 }
408 for ($i = $numboundaries + 1; $i < count($data['feedbacktext'] ); $i += 1) {
409 if (!empty($data['feedbacktext'][$i] ) && trim($data['feedbacktext'][$i] ) != '') {
410 $errors["feedbacktext[$i]"] = get_string('feedbackerrorjunkinfeedback', 'quiz', $i + 1);
411 }
412 }
60243313 413
32648682 414 return $errors;
2ee60b49 415 }
a23f0aaf 416
417}
2ee60b49 418?>