Merge branch 'wip-mdl-30121-m22' of git://github.com/rajeshtaneja/moodle into MOODLE_...
[moodle.git] / mod / quiz / mod_form.php
blobe207ad08fcb93b24d98586e927685a954dacaac8
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 // Grade settings
111 $this->standard_grading_coursemodule_elements();
113 $mform->removeElement('grade');
114 $mform->addElement('hidden', 'grade', $quizconfig->maximumgrade);
115 $mform->setType('grade', PARAM_NUMBER);
117 //-------------------------------------------------------------------------------
118 $mform->addElement('header', 'layouthdr', get_string('layout', 'quiz'));
120 // Shuffle questions.
121 $shuffleoptions = array(
122 0 => get_string('asshownoneditscreen', 'quiz'),
123 1 => get_string('shuffledrandomly', 'quiz')
125 $mform->addElement('select', 'shufflequestions', get_string('questionorder', 'quiz'),
126 $shuffleoptions, array('id' => 'id_shufflequestions'));
127 $mform->setAdvanced('shufflequestions', $quizconfig->shufflequestions_adv);
128 $mform->setDefault('shufflequestions', $quizconfig->shufflequestions);
130 // Questions per page.
131 $pageoptions = array();
132 $pageoptions[0] = get_string('neverallononepage', 'quiz');
133 $pageoptions[1] = get_string('everyquestion', 'quiz');
134 for ($i = 2; $i <= QUIZ_MAX_QPP_OPTION; ++$i) {
135 $pageoptions[$i] = get_string('everynquestions', 'quiz', $i);
138 $pagegroup = array();
139 $pagegroup[] = $mform->createElement('select', 'questionsperpage',
140 get_string('newpage', 'quiz'), $pageoptions, array('id' => 'id_questionsperpage'));
141 $mform->setDefault('questionsperpage', $quizconfig->questionsperpage);
143 if (!empty($this->_cm)) {
144 $pagegroup[] = $mform->createElement('checkbox', 'repaginatenow', '',
145 get_string('repaginatenow', 'quiz'), array('id' => 'id_repaginatenow'));
146 $mform->disabledIf('repaginatenow', 'shufflequestions', 'eq', 1);
147 $PAGE->requires->yui2_lib('event');
148 $PAGE->requires->js('/mod/quiz/edit.js');
149 $PAGE->requires->js_init_call('quiz_settings_init');
152 $mform->addGroup($pagegroup, 'questionsperpagegrp',
153 get_string('newpage', 'quiz'), null, false);
154 $mform->addHelpButton('questionsperpagegrp', 'newpage', 'quiz');
155 $mform->setAdvanced('questionsperpagegrp', $quizconfig->questionsperpage_adv);
157 //-------------------------------------------------------------------------------
158 $mform->addElement('header', 'interactionhdr', get_string('questionbehaviour', 'quiz'));
160 // Shuffle within questions.
161 $mform->addElement('selectyesno', 'shuffleanswers', get_string('shufflewithin', 'quiz'));
162 $mform->addHelpButton('shuffleanswers', 'shufflewithin', 'quiz');
163 $mform->setAdvanced('shuffleanswers', $quizconfig->shuffleanswers_adv);
164 $mform->setDefault('shuffleanswers', $quizconfig->shuffleanswers);
166 // How questions behave (question behaviour).
167 if (!empty($this->current->preferredbehaviour)) {
168 $currentbehaviour = $this->current->preferredbehaviour;
169 } else {
170 $currentbehaviour = '';
172 $behaviours = question_engine::get_behaviour_options($currentbehaviour);
173 $mform->addElement('select', 'preferredbehaviour',
174 get_string('howquestionsbehave', 'question'), $behaviours);
175 $mform->addHelpButton('preferredbehaviour', 'howquestionsbehave', 'question');
176 $mform->setDefault('preferredbehaviour', $quizconfig->preferredbehaviour);
178 // Each attempt builds on last.
179 $mform->addElement('selectyesno', 'attemptonlast',
180 get_string('eachattemptbuildsonthelast', 'quiz'));
181 $mform->addHelpButton('attemptonlast', 'eachattemptbuildsonthelast', 'quiz');
182 $mform->setAdvanced('attemptonlast', $quizconfig->attemptonlast_adv);
183 $mform->setDefault('attemptonlast', $quizconfig->attemptonlast);
184 $mform->disabledIf('attemptonlast', 'attempts', 'eq', 1);
186 //-------------------------------------------------------------------------------
187 $mform->addElement('header', 'reviewoptionshdr',
188 get_string('reviewoptionsheading', 'quiz'));
189 $mform->addHelpButton('reviewoptionshdr', 'reviewoptionsheading', 'quiz');
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);
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);
208 $mform->disabledIf('attemptduring', 'preferredbehaviour',
209 'neq', 'wontmatch');
210 $mform->disabledIf('overallfeedbackduring', 'preferredbehaviour',
211 'neq', 'wontmatch');
213 //-------------------------------------------------------------------------------
214 $mform->addElement('header', 'display', get_string('display', 'form'));
216 // Show user picture.
217 $mform->addElement('selectyesno', 'showuserpicture',
218 get_string('showuserpicture', 'quiz'));
219 $mform->addHelpButton('showuserpicture', 'showuserpicture', 'quiz');
220 $mform->setAdvanced('showuserpicture', $quizconfig->showuserpicture_adv);
221 $mform->setDefault('showuserpicture', $quizconfig->showuserpicture);
223 // Overall decimal points.
224 $options = array();
225 for ($i = 0; $i <= QUIZ_MAX_DECIMAL_OPTION; $i++) {
226 $options[$i] = $i;
228 $mform->addElement('select', 'decimalpoints', get_string('decimalplaces', 'quiz'),
229 $options);
230 $mform->addHelpButton('decimalpoints', 'decimalplaces', 'quiz');
231 $mform->setAdvanced('decimalpoints', $quizconfig->decimalpoints_adv);
232 $mform->setDefault('decimalpoints', $quizconfig->decimalpoints);
234 // Question decimal points.
235 $options = array(-1 => get_string('sameasoverall', 'quiz'));
236 for ($i = 0; $i <= QUIZ_MAX_Q_DECIMAL_OPTION; $i++) {
237 $options[$i] = $i;
239 $mform->addElement('select', 'questiondecimalpoints',
240 get_string('decimalplacesquestion', 'quiz'), $options);
241 $mform->addHelpButton('questiondecimalpoints', 'decimalplacesquestion', 'quiz');
242 $mform->setAdvanced('questiondecimalpoints', $quizconfig->questiondecimalpoints_adv);
243 $mform->setDefault('questiondecimalpoints', $quizconfig->questiondecimalpoints);
245 // Show blocks during quiz attempt
246 $mform->addElement('selectyesno', 'showblocks', get_string('showblocks', 'quiz'));
247 $mform->addHelpButton('showblocks', 'showblocks', 'quiz');
248 $mform->setAdvanced('showblocks', $quizconfig->showblocks_adv);
249 $mform->setDefault('showblocks', $quizconfig->showblocks);
251 //-------------------------------------------------------------------------------
252 $mform->addElement('header', 'security', get_string('extraattemptrestrictions', 'quiz'));
254 // Require password to begin quiz attempt.
255 $mform->addElement('passwordunmask', 'quizpassword', get_string('requirepassword', 'quiz'));
256 $mform->setType('quizpassword', PARAM_TEXT);
257 $mform->addHelpButton('quizpassword', 'requirepassword', 'quiz');
258 $mform->setAdvanced('quizpassword', $quizconfig->password_adv);
259 $mform->setDefault('quizpassword', $quizconfig->password);
261 // IP address.
262 $mform->addElement('text', 'subnet', get_string('requiresubnet', 'quiz'));
263 $mform->setType('subnet', PARAM_TEXT);
264 $mform->addHelpButton('subnet', 'requiresubnet', 'quiz');
265 $mform->setAdvanced('subnet', $quizconfig->subnet_adv);
266 $mform->setDefault('subnet', $quizconfig->subnet);
268 // Enforced time delay between quiz attempts.
269 $mform->addElement('duration', 'delay1', get_string('delay1st2nd', 'quiz'),
270 array('optional' => true));
271 $mform->addHelpButton('delay1', 'delay1st2nd', 'quiz');
272 $mform->setAdvanced('delay1', $quizconfig->delay1_adv);
273 $mform->setDefault('delay1', $quizconfig->delay1);
274 $mform->disabledIf('delay1', 'attempts', 'eq', 1);
276 $mform->addElement('duration', 'delay2', get_string('delaylater', 'quiz'),
277 array('optional' => true));
278 $mform->addHelpButton('delay2', 'delaylater', 'quiz');
279 $mform->setAdvanced('delay2', $quizconfig->delay2_adv);
280 $mform->setDefault('delay2', $quizconfig->delay2);
281 $mform->disabledIf('delay2', 'attempts', 'eq', 1);
282 $mform->disabledIf('delay2', 'attempts', 'eq', 2);
284 // 'Secure' window.
285 $mform->addElement('select', 'browsersecurity', get_string('browsersecurity', 'quiz'),
286 quiz_access_manager::get_browser_security_choices());
287 $mform->addHelpButton('browsersecurity', 'browsersecurity', 'quiz');
288 $mform->setAdvanced('browsersecurity', $quizconfig->browsersecurity_adv);
289 $mform->setDefault('browsersecurity', $quizconfig->browsersecurity);
291 // Any other rule plugins.
292 quiz_access_manager::add_settings_form_fields($this, $mform);
294 //-------------------------------------------------------------------------------
295 $mform->addElement('header', 'overallfeedbackhdr', get_string('overallfeedback', 'quiz'));
296 $mform->addHelpButton('overallfeedbackhdr', 'overallfeedback', 'quiz');
298 if (isset($this->current->grade)) {
299 $needwarning = $this->current->grade === 0;
300 } else {
301 $needwarning = $quizconfig->maximumgrade == 0;
303 if ($needwarning) {
304 $mform->addElement('static', 'nogradewarning', '',
305 get_string('nogradewarning', 'quiz'));
308 $mform->addElement('static', 'gradeboundarystatic1',
309 get_string('gradeboundary', 'quiz'), '100%');
311 $repeatarray = array();
312 $repeatedoptions = array();
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));
318 $repeatedoptions['feedbacktext']['type'] = PARAM_RAW;
319 $repeatedoptions['feedbackboundaries']['type'] = PARAM_RAW;
321 if (!empty($this->_instance)) {
322 $this->_feedbacks = $DB->get_records('quiz_feedback',
323 array('quizid' => $this->_instance), 'mingrade DESC');
324 } else {
325 $this->_feedbacks = array();
327 $numfeedbacks = max(count($this->_feedbacks) * 1.5, 5);
329 $nextel = $this->repeat_elements($repeatarray, $numfeedbacks - 1,
330 $repeatedoptions, 'boundary_repeats', 'boundary_add_fields', 3,
331 get_string('addmoreoverallfeedbacks', 'quiz'), true);
333 // Put some extra elements in before the button
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');
343 // Add the disabledif rules. We cannot do this using the $repeatoptions parameter to
344 // repeat_elements because we don't want to dissable the first feedbacktext.
345 for ($i = 0; $i < $nextel; $i++) {
346 $mform->disabledIf('feedbackboundaries[' . $i . ']', 'grade', 'eq', 0);
347 $mform->disabledIf('feedbacktext[' . ($i + 1) . ']', 'grade', 'eq', 0);
350 //-------------------------------------------------------------------------------
351 $this->standard_coursemodule_elements();
353 //-------------------------------------------------------------------------------
354 // buttons
355 $this->add_action_buttons();
358 protected function add_review_options_group($mform, $quizconfig, $whenname, $when) {
359 $group = array();
360 foreach (self::$reviewfields as $field => $label) {
361 $group[] = $mform->createElement('checkbox', $field . $whenname, '', $label);
363 $mform->addGroup($group, $whenname . 'optionsgrp',
364 get_string('review' . $whenname, 'quiz'), null, false);
366 foreach (self::$reviewfields as $field => $notused) {
367 $cfgfield = 'review' . $field;
368 if ($quizconfig->$cfgfield & $when) {
369 $mform->setDefault($field . $whenname, 1);
370 } else {
371 $mform->setDefault($field . $whenname, 0);
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);
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;
390 public function data_preprocessing(&$toform) {
391 if (isset($toform['grade'])) {
392 // Convert to a real number, so we don't get 0.0000.
393 $toform['grade'] = $toform['grade'] + 0;
396 if (count($this->_feedbacks)) {
397 $key = 0;
398 foreach ($this->_feedbacks as $feedback) {
399 $draftid = file_get_submitted_draft_itemid('feedbacktext['.$key.']');
400 $toform['feedbacktext['.$key.']']['text'] = file_prepare_draft_area(
401 $draftid, // draftid
402 $this->context->id, // context
403 'mod_quiz', // component
404 'feedback', // filarea
405 !empty($feedback->id) ? (int) $feedback->id : null, // itemid
406 null,
407 $feedback->feedbacktext // text
409 $toform['feedbacktext['.$key.']']['format'] = $feedback->feedbacktextformat;
410 $toform['feedbacktext['.$key.']']['itemid'] = $draftid;
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;
421 if ($feedback->mingrade > 0) {
422 $toform['feedbackboundaries['.$key.']'] =
423 (100.0 * $feedback->mingrade / $toform['grade']) . '%';
425 $key++;
429 if (isset($toform['timelimit'])) {
430 $toform['timelimitenable'] = $toform['timelimit'] > 0;
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);
441 $toform['attemptduring'] = true;
442 $toform['overallfeedbackduring'] = false;
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']);
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;
460 public function validation($data, $files) {
461 $errors = parent::validation($data, $files);
463 // Check open and close times are consistent.
464 if ($data['timeopen'] != 0 && $data['timeclose'] != 0 &&
465 $data['timeclose'] < $data['timeopen']) {
466 $errors['timeclose'] = get_string('closebeforeopen', 'quiz');
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 {
478 $errors["feedbackboundaries[$i]"] =
479 get_string('feedbackerrorboundaryformat', 'quiz', $i + 1);
482 if (is_numeric($boundary) && $boundary <= 0 || $boundary >= $data['grade'] ) {
483 $errors["feedbackboundaries[$i]"] =
484 get_string('feedbackerrorboundaryoutofrange', 'quiz', $i + 1);
486 if (is_numeric($boundary) && $i > 0 &&
487 $boundary >= $data['feedbackboundaries'][$i - 1]) {
488 $errors["feedbackboundaries[$i]"] =
489 get_string('feedbackerrororder', 'quiz', $i + 1);
491 $data['feedbackboundaries'][$i] = $boundary;
492 $i += 1;
494 $numboundaries = $i;
496 // Check there is nothing in the remaining unused fields.
497 if (!empty($data['feedbackboundaries'])) {
498 for ($i = $numboundaries; $i < count($data['feedbackboundaries']); $i += 1) {
499 if (!empty($data['feedbackboundaries'][$i] ) &&
500 trim($data['feedbackboundaries'][$i] ) != '') {
501 $errors["feedbackboundaries[$i]"] =
502 get_string('feedbackerrorjunkinboundary', 'quiz', $i + 1);
506 for ($i = $numboundaries + 1; $i < count($data['feedbacktext']); $i += 1) {
507 if (!empty($data['feedbacktext'][$i]['text']) &&
508 trim($data['feedbacktext'][$i]['text'] ) != '') {
509 $errors["feedbacktext[$i]"] =
510 get_string('feedbackerrorjunkinfeedback', 'quiz', $i + 1);
514 return $errors;