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