Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-32181 question/calculatedmulti validate multianswers

restructuring the validation function of edit_calculatedmulti_form.php
correcting attempt
  • Loading branch information...
commit 62aaf94f498f29492b1798338b69d9caeff848d8 1 parent eddceb6
@ppichet ppichet authored
View
135 question/type/calculatedmulti/edit_calculatedmulti_form.php
@@ -260,91 +260,80 @@ public function validation($data, $files) {
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.']'] .= '<br/>'.$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.']'] .= '<br/>'.$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];
}
+
+ $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);
- if ($answercount == 0) {
- $errors['answer[0]'] = get_string('atleastoneanswer', 'qtype_calculated');
+ }
+ /// Perform sanity checks on fractional grades
+ if ($data['single']== 1 ) {
+ if ($maxfraction != 1) {
+ $errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_multichoice',
+ $maxfraction * 100);
}
- if ($maxgrade == false) {
- $errors['fraction[0]'] = get_string('fractionsnomax', 'question');
+ } else {
+ $totalfraction = round($totalfraction, 2);
+ if ($totalfraction != 1) {
+ $totalfraction = $totalfraction * 100;
+ $errors['fraction[0]'] =
+ get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction);
}
-
}
return $errors;
}
View
2  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);
}
Please sign in to comment.
Something went wrong with that request. Please try again.