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 | |
aac80ff3 | 61 | //------------------------------------------------------------------------------- |
a23f0aaf | 62 | $mform->addElement('header', 'general', get_string('general', 'form')); |
63 | ||
aac80ff3 | 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 | |
aac80ff3 | 73 | // Introduction. |
0cd1affc | 74 | $this->add_intro_editor(false, get_string('introduction', 'quiz')); |
a23f0aaf | 75 | |
aac80ff3 | 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 | |
aac80ff3 TH |
84 | // Time limit. |
85 | $mform->addElement('duration', 'timelimit', get_string('timelimit', 'quiz'), | |
86 | array('optional' => true)); | |
7292c11f | 87 | $mform->addHelpButton('timelimit', 'timelimit', 'quiz'); |
adf8f3f9 | 88 | $mform->setAdvanced('timelimit', $quizconfig->timelimit_adv); |
e2249afe | 89 | $mform->setDefault('timelimit', $quizconfig->timelimit); |
a23f0aaf | 90 | |
aac80ff3 | 91 | // Number of attempts. |
84e628a0 | 92 | $attemptoptions = array('0' => get_string('unlimited')); |
93 | for ($i = 1; $i <= QUIZ_MAX_ATTEMPT_OPTION; $i++) { | |
94 | $attemptoptions[$i] = $i; | |
a23f0aaf | 95 | } |
aac80ff3 TH |
96 | $mform->addElement('select', 'attempts', get_string('attemptsallowed', 'quiz'), |
97 | $attemptoptions); | |
adf8f3f9 | 98 | $mform->setAdvanced('attempts', $quizconfig->attempts_adv); |
84e628a0 | 99 | $mform->setDefault('attempts', $quizconfig->attempts); |
a23f0aaf | 100 | |
aac80ff3 TH |
101 | // Grading method. |
102 | $mform->addElement('select', 'grademethod', get_string('grademethod', 'quiz'), | |
103 | quiz_get_grading_options()); | |
7292c11f | 104 | $mform->addHelpButton('grademethod', 'grademethod', 'quiz'); |
adf8f3f9 | 105 | $mform->setAdvanced('grademethod', $quizconfig->grademethod_adv); |
84e628a0 | 106 | $mform->setDefault('grademethod', $quizconfig->grademethod); |
107 | $mform->disabledIf('grademethod', 'attempts', 'eq', 1); | |
108 | ||
71c4154a TH |
109 | //------------------------------------------------------------------------------- |
110 | // Grade settings | |
111 | $this->standard_grading_coursemodule_elements(); | |
112 | ||
113 | $mform->removeElement('grade'); | |
114 | $mform->addElement('hidden', 'grade', $quizconfig->maximumgrade); | |
115 | $mform->setType('grade', PARAM_NUMBER); | |
116 | ||
aac80ff3 | 117 | //------------------------------------------------------------------------------- |
84e628a0 | 118 | $mform->addElement('header', 'layouthdr', get_string('layout', 'quiz')); |
119 | ||
aac80ff3 TH |
120 | // Shuffle questions. |
121 | $shuffleoptions = array( | |
122 | 0 => get_string('asshownoneditscreen', 'quiz'), | |
123 | 1 => get_string('shuffledrandomly', 'quiz') | |
124 | ); | |
125 | $mform->addElement('select', 'shufflequestions', get_string('questionorder', 'quiz'), | |
126 | $shuffleoptions, array('id' => 'id_shufflequestions')); | |
adf8f3f9 | 127 | $mform->setAdvanced('shufflequestions', $quizconfig->shufflequestions_adv); |
84e628a0 | 128 | $mform->setDefault('shufflequestions', $quizconfig->shufflequestions); |
129 | ||
aac80ff3 | 130 | // Questions per page. |
eeab18f0 | 131 | $pageoptions = array(); |
132 | $pageoptions[0] = get_string('neverallononepage', 'quiz'); | |
133 | $pageoptions[1] = get_string('everyquestion', 'quiz'); | |
84e628a0 | 134 | for ($i = 2; $i <= QUIZ_MAX_QPP_OPTION; ++$i) { |
eeab18f0 | 135 | $pageoptions[$i] = get_string('everynquestions', 'quiz', $i); |
a23f0aaf | 136 | } |
eeab18f0 | 137 | |
138 | $pagegroup = array(); | |
aac80ff3 TH |
139 | $pagegroup[] = $mform->createElement('select', 'questionsperpage', |
140 | get_string('newpage', 'quiz'), $pageoptions, array('id' => 'id_questionsperpage')); | |
e2249afe | 141 | $mform->setDefault('questionsperpage', $quizconfig->questionsperpage); |
a23f0aaf | 142 | |
eeab18f0 | 143 | if (!empty($this->_cm)) { |
aac80ff3 TH |
144 | $pagegroup[] = $mform->createElement('checkbox', 'repaginatenow', '', |
145 | get_string('repaginatenow', 'quiz'), array('id' => 'id_repaginatenow')); | |
eeab18f0 | 146 | $mform->disabledIf('repaginatenow', 'shufflequestions', 'eq', 1); |
f44b10ed | 147 | $PAGE->requires->yui2_lib('event'); |
9dec75db | 148 | $PAGE->requires->js('/mod/quiz/edit.js'); |
a13d4fbd | 149 | $PAGE->requires->js_init_call('quiz_settings_init'); |
eeab18f0 | 150 | } |
151 | ||
aac80ff3 TH |
152 | $mform->addGroup($pagegroup, 'questionsperpagegrp', |
153 | get_string('newpage', 'quiz'), null, false); | |
7292c11f | 154 | $mform->addHelpButton('questionsperpagegrp', 'newpage', 'quiz'); |
adf8f3f9 | 155 | $mform->setAdvanced('questionsperpagegrp', $quizconfig->questionsperpage_adv); |
eeab18f0 | 156 | |
aac80ff3 | 157 | //------------------------------------------------------------------------------- |
84e628a0 | 158 | $mform->addElement('header', 'interactionhdr', get_string('questionbehaviour', 'quiz')); |
a23f0aaf | 159 | |
aac80ff3 | 160 | // Shuffle within questions. |
84e628a0 | 161 | $mform->addElement('selectyesno', 'shuffleanswers', get_string('shufflewithin', 'quiz')); |
7292c11f | 162 | $mform->addHelpButton('shuffleanswers', 'shufflewithin', 'quiz'); |
adf8f3f9 | 163 | $mform->setAdvanced('shuffleanswers', $quizconfig->shuffleanswers_adv); |
e2249afe | 164 | $mform->setDefault('shuffleanswers', $quizconfig->shuffleanswers); |
a23f0aaf | 165 | |
aac80ff3 | 166 | // How questions behave (question behaviour). |
f2557823 TH |
167 | if (!empty($this->current->preferredbehaviour)) { |
168 | $currentbehaviour = $this->current->preferredbehaviour; | |
25302dee TH |
169 | } else { |
170 | $currentbehaviour = ''; | |
171 | } | |
172 | $behaviours = question_engine::get_behaviour_options($currentbehaviour); | |
aac80ff3 TH |
173 | $mform->addElement('select', 'preferredbehaviour', |
174 | get_string('howquestionsbehave', 'question'), $behaviours); | |
f2557823 | 175 | $mform->addHelpButton('preferredbehaviour', 'howquestionsbehave', 'question'); |
bb28e3bc | 176 | $mform->setDefault('preferredbehaviour', $quizconfig->preferredbehaviour); |
a23f0aaf | 177 | |
aac80ff3 TH |
178 | // Each attempt builds on last. |
179 | $mform->addElement('selectyesno', 'attemptonlast', | |
180 | get_string('eachattemptbuildsonthelast', 'quiz')); | |
7292c11f | 181 | $mform->addHelpButton('attemptonlast', 'eachattemptbuildsonthelast', 'quiz'); |
adf8f3f9 | 182 | $mform->setAdvanced('attemptonlast', $quizconfig->attemptonlast_adv); |
84e628a0 | 183 | $mform->setDefault('attemptonlast', $quizconfig->attemptonlast); |
184 | $mform->disabledIf('attemptonlast', 'attempts', 'eq', 1); | |
a23f0aaf | 185 | |
aac80ff3 TH |
186 | //------------------------------------------------------------------------------- |
187 | $mform->addElement('header', 'reviewoptionshdr', | |
188 | get_string('reviewoptionsheading', 'quiz')); | |
7292c11f | 189 | $mform->addHelpButton('reviewoptionshdr', 'reviewoptionsheading', 'quiz'); |
a23f0aaf | 190 | |
aac80ff3 TH |
191 | // Review options. |
192 | $this->add_review_options_group($mform, $quizconfig, 'during', | |
193 | mod_quiz_display_options::DURING); | |
194 | $this->add_review_options_group($mform, $quizconfig, 'immediately', | |
195 | mod_quiz_display_options::IMMEDIATELY_AFTER); | |
196 | $this->add_review_options_group($mform, $quizconfig, 'open', | |
197 | mod_quiz_display_options::LATER_WHILE_OPEN); | |
198 | $this->add_review_options_group($mform, $quizconfig, 'closed', | |
199 | mod_quiz_display_options::AFTER_CLOSE); | |
25302dee TH |
200 | |
201 | foreach ($behaviours as $behaviour => $notused) { | |
202 | $unusedoptions = question_engine::get_behaviour_unused_display_options($behaviour); | |
203 | foreach ($unusedoptions as $unusedoption) { | |
204 | $mform->disabledIf($unusedoption . 'during', 'preferredbehaviour', | |
205 | 'eq', $behaviour); | |
206 | } | |
207 | } | |
208 | $mform->disabledIf('attemptduring', 'preferredbehaviour', | |
209 | 'neq', 'wontmatch'); | |
210 | $mform->disabledIf('overallfeedbackduring', 'preferredbehaviour', | |
211 | 'neq', 'wontmatch'); | |
a23f0aaf | 212 | |
aac80ff3 | 213 | //------------------------------------------------------------------------------- |
84e628a0 | 214 | $mform->addElement('header', 'display', get_string('display', 'form')); |
a23f0aaf | 215 | |
aac80ff3 TH |
216 | // Show user picture. |
217 | $mform->addElement('selectyesno', 'showuserpicture', | |
218 | get_string('showuserpicture', 'quiz')); | |
7292c11f | 219 | $mform->addHelpButton('showuserpicture', 'showuserpicture', 'quiz'); |
adf8f3f9 | 220 | $mform->setAdvanced('showuserpicture', $quizconfig->showuserpicture_adv); |
84e628a0 | 221 | $mform->setDefault('showuserpicture', $quizconfig->showuserpicture); |
222 | ||
aac80ff3 | 223 | // Overall decimal points. |
84e628a0 | 224 | $options = array(); |
225 | for ($i = 0; $i <= QUIZ_MAX_DECIMAL_OPTION; $i++) { | |
226 | $options[$i] = $i; | |
227 | } | |
aac80ff3 TH |
228 | $mform->addElement('select', 'decimalpoints', get_string('decimalplaces', 'quiz'), |
229 | $options); | |
7292c11f | 230 | $mform->addHelpButton('decimalpoints', 'decimalplaces', 'quiz'); |
adf8f3f9 | 231 | $mform->setAdvanced('decimalpoints', $quizconfig->decimalpoints_adv); |
84e628a0 | 232 | $mform->setDefault('decimalpoints', $quizconfig->decimalpoints); |
a23f0aaf | 233 | |
aac80ff3 | 234 | // Question decimal points. |
84e628a0 | 235 | $options = array(-1 => get_string('sameasoverall', 'quiz')); |
236 | for ($i = 0; $i <= QUIZ_MAX_Q_DECIMAL_OPTION; $i++) { | |
237 | $options[$i] = $i; | |
238 | } | |
aac80ff3 TH |
239 | $mform->addElement('select', 'questiondecimalpoints', |
240 | get_string('decimalplacesquestion', 'quiz'), $options); | |
25a03faa | 241 | $mform->addHelpButton('questiondecimalpoints', 'decimalplacesquestion', 'quiz'); |
adf8f3f9 | 242 | $mform->setAdvanced('questiondecimalpoints', $quizconfig->questiondecimalpoints_adv); |
84e628a0 | 243 | $mform->setDefault('questiondecimalpoints', $quizconfig->questiondecimalpoints); |
244 | ||
56ed242b SH |
245 | // Show blocks during quiz attempt |
246 | $mform->addElement('selectyesno', 'showblocks', get_string('showblocks', 'quiz')); | |
7292c11f | 247 | $mform->addHelpButton('showblocks', 'showblocks', 'quiz'); |
56ed242b SH |
248 | $mform->setAdvanced('showblocks', $quizconfig->showblocks_adv); |
249 | $mform->setDefault('showblocks', $quizconfig->showblocks); | |
250 | ||
aac80ff3 | 251 | //------------------------------------------------------------------------------- |
84e628a0 | 252 | $mform->addElement('header', 'security', get_string('extraattemptrestrictions', 'quiz')); |
253 | ||
7493a4bb | 254 | // Require password to begin quiz attempt. |
84e628a0 | 255 | $mform->addElement('passwordunmask', 'quizpassword', get_string('requirepassword', 'quiz')); |
2ee60b49 | 256 | $mform->setType('quizpassword', PARAM_TEXT); |
7292c11f | 257 | $mform->addHelpButton('quizpassword', 'requirepassword', 'quiz'); |
adf8f3f9 | 258 | $mform->setAdvanced('quizpassword', $quizconfig->password_adv); |
e2249afe | 259 | $mform->setDefault('quizpassword', $quizconfig->password); |
a23f0aaf | 260 | |
aac80ff3 | 261 | // IP address. |
84e628a0 | 262 | $mform->addElement('text', 'subnet', get_string('requiresubnet', 'quiz')); |
2ee60b49 | 263 | $mform->setType('subnet', PARAM_TEXT); |
7292c11f | 264 | $mform->addHelpButton('subnet', 'requiresubnet', 'quiz'); |
adf8f3f9 | 265 | $mform->setAdvanced('subnet', $quizconfig->subnet_adv); |
e2249afe | 266 | $mform->setDefault('subnet', $quizconfig->subnet); |
a23f0aaf | 267 | |
aac80ff3 TH |
268 | // Enforced time delay between quiz attempts. |
269 | $mform->addElement('duration', 'delay1', get_string('delay1st2nd', 'quiz'), | |
270 | array('optional' => true)); | |
7292c11f | 271 | $mform->addHelpButton('delay1', 'delay1st2nd', 'quiz'); |
adf8f3f9 | 272 | $mform->setAdvanced('delay1', $quizconfig->delay1_adv); |
84e628a0 | 273 | $mform->setDefault('delay1', $quizconfig->delay1); |
274 | $mform->disabledIf('delay1', 'attempts', 'eq', 1); | |
275 | ||
aac80ff3 TH |
276 | $mform->addElement('duration', 'delay2', get_string('delaylater', 'quiz'), |
277 | array('optional' => true)); | |
7292c11f | 278 | $mform->addHelpButton('delay2', 'delaylater', 'quiz'); |
adf8f3f9 | 279 | $mform->setAdvanced('delay2', $quizconfig->delay2_adv); |
84e628a0 | 280 | $mform->setDefault('delay2', $quizconfig->delay2); |
281 | $mform->disabledIf('delay2', 'attempts', 'eq', 1); | |
282 | $mform->disabledIf('delay2', 'attempts', 'eq', 2); | |
283 | ||
aac80ff3 | 284 | // 'Secure' window. |
4344c5d5 | 285 | $mform->addElement('select', 'browsersecurity', get_string('browsersecurity', 'quiz'), |
1d9e1a3c | 286 | quiz_access_manager::get_browser_security_choices()); |
4344c5d5 TH |
287 | $mform->addHelpButton('browsersecurity', 'browsersecurity', 'quiz'); |
288 | $mform->setAdvanced('browsersecurity', $quizconfig->browsersecurity_adv); | |
289 | $mform->setDefault('browsersecurity', $quizconfig->browsersecurity); | |
84e628a0 | 290 | |
b83c32d3 TH |
291 | // Any other rule plugins. |
292 | quiz_access_manager::add_settings_form_fields($this, $mform); | |
293 | ||
aac80ff3 | 294 | //------------------------------------------------------------------------------- |
08224df1 | 295 | $mform->addElement('header', 'overallfeedbackhdr', get_string('overallfeedback', 'quiz')); |
7292c11f | 296 | $mform->addHelpButton('overallfeedbackhdr', 'overallfeedback', 'quiz'); |
a23f0aaf | 297 | |
f2557823 TH |
298 | if (isset($this->current->grade)) { |
299 | $needwarning = $this->current->grade === 0; | |
e0b7cfcb | 300 | } else { |
f2557823 | 301 | $needwarning = $quizconfig->maximumgrade == 0; |
e0b7cfcb | 302 | } |
303 | if ($needwarning) { | |
aac80ff3 TH |
304 | $mform->addElement('static', 'nogradewarning', '', |
305 | get_string('nogradewarning', 'quiz')); | |
e0b7cfcb | 306 | } |
307 | ||
aac80ff3 TH |
308 | $mform->addElement('static', 'gradeboundarystatic1', |
309 | get_string('gradeboundary', 'quiz'), '100%'); | |
a23f0aaf | 310 | |
e0b7cfcb | 311 | $repeatarray = array(); |
d0e25622 | 312 | $repeatedoptions = array(); |
aac80ff3 TH |
313 | $repeatarray[] = MoodleQuickForm::createElement('editor', 'feedbacktext', |
314 | get_string('feedback', 'quiz'), null, array('maxfiles' => EDITOR_UNLIMITED_FILES, | |
315 | 'noclean' => true, 'context' => $this->context)); | |
316 | $repeatarray[] = MoodleQuickForm::createElement('text', 'feedbackboundaries', | |
317 | get_string('gradeboundary', 'quiz'), array('size' => 10)); | |
d0e25622 TH |
318 | $repeatedoptions['feedbacktext']['type'] = PARAM_RAW; |
319 | $repeatedoptions['feedbackboundaries']['type'] = PARAM_RAW; | |
a23f0aaf | 320 | |
321 | if (!empty($this->_instance)) { | |
d0e25622 TH |
322 | $this->_feedbacks = $DB->get_records('quiz_feedback', |
323 | array('quizid' => $this->_instance), 'mingrade DESC'); | |
a23f0aaf | 324 | } else { |
325 | $this->_feedbacks = array(); | |
326 | } | |
327 | $numfeedbacks = max(count($this->_feedbacks) * 1.5, 5); | |
ebff6e2c | 328 | |
d0e25622 TH |
329 | $nextel = $this->repeat_elements($repeatarray, $numfeedbacks - 1, |
330 | $repeatedoptions, 'boundary_repeats', 'boundary_add_fields', 3, | |
6f3b54c8 | 331 | get_string('addmoreoverallfeedbacks', 'quiz'), true); |
a23f0aaf | 332 | |
e0b7cfcb | 333 | // Put some extra elements in before the button |
d0e25622 TH |
334 | $mform->insertElementBefore(MoodleQuickForm::createElement('editor', |
335 | "feedbacktext[$nextel]", get_string('feedback', 'quiz'), null, | |
336 | array('maxfiles' => EDITOR_UNLIMITED_FILES, 'noclean' => true, | |
337 | 'context' => $this->context)), | |
338 | 'boundary_add_fields'); | |
339 | $mform->insertElementBefore(MoodleQuickForm::createElement('static', | |
340 | 'gradeboundarystatic2', get_string('gradeboundary', 'quiz'), '0%'), | |
341 | 'boundary_add_fields'); | |
a23f0aaf | 342 | |
e0b7cfcb | 343 | // Add the disabledif rules. We cannot do this using the $repeatoptions parameter to |
81522abd | 344 | // repeat_elements because we don't want to dissable the first feedbacktext. |
e0b7cfcb | 345 | for ($i = 0; $i < $nextel; $i++) { |
346 | $mform->disabledIf('feedbackboundaries[' . $i . ']', 'grade', 'eq', 0); | |
347 | $mform->disabledIf('feedbacktext[' . ($i + 1) . ']', 'grade', 'eq', 0); | |
348 | } | |
349 | ||
aac80ff3 | 350 | //------------------------------------------------------------------------------- |
42f103be | 351 | $this->standard_coursemodule_elements(); |
84e628a0 | 352 | |
aac80ff3 | 353 | //------------------------------------------------------------------------------- |
a23f0aaf | 354 | // buttons |
355 | $this->add_action_buttons(); | |
2ee60b49 | 356 | } |
a23f0aaf | 357 | |
14936199 | 358 | protected function add_review_options_group($mform, $quizconfig, $whenname, $when) { |
25302dee TH |
359 | $group = array(); |
360 | foreach (self::$reviewfields as $field => $label) { | |
361 | $group[] = $mform->createElement('checkbox', $field . $whenname, '', $label); | |
362 | } | |
aac80ff3 TH |
363 | $mform->addGroup($group, $whenname . 'optionsgrp', |
364 | get_string('review' . $whenname, 'quiz'), null, false); | |
25302dee TH |
365 | |
366 | foreach (self::$reviewfields as $field => $notused) { | |
14936199 TH |
367 | $cfgfield = 'review' . $field; |
368 | if ($quizconfig->$cfgfield & $when) { | |
25302dee TH |
369 | $mform->setDefault($field . $whenname, 1); |
370 | } else { | |
371 | $mform->setDefault($field . $whenname, 0); | |
372 | } | |
373 | } | |
374 | ||
375 | $mform->disabledIf('correctness' . $whenname, 'attempt' . $whenname); | |
376 | $mform->disabledIf('specificfeedback' . $whenname, 'attempt' . $whenname); | |
377 | $mform->disabledIf('generalfeedback' . $whenname, 'attempt' . $whenname); | |
378 | $mform->disabledIf('rightanswer' . $whenname, 'attempt' . $whenname); | |
379 | } | |
380 | ||
381 | protected function preprocessing_review_settings(&$toform, $whenname, $when) { | |
382 | foreach (self::$reviewfields as $field => $notused) { | |
383 | $fieldname = 'review' . $field; | |
384 | if (array_key_exists($fieldname, $toform)) { | |
385 | $toform[$field . $whenname] = $toform[$fieldname] & $when; | |
386 | } | |
387 | } | |
388 | } | |
389 | ||
06cd11a9 | 390 | public function data_preprocessing(&$toform) { |
25302dee | 391 | if (isset($toform['grade'])) { |
aac80ff3 TH |
392 | // Convert to a real number, so we don't get 0.0000. |
393 | $toform['grade'] = $toform['grade'] + 0; | |
e0b7cfcb | 394 | } |
395 | ||
a23f0aaf | 396 | if (count($this->_feedbacks)) { |
397 | $key = 0; | |
aac80ff3 | 398 | foreach ($this->_feedbacks as $feedback) { |
fe6ce234 | 399 | $draftid = file_get_submitted_draft_itemid('feedbacktext['.$key.']'); |
25302dee | 400 | $toform['feedbacktext['.$key.']']['text'] = file_prepare_draft_area( |
c5c16a2c TH |
401 | $draftid, // draftid |
402 | $this->context->id, // context | |
403 | 'mod_quiz', // component | |
fe6ce234 | 404 | 'feedback', // filarea |
c5c16a2c | 405 | !empty($feedback->id) ? (int) $feedback->id : null, // itemid |
fe6ce234 | 406 | null, |
c5c16a2c | 407 | $feedback->feedbacktext // text |
fe6ce234 | 408 | ); |
25302dee TH |
409 | $toform['feedbacktext['.$key.']']['format'] = $feedback->feedbacktextformat; |
410 | $toform['feedbacktext['.$key.']']['itemid'] = $draftid; | |
fe6ce234 | 411 | |
c5c16a2c TH |
412 | if ($toform['grade'] == 0) { |
413 | // When a quiz is un-graded, there can only be one lot of | |
414 | // feedback. If the quiz previously had a maximum grade and | |
415 | // several lots of feedback, we must now avoid putting text | |
416 | // into input boxes that are disabled, but which the | |
417 | // validation will insist are blank. | |
418 | break; | |
419 | } | |
420 | ||
a23f0aaf | 421 | if ($feedback->mingrade > 0) { |
aac80ff3 TH |
422 | $toform['feedbackboundaries['.$key.']'] = |
423 | (100.0 * $feedback->mingrade / $toform['grade']) . '%'; | |
a23f0aaf | 424 | } |
425 | $key++; | |
426 | } | |
a23f0aaf | 427 | } |
298daa21 | 428 | |
25302dee TH |
429 | if (isset($toform['timelimit'])) { |
430 | $toform['timelimitenable'] = $toform['timelimit'] > 0; | |
a23f0aaf | 431 | } |
432 | ||
aac80ff3 TH |
433 | $this->preprocessing_review_settings($toform, 'during', |
434 | mod_quiz_display_options::DURING); | |
435 | $this->preprocessing_review_settings($toform, 'immediately', | |
436 | mod_quiz_display_options::IMMEDIATELY_AFTER); | |
437 | $this->preprocessing_review_settings($toform, 'open', | |
438 | mod_quiz_display_options::LATER_WHILE_OPEN); | |
439 | $this->preprocessing_review_settings($toform, 'closed', | |
440 | mod_quiz_display_options::AFTER_CLOSE); | |
25302dee TH |
441 | $toform['attemptduring'] = true; |
442 | $toform['overallfeedbackduring'] = false; | |
443 | ||
444 | // Password field - different in form to stop browsers that remember | |
445 | // passwords from getting confused. | |
446 | if (isset($toform['password'])) { | |
447 | $toform['quizpassword'] = $toform['password']; | |
448 | unset($toform['password']); | |
ab0a8ff2 | 449 | } |
c18ba64c TH |
450 | |
451 | // Load any settings belonging to the access rules. | |
452 | if (!empty($toform['instance'])) { | |
453 | $accesssettings = quiz_access_manager::load_settings($toform['instance']); | |
454 | foreach ($accesssettings as $name => $value) { | |
455 | $toform[$name] = $value; | |
456 | } | |
457 | } | |
2ee60b49 | 458 | } |
a23f0aaf | 459 | |
c7df5006 | 460 | public function validation($data, $files) { |
a78890d5 | 461 | $errors = parent::validation($data, $files); |
60243313 | 462 | |
a23f0aaf | 463 | // Check open and close times are consistent. |
aac80ff3 TH |
464 | if ($data['timeopen'] != 0 && $data['timeclose'] != 0 && |
465 | $data['timeclose'] < $data['timeopen']) { | |
a23f0aaf | 466 | $errors['timeclose'] = get_string('closebeforeopen', 'quiz'); |
467 | } | |
468 | ||
469 | // Check the boundary value is a number or a percentage, and in range. | |
470 | $i = 0; | |
471 | while (!empty($data['feedbackboundaries'][$i] )) { | |
472 | $boundary = trim($data['feedbackboundaries'][$i]); | |
473 | if (strlen($boundary) > 0 && $boundary[strlen($boundary) - 1] == '%') { | |
474 | $boundary = trim(substr($boundary, 0, -1)); | |
475 | if (is_numeric($boundary)) { | |
476 | $boundary = $boundary * $data['grade'] / 100.0; | |
477 | } else { | |
aac80ff3 TH |
478 | $errors["feedbackboundaries[$i]"] = |
479 | get_string('feedbackerrorboundaryformat', 'quiz', $i + 1); | |
a23f0aaf | 480 | } |
481 | } | |
482 | if (is_numeric($boundary) && $boundary <= 0 || $boundary >= $data['grade'] ) { | |
aac80ff3 TH |
483 | $errors["feedbackboundaries[$i]"] = |
484 | get_string('feedbackerrorboundaryoutofrange', 'quiz', $i + 1); | |
a23f0aaf | 485 | } |
aac80ff3 TH |
486 | if (is_numeric($boundary) && $i > 0 && |
487 | $boundary >= $data['feedbackboundaries'][$i - 1]) { | |
488 | $errors["feedbackboundaries[$i]"] = | |
489 | get_string('feedbackerrororder', 'quiz', $i + 1); | |
a23f0aaf | 490 | } |
491 | $data['feedbackboundaries'][$i] = $boundary; | |
492 | $i += 1; | |
493 | } | |
494 | $numboundaries = $i; | |
495 | ||
496 | // Check there is nothing in the remaining unused fields. | |
e0b7cfcb | 497 | if (!empty($data['feedbackboundaries'])) { |
498 | for ($i = $numboundaries; $i < count($data['feedbackboundaries']); $i += 1) { | |
aac80ff3 TH |
499 | if (!empty($data['feedbackboundaries'][$i] ) && |
500 | trim($data['feedbackboundaries'][$i] ) != '') { | |
501 | $errors["feedbackboundaries[$i]"] = | |
502 | get_string('feedbackerrorjunkinboundary', 'quiz', $i + 1); | |
e0b7cfcb | 503 | } |
a23f0aaf | 504 | } |
505 | } | |
e0b7cfcb | 506 | for ($i = $numboundaries + 1; $i < count($data['feedbacktext']); $i += 1) { |
aac80ff3 TH |
507 | if (!empty($data['feedbacktext'][$i]['text']) && |
508 | trim($data['feedbacktext'][$i]['text'] ) != '') { | |
509 | $errors["feedbacktext[$i]"] = | |
510 | get_string('feedbackerrorjunkinfeedback', 'quiz', $i + 1); | |
a23f0aaf | 511 | } |
512 | } | |
60243313 | 513 | |
32648682 | 514 | return $errors; |
2ee60b49 | 515 | } |
a23f0aaf | 516 | } |