MDL-20636 Try to allow Opaque questions to work in deferred feedback mode, if they...
[moodle.git] / mod / quiz / mod_form.php
CommitLineData
83192608 1<?php
a23f0aaf 2
84e628a0 3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.org //
9// //
10// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
11// //
12// This program is free software; you can redistribute it and/or modify //
13// it under the terms of the GNU General Public License as published by //
14// the Free Software Foundation; either version 2 of the License, or //
15// (at your option) any later version. //
16// //
17// This program is distributed in the hope that it will be useful, //
18// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20// GNU General Public License for more details: //
21// //
22// http://www.gnu.org/copyleft/gpl.html //
23// //
24///////////////////////////////////////////////////////////////////////////
25
bfebaf64
MD
26if (!defined('MOODLE_INTERNAL')) {
27 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
28}
29
84e628a0 30require_once($CFG->dirroot . '/course/moodleform_mod.php');
31require_once($CFG->dirroot . '/mod/quiz/locallib.php');
32
33/**
34 * Settings form for the quiz module.
3268cf99 35 *
84e628a0 36 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
37 * @package quiz
38 */
f07b9627 39class mod_quiz_mod_form extends moodleform_mod {
a23f0aaf 40 var $_feedbacks;
41
2ee60b49 42 function definition() {
a23f0aaf 43
25ddb7ef 44 global $COURSE, $CFG, $DB, $PAGE;
e2249afe 45 $quizconfig = get_config('quiz');
2ee60b49 46 $mform =& $this->_form;
a23f0aaf 47
a23f0aaf 48//-------------------------------------------------------------------------------
49 $mform->addElement('header', 'general', get_string('general', 'form'));
50
84e628a0 51 /// Name.
26de8d35 52 $mform->addElement('text', 'name', get_string('name'), array('size'=>'64'));
8eb1d25f 53 if (!empty($CFG->formatstringstriptags)) {
54 $mform->setType('name', PARAM_TEXT);
55 } else {
b8ea3041 56 $mform->setType('name', PARAM_CLEANHTML);
8eb1d25f 57 }
99ebfea2 58 $mform->addRule('name', null, 'required', null, 'client');
a23f0aaf 59
84e628a0 60 /// Introduction.
0cd1affc 61 $this->add_intro_editor(false, get_string('introduction', 'quiz'));
a23f0aaf 62
84e628a0 63 /// Open and close dates.
64 $mform->addElement('date_time_selector', 'timeopen', get_string('quizopen', 'quiz'), array('optional' => true));
84e628a0 65 $mform->addElement('date_time_selector', 'timeclose', get_string('quizclose', 'quiz'), array('optional' => true));
a23f0aaf 66
84e628a0 67 /// Time limit.
68 $mform->addElement('duration', 'timelimit', get_string('timelimit', 'quiz'), array('optional' => true));
7292c11f 69 $mform->addHelpButton('timelimit', 'timelimit', 'quiz');
adf8f3f9 70 $mform->setAdvanced('timelimit', $quizconfig->timelimit_adv);
e2249afe 71 $mform->setDefault('timelimit', $quizconfig->timelimit);
a23f0aaf 72
84e628a0 73 /// Number of attempts.
74 $attemptoptions = array('0' => get_string('unlimited'));
75 for ($i = 1; $i <= QUIZ_MAX_ATTEMPT_OPTION; $i++) {
76 $attemptoptions[$i] = $i;
a23f0aaf 77 }
84e628a0 78 $mform->addElement('select', 'attempts', get_string('attemptsallowed', 'quiz'), $attemptoptions);
adf8f3f9 79 $mform->setAdvanced('attempts', $quizconfig->attempts_adv);
84e628a0 80 $mform->setDefault('attempts', $quizconfig->attempts);
a23f0aaf 81
84e628a0 82 /// Grading method.
83 $mform->addElement('select', 'grademethod', get_string('grademethod', 'quiz'), quiz_get_grading_options());
7292c11f 84 $mform->addHelpButton('grademethod', 'grademethod', 'quiz');
adf8f3f9 85 $mform->setAdvanced('grademethod', $quizconfig->grademethod_adv);
84e628a0 86 $mform->setDefault('grademethod', $quizconfig->grademethod);
87 $mform->disabledIf('grademethod', 'attempts', 'eq', 1);
88
a23f0aaf 89//-------------------------------------------------------------------------------
84e628a0 90 $mform->addElement('header', 'layouthdr', get_string('layout', 'quiz'));
91
92 /// Shuffle questions.
eeab18f0 93 $shuffleoptions = array(0 => get_string('asshownoneditscreen', 'quiz'), 1 => get_string('shuffledrandomly', 'quiz'));
94 $mform->addElement('select', 'shufflequestions', get_string('questionorder', 'quiz'), $shuffleoptions, array('id' => 'id_shufflequestions'));
adf8f3f9 95 $mform->setAdvanced('shufflequestions', $quizconfig->shufflequestions_adv);
84e628a0 96 $mform->setDefault('shufflequestions', $quizconfig->shufflequestions);
97
98 /// Questions per page.
eeab18f0 99 $pageoptions = array();
100 $pageoptions[0] = get_string('neverallononepage', 'quiz');
101 $pageoptions[1] = get_string('everyquestion', 'quiz');
84e628a0 102 for ($i = 2; $i <= QUIZ_MAX_QPP_OPTION; ++$i) {
eeab18f0 103 $pageoptions[$i] = get_string('everynquestions', 'quiz', $i);
a23f0aaf 104 }
eeab18f0 105
106 $pagegroup = array();
107 $pagegroup[] = &$mform->createElement('select', 'questionsperpage', get_string('newpage', 'quiz'), $pageoptions, array('id' => 'id_questionsperpage'));
e2249afe 108 $mform->setDefault('questionsperpage', $quizconfig->questionsperpage);
a23f0aaf 109
eeab18f0 110 if (!empty($this->_cm)) {
111 $pagegroup[] = &$mform->createElement('checkbox', 'repaginatenow', '', get_string('repaginatenow', 'quiz'), array('id' => 'id_repaginatenow'));
112 $mform->disabledIf('repaginatenow', 'shufflequestions', 'eq', 1);
f44b10ed 113 $PAGE->requires->yui2_lib('event');
9dec75db 114 $PAGE->requires->js('/mod/quiz/edit.js');
eeab18f0 115 }
116
117 $mform->addGroup($pagegroup, 'questionsperpagegrp', get_string('newpage', 'quiz'), null, false);
7292c11f 118 $mform->addHelpButton('questionsperpagegrp', 'newpage', 'quiz');
adf8f3f9 119 $mform->setAdvanced('questionsperpagegrp', $quizconfig->questionsperpage_adv);
eeab18f0 120
84e628a0 121//-------------------------------------------------------------------------------
122 $mform->addElement('header', 'interactionhdr', get_string('questionbehaviour', 'quiz'));
a23f0aaf 123
84e628a0 124 /// Shuffle within questions.
125 $mform->addElement('selectyesno', 'shuffleanswers', get_string('shufflewithin', 'quiz'));
7292c11f 126 $mform->addHelpButton('shuffleanswers', 'shufflewithin', 'quiz');
adf8f3f9 127 $mform->setAdvanced('shuffleanswers', $quizconfig->shuffleanswers_adv);
e2249afe 128 $mform->setDefault('shuffleanswers', $quizconfig->shuffleanswers);
a23f0aaf 129
84e628a0 130 /// Adaptive mode.
131 $mform->addElement('selectyesno', 'adaptive', get_string('adaptive', 'quiz'));
7292c11f 132 $mform->addHelpButton('adaptive', 'adaptive', 'quiz');
adf8f3f9 133 $mform->setAdvanced('adaptive', $quizconfig->optionflags_adv);
e2249afe 134 $mform->setDefault('adaptive', $quizconfig->optionflags & QUESTION_ADAPTIVE);
a23f0aaf 135
84e628a0 136 /// Apply penalties.
137 $mform->addElement('selectyesno', 'penaltyscheme', get_string('penaltyscheme', 'quiz'));
7292c11f 138 $mform->addHelpButton('penaltyscheme', 'penaltyscheme', 'quiz');
adf8f3f9 139 $mform->setAdvanced('penaltyscheme', $quizconfig->penaltyscheme_adv);
e2249afe 140 $mform->setDefault('penaltyscheme', $quizconfig->penaltyscheme);
84e628a0 141 $mform->disabledIf('penaltyscheme', 'adaptive', 'neq', 1);
a23f0aaf 142
84e628a0 143 /// Each attempt builds on last.
144 $mform->addElement('selectyesno', 'attemptonlast', get_string('eachattemptbuildsonthelast', 'quiz'));
7292c11f 145 $mform->addHelpButton('attemptonlast', 'eachattemptbuildsonthelast', 'quiz');
adf8f3f9 146 $mform->setAdvanced('attemptonlast', $quizconfig->attemptonlast_adv);
84e628a0 147 $mform->setDefault('attemptonlast', $quizconfig->attemptonlast);
148 $mform->disabledIf('attemptonlast', 'attempts', 'eq', 1);
a23f0aaf 149
150//-------------------------------------------------------------------------------
e2833e87 151 $mform->addElement('header', 'reviewoptionshdr', get_string('reviewoptionsheading', 'quiz'));
7292c11f 152 $mform->addHelpButton('reviewoptionshdr', 'reviewoptionsheading', 'quiz');
adf8f3f9 153 $mform->setAdvanced('reviewoptionshdr', $quizconfig->review_adv);
a23f0aaf 154
84e628a0 155 /// Review options.
2ee60b49 156 $immediatelyoptionsgrp=array();
a23f0aaf 157 $immediatelyoptionsgrp[] = &$mform->createElement('checkbox', 'responsesimmediately', '', get_string('responses', 'quiz'));
a23f0aaf 158 $immediatelyoptionsgrp[] = &$mform->createElement('checkbox', 'answersimmediately', '', get_string('answers', 'quiz'));
00719c02 159 $immediatelyoptionsgrp[] = &$mform->createElement('checkbox', 'feedbackimmediately', '', get_string('feedback', 'quiz'));
a23f0aaf 160 $immediatelyoptionsgrp[] = &$mform->createElement('checkbox', 'generalfeedbackimmediately', '', get_string('generalfeedback', 'quiz'));
00719c02 161 $immediatelyoptionsgrp[] = &$mform->createElement('checkbox', 'scoreimmediately', '', get_string('scores', 'quiz'));
162 $immediatelyoptionsgrp[] = &$mform->createElement('checkbox', 'overallfeedbackimmediately', '', get_string('overallfeedback', 'quiz'));
84e628a0 163 $mform->addGroup($immediatelyoptionsgrp, 'immediatelyoptionsgrp', get_string('reviewimmediately', 'quiz'), null, false);
e2249afe 164 $mform->setDefault('responsesimmediately', $quizconfig->review & QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_IMMEDIATELY);
165 $mform->setDefault('answersimmediately', $quizconfig->review & QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_IMMEDIATELY);
166 $mform->setDefault('feedbackimmediately', $quizconfig->review & QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_IMMEDIATELY);
167 $mform->setDefault('generalfeedbackimmediately', $quizconfig->review & QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_IMMEDIATELY);
168 $mform->setDefault('scoreimmediately', $quizconfig->review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_IMMEDIATELY);
169 $mform->setDefault('overallfeedbackimmediately', $quizconfig->review & QUIZ_REVIEW_OVERALLFEEDBACK & QUIZ_REVIEW_IMMEDIATELY);
a23f0aaf 170
2ee60b49 171 $openoptionsgrp=array();
a23f0aaf 172 $openoptionsgrp[] = &$mform->createElement('checkbox', 'responsesopen', '', get_string('responses', 'quiz'));
a23f0aaf 173 $openoptionsgrp[] = &$mform->createElement('checkbox', 'answersopen', '', get_string('answers', 'quiz'));
00719c02 174 $openoptionsgrp[] = &$mform->createElement('checkbox', 'feedbackopen', '', get_string('feedback', 'quiz'));
a23f0aaf 175 $openoptionsgrp[] = &$mform->createElement('checkbox', 'generalfeedbackopen', '', get_string('generalfeedback', 'quiz'));
00719c02 176 $openoptionsgrp[] = &$mform->createElement('checkbox', 'scoreopen', '', get_string('scores', 'quiz'));
177 $openoptionsgrp[] = &$mform->createElement('checkbox', 'overallfeedbackopen', '', get_string('overallfeedback', 'quiz'));
84e628a0 178 $mform->addGroup($openoptionsgrp, 'openoptionsgrp', get_string('reviewopen', 'quiz'), array(' '), false);
e2249afe 179 $mform->setDefault('responsesopen', $quizconfig->review & QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_OPEN);
180 $mform->setDefault('answersopen', $quizconfig->review & QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_OPEN);
181 $mform->setDefault('feedbackopen', $quizconfig->review & QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_OPEN);
182 $mform->setDefault('generalfeedbackopen', $quizconfig->review & QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_OPEN);
183 $mform->setDefault('scoreopen', $quizconfig->review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_OPEN);
184 $mform->setDefault('overallfeedbackopen', $quizconfig->review & QUIZ_REVIEW_OVERALLFEEDBACK & QUIZ_REVIEW_OPEN);
a23f0aaf 185
2ee60b49 186 $closedoptionsgrp=array();
a23f0aaf 187 $closedoptionsgrp[] = &$mform->createElement('checkbox', 'responsesclosed', '', get_string('responses', 'quiz'));
a23f0aaf 188 $closedoptionsgrp[] = &$mform->createElement('checkbox', 'answersclosed', '', get_string('answers', 'quiz'));
00719c02 189 $closedoptionsgrp[] = &$mform->createElement('checkbox', 'feedbackclosed', '', get_string('feedback', 'quiz'));
a23f0aaf 190 $closedoptionsgrp[] = &$mform->createElement('checkbox', 'generalfeedbackclosed', '', get_string('generalfeedback', 'quiz'));
00719c02 191 $closedoptionsgrp[] = &$mform->createElement('checkbox', 'scoreclosed', '', get_string('scores', 'quiz'));
192 $closedoptionsgrp[] = &$mform->createElement('checkbox', 'overallfeedbackclosed', '', get_string('overallfeedback', 'quiz'));
84e628a0 193 $mform->addGroup($closedoptionsgrp, 'closedoptionsgrp', get_string('reviewclosed', 'quiz'), array(' '), false);
e2249afe 194 $mform->setDefault('responsesclosed', $quizconfig->review & QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_CLOSED);
195 $mform->setDefault('answersclosed', $quizconfig->review & QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_CLOSED);
196 $mform->setDefault('feedbackclosed', $quizconfig->review & QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_CLOSED);
197 $mform->setDefault('generalfeedbackclosed', $quizconfig->review & QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_CLOSED);
198 $mform->setDefault('scoreclosed', $quizconfig->review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_CLOSED);
199 $mform->setDefault('overallfeedbackclosed', $quizconfig->review & QUIZ_REVIEW_OVERALLFEEDBACK & QUIZ_REVIEW_CLOSED);
84e628a0 200 $mform->disabledIf('closedoptionsgrp', 'timeclose[enabled]');
a23f0aaf 201
202//-------------------------------------------------------------------------------
84e628a0 203 $mform->addElement('header', 'display', get_string('display', 'form'));
a23f0aaf 204
84e628a0 205 /// Show user picture.
206 $mform->addElement('selectyesno', 'showuserpicture', get_string('showuserpicture', 'quiz'));
7292c11f 207 $mform->addHelpButton('showuserpicture', 'showuserpicture', 'quiz');
adf8f3f9 208 $mform->setAdvanced('showuserpicture', $quizconfig->showuserpicture_adv);
84e628a0 209 $mform->setDefault('showuserpicture', $quizconfig->showuserpicture);
210
211 /// Overall decimal points.
212 $options = array();
213 for ($i = 0; $i <= QUIZ_MAX_DECIMAL_OPTION; $i++) {
214 $options[$i] = $i;
215 }
216 $mform->addElement('select', 'decimalpoints', get_string('decimalplaces', 'quiz'), $options);
7292c11f 217 $mform->addHelpButton('decimalpoints', 'decimalplaces', 'quiz');
adf8f3f9 218 $mform->setAdvanced('decimalpoints', $quizconfig->decimalpoints_adv);
84e628a0 219 $mform->setDefault('decimalpoints', $quizconfig->decimalpoints);
a23f0aaf 220
84e628a0 221 /// Question decimal points.
222 $options = array(-1 => get_string('sameasoverall', 'quiz'));
223 for ($i = 0; $i <= QUIZ_MAX_Q_DECIMAL_OPTION; $i++) {
224 $options[$i] = $i;
225 }
226 $mform->addElement('select', 'questiondecimalpoints', get_string('decimalplacesquestion', 'quiz'), $options);
7292c11f 227 $mform->addHelpButton('questiondecimalpoints', 'decimalplacesquestion','quiz');
adf8f3f9 228 $mform->setAdvanced('questiondecimalpoints', $quizconfig->questiondecimalpoints_adv);
84e628a0 229 $mform->setDefault('questiondecimalpoints', $quizconfig->questiondecimalpoints);
230
56ed242b
SH
231 // Show blocks during quiz attempt
232 $mform->addElement('selectyesno', 'showblocks', get_string('showblocks', 'quiz'));
7292c11f 233 $mform->addHelpButton('showblocks', 'showblocks', 'quiz');
56ed242b
SH
234 $mform->setAdvanced('showblocks', $quizconfig->showblocks_adv);
235 $mform->setDefault('showblocks', $quizconfig->showblocks);
236
84e628a0 237//-------------------------------------------------------------------------------
238 $mform->addElement('header', 'security', get_string('extraattemptrestrictions', 'quiz'));
239
240 /// Enforced time delay between quiz attempts.
241 $mform->addElement('passwordunmask', 'quizpassword', get_string('requirepassword', 'quiz'));
2ee60b49 242 $mform->setType('quizpassword', PARAM_TEXT);
7292c11f 243 $mform->addHelpButton('quizpassword', 'requirepassword', 'quiz');
adf8f3f9 244 $mform->setAdvanced('quizpassword', $quizconfig->password_adv);
e2249afe 245 $mform->setDefault('quizpassword', $quizconfig->password);
a23f0aaf 246
84e628a0 247 /// IP address.
248 $mform->addElement('text', 'subnet', get_string('requiresubnet', 'quiz'));
2ee60b49 249 $mform->setType('subnet', PARAM_TEXT);
7292c11f 250 $mform->addHelpButton('subnet', 'requiresubnet', 'quiz');
adf8f3f9 251 $mform->setAdvanced('subnet', $quizconfig->subnet_adv);
e2249afe 252 $mform->setDefault('subnet', $quizconfig->subnet);
a23f0aaf 253
84e628a0 254 /// Enforced time delay between quiz attempts.
255 $mform->addElement('duration', 'delay1', get_string('delay1st2nd', 'quiz'), array('optional' => true));
7292c11f 256 $mform->addHelpButton('delay1', 'delay1st2nd', 'quiz');
adf8f3f9 257 $mform->setAdvanced('delay1', $quizconfig->delay1_adv);
84e628a0 258 $mform->setDefault('delay1', $quizconfig->delay1);
259 $mform->disabledIf('delay1', 'attempts', 'eq', 1);
260
261 $mform->addElement('duration', 'delay2', get_string('delaylater', 'quiz'), array('optional' => true));
7292c11f 262 $mform->addHelpButton('delay2', 'delaylater', 'quiz');
adf8f3f9 263 $mform->setAdvanced('delay2', $quizconfig->delay2_adv);
84e628a0 264 $mform->setDefault('delay2', $quizconfig->delay2);
265 $mform->disabledIf('delay2', 'attempts', 'eq', 1);
266 $mform->disabledIf('delay2', 'attempts', 'eq', 2);
267
268 /// 'Secure' window.
7d4dfc48 269 $options = array(
270 0 => get_string('none', 'quiz'),
271 1 => get_string('popupwithjavascriptsupport', 'quiz'));
05d5801a 272 if (!empty($CFG->enablesafebrowserintegration)) {
7d4dfc48 273 $options[2] = get_string('requiresafeexambrowser', 'quiz');
274 }
275 $mform->addElement('select', 'popup', get_string('browsersecurity', 'quiz'), $options);
7292c11f 276 $mform->addHelpButton('popup', 'browsersecurity', 'quiz');
adf8f3f9 277 $mform->setAdvanced('popup', $quizconfig->popup_adv);
84e628a0 278 $mform->setDefault('popup', $quizconfig->popup);
279
a23f0aaf 280//-------------------------------------------------------------------------------
08224df1 281 $mform->addElement('header', 'overallfeedbackhdr', get_string('overallfeedback', 'quiz'));
7292c11f 282 $mform->addHelpButton('overallfeedbackhdr', 'overallfeedback', 'quiz');
a23f0aaf 283
e0b7cfcb 284 $mform->addElement('hidden', 'grade', $quizconfig->maximumgrade);
d18e0fe6 285 $mform->setType('grade', PARAM_RAW);
e0b7cfcb 286 if (empty($this->_cm)) {
287 $needwarning = $quizconfig->maximumgrade == 0;
288 } else {
2f5f73d0 289 $quizgrade = $DB->get_field('quiz', 'grade', array('id' => $this->_instance));
290 $needwarning = $quizgrade == 0;
e0b7cfcb 291 }
292 if ($needwarning) {
293 $mform->addElement('static', 'nogradewarning', '', get_string('nogradewarning', 'quiz'));
294 }
295
2ee60b49 296 $mform->addElement('static', 'gradeboundarystatic1', get_string('gradeboundary', 'quiz'), '100%');
a23f0aaf 297
e0b7cfcb 298 $repeatarray = array();
fe6ce234 299 $repeatarray[] = &MoodleQuickForm::createElement('editor', 'feedbacktext', get_string('feedback', 'quiz'), null, array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'noclean'=>true, 'context'=>$this->context));
e0b7cfcb 300 $mform->setType('feedbacktext', PARAM_RAW);
a462602a 301 $repeatarray[] = &MoodleQuickForm::createElement('text', 'feedbackboundaries', get_string('gradeboundary', 'quiz'), array('size' => 10));
e0b7cfcb 302 $mform->setType('feedbackboundaries', PARAM_NOTAGS);
a23f0aaf 303
304 if (!empty($this->_instance)) {
c18269c7 305 $this->_feedbacks = $DB->get_records('quiz_feedback', array('quizid'=>$this->_instance), 'mingrade DESC');
a23f0aaf 306 } else {
307 $this->_feedbacks = array();
308 }
309 $numfeedbacks = max(count($this->_feedbacks) * 1.5, 5);
ebff6e2c 310
84e628a0 311 $nextel=$this->repeat_elements($repeatarray, $numfeedbacks - 1,
6f3b54c8 312 array(), 'boundary_repeats', 'boundary_add_fields', 3,
313 get_string('addmoreoverallfeedbacks', 'quiz'), true);
a23f0aaf 314
e0b7cfcb 315 // Put some extra elements in before the button
fe6ce234 316 $insertEl = &MoodleQuickForm::createElement('editor', "feedbacktext[$nextel]", get_string('feedback', 'quiz'), null, array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'noclean'=>true, 'context'=>$this->context));
a23f0aaf 317 $mform->insertElementBefore($insertEl, 'boundary_add_fields');
a23f0aaf 318
319 $insertEl = &MoodleQuickForm::createElement('static', 'gradeboundarystatic2', get_string('gradeboundary', 'quiz'), '0%');
320 $mform->insertElementBefore($insertEl, 'boundary_add_fields');
321
e0b7cfcb 322 // Add the disabledif rules. We cannot do this using the $repeatoptions parameter to
323 // repeat_elements becuase we don't want to dissable the first feedbacktext.
324 for ($i = 0; $i < $nextel; $i++) {
325 $mform->disabledIf('feedbackboundaries[' . $i . ']', 'grade', 'eq', 0);
326 $mform->disabledIf('feedbacktext[' . ($i + 1) . ']', 'grade', 'eq', 0);
327 }
328
84e628a0 329//-------------------------------------------------------------------------------
42f103be 330 $this->standard_coursemodule_elements();
84e628a0 331
a23f0aaf 332//-------------------------------------------------------------------------------
333 // buttons
334 $this->add_action_buttons();
2ee60b49 335 }
a23f0aaf 336
2ee60b49 337 function data_preprocessing(&$default_values){
e0b7cfcb 338 if (isset($default_values['grade'])) {
339 $default_values['grade'] = $default_values['grade'] + 0; // Convert to a real number, so we don't get 0.0000.
340 }
341
a23f0aaf 342 if (count($this->_feedbacks)) {
343 $key = 0;
344 foreach ($this->_feedbacks as $feedback){
fe6ce234
DC
345 $draftid = file_get_submitted_draft_itemid('feedbacktext['.$key.']');
346 $default_values['feedbacktext['.$key.']']['text'] = file_prepare_draft_area(
347 $draftid, // draftid
348 $this->context->id, // context
349 'mod_quiz', // component
350 'feedback', // filarea
351 !empty($feedback->id)?(int)$feedback->id:null, // itemid
352 null,
353 $feedback->feedbacktext // text
354 );
355 $default_values['feedbacktext['.$key.']']['format'] = $feedback->feedbacktextformat;
356 $default_values['feedbacktext['.$key.']']['itemid'] = $draftid;
357
a23f0aaf 358 if ($feedback->mingrade > 0) {
359 $default_values['feedbackboundaries['.$key.']'] = (100.0 * $feedback->mingrade / $default_values['grade']) . '%';
360 }
361 $key++;
362 }
a23f0aaf 363 }
298daa21 364
365 if (isset($default_values['timelimit'])) {
366 $default_values['timelimitenable'] = $default_values['timelimit'] > 0;
a23f0aaf 367 }
368
369 if (isset($default_values['review'])){
370 $review = (int)$default_values['review'];
371 unset($default_values['review']);
372
373 $default_values['responsesimmediately'] = $review & QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_IMMEDIATELY;
a23f0aaf 374 $default_values['answersimmediately'] = $review & QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_IMMEDIATELY;
00719c02 375 $default_values['feedbackimmediately'] = $review & QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_IMMEDIATELY;
a23f0aaf 376 $default_values['generalfeedbackimmediately'] = $review & QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_IMMEDIATELY;
00719c02 377 $default_values['scoreimmediately'] = $review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_IMMEDIATELY;
378 $default_values['overallfeedbackimmediately'] = $review & QUIZ_REVIEW_OVERALLFEEDBACK & QUIZ_REVIEW_IMMEDIATELY;
a23f0aaf 379
380 $default_values['responsesopen'] = $review & QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_OPEN;
a23f0aaf 381 $default_values['answersopen'] = $review & QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_OPEN;
00719c02 382 $default_values['feedbackopen'] = $review & QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_OPEN;
a23f0aaf 383 $default_values['generalfeedbackopen'] = $review & QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_OPEN;
00719c02 384 $default_values['scoreopen'] = $review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_OPEN;
385 $default_values['overallfeedbackopen'] = $review & QUIZ_REVIEW_OVERALLFEEDBACK & QUIZ_REVIEW_OPEN;
a23f0aaf 386
387 $default_values['responsesclosed'] = $review & QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_CLOSED;
a23f0aaf 388 $default_values['answersclosed'] = $review & QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_CLOSED;
00719c02 389 $default_values['feedbackclosed'] = $review & QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_CLOSED;
a23f0aaf 390 $default_values['generalfeedbackclosed'] = $review & QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_CLOSED;
00719c02 391 $default_values['scoreclosed'] = $review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_CLOSED;
392 $default_values['overallfeedbackclosed'] = $review & QUIZ_REVIEW_OVERALLFEEDBACK & QUIZ_REVIEW_CLOSED;
a23f0aaf 393 }
394
395 if (isset($default_values['optionflags'])){
396 $default_values['adaptive'] = $default_values['optionflags'] & QUESTION_ADAPTIVE;
397 unset($default_values['optionflags']);
398 }
ab0a8ff2 399
400 // Password field - different in form to stop browsers that remember passwords
401 // getting confused.
2ee60b49 402 if (isset($default_values['password'])) {
ab0a8ff2 403 $default_values['quizpassword'] = $default_values['password'];
404 unset($default_values['password']);
405 }
2ee60b49 406 }
a23f0aaf 407
a78890d5 408 function validation($data, $files) {
409 $errors = parent::validation($data, $files);
60243313 410
a23f0aaf 411 // Check open and close times are consistent.
412 if ($data['timeopen'] != 0 && $data['timeclose'] != 0 && $data['timeclose'] < $data['timeopen']) {
413 $errors['timeclose'] = get_string('closebeforeopen', 'quiz');
414 }
415
416 // Check the boundary value is a number or a percentage, and in range.
417 $i = 0;
418 while (!empty($data['feedbackboundaries'][$i] )) {
419 $boundary = trim($data['feedbackboundaries'][$i]);
420 if (strlen($boundary) > 0 && $boundary[strlen($boundary) - 1] == '%') {
421 $boundary = trim(substr($boundary, 0, -1));
422 if (is_numeric($boundary)) {
423 $boundary = $boundary * $data['grade'] / 100.0;
424 } else {
425 $errors["feedbackboundaries[$i]"] = get_string('feedbackerrorboundaryformat', 'quiz', $i + 1);
426 }
427 }
428 if (is_numeric($boundary) && $boundary <= 0 || $boundary >= $data['grade'] ) {
429 $errors["feedbackboundaries[$i]"] = get_string('feedbackerrorboundaryoutofrange', 'quiz', $i + 1);
430 }
431 if (is_numeric($boundary) && $i > 0 && $boundary >= $data['feedbackboundaries'][$i - 1]) {
432 $errors["feedbackboundaries[$i]"] = get_string('feedbackerrororder', 'quiz', $i + 1);
433 }
434 $data['feedbackboundaries'][$i] = $boundary;
435 $i += 1;
436 }
437 $numboundaries = $i;
438
439 // Check there is nothing in the remaining unused fields.
e0b7cfcb 440 if (!empty($data['feedbackboundaries'])) {
441 for ($i = $numboundaries; $i < count($data['feedbackboundaries']); $i += 1) {
442 if (!empty($data['feedbackboundaries'][$i] ) && trim($data['feedbackboundaries'][$i] ) != '') {
443 $errors["feedbackboundaries[$i]"] = get_string('feedbackerrorjunkinboundary', 'quiz', $i + 1);
444 }
a23f0aaf 445 }
446 }
e0b7cfcb 447 for ($i = $numboundaries + 1; $i < count($data['feedbacktext']); $i += 1) {
fe6ce234 448 if (!empty($data['feedbacktext'][$i]['text']) && trim($data['feedbacktext'][$i]['text'] ) != '') {
a23f0aaf 449 $errors["feedbacktext[$i]"] = get_string('feedbackerrorjunkinfeedback', 'quiz', $i + 1);
450 }
451 }
60243313 452
32648682 453 return $errors;
2ee60b49 454 }
a23f0aaf 455
456}
83192608 457