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