MDL-27408 Moved the question engine install/upgrade code into the proper place.
[moodle.git] / mod / quiz / mod_form.php
blob4b84862a4138cb609dc3631e5f09b93f65ccac59
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'), array('optional' => true));
86 $mform->addHelpButton('timelimit', 'timelimit', 'quiz');
87 $mform->setAdvanced('timelimit', $quizconfig->timelimit_adv);
88 $mform->setDefault('timelimit', $quizconfig->timelimit);
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;
95 $mform->addElement('select', 'attempts', get_string('attemptsallowed', 'quiz'), $attemptoptions);
96 $mform->setAdvanced('attempts', $quizconfig->attempts_adv);
97 $mform->setDefault('attempts', $quizconfig->attempts);
99 /// Grading method.
100 $mform->addElement('select', 'grademethod', get_string('grademethod', 'quiz'), quiz_get_grading_options());
101 $mform->addHelpButton('grademethod', 'grademethod', 'quiz');
102 $mform->setAdvanced('grademethod', $quizconfig->grademethod_adv);
103 $mform->setDefault('grademethod', $quizconfig->grademethod);
104 $mform->disabledIf('grademethod', 'attempts', 'eq', 1);
106 //-------------------------------------------------------------------------------
107 $mform->addElement('header', 'layouthdr', get_string('layout', 'quiz'));
109 /// Shuffle questions.
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'));
112 $mform->setAdvanced('shufflequestions', $quizconfig->shufflequestions_adv);
113 $mform->setDefault('shufflequestions', $quizconfig->shufflequestions);
115 /// Questions per page.
116 $pageoptions = array();
117 $pageoptions[0] = get_string('neverallononepage', 'quiz');
118 $pageoptions[1] = get_string('everyquestion', 'quiz');
119 for ($i = 2; $i <= QUIZ_MAX_QPP_OPTION; ++$i) {
120 $pageoptions[$i] = get_string('everynquestions', 'quiz', $i);
123 $pagegroup = array();
124 $pagegroup[] = &$mform->createElement('select', 'questionsperpage', get_string('newpage', 'quiz'), $pageoptions, array('id' => 'id_questionsperpage'));
125 $mform->setDefault('questionsperpage', $quizconfig->questionsperpage);
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);
130 $PAGE->requires->yui2_lib('event');
131 $PAGE->requires->js('/mod/quiz/edit.js');
132 $PAGE->requires->js_init_call('quiz_settings_init');
135 $mform->addGroup($pagegroup, 'questionsperpagegrp', get_string('newpage', 'quiz'), null, false);
136 $mform->addHelpButton('questionsperpagegrp', 'newpage', 'quiz');
137 $mform->setAdvanced('questionsperpagegrp', $quizconfig->questionsperpage_adv);
139 //-------------------------------------------------------------------------------
140 $mform->addElement('header', 'interactionhdr', get_string('questionbehaviour', 'quiz'));
142 /// Shuffle within questions.
143 $mform->addElement('selectyesno', 'shuffleanswers', get_string('shufflewithin', 'quiz'));
144 $mform->addHelpButton('shuffleanswers', 'shufflewithin', 'quiz');
145 $mform->setAdvanced('shuffleanswers', $quizconfig->shuffleanswers_adv);
146 $mform->setDefault('shuffleanswers', $quizconfig->shuffleanswers);
148 /// How questions behave (question behaviour).
149 if (!empty($this->current->preferredbehaviour)) {
150 $currentbehaviour = $this->current->preferredbehaviour;
151 } else {
152 $currentbehaviour = '';
154 $behaviours = question_engine::get_behaviour_options($currentbehaviour);
155 $mform->addElement('select', 'preferredbehaviour', get_string('howquestionsbehave', 'question'), $behaviours);
156 $mform->addHelpButton('preferredbehaviour', 'howquestionsbehave', 'question');
157 $mform->setDefault('preferredbehaviour', $quizconfig->preferredbehaviour);
159 /// Each attempt builds on last.
160 $mform->addElement('selectyesno', 'attemptonlast', get_string('eachattemptbuildsonthelast', 'quiz'));
161 $mform->addHelpButton('attemptonlast', 'eachattemptbuildsonthelast', 'quiz');
162 $mform->setAdvanced('attemptonlast', $quizconfig->attemptonlast_adv);
163 $mform->setDefault('attemptonlast', $quizconfig->attemptonlast);
164 $mform->disabledIf('attemptonlast', 'attempts', 'eq', 1);
166 //-------------------------------------------------------------------------------
167 $mform->addElement('header', 'reviewoptionshdr', get_string('reviewoptionsheading', 'quiz'));
168 $mform->addHelpButton('reviewoptionshdr', 'reviewoptionsheading', 'quiz');
170 /// Review options.
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);
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);
183 $mform->disabledIf('attemptduring', 'preferredbehaviour',
184 'neq', 'wontmatch');
185 $mform->disabledIf('overallfeedbackduring', 'preferredbehaviour',
186 'neq', 'wontmatch');
188 //-------------------------------------------------------------------------------
189 $mform->addElement('header', 'display', get_string('display', 'form'));
191 /// Show user picture.
192 $mform->addElement('selectyesno', 'showuserpicture', get_string('showuserpicture', 'quiz'));
193 $mform->addHelpButton('showuserpicture', 'showuserpicture', 'quiz');
194 $mform->setAdvanced('showuserpicture', $quizconfig->showuserpicture_adv);
195 $mform->setDefault('showuserpicture', $quizconfig->showuserpicture);
197 /// Overall decimal points.
198 $options = array();
199 for ($i = 0; $i <= QUIZ_MAX_DECIMAL_OPTION; $i++) {
200 $options[$i] = $i;
202 $mform->addElement('select', 'decimalpoints', get_string('decimalplaces', 'quiz'), $options);
203 $mform->addHelpButton('decimalpoints', 'decimalplaces', 'quiz');
204 $mform->setAdvanced('decimalpoints', $quizconfig->decimalpoints_adv);
205 $mform->setDefault('decimalpoints', $quizconfig->decimalpoints);
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;
212 $mform->addElement('select', 'questiondecimalpoints', get_string('decimalplacesquestion', 'quiz'), $options);
213 $mform->addHelpButton('questiondecimalpoints', 'decimalplacesquestion', 'quiz');
214 $mform->setAdvanced('questiondecimalpoints', $quizconfig->questiondecimalpoints_adv);
215 $mform->setDefault('questiondecimalpoints', $quizconfig->questiondecimalpoints);
217 // Show blocks during quiz attempt
218 $mform->addElement('selectyesno', 'showblocks', get_string('showblocks', 'quiz'));
219 $mform->addHelpButton('showblocks', 'showblocks', 'quiz');
220 $mform->setAdvanced('showblocks', $quizconfig->showblocks_adv);
221 $mform->setDefault('showblocks', $quizconfig->showblocks);
223 //-------------------------------------------------------------------------------
224 $mform->addElement('header', 'security', get_string('extraattemptrestrictions', 'quiz'));
226 /// Enforced time delay between quiz attempts.
227 $mform->addElement('passwordunmask', 'quizpassword', get_string('requirepassword', 'quiz'));
228 $mform->setType('quizpassword', PARAM_TEXT);
229 $mform->addHelpButton('quizpassword', 'requirepassword', 'quiz');
230 $mform->setAdvanced('quizpassword', $quizconfig->password_adv);
231 $mform->setDefault('quizpassword', $quizconfig->password);
233 /// IP address.
234 $mform->addElement('text', 'subnet', get_string('requiresubnet', 'quiz'));
235 $mform->setType('subnet', PARAM_TEXT);
236 $mform->addHelpButton('subnet', 'requiresubnet', 'quiz');
237 $mform->setAdvanced('subnet', $quizconfig->subnet_adv);
238 $mform->setDefault('subnet', $quizconfig->subnet);
240 /// Enforced time delay between quiz attempts.
241 $mform->addElement('duration', 'delay1', get_string('delay1st2nd', 'quiz'), array('optional' => true));
242 $mform->addHelpButton('delay1', 'delay1st2nd', 'quiz');
243 $mform->setAdvanced('delay1', $quizconfig->delay1_adv);
244 $mform->setDefault('delay1', $quizconfig->delay1);
245 $mform->disabledIf('delay1', 'attempts', 'eq', 1);
247 $mform->addElement('duration', 'delay2', get_string('delaylater', 'quiz'), array('optional' => true));
248 $mform->addHelpButton('delay2', 'delaylater', 'quiz');
249 $mform->setAdvanced('delay2', $quizconfig->delay2_adv);
250 $mform->setDefault('delay2', $quizconfig->delay2);
251 $mform->disabledIf('delay2', 'attempts', 'eq', 1);
252 $mform->disabledIf('delay2', 'attempts', 'eq', 2);
254 /// 'Secure' window.
255 $options = array(
256 0 => get_string('none', 'quiz'),
257 1 => get_string('popupwithjavascriptsupport', 'quiz'));
258 if (!empty($CFG->enablesafebrowserintegration)) {
259 $options[2] = get_string('requiresafeexambrowser', 'quiz');
261 $mform->addElement('select', 'popup', get_string('browsersecurity', 'quiz'), $options);
262 $mform->addHelpButton('popup', 'browsersecurity', 'quiz');
263 $mform->setAdvanced('popup', $quizconfig->popup_adv);
264 $mform->setDefault('popup', $quizconfig->popup);
266 //-------------------------------------------------------------------------------
267 $mform->addElement('header', 'overallfeedbackhdr', get_string('overallfeedback', 'quiz'));
268 $mform->addHelpButton('overallfeedbackhdr', 'overallfeedback', 'quiz');
270 $mform->addElement('hidden', 'grade', $quizconfig->maximumgrade);
271 $mform->setType('grade', PARAM_RAW);
273 if (isset($this->current->grade)) {
274 $needwarning = $this->current->grade === 0;
275 } else {
276 $needwarning = $quizconfig->maximumgrade == 0;
278 if ($needwarning) {
279 $mform->addElement('static', 'nogradewarning', '', get_string('nogradewarning', 'quiz'));
282 $mform->addElement('static', 'gradeboundarystatic1', get_string('gradeboundary', 'quiz'), '100%');
284 $repeatarray = array();
285 $repeatedoptions = array();
286 $repeatarray[] = MoodleQuickForm::createElement('editor', 'feedbacktext', get_string('feedback', 'quiz'), null, array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'noclean'=>true, 'context'=>$this->context));
287 $repeatarray[] = MoodleQuickForm::createElement('text', 'feedbackboundaries', get_string('gradeboundary', 'quiz'), array('size' => 10));
288 $repeatedoptions['feedbacktext']['type'] = PARAM_RAW;
289 $repeatedoptions['feedbackboundaries']['type'] = PARAM_RAW;
291 if (!empty($this->_instance)) {
292 $this->_feedbacks = $DB->get_records('quiz_feedback',
293 array('quizid' => $this->_instance), 'mingrade DESC');
294 } else {
295 $this->_feedbacks = array();
297 $numfeedbacks = max(count($this->_feedbacks) * 1.5, 5);
299 $nextel = $this->repeat_elements($repeatarray, $numfeedbacks - 1,
300 $repeatedoptions, 'boundary_repeats', 'boundary_add_fields', 3,
301 get_string('addmoreoverallfeedbacks', 'quiz'), true);
303 // Put some extra elements in before the button
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');
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);
320 //-------------------------------------------------------------------------------
321 $this->standard_coursemodule_elements();
323 //-------------------------------------------------------------------------------
324 // buttons
325 $this->add_action_buttons();
328 protected function add_review_options_group($mform, $quizconfig, $whenname, $when) {
329 $group = array();
330 foreach (self::$reviewfields as $field => $label) {
331 $group[] = $mform->createElement('checkbox', $field . $whenname, '', $label);
333 $mform->addGroup($group, $whenname . 'optionsgrp', get_string('review' . $whenname, 'quiz'), null, false);
335 foreach (self::$reviewfields as $field => $notused) {
336 $cfgfield = 'review' . $field;
337 if ($quizconfig->$cfgfield & $when) {
338 $mform->setDefault($field . $whenname, 1);
339 } else {
340 $mform->setDefault($field . $whenname, 0);
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);
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;
359 public function data_preprocessing(&$toform) {
360 if (isset($toform['grade'])) {
361 $toform['grade'] = $toform['grade'] + 0; // Convert to a real number, so we don't get 0.0000.
364 if (count($this->_feedbacks)) {
365 $key = 0;
366 foreach ($this->_feedbacks as $feedback){
367 $draftid = file_get_submitted_draft_itemid('feedbacktext['.$key.']');
368 $toform['feedbacktext['.$key.']']['text'] = file_prepare_draft_area(
369 $draftid, // draftid
370 $this->context->id, // context
371 'mod_quiz', // component
372 'feedback', // filarea
373 !empty($feedback->id) ? (int) $feedback->id : null, // itemid
374 null,
375 $feedback->feedbacktext // text
377 $toform['feedbacktext['.$key.']']['format'] = $feedback->feedbacktextformat;
378 $toform['feedbacktext['.$key.']']['itemid'] = $draftid;
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;
389 if ($feedback->mingrade > 0) {
390 $toform['feedbackboundaries['.$key.']'] = (100.0 * $feedback->mingrade / $toform['grade']) . '%';
392 $key++;
396 if (isset($toform['timelimit'])) {
397 $toform['timelimitenable'] = $toform['timelimit'] > 0;
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;
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']);
415 public function validation($data, $files) {
416 $errors = parent::validation($data, $files);
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');
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);
435 if (is_numeric($boundary) && $boundary <= 0 || $boundary >= $data['grade'] ) {
436 $errors["feedbackboundaries[$i]"] = get_string('feedbackerrorboundaryoutofrange', 'quiz', $i + 1);
438 if (is_numeric($boundary) && $i > 0 && $boundary >= $data['feedbackboundaries'][$i - 1]) {
439 $errors["feedbackboundaries[$i]"] = get_string('feedbackerrororder', 'quiz', $i + 1);
441 $data['feedbackboundaries'][$i] = $boundary;
442 $i += 1;
444 $numboundaries = $i;
446 // Check there is nothing in the remaining unused fields.
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);
454 for ($i = $numboundaries + 1; $i < count($data['feedbacktext']); $i += 1) {
455 if (!empty($data['feedbacktext'][$i]['text']) && trim($data['feedbacktext'][$i]['text'] ) != '') {
456 $errors["feedbacktext[$i]"] = get_string('feedbackerrorjunkinfeedback', 'quiz', $i + 1);
460 return $errors;