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