function fp_translate_numeric_grade

6.x misc.inc fp_translate_numeric_grade($grade, $school_id = 0)

This function will use the "Numeric to Letter Grade" setting in the School settings to translate the given grade (if it is numeric) to a letter grade. Otherwise, it will return the grade as-is.

1 call to fp_translate_numeric_grade()

File

includes/misc.inc, line 134
This file contains misc functions for FlightPath

Code

function fp_translate_numeric_grade($grade, $school_id = 0) {


  $only_grade = $grade;

  // We may have included MID at the end of our numeric grade, and if so, it's a midterm grade.

  $bool_midterm = FALSE;
  if (strstr($grade, "MID")) {
    $bool_midterm = TRUE;
    $only_grade = trim(str_replace("MID", "", $grade));
  }


  if (!is_numeric($only_grade)) {
    return $grade; // already a letter, return the original grade.
  }


  $translate = array();


  // Get our translation array from globals cache, if there.
  if (isset($GLOBALS ['fp_translate_numeric_grade'][$school_id])) {
    $translate = $GLOBALS ['fp_translate_numeric_grade'][$school_id];
  }
  else {
    $temp = trim(variable_get_for_school("numeric_to_letter_grades", "", $school_id));
    if ($temp === '') {
      return $grade;
    }

    $temp = explode("\n", $temp);
    foreach ($temp as $line) {
      $line = trim($line);
      if ($line == "") {
        continue;
      }

      $tokens = explode("~", $line);
      $low = floatval(trim($tokens [0]));
      $high = floatval(trim($tokens [1]));
      $letter = trim($tokens [2]);

      $translate [$low][$high] = $letter;

    } // foreach

    $GLOBALS ['fp_translate_numeric_grade'][$school_id] = $translate;
  }


  // Okay, now that we have our "translate" array, we can do that with the grade.
  $grade = floatval($grade);
  foreach ($translate as $low => $details) {
    foreach ($translate [$low] as $high => $letter) {
      if ($grade >= $low && $grade <= $high) {
        if ($bool_midterm) {
          $letter .= "MID";
        }
        return $letter;
      }
    }
  }

  // Else, return back the original grade.
  return $grade;
}