3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Defines the quiz module ettings form.
23 * @copyright 2006 Jamie Pratt
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') || die();
30 require_once($CFG->dirroot . '/course/moodleform_mod.php');
31 require_once($CFG->dirroot . '/mod/quiz/locallib.php');
35 * Settings form for the quiz module.
37 * @copyright 2006 Jamie Pratt
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class mod_quiz_mod_form extends moodleform_mod {
41 protected $_feedbacks;
42 protected static $reviewfields = array(); // Initialised in the constructor.
44 public function __construct($current, $section, $cm, $course) {
45 self::$reviewfields = array(
46 'attempt' => get_string('theattempt', 'quiz'),
47 'correctness' => get_string('whethercorrect', 'question'),
48 'marks' => get_string('marks', 'question'),
49 'specificfeedback' => get_string('specificfeedback', 'question'),
50 'generalfeedback' => get_string('generalfeedback', 'question'),
51 'rightanswer' => get_string('rightanswer', 'question'),
52 'overallfeedback' => get_string('overallfeedback', 'quiz'),
54 parent::__construct($current, $section, $cm, $course);
57 function definition() {
58 global $COURSE, $CFG, $DB, $PAGE;
59 $quizconfig = get_config('quiz');
60 $mform = $this->_form;
62 //-------------------------------------------------------------------------------
63 $mform->addElement('header', 'general', get_string('general', 'form'));
66 $mform->addElement('text', 'name', get_string('name'), array('size'=>'64'));
67 if (!empty($CFG->formatstringstriptags)) {
68 $mform->setType('name', PARAM_TEXT);
70 $mform->setType('name', PARAM_CLEANHTML);
72 $mform->addRule('name', null, 'required', null, 'client');
75 $this->add_intro_editor(false, get_string('introduction', 'quiz'));
77 /// Open and close dates.
78 $mform->addElement('date_time_selector', 'timeopen', get_string('quizopen', 'quiz'),
79 array('optional' => true, 'step' => 1));
80 $mform->addHelpButton('timeopen', 'quizopenclose', 'quiz');
82 $mform->addElement('date_time_selector', 'timeclose', get_string('quizclose', 'quiz'),
83 array('optional' => true, 'step' => 1));
86 $mform->addElement('duration', 'timelimit', get_string('timelimit', 'quiz'), 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'), $attemptoptions);
97 $mform->setAdvanced('attempts', $quizconfig->attempts_adv);
98 $mform->setDefault('attempts', $quizconfig->attempts);
101 $mform->addElement('select', 'grademethod', get_string('grademethod', 'quiz'), quiz_get_grading_options());
102 $mform->addHelpButton('grademethod', 'grademethod', 'quiz');
103 $mform->setAdvanced('grademethod', $quizconfig->grademethod_adv);
104 $mform->setDefault('grademethod', $quizconfig->grademethod);
105 $mform->disabledIf('grademethod', 'attempts', 'eq', 1);
107 //-------------------------------------------------------------------------------
108 $mform->addElement('header', 'layouthdr', get_string('layout', 'quiz'));
110 /// Shuffle questions.
111 $shuffleoptions = array(0 => get_string('asshownoneditscreen', 'quiz'), 1 => get_string('shuffledrandomly', 'quiz'));
112 $mform->addElement('select', 'shufflequestions', get_string('questionorder', 'quiz'), $shuffleoptions, array('id' => 'id_shufflequestions'));
113 $mform->setAdvanced('shufflequestions', $quizconfig->shufflequestions_adv);
114 $mform->setDefault('shufflequestions', $quizconfig->shufflequestions);
116 /// Questions per page.
117 $pageoptions = array();
118 $pageoptions[0] = get_string('neverallononepage', 'quiz');
119 $pageoptions[1] = get_string('everyquestion', 'quiz');
120 for ($i = 2; $i <= QUIZ_MAX_QPP_OPTION; ++$i) {
121 $pageoptions[$i] = get_string('everynquestions', 'quiz', $i);
124 $pagegroup = array();
125 $pagegroup[] = &$mform->createElement('select', 'questionsperpage', get_string('newpage', 'quiz'), $pageoptions, array('id' => 'id_questionsperpage'));
126 $mform->setDefault('questionsperpage', $quizconfig->questionsperpage);
128 if (!empty($this->_cm)) {
129 $pagegroup[] = &$mform->createElement('checkbox', 'repaginatenow', '', get_string('repaginatenow', 'quiz'), array('id' => 'id_repaginatenow'));
130 $mform->disabledIf('repaginatenow', 'shufflequestions', 'eq', 1);
131 $PAGE->requires->yui2_lib('event');
132 $PAGE->requires->js('/mod/quiz/edit.js');
133 $PAGE->requires->js_init_call('quiz_settings_init');
136 $mform->addGroup($pagegroup, 'questionsperpagegrp', get_string('newpage', 'quiz'), null, false);
137 $mform->addHelpButton('questionsperpagegrp', 'newpage', 'quiz');
138 $mform->setAdvanced('questionsperpagegrp', $quizconfig->questionsperpage_adv);
140 //-------------------------------------------------------------------------------
141 $mform->addElement('header', 'interactionhdr', get_string('questionbehaviour', 'quiz'));
143 /// Shuffle within questions.
144 $mform->addElement('selectyesno', 'shuffleanswers', get_string('shufflewithin', 'quiz'));
145 $mform->addHelpButton('shuffleanswers', 'shufflewithin', 'quiz');
146 $mform->setAdvanced('shuffleanswers', $quizconfig->shuffleanswers_adv);
147 $mform->setDefault('shuffleanswers', $quizconfig->shuffleanswers);
149 /// How questions behave (question behaviour).
150 if (!empty($this->current->preferredbehaviour)) {
151 $currentbehaviour = $this->current->preferredbehaviour;
153 $currentbehaviour = '';
155 $behaviours = question_engine::get_behaviour_options($currentbehaviour);
156 $mform->addElement('select', 'preferredbehaviour', get_string('howquestionsbehave', 'question'), $behaviours);
157 $mform->addHelpButton('preferredbehaviour', 'howquestionsbehave', 'question');
158 $mform->setAdvanced('preferredbehaviour', $CFG->quiz_fix_preferredbehaviour);
159 $mform->setDefault('preferredbehaviour', $CFG->quiz_preferredbehaviour);
161 /// Each attempt builds on last.
162 $mform->addElement('selectyesno', 'attemptonlast', get_string('eachattemptbuildsonthelast', 'quiz'));
163 $mform->addHelpButton('attemptonlast', 'eachattemptbuildsonthelast', 'quiz');
164 $mform->setAdvanced('attemptonlast', $quizconfig->attemptonlast_adv);
165 $mform->setDefault('attemptonlast', $quizconfig->attemptonlast);
166 $mform->disabledIf('attemptonlast', 'attempts', 'eq', 1);
168 //-------------------------------------------------------------------------------
169 $mform->addElement('header', 'reviewoptionshdr', get_string('reviewoptionsheading', 'quiz'));
170 $mform->addHelpButton('reviewoptionshdr', 'reviewoptionsheading', 'quiz');
171 $mform->setAdvanced('reviewoptionshdr', $quizconfig->review_adv);
174 $this->add_review_options_group($mform, $quizconfig, 'during', mod_quiz_display_options::DURING);
175 $this->add_review_options_group($mform, $quizconfig, 'immediately', mod_quiz_display_options::IMMEDIATELY_AFTER);
176 $this->add_review_options_group($mform, $quizconfig, 'open', mod_quiz_display_options::LATER_WHILE_OPEN);
177 $this->add_review_options_group($mform, $quizconfig, 'closed', mod_quiz_display_options::AFTER_CLOSE);
179 foreach ($behaviours as $behaviour => $notused) {
180 $unusedoptions = question_engine::get_behaviour_unused_display_options($behaviour);
181 foreach ($unusedoptions as $unusedoption) {
182 $mform->disabledIf($unusedoption . 'during', 'preferredbehaviour',
186 $mform->disabledIf('attemptduring', 'preferredbehaviour',
188 $mform->disabledIf('overallfeedbackduring', 'preferredbehaviour',
191 //-------------------------------------------------------------------------------
192 $mform->addElement('header', 'display', get_string('display', 'form'));
194 /// Show user picture.
195 $mform->addElement('selectyesno', 'showuserpicture', get_string('showuserpicture', 'quiz'));
196 $mform->addHelpButton('showuserpicture', 'showuserpicture', 'quiz');
197 $mform->setAdvanced('showuserpicture', $quizconfig->showuserpicture_adv);
198 $mform->setDefault('showuserpicture', $quizconfig->showuserpicture);
200 /// Overall decimal points.
202 for ($i = 0; $i <= QUIZ_MAX_DECIMAL_OPTION; $i++) {
205 $mform->addElement('select', 'decimalpoints', get_string('decimalplaces', 'quiz'), $options);
206 $mform->addHelpButton('decimalpoints', 'decimalplaces', 'quiz');
207 $mform->setAdvanced('decimalpoints', $quizconfig->decimalpoints_adv);
208 $mform->setDefault('decimalpoints', $quizconfig->decimalpoints);
210 /// Question decimal points.
211 $options = array(-1 => get_string('sameasoverall', 'quiz'));
212 for ($i = 0; $i <= QUIZ_MAX_Q_DECIMAL_OPTION; $i++) {
215 $mform->addElement('select', 'questiondecimalpoints', get_string('decimalplacesquestion', 'quiz'), $options);
216 $mform->addHelpButton('questiondecimalpoints', 'decimalplacesquestion','quiz');
217 $mform->setAdvanced('questiondecimalpoints', $quizconfig->questiondecimalpoints_adv);
218 $mform->setDefault('questiondecimalpoints', $quizconfig->questiondecimalpoints);
220 // Show blocks during quiz attempt
221 $mform->addElement('selectyesno', 'showblocks', get_string('showblocks', 'quiz'));
222 $mform->addHelpButton('showblocks', 'showblocks', 'quiz');
223 $mform->setAdvanced('showblocks', $quizconfig->showblocks_adv);
224 $mform->setDefault('showblocks', $quizconfig->showblocks);
226 //-------------------------------------------------------------------------------
227 $mform->addElement('header', 'security', get_string('extraattemptrestrictions', 'quiz'));
229 /// Enforced time delay between quiz attempts.
230 $mform->addElement('passwordunmask', 'quizpassword', get_string('requirepassword', 'quiz'));
231 $mform->setType('quizpassword', PARAM_TEXT);
232 $mform->addHelpButton('quizpassword', 'requirepassword', 'quiz');
233 $mform->setAdvanced('quizpassword', $quizconfig->password_adv);
234 $mform->setDefault('quizpassword', $quizconfig->password);
237 $mform->addElement('text', 'subnet', get_string('requiresubnet', 'quiz'));
238 $mform->setType('subnet', PARAM_TEXT);
239 $mform->addHelpButton('subnet', 'requiresubnet', 'quiz');
240 $mform->setAdvanced('subnet', $quizconfig->subnet_adv);
241 $mform->setDefault('subnet', $quizconfig->subnet);
243 /// Enforced time delay between quiz attempts.
244 $mform->addElement('duration', 'delay1', get_string('delay1st2nd', 'quiz'), array('optional' => true));
245 $mform->addHelpButton('delay1', 'delay1st2nd', 'quiz');
246 $mform->setAdvanced('delay1', $quizconfig->delay1_adv);
247 $mform->setDefault('delay1', $quizconfig->delay1);
248 $mform->disabledIf('delay1', 'attempts', 'eq', 1);
250 $mform->addElement('duration', 'delay2', get_string('delaylater', 'quiz'), array('optional' => true));
251 $mform->addHelpButton('delay2', 'delaylater', 'quiz');
252 $mform->setAdvanced('delay2', $quizconfig->delay2_adv);
253 $mform->setDefault('delay2', $quizconfig->delay2);
254 $mform->disabledIf('delay2', 'attempts', 'eq', 1);
255 $mform->disabledIf('delay2', 'attempts', 'eq', 2);
259 0 => get_string('none', 'quiz'),
260 1 => get_string('popupwithjavascriptsupport', 'quiz'));
261 if (!empty($CFG->enablesafebrowserintegration)) {
262 $options[2] = get_string('requiresafeexambrowser', 'quiz');
264 $mform->addElement('select', 'popup', get_string('browsersecurity', 'quiz'), $options);
265 $mform->addHelpButton('popup', 'browsersecurity', 'quiz');
266 $mform->setAdvanced('popup', $quizconfig->popup_adv);
267 $mform->setDefault('popup', $quizconfig->popup);
269 //-------------------------------------------------------------------------------
270 $mform->addElement('header', 'overallfeedbackhdr', get_string('overallfeedback', 'quiz'));
271 $mform->addHelpButton('overallfeedbackhdr', 'overallfeedback', 'quiz');
273 $mform->addElement('hidden', 'grade', $quizconfig->maximumgrade);
274 $mform->setType('grade', PARAM_RAW);
276 if (isset($this->current->grade)) {
277 $needwarning = $this->current->grade === 0;
279 $needwarning = $quizconfig->maximumgrade == 0;
282 $mform->addElement('static', 'nogradewarning', '', get_string('nogradewarning', 'quiz'));
285 $mform->addElement('static', 'gradeboundarystatic1', get_string('gradeboundary', 'quiz'), '100%');
287 $repeatarray = array();
288 $repeatarray[] = MoodleQuickForm::createElement('editor', 'feedbacktext', get_string('feedback', 'quiz'), null, array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'noclean'=>true, 'context'=>$this->context));
289 $mform->setType('feedbacktext', PARAM_RAW);
290 $repeatarray[] = MoodleQuickForm::createElement('text', 'feedbackboundaries', get_string('gradeboundary', 'quiz'), array('size' => 10));
291 $mform->setType('feedbackboundaries', PARAM_NOTAGS);
293 if (!empty($this->_instance)) {
294 $this->_feedbacks = $DB->get_records('quiz_feedback', array('quizid'=>$this->_instance), 'mingrade DESC');
296 $this->_feedbacks = array();
298 $numfeedbacks = max(count($this->_feedbacks) * 1.5, 5);
300 $nextel=$this->repeat_elements($repeatarray, $numfeedbacks - 1,
301 array(), 'boundary_repeats', 'boundary_add_fields', 3,
302 get_string('addmoreoverallfeedbacks', 'quiz'), true);
304 // Put some extra elements in before the button
305 $insertEl = MoodleQuickForm::createElement('editor', "feedbacktext[$nextel]", get_string('feedback', 'quiz'), null, array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'noclean'=>true, 'context'=>$this->context));
306 $mform->insertElementBefore($insertEl, 'boundary_add_fields');
308 $insertEl = MoodleQuickForm::createElement('static', 'gradeboundarystatic2', get_string('gradeboundary', 'quiz'), '0%');
309 $mform->insertElementBefore($insertEl, 'boundary_add_fields');
311 // Add the disabledif rules. We cannot do this using the $repeatoptions parameter to
312 // repeat_elements becuase we don't want to dissable the first feedbacktext.
313 for ($i = 0; $i < $nextel; $i++) {
314 $mform->disabledIf('feedbackboundaries[' . $i . ']', 'grade', 'eq', 0);
315 $mform->disabledIf('feedbacktext[' . ($i + 1) . ']', 'grade', 'eq', 0);
318 //-------------------------------------------------------------------------------
319 $this->standard_coursemodule_elements();
321 //-------------------------------------------------------------------------------
323 $this->add_action_buttons();
326 protected function add_review_options_group($mform, $quizconfig, $whenname, $when) {
328 foreach (self::$reviewfields as $field => $label) {
329 $group[] = $mform->createElement('checkbox', $field . $whenname, '', $label);
331 $mform->addGroup($group, $whenname . 'optionsgrp', get_string('review' . $whenname, 'quiz'), null, false);
333 foreach (self::$reviewfields as $field => $notused) {
334 $cfgfield = 'review' . $field;
335 if ($quizconfig->$cfgfield & $when) {
336 $mform->setDefault($field . $whenname, 1);
338 $mform->setDefault($field . $whenname, 0);
342 $mform->disabledIf('correctness' . $whenname, 'attempt' . $whenname);
343 $mform->disabledIf('specificfeedback' . $whenname, 'attempt' . $whenname);
344 $mform->disabledIf('generalfeedback' . $whenname, 'attempt' . $whenname);
345 $mform->disabledIf('rightanswer' . $whenname, 'attempt' . $whenname);
348 protected function preprocessing_review_settings(&$toform, $whenname, $when) {
349 foreach (self::$reviewfields as $field => $notused) {
350 $fieldname = 'review' . $field;
351 if (array_key_exists($fieldname, $toform)) {
352 $toform[$field . $whenname] = $toform[$fieldname] & $when;
357 function data_preprocessing(&$toform) {
358 if (isset($toform['grade'])) {
359 $toform['grade'] = $toform['grade'] + 0; // Convert to a real number, so we don't get 0.0000.
362 if (count($this->_feedbacks)) {
364 foreach ($this->_feedbacks as $feedback){
365 $draftid = file_get_submitted_draft_itemid('feedbacktext['.$key.']');
366 $toform['feedbacktext['.$key.']']['text'] = file_prepare_draft_area(
368 $this->context->id, // context
369 'mod_quiz', // component
370 'feedback', // filarea
371 !empty($feedback->id)?(int)$feedback->id:null, // itemid
373 $feedback->feedbacktext // text
375 $toform['feedbacktext['.$key.']']['format'] = $feedback->feedbacktextformat;
376 $toform['feedbacktext['.$key.']']['itemid'] = $draftid;
378 if ($feedback->mingrade > 0) {
379 $toform['feedbackboundaries['.$key.']'] = (100.0 * $feedback->mingrade / $toform['grade']) . '%';
385 if (isset($toform['timelimit'])) {
386 $toform['timelimitenable'] = $toform['timelimit'] > 0;
389 $this->preprocessing_review_settings($toform, 'during', mod_quiz_display_options::DURING);
390 $this->preprocessing_review_settings($toform, 'immediately', mod_quiz_display_options::IMMEDIATELY_AFTER);
391 $this->preprocessing_review_settings($toform, 'open', mod_quiz_display_options::LATER_WHILE_OPEN);
392 $this->preprocessing_review_settings($toform, 'closed', mod_quiz_display_options::AFTER_CLOSE);
393 $toform['attemptduring'] = true;
394 $toform['overallfeedbackduring'] = false;
396 // Password field - different in form to stop browsers that remember
397 // passwords from getting confused.
398 if (isset($toform['password'])) {
399 $toform['quizpassword'] = $toform['password'];
400 unset($toform['password']);
404 function validation($data, $files) {
405 $errors = parent::validation($data, $files);
407 // Check open and close times are consistent.
408 if ($data['timeopen'] != 0 && $data['timeclose'] != 0 && $data['timeclose'] < $data['timeopen']) {
409 $errors['timeclose'] = get_string('closebeforeopen', 'quiz');
412 // Check the boundary value is a number or a percentage, and in range.
414 while (!empty($data['feedbackboundaries'][$i] )) {
415 $boundary = trim($data['feedbackboundaries'][$i]);
416 if (strlen($boundary) > 0 && $boundary[strlen($boundary) - 1] == '%') {
417 $boundary = trim(substr($boundary, 0, -1));
418 if (is_numeric($boundary)) {
419 $boundary = $boundary * $data['grade'] / 100.0;
421 $errors["feedbackboundaries[$i]"] = get_string('feedbackerrorboundaryformat', 'quiz', $i + 1);
424 if (is_numeric($boundary) && $boundary <= 0 || $boundary >= $data['grade'] ) {
425 $errors["feedbackboundaries[$i]"] = get_string('feedbackerrorboundaryoutofrange', 'quiz', $i + 1);
427 if (is_numeric($boundary) && $i > 0 && $boundary >= $data['feedbackboundaries'][$i - 1]) {
428 $errors["feedbackboundaries[$i]"] = get_string('feedbackerrororder', 'quiz', $i + 1);
430 $data['feedbackboundaries'][$i] = $boundary;
435 // Check there is nothing in the remaining unused fields.
436 if (!empty($data['feedbackboundaries'])) {
437 for ($i = $numboundaries; $i < count($data['feedbackboundaries']); $i += 1) {
438 if (!empty($data['feedbackboundaries'][$i] ) && trim($data['feedbackboundaries'][$i] ) != '') {
439 $errors["feedbackboundaries[$i]"] = get_string('feedbackerrorjunkinboundary', 'quiz', $i + 1);
443 for ($i = $numboundaries + 1; $i < count($data['feedbacktext']); $i += 1) {
444 if (!empty($data['feedbacktext'][$i]['text']) && trim($data['feedbacktext'][$i]['text'] ) != '') {
445 $errors["feedbacktext[$i]"] = get_string('feedbackerrorjunkinfeedback', 'quiz', $i + 1);