function CourseList::sort_best_grade_first

7.x CourseList.php CourseList::sort_best_grade_first(Student $student = NULL, $school_id = 0)
6.x CourseList.php CourseList::sort_best_grade_first(Student $student = NULL, $school_id = 0)

Sorts best-grade-first, as defined by the setting "grade_order", which is a CSV of grades, best-first. Ex: A, B, C, D, F

If the student object is set to a student, we will use that's student's best grade for a course, rather than the actual course's grade. Generally, this can be left as set to null. This is only for when we are trying to organize a list of courses into the grade order, based on what a student has taken. For example, if we want to order a Group's list of courses based on what the student has taken and the grades they made.

File

classes/CourseList.php, line 619

Class

CourseList

Code

function sort_best_grade_first(Student $student = NULL, $school_id = 0) {

  if ($student) {
    $school_id = $student->school_id;
  }

  $temp = csv_to_array(variable_get_for_school("grade_order", "AMID,BMID,CMID,DMID,FMID,A,B,C,D,F,W,I", $school_id));
  // 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][] = $c;

  }

  // Sort best-grade-first:
  ksort($tarray, SORT_NUMERIC);

  // Okay, now go back through tarray and re-construct a new CourseList
  $new_list = new CourseList();
  foreach ($tarray as $per_grade_courses) {
    foreach ($per_grade_courses as $course) {
      $new_list->add($course);
    }
  }

  // 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;
  }


}