weekly release 2.4dev
[moodle.git] / question / type / calculatedsimple / edit_calculatedsimple_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
83a15d02 17/**
18 * Defines the editing form for the calculated simplequestion type.
19 *
b04a4319
TH
20 * @package qtype
21 * @subpackage calculatedsimple
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
27defined('MOODLE_INTERNAL') || die();
28
efe3e87b
TH
29require_once($CFG->dirroot . '/question/type/calculated/edit_calculated_form.php');
30
a17b297d 31
b04a4319
TH
32/**
33 * Editing form for the calculated simplequestion type.
34 *
35 * @copyright 2007 Jamie Pratt me@jamiep.org
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
83a15d02 37 */
efe3e87b 38class qtype_calculatedsimple_edit_form extends qtype_calculated_edit_form {
83a15d02 39 /**
40 * Handle to the question type for this question.
41 *
42 * @var question_calculatedsimple_qtype
43 */
fe6ce234 44 public $qtypeobj;
0735b509 45
8200ccd6 46 public $wildcarddisplay;
0735b509 47
8200ccd6 48 public $questiondisplay;
83a15d02 49
50 public $datasetdefs;
51
8200ccd6 52 public $reload = false;
aeb15530 53
83a15d02 54 public $maxnumber = -1;
55
56 public $regenerate = true;
57
58 public $noofitems;
aeb15530 59
8200ccd6 60 public $outsidelimit = false;
aeb15530
PS
61
62 public $commentanswer = array();
63
83a15d02 64 public $answer = array();
65
66 public $nonemptyanswer = array();
aeb15530 67
a31697ba 68 public $numbererrors = array();
69
aeb15530
PS
70 public $formdata = array();
71
8200ccd6 72 public function __construct($submiturl, $question, $category, $contexts, $formeditable = true) {
83a15d02 73 $this->regenerate = true;
74 $this->question = $question;
b003b818 75
efe3e87b 76 $this->qtypeobj = question_bank::get_qtype($this->question->qtype);
8200ccd6
TH
77 // get the dataset definitions for this question
78 // coming here everytime even when using a NoSubmitButton
79 // so this will only set the values to the actual question database content
80 // which is not what we want so this should be removed from here
83a15d02 81 // get priority to paramdatasets
aeb15530 82
7d087744 83 $this->reload = optional_param('reload', false, PARAM_BOOL);
fe6ce234 84 if (!$this->reload) { // use database data as this is first pass
0735b509 85 // question->id == 0 so no stored datasets
fe6ce234
DC
86 if (!empty($question->id)) {
87
8200ccd6
TH
88 $this->datasetdefs = $this->qtypeobj->get_dataset_definitions(
89 $question->id, array());
aeb15530 90
8200ccd6 91 if (!empty($this->datasetdefs)) {
1d1f5784 92 foreach ($this->datasetdefs as $defid => $datasetdef) {
aeb15530 93 // first get the items in case their number does not correspond to itemcount
1d1f5784 94 if (isset($datasetdef->id)) {
8200ccd6
TH
95 $this->datasetdefs[$defid]->items =
96 $this->qtypeobj->get_database_dataset_items($datasetdef->id);
97 if ($this->datasetdefs[$defid]->items != '') {
0735b509 98 $datasetdef->itemcount = count($this->datasetdefs[$defid]->items);
99 } else {
8200ccd6 100 $datasetdef->itemcount = 0;
0735b509 101 }
1d1f5784 102 }
83a15d02 103 // Get maxnumber
104 if ($this->maxnumber == -1 || $datasetdef->itemcount < $this->maxnumber) {
105 $this->maxnumber = $datasetdef->itemcount;
106 }
107 }
83a15d02 108 }
1d1f5784 109
8200ccd6
TH
110 $i = 0;
111 foreach ($this->question->options->answers as $answer) {
112 $this->answer[$i] = $answer;
83a15d02 113 $i++;
aeb15530 114 }
8200ccd6 115 $this->nonemptyanswer = $this->answer;
aeb15530 116 }
83a15d02 117 $datasettoremove = false;
8200ccd6
TH
118 $newdatasetvalues = false;
119 $newdataset = false;
120 } else {
83a15d02 121 // handle reload to get values from the form-elements
8200ccd6
TH
122 // answers, datasetdefs and data_items. In any case the validation
123 // step will warn the user of any error in settings the values.
aeb15530 124 // Verification for the specific dataset values as the other parameters
83a15d02 125 // unints, feeedback etc are handled elsewhere
126 // handle request buttons :
aeb15530 127 // 'analyzequestion' (Identify the wild cards {x..} present in answers)
83a15d02 128 // 'addbutton' (create new set of datatitems)
129 // 'updatedatasets' is handled automatically on each reload
aeb15530 130 // The analyzequestion is done every time on reload
83a15d02 131 // to detect any new wild cards so that the current display reflects
132 // the mandatory (i.e. in answers) datasets
133 // to implement : don't do any changes if the question is used in a quiz.
aeb15530
PS
134 // If new datadef, new properties should erase items.
135 // Most of the data
06f80c6c 136 $datasettoremove = false;
8200ccd6
TH
137 $newdatasetvalues = false;
138 $newdataset = false;
0735b509 139 $dummyform = new stdClass();
140 $mandatorydatasets = array();
aeb15530 141 // should not test on adding a new answer
8200ccd6
TH
142 // should test if there are already olddatasets or if the 'analyzequestion'
143 // submit button has been clicked
7d087744
TH
144 if (optional_param_array('datasetdef', false, PARAM_BOOL) ||
145 optional_param('analyzequestion', false, PARAM_BOOL)) {
aeb15530 146
7d087744 147 if ($dummyform->answer = optional_param_array('answer', '', PARAM_NOTAGS)) {
8200ccd6 148 // there is always at least one answer...
7d087744
TH
149 $fraction = optional_param_array('fraction', '', PARAM_NUMBER);
150 $tolerance = optional_param_array('tolerance', '', PARAM_NUMBER);
151 $tolerancetype = optional_param_array('tolerancetype', '', PARAM_NUMBER);
152 $correctanswerlength = optional_param_array('correctanswerlength', '', PARAM_INT);
153 $correctanswerformat = optional_param_array('correctanswerformat', '', PARAM_INT);
aeb15530 154
8200ccd6
TH
155 foreach ($dummyform->answer as $key => $answer) {
156 if (trim($answer) != '') { // just look for non-empty
157 $this->answer[$key] = new stdClass();
06f80c6c 158 $this->answer[$key]->answer = $answer;
2a2c5eff 159 $this->answer[$key]->fraction = $fraction[$key];
06f80c6c 160 $this->answer[$key]->tolerance = $tolerance[$key];
161 $this->answer[$key]->tolerancetype = $tolerancetype[$key];
162 $this->answer[$key]->correctanswerlength = $correctanswerlength[$key];
163 $this->answer[$key]->correctanswerformat = $correctanswerformat[$key];
164 $this->nonemptyanswer[]= $this->answer[$key];
8200ccd6 165 $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer);
06f80c6c 166 }
0735b509 167 }
83a15d02 168 }
06f80c6c 169 $this->datasetdefs = array();
170 // rebuild datasetdefs from old values
7d087744
TH
171 if ($olddef = optional_param_array('datasetdef', '', PARAM_RAW)) {
172 $calcmin = optional_param_array('calcmin', '', PARAM_NUMBER);
173 $calclength = optional_param_array('calclength', '', PARAM_INT);
174 $calcmax = optional_param_array('calcmax', '', PARAM_NUMBER);
175 $oldoptions = optional_param_array('defoptions', '', PARAM_RAW);
8200ccd6
TH
176 $newdatasetvalues = false;
177 $sizeofolddef = count($olddef);
178 for ($key = 1; $key <= $sizeofolddef; $key++) {
179 $def = $olddef[$key];
180 $this->datasetdefs[$def]= new stdClass();
06f80c6c 181 $this->datasetdefs[$def]->type = 1;
182 $this->datasetdefs[$def]->category = 0;
8200ccd6
TH
183 $this->datasetdefs[$def]->options = $oldoptions[$key];
184 $this->datasetdefs[$def]->calcmin = $calcmin[$key];
185 $this->datasetdefs[$def]->calcmax = $calcmax[$key];
186 $this->datasetdefs[$def]->calclength = $calclength[$key];
06f80c6c 187 //then compare with new values
8200ccd6
TH
188 if (preg_match('~^(uniform|loguniform):([^:]*):([^:]*):([0-9]*)$~',
189 $this->datasetdefs[$def]->options, $regs)) {
190 if ($this->datasetdefs[$def]->calcmin != $regs[2]||
191 $this->datasetdefs[$def]->calcmax != $regs[3] ||
192 $this->datasetdefs[$def]->calclength != $regs[4]) {
193 $newdatasetvalues = true;
aeb15530 194 }
06f80c6c 195 }
8200ccd6
TH
196 $this->datasetdefs[$def]->options = "uniform:" .
197 $this->datasetdefs[$def]->calcmin . ":" .
198 $this->datasetdefs[$def]->calcmax . ":" .
199 $this->datasetdefs[$def]->calclength;
06f80c6c 200 }
8200ccd6 201 }
aeb15530 202 // detect new datasets
8200ccd6
TH
203 $newdataset = false;
204 foreach ($mandatorydatasets as $datasetname) {
205 if (!isset($this->datasetdefs["1-0-$datasetname"])) {
206 $key = "1-0-$datasetname";
207 $this->datasetdefs[$key] = new stdClass();
208 $this->datasetdefs[$key]->type = 1;
209 $this->datasetdefs[$key]->category = 0;
210 $this->datasetdefs[$key]->name = $datasetname;
211 $this->datasetdefs[$key]->options = "uniform:1.0:10.0:1";
212 $newdataset = true;
213 } else {
214 $this->datasetdefs["1-0-$datasetname"]->name = $datasetname;
215 }
83a15d02 216 }
8200ccd6
TH
217 // remove obsolete datasets
218 $datasettoremove = false;
219 foreach ($this->datasetdefs as $defkey => $datasetdef) {
220 if (!isset($datasetdef->name)) {
221 $datasettoremove = true;
222 unset($this->datasetdefs[$defkey]);
223 }
83a15d02 224 }
06f80c6c 225 }
83a15d02 226 } // handle reload
227 // create items if $newdataset and noofitems > 0 and !$newdatasetvalues
228 // eliminate any items if $newdatasetvalues
229 // eliminate any items if $datasettoremove, $newdataset, $newdatasetvalues
8200ccd6
TH
230 if ($datasettoremove ||$newdataset ||$newdatasetvalues) {
231 foreach ($this->datasetdefs as $defkey => $datasetdef) {
83a15d02 232 $datasetdef->itemcount = 0;
233 unset($datasetdef->items);
234 }
235 }
8200ccd6 236 $maxnumber = -1;
7d087744 237 if (optional_param('addbutton', false, PARAM_BOOL)) {
06f80c6c 238 $maxnumber = optional_param('selectadd', '', PARAM_INT); //FIXME: sloppy coding
83a15d02 239 foreach ($this->datasetdefs as $defid => $datasetdef) {
240 $datasetdef->itemcount = $maxnumber;
241 unset($datasetdef->items);
8200ccd6 242 for ($numberadded = 1; $numberadded <= $maxnumber; $numberadded++) {
0ff4bd08 243 $datasetitem = new stdClass();
83a15d02 244 $datasetitem->itemnumber = $numberadded;
245 $datasetitem->id = 0;
8200ccd6
TH
246 $datasetitem->value = $this->qtypeobj->generate_dataset_item(
247 $datasetdef->options);
248 $this->datasetdefs[$defid]->items[$numberadded] = $datasetitem;
249 }
250 }
251 $this->maxnumber = $maxnumber;
252 } else {
0735b509 253 // Handle reload dataset items
7d087744 254 if (optional_param_array('definition', '', PARAM_NOTAGS) &&
8200ccd6 255 !($datasettoremove ||$newdataset ||$newdatasetvalues)) {
83a15d02 256 $i = 1;
7d087744
TH
257 $fromformdefinition = optional_param_array('definition', '', PARAM_NOTAGS);
258 $fromformnumber = optional_param_array('number', '', PARAM_INT);
259 $fromformitemid = optional_param_array('itemid', '', PARAM_INT);
83a15d02 260 ksort($fromformdefinition);
aeb15530 261
8200ccd6 262 foreach ($fromformdefinition as $key => $defid) {
83a15d02 263 $addeditem = new stdClass();
8200ccd6 264 $addeditem->id = $fromformitemid[$i];
83a15d02 265 $addeditem->value = $fromformnumber[$i];
266 $addeditem->itemnumber = ceil($i / count($this->datasetdefs));
8200ccd6
TH
267 $this->datasetdefs[$defid]->items[$addeditem->itemnumber] = $addeditem;
268 $this->datasetdefs[$defid]->itemcount = $i;
83a15d02 269 $i++;
270 }
271 }
8200ccd6 272 if (isset($addeditem->itemnumber) && $this->maxnumber < $addeditem->itemnumber) {
83a15d02 273 $this->maxnumber = $addeditem->itemnumber;
8200ccd6 274 if (!empty($this->datasetdefs)) {
83a15d02 275 foreach ($this->datasetdefs as $datasetdef) {
8200ccd6 276 $datasetdef->itemcount = $this->maxnumber;
83a15d02 277 }
278 }
279 }
280 }
281
8200ccd6 282 parent::__construct($submiturl, $question, $category, $contexts, $formeditable);
83a15d02 283 }
284
83a15d02 285 /**
286 * Add question-type specific form fields.
287 *
288 * @param MoodleQuickForm $mform the form being built.
289 */
c7df5006 290 protected function definition_inner($mform) {
83a15d02 291 $strquestionlabel = $this->qtypeobj->comment_header($this->nonemptyanswer);
2fa83505 292 $label = get_string("sharedwildcards", "qtype_calculated");
b003b818 293 $mform->addElement('hidden', 'synchronize', 0);
83a15d02 294 $mform->addElement('hidden', 'initialcategory', 1);
d18e0fe6 295 $mform->setType('initialcategory', PARAM_INT);
83a15d02 296 $mform->addElement('hidden', 'reload', 1);
d18e0fe6 297 $mform->setType('reload', PARAM_INT);
8200ccd6
TH
298 $addfieldsname = 'updatequestion value';
299 $addstring = get_string("updatecategory", "qtype_calculated");
a31697ba 300 $mform->registerNoSubmitButton($addfieldsname);
83a15d02 301
83a15d02 302 $this->add_per_answer_fields($mform, get_string('answerhdr', 'qtype_calculated', '{no}'),
92111e8d 303 question_bank::fraction_options(), 1, 1);
83a15d02 304
efe3e87b
TH
305 $this->add_unit_options($mform, $this);
306 $this->add_unit_fields($mform, $this);
b130270d 307 $this->add_interactive_settings();
aeb15530 308
8200ccd6
TH
309 $label = "<div class='mdl-align'></div><div class='mdl-align'>" .
310 get_string('wildcardrole', 'qtype_calculatedsimple') . "</div>";
83a15d02 311 $mform->addElement('html', "<div class='mdl-align'>&nbsp;</div>");
8200ccd6
TH
312 // explaining the role of datasets so other strings can be shortened
313 $mform->addElement('html', $label);
83a15d02 314
8200ccd6
TH
315 $mform->addElement('submit', 'analyzequestion',
316 get_string('findwildcards', 'qtype_calculatedsimple'));
83a15d02 317 $mform->registerNoSubmitButton('analyzequestion');
318 $mform->closeHeaderBefore('analyzequestion');
72553162 319 $this->wizarddisplay = optional_param('analyzequestion', false, PARAM_BOOL);
8200ccd6 320 if ($this->maxnumber != -1) {
83a15d02 321 $this->noofitems = $this->maxnumber;
322 } else {
323 $this->noofitems = 0;
324 }
8200ccd6 325 if (!empty($this->datasetdefs)) {//So there are some datadefs
fe6ce234 326 // we put them on the page
0735b509 327 $key = 0;
8200ccd6
TH
328 $mform->addElement('header', 'additemhdr',
329 get_string('wildcardparam', 'qtype_calculatedsimple'));
0735b509 330 $idx = 1;
8200ccd6 331 if (!empty($this->datasetdefs)) {// unnecessary test
0735b509 332 $j = (($this->noofitems) * count($this->datasetdefs))+1;//
8200ccd6
TH
333 foreach ($this->datasetdefs as $defkey => $datasetdef) {
334 $mform->addElement('static', "na[$j]",
335 get_string('param', 'qtype_calculated', $datasetdef->name));
0735b509 336 $this->qtypeobj->custom_generator_tools_part($mform, $idx, $j);
337 $mform->addElement('hidden', "datasetdef[$idx]");
338 $mform->setType("datasetdef[$idx]", PARAM_RAW);
b003b818 339 $mform->addElement('hidden', "defoptions[$idx]");
aeb15530 340 $mform->setType("defoptions[$idx]", PARAM_RAW);
0735b509 341 $idx++;
342 $mform->addElement('static', "divider[$j]", '', '<hr />');
343 $j++;
344 }
345 }
8200ccd6 346 //this should be done before the elements are created and stored as $this->formdata;
0735b509 347 //fill out all data sets and also the fields for the next item to add.
fe6ce234
DC
348 /*Here we do already the values error analysis so that
349 * we could force all wild cards values display if there is an error in values.
350 * as using a , in a number */
351 $this->numbererrors = array();
8200ccd6 352 if (!empty($this->datasetdefs)) {
fe6ce234 353 $j = $this->noofitems * count($this->datasetdefs);
8200ccd6 354 for ($itemnumber = $this->noofitems; $itemnumber >= 1; $itemnumber--) {
fe6ce234 355 $data = array();
8200ccd6 356 $numbererrors = array();
0ff4bd08 357 $comment = new stdClass();
fe6ce234 358 $comment->stranswers = array();
8200ccd6 359 $comment->outsidelimit = false;
fe6ce234
DC
360 $comment->answers = array();
361
8200ccd6
TH
362 foreach ($this->datasetdefs as $defid => $datasetdef) {
363 if (isset($datasetdef->items[$itemnumber])) {
fe6ce234 364 $this->formdata["definition[$j]"] = $defid;
8200ccd6
TH
365 $this->formdata["itemid[$j]"] =
366 $datasetdef->items[$itemnumber]->id;
fe6ce234 367 $data[$datasetdef->name] = $datasetdef->items[$itemnumber]->value;
8200ccd6
TH
368 $this->formdata["number[$j]"] = $number =
369 $datasetdef->items[$itemnumber]->value;
370 if (! is_numeric($number)) {
0ff4bd08 371 $a = new stdClass();
8200ccd6
TH
372 $a->name = '{'.$datasetdef->name.'}';
373 $a->value = $datasetdef->items[$itemnumber]->value;
374 if (stristr($number, ',')) {
375 $this->numbererrors["number[$j]"] =
376 get_string('nocommaallowed', 'qtype_calculated');
fe6ce234 377 $numbererrors .= $this->numbererrors['number['.$j.']']."<br />";
aeb15530 378
8200ccd6
TH
379 } else {
380 $this->numbererrors["number[$j]"] =
381 get_string('notvalidnumber', 'qtype_calculated', $a);
0735b509 382 $numbererrors .= $this->numbererrors['number['.$j.']']."<br />";
fe6ce234 383 }
8200ccd6 384 } else if (stristr($number, 'x')) { // hexa will pass the test
0ff4bd08 385 $a = new stdClass();
8200ccd6
TH
386 $a->name = '{'.$datasetdef->name.'}';
387 $a->value = $datasetdef->items[$itemnumber]->value;
388 $this->numbererrors['number['.$j.']'] =
389 get_string('hexanotallowed', 'qtype_calculated', $a);
fe6ce234 390 $numbererrors .= $this->numbererrors['number['.$j.']']."<br />";
8200ccd6 391 } else if (is_nan($number)) {
0ff4bd08 392 $a = new stdClass();
8200ccd6
TH
393 $a->name = '{'.$datasetdef->name.'}';
394 $a->value = $datasetdef->items[$itemnumber]->value;
395 $this->numbererrors["number[$j]"] =
396 get_string('notvalidnumber', 'qtype_calculated', $a);
fe6ce234 397 $numbererrors .= $this->numbererrors['number['.$j.']']."<br />";
0735b509 398 }
0735b509 399 }
fe6ce234
DC
400 $j--;
401 }
8200ccd6 402 if ($this->noofitems != 0) {
fe6ce234
DC
403 if (empty($numbererrors)) {
404 if (!isset($this->question->id)) {
8200ccd6 405 $this->question->id = 0;
fe6ce234 406 }
8200ccd6
TH
407 $this->question->questiontext = !empty($this->question->questiontext) ?
408 $this->question->questiontext : '';
409 $comment = $this->qtypeobj->comment_on_datasetitems(
410 $this->qtypeobj, $this->question->id,
411 $this->question->questiontext, $this->nonemptyanswer,
412 $data, $itemnumber);
fe6ce234 413 if ($comment->outsidelimit) {
8200ccd6 414 $this->outsidelimit = $comment->outsidelimit;
0735b509 415 }
8200ccd6 416 $totalcomment = '';
fe6ce234
DC
417
418 foreach ($this->nonemptyanswer as $key => $answer) {
419 $totalcomment .= $comment->stranswers[$key].'<br/>';
420 }
421
8200ccd6 422 $this->formdata['answercomment['.$itemnumber.']'] = $totalcomment;
fe6ce234 423 }
0735b509 424 }
fe6ce234
DC
425 }
426 $this->formdata['selectdelete'] = '1';
427 $this->formdata['selectadd'] = '1';
428 $j = $this->noofitems * count($this->datasetdefs)+1;
429 $data = array(); // data for comment_on_datasetitems later
8200ccd6
TH
430 $idx = 1;
431 foreach ($this->datasetdefs as $defid => $datasetdef) {
0735b509 432 $this->formdata["datasetdef[$idx]"] = $defid;
433 $idx++;
434 }
8200ccd6
TH
435 $this->formdata = $this->qtypeobj->custom_generator_set_data(
436 $this->datasetdefs, $this->formdata);
fe6ce234
DC
437 }
438
fe6ce234 439 $addoptions = Array();
8200ccd6
TH
440 $addoptions['1'] = '1';
441 for ($i = 10; $i <= 100; $i += 10) {
442 $addoptions["$i"] = "$i";
fe6ce234
DC
443 }
444 $showoptions = Array();
8200ccd6
TH
445 $showoptions['1'] = '1';
446 $showoptions['2'] = '2';
447 $showoptions['5'] = '5';
448 for ($i = 10; $i <= 100; $i += 10) {
449 $showoptions["$i"] = "$i";
fe6ce234
DC
450 }
451 $mform->closeHeaderBefore('additemhdr');
452 $addgrp = array();
8200ccd6
TH
453 $addgrp[] = $mform->createElement('submit', 'addbutton',
454 get_string('generatenewitemsset', 'qtype_calculatedsimple'));
455 $addgrp[] = $mform->createElement('select', "selectadd", '', $addoptions);
456 $addgrp[] = $mform->createElement('static', "stat", '',
457 get_string('newsetwildcardvalues', 'qtype_calculatedsimple'));
fe6ce234
DC
458 $mform->addGroup($addgrp, 'addgrp', '', ' ', false);
459 $mform->registerNoSubmitButton('addbutton');
460 $mform->closeHeaderBefore('addgrp');
461 $addgrp1 = array();
8200ccd6
TH
462 $addgrp1[] = $mform->createElement('submit', 'showbutton',
463 get_string('showitems', 'qtype_calculatedsimple'));
464 $addgrp1[] = $mform->createElement('select', "selectshow", '', $showoptions);
465 $addgrp1[] = $mform->createElement('static', "stat", '',
466 get_string('setwildcardvalues', 'qtype_calculatedsimple'));
fe6ce234
DC
467 $mform->addGroup($addgrp1, 'addgrp1', '', ' ', false);
468 $mform->registerNoSubmitButton('showbutton');
469 $mform->closeHeaderBefore('addgrp1');
470 $mform->addElement('static', "divideradd", '', '');
471 if ($this->noofitems == 0) {
8200ccd6
TH
472 $mform->addElement('static', 'warningnoitems', '', '<span class="error">' .
473 get_string('youmustaddatleastonevalue', 'qtype_calculatedsimple') .
474 '</span>');
fe6ce234 475 $mform->closeHeaderBefore('warningnoitems');
8200ccd6
TH
476 } else {
477 $mform->addElement('header', 'additemhdr1',
478 get_string('wildcardvalues', 'qtype_calculatedsimple'));
fe6ce234 479 $mform->closeHeaderBefore('additemhdr1');
8200ccd6
TH
480 if (!empty($this->numbererrors) || $this->outsidelimit) {
481 $mform->addElement('static', "alert", '', '<span class="error">' .
482 get_string('useadvance', 'qtype_calculatedsimple').'</span>');
0735b509 483 }
484
8200ccd6
TH
485 $mform->addElement('submit', 'updatedatasets',
486 get_string('updatewildcardvalues', 'qtype_calculatedsimple'));
fe6ce234 487 $mform->registerNoSubmitButton('updatedatasets');
8200ccd6 488 $mform->setAdvanced("updatedatasets", true);
aeb15530 489
8200ccd6 490 //--------------------------------------------------------------
fe6ce234 491 $j = $this->noofitems * count($this->datasetdefs);
7d087744 492 $k = optional_param('selectshow', 1, PARAM_INT);
aeb15530 493
8200ccd6
TH
494 for ($i = $this->noofitems; $i >= 1; $i--) {
495 foreach ($this->datasetdefs as $defkey => $datasetdef) {
496 if ($k > 0 || $this->outsidelimit || !empty($this->numbererrors)) {
497 $mform->addElement('text', "number[$j]", get_string(
498 'wildcard', 'qtype_calculatedsimple', $datasetdef->name));
499 $mform->setAdvanced("number[$j]", true);
500 if (!empty($this->numbererrors['number['.$j.']'])) {
501 $mform->addElement('static', "numbercomment[$j]", '',
502 '<span class="error">' .
503 $this->numbererrors['number['.$j.']'] . '</span>');
504 $mform->setAdvanced("numbercomment[$j]", true);
fe6ce234 505 }
8200ccd6
TH
506 } else {
507 $mform->addElement('hidden', "number[$j]", get_string(
508 'wildcard', 'qtype_calculatedsimple', $datasetdef->name));
fe6ce234
DC
509 }
510 $mform->setType("number[$j]", PARAM_NUMBER);
83a15d02 511
fe6ce234
DC
512 $mform->addElement('hidden', "itemid[$j]");
513 $mform->setType("itemid[$j]", PARAM_INT);
a31697ba 514
fe6ce234
DC
515 $mform->addElement('hidden', "definition[$j]");
516 $mform->setType("definition[$j]", PARAM_NOTAGS);
83a15d02 517
fe6ce234
DC
518 $j--;
519 }
8200ccd6
TH
520 if (!empty($strquestionlabel) && ($k > 0 || $this->outsidelimit ||
521 !empty($this->numbererrors))) {
522 $mform->addElement('static', "answercomment[$i]", "<b>" .
523 get_string('setno', 'qtype_calculatedsimple', $i) .
524 "</b>&nbsp;&nbsp;" . $strquestionlabel);
aeb15530 525
fe6ce234 526 }
8200ccd6 527 if ($k > 0 || $this->outsidelimit || !empty($this->numbererrors)) {
fe6ce234 528 $mform->addElement('static', "divider1[$j]", '', '<hr />');
aeb15530 529
fe6ce234 530 }
8200ccd6 531 $k--;
fe6ce234
DC
532 }
533 }
8200ccd6
TH
534 } else {
535 $mform->addElement('static', 'warningnowildcards', '', '<span class="error">' .
536 get_string('atleastonewildcard', 'qtype_calculatedsimple') . '</span>');
fe6ce234 537 $mform->closeHeaderBefore('warningnowildcards');
83a15d02 538 }
83a15d02 539
8200ccd6
TH
540 //----------------------------------------------------------------------
541 // Non standard name for button element needed so not using add_action_buttons
542 // hidden elements
83a15d02 543
544 $mform->addElement('hidden', 'id');
545 $mform->setType('id', PARAM_INT);
546
547 $mform->addElement('hidden', 'courseid');
548 $mform->setType('courseid', PARAM_INT);
549 $mform->setDefault('courseid', 0);
550
551 $mform->addElement('hidden', 'cmid');
552 $mform->setType('cmid', PARAM_INT);
553 $mform->setDefault('cmid', 0);
8200ccd6
TH
554 if (!empty($this->question->id)) {
555 if ($this->question->formoptions->cansaveasnew) {
556 $mform->addElement('header', 'additemhdr',
557 get_string('converttocalculated', 'qtype_calculatedsimple'));
fe6ce234 558 $mform->closeHeaderBefore('additemhdr');
aeb15530 559
8200ccd6
TH
560 $mform->addElement('checkbox', 'convert', '',
561 get_string('willconverttocalculated', 'qtype_calculatedsimple'));
fe6ce234 562 $mform->setDefault('convert', 0);
83a15d02 563
83a15d02 564 }
fe6ce234 565 }
83a15d02 566 }
567
8200ccd6 568 public function data_preprocessing($question) {
5cf69d7f
TH
569 $question = parent::data_preprocessing($question);
570 $question = $this->data_preprocessing_answers($question);
571 $question = $this->data_preprocessing_hints($question);
efe3e87b
TH
572 $question = $this->data_preprocessing_units($question);
573 $question = $this->data_preprocessing_unit_options($question);
574
5cf69d7f
TH
575 // This is a bit ugly, but it loads all the dataset values.
576 $question = (object)((array)$question + $this->formdata);
577
fe6ce234 578 return $question;
83a15d02 579 }
580
8200ccd6 581 public function qtype() {
83a15d02 582 return 'calculatedsimple';
583 }
584
8200ccd6 585 public function validation($data, $files) {
83a15d02 586 $errors = parent::validation($data, $files);
83a15d02 587
e6d76583
TH
588 if (empty($data['definition'])) {
589 $errors['selectadd'] = get_string('youmustaddatleastonevalue', 'qtype_calculatedsimple');
590 }
591
83a15d02 592 return $errors;
593 }
594}