Merge branch 'MDL-68749-310-2' of git://github.com/mickhawkins/moodle into MOODLE_310...
[moodle.git] / question / type / numerical / edit_numerical_form.php
CommitLineData
aeb15530 1<?php
fe6ce234
DC
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.
13//
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/>.
16
6e9b6ba2 17/**
18 * Defines the editing form for the numerical question type.
19 *
b04a4319
TH
20 * @package qtype
21 * @subpackage numerical
22 * @copyright 2007 Jamie Pratt me@jamiep.org
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
6e9b6ba2 24 */
25
a17b297d
TH
26
27defined('MOODLE_INTERNAL') || die();
28
90489407 29require_once($CFG->dirroot . '/question/type/edit_question_form.php');
18f9b2d2
TH
30require_once($CFG->dirroot . '/question/type/numerical/questiontype.php');
31
a17b297d 32
6e9b6ba2 33/**
34 * numerical editing form definition.
b04a4319
TH
35 *
36 * @copyright 2007 Jamie Pratt me@jamiep.org
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
6e9b6ba2 38 */
1fa39364 39class qtype_numerical_edit_form extends question_edit_form {
c3c65f41
TH
40 /** @var int we always show at least this many sets of unit fields. */
41 const UNITS_MIN_REPEATS = 1;
42 const UNITS_TO_ADD = 2;
43
90489407 44 protected $ap = null;
1fa39364
TH
45
46 protected function definition_inner($mform) {
1fa39364 47 $this->add_per_answer_fields($mform, get_string('answerno', 'qtype_numerical', '{no}'),
92111e8d 48 question_bank::fraction_options());
1fa39364 49
fdd015b7
TH
50 $this->add_unit_options($mform);
51 $this->add_unit_fields($mform);
1fa39364
TH
52 $this->add_interactive_settings();
53 }
2aef1fe5 54
fdd015b7
TH
55 protected function get_per_answer_fields($mform, $label, $gradeoptions,
56 &$repeatedoptions, &$answersoption) {
57 $repeated = parent::get_per_answer_fields($mform, $label, $gradeoptions,
58 $repeatedoptions, $answersoption);
2aef1fe5 59
688c8035 60 $tolerance = $mform->createElement('float', 'tolerance',
c7218aef 61 get_string('answererror', 'qtype_numerical'), array('size' => 15));
90489407 62 $repeatedoptions['tolerance']['default'] = 0;
c7218aef
CC
63 $elements = $repeated[0]->getElements();
64 $elements[0]->setSize(15);
65 array_splice($elements, 1, 0, array($tolerance));
66 $repeated[0]->setElements($elements);
2aef1fe5 67
68 return $repeated;
69 }
70
55748620
TH
71 protected function get_more_choices_string() {
72 return get_string('addmoreanswerblanks', 'qtype_numerical');
73 }
74
f77f47ec 75 /**
1fa39364
TH
76 * Add the unit handling options to the form.
77 * @param object $mform the form being built.
f77f47ec 78 */
fdd015b7 79 protected function add_unit_options($mform) {
6e9b6ba2 80
fdd015b7
TH
81 $mform->addElement('header', 'unithandling',
82 get_string('unithandling', 'qtype_numerical'));
1fa39364
TH
83
84 $unitoptions = array(
85 qtype_numerical::UNITNONE => get_string('onlynumerical', 'qtype_numerical'),
1fa39364
TH
86 qtype_numerical::UNITOPTIONAL => get_string('manynumerical', 'qtype_numerical'),
87 qtype_numerical::UNITGRADED => get_string('unitgraded', 'qtype_numerical'),
88 );
fdd015b7
TH
89 $mform->addElement('select', 'unitrole',
90 get_string('unithandling', 'qtype_numerical'), $unitoptions);
1fa39364
TH
91
92 $penaltygrp = array();
688c8035 93 $penaltygrp[] = $mform->createElement('float', 'unitpenalty',
fdd015b7 94 get_string('unitpenalty', 'qtype_numerical'), array('size' => 6));
1fa39364
TH
95 $mform->setDefault('unitpenalty', 0.1000000);
96
97 $unitgradingtypes = array(
fdd015b7
TH
98 qtype_numerical::UNITGRADEDOUTOFMARK =>
99 get_string('decfractionofresponsegrade', 'qtype_numerical'),
100 qtype_numerical::UNITGRADEDOUTOFMAX =>
101 get_string('decfractionofquestiongrade', 'qtype_numerical'),
1fa39364 102 );
fdd015b7 103 $penaltygrp[] = $mform->createElement('select', 'unitgradingtypes', '', $unitgradingtypes);
1fa39364
TH
104 $mform->setDefault('unitgradingtypes', 1);
105
fdd015b7
TH
106 $mform->addGroup($penaltygrp, 'penaltygrp',
107 get_string('unitpenalty', 'qtype_numerical'), ' ', false);
1fa39364
TH
108 $mform->addHelpButton('penaltygrp', 'unitpenalty', 'qtype_numerical');
109
110 $unitinputoptions = array(
111 qtype_numerical::UNITINPUT => get_string('editableunittext', 'qtype_numerical'),
8566369f
TH
112 qtype_numerical::UNITRADIO => get_string('unitchoice', 'qtype_numerical'),
113 qtype_numerical::UNITSELECT => get_string('unitselect', 'qtype_numerical'),
1fa39364 114 );
fdd015b7
TH
115 $mform->addElement('select', 'multichoicedisplay',
116 get_string('studentunitanswer', 'qtype_numerical'), $unitinputoptions);
1fa39364 117
544de1c0 118 $unitsleftoptions = array(
1fa39364
TH
119 0 => get_string('rightexample', 'qtype_numerical'),
120 1 => get_string('leftexample', 'qtype_numerical')
121 );
fdd015b7 122 $mform->addElement('select', 'unitsleft',
544de1c0 123 get_string('unitposition', 'qtype_numerical'), $unitsleftoptions);
1fa39364
TH
124 $mform->setDefault('unitsleft', 0);
125
1fa39364 126 $mform->disabledIf('penaltygrp', 'unitrole', 'eq', qtype_numerical::UNITNONE);
1fa39364
TH
127 $mform->disabledIf('penaltygrp', 'unitrole', 'eq', qtype_numerical::UNITOPTIONAL);
128
129 $mform->disabledIf('unitsleft', 'unitrole', 'eq', qtype_numerical::UNITNONE);
130
131 $mform->disabledIf('multichoicedisplay', 'unitrole', 'eq', qtype_numerical::UNITNONE);
1fa39364
TH
132 $mform->disabledIf('multichoicedisplay', 'unitrole', 'eq', qtype_numerical::UNITOPTIONAL);
133 }
134
135 /**
136 * Add the input areas for each unit.
137 * @param object $mform the form being built.
138 */
fdd015b7 139 protected function add_unit_fields($mform) {
c4b2600d
CC
140 $mform->addElement('header', 'unithdr',
141 get_string('units', 'qtype_numerical'), '');
142
55748620
TH
143 $unitfields = array($mform->createElement('group', 'units',
144 get_string('unitx', 'qtype_numerical'), $this->unit_group($mform), null, false));
1fa39364 145
7d1904de
FM
146 $repeatedoptions['unit']['disabledif'] = array('unitrole', 'eq', qtype_numerical::UNITNONE);
147 $repeatedoptions['unit']['type'] = PARAM_NOTAGS;
148 $repeatedoptions['multiplier']['disabledif'] = array('unitrole', 'eq', qtype_numerical::UNITNONE);
7d1904de 149
c4b2600d 150 $mform->disabledIf('addunits', 'unitrole', 'eq', qtype_numerical::UNITNONE);
1fa39364
TH
151
152 if (isset($this->question->options->units)) {
c3c65f41 153 $repeatsatstart = max(count($this->question->options->units), self::UNITS_MIN_REPEATS);
1fa39364 154 } else {
c3c65f41 155 $repeatsatstart = self::UNITS_MIN_REPEATS;
1fa39364 156 }
c4b2600d 157
55748620 158 $this->repeat_elements($unitfields, $repeatsatstart, $repeatedoptions, 'nounits',
c3c65f41 159 'addunits', self::UNITS_TO_ADD, get_string('addmoreunitblanks', 'qtype_numerical', '{no}'), true);
c4b2600d
CC
160
161 // The following strange-looking if statement is to do with when the
162 // form is used to move questions between categories. See MDL-15159.
163 if ($mform->elementExists('units[0]')) {
164 $firstunit = $mform->getElement('units[0]');
165 $elements = $firstunit->getElements();
166 foreach ($elements as $element) {
167 if ($element->getName() != 'multiplier[0]') {
168 continue;
169 }
170 $element->freeze();
171 $element->setValue('1.0');
172 $element->setPersistantFreeze(true);
173 }
174 $mform->addHelpButton('units[0]', 'numericalmultiplier', 'qtype_numerical');
1fa39364 175 }
6e9b6ba2 176 }
177
55748620
TH
178 /**
179 * Get the form fields needed to edit one unit.
180 * @param MoodleQuickForm $mform the form being built.
181 * @return array of form fields.
182 */
c4b2600d
CC
183 protected function unit_group($mform) {
184 $grouparray = array();
8766d198 185 $grouparray[] = $mform->createElement('text', 'unit', get_string('unit', 'qtype_numerical'), array('size'=>10));
688c8035 186 $grouparray[] = $mform->createElement('float', 'multiplier',
8766d198 187 get_string('multiplier', 'qtype_numerical'), array('size'=>10));
c4b2600d
CC
188
189 return $grouparray;
190 }
191
c7df5006 192 protected function data_preprocessing($question) {
1fa39364
TH
193 $question = parent::data_preprocessing($question);
194 $question = $this->data_preprocessing_answers($question);
195 $question = $this->data_preprocessing_hints($question);
196 $question = $this->data_preprocessing_units($question);
fdd015b7 197 $question = $this->data_preprocessing_unit_options($question);
1fa39364
TH
198 return $question;
199 }
200
072db71c
PS
201 protected function data_preprocessing_answers($question, $withanswerfiles = false) {
202 $question = parent::data_preprocessing_answers($question, $withanswerfiles);
1fa39364
TH
203 if (empty($question->options->answers)) {
204 return $question;
205 }
206
207 $key = 0;
208 foreach ($question->options->answers as $answer) {
5cf69d7f 209 // See comment in the parent method about this hack.
f4fe3968 210 unset($this->_form->_defaultValues["tolerance[{$key}]"]);
5cf69d7f 211
1fa39364 212 $question->tolerance[$key] = $answer->tolerance;
326bb547
SR
213
214 if (is_numeric($question->answer[$key])) {
bb8d19b8 215 $question->answer[$key] = format_float($question->answer[$key], -1);
326bb547
SR
216 }
217
1fa39364
TH
218 $key++;
219 }
4d2657c2 220
1fa39364
TH
221 return $question;
222 }
223
224 /**
fdd015b7
TH
225 * Perform the necessary preprocessing for the fields added by
226 * {@link add_unit_fields()}.
227 * @param object $question the data being passed to the form.
228 * @return object $question the modified data.
1fa39364 229 */
fdd015b7 230 protected function data_preprocessing_units($question) {
1fa39364
TH
231 if (empty($question->options->units)) {
232 return $question;
233 }
234
235 foreach ($question->options->units as $key => $unit) {
236 $question->unit[$key] = $unit->unit;
237 $question->multiplier[$key] = $unit->multiplier;
238 }
239
240 return $question;
241 }
242
243 /**
fdd015b7
TH
244 * Perform the necessary preprocessing for the fields added by
245 * {@link add_unit_options()}.
246 * @param object $question the data being passed to the form.
247 * @return object $question the modified data.
1fa39364 248 */
fdd015b7 249 protected function data_preprocessing_unit_options($question) {
1fa39364
TH
250 if (empty($question->options)) {
251 return $question;
252 }
253
254 $question->unitpenalty = $question->options->unitpenalty;
255 $question->unitsleft = $question->options->unitsleft;
256
257 if ($question->options->unitgradingtype) {
258 $question->unitgradingtypes = $question->options->unitgradingtype;
259 $question->multichoicedisplay = $question->options->showunits;
260 $question->unitrole = qtype_numerical::UNITGRADED;
261 } else {
262 $question->unitrole = $question->options->showunits;
6e9b6ba2 263 }
f77f47ec 264
fe6ce234 265 return $question;
6e9b6ba2 266 }
fe6ce234 267
c7df5006 268 public function validation($data, $files) {
fe93ba83 269 $errors = parent::validation($data, $files);
18f9b2d2
TH
270 $errors = $this->validate_answers($data, $errors);
271 $errors = $this->validate_numerical_options($data, $errors);
272 return $errors;
273 }
51e89d95 274
18f9b2d2
TH
275 /**
276 * Validate the answers.
277 * @param array $data the submitted data.
278 * @param array $errors the errors array to add to.
279 * @return array the updated errors array.
280 */
e0736817 281 protected function validate_answers($data, $errors) {
51e89d95 282 // Check the answers.
6e9b6ba2 283 $answercount = 0;
51e89d95 284 $maxgrade = false;
285 $answers = $data['answer'];
286 foreach ($answers as $key => $answer) {
6e9b6ba2 287 $trimmedanswer = trim($answer);
d95c82ab 288 if ($trimmedanswer != '') {
6e9b6ba2 289 $answercount++;
18f9b2d2 290 if (!$this->is_valid_answer($trimmedanswer, $data)) {
11a66e2c 291 $errors['answeroptions[' . $key . ']'] = $this->valid_answer_message($trimmedanswer);
d95c82ab 292 }
51e89d95 293 if ($data['fraction'][$key] == 1) {
294 $maxgrade = true;
295 }
688c8035 296 if ($answer !== '*' && $data['tolerance'][$key] === false) {
11a66e2c 297 $errors['answeroptions['.$key.']'] =
55748620 298 get_string('xmustbenumeric', 'qtype_numerical',
c4b2600d 299 get_string('acceptederror', 'qtype_numerical'));
18f9b2d2 300 }
fdd015b7
TH
301 } else if ($data['fraction'][$key] != 0 ||
302 !html_is_blank($data['feedback'][$key]['text'])) {
11a66e2c 303 $errors['answeroptions[' . $key . ']'] = $this->valid_answer_message($trimmedanswer);
94a6d656 304 $answercount++;
6e9b6ba2 305 }
306 }
26b26662 307 if ($answercount == 0) {
11a66e2c 308 $errors['answeroptions[0]'] = get_string('notenoughanswers', 'qtype_numerical');
6e9b6ba2 309 }
51e89d95 310 if ($maxgrade == false) {
11a66e2c 311 $errors['answeroptions[0]'] = get_string('fractionsnomax', 'question');
51e89d95 312 }
90489407
TH
313
314 return $errors;
18f9b2d2 315 }
1fa39364 316
18f9b2d2
TH
317 /**
318 * Validate a particular answer.
319 * @param string $answer an answer to validate. Known to be non-blank and already trimmed.
320 * @param array $data the submitted data.
321 * @return bool whether this is a valid answer.
322 */
e0736817 323 protected function is_valid_answer($answer, $data) {
48aad79a 324 return $answer == '*' || qtype_numerical::is_valid_number($answer);
18f9b2d2 325 }
1fa39364 326
18f9b2d2
TH
327 /**
328 * @return string erre describing what an answer should be.
329 */
e0736817 330 protected function valid_answer_message($answer) {
18f9b2d2 331 return get_string('answermustbenumberorstar', 'qtype_numerical');
1fa39364
TH
332 }
333
334 /**
18f9b2d2
TH
335 * Validate the answers.
336 * @param array $data the submitted data.
337 * @param array $errors the errors array to add to.
338 * @return array the updated errors array.
339 */
e0736817 340 protected function validate_numerical_options($data, $errors) {
1fa39364 341 if ($data['unitrole'] != qtype_numerical::UNITNONE && trim($data['unit'][0]) == '') {
c4b2600d 342 $errors['units[0]'] = get_string('unitonerequired', 'qtype_numerical');
1fa39364
TH
343 }
344
c4b2600d 345 if (empty($data['unit']) || $data['unitrole'] == qtype_numerical::UNITNONE) {
1fa39364
TH
346 return $errors;
347 }
348
349 // Basic unit validation.
350 foreach ($data['unit'] as $key => $unit) {
351 if (is_numeric($unit)) {
c4b2600d 352 $errors['units[' . $key . ']'] =
55748620
TH
353 get_string('xmustnotbenumeric', 'qtype_numerical',
354 get_string('unit', 'qtype_numerical'));
1fa39364
TH
355 }
356
357 $trimmedunit = trim($unit);
358 if (empty($trimmedunit)) {
359 continue;
360 }
361
362 $trimmedmultiplier = trim($data['multiplier'][$key]);
363 if (empty($trimmedmultiplier)) {
c4b2600d 364 $errors['units[' . $key . ']'] =
55748620 365 get_string('youmustenteramultiplierhere', 'qtype_numerical');
688c8035 366 } else if ($trimmedmultiplier === false) {
c4b2600d 367 $errors['units[' . $key . ']'] =
55748620
TH
368 get_string('xmustbenumeric', 'qtype_numerical',
369 get_string('multiplier', 'qtype_numerical'));
1fa39364
TH
370 }
371 }
372
373 // Check for repeated units.
374 $alreadyseenunits = array();
375 foreach ($data['unit'] as $key => $unit) {
376 $trimmedunit = trim($unit);
377 if ($trimmedunit == '') {
378 continue;
379 }
380
381 if (in_array($trimmedunit, $alreadyseenunits)) {
c4b2600d 382 $errors['units[' . $key . ']'] =
1fa39364
TH
383 get_string('errorrepeatedunit', 'qtype_numerical');
384 } else {
385 $alreadyseenunits[] = $trimmedunit;
386 }
387 }
51e89d95 388
6e9b6ba2 389 return $errors;
390 }
fe6ce234 391
c7df5006 392 public function qtype() {
6e9b6ba2 393 return 'numerical';
394 }
395}