Skip to content

Commit

Permalink
MDL-32181 question/calculatedmulti validate multianswers
Browse files Browse the repository at this point in the history
restructuring the validation function of edit_calculatedmulti_form.php
correcting attempt
  • Loading branch information
ppichet committed Apr 27, 2012
1 parent eddceb6 commit 62aaf94
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 74 deletions.
135 changes: 62 additions & 73 deletions question/type/calculatedmulti/edit_calculatedmulti_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion question/type/calculatedmulti/question.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 62aaf94

Please sign in to comment.