diff --git a/admin/rehash.php b/admin/rehash.php --- a/admin/rehash.php +++ b/admin/rehash.php @@ -380,26 +380,26 @@ function test() $t1 = 'CS-262-A'; $n += assert_equal($t1, Section::parse($t1), $ideal); - $n += assert_equal($t1 . '_class', Classes::parse($t1), $ideal_c); + $n += assert_equal($t1 . '_class', Course::parse($t1), $ideal_c); $t2 = 'cs262 a'; $n += assert_equal($t2, Section::parse($t2), $ideal); - $n += assert_equal($t2 . '_class', Classes::parse($t2), $ideal_c); + $n += assert_equal($t2 . '_class', Course::parse($t2), $ideal_c); $t3 = 'cs 262 a'; $n += assert_equal($t3, Section::parse($t2), $ideal); - $n += assert_equal($t3 . '_class', Classes::parse($t3), $ideal_c); + $n += assert_equal($t3 . '_class', Course::parse($t3), $ideal_c); $ideal['course'] .= 'L'; $ideal_c['course'] = $ideal['course']; $t1 = 'CS-262L-A'; $n += assert_equal($t1, Section::parse($t1), $ideal); - $n += assert_equal($t1 . '_class', Classes::parse($t1), $ideal_c); + $n += assert_equal($t1 . '_class', Course::parse($t1), $ideal_c); $t2 = 'cs262l a'; $n += assert_equal($t2, Section::parse($t2), $ideal); - $n += assert_equal($t2 . '_class', Classes::parse($t2), $ideal_c); + $n += assert_equal($t2 . '_class', Course::parse($t2), $ideal_c); $t3 = 'cs 262l a'; $n += assert_equal($t3, Section::parse($t2), $ideal); - $n += assert_equal($t3 . '_class', Classes::parse($t3), $ideal_c); + $n += assert_equal($t3 . '_class', Course::parse($t3), $ideal_c); return $n; } diff --git a/auto.php b/auto.php --- a/auto.php +++ b/auto.php @@ -33,7 +33,7 @@ require_once('inc/school.inc'); require_once('inc/class.page.php'); -require_once('class.class.php'); +require_once('inc/class.course.inc'); Page::session_start(); @@ -54,7 +54,7 @@ if (isset($_REQUEST['getsections'])) { } $term = $_REQUEST['term']; -$term_parts = Classes::parse($term); +$term_parts = Course::parse($term); if (!count($term_parts)) { clean_empty_exit(); } diff --git a/class.schedule.php b/class.schedule.php --- a/class.schedule.php +++ b/class.schedule.php @@ -26,14 +26,27 @@ // all the possible permutations. //************************************************** -include_once 'class.class.php'; +include_once('inc/class.course.inc'); include_once 'class.section.php'; include_once 'inc/class.page.php'; +/* + * Load a Classes -> Course converter class for the sake of the + * Schedule::__wakeup() magic function. + */ +require_once('inc/class.classes_convert.inc'); + class Schedule { - private $classStorage; // Classes array of classes + /* + * Variables for upgrading from saved schedules created when there + * was a class called Classes. + */ + private $classStorage; // array of courses private $nclasses; // Integer number of classes + + /* My member variables. */ + private $courses; private $nPermutations = 0; // Integer number of real permutations private $possiblePermutations; // Integer number of possible permutations private $scheduleName; // String name of schedule @@ -59,12 +72,14 @@ class Schedule */ function __construct($name) { - $this->classStorage = array(); - $this->nclasses = 0; + $this->courses = array(); $this->scheduleName = $name; $this->storage = array(); $this->title = "SlatePermutate - Scheduler"; $this->section_format = 'numerous'; + + /* mark this as an upgraded Schedule class. See __wakeup() */ + $this->nclasses = -1; } //-------------------------------------------------- @@ -78,10 +93,9 @@ class Schedule //-------------------------------------------------- // Adds a new class to the schedule. //-------------------------------------------------- - function addClass($n) + function addCourse($n) { - $this->classStorage[$this->nclasses] = new Classes($n); - $this->nclasses++; + $this->courses[] = new Course($n); } //-------------------------------------------------- @@ -91,31 +105,22 @@ class Schedule { $found = false; $counter = 0; - - while(!$found && ($counter < $this->nclasses)) - { - $temp = $this->classStorage[$counter]->getName(); - - if(strcmp($temp,$course_name) == 0) - { - $found = true; - } else { - $counter++; + + foreach ($this->courses as $course) + if (!strcmp($course_name, $course->getName())) + { + $section = $course->section_get($letter); + if (!$section) + { + $section = new Section($letter, array(), $synonym, $faculty); + $course->section_add($section); + } + $section->meeting_add(new SectionMeeting($days, $time_start, $time_end, $location, $type)); + + return; } - } - - if($counter == $this->nclasses) - { - echo 'Could not find class: ' . $course_name . "
\n"; - } else { - $section = $this->classStorage[$counter]->section_get($letter); - if (!$section) - { - $section = new Section($letter, array(), $synonym, $faculty); - $this->classStorage[$counter]->section_add($section); - } - $section->meeting_add(new SectionMeeting($days, $time_start, $time_end, $location, $type)); - } + + echo 'Could not find class: ' . $course_name . "
\n"; } //-------------------------------------------------- @@ -126,10 +131,9 @@ class Schedule { $this->possiblePermutations = 1; /* special case: there is nothing entered into the schedule and thus there is one, NULL permutation */ - if (!$this->nclasses) + if (!count($this->courses)) { /* have an empty schedule */ - $this->classStorage[0] = array(); $this->nPermutations = 1; return; } @@ -137,10 +141,12 @@ class Schedule $position = 0; $counter = 0; - for($i = 0; $i < $this->nclasses; $i++) + $i = 0; + foreach ($this->courses as $course) { - $this->possiblePermutations = $this->possiblePermutations * $this->classStorage[$i]->getnsections(); + $this->possiblePermutations = $this->possiblePermutations * $course->getnsections(); $cs[$i] = 0; // Sets the counter array to all zeroes. + $i ++; } // Checks for conflicts in given classes, stores if none found @@ -149,13 +155,13 @@ class Schedule $conflict = false; // Get first class to compare - for ($upCounter = 0; $upCounter < $this->nclasses && !$conflict; $upCounter ++) + for ($upCounter = 0; $upCounter < count($this->courses) && !$conflict; $upCounter ++) { - for ($downCounter = $this->nclasses - 1; $downCounter > $upCounter && !$conflict; $downCounter --) + for ($downCounter = count($this->courses) - 1; $downCounter > $upCounter && !$conflict; $downCounter --) { - if ($this->classStorage[$upCounter]->getSection($cs[$upCounter]) - ->conflictsWith($this->classStorage[$downCounter]->getSection($cs[$downCounter]))) + if ($this->courses[$upCounter]->getSection($cs[$upCounter]) + ->conflictsWith($this->courses[$downCounter]->getSection($cs[$downCounter]))) { $conflict = TRUE; break; @@ -166,7 +172,7 @@ class Schedule // Store to storage if no conflict is found. if(!$conflict) { - for($i = 0; $i < $this->nclasses; $i++) + for($i = 0; $i < count($this->courses); $i++) { $this->storage[$this->nPermutations][$i] = $cs[$i]; } @@ -180,7 +186,7 @@ class Schedule $valid = false; while(!$valid) { - if($cs[$position] == $this->classStorage[$position]->getnsections()) + if($cs[$position] == $this->courses[$position]->getnsections()) { $cs[$position] = 0; @@ -190,7 +196,7 @@ class Schedule // though the combination is not actually true // the larger while loop will end before any // new combinations are performed. - if($position == $this->nclasses) + if($position == count($this->courses)) { $valid = true; } else { @@ -355,9 +361,9 @@ class Schedule /* Makes sure there is not a class already in progress */ if($rowspan[$dayLoop] <= 0) { - for($j = 0; $j < $this->nclasses; $j++) + for($j = 0; $j < count($this->courses); $j++) { - $class = $this->classStorage[$j]; + $class = $this->courses[$j]; $section_index = $this->storage[$i][$j]; $section = $class->getSection($section_index); /* iterate through all of a class's meeting times */ @@ -473,16 +479,16 @@ class Schedule */ function nclasses_get() { - return $this->nclasses; + return count($this->courses); } - /* + /** * \brief * fetch a specified class by its key */ function class_get($class_key) { - return $this->classStorage[$class_key]; + return $this->courses[$class_key]; } /** @@ -537,4 +543,23 @@ class Schedule return $url; } + + /** + * \brief + * A magic function which tries to upgrade old serialized sections + * to the new format. + */ + function __wakeup() + { + if ($this->nclasses == -1) + /* this Schedule doesn't need to be upgraded from Classes to Course */ + return; + + $this->courses = array(); + foreach ($this->classStorage as $classes) + { + $this->courses[] = $classes->to_course(); + } + $this->nclasses = -1; + } } diff --git a/inc/class.classes_convert.inc b/inc/class.classes_convert.inc new file mode 100644 --- /dev/null +++ b/inc/class.classes_convert.inc @@ -0,0 +1,43 @@ +. + */ + +require_once('class.course.inc'); + +/** + * \brief + * Class whose purpose is to upgrade deprecated saved_schedules in + * the Classes form into Course objects. + */ +class Classes +{ + private $name; // String + private $sections; // Array of sections + private $nsections; // int + + public function to_course() + { + $course = new Course($this->name); + + foreach ($this->sections as $section) + $course->section_add($section); + + return $course; + } +} diff --git a/class.class.php b/inc/class.course.inc rename from class.class.php rename to inc/class.course.inc --- a/class.class.php +++ b/inc/class.course.inc @@ -26,7 +26,7 @@ include_once 'class.section.php'; -class Classes +class Course { private $name; // String private $sections; // Array of sections diff --git a/inc/class.semester.inc b/inc/class.semester.inc --- a/inc/class.semester.inc +++ b/inc/class.semester.inc @@ -19,7 +19,8 @@ */ $root_dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR; -require_once($root_dir . 'class.class.php'); +$inc_dir = $root_dir . 'inc' . DIRECTORY_SEPARATOR; +require_once($inc_dir . 'class.course.inc'); require_once($root_dir . 'class.section.php'); /** @@ -71,9 +72,9 @@ class Semester * \param $class * The class/course to add. */ - public function class_add(Classes $class) + public function class_add(Course $class) { - $class_parts = Classes::parse($class->getName()); + $class_parts = Course::parse($class->getName()); if (!isset($class_parts['course'])) throw ErrorException('I was given a class with an invalid name: `' . $class->getName() . '\''); @@ -93,7 +94,7 @@ class Semester * \param $class * The course/class number. '262' for 'cs-262'. * \return - * A Classes or NULL if not found. + * A Course or NULL if not found. */ public function class_get($dept, $class) { @@ -145,7 +146,7 @@ class Semester if (!isset($this->departments[$dept]) || !isset($this->departments[$dept][$class])) { - $classobj = new Classes($dept . '-' . $class); + $classobj = new Course($dept . '-' . $class); $this->class_add($classobj); } else diff --git a/inc/schedule_store.inc b/inc/schedule_store.inc --- a/inc/schedule_store.inc +++ b/inc/schedule_store.inc @@ -142,6 +142,8 @@ function schedule_store_delete($schedule * The schedule_store instance we're working with. * \param $operation * Which flock() operation to perform: valid are LOCK_SH and LOCK_EX. + * \return + * TRUE on success, FALSE on failure. (see flock()). */ function _schedule_store_flock_grab(&$schedule_store, $operation) { diff --git a/inc/school.inc b/inc/school.inc --- a/inc/school.inc +++ b/inc/school.inc @@ -262,7 +262,7 @@ function school_default_classes($school) $school_default_classes = $school['id'] . '_default_classes'; if (function_exists($school_default_classes)) { - require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'class.class.php'); + require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'class.course.inc'); return $school_default_classes(); } diff --git a/input.php b/input.php --- a/input.php +++ b/input.php @@ -19,7 +19,7 @@ */ include_once 'class.schedule.php'; -include_once 'class.class.php'; +include_once 'inc' . DIRECTORY_SEPARATOR . 'class.course.inc'; include_once 'class.section.php'; include_once 'inc/class.page.php'; require_once('inc/schedule_store.inc'); @@ -151,7 +151,7 @@ if (!empty($_REQUEST['selectschool']) $inputPage->showSchoolInstructions(); $inputPage->foot(); -function input_class_js(Classes $class, $whitespace = ' ') +function input_class_js(Course $class, $whitespace = ' ') { $js = $whitespace . 'class_last = add_class_n(\'' . htmlentities($class->getName(), ENT_QUOTES) . "');\n"; diff --git a/process.php b/process.php --- a/process.php +++ b/process.php @@ -21,7 +21,7 @@ require_once('inc/schedule_store.inc'); require_once('inc/class.page.php'); include_once 'class.schedule.php'; -include_once 'class.class.php'; +include_once('inc/class.course.inc'); include_once 'class.section.php'; // Converts a 5-element day array into a string. @@ -133,7 +133,7 @@ if(!$DEBUG) */ if(is_array($class) && count($class) > 1) { - $allClasses->addClass($class['name']); + $allClasses->addCourse($class['name']); foreach($class as $section) /* Skip the section name, which isn't a section */ diff --git a/schedulecreator.php b/schedulecreator.php --- a/schedulecreator.php +++ b/schedulecreator.php @@ -19,7 +19,7 @@ */ include_once 'class.schedule.php'; -include_once 'class.class.php'; +include_once 'inc/class.course.inc'; include_once 'class.section.php'; //************************************************** @@ -31,32 +31,32 @@ include_once 'class.section.php'; $allClasses = new Schedule("Fall 2010"); - $allClasses->addClass("CS 104"); + $allClasses->addCourse("CS 104"); $allClasses->addSection("CS 104", "A", 1030, 1120, 'th'); - $allClasses->addClass("Engr 209"); + $allClasses->addCourse("Engr 209"); $allClasses->addSection("Engr 209", "A", 1330, 1420, 'mtwf'); $allClasses->addSection("Engr 209", "B", 1430, 1520, 'mtwf'); - $allClasses->addClass("Engr 209L"); + $allClasses->addCourse("Engr 209L"); $allClasses->addSection("Engr 209L", "A", 1830, 2120, 'h'); $allClasses->addSection("Engr 209L", "B", 1830, 2120, 'w'); - $allClasses->addClass("Math 231"); + $allClasses->addCourse("Math 231"); $allClasses->addSection("Math 231", "A", 800, 850, 'mtwf'); - $allClasses->addClass("Phys 235"); + $allClasses->addCourse("Phys 235"); $allClasses->addSection("Phys 235", "A", 900, 950, 'mwf'); $allClasses->addSection("Phys 235", "B", 1130, 1220, 'mwf'); $allClasses->addSection("Phys 235", "C", 1230, 1320, 'mwf'); - $allClasses->addClass("Phys 235L"); + $allClasses->addCourse("Phys 235L"); $allClasses->addSection("Phys 235L", "A", 1430, 1720, 'm'); $allClasses->addSection("Phys 235L", "B", 1430, 1720, 'w'); $allClasses->addSection("Phys 235L", "C", 830, 1120, 'h'); $allClasses->addSection("Phys 235L", "D", 1230, 1520, 'h'); - $allClasses->addClass("Rel 131"); + $allClasses->addCourse("Rel 131"); $allClasses->addSection("Rel 131", "A", 800, 850, 'mwf'); $allClasses->addSection("Rel 131", "B", 900, 950, 'mwf'); $allClasses->addSection("Rel 131", "C", 1330, 1420, 'mwf'); diff --git a/school.d/cedarville.inc b/school.d/cedarville.inc --- a/school.d/cedarville.inc +++ b/school.d/cedarville.inc @@ -52,11 +52,11 @@ EOF; * times)) for Cedarville students. * * \return - * An array of Classes objects. + * An array of Course objects. */ function cedarville_default_classes() { - $chapel = new Classes('Chapel'); + $chapel = new Course('Chapel'); $chapel->section_add(new Section('_', array(new SectionMeeting('mtwhf', '1000', '1045', '', 'chapel')), '', '_'));