From: tjhunt Date: Wed, 28 Feb 2007 17:17:40 +0000 (+0000) Subject: Improve validation code to match the recent improvements in shortanswer validation. X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=51e89d95f7e454aa517193cbe9b9ec8dd1fe4f8e;p=moodle.git Improve validation code to match the recent improvements in shortanswer validation. Also do some validation on the units entered to stop duplicate units being defined. Merged from MOODLE_18_STABLE. --- diff --git a/lang/en_utf8/qtype_numerical.php b/lang/en_utf8/qtype_numerical.php index baa2f2ef32..9239f86bf1 100644 --- a/lang/en_utf8/qtype_numerical.php +++ b/lang/en_utf8/qtype_numerical.php @@ -1,7 +1,9 @@ \ No newline at end of file diff --git a/question/type/numerical/edit_numerical_form.php b/question/type/numerical/edit_numerical_form.php index 29854c8e92..8b06ec8371 100644 --- a/question/type/numerical/edit_numerical_form.php +++ b/question/type/numerical/edit_numerical_form.php @@ -105,17 +105,41 @@ class question_edit_numerical_form extends question_edit_form { } function validation($data){ $errors = array(); - $answers = $data['answer']; + + // Check the answers. $answercount = 0; - foreach ($answers as $answer){ + $maxgrade = false; + $answers = $data['answer']; + foreach ($answers as $key => $answer) { $trimmedanswer = trim($answer); if ($trimmedanswer!=''){ $answercount++; + if ($data['fraction'][$key] == 1) { + $maxgrade = true; + } } } if ($answercount==0){ $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_numerical'); } + if ($maxgrade == false) { + $errors['fraction[0]'] = get_string('fractionsnomax', 'question'); + } + + // Check units. + $alreadyseenunits = array(); + foreach ($data['unit'] as $key => $unit) { + $trimmedunit = trim($unit); + if ($trimmedunit!='' && in_array($trimmedunit, $alreadyseenunits)) { + $errors["unit[$key]"] = get_string('errorrepeatedunit', 'qtype_numerical'); + if (trim($data['multiplier'][$key]) == '') { + $errors["multiplier[$key]"] = get_string('errornomultiplier', 'qtype_numerical'); + } + } else { + $alreadyseenunits[] = $trimmedunit; + } + } + return $errors; } function qtype() {