diff --git a/inc/admin.inc b/inc/admin.inc --- a/inc/admin.inc +++ b/inc/admin.inc @@ -146,12 +146,13 @@ function school_cache($schools) foreach ($school['crawled_semesters'] as $semester) { $semesters[$semester->id()] = array( - 'id' => $semester->id(), - 'time_start' => $semester->time_start_get(), - 'time_end' => $semester->time_end_get(), - 'weight' => $semester_weights ++, - 'name' => $semester->name_get(), - ); + 'id' => $semester->id(), + 'popular_course_id' => $semester->popular_course_id_get(), + 'name' => $semester->name_get(), + 'time_start' => $semester->time_start_get(), + 'time_end' => $semester->time_end_get(), + 'weight' => $semester_weights ++, + ); } /* foreach ( => $semester) */ /* * Store/cache the semester metadata: diff --git a/inc/class.semester.inc b/inc/class.semester.inc --- a/inc/class.semester.inc +++ b/inc/class.semester.inc @@ -80,6 +80,11 @@ class Semester $this->departments = array(); $this->department_names = array(); + + /* + * For $this->popular_course_get(). + */ + $this->course_num_sections_map = array(); } /** @@ -89,22 +94,26 @@ class Semester * \param $class * The class/course to add. */ - public function class_add(Course $class) + public function class_add(Course $course) { - $class_parts = Course::parse($class->getName()); - if (!isset($class_parts['course'])) - throw new ErrorException('I was given a class with an invalid name: `' . $class->getName() . '\''); + $course_parts = Course::parse($course->getName()); + if (!isset($course_parts['course'])) + throw new ErrorException('I was given a class with an invalid name: `' . $course->getName() . '\''); - foreach ($class as $course_slot) + foreach ($course as $course_slot) foreach ($course_slot as $section) + { foreach ($section as $meeting) $this->time_set_section_meeting($meeting); - if (!isset($this->departments[$class_parts['department']])) - $this->departments[$class_parts['department']] = array(); - $department =& $this->departments[$class_parts['department']]; + $this->_section_count($course, $section); + } - $department[$class_parts['course']] = $class; + if (!isset($this->departments[$course_parts['department']])) + $this->departments[$course_parts['department']] = array(); + $department =& $this->departments[$course_parts['department']]; + + $department[$course_parts['course']] = $course; } /** @@ -244,6 +253,8 @@ class Semester } $classobj->section_add($section, $course_slot_id); + + $this->_section_count($classobj, $section); } /** @@ -299,6 +310,46 @@ class Semester /** * \brief + * Account for the addition of a new section to this Semester. + * + * \param $course + * The course this section is a part of. + * \param $section + * The section. + */ + private function _section_count(Course $course, Section $section) + { + $fully_qualified_course_id = $course->getName(); + $this->course_num_sections_map += array($fully_qualified_course_id => 0); + $this->course_num_sections_map[$fully_qualified_course_id] ++; + } + + /** + * \brief + * Get the most popular course. + * + * \return + * The fully-qualified course_id of the most popular course in + * this Semester. + */ + public function popular_course_id_get() + { + if (count($this->course_num_sections_map)) + { + arsort($this->course_num_sections_map, SORT_NUMERIC); + reset($this->course_num_sections_map); + $this->_popular_course = key($this->course_num_sections_map); + } + if (!isset($this->_popular_course)) + { + /* The default popular course */ + $this->_popular_course = 'ENGL-101'; + } + return $this->_popular_course; + } + + /** + * \brief * Update the time_end. * * The time_end is a unix timestamp roughly estimating the time at @@ -527,5 +578,14 @@ class Semester $this->time_ends = array(); $this->time_start_get(); $this->time_starts = array(); + + /* + * A mapping which keeps track of how many sections any given + * course has, with course_id '-' section_id as the key and the + * count as the value. Used to calculate the most frequently-used + * course_id to use as the example course_id (bug #102). + */ + $this->popular_course_id_get(); + $this->course_num_sections_map = array(); } } diff --git a/input.php b/input.php --- a/input.php +++ b/input.php @@ -114,8 +114,10 @@ elseif (!empty($_REQUEST['e'])) $inputPage = page::page_create('Enter Courses', $scripts, $inputPage_options); $school = $inputPage->get_school(); $semester = $inputPage->semester_get(); +if (empty($semesters)) + $semesters = school_semesters($school); -$my_hc = 'var slate_permutate_example_course_id = ' . json_encode(school_example_course_id($school)) . '; +$my_hc = 'var slate_permutate_example_course_id = ' . json_encode(empty($semester) || empty($semester['popular_course_id']) ? school_example_course_id($school) : $semester['popular_course_id']) . '; jQuery(document).ready( function() @@ -270,7 +272,7 @@ if (!empty($_REQUEST['selectsemester'])) Welcome to SlatePermutateaddressStudent(', ', '', FALSE); ?>! get_school())): ?> To get started, enter in a course identifier (e.g., - get_school()); ?>) + get_school()) : $semester['popular_course_id']; ?>) and click the autosuggestion to automatically load available sections for each class.