From 0cba6a8d5e67c5a88588cedc61e5369893a3d65f Mon Sep 17 00:00:00 2001 From: ppichet Date: Fri, 27 Apr 2012 14:22:03 -0400 Subject: [PATCH] MDL-32181 question/calculatedmulti validate multianswers restructuring the validation function of edit_calculatedmulti_form.php correcting attempt defining = new stdClass() before using them : Strict standards --- question/type/calculated/questiontype.php | 5 +- .../edit_calculatedmulti_form.php | 139 ++++++++---------- question/type/calculatedmulti/question.php | 2 +- 3 files changed, 69 insertions(+), 77 deletions(-) diff --git a/question/type/calculated/questiontype.php b/question/type/calculated/questiontype.php index af7cd38433a..b61d774eddd 100644 --- a/question/type/calculated/questiontype.php +++ b/question/type/calculated/questiontype.php @@ -46,6 +46,7 @@ class qtype_calculated extends question_type { global $CFG, $DB, $OUTPUT; if (!$question->options = $DB->get_record('question_calculated_options', array('question' => $question->id))) { + $question->options = new stdClass(); $question->options->synchronize = 0; $question->options->single = 0; $question->options->answernumbering = 'abc'; @@ -1079,6 +1080,7 @@ class qtype_calculated extends question_type { get_string('anyvalue', 'qtype_calculated') . '


'; } else { $comment->stranswers[$key] = $formula . ' = ' . $formattedanswer->answer . '
'; + $correcttrue = new stdClass(); $correcttrue->correct = $formattedanswer->answer; $correcttrue->true = $answer->answer; if ($formattedanswer->answer < $answer->min || @@ -1524,6 +1526,7 @@ class qtype_calculated extends question_type { WHERE a.id = b.datasetdefinition AND a.type = '1' AND b.question = ? AND a.name = ?"; $currentdatasetdef = $DB->get_record_sql($sql, array($form->id, $name)); if (!$currentdatasetdef) { + $currentdatasetdef = new stdClass(); $currentdatasetdef->type = '0'; } $key = "$type-0-$name"; @@ -1858,7 +1861,7 @@ function qtype_calculated_calculate_answer($formula, $individualdata, // ->answer the correct answer // ->min the lower bound for an acceptable response // ->max the upper bound for an accetpable response - + $calculated = new stdClass(); // Exchange formula variables with the correct values... $answer = question_bank::get_qtype('calculated')->substitute_variables_and_eval( $formula, $individualdata); diff --git a/question/type/calculatedmulti/edit_calculatedmulti_form.php b/question/type/calculatedmulti/edit_calculatedmulti_form.php index 0ac528bb33f..bdfa2504763 100644 --- a/question/type/calculatedmulti/edit_calculatedmulti_form.php +++ b/question/type/calculatedmulti/edit_calculatedmulti_form.php @@ -180,7 +180,7 @@ class qtype_calculatedmulti_edit_form extends question_edit_form { public function data_preprocessing($question) { $question = parent::data_preprocessing($question); - $question = $this->data_preprocessing_answers($question, true); + $question = $this->data_preprocessing_answers($question, false); $question = $this->data_preprocessing_combined_feedback($question, true); $question = $this->data_preprocessing_hints($question, true, true); @@ -256,92 +256,81 @@ class qtype_calculatedmulti_edit_form extends question_edit_form { get_string('atleastonewildcard', 'qtype_calculated'); } } - if ($data['multichoice'] == 1) { - foreach ($answers as $key => $answer) { - $trimmedanswer = trim($answer); - if ($trimmedanswer != '' || $answercount == 0) { - //verifying for errors in {=...} in answer text; - $qanswer = ''; - $qanswerremaining = $trimmedanswer; - $possibledatasets = $this->qtypeobj->find_dataset_names($trimmedanswer); - foreach ($possibledatasets as $name => $value) { - $qanswerremaining = str_replace('{'.$name.'}', '1', $qanswerremaining); - } + $totalfraction = 0; + $maxfraction = -1; + foreach ($answers as $key => $answer) { + $trimmedanswer = trim($answer); + $fraction = (float) $data['fraction'][$key]; + if (empty($trimmedanswer) && $trimmedanswer != '0' && empty($fraction)) { + continue; + } + if (empty($trimmedanswer)) { + $errors['fraction['.$key.']'] = get_string('errgradesetanswerblank', 'qtype_multichoice'); + } + if ($trimmedanswer != '' || $answercount == 0) { + //verifying for errors in {=...} in answer text; + $qanswer = ''; + $qanswerremaining = $trimmedanswer; + $possibledatasets = $this->qtypeobj->find_dataset_names($trimmedanswer); + foreach ($possibledatasets as $name => $value) { + $qanswerremaining = str_replace('{'.$name.'}', '1', $qanswerremaining); + } - while (preg_match('~\{=([^[:space:]}]*)}~', $qanswerremaining, $regs1)) { - $qanswersplits = explode($regs1[0], $qanswerremaining, 2); - $qanswer = $qanswer . $qanswersplits[0]; - $qanswerremaining = $qanswersplits[1]; - if (!empty($regs1[1]) && $formulaerrors = - qtype_calculated_find_formula_errors($regs1[1])) { - if (!isset($errors['answer['.$key.']'])) { - $errors['answer['.$key.']'] = $formulaerrors.':'.$regs1[1]; - } else { - $errors['answer['.$key.']'] .= '
'.$formulaerrors.':'.$regs1[1]; - } + while (preg_match('~\{=([^[:space:]}]*)}~', $qanswerremaining, $regs1)) { + $qanswersplits = explode($regs1[0], $qanswerremaining, 2); + $qanswer = $qanswer . $qanswersplits[0]; + $qanswerremaining = $qanswersplits[1]; + if (!empty($regs1[1]) && $formulaerrors = + qtype_calculated_find_formula_errors($regs1[1])) { + if (!isset($errors['answer['.$key.']'])) { + $errors['answer['.$key.']'] = $formulaerrors.':'.$regs1[1]; + } else { + $errors['answer['.$key.']'] .= '
'.$formulaerrors.':'.$regs1[1]; } } } - if ($trimmedanswer != '') { - if ('2' == $data['correctanswerformat'][$key] && - '0' == $data['correctanswerlength'][$key]) { - $errors['correctanswerlength['.$key.']'] = - get_string('zerosignificantfiguresnotallowed', 'qtype_calculated'); - } - if (!is_numeric($data['tolerance'][$key])) { - $errors['tolerance['.$key.']'] = - get_string('mustbenumeric', 'qtype_calculated'); - } - if ($data['fraction'][$key] == 1) { - $maxgrade = true; - } - - $answercount++; + } + if ($trimmedanswer != '') { + if ('2' == $data['correctanswerformat'][$key] && + '0' == $data['correctanswerlength'][$key]) { + $errors['correctanswerlength['.$key.']'] = + get_string('zerosignificantfiguresnotallowed', 'qtype_calculated'); } - //check grades - $totalfraction = 0; - $maxfraction = 0; - if ($answer != '') { - if ($data['fraction'][$key] > 0) { - $totalfraction += $data['fraction'][$key]; - } - if ($data['fraction'][$key] > $maxfraction) { - $maxfraction = $data['fraction'][$key]; - } + if (!is_numeric($data['tolerance'][$key])) { + $errors['tolerance['.$key.']'] = + get_string('mustbenumeric', 'qtype_calculated'); } - } - if ($answercount == 0) { - $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 2); - $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2); - } else if ($answercount == 1) { - $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2); - - } - - /// Perform sanity checks on fractional grades - if ($data['single']) { - if ($maxfraction > 0.999) { - $maxfraction = $maxfraction * 100; - $errors['fraction[0]'] = - get_string('errfractionsnomax', 'qtype_multichoice', $maxfraction); + if ($data['fraction'][$key] > 0) { + $totalfraction += $data['fraction'][$key]; } - } else { - $totalfraction = round($totalfraction, 2); - if ($totalfraction != 1) { - $totalfraction = $totalfraction * 100; - $errors['fraction[0]'] = - get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction); + if ($data['fraction'][$key] > $maxfraction) { + $maxfraction = $data['fraction'][$key]; } - } - if ($answercount == 0) { - $errors['answer[0]'] = get_string('atleastoneanswer', 'qtype_calculated'); - } - if ($maxgrade == false) { - $errors['fraction[0]'] = get_string('fractionsnomax', 'question'); + $answercount++; } + } + if ($answercount == 0) { + $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 2); + $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2); + } else if ($answercount == 1) { + $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2); } + /// Perform sanity checks on fractional grades + if ($data['single']== 1 ) { + if ($maxfraction != 1) { + $errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_multichoice', + $maxfraction * 100); + } + } else { + $totalfraction = round($totalfraction, 2); + if ($totalfraction != 1) { + $totalfraction = $totalfraction * 100; + $errors['fraction[0]'] = + get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction); + } + } return $errors; } diff --git a/question/type/calculatedmulti/question.php b/question/type/calculatedmulti/question.php index 35c9155a685..a6ff02abcaf 100644 --- a/question/type/calculatedmulti/question.php +++ b/question/type/calculatedmulti/question.php @@ -103,7 +103,7 @@ class qtype_calculatedmulti_multi_question extends qtype_multichoice_multi_quest public $synchronised; public function start_attempt(question_attempt_step $step, $variant) { - qtype_calculated_question_helper::start_attempt($this, $step); + qtype_calculated_question_helper::start_attempt($this, $step, $variant); parent::start_attempt($step, $variant); }