]> git.mjollnir.org Git - moodle.git/commitdiff
Improve validation code to match the recent improvements in shortanswer validation.
authortjhunt <tjhunt>
Wed, 28 Feb 2007 17:17:40 +0000 (17:17 +0000)
committertjhunt <tjhunt>
Wed, 28 Feb 2007 17:17:40 +0000 (17:17 +0000)
Also do some validation on the units entered to stop duplicate units being defined.

 Merged from MOODLE_18_STABLE.

lang/en_utf8/qtype_numerical.php
question/type/numerical/edit_numerical_form.php

index baa2f2ef3244ec152dee0433fafa1b6a0d782c8e..9239f86bf153c4108dfe0f8300b78dcbc55c0847 100644 (file)
@@ -1,7 +1,9 @@
 <?php
 $string['addmoreanswerblanks'] = 'Blanks for {no} More Answers';
-$string['answerno'] = 'Answer $a';
-$string['unithdr'] = 'Unit $a';
 $string['addmoreunitblanks'] = 'Blanks for {no} More Units';
+$string['answerno'] = 'Answer $a';
+$string['errorrepeatedunit'] = 'You cannot have two units with the same name.';
+$string['errornomultiplier'] = 'You must specify a multiplier for this unit.';
 $string['notenoughanswers'] = 'You must enter at least one answer.';
+$string['unithdr'] = 'Unit $a';
 ?>
\ No newline at end of file
index 29854c8e924bee79fb72694d77cc4dbb0ce41f8f..8b06ec837172f026d925825c5446b4125c241aeb 100644 (file)
@@ -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() {