Commit | Line | Data |
---|---|---|
8fc3e643 | 1 | <?php |
d3603157 TH |
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 | ||
17 | /** | |
18 | * Defines the editing form for the calculated question data set items. | |
19 | * | |
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 | |
24 | */ | |
25 | ||
26 | ||
a17b297d TH |
27 | defined('MOODLE_INTERNAL') || die(); |
28 | ||
72553162 TH |
29 | require_once($CFG->dirroot . '/question/type/edit_question_form.php'); |
30 | ||
a17b297d | 31 | |
d3603157 TH |
32 | /** |
33 | * Calculated question data set items editing form definition. | |
34 | * | |
35 | * @copyright 2007 Jamie Pratt me@jamiep.org | |
36 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
37 | */ | |
72553162 | 38 | class question_dataset_dependent_items_form extends question_wizard_form { |
8fc3e643 | 39 | /** |
40 | * Question object with options and answers already loaded by get_question_options | |
41 | * Be careful how you use this it is needed sometimes to set up the structure of the | |
42 | * form in definition_inner but data is always loaded into the form with set_defaults. | |
43 | * | |
44 | * @var object | |
45 | */ | |
f34488b2 | 46 | public $question; |
8fc3e643 | 47 | /** |
48 | * Reference to question type object | |
49 | * | |
50 | * @var question_dataset_dependent_questiontype | |
51 | */ | |
f34488b2 | 52 | public $qtypeobj; |
60b5ecd3 | 53 | |
f34488b2 | 54 | public $datasetdefs; |
60b5ecd3 | 55 | |
f34488b2 | 56 | public $maxnumber = -1; |
60b5ecd3 | 57 | |
f34488b2 | 58 | public $regenerate; |
60b5ecd3 | 59 | |
f34488b2 | 60 | public $noofitems; |
aeb15530 | 61 | |
f184c65c | 62 | public $outsidelimit = false; |
aeb15530 | 63 | |
450f1127 | 64 | public $commentanswers = array(); |
f184c65c | 65 | |
8fc3e643 | 66 | /** |
67 | * Add question-type specific form fields. | |
68 | * | |
69 | * @param MoodleQuickForm $mform the form being built. | |
70 | */ | |
f184c65c TH |
71 | public function __construct($submiturl, $question, $regenerate) { |
72 | global $SESSION, $CFG, $DB; | |
60b5ecd3 | 73 | $this->regenerate = $regenerate; |
8fc3e643 | 74 | $this->question = $question; |
18f9b2d2 | 75 | $this->qtypeobj = question_bank::get_qtype($this->question->qtype); |
4454447d | 76 | // Validate the question category. |
f184c65c TH |
77 | if (!$category = $DB->get_record('question_categories', |
78 | array('id' => $question->category))) { | |
4454447d PS |
79 | print_error('categorydoesnotexist', 'question', $returnurl); |
80 | } | |
315efce7 | 81 | $this->category = $category; |
d197ea43 | 82 | $this->categorycontext = context::instance_by_id($category->contextid); |
60b5ecd3 | 83 | //get the dataset defintions for this question |
84 | if (empty($question->id)) { | |
f184c65c TH |
85 | $this->datasetdefs = $this->qtypeobj->get_dataset_definitions( |
86 | $question->id, $SESSION->calculated->definitionform->dataset); | |
60b5ecd3 | 87 | } else { |
88 | if (empty($question->options)) { | |
89 | $this->get_question_options($question); | |
90 | } | |
f184c65c TH |
91 | $this->datasetdefs = $this->qtypeobj->get_dataset_definitions( |
92 | $question->id, array()); | |
60b5ecd3 | 93 | } |
94 | ||
95 | foreach ($this->datasetdefs as $datasetdef) { | |
60b5ecd3 | 96 | // Get maxnumber |
97 | if ($this->maxnumber == -1 || $datasetdef->itemcount < $this->maxnumber) { | |
98 | $this->maxnumber = $datasetdef->itemcount; | |
99 | } | |
100 | } | |
101 | foreach ($this->datasetdefs as $defid => $datasetdef) { | |
102 | if (isset($datasetdef->id)) { | |
f184c65c TH |
103 | $this->datasetdefs[$defid]->items = |
104 | $this->qtypeobj->get_database_dataset_items($datasetdef->id); | |
60b5ecd3 | 105 | } |
106 | } | |
f184c65c | 107 | parent::__construct($submiturl); |
8fc3e643 | 108 | } |
f184c65c | 109 | |
c7df5006 | 110 | protected function definition() { |
5940864e | 111 | $labelsharedwildcard = get_string("sharedwildcard", "qtype_calculated"); |
ab8b5142 | 112 | |
8fc3e643 | 113 | $mform =& $this->_form; |
d6b8e192 TH |
114 | $mform->setDisableShortforms(); |
115 | ||
60b5ecd3 | 116 | $strquestionlabel = $this->qtypeobj->comment_header($this->question); |
f184c65c | 117 | if ($this->maxnumber != -1 ) { |
60b5ecd3 | 118 | $this->noofitems = $this->maxnumber; |
119 | } else { | |
120 | $this->noofitems = 0; | |
8fc3e643 | 121 | } |
5940864e | 122 | $label = get_string("sharedwildcards", "qtype_calculated"); |
f184c65c TH |
123 | |
124 | $html2 = $this->qtypeobj->print_dataset_definitions_category_shared( | |
125 | $this->question, $this->datasetdefs); | |
126 | $mform->addElement('static', 'listcategory', $label, $html2); | |
127 | //---------------------------------------------------------------------- | |
128 | $mform->addElement('submit', 'updatedatasets', | |
129 | get_string('updatedatasetparam', 'qtype_calculated')); | |
5d0b1e40 | 130 | $mform->registerNoSubmitButton('updatedatasets'); |
f184c65c TH |
131 | $mform->addElement('header', 'additemhdr', |
132 | get_string('itemtoadd', 'qtype_calculated')); | |
60b5ecd3 | 133 | $idx = 1; |
ab8b5142 | 134 | $data = array(); |
a8d2a373 | 135 | $j = (($this->noofitems) * count($this->datasetdefs))+1; |
f184c65c TH |
136 | foreach ($this->datasetdefs as $defkey => $datasetdef) { |
137 | if ($datasetdef->category |= 0 ) { | |
6ef90fde | 138 | $name = get_string('sharedwildcard', 'qtype_calculated', $datasetdef->name); |
f184c65c | 139 | } else { |
d90b016b | 140 | $name = get_string('wildcard', 'qtype_calculated', $datasetdef->name); |
ab8b5142 PP |
141 | } |
142 | $mform->addElement('text', "number[$j]", $name); | |
61cca0b7 | 143 | $mform->setType("number[$j]", PARAM_FLOAT); |
f34488b2 | 144 | $this->qtypeobj->custom_generator_tools_part($mform, $idx, $j); |
60b5ecd3 | 145 | $idx++; |
a8d2a373 | 146 | $mform->addElement('hidden', "definition[$j]"); |
d18e0fe6 | 147 | $mform->setType("definition[$j]", PARAM_RAW); |
a8d2a373 | 148 | $mform->addElement('hidden', "itemid[$j]"); |
d18e0fe6 | 149 | $mform->setType("itemid[$j]", PARAM_RAW); |
60b5ecd3 | 150 | $mform->addElement('static', "divider[$j]", '', '<hr />'); |
d18e0fe6 | 151 | $mform->setType("divider[$j]", PARAM_RAW); |
a8d2a373 | 152 | $j++; |
60b5ecd3 | 153 | } |
f184c65c TH |
154 | |
155 | $mform->addElement('header', 'updateanswershdr', | |
156 | get_string('answerstoleranceparam', 'qtype_calculated')); | |
157 | $mform->addElement('submit', 'updateanswers', | |
158 | get_string('updatetolerancesparam', 'qtype_calculated')); | |
159 | $mform->setAdvanced('updateanswers', true); | |
5d0b1e40 | 160 | $mform->registerNoSubmitButton('updateanswers'); |
aeb15530 | 161 | |
450f1127 | 162 | $answers = fullclone($this->question->options->answers); |
163 | $key1 =1; | |
164 | foreach ($answers as $key => $answer) { | |
f184c65c TH |
165 | if ('' === $answer->answer) { |
166 | // Do nothing. | |
167 | } else if ('*' === $answer->answer) { | |
168 | $mform->addElement('static', | |
169 | 'answercomment[' . ($this->noofitems+$key1) . ']', $answer->answer); | |
450f1127 | 170 | $mform->addElement('hidden', 'tolerance['.$key.']', ''); |
d18e0fe6 | 171 | $mform->setType('tolerance['.$key.']', PARAM_RAW); |
f184c65c | 172 | $mform->setAdvanced('tolerance['.$key.']', true); |
450f1127 | 173 | $mform->addElement('hidden', 'tolerancetype['.$key.']', ''); |
d18e0fe6 | 174 | $mform->setType('tolerancetype['.$key.']', PARAM_RAW); |
f184c65c | 175 | $mform->setAdvanced('tolerancetype['.$key.']', true); |
450f1127 | 176 | $mform->addElement('hidden', 'correctanswerlength['.$key.']', ''); |
d18e0fe6 | 177 | $mform->setType('correctanswerlength['.$key.']', PARAM_RAW); |
f184c65c | 178 | $mform->setAdvanced('correctanswerlength['.$key.']', true); |
450f1127 | 179 | $mform->addElement('hidden', 'correctanswerformat['.$key.']', ''); |
d18e0fe6 | 180 | $mform->setType('correctanswerformat['.$key.']', PARAM_RAW); |
f184c65c TH |
181 | $mform->setAdvanced('correctanswerformat['.$key.']', true); |
182 | } else { | |
183 | $mform->addElement('static', 'answercomment[' . ($this->noofitems+$key1) . ']', | |
184 | $answer->answer); | |
185 | $mform->addElement('text', 'tolerance['.$key.']', | |
186 | get_string('tolerance', 'qtype_calculated')); | |
187 | $mform->setAdvanced('tolerance['.$key.']', true); | |
188 | $mform->addElement('select', 'tolerancetype['.$key.']', | |
189 | get_string('tolerancetype', 'qtype_numerical'), | |
190 | $this->qtypeobj->tolerance_types()); | |
191 | $mform->setAdvanced('tolerancetype['.$key.']', true); | |
192 | ||
193 | $mform->addElement('select', 'correctanswerlength['.$key.']', | |
194 | get_string('correctanswershows', 'qtype_calculated'), range(0, 9)); | |
195 | $mform->setAdvanced('correctanswerlength['.$key.']', true); | |
196 | ||
197 | $answerlengthformats = array( | |
198 | '1' => get_string('decimalformat', 'qtype_numerical'), | |
18f9b2d2 | 199 | '2' => get_string('significantfiguresformat', 'qtype_calculated') |
f184c65c TH |
200 | ); |
201 | $mform->addElement('select', 'correctanswerformat['.$key.']', | |
202 | get_string('correctanswershowsformat', 'qtype_calculated'), | |
203 | $answerlengthformats); | |
204 | $mform->setAdvanced('correctanswerformat['.$key.']', true); | |
450f1127 | 205 | $mform->addElement('static', 'dividertolerance', '', '<hr />'); |
f184c65c | 206 | $mform->setAdvanced('dividertolerance', true); |
450f1127 | 207 | } |
208 | $key1++; | |
aeb15530 | 209 | } |
60b5ecd3 | 210 | |
28a27ef1 | 211 | $addremoveoptions = array(); |
451373ed | 212 | $addremoveoptions['1']='1'; |
f184c65c | 213 | for ($i=10; $i<=100; $i+=10) { |
451373ed | 214 | $addremoveoptions["$i"]="$i"; |
215 | } | |
ab8b5142 PP |
216 | $showoptions = Array(); |
217 | $showoptions['1']='1'; | |
218 | $showoptions['2']='2'; | |
219 | $showoptions['5']='5'; | |
f184c65c | 220 | for ($i=10; $i<=100; $i+=10) { |
ab8b5142 PP |
221 | $showoptions["$i"]="$i"; |
222 | } | |
864ce820 DW |
223 | $mform->addElement('header', 'addhdr', get_string('add', 'moodle')); |
224 | $mform->closeHeaderBefore('addhdr'); | |
60b5ecd3 | 225 | |
f184c65c | 226 | if ($this->qtypeobj->supports_dataset_item_generation()) { |
8fc3e643 | 227 | $radiogrp = array(); |
f184c65c TH |
228 | $radiogrp[] =& $mform->createElement('radio', 'nextpageparam[forceregeneration]', |
229 | null, get_string('reuseifpossible', 'qtype_calculated'), 0); | |
230 | $radiogrp[] =& $mform->createElement('radio', 'nextpageparam[forceregeneration]', | |
231 | null, get_string('forceregenerationshared', 'qtype_calculated'), 1); | |
232 | $radiogrp[] =& $mform->createElement('radio', 'nextpageparam[forceregeneration]', | |
233 | null, get_string('forceregenerationall', 'qtype_calculated'), 2); | |
234 | $mform->addGroup($radiogrp, 'forceregenerationgrp', | |
235 | get_string('nextitemtoadd', 'qtype_calculated'), "<br/>", false); | |
8fc3e643 | 236 | } |
237 | ||
5940864e | 238 | $mform->addElement('submit', 'getnextbutton', get_string('getnextnow', 'qtype_calculated')); |
451373ed | 239 | $mform->addElement('static', "dividera", '', '<hr />'); |
240 | $addgrp = array(); | |
241 | $addgrp[] =& $mform->createElement('submit', 'addbutton', get_string('add', 'moodle')); | |
f184c65c TH |
242 | $addgrp[] =& $mform->createElement('select', "selectadd", |
243 | get_string('additem', 'qtype_calculated'), $addremoveoptions); | |
244 | $addgrp[] = & $mform->createElement('static', "stat", "Items", | |
245 | get_string('newsetwildcardvalues', 'qtype_calculatedsimple')); | |
246 | $mform->addGroup($addgrp, 'addgrp', get_string('additem', 'qtype_calculated'), ' ', false); | |
450f1127 | 247 | $mform->addElement('static', "divideradd", '', ''); |
451373ed | 248 | if ($this->noofitems > 0) { |
864ce820 | 249 | $mform->addElement('header', 'deleteitemhdr', get_string('delete', 'moodle')); |
451373ed | 250 | $deletegrp = array(); |
f184c65c TH |
251 | $deletegrp[] = $mform->createElement('submit', 'deletebutton', |
252 | get_string('delete', 'moodle')); | |
253 | $deletegrp[] = $mform->createElement('select', 'selectdelete', | |
254 | get_string('deleteitem', 'qtype_calculated')."1", $addremoveoptions); | |
255 | $deletegrp[] = $mform->createElement('static', "stat", "Items", | |
256 | get_string('setwildcardvalues', 'qtype_calculatedsimple')); | |
451373ed | 257 | $mform->addGroup($deletegrp, 'deletegrp', '', ' ', false); |
451373ed | 258 | } else { |
f184c65c TH |
259 | $mform->addElement('static', 'warning', '', '<span class="error">' . |
260 | get_string('youmustaddatleastoneitem', 'qtype_calculated').'</span>'); | |
451373ed | 261 | } |
8fc3e643 | 262 | |
ab8b5142 | 263 | $addgrp1 = array(); |
f184c65c TH |
264 | $addgrp1[] = $mform->createElement('submit', 'showbutton', |
265 | get_string('showitems', 'qtype_calculated')); | |
266 | $addgrp1[] = $mform->createElement('select', "selectshow", '' , $showoptions); | |
267 | $addgrp1[] = $mform->createElement('static', "stat", '', | |
268 | get_string('setwildcardvalues', 'qtype_calculated')); | |
ab8b5142 PP |
269 | $mform->addGroup($addgrp1, 'addgrp1', '', ' ', false); |
270 | $mform->registerNoSubmitButton('showbutton'); | |
271 | $mform->closeHeaderBefore('addgrp1'); | |
f184c65c | 272 | //---------------------------------------------------------------------- |
a8d2a373 | 273 | $j = $this->noofitems * count($this->datasetdefs); |
7d087744 | 274 | $k = optional_param('selectshow', 1, PARAM_INT); |
f184c65c TH |
275 | for ($i = $this->noofitems; $i >= 1; $i--) { |
276 | if ($k > 0) { | |
864ce820 | 277 | $mform->addElement('header', 'setnoheader' . $i, "<b>" . |
f184c65c TH |
278 | get_string('setno', 'qtype_calculated', $i)."</b> "); |
279 | } | |
280 | foreach ($this->datasetdefs as $defkey => $datasetdef) { | |
281 | if ($k > 0) { | |
282 | if ($datasetdef->category == 0 ) { | |
283 | $mform->addElement('text', "number[$j]", | |
284 | get_string('wildcard', 'qtype_calculated', $datasetdef->name)); | |
285 | } else { | |
286 | $mform->addElement('text', "number[$j]", get_string( | |
6ef90fde | 287 | 'sharedwildcard', 'qtype_calculated', $datasetdef->name)); |
ab8b5142 | 288 | } |
f184c65c TH |
289 | |
290 | } else { | |
291 | $mform->addElement('hidden', "number[$j]" , ''); | |
292 | } | |
61cca0b7 | 293 | $mform->setType("number[$j]", PARAM_FLOAT); |
a8d2a373 | 294 | $mform->addElement('hidden', "itemid[$j]"); |
87cd4f54 | 295 | $mform->setType("itemid[$j]", PARAM_INT); |
a8d2a373 | 296 | |
297 | $mform->addElement('hidden', "definition[$j]"); | |
87cd4f54 | 298 | $mform->setType("definition[$j]", PARAM_NOTAGS); |
ab8b5142 | 299 | $data[$datasetdef->name] =$datasetdef->items[$i]->value; |
a8d2a373 | 300 | |
301 | $j--; | |
302 | } | |
f184c65c TH |
303 | if ('' != $strquestionlabel && ($k > 0 )) { |
304 | //|| $this->outsidelimit || !empty($this->numbererrors ) | |
305 | $repeated[] = $mform->addElement('static', "answercomment[$i]", $strquestionlabel); | |
ab8b5142 | 306 | // decode equations in question text |
f184c65c TH |
307 | $qtext = $this->qtypeobj->substitute_variables( |
308 | $this->question->questiontext, $data); | |
ab8b5142 | 309 | $textequations = $this->qtypeobj->find_math_equations($qtext); |
f184c65c TH |
310 | if ($textequations != '' && count($textequations) > 0 ) { |
311 | $mform->addElement('static', "divider1[$j]", '', | |
312 | 'Formulas {=..} in question text'); | |
313 | foreach ($textequations as $key => $equation) { | |
314 | if ($formulaerrors = qtype_calculated_find_formula_errors($equation)) { | |
315 | $str=$formulaerrors; | |
316 | } else { | |
317 | eval('$str = '.$equation.';'); | |
ab8b5142 | 318 | } |
ab8b5142 | 319 | |
f184c65c TH |
320 | $mform->addElement('static', "textequation", "{=$equation}", "=".$str); |
321 | } | |
322 | } | |
323 | ||
324 | } | |
325 | $k--; | |
ab8b5142 | 326 | |
a8d2a373 | 327 | } |
f184c65c TH |
328 | $mform->addElement('static', 'outsidelimit', '', ''); |
329 | //---------------------------------------------------------------------- | |
330 | // Non standard name for button element needed so not using add_action_buttons | |
331 | if (!($this->noofitems==0) ) { | |
332 | $mform->addElement('submit', 'savechanges', get_string('savechanges')); | |
333 | $mform->closeHeaderBefore('savechanges'); | |
450f1127 | 334 | } |
79bb7202 | 335 | |
72553162 | 336 | $this->add_hidden_fields(); |
79bb7202 | 337 | |
315efce7 | 338 | $mform->addElement('hidden', 'category'); |
72553162 | 339 | $mform->setType('category', PARAM_SEQUENCE); |
79bb7202 | 340 | |
60b5ecd3 | 341 | $mform->addElement('hidden', 'wizard', 'datasetitems'); |
342 | $mform->setType('wizard', PARAM_ALPHA); | |
343 | } | |
8fc3e643 | 344 | |
f184c65c | 345 | public function set_data($question) { |
60b5ecd3 | 346 | $formdata = array(); |
5d0b1e40 | 347 | $fromform = new stdClass(); |
f184c65c | 348 | if (isset($question->options)) { |
450f1127 | 349 | $answers = $question->options->answers; |
350 | if (count($answers)) { | |
7d087744 TH |
351 | if (optional_param('updateanswers', false, PARAM_BOOL) || |
352 | optional_param('updatedatasets', false, PARAM_BOOL)) { | |
f184c65c TH |
353 | foreach ($answers as $key => $answer) { |
354 | $fromform->tolerance[$key]= $this->_form->getElementValue( | |
355 | 'tolerance['.$key.']'); | |
5d0b1e40 | 356 | $answer->tolerance = $fromform->tolerance[$key]; |
f184c65c TH |
357 | $fromform->tolerancetype[$key]= $this->_form->getElementValue( |
358 | 'tolerancetype['.$key.']'); | |
359 | if (is_array($fromform->tolerancetype[$key])) { | |
360 | $fromform->tolerancetype[$key]= $fromform->tolerancetype[$key][0]; | |
361 | } | |
5d0b1e40 | 362 | $answer->tolerancetype = $fromform->tolerancetype[$key]; |
f184c65c TH |
363 | $fromform->correctanswerlength[$key]= $this->_form->getElementValue( |
364 | 'correctanswerlength['.$key.']'); | |
365 | if (is_array($fromform->correctanswerlength[$key])) { | |
366 | $fromform->correctanswerlength[$key] = | |
367 | $fromform->correctanswerlength[$key][0]; | |
368 | } | |
5d0b1e40 | 369 | $answer->correctanswerlength = $fromform->correctanswerlength[$key]; |
f184c65c TH |
370 | $fromform->correctanswerformat[$key] = $this->_form->getElementValue( |
371 | 'correctanswerformat['.$key.']'); | |
372 | if (is_array($fromform->correctanswerformat[$key])) { | |
373 | $fromform->correctanswerformat[$key] = | |
374 | $fromform->correctanswerformat[$key][0]; | |
375 | } | |
5d0b1e40 | 376 | $answer->correctanswerformat = $fromform->correctanswerformat[$key]; |
377 | } | |
f184c65c TH |
378 | $this->qtypeobj->save_question_calculated($question, $fromform); |
379 | ||
380 | } else { | |
381 | foreach ($answers as $key => $answer) { | |
382 | $formdata['tolerance['.$key.']'] = $answer->tolerance; | |
383 | $formdata['tolerancetype['.$key.']'] = $answer->tolerancetype; | |
384 | $formdata['correctanswerlength['.$key.']'] = $answer->correctanswerlength; | |
385 | $formdata['correctanswerformat['.$key.']'] = $answer->correctanswerformat; | |
386 | } | |
387 | } | |
450f1127 | 388 | } |
5d0b1e40 | 389 | } |
60b5ecd3 | 390 | //fill out all data sets and also the fields for the next item to add. |
a8d2a373 | 391 | $j = $this->noofitems * count($this->datasetdefs); |
f184c65c | 392 | for ($itemnumber = $this->noofitems; $itemnumber >= 1; $itemnumber--) { |
60b5ecd3 | 393 | $data = array(); |
f184c65c TH |
394 | foreach ($this->datasetdefs as $defid => $datasetdef) { |
395 | if (isset($datasetdef->items[$itemnumber])) { | |
60b5ecd3 | 396 | $formdata["number[$j]"] = $datasetdef->items[$itemnumber]->value; |
397 | $formdata["definition[$j]"] = $defid; | |
398 | $formdata["itemid[$j]"] = $datasetdef->items[$itemnumber]->id; | |
399 | $data[$datasetdef->name] = $datasetdef->items[$itemnumber]->value; | |
400 | } | |
a8d2a373 | 401 | $j--; |
60b5ecd3 | 402 | } |
f184c65c TH |
403 | $comment = $this->qtypeobj->comment_on_datasetitems($this->qtypeobj, $question->id, |
404 | $question->questiontext, $answers, $data, $itemnumber); | |
405 | if ($comment->outsidelimit) { | |
406 | $this->outsidelimit=$comment->outsidelimit; | |
450f1127 | 407 | } |
408 | $totalcomment=''; | |
409 | foreach ($question->options->answers as $key => $answer) { | |
410 | $totalcomment .= $comment->stranswers[$key].'<br/>'; | |
411 | } | |
f184c65c | 412 | $formdata['answercomment['.$itemnumber.']'] = $totalcomment; |
60b5ecd3 | 413 | } |
8fc3e643 | 414 | |
60b5ecd3 | 415 | $formdata['nextpageparam[forceregeneration]'] = $this->regenerate; |
451373ed | 416 | $formdata['selectdelete'] = '1'; |
417 | $formdata['selectadd'] = '1'; | |
a8d2a373 | 418 | $j = $this->noofitems * count($this->datasetdefs)+1; |
60b5ecd3 | 419 | $data = array(); // data for comment_on_datasetitems later |
420 | //dataset generation dafaults | |
421 | if ($this->qtypeobj->supports_dataset_item_generation()) { | |
422 | $itemnumber = $this->noofitems+1; | |
f184c65c | 423 | foreach ($this->datasetdefs as $defid => $datasetdef) { |
7d087744 TH |
424 | if (!optional_param('updatedatasets', false, PARAM_BOOL) && |
425 | !optional_param('updateanswers', false, PARAM_BOOL)) { | |
f184c65c TH |
426 | $formdata["number[$j]"] = $this->qtypeobj->generate_dataset_item( |
427 | $datasetdef->options); | |
428 | } else { | |
429 | $formdata["number[$j]"] = $this->_form->getElementValue("number[$j]"); | |
430 | } | |
60b5ecd3 | 431 | $formdata["definition[$j]"] = $defid; |
f184c65c TH |
432 | $formdata["itemid[$j]"] = isset($datasetdef->items[$itemnumber]) ? |
433 | $datasetdef->items[$itemnumber]->id : 0; | |
60b5ecd3 | 434 | $data[$datasetdef->name] = $formdata["number[$j]"]; |
435 | $j++; | |
436 | } | |
8fc3e643 | 437 | } |
60b5ecd3 | 438 | |
439 | //existing records override generated data depending on radio element | |
7d087744 TH |
440 | $j = $this->noofitems * count($this->datasetdefs) + 1; |
441 | if (!$this->regenerate && !optional_param('updatedatasets', false, PARAM_BOOL) && | |
442 | !optional_param('updateanswers', false, PARAM_BOOL)) { | |
60b5ecd3 | 443 | $idx = 1; |
7d087744 | 444 | $itemnumber = $this->noofitems + 1; |
f184c65c TH |
445 | foreach ($this->datasetdefs as $defid => $datasetdef) { |
446 | if (isset($datasetdef->items[$itemnumber])) { | |
60b5ecd3 | 447 | $formdata["number[$j]"] = $datasetdef->items[$itemnumber]->value; |
448 | $formdata["definition[$j]"] = $defid; | |
449 | $formdata["itemid[$j]"] = $datasetdef->items[$itemnumber]->id; | |
450 | $data[$datasetdef->name] = $datasetdef->items[$itemnumber]->value; | |
451 | } | |
452 | $j++; | |
453 | } | |
60b5ecd3 | 454 | } |
28a27ef1 | 455 | |
f184c65c TH |
456 | $comment = $this->qtypeobj->comment_on_datasetitems($this->qtypeobj, $question->id, |
457 | $question->questiontext, $answers, $data, ($this->noofitems + 1)); | |
7d087744 | 458 | if (isset($comment->outsidelimit) && $comment->outsidelimit) { |
f184c65c TH |
459 | $this->outsidelimit=$comment->outsidelimit; |
460 | } | |
461 | $key1 = 1; | |
462 | foreach ($question->options->answers as $key => $answer) { | |
463 | $formdata['answercomment['.($this->noofitems+$key1).']'] = $comment->stranswers[$key]; | |
464 | $key1++; | |
465 | } | |
60b5ecd3 | 466 | |
f184c65c TH |
467 | if ($this->outsidelimit) { |
468 | $formdata['outsidelimit']= '<span class="error">' . | |
469 | get_string('oneanswertrueansweroutsidelimits', 'qtype_calculated') . '</span>'; | |
450f1127 | 470 | } |
60b5ecd3 | 471 | $formdata = $this->qtypeobj->custom_generator_set_data($this->datasetdefs, $formdata); |
472 | ||
473 | parent::set_data((object)($formdata + (array)$question)); | |
8fc3e643 | 474 | } |
475 | ||
f184c65c | 476 | public function validation($data, $files) { |
60b5ecd3 | 477 | $errors = array(); |
f184c65c | 478 | if (isset($data['savechanges']) && ($this->noofitems==0) ) { |
451373ed | 479 | $errors['warning'] = get_string('warning', 'mnet'); |
aeb15530 | 480 | } |
f184c65c TH |
481 | if ($this->outsidelimit) { |
482 | $errors['outsidelimits'] = | |
483 | get_string('oneanswertrueansweroutsidelimits', 'qtype_calculated'); | |
60b5ecd3 | 484 | } |
ac5230fe | 485 | $numbers = $data['number']; |
f184c65c TH |
486 | foreach ($numbers as $key => $number) { |
487 | if (! is_numeric($number)) { | |
488 | if (stristr($number, ', ')) { | |
489 | $errors['number['.$key.']'] = get_string( | |
490 | 'The , cannot be used, use . as in 0.013 or 1.3e-2', 'qtype_calculated'); | |
491 | } else { | |
492 | $errors['number['.$key.']'] = get_string( | |
493 | 'This is not a valid number', 'qtype_calculated'); | |
ac5230fe | 494 | } |
f184c65c TH |
495 | } else if (stristr($number, 'x')) { |
496 | $errors['number['.$key.']'] = get_string( | |
497 | 'Hexadecimal format (i.e. 0X12d) is not allowed', 'qtype_calculated'); | |
498 | } else if (is_nan($number)) { | |
499 | $errors['number['.$key.']'] = get_string( | |
500 | 'is a NAN number', 'qtype_calculated'); | |
aeb15530 | 501 | } |
ac5230fe | 502 | } |
60b5ecd3 | 503 | return $errors; |
504 | } | |
8fc3e643 | 505 | } |