Commit | Line | Data |
---|---|---|
83192608 | 1 | <?php |
a23f0aaf | 2 | |
ba643847 TH |
3 | // This file is part of Moodle - http://moodle.org/ |
4 | // | |
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. | |
9 | // | |
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. | |
14 | // | |
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/>. | |
17 | ||
18 | /** | |
19 | * Defines the quiz module ettings form. | |
20 | * | |
21 | * @package mod | |
22 | * @subpackage quiz | |
23 | * @copyright 2006 Jamie Pratt | |
24 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
25 | */ | |
84e628a0 | 26 | |
25302dee | 27 | |
a17b297d | 28 | defined('MOODLE_INTERNAL') || die(); |
bfebaf64 | 29 | |
84e628a0 | 30 | require_once($CFG->dirroot . '/course/moodleform_mod.php'); |
31 | require_once($CFG->dirroot . '/mod/quiz/locallib.php'); | |
32 | ||
25302dee | 33 | |
84e628a0 | 34 | /** |
35 | * Settings form for the quiz module. | |
3268cf99 | 36 | * |
ba643847 TH |
37 | * @copyright 2006 Jamie Pratt |
38 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
84e628a0 | 39 | */ |
f07b9627 | 40 | class mod_quiz_mod_form extends moodleform_mod { |
25302dee TH |
41 | protected $_feedbacks; |
42 | protected static $reviewfields = array(); // Initialised in the constructor. | |
43 | ||
f2557823 | 44 | public function __construct($current, $section, $cm, $course) { |
25302dee TH |
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'), | |
53 | ); | |
f2557823 | 54 | parent::__construct($current, $section, $cm, $course); |
25302dee | 55 | } |
a23f0aaf | 56 | |
c7df5006 | 57 | protected function definition() { |
25ddb7ef | 58 | global $COURSE, $CFG, $DB, $PAGE; |
e2249afe | 59 | $quizconfig = get_config('quiz'); |
25302dee | 60 | $mform = $this->_form; |
a23f0aaf | 61 | |
a23f0aaf | 62 | //------------------------------------------------------------------------------- |
63 | $mform->addElement('header', 'general', get_string('general', 'form')); | |
64 | ||
84e628a0 | 65 | /// Name. |
26de8d35 | 66 | $mform->addElement('text', 'name', get_string('name'), array('size'=>'64')); |
8eb1d25f | 67 | if (!empty($CFG->formatstringstriptags)) { |
68 | $mform->setType('name', PARAM_TEXT); | |
69 | } else { | |
b8ea3041 | 70 | $mform->setType('name', PARAM_CLEANHTML); |
8eb1d25f | 71 | } |
99ebfea2 | 72 | $mform->addRule('name', null, 'required', null, 'client'); |
a23f0aaf | 73 | |
84e628a0 | 74 | /// Introduction. |
0cd1affc | 75 | $this->add_intro_editor(false, get_string('introduction', 'quiz')); |
a23f0aaf | 76 | |
84e628a0 | 77 | /// Open and close dates. |
25302dee TH |
78 | $mform->addElement('date_time_selector', 'timeopen', get_string('quizopen', 'quiz'), |
79 | array('optional' => true, 'step' => 1)); | |
f2557823 | 80 | $mform->addHelpButton('timeopen', 'quizopenclose', 'quiz'); |
25302dee TH |
81 | |
82 | $mform->addElement('date_time_selector', 'timeclose', get_string('quizclose', 'quiz'), | |
83 | array('optional' => true, 'step' => 1)); | |
a23f0aaf | 84 | |
84e628a0 | 85 | /// Time limit. |
86 | $mform->addElement('duration', 'timelimit', get_string('timelimit', 'quiz'), array('optional' => true)); | |
7292c11f | 87 | $mform->addHelpButton('timelimit', 'timelimit', 'quiz'); |
adf8f3f9 | 88 | $mform->setAdvanced('timelimit', $quizconfig->timelimit_adv); |
e2249afe | 89 | $mform->setDefault('timelimit', $quizconfig->timelimit); |
a23f0aaf | 90 | |
84e628a0 | 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; | |
a23f0aaf | 95 | } |
84e628a0 | 96 | $mform->addElement('select', 'attempts', get_string('attemptsallowed', 'quiz'), $attemptoptions); |
adf8f3f9 | 97 | $mform->setAdvanced('attempts', $quizconfig->attempts_adv); |
84e628a0 | 98 | $mform->setDefault('attempts', $quizconfig->attempts); |
a23f0aaf | 99 | |
84e628a0 | 100 | /// Grading method. |
101 | $mform->addElement('select', 'grademethod', get_string('grademethod', 'quiz'), quiz_get_grading_options()); | |
7292c11f | 102 | $mform->addHelpButton('grademethod', 'grademethod', 'quiz'); |
adf8f3f9 | 103 | $mform->setAdvanced('grademethod', $quizconfig->grademethod_adv); |
84e628a0 | 104 | $mform->setDefault('grademethod', $quizconfig->grademethod); |
105 | $mform->disabledIf('grademethod', 'attempts', 'eq', 1); | |
106 | ||
a23f0aaf | 107 | //------------------------------------------------------------------------------- |
84e628a0 | 108 | $mform->addElement('header', 'layouthdr', get_string('layout', 'quiz')); |
109 | ||
110 | /// Shuffle questions. | |
eeab18f0 | 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')); | |
adf8f3f9 | 113 | $mform->setAdvanced('shufflequestions', $quizconfig->shufflequestions_adv); |
84e628a0 | 114 | $mform->setDefault('shufflequestions', $quizconfig->shufflequestions); |
115 | ||
116 | /// Questions per page. | |
eeab18f0 | 117 | $pageoptions = array(); |
118 | $pageoptions[0] = get_string('neverallononepage', 'quiz'); | |
119 | $pageoptions[1] = get_string('everyquestion', 'quiz'); | |
84e628a0 | 120 | for ($i = 2; $i <= QUIZ_MAX_QPP_OPTION; ++$i) { |
eeab18f0 | 121 | $pageoptions[$i] = get_string('everynquestions', 'quiz', $i); |
a23f0aaf | 122 | } |
eeab18f0 | 123 | |
124 | $pagegroup = array(); | |
125 | $pagegroup[] = &$mform->createElement('select', 'questionsperpage', get_string('newpage', 'quiz'), $pageoptions, array('id' => 'id_questionsperpage')); | |
e2249afe | 126 | $mform->setDefault('questionsperpage', $quizconfig->questionsperpage); |
a23f0aaf | 127 | |
eeab18f0 | 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); | |
f44b10ed | 131 | $PAGE->requires->yui2_lib('event'); |
9dec75db | 132 | $PAGE->requires->js('/mod/quiz/edit.js'); |
a13d4fbd | 133 | $PAGE->requires->js_init_call('quiz_settings_init'); |
eeab18f0 | 134 | } |
135 | ||
136 | $mform->addGroup($pagegroup, 'questionsperpagegrp', get_string('newpage', 'quiz'), null, false); | |
7292c11f | 137 | $mform->addHelpButton('questionsperpagegrp', 'newpage', 'quiz'); |
adf8f3f9 | 138 | $mform->setAdvanced('questionsperpagegrp', $quizconfig->questionsperpage_adv); |
eeab18f0 | 139 | |
84e628a0 | 140 | //------------------------------------------------------------------------------- |
141 | $mform->addElement('header', 'interactionhdr', get_string('questionbehaviour', 'quiz')); | |
a23f0aaf | 142 | |
84e628a0 | 143 | /// Shuffle within questions. |
144 | $mform->addElement('selectyesno', 'shuffleanswers', get_string('shufflewithin', 'quiz')); | |
7292c11f | 145 | $mform->addHelpButton('shuffleanswers', 'shufflewithin', 'quiz'); |
adf8f3f9 | 146 | $mform->setAdvanced('shuffleanswers', $quizconfig->shuffleanswers_adv); |
e2249afe | 147 | $mform->setDefault('shuffleanswers', $quizconfig->shuffleanswers); |
a23f0aaf | 148 | |
25302dee | 149 | /// How questions behave (question behaviour). |
f2557823 TH |
150 | if (!empty($this->current->preferredbehaviour)) { |
151 | $currentbehaviour = $this->current->preferredbehaviour; | |
25302dee TH |
152 | } else { |
153 | $currentbehaviour = ''; | |
154 | } | |
155 | $behaviours = question_engine::get_behaviour_options($currentbehaviour); | |
156 | $mform->addElement('select', 'preferredbehaviour', get_string('howquestionsbehave', 'question'), $behaviours); | |
f2557823 | 157 | $mform->addHelpButton('preferredbehaviour', 'howquestionsbehave', 'question'); |
25302dee TH |
158 | $mform->setAdvanced('preferredbehaviour', $CFG->quiz_fix_preferredbehaviour); |
159 | $mform->setDefault('preferredbehaviour', $CFG->quiz_preferredbehaviour); | |
a23f0aaf | 160 | |
84e628a0 | 161 | /// Each attempt builds on last. |
162 | $mform->addElement('selectyesno', 'attemptonlast', get_string('eachattemptbuildsonthelast', 'quiz')); | |
7292c11f | 163 | $mform->addHelpButton('attemptonlast', 'eachattemptbuildsonthelast', 'quiz'); |
adf8f3f9 | 164 | $mform->setAdvanced('attemptonlast', $quizconfig->attemptonlast_adv); |
84e628a0 | 165 | $mform->setDefault('attemptonlast', $quizconfig->attemptonlast); |
166 | $mform->disabledIf('attemptonlast', 'attempts', 'eq', 1); | |
a23f0aaf | 167 | |
168 | //------------------------------------------------------------------------------- | |
e2833e87 | 169 | $mform->addElement('header', 'reviewoptionshdr', get_string('reviewoptionsheading', 'quiz')); |
7292c11f | 170 | $mform->addHelpButton('reviewoptionshdr', 'reviewoptionsheading', 'quiz'); |
a23f0aaf | 171 | |
84e628a0 | 172 | /// Review options. |
14936199 TH |
173 | $this->add_review_options_group($mform, $quizconfig, 'during', mod_quiz_display_options::DURING); |
174 | $this->add_review_options_group($mform, $quizconfig, 'immediately', mod_quiz_display_options::IMMEDIATELY_AFTER); | |
175 | $this->add_review_options_group($mform, $quizconfig, 'open', mod_quiz_display_options::LATER_WHILE_OPEN); | |
176 | $this->add_review_options_group($mform, $quizconfig, 'closed', mod_quiz_display_options::AFTER_CLOSE); | |
25302dee TH |
177 | |
178 | foreach ($behaviours as $behaviour => $notused) { | |
179 | $unusedoptions = question_engine::get_behaviour_unused_display_options($behaviour); | |
180 | foreach ($unusedoptions as $unusedoption) { | |
181 | $mform->disabledIf($unusedoption . 'during', 'preferredbehaviour', | |
182 | 'eq', $behaviour); | |
183 | } | |
184 | } | |
185 | $mform->disabledIf('attemptduring', 'preferredbehaviour', | |
186 | 'neq', 'wontmatch'); | |
187 | $mform->disabledIf('overallfeedbackduring', 'preferredbehaviour', | |
188 | 'neq', 'wontmatch'); | |
a23f0aaf | 189 | |
190 | //------------------------------------------------------------------------------- | |
84e628a0 | 191 | $mform->addElement('header', 'display', get_string('display', 'form')); |
a23f0aaf | 192 | |
84e628a0 | 193 | /// Show user picture. |
194 | $mform->addElement('selectyesno', 'showuserpicture', get_string('showuserpicture', 'quiz')); | |
7292c11f | 195 | $mform->addHelpButton('showuserpicture', 'showuserpicture', 'quiz'); |
adf8f3f9 | 196 | $mform->setAdvanced('showuserpicture', $quizconfig->showuserpicture_adv); |
84e628a0 | 197 | $mform->setDefault('showuserpicture', $quizconfig->showuserpicture); |
198 | ||
199 | /// Overall decimal points. | |
200 | $options = array(); | |
201 | for ($i = 0; $i <= QUIZ_MAX_DECIMAL_OPTION; $i++) { | |
202 | $options[$i] = $i; | |
203 | } | |
204 | $mform->addElement('select', 'decimalpoints', get_string('decimalplaces', 'quiz'), $options); | |
7292c11f | 205 | $mform->addHelpButton('decimalpoints', 'decimalplaces', 'quiz'); |
adf8f3f9 | 206 | $mform->setAdvanced('decimalpoints', $quizconfig->decimalpoints_adv); |
84e628a0 | 207 | $mform->setDefault('decimalpoints', $quizconfig->decimalpoints); |
a23f0aaf | 208 | |
84e628a0 | 209 | /// Question decimal points. |
210 | $options = array(-1 => get_string('sameasoverall', 'quiz')); | |
211 | for ($i = 0; $i <= QUIZ_MAX_Q_DECIMAL_OPTION; $i++) { | |
212 | $options[$i] = $i; | |
213 | } | |
214 | $mform->addElement('select', 'questiondecimalpoints', get_string('decimalplacesquestion', 'quiz'), $options); | |
7292c11f | 215 | $mform->addHelpButton('questiondecimalpoints', 'decimalplacesquestion','quiz'); |
adf8f3f9 | 216 | $mform->setAdvanced('questiondecimalpoints', $quizconfig->questiondecimalpoints_adv); |
84e628a0 | 217 | $mform->setDefault('questiondecimalpoints', $quizconfig->questiondecimalpoints); |
218 | ||
56ed242b SH |
219 | // Show blocks during quiz attempt |
220 | $mform->addElement('selectyesno', 'showblocks', get_string('showblocks', 'quiz')); | |
7292c11f | 221 | $mform->addHelpButton('showblocks', 'showblocks', 'quiz'); |
56ed242b SH |
222 | $mform->setAdvanced('showblocks', $quizconfig->showblocks_adv); |
223 | $mform->setDefault('showblocks', $quizconfig->showblocks); | |
224 | ||
84e628a0 | 225 | //------------------------------------------------------------------------------- |
226 | $mform->addElement('header', 'security', get_string('extraattemptrestrictions', 'quiz')); | |
227 | ||
228 | /// Enforced time delay between quiz attempts. | |
229 | $mform->addElement('passwordunmask', 'quizpassword', get_string('requirepassword', 'quiz')); | |
2ee60b49 | 230 | $mform->setType('quizpassword', PARAM_TEXT); |
7292c11f | 231 | $mform->addHelpButton('quizpassword', 'requirepassword', 'quiz'); |
adf8f3f9 | 232 | $mform->setAdvanced('quizpassword', $quizconfig->password_adv); |
e2249afe | 233 | $mform->setDefault('quizpassword', $quizconfig->password); |
a23f0aaf | 234 | |
84e628a0 | 235 | /// IP address. |
236 | $mform->addElement('text', 'subnet', get_string('requiresubnet', 'quiz')); | |
2ee60b49 | 237 | $mform->setType('subnet', PARAM_TEXT); |
7292c11f | 238 | $mform->addHelpButton('subnet', 'requiresubnet', 'quiz'); |
adf8f3f9 | 239 | $mform->setAdvanced('subnet', $quizconfig->subnet_adv); |
e2249afe | 240 | $mform->setDefault('subnet', $quizconfig->subnet); |
a23f0aaf | 241 | |
84e628a0 | 242 | /// Enforced time delay between quiz attempts. |
243 | $mform->addElement('duration', 'delay1', get_string('delay1st2nd', 'quiz'), array('optional' => true)); | |
7292c11f | 244 | $mform->addHelpButton('delay1', 'delay1st2nd', 'quiz'); |
adf8f3f9 | 245 | $mform->setAdvanced('delay1', $quizconfig->delay1_adv); |
84e628a0 | 246 | $mform->setDefault('delay1', $quizconfig->delay1); |
247 | $mform->disabledIf('delay1', 'attempts', 'eq', 1); | |
248 | ||
249 | $mform->addElement('duration', 'delay2', get_string('delaylater', 'quiz'), array('optional' => true)); | |
7292c11f | 250 | $mform->addHelpButton('delay2', 'delaylater', 'quiz'); |
adf8f3f9 | 251 | $mform->setAdvanced('delay2', $quizconfig->delay2_adv); |
84e628a0 | 252 | $mform->setDefault('delay2', $quizconfig->delay2); |
253 | $mform->disabledIf('delay2', 'attempts', 'eq', 1); | |
254 | $mform->disabledIf('delay2', 'attempts', 'eq', 2); | |
255 | ||
256 | /// 'Secure' window. | |
7d4dfc48 | 257 | $options = array( |
258 | 0 => get_string('none', 'quiz'), | |
259 | 1 => get_string('popupwithjavascriptsupport', 'quiz')); | |
05d5801a | 260 | if (!empty($CFG->enablesafebrowserintegration)) { |
7d4dfc48 | 261 | $options[2] = get_string('requiresafeexambrowser', 'quiz'); |
262 | } | |
263 | $mform->addElement('select', 'popup', get_string('browsersecurity', 'quiz'), $options); | |
7292c11f | 264 | $mform->addHelpButton('popup', 'browsersecurity', 'quiz'); |
adf8f3f9 | 265 | $mform->setAdvanced('popup', $quizconfig->popup_adv); |
84e628a0 | 266 | $mform->setDefault('popup', $quizconfig->popup); |
267 | ||
a23f0aaf | 268 | //------------------------------------------------------------------------------- |
08224df1 | 269 | $mform->addElement('header', 'overallfeedbackhdr', get_string('overallfeedback', 'quiz')); |
7292c11f | 270 | $mform->addHelpButton('overallfeedbackhdr', 'overallfeedback', 'quiz'); |
a23f0aaf | 271 | |
e0b7cfcb | 272 | $mform->addElement('hidden', 'grade', $quizconfig->maximumgrade); |
d18e0fe6 | 273 | $mform->setType('grade', PARAM_RAW); |
f2557823 TH |
274 | |
275 | if (isset($this->current->grade)) { | |
276 | $needwarning = $this->current->grade === 0; | |
e0b7cfcb | 277 | } else { |
f2557823 | 278 | $needwarning = $quizconfig->maximumgrade == 0; |
e0b7cfcb | 279 | } |
280 | if ($needwarning) { | |
281 | $mform->addElement('static', 'nogradewarning', '', get_string('nogradewarning', 'quiz')); | |
282 | } | |
283 | ||
2ee60b49 | 284 | $mform->addElement('static', 'gradeboundarystatic1', get_string('gradeboundary', 'quiz'), '100%'); |
a23f0aaf | 285 | |
e0b7cfcb | 286 | $repeatarray = array(); |
25302dee | 287 | $repeatarray[] = MoodleQuickForm::createElement('editor', 'feedbacktext', get_string('feedback', 'quiz'), null, array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'noclean'=>true, 'context'=>$this->context)); |
e0b7cfcb | 288 | $mform->setType('feedbacktext', PARAM_RAW); |
25302dee | 289 | $repeatarray[] = MoodleQuickForm::createElement('text', 'feedbackboundaries', get_string('gradeboundary', 'quiz'), array('size' => 10)); |
e0b7cfcb | 290 | $mform->setType('feedbackboundaries', PARAM_NOTAGS); |
a23f0aaf | 291 | |
292 | if (!empty($this->_instance)) { | |
c18269c7 | 293 | $this->_feedbacks = $DB->get_records('quiz_feedback', array('quizid'=>$this->_instance), 'mingrade DESC'); |
a23f0aaf | 294 | } else { |
295 | $this->_feedbacks = array(); | |
296 | } | |
297 | $numfeedbacks = max(count($this->_feedbacks) * 1.5, 5); | |
ebff6e2c | 298 | |
84e628a0 | 299 | $nextel=$this->repeat_elements($repeatarray, $numfeedbacks - 1, |
6f3b54c8 | 300 | array(), 'boundary_repeats', 'boundary_add_fields', 3, |
301 | get_string('addmoreoverallfeedbacks', 'quiz'), true); | |
a23f0aaf | 302 | |
e0b7cfcb | 303 | // Put some extra elements in before the button |
25302dee | 304 | $insertEl = MoodleQuickForm::createElement('editor', "feedbacktext[$nextel]", get_string('feedback', 'quiz'), null, array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'noclean'=>true, 'context'=>$this->context)); |
a23f0aaf | 305 | $mform->insertElementBefore($insertEl, 'boundary_add_fields'); |
a23f0aaf | 306 | |
25302dee | 307 | $insertEl = MoodleQuickForm::createElement('static', 'gradeboundarystatic2', get_string('gradeboundary', 'quiz'), '0%'); |
a23f0aaf | 308 | $mform->insertElementBefore($insertEl, 'boundary_add_fields'); |
309 | ||
e0b7cfcb | 310 | // Add the disabledif rules. We cannot do this using the $repeatoptions parameter to |
311 | // repeat_elements becuase we don't want to dissable the first feedbacktext. | |
312 | for ($i = 0; $i < $nextel; $i++) { | |
313 | $mform->disabledIf('feedbackboundaries[' . $i . ']', 'grade', 'eq', 0); | |
314 | $mform->disabledIf('feedbacktext[' . ($i + 1) . ']', 'grade', 'eq', 0); | |
315 | } | |
316 | ||
84e628a0 | 317 | //------------------------------------------------------------------------------- |
42f103be | 318 | $this->standard_coursemodule_elements(); |
84e628a0 | 319 | |
a23f0aaf | 320 | //------------------------------------------------------------------------------- |
321 | // buttons | |
322 | $this->add_action_buttons(); | |
2ee60b49 | 323 | } |
a23f0aaf | 324 | |
14936199 | 325 | protected function add_review_options_group($mform, $quizconfig, $whenname, $when) { |
25302dee TH |
326 | $group = array(); |
327 | foreach (self::$reviewfields as $field => $label) { | |
328 | $group[] = $mform->createElement('checkbox', $field . $whenname, '', $label); | |
329 | } | |
330 | $mform->addGroup($group, $whenname . 'optionsgrp', get_string('review' . $whenname, 'quiz'), null, false); | |
331 | ||
332 | foreach (self::$reviewfields as $field => $notused) { | |
14936199 TH |
333 | $cfgfield = 'review' . $field; |
334 | if ($quizconfig->$cfgfield & $when) { | |
25302dee TH |
335 | $mform->setDefault($field . $whenname, 1); |
336 | } else { | |
337 | $mform->setDefault($field . $whenname, 0); | |
338 | } | |
339 | } | |
340 | ||
341 | $mform->disabledIf('correctness' . $whenname, 'attempt' . $whenname); | |
342 | $mform->disabledIf('specificfeedback' . $whenname, 'attempt' . $whenname); | |
343 | $mform->disabledIf('generalfeedback' . $whenname, 'attempt' . $whenname); | |
344 | $mform->disabledIf('rightanswer' . $whenname, 'attempt' . $whenname); | |
345 | } | |
346 | ||
347 | protected function preprocessing_review_settings(&$toform, $whenname, $when) { | |
348 | foreach (self::$reviewfields as $field => $notused) { | |
349 | $fieldname = 'review' . $field; | |
350 | if (array_key_exists($fieldname, $toform)) { | |
351 | $toform[$field . $whenname] = $toform[$fieldname] & $when; | |
352 | } | |
353 | } | |
354 | } | |
355 | ||
06cd11a9 | 356 | public function data_preprocessing(&$toform) { |
25302dee TH |
357 | if (isset($toform['grade'])) { |
358 | $toform['grade'] = $toform['grade'] + 0; // Convert to a real number, so we don't get 0.0000. | |
e0b7cfcb | 359 | } |
360 | ||
a23f0aaf | 361 | if (count($this->_feedbacks)) { |
362 | $key = 0; | |
363 | foreach ($this->_feedbacks as $feedback){ | |
fe6ce234 | 364 | $draftid = file_get_submitted_draft_itemid('feedbacktext['.$key.']'); |
25302dee | 365 | $toform['feedbacktext['.$key.']']['text'] = file_prepare_draft_area( |
c5c16a2c TH |
366 | $draftid, // draftid |
367 | $this->context->id, // context | |
368 | 'mod_quiz', // component | |
fe6ce234 | 369 | 'feedback', // filarea |
c5c16a2c | 370 | !empty($feedback->id) ? (int) $feedback->id : null, // itemid |
fe6ce234 | 371 | null, |
c5c16a2c | 372 | $feedback->feedbacktext // text |
fe6ce234 | 373 | ); |
25302dee TH |
374 | $toform['feedbacktext['.$key.']']['format'] = $feedback->feedbacktextformat; |
375 | $toform['feedbacktext['.$key.']']['itemid'] = $draftid; | |
fe6ce234 | 376 | |
c5c16a2c TH |
377 | if ($toform['grade'] == 0) { |
378 | // When a quiz is un-graded, there can only be one lot of | |
379 | // feedback. If the quiz previously had a maximum grade and | |
380 | // several lots of feedback, we must now avoid putting text | |
381 | // into input boxes that are disabled, but which the | |
382 | // validation will insist are blank. | |
383 | break; | |
384 | } | |
385 | ||
a23f0aaf | 386 | if ($feedback->mingrade > 0) { |
167f1562 | 387 | $toform['feedbackboundaries['.$key.']'] = (100.0 * $feedback->mingrade / $toform['grade']) . '%'; |
a23f0aaf | 388 | } |
389 | $key++; | |
390 | } | |
a23f0aaf | 391 | } |
298daa21 | 392 | |
25302dee TH |
393 | if (isset($toform['timelimit'])) { |
394 | $toform['timelimitenable'] = $toform['timelimit'] > 0; | |
a23f0aaf | 395 | } |
396 | ||
25302dee TH |
397 | $this->preprocessing_review_settings($toform, 'during', mod_quiz_display_options::DURING); |
398 | $this->preprocessing_review_settings($toform, 'immediately', mod_quiz_display_options::IMMEDIATELY_AFTER); | |
399 | $this->preprocessing_review_settings($toform, 'open', mod_quiz_display_options::LATER_WHILE_OPEN); | |
400 | $this->preprocessing_review_settings($toform, 'closed', mod_quiz_display_options::AFTER_CLOSE); | |
401 | $toform['attemptduring'] = true; | |
402 | $toform['overallfeedbackduring'] = false; | |
403 | ||
404 | // Password field - different in form to stop browsers that remember | |
405 | // passwords from getting confused. | |
406 | if (isset($toform['password'])) { | |
407 | $toform['quizpassword'] = $toform['password']; | |
408 | unset($toform['password']); | |
ab0a8ff2 | 409 | } |
2ee60b49 | 410 | } |
a23f0aaf | 411 | |
c7df5006 | 412 | public function validation($data, $files) { |
a78890d5 | 413 | $errors = parent::validation($data, $files); |
60243313 | 414 | |
a23f0aaf | 415 | // Check open and close times are consistent. |
416 | if ($data['timeopen'] != 0 && $data['timeclose'] != 0 && $data['timeclose'] < $data['timeopen']) { | |
417 | $errors['timeclose'] = get_string('closebeforeopen', 'quiz'); | |
418 | } | |
419 | ||
420 | // Check the boundary value is a number or a percentage, and in range. | |
421 | $i = 0; | |
422 | while (!empty($data['feedbackboundaries'][$i] )) { | |
423 | $boundary = trim($data['feedbackboundaries'][$i]); | |
424 | if (strlen($boundary) > 0 && $boundary[strlen($boundary) - 1] == '%') { | |
425 | $boundary = trim(substr($boundary, 0, -1)); | |
426 | if (is_numeric($boundary)) { | |
427 | $boundary = $boundary * $data['grade'] / 100.0; | |
428 | } else { | |
429 | $errors["feedbackboundaries[$i]"] = get_string('feedbackerrorboundaryformat', 'quiz', $i + 1); | |
430 | } | |
431 | } | |
432 | if (is_numeric($boundary) && $boundary <= 0 || $boundary >= $data['grade'] ) { | |
433 | $errors["feedbackboundaries[$i]"] = get_string('feedbackerrorboundaryoutofrange', 'quiz', $i + 1); | |
434 | } | |
435 | if (is_numeric($boundary) && $i > 0 && $boundary >= $data['feedbackboundaries'][$i - 1]) { | |
436 | $errors["feedbackboundaries[$i]"] = get_string('feedbackerrororder', 'quiz', $i + 1); | |
437 | } | |
438 | $data['feedbackboundaries'][$i] = $boundary; | |
439 | $i += 1; | |
440 | } | |
441 | $numboundaries = $i; | |
442 | ||
443 | // Check there is nothing in the remaining unused fields. | |
e0b7cfcb | 444 | if (!empty($data['feedbackboundaries'])) { |
445 | for ($i = $numboundaries; $i < count($data['feedbackboundaries']); $i += 1) { | |
446 | if (!empty($data['feedbackboundaries'][$i] ) && trim($data['feedbackboundaries'][$i] ) != '') { | |
447 | $errors["feedbackboundaries[$i]"] = get_string('feedbackerrorjunkinboundary', 'quiz', $i + 1); | |
448 | } | |
a23f0aaf | 449 | } |
450 | } | |
e0b7cfcb | 451 | for ($i = $numboundaries + 1; $i < count($data['feedbacktext']); $i += 1) { |
fe6ce234 | 452 | if (!empty($data['feedbacktext'][$i]['text']) && trim($data['feedbacktext'][$i]['text'] ) != '') { |
a23f0aaf | 453 | $errors["feedbacktext[$i]"] = get_string('feedbackerrorjunkinfeedback', 'quiz', $i + 1); |
454 | } | |
455 | } | |
60243313 | 456 | |
32648682 | 457 | return $errors; |
2ee60b49 | 458 | } |
a23f0aaf | 459 | } |