Skip to content

Commit 74627c9

Browse files
committed
Fixes to the getCourseOpts ajax handler to support searching for 4 character courses
It uses regex now, so its kinda robust Fixes #41
1 parent 85290a5 commit 74627c9

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

js/scheduleAjax.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,6 @@ function renderSvg($svg, $id) {
247247
die(json_encode(array("error" => "argument", "msg" => "You must provide a term")));
248248
}
249249

250-
// If it has dashes or whitespace, then strip them out
251-
$_POST['course'] = preg_replace("/[-\s]/", "", $_POST['course']);
252-
253250
// Iterate over the multiple options
254251
$courseOptions = array();
255252
foreach(explode(',', $_POST['course']) as $course) {
@@ -261,6 +258,10 @@ function renderSvg($svg, $id) {
261258
}
262259

263260
// Now we'll split the course into the various components and build the query for it all
261+
// We'll also strip out whitespace/dashes and force it to uppercase
262+
$course = strtoupper(preg_replace("/[-\s]/", "", $course));
263+
preg_match('/([A-Z]{4})(\d{0,3}[A-Z]?)?(\d{0,2}[A-Z]?\d?)?/', $course, $courseParts);
264+
264265
// Query base: Noncancelled courses from the requested term
265266
$query = "SELECT s.id
266267
FROM courses AS c
@@ -271,24 +272,26 @@ function renderSvg($svg, $id) {
271272
AND c.quarter = '{$_POST['term']}'";
272273

273274
// Component 1: Department
274-
$department = substr($course, 0, 4);
275+
$department = $courseParts[1];
275276
if(strlen($department) != 4) {
276277
// We didn't get an entire department. We won't proceed
277278
die(json_encode(array("error" => "argument", "msg" => "You must provide at least a complete department")));
278279
}
279280
$query .= " AND (d.code = '{$department}' OR d.number = '{$department}')";
280281

281282
// Component 2: Course number
282-
$coursenum = substr($course, 4, 3);
283-
if(!$coursenum || strlen($coursenum) != 3) {
283+
$coursenum = $courseParts[2];
284+
if(!$coursenum || (strlen($coursenum) != 3 && strlen($coursenum) != 4)) {
284285
// We got a partial course. That's ok.
285286
$query .= " AND c.course LIKE '{$coursenum}%'";
286287
} else {
288+
// The user has specified a 3 or 4 character course number. If its 4 chars then the user had better know
289+
// what they're doing.
287290
$query .= " AND c.course = '{$coursenum}'";
288291
}
289292

290293
// Component 3: Section number
291-
$section = substr($course, 7);
294+
$section = $courseParts[3];
292295
if(!$section || strlen($coursenum) != 4) {
293296
// We got a partial section number. That's ok.
294297
$query .= " AND s.section LIKE '{$section}%'";

0 commit comments

Comments
 (0)