# HG changeset patch # User Nathan Phillip Brink # Date 2012-02-15 23:22:23 # Node ID ff180c3283536804dc368ca4009381956f5a2ec0 # Parent ae13413f1878253a8127fbc03166381d0ee1c241 Add support for displaying the friendly name of the department or course in the autocomplete. diff --git a/auto.php b/auto.php --- a/auto.php +++ b/auto.php @@ -89,11 +89,20 @@ if (!$getsections && count($term_parts) } $departments = unserialize(file_get_contents($dept_file)); $json_depts = array(); - foreach ($departments as $key => $department) { - if (!strncmp($department, $dept, $dept_strlen)) { - $json_depts[] = $department; + if (!empty($departments) && is_array($departments[0])) + { + /* New format with department names/labels */ + foreach ($departments as $department) + if (!strncmp($department['value'], $dept, $dept_strlen)) + $json_depts[] = $department; } - } + else + { + /* Old format with just department id. */ + foreach ($departments as $department) + if (!strncmp($department, $dept, $dept_strlen)) + $json_depts[] = $department; + } echo json_encode($json_depts); exit(0); @@ -139,11 +148,19 @@ if (file_exists($classes_file)) /* reduce/create resultset */ $json_classes = array(); - foreach ($classes as $class) - if (!strncmp($class, $class_start, $class_start_strlen)) - { - $json_classes[] = $dept . '-' . $class; - } + if (!empty($classes) && is_array($classes[0])) + { + foreach ($classes as $course) + if (!strncmp($course['value'], $class_start, $class_start_strlen)) + $json_classes[] = $course; + } + else + { + /* Old format with just course id. */ + foreach ($classes as $class) + if (!strncmp($class, $class_start, $class_start_strlen)) + $json_classes[] = $dept . '-' . $class; + } echo json_encode($json_classes); exit(0); diff --git a/inc/admin.inc b/inc/admin.inc --- a/inc/admin.inc +++ b/inc/admin.inc @@ -298,16 +298,43 @@ function school_crawl(array &$school, Pa $departments = $semester->departments_get(); sort($departments); + /* + * Create friendly department labels (like `MATH + * (Mathematics)') if the crawler has given us that + * data. + */ + $has_department_names = FALSE; + $department_names = array(); + foreach ($semester->department_names_get() as $dept => $dept_name) + { + $department_names[] = array('value' => $dept, 'label' => $dept . ' (' . $dept_name . ')'); + if (!$has_department_names && strcmp($dept, $dept_name)) + $has_department_names = TRUE; + } + $dept_file = fopen($cache_auto_school_semester_dir_name . '-depts', 'wb'); - fwrite($dept_file, serialize($departments)); + fwrite($dept_file, serialize($has_department_names ? $department_names : $departments)); fclose($dept_file); /* now per-department autocomplete */ foreach ($departments as $department) { $classes = $semester->department_classes_get($department); + $courses_json = array(); + foreach ($classes as $course_id) + { + $course = $semester->class_get($department, $course_id); + $course_json = array( + 'value' => $course->getName(), + 'label' => $course->getName(), + ); + if (strlen($course_title = $course->title_get())) + $course_json['label'] .= ' (' . $course_title . ')'; + $courses_json[] = $course_json; + } + $classes_file = fopen($cache_auto_school_semester_dir_name . $department . '.sects', 'wb'); - fwrite($classes_file, serialize($classes)); + fwrite($classes_file, serialize($courses_json)); fclose($classes_file); /* now individual section informations, pre-JSON-ized */ diff --git a/inc/class.semester.inc b/inc/class.semester.inc --- a/inc/class.semester.inc +++ b/inc/class.semester.inc @@ -79,6 +79,7 @@ class Semester $this->year = $year; $this->departments = array(); + $this->department_names = array(); } /** @@ -131,6 +132,59 @@ class Semester /** * \brief + * Set a department's human-friendly name. + * + * \param $dept + * The department. + * \param $name + * The human-friendly name of the department. + */ + public function department_name_set($dept, $name) + { + $this->department_names[$dept] = $name; + } + + /** + * \brief + * Determine if a particular department has a human-oriented name. + * + * \param $dept + * The department to check. + * \return + * TRUE if this department has been assigned a human-friendly + * name. + */ + public function department_name_has($dept) + { + return !empty($this->department_names[$dept]); + } + + /** + * \brief + * Get a list of departments and long names, if available. + * + * This function only returns departments for which this Semester + * has courses. + * + * \return + * An array where keys are department identifiers and values are + * human-friendly names (if available). If human-friendly names + * are not available, department identifiers are used instead of + * the human-friendly ones. + */ + public function department_names_get() + { + $department_names = array(); + foreach ($this->departments_get() as $dept) + if (empty($this->department_names[$dept])) + $department_names[$dept] = $dept; + else + $department_names[$dept] = $this->department_names[$dept]; + return $department_names; + } + + /** + * \brief * Gets a list of class/course numbers available for a particular * department. */