Commit | Line | Data |
---|---|---|
83192608 | 1 | <?php |
ba643847 TH |
2 | // This file is part of Moodle - http://moodle.org/ |
3 | // | |
4 | // Moodle is free software: you can redistribute it and/or modify | |
5 | // it under the terms of the GNU General Public License as published by | |
6 | // the Free Software Foundation, either version 3 of the License, or | |
7 | // (at your option) any later version. | |
8 | // | |
9 | // Moodle is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | // | |
14 | // You should have received a copy of the GNU General Public License | |
15 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
16 | ||
17 | /** | |
18 | * Defines the quiz module ettings form. | |
19 | * | |
20 | * @package mod | |
21 | * @subpackage quiz | |
22 | * @copyright 2006 Jamie Pratt | |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
24 | */ | |
84e628a0 | 25 | |
25302dee | 26 | |
a17b297d | 27 | defined('MOODLE_INTERNAL') || die(); |
bfebaf64 | 28 | |
84e628a0 | 29 | require_once($CFG->dirroot . '/course/moodleform_mod.php'); |
30 | require_once($CFG->dirroot . '/mod/quiz/locallib.php'); | |
31 | ||
25302dee | 32 | |
84e628a0 | 33 | /** |
34 | * Settings form for the quiz module. | |
3268cf99 | 35 | * |
ba643847 TH |
36 | * @copyright 2006 Jamie Pratt |
37 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
84e628a0 | 38 | */ |
f07b9627 | 39 | class mod_quiz_mod_form extends moodleform_mod { |
25302dee TH |
40 | protected $_feedbacks; |
41 | protected static $reviewfields = array(); // Initialised in the constructor. | |
42 | ||
f2557823 | 43 | public function __construct($current, $section, $cm, $course) { |
25302dee TH |
44 | self::$reviewfields = array( |
45 | 'attempt' => get_string('theattempt', 'quiz'), | |
46 | 'correctness' => get_string('whethercorrect', 'question'), | |
47 | 'marks' => get_string('marks', 'question'), | |
48 | 'specificfeedback' => get_string('specificfeedback', 'question'), | |
49 | 'generalfeedback' => get_string('generalfeedback', 'question'), | |
50 | 'rightanswer' => get_string('rightanswer', 'question'), | |
51 | 'overallfeedback' => get_string('overallfeedback', 'quiz'), | |
52 | ); | |
f2557823 | 53 | parent::__construct($current, $section, $cm, $course); |
25302dee | 54 | } |
a23f0aaf | 55 | |
c7df5006 | 56 | protected function definition() { |
25ddb7ef | 57 | global $COURSE, $CFG, $DB, $PAGE; |
e2249afe | 58 | $quizconfig = get_config('quiz'); |
25302dee | 59 | $mform = $this->_form; |
a23f0aaf | 60 | |
a23f0aaf | 61 | //------------------------------------------------------------------------------- |
62 | $mform->addElement('header', 'general', get_string('general', 'form')); | |
63 | ||
84e628a0 | 64 | /// Name. |
26de8d35 | 65 | $mform->addElement('text', 'name', get_string('name'), array('size'=>'64')); |
8eb1d25f | 66 | if (!empty($CFG->formatstringstriptags)) { |
67 | $mform->setType('name', PARAM_TEXT); | |
68 | } else { | |
b8ea3041 | 69 | $mform->setType('name', PARAM_CLEANHTML); |
8eb1d25f | 70 | } |
99ebfea2 | 71 | $mform->addRule('name', null, 'required', null, 'client'); |
a23f0aaf | 72 | |
84e628a0 | 73 | /// Introduction. |
0cd1affc | 74 | $this->add_intro_editor(false, get_string('introduction', 'quiz')); |
a23f0aaf | 75 | |
84e628a0 | 76 | /// Open and close dates. |
25302dee TH |
77 | $mform->addElement('date_time_selector', 'timeopen', get_string('quizopen', 'quiz'), |
78 | array('optional' => true, 'step' => 1)); | |
f2557823 | 79 | $mform->addHelpButton('timeopen', 'quizopenclose', 'quiz'); |
25302dee TH |
80 | |
81 | $mform->addElement('date_time_selector', 'timeclose', get_string('quizclose', 'quiz'), | |
82 | array('optional' => true, 'step' => 1)); | |
a23f0aaf | 83 | |
84e628a0 | 84 | /// Time limit. |
85 | $mform->addElement('duration', 'timelimit', get_string('timelimit', 'quiz'), array('optional' => true)); | |
7292c11f | 86 | $mform->addHelpButton('timelimit', 'timelimit', 'quiz'); |
adf8f3f9 | 87 | $mform->setAdvanced('timelimit', $quizconfig->timelimit_adv); |
e2249afe | 88 | $mform->setDefault('timelimit', $quizconfig->timelimit); |
a23f0aaf | 89 | |
84e628a0 | 90 | /// Number of attempts. |
91 | $attemptoptions = array('0' => get_string('unlimited')); | |
92 | for ($i = 1; $i <= QUIZ_MAX_ATTEMPT_OPTION; $i++) { | |
93 | $attemptoptions[$i] = $i; | |
a23f0aaf | 94 | } |
84e628a0 | 95 | $mform->addElement('select', 'attempts', get_string('attemptsallowed', 'quiz'), $attemptoptions); |
adf8f3f9 | 96 | $mform->setAdvanced('attempts', $quizconfig->attempts_adv); |
84e628a0 | 97 | $mform->setDefault('attempts', $quizconfig->attempts); |
a23f0aaf | 98 | |
84e628a0 | 99 | /// Grading method. |
100 | $mform->addElement('select', 'grademethod', get_string('grademethod', 'quiz'), quiz_get_grading_options()); | |
7292c11f | 101 | $mform->addHelpButton('grademethod', 'grademethod', 'quiz'); |
adf8f3f9 | 102 | $mform->setAdvanced('grademethod', $quizconfig->grademethod_adv); |
84e628a0 | 103 | $mform->setDefault('grademethod', $quizconfig->grademethod); |
104 | $mform->disabledIf('grademethod', 'attempts', 'eq', 1); | |
105 | ||
a23f0aaf | 106 | //------------------------------------------------------------------------------- |
84e628a0 | 107 | $mform->addElement('header', 'layouthdr', get_string('layout', 'quiz')); |
108 | ||
109 | /// Shuffle questions. | |
eeab18f0 | 110 | $shuffleoptions = array(0 => get_string('asshownoneditscreen', 'quiz'), 1 => get_string('shuffledrandomly', 'quiz')); |
111 | $mform->addElement('select', 'shufflequestions', get_string('questionorder', 'quiz'), $shuffleoptions, array('id' => 'id_shufflequestions')); | |
adf8f3f9 | 112 | $mform->setAdvanced('shufflequestions', $quizconfig->shufflequestions_adv); |
84e628a0 | 113 | $mform->setDefault('shufflequestions', $quizconfig->shufflequestions); |
114 | ||
115 | /// Questions per page. | |
eeab18f0 | 116 | $pageoptions = array(); |
117 | $pageoptions[0] = get_string('neverallononepage', 'quiz'); | |
118 | $pageoptions[1] = get_string('everyquestion', 'quiz'); | |
84e628a0 | 119 | for ($i = 2; $i <= QUIZ_MAX_QPP_OPTION; ++$i) { |
eeab18f0 | 120 | $pageoptions[$i] = get_string('everynquestions', 'quiz', $i); |
a23f0aaf | 121 | } |
eeab18f0 | 122 | |
123 | $pagegroup = array(); | |
124 | $pagegroup[] = &$mform->createElement('select', 'questionsperpage', get_string('newpage', 'quiz'), $pageoptions, array('id' => 'id_questionsperpage')); | |
e2249afe | 125 | $mform->setDefault('questionsperpage', $quizconfig->questionsperpage); |
a23f0aaf | 126 | |
eeab18f0 | 127 | if (!empty($this->_cm)) { |
128 | $pagegroup[] = &$mform->createElement('checkbox', 'repaginatenow', '', get_string('repaginatenow', 'quiz'), array('id' => 'id_repaginatenow')); | |
129 | $mform->disabledIf('repaginatenow', 'shufflequestions', 'eq', 1); | |
f44b10ed | 130 | $PAGE->requires->yui2_lib('event'); |
9dec75db | 131 | $PAGE->requires->js('/mod/quiz/edit.js'); |
a13d4fbd | 132 | $PAGE->requires->js_init_call('quiz_settings_init'); |
eeab18f0 | 133 | } |
134 | ||
135 | $mform->addGroup($pagegroup, 'questionsperpagegrp', get_string('newpage', 'quiz'), null, false); | |
7292c11f | 136 | $mform->addHelpButton('questionsperpagegrp', 'newpage', 'quiz'); |
adf8f3f9 | 137 | $mform->setAdvanced('questionsperpagegrp', $quizconfig->questionsperpage_adv); |
eeab18f0 | 138 | |
84e628a0 | 139 | //------------------------------------------------------------------------------- |
140 | $mform->addElement('header', 'interactionhdr', get_string('questionbehaviour', 'quiz')); | |
a23f0aaf | 141 | |
84e628a0 | 142 | /// Shuffle within questions. |
143 | $mform->addElement('selectyesno', 'shuffleanswers', get_string('shufflewithin', 'quiz')); | |
7292c11f | 144 | $mform->addHelpButton('shuffleanswers', 'shufflewithin', 'quiz'); |
adf8f3f9 | 145 | $mform->setAdvanced('shuffleanswers', $quizconfig->shuffleanswers_adv); |
e2249afe | 146 | $mform->setDefault('shuffleanswers', $quizconfig->shuffleanswers); |
a23f0aaf | 147 | |
25302dee | 148 | /// How questions behave (question behaviour). |
f2557823 TH |
149 | if (!empty($this->current->preferredbehaviour)) { |
150 | $currentbehaviour = $this->current->preferredbehaviour; | |
25302dee TH |
151 | } else { |
152 | $currentbehaviour = ''; | |
153 | } | |
154 | $behaviours = question_engine::get_behaviour_options($currentbehaviour); | |
155 | $mform->addElement('select', 'preferredbehaviour', get_string('howquestionsbehave', 'question'), $behaviours); | |
f2557823 | 156 | $mform->addHelpButton('preferredbehaviour', 'howquestionsbehave', 'question'); |
bb28e3bc | 157 | $mform->setDefault('preferredbehaviour', $quizconfig->preferredbehaviour); |
a23f0aaf | 158 | |
84e628a0 | 159 | /// Each attempt builds on last. |
160 | $mform->addElement('selectyesno', 'attemptonlast', get_string('eachattemptbuildsonthelast', 'quiz')); | |
7292c11f | 161 | $mform->addHelpButton('attemptonlast', 'eachattemptbuildsonthelast', 'quiz'); |
adf8f3f9 | 162 | $mform->setAdvanced('attemptonlast', $quizconfig->attemptonlast_adv); |
84e628a0 | 163 | $mform->setDefault('attemptonlast', $quizconfig->attemptonlast); |
164 | $mform->disabledIf('attemptonlast', 'attempts', 'eq', 1); | |
a23f0aaf | 165 | |
166 | //------------------------------------------------------------------------------- | |
e2833e87 | 167 | $mform->addElement('header', 'reviewoptionshdr', get_string('reviewoptionsheading', 'quiz')); |
7292c11f | 168 | $mform->addHelpButton('reviewoptionshdr', 'reviewoptionsheading', 'quiz'); |
a23f0aaf | 169 | |
84e628a0 | 170 | /// Review options. |
14936199 TH |
171 | $this->add_review_options_group($mform, $quizconfig, 'during', mod_quiz_display_options::DURING); |
172 | $this->add_review_options_group($mform, $quizconfig, 'immediately', mod_quiz_display_options::IMMEDIATELY_AFTER); | |
173 | $this->add_review_options_group($mform, $quizconfig, 'open', mod_quiz_display_options::LATER_WHILE_OPEN); | |
174 | $this->add_review_options_group($mform, $quizconfig, 'closed', mod_quiz_display_options::AFTER_CLOSE); | |
25302dee TH |
175 | |
176 | foreach ($behaviours as $behaviour => $notused) { | |
177 | $unusedoptions = question_engine::get_behaviour_unused_display_options($behaviour); | |
178 | foreach ($unusedoptions as $unusedoption) { | |
179 | $mform->disabledIf($unusedoption . 'during', 'preferredbehaviour', | |
180 | 'eq', $behaviour); | |
181 | } | |
182 | } | |
183 | $mform->disabledIf('attemptduring', 'preferredbehaviour', | |
184 | 'neq', 'wontmatch'); | |
185 | $mform->disabledIf('overallfeedbackduring', 'preferredbehaviour', | |
186 | 'neq', 'wontmatch'); | |
a23f0aaf | 187 | |
188 | //------------------------------------------------------------------------------- | |
84e628a0 | 189 | $mform->addElement('header', 'display', get_string('display', 'form')); |
a23f0aaf | 190 | |
84e628a0 | 191 | /// Show user picture. |
192 | $mform->addElement('selectyesno', 'showuserpicture', get_string('showuserpicture', 'quiz')); | |
7292c11f | 193 | $mform->addHelpButton('showuserpicture', 'showuserpicture', 'quiz'); |
adf8f3f9 | 194 | $mform->setAdvanced('showuserpicture', $quizconfig->showuserpicture_adv); |
84e628a0 | 195 | $mform->setDefault('showuserpicture', $quizconfig->showuserpicture); |
196 | ||
197 | /// Overall decimal points. | |
198 | $options = array(); | |
199 | for ($i = 0; $i <= QUIZ_MAX_DECIMAL_OPTION; $i++) { | |
200 | $options[$i] = $i; | |
201 | } | |
202 | $mform->addElement('select', 'decimalpoints', get_string('decimalplaces', 'quiz'), $options); | |
7292c11f | 203 | $mform->addHelpButton('decimalpoints', 'decimalplaces', 'quiz'); |
adf8f3f9 | 204 | $mform->setAdvanced('decimalpoints', $quizconfig->decimalpoints_adv); |
84e628a0 | 205 | $mform->setDefault('decimalpoints', $quizconfig->decimalpoints); |
a23f0aaf | 206 | |
84e628a0 | 207 | /// Question decimal points. |
208 | $options = array(-1 => get_string('sameasoverall', 'quiz')); | |
209 | for ($i = 0; $i <= QUIZ_MAX_Q_DECIMAL_OPTION; $i++) { | |
210 | $options[$i] = $i; | |
211 | } | |
212 | $mform->addElement('select', 'questiondecimalpoints', get_string('decimalplacesquestion', 'quiz'), $options); | |
25a03faa | 213 | $mform->addHelpButton('questiondecimalpoints', 'decimalplacesquestion', 'quiz'); |
adf8f3f9 | 214 | $mform->setAdvanced('questiondecimalpoints', $quizconfig->questiondecimalpoints_adv); |
84e628a0 | 215 | $mform->setDefault('questiondecimalpoints', $quizconfig->questiondecimalpoints); |
216 | ||
56ed242b SH |
217 | // Show blocks during quiz attempt |
218 | $mform->addElement('selectyesno', 'showblocks', get_string('showblocks', 'quiz')); | |
7292c11f | 219 | $mform->addHelpButton('showblocks', 'showblocks', 'quiz'); |
56ed242b SH |
220 | $mform->setAdvanced('showblocks', $quizconfig->showblocks_adv); |
221 | $mform->setDefault('showblocks', $quizconfig->showblocks); | |
222 | ||
84e628a0 | 223 | //------------------------------------------------------------------------------- |
224 | $mform->addElement('header', 'security', get_string('extraattemptrestrictions', 'quiz')); | |
225 | ||
226 | /// Enforced time delay between quiz attempts. | |
227 | $mform->addElement('passwordunmask', 'quizpassword', get_string('requirepassword', 'quiz')); | |
2ee60b49 | 228 | $mform->setType('quizpassword', PARAM_TEXT); |
7292c11f | 229 | $mform->addHelpButton('quizpassword', 'requirepassword', 'quiz'); |
adf8f3f9 | 230 | $mform->setAdvanced('quizpassword', $quizconfig->password_adv); |
e2249afe | 231 | $mform->setDefault('quizpassword', $quizconfig->password); |
a23f0aaf | 232 | |
84e628a0 | 233 | /// IP address. |
234 | $mform->addElement('text', 'subnet', get_string('requiresubnet', 'quiz')); | |
2ee60b49 | 235 | $mform->setType('subnet', PARAM_TEXT); |
7292c11f | 236 | $mform->addHelpButton('subnet', 'requiresubnet', 'quiz'); |
adf8f3f9 | 237 | $mform->setAdvanced('subnet', $quizconfig->subnet_adv); |
e2249afe | 238 | $mform->setDefault('subnet', $quizconfig->subnet); |
a23f0aaf | 239 | |
84e628a0 | 240 | /// Enforced time delay between quiz attempts. |
241 | $mform->addElement('duration', 'delay1', get_string('delay1st2nd', 'quiz'), array('optional' => true)); | |
7292c11f | 242 | $mform->addHelpButton('delay1', 'delay1st2nd', 'quiz'); |
adf8f3f9 | 243 | $mform->setAdvanced('delay1', $quizconfig->delay1_adv); |
84e628a0 | 244 | $mform->setDefault('delay1', $quizconfig->delay1); |
245 | $mform->disabledIf('delay1', 'attempts', 'eq', 1); | |
246 | ||
247 | $mform->addElement('duration', 'delay2', get_string('delaylater', 'quiz'), array('optional' => true)); | |
7292c11f | 248 | $mform->addHelpButton('delay2', 'delaylater', 'quiz'); |
adf8f3f9 | 249 | $mform->setAdvanced('delay2', $quizconfig->delay2_adv); |
84e628a0 | 250 | $mform->setDefault('delay2', $quizconfig->delay2); |
251 | $mform->disabledIf('delay2', 'attempts', 'eq', 1); | |
252 | $mform->disabledIf('delay2', 'attempts', 'eq', 2); | |
253 | ||
254 | /// 'Secure' window. | |
7d4dfc48 | 255 | $options = array( |
256 | 0 => get_string('none', 'quiz'), | |
257 | 1 => get_string('popupwithjavascriptsupport', 'quiz')); | |
05d5801a | 258 | if (!empty($CFG->enablesafebrowserintegration)) { |
7d4dfc48 | 259 | $options[2] = get_string('requiresafeexambrowser', 'quiz'); |
260 | } | |
261 | $mform->addElement('select', 'popup', get_string('browsersecurity', 'quiz'), $options); | |
7292c11f | 262 | $mform->addHelpButton('popup', 'browsersecurity', 'quiz'); |
adf8f3f9 | 263 | $mform->setAdvanced('popup', $quizconfig->popup_adv); |
84e628a0 | 264 | $mform->setDefault('popup', $quizconfig->popup); |
265 | ||
a23f0aaf | 266 | //------------------------------------------------------------------------------- |
08224df1 | 267 | $mform->addElement('header', 'overallfeedbackhdr', get_string('overallfeedback', 'quiz')); |
7292c11f | 268 | $mform->addHelpButton('overallfeedbackhdr', 'overallfeedback', 'quiz'); |
a23f0aaf | 269 | |
e0b7cfcb | 270 | $mform->addElement('hidden', 'grade', $quizconfig->maximumgrade); |
d18e0fe6 | 271 | $mform->setType('grade', PARAM_RAW); |
f2557823 TH |
272 | |
273 | if (isset($this->current->grade)) { | |
274 | $needwarning = $this->current->grade === 0; | |
e0b7cfcb | 275 | } else { |
f2557823 | 276 | $needwarning = $quizconfig->maximumgrade == 0; |
e0b7cfcb | 277 | } |
278 | if ($needwarning) { | |
279 | $mform->addElement('static', 'nogradewarning', '', get_string('nogradewarning', 'quiz')); | |
280 | } | |
281 | ||
2ee60b49 | 282 | $mform->addElement('static', 'gradeboundarystatic1', get_string('gradeboundary', 'quiz'), '100%'); |
a23f0aaf | 283 | |
e0b7cfcb | 284 | $repeatarray = array(); |
d0e25622 | 285 | $repeatedoptions = array(); |
25302dee | 286 | $repeatarray[] = MoodleQuickForm::createElement('editor', 'feedbacktext', get_string('feedback', 'quiz'), null, array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'noclean'=>true, 'context'=>$this->context)); |
25302dee | 287 | $repeatarray[] = MoodleQuickForm::createElement('text', 'feedbackboundaries', get_string('gradeboundary', 'quiz'), array('size' => 10)); |
d0e25622 TH |
288 | $repeatedoptions['feedbacktext']['type'] = PARAM_RAW; |
289 | $repeatedoptions['feedbackboundaries']['type'] = PARAM_RAW; | |
a23f0aaf | 290 | |
291 | if (!empty($this->_instance)) { | |
d0e25622 TH |
292 | $this->_feedbacks = $DB->get_records('quiz_feedback', |
293 | 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 | |
d0e25622 TH |
299 | $nextel = $this->repeat_elements($repeatarray, $numfeedbacks - 1, |
300 | $repeatedoptions, 'boundary_repeats', 'boundary_add_fields', 3, | |
6f3b54c8 | 301 | get_string('addmoreoverallfeedbacks', 'quiz'), true); |
a23f0aaf | 302 | |
e0b7cfcb | 303 | // Put some extra elements in before the button |
d0e25622 TH |
304 | $mform->insertElementBefore(MoodleQuickForm::createElement('editor', |
305 | "feedbacktext[$nextel]", get_string('feedback', 'quiz'), null, | |
306 | array('maxfiles' => EDITOR_UNLIMITED_FILES, 'noclean' => true, | |
307 | 'context' => $this->context)), | |
308 | 'boundary_add_fields'); | |
309 | $mform->insertElementBefore(MoodleQuickForm::createElement('static', | |
310 | 'gradeboundarystatic2', get_string('gradeboundary', 'quiz'), '0%'), | |
311 | 'boundary_add_fields'); | |
a23f0aaf | 312 | |
e0b7cfcb | 313 | // Add the disabledif rules. We cannot do this using the $repeatoptions parameter to |
314 | // repeat_elements becuase we don't want to dissable the first feedbacktext. | |
315 | for ($i = 0; $i < $nextel; $i++) { | |
316 | $mform->disabledIf('feedbackboundaries[' . $i . ']', 'grade', 'eq', 0); | |
317 | $mform->disabledIf('feedbacktext[' . ($i + 1) . ']', 'grade', 'eq', 0); | |
318 | } | |
319 | ||
84e628a0 | 320 | //------------------------------------------------------------------------------- |
42f103be | 321 | $this->standard_coursemodule_elements(); |
84e628a0 | 322 | |
a23f0aaf | 323 | //------------------------------------------------------------------------------- |
324 | // buttons | |
325 | $this->add_action_buttons(); | |
2ee60b49 | 326 | } |
a23f0aaf | 327 | |
14936199 | 328 | protected function add_review_options_group($mform, $quizconfig, $whenname, $when) { |
25302dee TH |
329 | $group = array(); |
330 | foreach (self::$reviewfields as $field => $label) { | |
331 | $group[] = $mform->createElement('checkbox', $field . $whenname, '', $label); | |
332 | } | |
333 | $mform->addGroup($group, $whenname . 'optionsgrp', get_string('review' . $whenname, 'quiz'), null, false); | |
334 | ||
335 | foreach (self::$reviewfields as $field => $notused) { | |
14936199 TH |
336 | $cfgfield = 'review' . $field; |
337 | if ($quizconfig->$cfgfield & $when) { | |
25302dee TH |
338 | $mform->setDefault($field . $whenname, 1); |
339 | } else { | |
340 | $mform->setDefault($field . $whenname, 0); | |
341 | } | |
342 | } | |
343 | ||
344 | $mform->disabledIf('correctness' . $whenname, 'attempt' . $whenname); | |
345 | $mform->disabledIf('specificfeedback' . $whenname, 'attempt' . $whenname); | |
346 | $mform->disabledIf('generalfeedback' . $whenname, 'attempt' . $whenname); | |
347 | $mform->disabledIf('rightanswer' . $whenname, 'attempt' . $whenname); | |
348 | } | |
349 | ||
350 | protected function preprocessing_review_settings(&$toform, $whenname, $when) { | |
351 | foreach (self::$reviewfields as $field => $notused) { | |
352 | $fieldname = 'review' . $field; | |
353 | if (array_key_exists($fieldname, $toform)) { | |
354 | $toform[$field . $whenname] = $toform[$fieldname] & $when; | |
355 | } | |
356 | } | |
357 | } | |
358 | ||
06cd11a9 | 359 | public function data_preprocessing(&$toform) { |
25302dee TH |
360 | if (isset($toform['grade'])) { |
361 | $toform['grade'] = $toform['grade'] + 0; // Convert to a real number, so we don't get 0.0000. | |
e0b7cfcb | 362 | } |
363 | ||
a23f0aaf | 364 | if (count($this->_feedbacks)) { |
365 | $key = 0; | |
366 | foreach ($this->_feedbacks as $feedback){ | |
fe6ce234 | 367 | $draftid = file_get_submitted_draft_itemid('feedbacktext['.$key.']'); |
25302dee | 368 | $toform['feedbacktext['.$key.']']['text'] = file_prepare_draft_area( |
c5c16a2c TH |
369 | $draftid, // draftid |
370 | $this->context->id, // context | |
371 | 'mod_quiz', // component | |
fe6ce234 | 372 | 'feedback', // filarea |
c5c16a2c | 373 | !empty($feedback->id) ? (int) $feedback->id : null, // itemid |
fe6ce234 | 374 | null, |
c5c16a2c | 375 | $feedback->feedbacktext // text |
fe6ce234 | 376 | ); |
25302dee TH |
377 | $toform['feedbacktext['.$key.']']['format'] = $feedback->feedbacktextformat; |
378 | $toform['feedbacktext['.$key.']']['itemid'] = $draftid; | |
fe6ce234 | 379 | |
c5c16a2c TH |
380 | if ($toform['grade'] == 0) { |
381 | // When a quiz is un-graded, there can only be one lot of | |
382 | // feedback. If the quiz previously had a maximum grade and | |
383 | // several lots of feedback, we must now avoid putting text | |
384 | // into input boxes that are disabled, but which the | |
385 | // validation will insist are blank. | |
386 | break; | |
387 | } | |
388 | ||
a23f0aaf | 389 | if ($feedback->mingrade > 0) { |
167f1562 | 390 | $toform['feedbackboundaries['.$key.']'] = (100.0 * $feedback->mingrade / $toform['grade']) . '%'; |
a23f0aaf | 391 | } |
392 | $key++; | |
393 | } | |
a23f0aaf | 394 | } |
298daa21 | 395 | |
25302dee TH |
396 | if (isset($toform['timelimit'])) { |
397 | $toform['timelimitenable'] = $toform['timelimit'] > 0; | |
a23f0aaf | 398 | } |
399 | ||
25302dee TH |
400 | $this->preprocessing_review_settings($toform, 'during', mod_quiz_display_options::DURING); |
401 | $this->preprocessing_review_settings($toform, 'immediately', mod_quiz_display_options::IMMEDIATELY_AFTER); | |
402 | $this->preprocessing_review_settings($toform, 'open', mod_quiz_display_options::LATER_WHILE_OPEN); | |
403 | $this->preprocessing_review_settings($toform, 'closed', mod_quiz_display_options::AFTER_CLOSE); | |
404 | $toform['attemptduring'] = true; | |
405 | $toform['overallfeedbackduring'] = false; | |
406 | ||
407 | // Password field - different in form to stop browsers that remember | |
408 | // passwords from getting confused. | |
409 | if (isset($toform['password'])) { | |
410 | $toform['quizpassword'] = $toform['password']; | |
411 | unset($toform['password']); | |
ab0a8ff2 | 412 | } |
2ee60b49 | 413 | } |
a23f0aaf | 414 | |
c7df5006 | 415 | public function validation($data, $files) { |
a78890d5 | 416 | $errors = parent::validation($data, $files); |
60243313 | 417 | |
a23f0aaf | 418 | // Check open and close times are consistent. |
419 | if ($data['timeopen'] != 0 && $data['timeclose'] != 0 && $data['timeclose'] < $data['timeopen']) { | |
420 | $errors['timeclose'] = get_string('closebeforeopen', 'quiz'); | |
421 | } | |
422 | ||
423 | // Check the boundary value is a number or a percentage, and in range. | |
424 | $i = 0; | |
425 | while (!empty($data['feedbackboundaries'][$i] )) { | |
426 | $boundary = trim($data['feedbackboundaries'][$i]); | |
427 | if (strlen($boundary) > 0 && $boundary[strlen($boundary) - 1] == '%') { | |
428 | $boundary = trim(substr($boundary, 0, -1)); | |
429 | if (is_numeric($boundary)) { | |
430 | $boundary = $boundary * $data['grade'] / 100.0; | |
431 | } else { | |
432 | $errors["feedbackboundaries[$i]"] = get_string('feedbackerrorboundaryformat', 'quiz', $i + 1); | |
433 | } | |
434 | } | |
435 | if (is_numeric($boundary) && $boundary <= 0 || $boundary >= $data['grade'] ) { | |
436 | $errors["feedbackboundaries[$i]"] = get_string('feedbackerrorboundaryoutofrange', 'quiz', $i + 1); | |
437 | } | |
438 | if (is_numeric($boundary) && $i > 0 && $boundary >= $data['feedbackboundaries'][$i - 1]) { | |
439 | $errors["feedbackboundaries[$i]"] = get_string('feedbackerrororder', 'quiz', $i + 1); | |
440 | } | |
441 | $data['feedbackboundaries'][$i] = $boundary; | |
442 | $i += 1; | |
443 | } | |
444 | $numboundaries = $i; | |
445 | ||
446 | // Check there is nothing in the remaining unused fields. | |
e0b7cfcb | 447 | if (!empty($data['feedbackboundaries'])) { |
448 | for ($i = $numboundaries; $i < count($data['feedbackboundaries']); $i += 1) { | |
449 | if (!empty($data['feedbackboundaries'][$i] ) && trim($data['feedbackboundaries'][$i] ) != '') { | |
450 | $errors["feedbackboundaries[$i]"] = get_string('feedbackerrorjunkinboundary', 'quiz', $i + 1); | |
451 | } | |
a23f0aaf | 452 | } |
453 | } | |
e0b7cfcb | 454 | for ($i = $numboundaries + 1; $i < count($data['feedbacktext']); $i += 1) { |
fe6ce234 | 455 | if (!empty($data['feedbacktext'][$i]['text']) && trim($data['feedbacktext'][$i]['text'] ) != '') { |
a23f0aaf | 456 | $errors["feedbacktext[$i]"] = get_string('feedbackerrorjunkinfeedback', 'quiz', $i + 1); |
457 | } | |
458 | } | |
60243313 | 459 | |
32648682 | 460 | return $errors; |
2ee60b49 | 461 | } |
a23f0aaf | 462 | } |