]> git.mjollnir.org Git - moodle.git/commitdiff
Added function for matching/checking question grades on quiz import.
authorthepurpleblob <thepurpleblob>
Wed, 22 Mar 2006 16:27:46 +0000 (16:27 +0000)
committerthepurpleblob <thepurpleblob>
Wed, 22 Mar 2006 16:27:46 +0000 (16:27 +0000)
lib/questionlib.php

index ebb9a43d617e6cfdc341b511cb81641deee7e4b6..cc962a7614660a9e612278c5cc461d3ab070bb36 100644 (file)
@@ -247,6 +247,44 @@ function get_grade_options() {
     return $grades;
 }
 
+/**
+ * match grade options
+ * if no match return error or match nearest
+ * @param array $gradeoptionsfull list of valid options
+ * @param int $grade grade to be tested
+ * @param string $matchgrades 'error' or 'nearest'
+ * @return mixed either 'fixed' value or false if erro
+ */
+function match_grade_options($gradeoptionsfull, $grade, $matchgrades='error') {
+    // if we just need an error...
+    if ($matchgrades=='error') {
+        foreach($gradeoptionsfull as $value => $option) {
+            if ($grade==$value) {
+                return $grade;
+            }
+        }
+        // didn't find a match so that's an error
+        return false;
+    }
+    // work out nearest value
+    else if ($matchgrades=='nearest') {
+        $hownear = array();
+        foreach($gradeoptionsfull as $value => $option) {
+            if ($grade==$value) {
+                return $grade;
+            }
+            $hownear[ $value ] = abs( $grade - $value );
+        }
+        // reverse sort list of deltas and grab the last (smallest)
+        asort( $hownear, SORT_NUMERIC );
+        reset( $hownear );
+        return key( $hownear );
+    }
+    else {
+        return false;
+    } 
+}
+
 /**
  * Tests whether a category is in use by any activity module
  *