Skip to content

Instantly share code, notes, and snippets.

@BinaryMuse
Created September 15, 2010 19:24
Show Gist options
  • Save BinaryMuse/581289 to your computer and use it in GitHub Desktop.
Save BinaryMuse/581289 to your computer and use it in GitHub Desktop.
<?php
/* FPU EDIT (bkt) 1/2010
* These functions are used for course sorting and for course grouping.
* Used in the function below (print_my_courses()).
*/
/**
* This function is used to sort an array of course objects
* by chronological order based on the FPU naming scheme.
*/
function fpu_course_sort_compare($course_a, $course_b)
{
// We want to sort by our FPU course naming standard.
// Courses that are EARLIER in time are less than courses that are LATER in time.
// Thus 09 < 10, SP < FA
// Five parts: year, term, subject, course, and section.
// YEAR: Consecutive order. 08, 09, 10, 11, 12, etc.
// TERM: First letter, then SP < SS < FA
// COURSE: Alphabetical
// COURSE: Numeric/Alphabetical
// SECTION: Albpabetical
// If the two courses are identical, they are of course... well, identical.
// That means we have other issues, but that is not this function's concern.
if($course_a->shortname == $course_b->shortname)
return 0;
// If one is blank... well, it shouldn't be, but make it first.
if(empty($course_a->shortname) && !empty($course_b->shortname)) {
return -1;
} elseif(!empty($course_a->shortname) && empty($course_b->shortname)) {
return 1;
}
// Get the various parts of a standard or almost standard course name.
$parts_a = fpu_course_parts($course_a->shortname);
$parts_b = fpu_course_parts($course_b->shortname);
// If a course doesn't look like a standard course,
// and the other course does, the non-standard one comes first.
// If they're both non-standard, just do a string compare.
if($parts_a == false && $parts_b != false) {
return -1;
} elseif($parts_b == false && $parts_a != false) {
return 1;
} elseif($parts_a == false && $parts_b == false) {
if(strtolower($course_a->shortname) > strtolower($course_b->shortname))
return 1;
elseif(strtolower($course_a->shortname) < strtolower($course_b->shortname))
return -1;
else
return 0;
}
// Years that start with letters come before years that are numbers.
$year_a = $parts_a[1];
$year_b = $parts_b[1];
if(is_numeric($year_a) && !is_numeric($year_b)) {
return 1;
} elseif(!is_numeric($year_a) && is_numeric($year_b)) {
return -1;
}
// Lower years come before higher years.
$i_year_a = (int) $year_a;
$i_year_b = (int) $year_b;
if($i_year_a < $i_year_b) {
return 1;
} elseif($i_year_b < $i_year_a) {
return -1;
}
// The last two letters of the term part indicate the term.
// TODO: IMPLEMENT FIRST LETTER DEALIO
$term_a = substr($parts_a[2], -2);
$term_b = substr($parts_b[2], -2);
$compare = fpu_term_compare($term_a, $term_b);
if($compare != 0) {
return $compare * -1; // * -1 cause we're sorting in reverse here
}
// Within the year and term, hidden courses come last.
if($course_a->visible == 0 && $course_b->visible == 1) {
return 1;
} elseif($course_a->visible == 1 && $course_b->visible == 0) {
return -1;
}
// Coures part
$course_a = $parts_a[3];
$course_b = $parts_b[3];
if($course_a < $course_b) {
return -1;
} elseif($course_a > $course_b) {
return 1;
}
// And finally section
$sect_a = $parts_a[4];
$sect_b = $parts_b[4];
if($sect_a < $sect_b) {
return -1;
} elseif($sect_a > $sect_b) {
return 1;
}
// End of the line. Do a string compare.
if(strtolower($course_a->shortname) > strtolower($course_b->shortname))
return 1;
elseif(strtolower($course_a->shortname) < strtolower($course_b->shortname))
return -1;
else
return 0;
}
/**
* Compare one term (SS, FA, SP) to another.
*/
function fpu_term_compare($term1, $term2) {
if($term1 == "SP") {
if($term2 == "SS" || $term2 == "FA") {
return -1;
} else {
return 0;
}
} elseif($term1 == "SS") {
if($term2 == "SP") {
return 1;
} elseif($term2 == "SS") {
return 0;
} else {
return -1;
}
} elseif($term1 == "FA") {
if($term2 == "SP" || $term2 == "SS") {
return 1;
} else {
return 0;
}
}
return 0; // shouldn't happen :(
}
/**
* Returns the parts of the course name, if found.
* If not, returns false.
*
* Array
* (
* [0] => 10/GFA-PACS-710/PS-372-ALL
* [1] => 10
* [2] => GFA
* [3] => PACS-710/PS-372
* [4] => ALL
* )
*/
function fpu_course_parts($course) {
$regex = "/^([A-Z0-9]{2,3})\/([A-Z0-9]+)-(.*)-(.*)$/i";
$matches = array();
$matched = preg_match($regex, $course, $matches);
if($matched == false || $matched == 0) {
return false;
} else {
return $matches;
}
}
/* END FPU EDIT */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment