My Moodle: MDL-19089 Course limit for My Moodle not propperly obeyed. Fixing so that...
[moodle.git] / question / type / calculated / edit_calculated_form.php
CommitLineData
4d41f4ee 1<?php // $Id$
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());
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){
77 $repeated[1]->setLabel('...<strong>{={x}+..}</strong>...');
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()];
28a27ef1 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'){
106 $this->editasmultichoice = 1 ;
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 }
120
121 /* if ( '' != optional_param('changetomultichoice', '', PARAM_RAW)){
122 $this->editasmultichoice = 1 ;
123 }
124 if ( '' != optional_param('changetocalculated', '', PARAM_RAW)){
125 $this->editasmultichoice = 0 ;
126 }*/
271e6dec 127
a6d46515 128 $mform->insertElementBefore( $mform->createElement('submit', $addfieldsname, $addstring),'listcategory');
28a27ef1 129 $mform->registerNoSubmitButton('createoptionbutton');
130
131 if(!isset($this->question->id ) ){
132 $mform->addElement('header', 'choicehdr',get_string('Choosingcreationmode', 'qtype_calculated'));
133 $createoptions = Array();
134
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);
151
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);
155
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);
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 }
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));
192 $mform->setType('unit'."[$i]", PARAM_NOTAGS);
193 $mform->addElement('hidden', 'multiplier'."[$i]", optional_param('multiplier'."[$i]", '', PARAM_NUMBER));
194 $mform->setType('multiplier'."[$i]", PARAM_NUMBER);
28a27ef1 195 }
271ffe3f 196
28a27ef1 197 }else {
198 $repeated[] =& $mform->createElement('header', 'unithdr', get_string('unithdr', 'qtype_numerical', '{no}'));
199 $repeated[] =& $mform->createElement('text', 'unit', get_string('unit', 'quiz'));
200 $repeated[] =& $mform->createElement('text', 'multiplier', get_string('multiplier', 'quiz'));
201
3a298174 202 $mform->setType('unit', PARAM_NOTAGS);
271ffe3f 203
3a298174 204 $mform->setType('multiplier', PARAM_NUMBER);
9aa022fe 205
206 if (isset($this->question->options)){
207 $countunits = count($this->question->options->units);
208 } else {
209 $countunits = 0;
210 }
271e6dec 211 if ($this->question->formoptions->repeatelements){
212 $repeatsatstart = $countunits + 1;
213 } else {
214 $repeatsatstart = $countunits;
215 }
3a298174 216 $this->repeat_elements($repeated, $repeatsatstart, array(), 'nounits', 'addunits', 2, get_string('addmoreunitblanks', 'qtype_calculated', '{no}'));
271ffe3f 217
28a27ef1 218 }
79bb7202 219 if ($mform->elementExists('multiplier[0]')){
220 $firstunit =& $mform->getElement('multiplier[0]');
221 $firstunit->freeze();
222 $firstunit->setValue('1.0');
223 $firstunit->setPersistantFreeze(true);
224 }
28a27ef1 225 if ($this->editasmultichoice == 1){
226 $mform->setType('addunits','hidden');
227 $mform->addElement('header', 'overallfeedbackhdr', get_string('overallfeedback', 'qtype_multichoice'));
228
229 foreach (array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback') as $feedbackname) {
230 $mform->addElement('htmleditor', $feedbackname, get_string($feedbackname, 'qtype_multichoice'),
231 array('course' => $this->coursefilesid));
232 $mform->setType($feedbackname, PARAM_RAW);
233 }
234 }else {
235 foreach (array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback') as $feedbackname) {
236 $mform->addElement('hidden', $feedbackname);
237 $mform->setType($feedbackname, PARAM_RAW);
238 }
239 }
9aa022fe 240 //hidden elements
e593233a 241 $mform->addElement('hidden', 'synchronize', '');
d18e0fe6 242 $mform->setType('synchronize', PARAM_INT);
e593233a 243 if (isset($this->question->options)&& isset($this->question->options->synchronize) ){
244 $mform->setDefault("synchronize", $this->question->options->synchronize);
245 } else {
246 $mform->setDefault("synchronize", 0 );
247 }
60b5ecd3 248 $mform->addElement('hidden', 'wizard', 'datasetdefinitions');
249 $mform->setType('wizard', PARAM_ALPHA);
92186abc 250
271ffe3f 251
252 }
253
32db0d42 254 function set_data($question) {
28a27ef1 255 if (isset($this->editasmultichoice)){
256 $default_values['multichoice']= 1 ; //$this->editasmultichoice ;
257 }
271ffe3f 258 if (isset($question->options)){
259 $answers = $question->options->answers;
260 if (count($answers)) {
261 $key = 0;
262 foreach ($answers as $answer){
9af77e9d 263 $default_values['answer['.$key.']'] = $answer->answer;
92186abc 264 $default_values['fraction['.$key.']'] = $answer->fraction;
265 $default_values['tolerance['.$key.']'] = $answer->tolerance;
f7089b63 266 $default_values['tolerancetype['.$key.']'] = $answer->tolerancetype;
92186abc 267 $default_values['correctanswerlength['.$key.']'] = $answer->correctanswerlength;
9aa022fe 268 $default_values['correctanswerformat['.$key.']'] = $answer->correctanswerformat;
271ffe3f 269 $default_values['feedback['.$key.']'] = $answer->feedback;
270 $key++;
271 }
272 }
28a27ef1 273 if (isset($question->options->units)){
274 $units = array_values($question->options->units);
275 // make sure the default unit is at index 0
276 usort($units, create_function('$a, $b',
277 'if (1.0 === (float)$a->multiplier) { return -1; } else '.
278 'if (1.0 === (float)$b->multiplier) { return 1; } else { return 0; }'));
279 if (count($units)) {
280 $key = 0;
281 foreach ($units as $unit){
282 $default_values['unit['.$key.']'] = $unit->unit;
283 $default_values['multiplier['.$key.']'] = $unit->multiplier;
284 $key++;
285 }
9aa022fe 286 }
287 }
92186abc 288 }
28a27ef1 289 if (isset($question->options->single)){
290 $default_values['single'] = $question->options->single;
291 $default_values['answernumbering'] = $question->options->answernumbering;
292 $default_values['shuffleanswers'] = $question->options->shuffleanswers;
293 $default_values['correctfeedback'] = $question->options->correctfeedback;
294 $default_values['partiallycorrectfeedback'] = $question->options->partiallycorrectfeedback;
295 $default_values['incorrectfeedback'] = $question->options->incorrectfeedback;
296 }
8fc3e643 297 $default_values['submitbutton'] = get_string('nextpage', 'qtype_calculated');
298 $default_values['makecopy'] = get_string('makecopynextpage', 'qtype_calculated');
271e6dec 299 /* set the wild cards category display given that on loading the category element is
300 unselected when processing this function but have a valid value when processing the
0dd3e11c 301 update category button. The value can be obtain by
302 $qu->category =$this->_form->_elements[$this->_form->_elementIndex['category']]->_values[0];
303 but is coded using existing functions
271e6dec 304 */
0dd3e11c 305 $qu = new stdClass;
306 $el = new stdClass;
271e6dec 307 /* no need to call elementExists() here */
79bb7202 308 if ($this->_form->elementExists('category')){
309 $el=$this->_form->getElement('category');
310 } else {
311 $el=$this->_form->getElement('categorymoveto');
312 }
0dd3e11c 313 if($value =$el->getSelected()) {
314 $qu->category =$value[0];
315 }else {
316 $qu->category=$question->category;// on load $question->category is set by question.php
271e6dec 317 }
0dd3e11c 318 $html2 = $this->qtypeobj->print_dataset_definitions_category($qu);
79bb7202 319 $this->_form->_elements[$this->_form->_elementIndex['listcategory']]->_text = $html2 ;
320 $question = (object)((array)$question + $default_values);
92186abc 321
32db0d42 322 parent::set_data($question);
271ffe3f 323 }
324
325 function qtype() {
326 return 'calculated';
327 }
328
a78890d5 329 function validation($data, $files) {
28a27ef1 330 // echo code left for testing period
331
332 // echo "<p>question <pre>";print_r($this->question);echo "</pre></p>";
333 // echo "<p>data <pre>";print_r($data);echo "</pre></p>";
334
fe93ba83 335 $errors = parent::validation($data, $files);
bfdc0bce 336 //verifying for errors in {=...} in question text;
337 $qtext = "";
338 $qtextremaining = $data['questiontext'] ;
339 $possibledatasets = $this->qtypeobj->find_dataset_names($data['questiontext']);
340 foreach ($possibledatasets as $name => $value) {
341 $qtextremaining = str_replace('{'.$name.'}', '1', $qtextremaining);
342 }
271e6dec 343 // echo "numericalquestion qtextremaining <pre>";print_r($possibledatasets);
6dbcacee 344 while (preg_match('~\{=([^[:space:]}]*)}~', $qtextremaining, $regs1)) {
bfdc0bce 345 $qtextsplits = explode($regs1[0], $qtextremaining, 2);
346 $qtext =$qtext.$qtextsplits[0];
347 $qtextremaining = $qtextsplits[1];
348 if (!empty($regs1[1]) && $formulaerrors = qtype_calculated_find_formula_errors($regs1[1])) {
349 if(!isset($errors['questiontext'])){
350 $errors['questiontext'] = $formulaerrors.':'.$regs1[1] ;
351 }else {
352 $errors['questiontext'] .= '<br/>'.$formulaerrors.':'.$regs1[1];
271e6dec 353 }
bfdc0bce 354 }
271e6dec 355 }
9af77e9d 356 $answers = $data['answer'];
271ffe3f 357 $answercount = 0;
a6d46515 358 $maxgrade = false;
f6232d58 359 $possibledatasets = $this->qtypeobj->find_dataset_names($data['questiontext']);
360 $mandatorydatasets = array();
361 foreach ($answers as $key => $answer){
a6d46515 362 $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer);
271e6dec 363 }
a6d46515 364 if ( count($mandatorydatasets )==0){
365 // $errors['questiontext']=get_string('atleastonewildcard', 'qtype_datasetdependent');
f6232d58 366 foreach ($answers as $key => $answer){
9af77e9d 367 $errors['answer['.$key.']'] = get_string('atleastonewildcard', 'qtype_datasetdependent');
271e6dec 368 }
369 }
28a27ef1 370 if ($data['multichoice']== 1 ){
371 foreach ($answers as $key => $answer){
372 $trimmedanswer = trim($answer);
99a77ee5 373 if (($trimmedanswer!='')||$answercount==0){
374 //verifying for errors in {=...} in answer text;
375 $qanswer = "";
376 $qanswerremaining = $trimmedanswer ;
377 $possibledatasets = $this->qtypeobj->find_dataset_names($trimmedanswer);
378 foreach ($possibledatasets as $name => $value) {
379 $qanswerremaining = str_replace('{'.$name.'}', '1', $qanswerremaining);
380 }
381 // echo "numericalquestion qanswerremaining <pre>";print_r($possibledatasets);
382 while (preg_match('~\{=([^[:space:]}]*)}~', $qanswerremaining, $regs1)) {
383 $qanswersplits = explode($regs1[0], $qanswerremaining, 2);
384 $qanswer =$qanswer.$qanswersplits[0];
385 $qanswerremaining = $qanswersplits[1];
386 if (!empty($regs1[1]) && $formulaerrors = qtype_calculated_find_formula_errors($regs1[1])) {
387 if(!isset($errors['answer['.$key.']'])){
388 $errors['answer['.$key.']'] = $formulaerrors.':'.$regs1[1] ;
389 }else {
390 $errors['answer['.$key.']'] .= '<br/>'.$formulaerrors.':'.$regs1[1];
391 }
28a27ef1 392 }
393 }
394 }
99a77ee5 395 if ($trimmedanswer!=''){
396 if ('2' == $data['correctanswerformat'][$key]
397 && '0' == $data['correctanswerlength'][$key]) {
398 $errors['correctanswerlength['.$key.']'] = get_string('zerosignificantfiguresnotallowed','quiz');
399 }
400 if (!is_numeric($data['tolerance'][$key])){
401 $errors['tolerance['.$key.']'] = get_string('mustbenumeric', 'qtype_calculated');
402 }
403 if ($data['fraction'][$key] == 1) {
404 $maxgrade = true;
405 }
406
407 $answercount++;
28a27ef1 408 }
99a77ee5 409 //check grades
410 if ($answer != '') {
411 if ($data['fraction'][$key] > 0) {
412 $totalfraction += $data['fraction'][$key];
413 }
414 if ($data['fraction'][$key] > $maxfraction) {
415 $maxfraction = $data['fraction'][$key];
416 }
417 }
28a27ef1 418 }
99a77ee5 419 if ($answercount==0){
420 $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
421 $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
422 } elseif ($answercount==1){
423 $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
424
28a27ef1 425 }
99a77ee5 426
427 /// Perform sanity checks on fractional grades
428 if ($data['single']) {
429 if ($maxfraction != 1) {
430 $maxfraction = $maxfraction * 100;
431 $errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_multichoice', $maxfraction);
9aa022fe 432 }
99a77ee5 433 } else {
434 $totalfraction = round($totalfraction,2);
435 if ($totalfraction != 1) {
436 $totalfraction = $totalfraction * 100;
437 $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction);
9aa022fe 438 }
99a77ee5 439 }
440 }else{ // regular calculated
441 foreach ($answers as $key => $answer){
442 //check no of choices
443 // the * for everykind of answer not actually implemented
444 $trimmedanswer = trim($answer);
445 if (($trimmedanswer!='')||$answercount==0){
446 $eqerror = qtype_calculated_find_formula_errors($trimmedanswer);
447 if (FALSE !== $eqerror){
448 $errors['answer['.$key.']'] = $eqerror;
449 }
9aa022fe 450 }
99a77ee5 451 if ($trimmedanswer!=''){
452 if ('2' == $data['correctanswerformat'][$key]
453 && '0' == $data['correctanswerlength'][$key]) {
454 $errors['correctanswerlength['.$key.']'] = get_string('zerosignificantfiguresnotallowed','quiz');
455 }
456 if (!is_numeric($data['tolerance'][$key])){
457 $errors['tolerance['.$key.']'] = get_string('mustbenumeric', 'qtype_calculated');
458 }
459 if ($data['fraction'][$key] == 1) {
460 $maxgrade = true;
461 }
462
463 $answercount++;
a6d46515 464 }
99a77ee5 465 //check grades
466
467 //TODO how should grade checking work here??
468 /*if ($answer != '') {
469 if ($data['fraction'][$key] > 0) {
470 $totalfraction += $data['fraction'][$key];
471 }
472 if ($data['fraction'][$key] > $maxfraction) {
473 $maxfraction = $data['fraction'][$key];
474 }
475 }*/
271ffe3f 476 }
99a77ee5 477
478 //grade checking :
479 /// Perform sanity checks on fractional grades
480 /*if ( ) {
481 if ($maxfraction != 1) {
482 $maxfraction = $maxfraction * 100;
483 $errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_multichoice', $maxfraction);
9aa022fe 484 }
99a77ee5 485 } else {
486 $totalfraction = round($totalfraction,2);
487 if ($totalfraction != 1) {
488 $totalfraction = $totalfraction * 100;
489 $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction);
9aa022fe 490 }
491 }*/
99a77ee5 492 $units = $data['unit'];
493 if (count($units)) {
494 foreach ($units as $key => $unit){
495 if (is_numeric($unit)){
496 $errors['unit['.$key.']'] = get_string('mustnotbenumeric', 'qtype_calculated');
9aa022fe 497 }
99a77ee5 498 $trimmedunit = trim($unit);
499 $trimmedmultiplier = trim($data['multiplier'][$key]);
500 if (!empty($trimmedunit)){
501 if (empty($trimmedmultiplier)){
502 $errors['multiplier['.$key.']'] = get_string('youmustenteramultiplierhere', 'qtype_calculated');
503 }
504 if (!is_numeric($trimmedmultiplier)){
505 $errors['multiplier['.$key.']'] = get_string('mustbenumeric', 'qtype_calculated');
506 }
507
9aa022fe 508 }
9aa022fe 509 }
510 }
99a77ee5 511 if ($answercount==0){
512 $errors['answer[0]'] = get_string('atleastoneanswer', 'qtype_calculated');
513 }
514 if ($maxgrade == false) {
515 $errors['fraction[0]'] = get_string('fractionsnomax', 'question');
516 }
a6d46515 517 }
9aa022fe 518
271ffe3f 519 return $errors;
520 }
521}
6dbcacee 522?>