function _CourseList::z____sort_best_grade_first

5.x _CourseList.php _CourseList::z____sort_best_grade_first(Student $student = NULL)

This is the original sort_best_grade_first method, replaced by the new one (modified by Logan Buth) on 3-29-2018)

File

classes/_CourseList.php, line 674

Class

_CourseList

Code

function z____sort_best_grade_first(Student $student = NULL) {


  $temp = csv_to_array(variable_get("grade_order", "AMID,BMID,CMID,DMID,FMID,A,B,C,D,F,W,I"));
  // We will use array_flip to get back an assoc array where the grades are the keys and the indexes are the values.
  $temp = array_flip($temp);
  // Go through the grades and convert the integers to strings, padd with zeros so that everything is at least 3 digits.
  $grades = array();
  foreach ($temp as $grade => $val) {
    $grades [$grade] = str_pad((string) $val, 3, "0", STR_PAD_LEFT);
  }

  // We now have our grades array just how we want it.  Best grade has lowest value.  Worst grade has highest value.

  $unknown_grade_value = "999"; // sort to the very end, in other words.	  
  $student_grade_score = 0;


  // We are going to go through our courses and, based on the grade, assign them a value.
  $tarray = array();
  for ($t = 0; $t < $this->count; $t++) {
    // $t is the index for the array_list, keep in mind.

    $c = $this->array_list [$t];

    $use_grade = $c->grade;

    if ($student != null) {
      $use_grade = $student->get_best_grade_for_course($c);
      if (!$use_grade) {
        $use_grade = "";
      }
    }

    @$grade_value = $grades [$use_grade];
    if ($grade_value == "") {
      // Couldn't find this grade in our array, so give it the unknown value.
      $grade_value = $unknown_grade_value;
    }

    $student_grade_score += intval($grade_value);
    // Add to a string in array so we can sort easily using a normal sort operation.
    $tarray [] = "$grade_value ~~ $t";

  }

  // Sort best-grade-first:
  sort($tarray);

  // Okay, now go back through tarray and re-construct a new CourseList
  $new_list = new CourseList();
  for ($t = 0; $t < count($tarray); $t++) 
   {
    $temp = explode(" ~~ ", $tarray [$t]);
    $i = $temp [1];
    $new_list->add($this->array_list [$i]);
  }

  // Okay, now $new_list should contain the correct values.
  // We will transfer over the reference.
  $this->array_list = $new_list->array_list;


  // And we are done!
  if ($student != NULL) {
    // Return the "student grade score" for this list of courses.
    return $student_grade_score;
  }


}