Files
@ bc140e90c361
Branch filter:
Location: SlatePermutate/inc/class.semester.inc
bc140e90c361
11.4 KiB
text/x-povray
Crawl and store credit-hours per section. Display credit-hours, but provide no UI for updating them. Fixes bug #114.
Credit-hour crawling support for calvin and cedarville.
Credit-hour crawling support for calvin and cedarville.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 | <?php /* -*- mode: php; indent-tabs-mode: nil; -*- */
/*
* Copyright 2010 Nathan Phillip Brink <ohnobinki@ohnopublishing.net>
*
* This file is a part of slate_permutate.
*
* slate_permutate is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* slate_permutate is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with slate_permutate. If not, see <http://www.gnu.org/licenses/>.
*/
$inc_dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'inc' . DIRECTORY_SEPARATOR;
require_once($inc_dir . 'class.course.inc');
require_once($inc_dir . 'class.section.php');
require_once($inc_dir . 'math.inc');
/**
* \brief
* Identifies a school semester and acts as a container for Course s
* offered in a semester.
*/
class Semester
{
/**
* \brief
* The Fall season.
*/
const SEASON_FALL = 'fall';
/**
* \brief
* The Spring season.
*/
const SEASON_SPRING = 'spring';
/**
* \brief
* The Summer season.
*/
const SEASON_SUMMER = 'summer';
/**
* \brief
* Instantiate an empty Semester.
*
* \param $year
* The year of this semester. Must be four digits.
* \param $season
* The season of this semester. Please use the constants
* Semester::SEASON_FALL, Semester::SEASON_SPRING, or
* Semester::SEASON_SUMMER if possible.
* \param $time_start
* Specify a timestamp which roughly estimates when this semester
* starts to aid the algorithm for guessing the current
* semester. See Semester::time_start_set(), which may be used
* instead of this parameter
* \param $time_end
* This may be specified now or via Semester::time_end_set().
*/
function __construct($year, $season, $time_start = 0, $time_end = 0)
{
$this->time_start = 0;
$this->time_end = 0;
$this->time_starts = array();
$this->time_ends = array();
$this->season = $season;
if (strlen($year) != 4 || !is_numeric($year))
throw new ErrorException('Attempt to construct a Semester with an invalid year. The given year is `' . $year . '\'');
$this->year = $year;
$this->departments = array();
}
/**
* \brief
* Add a class to this Semester.
*
* \param $class
* The class/course to add.
*/
public function class_add(Course $class)
{
$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() . '\'');
if (!isset($this->departments[$class_parts['department']]))
$this->departments[$class_parts['department']] = array();
$department =& $this->departments[$class_parts['department']];
$department[$class_parts['course']] = $class;
}
/**
* \brief
* Retrieve a class.
*
* \param $dept
* The class's department. 'CS' for 'CS-262'.
* \param $class
* The course/class number. '262' for 'cs-262'.
* \return
* A Course or NULL if not found.
*/
public function class_get($dept, $class)
{
if (!isset($this->departments[$dept][$class]))
return NULL;
return $this->departments[$dept][$class];
}
/**
* \brief
* Gets a list of departments available in this semester.
*/
public function departments_get()
{
return array_keys($this->departments);
}
/**
* \brief
* Gets a list of class/course numbers available for a particular
* department.
*/
public function department_classes_get($dept)
{
if (!isset($this->departments[$dept]))
throw new ErrorException('I was asked for a department I don\'t own: ' . $dept);
return array_keys($this->departments[$dept]);
}
/**
* \brief
* Utility function to add a section to the semester,
* automatically creating classes as necessary.
*
* Crawler functions should generally use this instead of
* Semester::class_add().
*
* \param $dept
* The department this section belongs to.
* \param $class
* The class this section belongs to.
* \param $section
* The section itself.
* \param $title
* The course human-friendly title.
* \param $course_slot_id
* The slot of the course which this section should be added
* to. Use 'default' (or don't pass this parameter) if your school
* does not have the concept of course slots. Ask binki for help
* figuring this out. Course slots are a sort of
* inverse/complement to section_meetings.
*/
public function section_add($dept, $class, Section $section, $title = NULL, $course_slot_id = 'default')
{
$dept = strtoupper($dept);
$class = strtoupper($class);
if (!isset($this->departments[$dept])
|| !isset($this->departments[$dept][$class]))
{
$classobj = new Course($dept . '-' . $class, $title);
$this->class_add($classobj);
}
else
{
$classobj = $this->departments[$dept][$class];
}
$classobj->section_add($section, $course_slot_id);
}
/**
* \brief
* Add a section_meeting, calling Semester::section_add() as
* necessary.
*
* To be used by crawlers when parsing data which only presents one
* section_meeting at a time. I.e., when they do tabular data right.
*
* \param $dept
* The department this section_meeting's course belongs to.
* \param $course
* The course number this section_meeting's section belongs to.
* \param $title
* The course title of the given course the section_meeting or
* NULL.
* belongs to.
* \param $section
* The letter or numbers which make up the section's name.
* \param $synonym
* The section synonym or NULL.
* \param $section_meeting
* The SectionMeeting to be added to a section which may or may
* not already be in this Semester.
* \param $course_slot_id
* The name of the new CourseSlot to create if the given section
* does not yet exist.
* \param $credit_hours
* The number of credit hours of the associated course or a
* negative value if unknown.
*/
public function section_meeting_add($dept, $course, $title, $section, $synonym, $section_meeting, $course_slot_id = 'default', $credit_hours = -1.0)
{
$dept = strtoupper($dept);
$course = strtoupper($course);
if (empty($this->departments[$dept][$course]))
$course_obj = NULL;
else
{
$course_obj = $this->departments[$dept][$course];
$section_obj = $course_obj->section_get($section);
}
if (empty($course_obj) || empty($section_obj))
return $this->section_add($dept, $course, new Section($section, array($section_meeting), $synonym, $credit_hours), $title, $course_slot_id);
$section_obj->meeting_add($section_meeting);
return;
}
/**
* \brief
* Update the time_end.
*
* The time_end is a unix timestamp roughly estimating the time at
* which a semester starts. It is used when guessing what semester a
* user is interested in.
*
* \param $time_end
* The new time_end.
*/
public function time_end_set($time_end)
{
$this->time_end = $time_end;
}
/**
* \brief
* Set the time_end only if it would make the semester end later.
*
* Useful for crawler scripts incrementally guessing the endtime of
* a semester.
*
* \param $time_end
* The new time_end to consider.
*/
public function time_end_set_test($time_end)
{
if ($time_end && $time_end > $this->time_end)
$this->time_end_set($time_end);
}
/**
* \brief
* Add a potential end time to the pool of end times.
*/
public function time_end_pool_add($time_end)
{
$this->time_ends[] = $time_end;
}
public function time_end_get()
{
if (count($this->time_ends))
{
$times = filter_outliers($this->time_ends);
$this->time_end = max($times);
}
return $this->time_end;
}
/**
* \brief
* Update the time_start.
*
* The time_start is a unix timestamp roughly estimating the time at
* which a semester starts. It is used when guessing what semester a
* user is interested in.
*
* \param $time_start
* The new time_start.
*/
public function time_start_set($time_start)
{
$this->time_start = $time_start;
}
/**
* \brief
* Only update the time_start if the time_start isn't yet set or
* if the given time_start is earlier than the stored one.
*
* This should allow crawlers to easily accumulate proper time_start
* and time_end values, see Semester::time_end_set_test();
*
* \param $time_start
* The new estimation of the semester's start.
*/
public function time_start_set_test($time_start)
{
if ($time_start &&
(!$this->time_start || $time_start < $this->time_start))
$this->time_start_set($time_start);
}
/**
* \brief
* Add a potential semester start time to the pool of potential
* start times.
*
* The idea is that there might be erroneous entries in a school's
* database (
* http://www.facebook.com/CalvinRegistrar/posts/299438720070457 )
* which would skew the detected start time. Use statistics to
* detect and kill outliers by using a pool of endtimes :-D.
*/
public function time_start_pool_add($time_start)
{
$this->time_starts[] = $time_start;
}
public function time_start_get()
{
if (count($this->time_starts))
{
$times = filter_outliers($this->time_starts);
$this->time_end = min($times);
}
return $this->time_start;
}
/**
* \brief
* Get a semester's year.
*/
public function year_get()
{
return $this->year;
}
/**
* \brief
* Get a semester's season.
*/
public function season_get()
{
return $this->season;
}
/**
* \brief
* Get a semester's friendly name:
*
* \return
* A string, the semester's friendly name.
*/
public function name_get()
{
return ucfirst($this->season_get()) . ' ' . $this->year_get();
}
/**
* \brief
* Handle conversion to a string.
*
* \return
* A string.
*/
public function __tostring()
{
return $this->name_get();
}
/**
* \brief
* Return an identification string for this semester.
*
* Hopefully this identification string should be unique. Also, this
* identification string is filesystem-safe.
*
* \return
* A string which may be used in paths or to uniquely identify
* this semester in the context of its school.
*/
public function id()
{
return $this->year_get() . '_' . $this->season_get();
}
/**
* \brief
* Enumerate all valid seasons.
*/
public static function seasons_get_all()
{
return array(self::SEASON_SPRING, self::SEASON_SUMMER, self::SEASON_FALL);
}
/**
* \brief
* Clean the semester of all sections, keeping metadata intact.
*/
public function purge()
{
$this->departments = array();
/*
* Make sure that time_end is set to the proper end time before
* clearing out the pool in the time_ends array.
*/
$this->time_end_get();
$this->time_ends = array();
$this->time_start_get();
$this->time_starts = array();
}
}
|