Merge branch 'MDL-70094-310' of https://github.com/SangNguyen2601/moodle into MOODLE_...
[moodle.git] / question / type / calculated / edit_calculated_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
271ffe3f 17/**
18 * Defines the editing form for the calculated question type.
19 *
b04a4319
TH
20 * @package qtype
21 * @subpackage calculated
22 * @copyright 2007 Jamie Pratt me@jamiep.org
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
271ffe3f 24 */
25
b04a4319 26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
18f9b2d2
TH
29require_once($CFG->dirroot . '/question/type/numerical/edit_numerical_form.php');
30
a17b297d 31
271ffe3f 32/**
d3603157 33 * Calculated question type editing form definition.
b04a4319
TH
34 *
35 * @copyright 2007 Jamie Pratt me@jamiep.org
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
271ffe3f 37 */
18f9b2d2 38class qtype_calculated_edit_form extends qtype_numerical_edit_form {
9aa022fe 39 /**
40 * Handle to the question type for this question.
41 *
f184c65c 42 * @var qtype_calculated
9aa022fe 43 */
d90b016b 44 public $qtypeobj;
fe6ce234
DC
45 public $questiondisplay;
46 public $activecategory;
47 public $categorychanged = false;
d90b016b 48 public $initialname = '';
fe6ce234
DC
49 public $reload = false;
50
f184c65c
TH
51 public function __construct($submiturl, $question, $category, $contexts,
52 $formeditable = true) {
53 global $CFG, $DB;
d90b016b 54 $this->question = $question;
7d087744 55 $this->reload = optional_param('reload', false, PARAM_BOOL);
2aef1fe5 56
c7218aef 57 if (!$this->reload) { // Use database data as this is first pass.
f184c65c 58 if (isset($this->question->id)) {
c7218aef 59 // Remove prefix #{..}# if exists.
f184c65c 60 $this->initialname = $question->name;
75aa674b
TH
61 $question->name = question_bank::get_qtype($this->qtype())
62 ->clean_technical_prefix_from_question_name($question->name);
d90b016b 63 }
fe6ce234 64 }
f184c65c 65 parent::__construct($submiturl, $question, $category, $contexts, $formeditable);
d90b016b 66 }
fe6ce234 67
f184c65c
TH
68 public function get_per_answer_fields($mform, $label, $gradeoptions,
69 &$repeatedoptions, &$answersoption) {
fb17f129
TH
70 $repeated = parent::get_per_answer_fields($mform, $label, $gradeoptions,
71 $repeatedoptions, $answersoption);
72
c7218aef
CC
73 // Reorganise answer options group. 0 is the answer. 1 is tolerance. 2 is Grade.
74 $answeroptions = $repeated[0]->getElements();
75 // Tolerance field will be part of its own group.
76 $tolerance = $answeroptions[1];
77
78 // Update Answer options group to contain only answer and grade fields.
f16d5f97 79 $answeroptions[0]->setSize(55);
c7218aef
CC
80 $answeroptions = array($answeroptions[0], $answeroptions[2]);
81 $repeated[0]->setElements($answeroptions);
82
83 // Update answer field and group label.
84 $repeated[0]->setLabel(get_string('answerformula', 'qtype_calculated', '{no}') . ' =');
85 $answeroptions[0]->setLabel(get_string('answerformula', 'qtype_calculated', '{no}') . ' =');
86
87 // Get feedback field to re append later.
88 $feedback = array_pop($repeated);
89
90 // Create tolerance group.
91 $answertolerance = array();
92 $tolerance->setLabel(get_string('tolerance', 'qtype_calculated') . '=');
93 $answertolerance[] = $tolerance;
94 $answertolerance[] = $mform->createElement('select', 'tolerancetype',
95 get_string('tolerancetype', 'qtype_calculated'), $this->qtypeobj->tolerance_types());
96 $repeated[] = $mform->createElement('group', 'answertolerance',
97 get_string('tolerance', 'qtype_calculated'), $answertolerance, null, false);
fb17f129 98 $repeatedoptions['tolerance']['default'] = 0.01;
2aef1fe5 99
c7218aef
CC
100 // Create display group.
101 $answerdisplay = array();
102 $answerdisplay[] = $mform->createElement('select', 'correctanswerlength',
103 get_string('answerdisplay', 'qtype_calculated'), range(0, 9));
2aef1fe5 104 $repeatedoptions['correctanswerlength']['default'] = 2;
105
f184c65c
TH
106 $answerlengthformats = array(
107 '1' => get_string('decimalformat', 'qtype_numerical'),
18f9b2d2 108 '2' => get_string('significantfiguresformat', 'qtype_calculated')
f184c65c 109 );
c7218aef 110 $answerdisplay[] = $mform->createElement('select', 'correctanswerformat',
f184c65c 111 get_string('correctanswershowsformat', 'qtype_calculated'), $answerlengthformats);
c7218aef
CC
112 $repeated[] = $mform->createElement('group', 'answerdisplay',
113 get_string('answerdisplay', 'qtype_calculated'), $answerdisplay, null, false);
fb17f129 114
c7218aef
CC
115 // Add feedback.
116 $repeated[] = $feedback;
fb17f129 117
2aef1fe5 118 return $repeated;
119 }
120
271ffe3f 121 /**
122 * Add question-type specific form fields.
123 *
9aa022fe 124 * @param MoodleQuickForm $mform the form being built.
271ffe3f 125 */
18f9b2d2
TH
126 protected function definition_inner($mform) {
127 $this->qtypeobj = question_bank::get_qtype($this->qtype());
cf146692 128 $label = get_string('sharedwildcards', 'qtype_calculated');
a6d46515 129 $mform->addElement('hidden', 'initialcategory', 1);
d90b016b 130 $mform->addElement('hidden', 'reload', 1);
d18e0fe6 131 $mform->setType('initialcategory', PARAM_INT);
1e988d26 132 $mform->setType('reload', PARAM_BOOL);
a6d46515 133 $html2 = $this->qtypeobj->print_dataset_definitions_category($this->question);
f184c65c
TH
134 $mform->insertElementBefore(
135 $mform->createElement('static', 'listcategory', $label, $html2), 'name');
136 if (isset($this->question->id)) {
137 $mform->insertElementBefore($mform->createElement('static', 'initialname',
138 get_string('questionstoredname', 'qtype_calculated'),
c942cad7 139 format_string($this->initialname)), 'name');
d90b016b 140 };
f184c65c
TH
141 $addfieldsname = 'updatecategory';
142 $addstring = get_string('updatecategory', 'qtype_calculated');
fe6ce234 143 $mform->registerNoSubmitButton($addfieldsname);
271e6dec 144
f184c65c
TH
145 $mform->insertElementBefore(
146 $mform->createElement('submit', $addfieldsname, $addstring), 'listcategory');
28a27ef1 147 $mform->registerNoSubmitButton('createoptionbutton');
aeb15530 148
3d9645ae 149 // Editing as regular question.
fe6ce234 150 $mform->setType('single', PARAM_INT);
aeb15530 151
f184c65c 152 $mform->addElement('hidden', 'shuffleanswers', '1');
fe6ce234 153 $mform->setType('shuffleanswers', PARAM_INT);
f184c65c 154 $mform->addElement('hidden', 'answernumbering', 'abc');
fe6ce234 155 $mform->setType('answernumbering', PARAM_SAFEDIR);
271ffe3f 156
f184c65c 157 $this->add_per_answer_fields($mform, get_string('answerhdr', 'qtype_calculated', '{no}'),
92111e8d 158 question_bank::fraction_options(), 1, 1);
92186abc 159
271e6dec 160 $repeated = array();
271ffe3f 161
18f9b2d2
TH
162 $this->add_unit_options($mform, $this);
163 $this->add_unit_fields($mform, $this);
b130270d 164 $this->add_interactive_settings();
fe6ce234 165
c7218aef 166 // Hidden elements.
e593233a 167 $mform->addElement('hidden', 'synchronize', '');
d18e0fe6 168 $mform->setType('synchronize', PARAM_INT);
60b5ecd3 169 $mform->addElement('hidden', 'wizard', 'datasetdefinitions');
170 $mform->setType('wizard', PARAM_ALPHA);
271ffe3f 171 }
172
8011be18
TH
173 protected function can_preview() {
174 return false; // Generally not possible for calculated questions on this page.
175 }
176
f184c65c 177 public function data_preprocessing($question) {
5cf69d7f
TH
178 $question = parent::data_preprocessing($question);
179 $question = $this->data_preprocessing_answers($question);
b130270d 180 $question = $this->data_preprocessing_hints($question);
18f9b2d2
TH
181 $question = $this->data_preprocessing_units($question);
182 $question = $this->data_preprocessing_unit_options($question);
183
5cf69d7f
TH
184 if (isset($question->options->synchronize)) {
185 $question->synchronize = $question->options->synchronize;
186 }
187
188 return $question;
189 }
190
072db71c
PS
191 protected function data_preprocessing_answers($question, $withanswerfiles = false) {
192 $question = parent::data_preprocessing_answers($question, $withanswerfiles);
5cf69d7f
TH
193 if (empty($question->options->answers)) {
194 return $question;
195 }
196
197 $key = 0;
198 foreach ($question->options->answers as $answer) {
199 // See comment in the parent method about this hack.
f4fe3968
TH
200 unset($this->_form->_defaultValues["tolerancetype[{$key}]"]);
201 unset($this->_form->_defaultValues["correctanswerlength[{$key}]"]);
202 unset($this->_form->_defaultValues["correctanswerformat[{$key}]"]);
5cf69d7f
TH
203
204 $question->tolerancetype[$key] = $answer->tolerancetype;
205 $question->correctanswerlength[$key] = $answer->correctanswerlength;
206 $question->correctanswerformat[$key] = $answer->correctanswerformat;
207 $key++;
208 }
209
fe6ce234 210 return $question;
271ffe3f 211 }
212
f184c65c 213 public function qtype() {
271ffe3f 214 return 'calculated';
215 }
216
88ec9f30
TH
217 /**
218 * Validate the equations in the some question content.
219 * @param array $errors where errors are being accumulated.
220 * @param string $field the field being validated.
221 * @param string $text the content of that field.
222 * @return array the updated $errors array.
223 */
224 protected function validate_text($errors, $field, $text) {
225 $problems = qtype_calculated_find_formula_errors_in_text($text);
226 if ($problems) {
227 $errors[$field] = $problems;
271e6dec 228 }
88ec9f30
TH
229 return $errors;
230 }
18f9b2d2 231
88ec9f30 232 public function validation($data, $files) {
18f9b2d2
TH
233 $errors = parent::validation($data, $files);
234
88ec9f30
TH
235 // Verifying for errors in {=...} in question text.
236 $errors = $this->validate_text($errors, 'questiontext', $data['questiontext']['text']);
237 $errors = $this->validate_text($errors, 'generalfeedback', $data['generalfeedback']['text']);
238
18f9b2d2 239 // Check that the answers use datasets.
9af77e9d 240 $answers = $data['answer'];
f6232d58 241 $mandatorydatasets = array();
f184c65c 242 foreach ($answers as $key => $answer) {
88ec9f30
TH
243 $problems = qtype_calculated_find_formula_errors($answer);
244 if ($problems) {
245 $errors['answeroptions['.$key.']'] = $problems;
246 }
a6d46515 247 $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer);
88ec9f30
TH
248 $errors = $this->validate_text($errors, 'feedback[' . $key . ']',
249 $data['feedback'][$key]['text']);
271e6dec 250 }
18f9b2d2 251 if (empty($mandatorydatasets)) {
f184c65c 252 foreach ($answers as $key => $answer) {
11a66e2c 253 $errors['answeroptions['.$key.']'] =
efe3e87b 254 get_string('atleastonewildcard', 'qtype_calculated');
271e6dec 255 }
256 }
18f9b2d2
TH
257
258 // Validate the answer format.
f184c65c 259 foreach ($answers as $key => $answer) {
fe6ce234 260 $trimmedanswer = trim($answer);
18f9b2d2
TH
261 if (trim($answer)) {
262 if ($data['correctanswerformat'][$key] == 2 &&
263 $data['correctanswerlength'][$key] == '0') {
11a66e2c 264 $errors['answerdisplay['.$key.']'] =
f184c65c
TH
265 get_string('zerosignificantfiguresnotallowed', 'qtype_calculated');
266 }
fe6ce234 267 }
fe6ce234 268 }
aeb15530 269
271ffe3f 270 return $errors;
271 }
18f9b2d2 272
e0736817 273 protected function is_valid_answer($answer, $data) {
18f9b2d2
TH
274 return !qtype_calculated_find_formula_errors($answer);
275 }
276
e0736817 277 protected function valid_answer_message($answer) {
18f9b2d2
TH
278 if (!$answer) {
279 return get_string('mustenteraformulaorstar', 'qtype_numerical');
280 } else {
281 return qtype_calculated_find_formula_errors($answer);
282 }
283 }
271ffe3f 284}