You are here

function match_grade_options in Quiz 6.5

Same name and namespace in other branches
  1. 6.6 includes/moodle/lib/questionlib.php \match_grade_options()

match grade options if no match return error or match nearest

Parameters

array $gradeoptionsfull list of valid options:

int $grade grade to be tested:

string $matchgrades 'error' or 'nearest':

Return value

mixed either 'fixed' value or false if erro

1 call to match_grade_options()
qformat_default::importprocess in includes/moodle/question/format.php
Process the file This method should not normally be overidden

File

includes/moodle/lib/questionlib.php, line 357

Code

function match_grade_options($gradeoptionsfull, $grade, $matchgrades = 'error') {

  // if we just need an error...
  if ($matchgrades == 'error') {
    foreach ($gradeoptionsfull as $value => $option) {

      // slightly fuzzy test, never check floats for equality :-)
      if (abs($grade - $value) < 1.0E-5) {
        return $grade;
      }
    }

    // didn't find a match so that's an error
    return false;
  }
  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;
    }
  }
}