MDL-10110 Creating the calculatedmulti question type
[moodle.git] / question / type / calculated / edit_calculated_form.php
CommitLineData
aeb15530 1<?php
271ffe3f 2/**
3 * Defines the editing form for the calculated question type.
4 *
5 * @copyright &copy; 2007 Jamie Pratt
6 * @author Jamie Pratt me@jamiep.org
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9e20fbc2 8 * @package questionbank
9 * @subpackage questiontypes
271ffe3f 10 */
11
12/**
13 * calculated editing form definition.
14 */
15class question_edit_calculated_form extends question_edit_form {
9aa022fe 16 /**
17 * Handle to the question type for this question.
18 *
19 * @var question_calculated_qtype
20 */
21 var $qtypeobj;
2aef1fe5 22
28a27ef1 23 /**
24 * Get the list of form elements to repeat, one for each answer.
25 * @param object $mform the form being built.
26 * @param $label the label to use for each option.
27 * @param $gradeoptions the possible grades for each answer.
28 * @param $repeatedoptions reference to array of repeated options to fill
29 * @param $answersoption reference to return the name of $question->options field holding an array of answers
30 * @return array of form fields.
31 */
32 /* function get_per_answer_fields(&$mform, $label, $gradeoptions, &$repeatedoptions, &$answersoption) {
33 $repeated = array();
34 $repeated[] =& $mform->createElement('header', 'answerhdr', $label);
35 $repeated[] =& $mform->createElement('text', 'answer', get_string('answer', 'quiz'), array('size' => 50));
36 $repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
37 $repeated[] =& $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz'),
38 array('course' => $this->coursefilesid));
39 $repeatedoptions['answer']['type'] = PARAM_RAW;
40 $repeatedoptions['fraction']['default'] = 0;
41 $answersoption = 'answers';
42 return $repeated;
43 }*/
2aef1fe5 44 function get_per_answer_fields(&$mform, $label, $gradeoptions, &$repeatedoptions, &$answersoption) {
28a27ef1 45 // $repeated = parent::get_per_answer_fields($mform, $label, $gradeoptions, $repeatedoptions, $answersoption);
46 $repeated = array();
47 $repeated[] =& $mform->createElement('header', 'answerhdr', $label);
48 // if ($this->editasmultichoice == 1){
49 $repeated[] =& $mform->createElement('text', 'answer', get_string('answer', 'quiz'), array('size' => 50));
50 $repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
51 $repeated[] =& $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz'),
52 array('course' => $this->coursefilesid));
53 $repeatedoptions['answer']['type'] = PARAM_RAW;
54 $repeatedoptions['fraction']['default'] = 0;
55 $answersoption = 'answers';
56
2aef1fe5 57 $mform->setType('answer', PARAM_NOTAGS);
58
59 $addrepeated = array();
28a27ef1 60 if ($this->editasmultichoice == 1){
61 $addrepeated[] =& $mform->createElement('hidden', 'tolerance');
1b41f2b0 62 $addrepeated[] =& $mform->createElement('hidden', 'tolerancetype',1);
28a27ef1 63 }else {
64 $addrepeated[] =& $mform->createElement('text', 'tolerance', get_string('tolerance', 'qtype_calculated'));
65 $addrepeated[] =& $mform->createElement('select', 'tolerancetype', get_string('tolerancetype', 'quiz'), $this->qtypeobj->tolerance_types());
aeb15530 66 }
2aef1fe5 67 $repeatedoptions['tolerance']['type'] = PARAM_NUMBER;
68 $repeatedoptions['tolerance']['default'] = 0.01;
2aef1fe5 69
70 $addrepeated[] =& $mform->createElement('select', 'correctanswerlength', get_string('correctanswershows', 'qtype_calculated'), range(0, 9));
71 $repeatedoptions['correctanswerlength']['default'] = 2;
72
73 $answerlengthformats = array('1' => get_string('decimalformat', 'quiz'), '2' => get_string('significantfiguresformat', 'quiz'));
74 $addrepeated[] =& $mform->createElement('select', 'correctanswerformat', get_string('correctanswershowsformat', 'qtype_calculated'), $answerlengthformats);
75 array_splice($repeated, 3, 0, $addrepeated);
28a27ef1 76 if ($this->editasmultichoice == 1){
aeb15530 77 $repeated[1]->setLabel('...<strong>{={x}+..}</strong>...');
28a27ef1 78 }else {
79 $repeated[1]->setLabel(get_string('correctanswerformula', 'quiz').'=');
80
81 }
2aef1fe5 82
83 return $repeated;
84 }
85
271ffe3f 86 /**
87 * Add question-type specific form fields.
88 *
9aa022fe 89 * @param MoodleQuickForm $mform the form being built.
271ffe3f 90 */
91 function definition_inner(&$mform) {
9aa022fe 92 global $QTYPES;
92186abc 93 $this->qtypeobj =& $QTYPES[$this->qtype()];
aeb15530 94 // echo code left for testing period
99a77ee5 95 // echo "<p>question ".optional_param('multichoice', '', PARAM_RAW)." optional<pre>";print_r($this->question);echo "</pre></p>";
a6d46515 96 $label = get_string("sharedwildcards", "qtype_datasetdependent");
97 $mform->addElement('hidden', 'initialcategory', 1);
d18e0fe6 98 $mform->setType('initialcategory', PARAM_INT);
a6d46515 99 $html2 = $this->qtypeobj->print_dataset_definitions_category($this->question);
100 $mform->insertElementBefore($mform->createElement('static','listcategory',$label,$html2),'name');
101 $addfieldsname='updatecategory';
102 $addstring=get_string("updatecategory", "qtype_calculated");
103 $mform->registerNoSubmitButton($addfieldsname);
28a27ef1 104 $this->editasmultichoice = 0 ;
105 if ( isset($this->question->options->multichoice) && $this->question->options->multichoice == '1'){
aeb15530 106 $this->editasmultichoice = 1 ;
28a27ef1 107 }else {
1b41f2b0 108 if ( !isset($this->question->id ) && 1 == optional_param('multichoice', '', PARAM_RAW )){
109 $this->editasmultichoice = 1 ;
110 }else {
111 $this->editasmultichoice = 0 ;
112 }
28a27ef1 113 if ( !isset($this->question->id ) && '' != optional_param('createoptionbutton', '', PARAM_RAW) && 1 == optional_param('multichoice', '', PARAM_RAW)){
114 $this->editasmultichoice = 1 ;
115 }
116 if ( !isset($this->question->id )== 0 && '' != optional_param('createoptionbutton', '', PARAM_RAW) && 0 == optional_param('multichoice', '', PARAM_RAW)){
117 $this->editasmultichoice = 0 ;
118 }
119 }
aeb15530 120
28a27ef1 121 /* if ( '' != optional_param('changetomultichoice', '', PARAM_RAW)){
aeb15530 122 $this->editasmultichoice = 1 ;
28a27ef1 123 }
124 if ( '' != optional_param('changetocalculated', '', PARAM_RAW)){
aeb15530 125 $this->editasmultichoice = 0 ;
28a27ef1 126 }*/
271e6dec 127
a6d46515 128 $mform->insertElementBefore( $mform->createElement('submit', $addfieldsname, $addstring),'listcategory');
28a27ef1 129 $mform->registerNoSubmitButton('createoptionbutton');
aeb15530 130
28a27ef1 131 if(!isset($this->question->id ) ){
132 $mform->addElement('header', 'choicehdr',get_string('Choosingcreationmode', 'qtype_calculated'));
133 $createoptions = Array();
aeb15530 134
28a27ef1 135 $createoptions['0']=get_string('Regularcalculated', 'qtype_calculated');
136 $createoptions['1']=get_string('Multiplechoicecalculated', 'qtype_calculated');
137 $addgrp1 = array();
138 $addgrp1[] =& $mform->createElement('submit', 'createoptionbutton', get_string('Createas', 'qtype_calculatedsimple'));
139 $addgrp1[] =& $mform->createElement('select', "multichoice",'' , $createoptions);
140 $mform->addGroup($addgrp1, 'addgrp1', '', ' ', false);
141 }else {
142 $mform->addElement('hidden', 'multichoice',$this->editasmultichoice);
d18e0fe6 143 $mform->setType('multichoice', PARAM_INT);
28a27ef1 144 }
145
146 if ($this->editasmultichoice == 1){
147 $mform->addElement('header', 'choicehdr',get_string('multichoicecalculatedquestion', 'qtype_calculated'));
148 $menu = array(get_string('answersingleno', 'qtype_multichoice'), get_string('answersingleyes', 'qtype_multichoice'));
149 $mform->addElement('select', 'single', get_string('answerhowmany', 'qtype_multichoice'), $menu);
150 $mform->setDefault('single', 1);
aeb15530 151
28a27ef1 152 $mform->addElement('advcheckbox', 'shuffleanswers', get_string('shuffleanswers', 'qtype_multichoice'), null, null, array(0,1));
153 $mform->setHelpButton('shuffleanswers', array('multichoiceshuffle', get_string('shuffleanswers','qtype_multichoice'), 'qtype_multichoice'));
154 $mform->setDefault('shuffleanswers', 1);
aeb15530 155
28a27ef1 156 $numberingoptions = $QTYPES['multichoice']->get_numbering_styles();
157 $menu = array();
158 foreach ($numberingoptions as $numberingoption) {
159 $menu[$numberingoption] = get_string('answernumbering' . $numberingoption, 'qtype_multichoice');
160 }
161 $mform->addElement('select', 'answernumbering', get_string('answernumbering', 'qtype_multichoice'), $menu);
162 $mform->setDefault('answernumbering', 'abc');
163 }else { //editing as regular
164 $mform->addElement('header', 'choicehdr', get_string('regularcalculatedquestion', 'qtype_calculated'));
165 $mform->addElement('hidden','single', '1');
d18e0fe6 166 $mform->setType('single', PARAM_INT);
aeb15530 167
28a27ef1 168 $mform->addElement('hidden','shuffleanswers', '1');
d18e0fe6 169 $mform->setType('shuffleanswers', PARAM_INT);
170 $mform->addElement('hidden','answernumbering', 'abc');
171 $mform->setType('answernumbering', PARAM_SAFEDIR);
28a27ef1 172 }
271ffe3f 173
271ffe3f 174 $creategrades = get_grade_options();
28a27ef1 175 if ($this->editasmultichoice == 1){
176 $this->add_per_answer_fields($mform, get_string('choiceno', 'qtype_multichoice', '{no}'),
177 $creategrades->gradeoptionsfull, max(5, QUESTION_NUMANS_START));
178 }else{
179 $this->add_per_answer_fields($mform, get_string('answerhdr', 'qtype_calculated', '{no}'),
2aef1fe5 180 $creategrades->gradeoptions, 1, 1);
28a27ef1 181 }
aeb15530 182
92186abc 183
271e6dec 184 $repeated = array();
28a27ef1 185 if ($this->editasmultichoice == 1){
186 $nounits = optional_param('nounits', 1, PARAM_INT);
187 $mform->addElement('hidden', 'nounits', $nounits);
d18e0fe6 188 $mform->setType('nounits', PARAM_INT);
28a27ef1 189 $mform->setConstants(array('nounits'=>$nounits));
190 for ($i=0; $i< $nounits; $i++) {
d18e0fe6 191 $mform->addElement('hidden','unit'."[$i]", optional_param('unit'."[$i]", '', PARAM_NOTAGS));
aeb15530 192 $mform->setType('unit'."[$i]", PARAM_NOTAGS);
d18e0fe6 193 $mform->addElement('hidden', 'multiplier'."[$i]", optional_param('multiplier'."[$i]", '', PARAM_NUMBER));
194 $mform->setType('multiplier'."[$i]", PARAM_NUMBER);
aeb15530 195 }
b003b818 196 $mform->addElement('hidden','unitgradingtype',optional_param('unitgradingtype', '', PARAM_INT)) ;
197 $mform->addElement('hidden','unitpenalty',optional_param('unitpenalty', '', PARAM_NUMBER)) ;
198 $mform->addElement('hidden','showunits',optional_param('showunits', '', PARAM_INT)) ;
aeb15530
PS
199 $mform->addElement('hidden','unitsleft',optional_param('unitsleft', '', PARAM_INT)) ;
200 $mform->addElement('hidden','instructions',optional_param('instructions', '', PARAM_RAW)) ;
271ffe3f 201
28a27ef1 202 }else {
b003b818 203 $QTYPES['numerical']->edit_numerical_options($mform,$this);
aeb15530 204 $repeated[] =& $mform->createElement('header', 'unithdr', get_string('unithdr', 'qtype_numerical', '{no}'));
28a27ef1 205 $repeated[] =& $mform->createElement('text', 'unit', get_string('unit', 'quiz'));
206 $repeated[] =& $mform->createElement('text', 'multiplier', get_string('multiplier', 'quiz'));
aeb15530 207
3a298174 208 $mform->setType('unit', PARAM_NOTAGS);
271ffe3f 209
3a298174 210 $mform->setType('multiplier', PARAM_NUMBER);
9aa022fe 211
212 if (isset($this->question->options)){
213 $countunits = count($this->question->options->units);
214 } else {
215 $countunits = 0;
216 }
271e6dec 217 if ($this->question->formoptions->repeatelements){
218 $repeatsatstart = $countunits + 1;
219 } else {
220 $repeatsatstart = $countunits;
221 }
3a298174 222 $this->repeat_elements($repeated, $repeatsatstart, array(), 'nounits', 'addunits', 2, get_string('addmoreunitblanks', 'qtype_calculated', '{no}'));
271ffe3f 223
28a27ef1 224 }
79bb7202 225 if ($mform->elementExists('multiplier[0]')){
226 $firstunit =& $mform->getElement('multiplier[0]');
227 $firstunit->freeze();
228 $firstunit->setValue('1.0');
229 $firstunit->setPersistantFreeze(true);
230 }
28a27ef1 231 if ($this->editasmultichoice == 1){
232 $mform->setType('addunits','hidden');
233 $mform->addElement('header', 'overallfeedbackhdr', get_string('overallfeedback', 'qtype_multichoice'));
234
235 foreach (array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback') as $feedbackname) {
236 $mform->addElement('htmleditor', $feedbackname, get_string($feedbackname, 'qtype_multichoice'),
237 array('course' => $this->coursefilesid));
238 $mform->setType($feedbackname, PARAM_RAW);
239 }
240 }else {
241 foreach (array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback') as $feedbackname) {
242 $mform->addElement('hidden', $feedbackname);
243 $mform->setType($feedbackname, PARAM_RAW);
244 }
245 }
9aa022fe 246 //hidden elements
e593233a 247 $mform->addElement('hidden', 'synchronize', '');
d18e0fe6 248 $mform->setType('synchronize', PARAM_INT);
e593233a 249 if (isset($this->question->options)&& isset($this->question->options->synchronize) ){
250 $mform->setDefault("synchronize", $this->question->options->synchronize);
251 } else {
252 $mform->setDefault("synchronize", 0 );
253 }
60b5ecd3 254 $mform->addElement('hidden', 'wizard', 'datasetdefinitions');
255 $mform->setType('wizard', PARAM_ALPHA);
92186abc 256
271ffe3f 257
258 }
259
32db0d42 260 function set_data($question) {
28a27ef1 261 if (isset($this->editasmultichoice)){
b003b818 262 $default_values['multichoice']= $this->editasmultichoice ; //$this->editasmultichoice ;
28a27ef1 263 }
271ffe3f 264 if (isset($question->options)){
265 $answers = $question->options->answers;
266 if (count($answers)) {
267 $key = 0;
268 foreach ($answers as $answer){
9af77e9d 269 $default_values['answer['.$key.']'] = $answer->answer;
92186abc 270 $default_values['fraction['.$key.']'] = $answer->fraction;
271 $default_values['tolerance['.$key.']'] = $answer->tolerance;
f7089b63 272 $default_values['tolerancetype['.$key.']'] = $answer->tolerancetype;
92186abc 273 $default_values['correctanswerlength['.$key.']'] = $answer->correctanswerlength;
9aa022fe 274 $default_values['correctanswerformat['.$key.']'] = $answer->correctanswerformat;
271ffe3f 275 $default_values['feedback['.$key.']'] = $answer->feedback;
276 $key++;
277 }
278 }
b003b818 279 $default_values['unitgradingtype'] = $question->options->unitgradingtype ;
280 $default_values['unitpenalty'] = $question->options->unitpenalty ;
281 $default_values['showunits'] = $question->options->showunits ;
282 $default_values['unitsleft'] = $question->options->unitsleft ;
283 $default_values['instructions'] = $question->options->instructions ;
284
28a27ef1 285 if (isset($question->options->units)){
286 $units = array_values($question->options->units);
287 // make sure the default unit is at index 0
288 usort($units, create_function('$a, $b',
289 'if (1.0 === (float)$a->multiplier) { return -1; } else '.
290 'if (1.0 === (float)$b->multiplier) { return 1; } else { return 0; }'));
291 if (count($units)) {
292 $key = 0;
293 foreach ($units as $unit){
294 $default_values['unit['.$key.']'] = $unit->unit;
295 $default_values['multiplier['.$key.']'] = $unit->multiplier;
296 $key++;
297 }
9aa022fe 298 }
299 }
92186abc 300 }
28a27ef1 301 if (isset($question->options->single)){
302 $default_values['single'] = $question->options->single;
303 $default_values['answernumbering'] = $question->options->answernumbering;
304 $default_values['shuffleanswers'] = $question->options->shuffleanswers;
305 $default_values['correctfeedback'] = $question->options->correctfeedback;
306 $default_values['partiallycorrectfeedback'] = $question->options->partiallycorrectfeedback;
307 $default_values['incorrectfeedback'] = $question->options->incorrectfeedback;
308 }
8fc3e643 309 $default_values['submitbutton'] = get_string('nextpage', 'qtype_calculated');
310 $default_values['makecopy'] = get_string('makecopynextpage', 'qtype_calculated');
271e6dec 311 /* set the wild cards category display given that on loading the category element is
312 unselected when processing this function but have a valid value when processing the
0dd3e11c 313 update category button. The value can be obtain by
314 $qu->category =$this->_form->_elements[$this->_form->_elementIndex['category']]->_values[0];
315 but is coded using existing functions
271e6dec 316 */
0dd3e11c 317 $qu = new stdClass;
318 $el = new stdClass;
271e6dec 319 /* no need to call elementExists() here */
79bb7202 320 if ($this->_form->elementExists('category')){
321 $el=$this->_form->getElement('category');
322 } else {
323 $el=$this->_form->getElement('categorymoveto');
324 }
0dd3e11c 325 if($value =$el->getSelected()) {
326 $qu->category =$value[0];
327 }else {
328 $qu->category=$question->category;// on load $question->category is set by question.php
271e6dec 329 }
0dd3e11c 330 $html2 = $this->qtypeobj->print_dataset_definitions_category($qu);
79bb7202 331 $this->_form->_elements[$this->_form->_elementIndex['listcategory']]->_text = $html2 ;
332 $question = (object)((array)$question + $default_values);
92186abc 333
32db0d42 334 parent::set_data($question);
271ffe3f 335 }
336
337 function qtype() {
338 return 'calculated';
339 }
340
a78890d5 341 function validation($data, $files) {
aeb15530 342 // echo code left for testing period
28a27ef1 343
344 // echo "<p>question <pre>";print_r($this->question);echo "</pre></p>";
345 // echo "<p>data <pre>";print_r($data);echo "</pre></p>";
346
fe93ba83 347 $errors = parent::validation($data, $files);
bfdc0bce 348 //verifying for errors in {=...} in question text;
349 $qtext = "";
350 $qtextremaining = $data['questiontext'] ;
351 $possibledatasets = $this->qtypeobj->find_dataset_names($data['questiontext']);
352 foreach ($possibledatasets as $name => $value) {
353 $qtextremaining = str_replace('{'.$name.'}', '1', $qtextremaining);
354 }
271e6dec 355 // echo "numericalquestion qtextremaining <pre>";print_r($possibledatasets);
6dbcacee 356 while (preg_match('~\{=([^[:space:]}]*)}~', $qtextremaining, $regs1)) {
bfdc0bce 357 $qtextsplits = explode($regs1[0], $qtextremaining, 2);
358 $qtext =$qtext.$qtextsplits[0];
359 $qtextremaining = $qtextsplits[1];
360 if (!empty($regs1[1]) && $formulaerrors = qtype_calculated_find_formula_errors($regs1[1])) {
361 if(!isset($errors['questiontext'])){
362 $errors['questiontext'] = $formulaerrors.':'.$regs1[1] ;
363 }else {
364 $errors['questiontext'] .= '<br/>'.$formulaerrors.':'.$regs1[1];
271e6dec 365 }
bfdc0bce 366 }
271e6dec 367 }
9af77e9d 368 $answers = $data['answer'];
271ffe3f 369 $answercount = 0;
a6d46515 370 $maxgrade = false;
f6232d58 371 $possibledatasets = $this->qtypeobj->find_dataset_names($data['questiontext']);
372 $mandatorydatasets = array();
373 foreach ($answers as $key => $answer){
a6d46515 374 $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer);
271e6dec 375 }
a6d46515 376 if ( count($mandatorydatasets )==0){
377 // $errors['questiontext']=get_string('atleastonewildcard', 'qtype_datasetdependent');
f6232d58 378 foreach ($answers as $key => $answer){
9af77e9d 379 $errors['answer['.$key.']'] = get_string('atleastonewildcard', 'qtype_datasetdependent');
271e6dec 380 }
381 }
28a27ef1 382 if ($data['multichoice']== 1 ){
383 foreach ($answers as $key => $answer){
384 $trimmedanswer = trim($answer);
aeb15530 385 if (($trimmedanswer!='')||$answercount==0){
99a77ee5 386 //verifying for errors in {=...} in answer text;
387 $qanswer = "";
388 $qanswerremaining = $trimmedanswer ;
389 $possibledatasets = $this->qtypeobj->find_dataset_names($trimmedanswer);
390 foreach ($possibledatasets as $name => $value) {
391 $qanswerremaining = str_replace('{'.$name.'}', '1', $qanswerremaining);
392 }
393 // echo "numericalquestion qanswerremaining <pre>";print_r($possibledatasets);
394 while (preg_match('~\{=([^[:space:]}]*)}~', $qanswerremaining, $regs1)) {
395 $qanswersplits = explode($regs1[0], $qanswerremaining, 2);
396 $qanswer =$qanswer.$qanswersplits[0];
397 $qanswerremaining = $qanswersplits[1];
398 if (!empty($regs1[1]) && $formulaerrors = qtype_calculated_find_formula_errors($regs1[1])) {
399 if(!isset($errors['answer['.$key.']'])){
400 $errors['answer['.$key.']'] = $formulaerrors.':'.$regs1[1] ;
401 }else {
402 $errors['answer['.$key.']'] .= '<br/>'.$formulaerrors.':'.$regs1[1];
403 }
28a27ef1 404 }
405 }
406 }
99a77ee5 407 if ($trimmedanswer!=''){
408 if ('2' == $data['correctanswerformat'][$key]
409 && '0' == $data['correctanswerlength'][$key]) {
410 $errors['correctanswerlength['.$key.']'] = get_string('zerosignificantfiguresnotallowed','quiz');
411 }
412 if (!is_numeric($data['tolerance'][$key])){
413 $errors['tolerance['.$key.']'] = get_string('mustbenumeric', 'qtype_calculated');
414 }
415 if ($data['fraction'][$key] == 1) {
416 $maxgrade = true;
417 }
aeb15530 418
99a77ee5 419 $answercount++;
28a27ef1 420 }
99a77ee5 421 //check grades
422 if ($answer != '') {
423 if ($data['fraction'][$key] > 0) {
424 $totalfraction += $data['fraction'][$key];
425 }
426 if ($data['fraction'][$key] > $maxfraction) {
427 $maxfraction = $data['fraction'][$key];
428 }
aeb15530 429 }
28a27ef1 430 }
99a77ee5 431 if ($answercount==0){
432 $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
433 $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
434 } elseif ($answercount==1){
435 $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
aeb15530 436
28a27ef1 437 }
99a77ee5 438
439 /// Perform sanity checks on fractional grades
440 if ($data['single']) {
441 if ($maxfraction != 1) {
442 $maxfraction = $maxfraction * 100;
443 $errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_multichoice', $maxfraction);
9aa022fe 444 }
99a77ee5 445 } else {
446 $totalfraction = round($totalfraction,2);
447 if ($totalfraction != 1) {
448 $totalfraction = $totalfraction * 100;
449 $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction);
9aa022fe 450 }
99a77ee5 451 }
452 }else{ // regular calculated
453 foreach ($answers as $key => $answer){
454 //check no of choices
455 // the * for everykind of answer not actually implemented
456 $trimmedanswer = trim($answer);
457 if (($trimmedanswer!='')||$answercount==0){
458 $eqerror = qtype_calculated_find_formula_errors($trimmedanswer);
459 if (FALSE !== $eqerror){
460 $errors['answer['.$key.']'] = $eqerror;
461 }
9aa022fe 462 }
99a77ee5 463 if ($trimmedanswer!=''){
464 if ('2' == $data['correctanswerformat'][$key]
465 && '0' == $data['correctanswerlength'][$key]) {
466 $errors['correctanswerlength['.$key.']'] = get_string('zerosignificantfiguresnotallowed','quiz');
467 }
468 if (!is_numeric($data['tolerance'][$key])){
469 $errors['tolerance['.$key.']'] = get_string('mustbenumeric', 'qtype_calculated');
470 }
471 if ($data['fraction'][$key] == 1) {
472 $maxgrade = true;
473 }
aeb15530 474
99a77ee5 475 $answercount++;
a6d46515 476 }
99a77ee5 477 //check grades
aeb15530 478
99a77ee5 479 //TODO how should grade checking work here??
480 /*if ($answer != '') {
481 if ($data['fraction'][$key] > 0) {
482 $totalfraction += $data['fraction'][$key];
483 }
484 if ($data['fraction'][$key] > $maxfraction) {
485 $maxfraction = $data['fraction'][$key];
486 }
487 }*/
271ffe3f 488 }
aeb15530 489
99a77ee5 490 //grade checking :
491 /// Perform sanity checks on fractional grades
492 /*if ( ) {
493 if ($maxfraction != 1) {
494 $maxfraction = $maxfraction * 100;
495 $errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_multichoice', $maxfraction);
9aa022fe 496 }
99a77ee5 497 } else {
498 $totalfraction = round($totalfraction,2);
499 if ($totalfraction != 1) {
500 $totalfraction = $totalfraction * 100;
501 $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction);
9aa022fe 502 }
503 }*/
99a77ee5 504 $units = $data['unit'];
505 if (count($units)) {
506 foreach ($units as $key => $unit){
507 if (is_numeric($unit)){
508 $errors['unit['.$key.']'] = get_string('mustnotbenumeric', 'qtype_calculated');
9aa022fe 509 }
99a77ee5 510 $trimmedunit = trim($unit);
511 $trimmedmultiplier = trim($data['multiplier'][$key]);
512 if (!empty($trimmedunit)){
513 if (empty($trimmedmultiplier)){
514 $errors['multiplier['.$key.']'] = get_string('youmustenteramultiplierhere', 'qtype_calculated');
515 }
516 if (!is_numeric($trimmedmultiplier)){
517 $errors['multiplier['.$key.']'] = get_string('mustbenumeric', 'qtype_calculated');
518 }
aeb15530 519
9aa022fe 520 }
9aa022fe 521 }
522 }
99a77ee5 523 if ($answercount==0){
524 $errors['answer[0]'] = get_string('atleastoneanswer', 'qtype_calculated');
525 }
526 if ($maxgrade == false) {
527 $errors['fraction[0]'] = get_string('fractionsnomax', 'question');
528 }
a6d46515 529 }
9aa022fe 530
271ffe3f 531 return $errors;
532 }
533}
aeb15530 534