MDL-28653 question output. Add a class to the main div based on question state.
[moodle.git] / mod / quiz / mod_form.php
blobd93efdd2bf015cd1bb376c7d624659988daec908
1 <?php
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.
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/>.
17 /**
18 * Defines the quiz module ettings form.
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
27 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->dirroot . '/course/moodleform_mod.php');
30 require_once($CFG->dirroot . '/mod/quiz/locallib.php');
33 /**
34 * Settings form for the quiz module.
36 * @copyright 2006 Jamie Pratt
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class mod_quiz_mod_form extends moodleform_mod {
40 protected $_feedbacks;
41 protected static $reviewfields = array(); // Initialised in the constructor.
43 public function __construct($current, $section, $cm, $course) {
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'),
53 parent::__construct($current, $section, $cm, $course);
56 protected function definition() {
57 global $COURSE, $CFG, $DB, $PAGE;
58 $quizconfig = get_config('quiz');
59 $mform = $this->_form;
61 //-------------------------------------------------------------------------------
62 $mform->addElement('header', 'general', get_string('general', 'form'));
64 // Name.
65 $mform->addElement('text', 'name', get_string('name'), array('size'=>'64'));
66 if (!empty($CFG->formatstringstriptags)) {
67 $mform->setType('name', PARAM_TEXT);
68 } else {
69 $mform->setType('name', PARAM_CLEANHTML);
71 $mform->addRule('name', null, 'required', null, 'client');
73 // Introduction.
74 $this->add_intro_editor(false, get_string('introduction', 'quiz'));
76 // Open and close dates.
77 $mform->addElement('date_time_selector', 'timeopen', get_string('quizopen', 'quiz'),
78 array('optional' => true, 'step' => 1));
79 $mform->addHelpButton('timeopen', 'quizopenclose', 'quiz');
81 $mform->addElement('date_time_selector', 'timeclose', get_string('quizclose', 'quiz'),
82 array('optional' => true, 'step' => 1));
84 // Time limit.
85 $mform->addElement('duration', 'timelimit', get_string('timelimit', 'quiz'),
86 array('optional' => true));
87 $mform->addHelpButton('timelimit', 'timelimit', 'quiz');
88 $mform->setAdvanced('timelimit', $quizconfig->timelimit_adv);
89 $mform->setDefault('timelimit', $quizconfig->timelimit);
91 // Number of attempts.
92 $attemptoptions = array('0' => get_string('unlimited'));
93 for ($i = 1; $i <= QUIZ_MAX_ATTEMPT_OPTION; $i++) {
94 $attemptoptions[$i] = $i;
96 $mform->addElement('select', 'attempts', get_string('attemptsallowed', 'quiz'),
97 $attemptoptions);
98 $mform->setAdvanced('attempts', $quizconfig->attempts_adv);
99 $mform->setDefault('attempts', $quizconfig->attempts);
101 // Grading method.
102 $mform->addElement('select', 'grademethod', get_string('grademethod', 'quiz'),
103 quiz_get_grading_options());
104 $mform->addHelpButton('grademethod', 'grademethod', 'quiz');
105 $mform->setAdvanced('grademethod', $quizconfig->grademethod_adv);
106 $mform->setDefault('grademethod', $quizconfig->grademethod);
107 $mform->disabledIf('grademethod', 'attempts', 'eq', 1);
109 //-------------------------------------------------------------------------------
110 $mform->addElement('header', 'layouthdr', get_string('layout', 'quiz'));
112 // Shuffle questions.
113 $shuffleoptions = array(
114 0 => get_string('asshownoneditscreen', 'quiz'),
115 1 => get_string('shuffledrandomly', 'quiz')
117 $mform->addElement('select', 'shufflequestions', get_string('questionorder', 'quiz'),
118 $shuffleoptions, array('id' => 'id_shufflequestions'));
119 $mform->setAdvanced('shufflequestions', $quizconfig->shufflequestions_adv);
120 $mform->setDefault('shufflequestions', $quizconfig->shufflequestions);
122 // Questions per page.
123 $pageoptions = array();
124 $pageoptions[0] = get_string('neverallononepage', 'quiz');
125 $pageoptions[1] = get_string('everyquestion', 'quiz');
126 for ($i = 2; $i <= QUIZ_MAX_QPP_OPTION; ++$i) {
127 $pageoptions[$i] = get_string('everynquestions', 'quiz', $i);
130 $pagegroup = array();
131 $pagegroup[] = $mform->createElement('select', 'questionsperpage',
132 get_string('newpage', 'quiz'), $pageoptions, array('id' => 'id_questionsperpage'));
133 $mform->setDefault('questionsperpage', $quizconfig->questionsperpage);
135 if (!empty($this->_cm)) {
136 $pagegroup[] = $mform->createElement('checkbox', 'repaginatenow', '',
137 get_string('repaginatenow', 'quiz'), array('id' => 'id_repaginatenow'));
138 $mform->disabledIf('repaginatenow', 'shufflequestions', 'eq', 1);
139 $PAGE->requires->yui2_lib('event');
140 $PAGE->requires->js('/mod/quiz/edit.js');
141 $PAGE->requires->js_init_call('quiz_settings_init');
144 $mform->addGroup($pagegroup, 'questionsperpagegrp',
145 get_string('newpage', 'quiz'), null, false);
146 $mform->addHelpButton('questionsperpagegrp', 'newpage', 'quiz');
147 $mform->setAdvanced('questionsperpagegrp', $quizconfig->questionsperpage_adv);
149 //-------------------------------------------------------------------------------
150 $mform->addElement('header', 'interactionhdr', get_string('questionbehaviour', 'quiz'));
152 // Shuffle within questions.
153 $mform->addElement('selectyesno', 'shuffleanswers', get_string('shufflewithin', 'quiz'));
154 $mform->addHelpButton('shuffleanswers', 'shufflewithin', 'quiz');
155 $mform->setAdvanced('shuffleanswers', $quizconfig->shuffleanswers_adv);
156 $mform->setDefault('shuffleanswers', $quizconfig->shuffleanswers);
158 // How questions behave (question behaviour).
159 if (!empty($this->current->preferredbehaviour)) {
160 $currentbehaviour = $this->current->preferredbehaviour;
161 } else {
162 $currentbehaviour = '';
164 $behaviours = question_engine::get_behaviour_options($currentbehaviour);
165 $mform->addElement('select', 'preferredbehaviour',
166 get_string('howquestionsbehave', 'question'), $behaviours);
167 $mform->addHelpButton('preferredbehaviour', 'howquestionsbehave', 'question');
168 $mform->setDefault('preferredbehaviour', $quizconfig->preferredbehaviour);
170 // Each attempt builds on last.
171 $mform->addElement('selectyesno', 'attemptonlast',
172 get_string('eachattemptbuildsonthelast', 'quiz'));
173 $mform->addHelpButton('attemptonlast', 'eachattemptbuildsonthelast', 'quiz');
174 $mform->setAdvanced('attemptonlast', $quizconfig->attemptonlast_adv);
175 $mform->setDefault('attemptonlast', $quizconfig->attemptonlast);
176 $mform->disabledIf('attemptonlast', 'attempts', 'eq', 1);
178 //-------------------------------------------------------------------------------
179 $mform->addElement('header', 'reviewoptionshdr',
180 get_string('reviewoptionsheading', 'quiz'));
181 $mform->addHelpButton('reviewoptionshdr', 'reviewoptionsheading', 'quiz');
183 // Review options.
184 $this->add_review_options_group($mform, $quizconfig, 'during',
185 mod_quiz_display_options::DURING);
186 $this->add_review_options_group($mform, $quizconfig, 'immediately',
187 mod_quiz_display_options::IMMEDIATELY_AFTER);
188 $this->add_review_options_group($mform, $quizconfig, 'open',
189 mod_quiz_display_options::LATER_WHILE_OPEN);
190 $this->add_review_options_group($mform, $quizconfig, 'closed',
191 mod_quiz_display_options::AFTER_CLOSE);
193 foreach ($behaviours as $behaviour => $notused) {
194 $unusedoptions = question_engine::get_behaviour_unused_display_options($behaviour);
195 foreach ($unusedoptions as $unusedoption) {
196 $mform->disabledIf($unusedoption . 'during', 'preferredbehaviour',
197 'eq', $behaviour);
200 $mform->disabledIf('attemptduring', 'preferredbehaviour',
201 'neq', 'wontmatch');
202 $mform->disabledIf('overallfeedbackduring', 'preferredbehaviour',
203 'neq', 'wontmatch');
205 //-------------------------------------------------------------------------------
206 $mform->addElement('header', 'display', get_string('display', 'form'));
208 // Show user picture.
209 $mform->addElement('selectyesno', 'showuserpicture',
210 get_string('showuserpicture', 'quiz'));
211 $mform->addHelpButton('showuserpicture', 'showuserpicture', 'quiz');
212 $mform->setAdvanced('showuserpicture', $quizconfig->showuserpicture_adv);
213 $mform->setDefault('showuserpicture', $quizconfig->showuserpicture);
215 // Overall decimal points.
216 $options = array();
217 for ($i = 0; $i <= QUIZ_MAX_DECIMAL_OPTION; $i++) {
218 $options[$i] = $i;
220 $mform->addElement('select', 'decimalpoints', get_string('decimalplaces', 'quiz'),
221 $options);
222 $mform->addHelpButton('decimalpoints', 'decimalplaces', 'quiz');
223 $mform->setAdvanced('decimalpoints', $quizconfig->decimalpoints_adv);
224 $mform->setDefault('decimalpoints', $quizconfig->decimalpoints);
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;
231 $mform->addElement('select', 'questiondecimalpoints',
232 get_string('decimalplacesquestion', 'quiz'), $options);
233 $mform->addHelpButton('questiondecimalpoints', 'decimalplacesquestion', 'quiz');
234 $mform->setAdvanced('questiondecimalpoints', $quizconfig->questiondecimalpoints_adv);
235 $mform->setDefault('questiondecimalpoints', $quizconfig->questiondecimalpoints);
237 // Show blocks during quiz attempt
238 $mform->addElement('selectyesno', 'showblocks', get_string('showblocks', 'quiz'));
239 $mform->addHelpButton('showblocks', 'showblocks', 'quiz');
240 $mform->setAdvanced('showblocks', $quizconfig->showblocks_adv);
241 $mform->setDefault('showblocks', $quizconfig->showblocks);
243 //-------------------------------------------------------------------------------
244 $mform->addElement('header', 'security', get_string('extraattemptrestrictions', 'quiz'));
246 // Enforced time delay between quiz attempts.
247 $mform->addElement('passwordunmask', 'quizpassword', get_string('requirepassword', 'quiz'));
248 $mform->setType('quizpassword', PARAM_TEXT);
249 $mform->addHelpButton('quizpassword', 'requirepassword', 'quiz');
250 $mform->setAdvanced('quizpassword', $quizconfig->password_adv);
251 $mform->setDefault('quizpassword', $quizconfig->password);
253 // IP address.
254 $mform->addElement('text', 'subnet', get_string('requiresubnet', 'quiz'));
255 $mform->setType('subnet', PARAM_TEXT);
256 $mform->addHelpButton('subnet', 'requiresubnet', 'quiz');
257 $mform->setAdvanced('subnet', $quizconfig->subnet_adv);
258 $mform->setDefault('subnet', $quizconfig->subnet);
260 // Enforced time delay between quiz attempts.
261 $mform->addElement('duration', 'delay1', get_string('delay1st2nd', 'quiz'),
262 array('optional' => true));
263 $mform->addHelpButton('delay1', 'delay1st2nd', 'quiz');
264 $mform->setAdvanced('delay1', $quizconfig->delay1_adv);
265 $mform->setDefault('delay1', $quizconfig->delay1);
266 $mform->disabledIf('delay1', 'attempts', 'eq', 1);
268 $mform->addElement('duration', 'delay2', get_string('delaylater', 'quiz'),
269 array('optional' => true));
270 $mform->addHelpButton('delay2', 'delaylater', 'quiz');
271 $mform->setAdvanced('delay2', $quizconfig->delay2_adv);
272 $mform->setDefault('delay2', $quizconfig->delay2);
273 $mform->disabledIf('delay2', 'attempts', 'eq', 1);
274 $mform->disabledIf('delay2', 'attempts', 'eq', 2);
276 // 'Secure' window.
277 $options = array(
278 0 => get_string('none', 'quiz'),
279 1 => get_string('popupwithjavascriptsupport', 'quiz'));
280 if (!empty($CFG->enablesafebrowserintegration)) {
281 $options[2] = get_string('requiresafeexambrowser', 'quiz');
283 $mform->addElement('select', 'popup', get_string('browsersecurity', 'quiz'), $options);
284 $mform->addHelpButton('popup', 'browsersecurity', 'quiz');
285 $mform->setAdvanced('popup', $quizconfig->popup_adv);
286 $mform->setDefault('popup', $quizconfig->popup);
288 //-------------------------------------------------------------------------------
289 $mform->addElement('header', 'overallfeedbackhdr', get_string('overallfeedback', 'quiz'));
290 $mform->addHelpButton('overallfeedbackhdr', 'overallfeedback', 'quiz');
292 $mform->addElement('hidden', 'grade', $quizconfig->maximumgrade);
293 $mform->setType('grade', PARAM_RAW);
295 if (isset($this->current->grade)) {
296 $needwarning = $this->current->grade === 0;
297 } else {
298 $needwarning = $quizconfig->maximumgrade == 0;
300 if ($needwarning) {
301 $mform->addElement('static', 'nogradewarning', '',
302 get_string('nogradewarning', 'quiz'));
305 $mform->addElement('static', 'gradeboundarystatic1',
306 get_string('gradeboundary', 'quiz'), '100%');
308 $repeatarray = array();
309 $repeatedoptions = array();
310 $repeatarray[] = MoodleQuickForm::createElement('editor', 'feedbacktext',
311 get_string('feedback', 'quiz'), null, array('maxfiles' => EDITOR_UNLIMITED_FILES,
312 'noclean' => true, 'context' => $this->context));
313 $repeatarray[] = MoodleQuickForm::createElement('text', 'feedbackboundaries',
314 get_string('gradeboundary', 'quiz'), array('size' => 10));
315 $repeatedoptions['feedbacktext']['type'] = PARAM_RAW;
316 $repeatedoptions['feedbackboundaries']['type'] = PARAM_RAW;
318 if (!empty($this->_instance)) {
319 $this->_feedbacks = $DB->get_records('quiz_feedback',
320 array('quizid' => $this->_instance), 'mingrade DESC');
321 } else {
322 $this->_feedbacks = array();
324 $numfeedbacks = max(count($this->_feedbacks) * 1.5, 5);
326 $nextel = $this->repeat_elements($repeatarray, $numfeedbacks - 1,
327 $repeatedoptions, 'boundary_repeats', 'boundary_add_fields', 3,
328 get_string('addmoreoverallfeedbacks', 'quiz'), true);
330 // Put some extra elements in before the button
331 $mform->insertElementBefore(MoodleQuickForm::createElement('editor',
332 "feedbacktext[$nextel]", get_string('feedback', 'quiz'), null,
333 array('maxfiles' => EDITOR_UNLIMITED_FILES, 'noclean' => true,
334 'context' => $this->context)),
335 'boundary_add_fields');
336 $mform->insertElementBefore(MoodleQuickForm::createElement('static',
337 'gradeboundarystatic2', get_string('gradeboundary', 'quiz'), '0%'),
338 'boundary_add_fields');
340 // Add the disabledif rules. We cannot do this using the $repeatoptions parameter to
341 // repeat_elements because we don't want to dissable the first feedbacktext.
342 for ($i = 0; $i < $nextel; $i++) {
343 $mform->disabledIf('feedbackboundaries[' . $i . ']', 'grade', 'eq', 0);
344 $mform->disabledIf('feedbacktext[' . ($i + 1) . ']', 'grade', 'eq', 0);
347 //-------------------------------------------------------------------------------
348 $this->standard_coursemodule_elements();
350 //-------------------------------------------------------------------------------
351 // buttons
352 $this->add_action_buttons();
355 protected function add_review_options_group($mform, $quizconfig, $whenname, $when) {
356 $group = array();
357 foreach (self::$reviewfields as $field => $label) {
358 $group[] = $mform->createElement('checkbox', $field . $whenname, '', $label);
360 $mform->addGroup($group, $whenname . 'optionsgrp',
361 get_string('review' . $whenname, 'quiz'), null, false);
363 foreach (self::$reviewfields as $field => $notused) {
364 $cfgfield = 'review' . $field;
365 if ($quizconfig->$cfgfield & $when) {
366 $mform->setDefault($field . $whenname, 1);
367 } else {
368 $mform->setDefault($field . $whenname, 0);
372 $mform->disabledIf('correctness' . $whenname, 'attempt' . $whenname);
373 $mform->disabledIf('specificfeedback' . $whenname, 'attempt' . $whenname);
374 $mform->disabledIf('generalfeedback' . $whenname, 'attempt' . $whenname);
375 $mform->disabledIf('rightanswer' . $whenname, 'attempt' . $whenname);
378 protected function preprocessing_review_settings(&$toform, $whenname, $when) {
379 foreach (self::$reviewfields as $field => $notused) {
380 $fieldname = 'review' . $field;
381 if (array_key_exists($fieldname, $toform)) {
382 $toform[$field . $whenname] = $toform[$fieldname] & $when;
387 public function data_preprocessing(&$toform) {
388 if (isset($toform['grade'])) {
389 // Convert to a real number, so we don't get 0.0000.
390 $toform['grade'] = $toform['grade'] + 0;
393 if (count($this->_feedbacks)) {
394 $key = 0;
395 foreach ($this->_feedbacks as $feedback) {
396 $draftid = file_get_submitted_draft_itemid('feedbacktext['.$key.']');
397 $toform['feedbacktext['.$key.']']['text'] = file_prepare_draft_area(
398 $draftid, // draftid
399 $this->context->id, // context
400 'mod_quiz', // component
401 'feedback', // filarea
402 !empty($feedback->id) ? (int) $feedback->id : null, // itemid
403 null,
404 $feedback->feedbacktext // text
406 $toform['feedbacktext['.$key.']']['format'] = $feedback->feedbacktextformat;
407 $toform['feedbacktext['.$key.']']['itemid'] = $draftid;
409 if ($toform['grade'] == 0) {
410 // When a quiz is un-graded, there can only be one lot of
411 // feedback. If the quiz previously had a maximum grade and
412 // several lots of feedback, we must now avoid putting text
413 // into input boxes that are disabled, but which the
414 // validation will insist are blank.
415 break;
418 if ($feedback->mingrade > 0) {
419 $toform['feedbackboundaries['.$key.']'] =
420 (100.0 * $feedback->mingrade / $toform['grade']) . '%';
422 $key++;
426 if (isset($toform['timelimit'])) {
427 $toform['timelimitenable'] = $toform['timelimit'] > 0;
430 $this->preprocessing_review_settings($toform, 'during',
431 mod_quiz_display_options::DURING);
432 $this->preprocessing_review_settings($toform, 'immediately',
433 mod_quiz_display_options::IMMEDIATELY_AFTER);
434 $this->preprocessing_review_settings($toform, 'open',
435 mod_quiz_display_options::LATER_WHILE_OPEN);
436 $this->preprocessing_review_settings($toform, 'closed',
437 mod_quiz_display_options::AFTER_CLOSE);
438 $toform['attemptduring'] = true;
439 $toform['overallfeedbackduring'] = false;
441 // Password field - different in form to stop browsers that remember
442 // passwords from getting confused.
443 if (isset($toform['password'])) {
444 $toform['quizpassword'] = $toform['password'];
445 unset($toform['password']);
449 public function validation($data, $files) {
450 $errors = parent::validation($data, $files);
452 // Check open and close times are consistent.
453 if ($data['timeopen'] != 0 && $data['timeclose'] != 0 &&
454 $data['timeclose'] < $data['timeopen']) {
455 $errors['timeclose'] = get_string('closebeforeopen', 'quiz');
458 // Check the boundary value is a number or a percentage, and in range.
459 $i = 0;
460 while (!empty($data['feedbackboundaries'][$i] )) {
461 $boundary = trim($data['feedbackboundaries'][$i]);
462 if (strlen($boundary) > 0 && $boundary[strlen($boundary) - 1] == '%') {
463 $boundary = trim(substr($boundary, 0, -1));
464 if (is_numeric($boundary)) {
465 $boundary = $boundary * $data['grade'] / 100.0;
466 } else {
467 $errors["feedbackboundaries[$i]"] =
468 get_string('feedbackerrorboundaryformat', 'quiz', $i + 1);
471 if (is_numeric($boundary) && $boundary <= 0 || $boundary >= $data['grade'] ) {
472 $errors["feedbackboundaries[$i]"] =
473 get_string('feedbackerrorboundaryoutofrange', 'quiz', $i + 1);
475 if (is_numeric($boundary) && $i > 0 &&
476 $boundary >= $data['feedbackboundaries'][$i - 1]) {
477 $errors["feedbackboundaries[$i]"] =
478 get_string('feedbackerrororder', 'quiz', $i + 1);
480 $data['feedbackboundaries'][$i] = $boundary;
481 $i += 1;
483 $numboundaries = $i;
485 // Check there is nothing in the remaining unused fields.
486 if (!empty($data['feedbackboundaries'])) {
487 for ($i = $numboundaries; $i < count($data['feedbackboundaries']); $i += 1) {
488 if (!empty($data['feedbackboundaries'][$i] ) &&
489 trim($data['feedbackboundaries'][$i] ) != '') {
490 $errors["feedbackboundaries[$i]"] =
491 get_string('feedbackerrorjunkinboundary', 'quiz', $i + 1);
495 for ($i = $numboundaries + 1; $i < count($data['feedbacktext']); $i += 1) {
496 if (!empty($data['feedbacktext'][$i]['text']) &&
497 trim($data['feedbacktext'][$i]['text'] ) != '') {
498 $errors["feedbacktext[$i]"] =
499 get_string('feedbackerrorjunkinfeedback', 'quiz', $i + 1);
503 return $errors;