From 8511669cb91365e52b1f42808801f34b2642a462 Mon Sep 17 00:00:00 2001 From: thepurpleblob Date: Wed, 22 Mar 2006 16:27:46 +0000 Subject: [PATCH] Added function for matching/checking question grades on quiz import. --- lib/questionlib.php | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/lib/questionlib.php b/lib/questionlib.php index ebb9a43d61..cc962a7614 100644 --- a/lib/questionlib.php +++ b/lib/questionlib.php @@ -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 * -- 2.39.5