Skip to content

Commit 284454d

Browse files
committed
Merge branch 'develop' into feature/spaRefactor
2 parents b1d1249 + aa9f40b commit 284454d

24 files changed

+299
-938
lines changed

api/generate.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,6 @@ function timeStringToMinutes($str) {
206206
die(json_encode(array("error" => "argument", "msg" => "You must provide a term")));
207207
}
208208

209-
// If it has dashes or whitespace, then strip them out
210-
$_POST['course'] = preg_replace("/[-\s]/", "", $_POST['course']);
211-
212209
// Iterate over the multiple options
213210
$courseOptions = array();
214211
foreach(explode(',', $_POST['course']) as $course) {
@@ -220,6 +217,10 @@ function timeStringToMinutes($str) {
220217
}
221218

222219
// Now we'll split the course into the various components and build the query for it all
220+
// We'll also strip out whitespace/dashes and force it to uppercase
221+
$course = strtoupper(preg_replace("/[-\s]/", "", $course));
222+
preg_match('/([A-Z]{4})(\d{0,3}[A-Z]?)?(\d{0,2}[A-Z]?\d?)?/', $course, $courseParts);
223+
223224
// Query base: Noncancelled courses from the requested term
224225
$query = "SELECT s.id
225226
FROM courses AS c
@@ -230,24 +231,26 @@ function timeStringToMinutes($str) {
230231
AND c.quarter = '{$_POST['term']}'";
231232

232233
// Component 1: Department
233-
$department = substr($course, 0, 4);
234+
$department = $courseParts[1];
234235
if(strlen($department) != 4) {
235236
// We didn't get an entire department. We won't proceed
236237
die(json_encode(array("error" => "argument", "msg" => "You must provide at least a complete department")));
237238
}
238239
$query .= " AND (d.code = '{$department}' OR d.number = '{$department}')";
239240

240241
// Component 2: Course number
241-
$coursenum = substr($course, 4, 3);
242-
if(!$coursenum || strlen($coursenum) != 3) {
242+
$coursenum = $courseParts[2];
243+
if(!$coursenum || (strlen($coursenum) != 3 && strlen($coursenum) != 4)) {
243244
// We got a partial course. That's ok.
244245
$query .= " AND c.course LIKE '{$coursenum}%'";
245246
} else {
247+
// The user has specified a 3 or 4 character course number. If its 4 chars then the user had better know
248+
// what they're doing.
246249
$query .= " AND c.course = '{$coursenum}'";
247250
}
248251

249252
// Component 3: Section number
250-
$section = substr($course, 7);
253+
$section = $courseParts[3];
251254
if(!$section || strlen($coursenum) != 4) {
252255
// We got a partial section number. That's ok.
253256
$query .= " AND s.section LIKE '{$section}%'";

api/schedule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ function getScheduleFromId($id) {
108108
$query = "UPDATE schedules SET datelastaccessed = NOW() WHERE id={$id}";
109109
$result = mysql_query($query);
110110

111-
$query = "SELECT startday, endday, starttime, endtime, building, `quarter`, `image` FROM schedules WHERE id={$id}";
111+
$query = "SELECT startday, endday, starttime, endtime, building, `quarter`, CAST(`image` AS unsigned int) AS `image` FROM schedules WHERE id={$id}";
112112

113113
$result = mysql_query($query);
114114
if(!$result) {

favicon.ico

1.12 KB
Binary file not shown.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
-- -------------------------------------------------------------------------
2+
-- Migration Script for f_4charcourses
3+
--
4+
-- @author Benjamin Russell ([email protected])
5+
-- @descrip Migration script to implement the changes for f_4charcourses.
6+
-- basically sets the course number field to a varchar(4) to enable
7+
-- courses that have letters in their number (eg. PHYS-211A)
8+
-- -------------------------------------------------------------------------
9+
10+
ALTER TABLE `courses`
11+
CHANGE `course` `course` VARCHAR(4) NOT NULL;
12+
13+
delimiter //
14+
DROP PROCEDURE IF EXISTS InsertOrUpdateCourse//
15+
CREATE PROCEDURE InsertOrUpdateCourse(
16+
IN p_quarter INT,
17+
IN p_department_num INT,
18+
IN p_department_code VARCHAR(4),
19+
IN p_course VARCHAR(4),
20+
IN p_credits INT,
21+
IN p_title VARCHAR(50),
22+
IN p_description TEXT
23+
)
24+
BEGIN
25+
-- Determine if the course already exists
26+
DECLARE recordFound INT(1);
27+
DECLARE v_department INT(10);
28+
29+
-- Select the department ID for the course
30+
IF p_quarter > 20130 THEN
31+
-- NOTE: This LIMIT 1 looks jank as fuck. Here's why it's important: There are some
32+
-- departments under semesters that condensed multiple departments from quarters
33+
-- (eg: CSCI is made from 4003 and 4005). That's ok. Problems arose because when deciding
34+
-- which department record would own a course under semesters, this query would return
35+
-- multiple records. Limiting it to one seems like a cop-out since (eg here) what if
36+
-- a CSCI course belongs to 4003?? The truth: It doesn't matter. By limiting it to one, we
37+
-- are storing all semester courses under the same department record (a "random" CSCI
38+
-- one not the 4003 CSCI or the 4005 CSCI). Ordering it will guarantee it's the same one each time.
39+
SET v_department = (SELECT d.id FROM departments AS d WHERE d.code = p_department_code ORDER BY d.number LIMIT 1);
40+
ELSE
41+
SET v_department = (SELECT d.id FROM departments AS d WHERE d.number = p_department_num);
42+
END IF;
43+
44+
-- Does the record exist?
45+
SET recordFound = (
46+
SELECT COUNT(*)
47+
FROM courses AS c
48+
WHERE c.department = v_department
49+
AND c.course = p_course
50+
AND c.quarter = p_quarter
51+
);
52+
IF recordFound > 0 THEN
53+
-- Course exists, so update it
54+
UPDATE courses AS c
55+
SET c.title = p_title, c.description = p_description, c.credits = p_credits
56+
WHERE c.department = v_department AND course = p_course AND quarter = p_quarter;
57+
SELECT "updated" AS action;
58+
ELSE
59+
-- Course doesn't exist, so insert it
60+
INSERT INTO courses (quarter, department, course, title, description, credits)
61+
VALUES(p_quarter, v_department, p_course, p_title, p_description, p_credits);
62+
SELECT "inserted" AS action;
63+
END IF;
64+
65+
-- Return the id of the course
66+
SELECT c.id FROM courses AS c WHERE c.department = v_department AND c.course = p_course AND c.quarter = p_quarter;
67+
END//

schema/procedures/InsertOrUpdateCourse.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ CREATE PROCEDURE InsertOrUpdateCourse(
44
IN p_quarter INT,
55
IN p_department_num INT,
66
IN p_department_code VARCHAR(4),
7-
IN p_course INT,
7+
IN p_course VARCHAR(4),
88
IN p_credits INT,
99
IN p_title VARCHAR(50),
1010
IN p_description TEXT
@@ -52,4 +52,4 @@ BEGIN
5252

5353
-- Return the id of the course
5454
SELECT c.id FROM courses AS c WHERE c.department = v_department AND c.course = p_course AND c.quarter = p_quarter;
55-
END//
55+
END

schema/schema.sql

Lines changed: 0 additions & 164 deletions
This file was deleted.

schema/tables/buildings.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
DROP TABLE IF EXISTS buildings;
1010
CREATE TABLE buildings (
11-
`number` VARCHAR(3) PRIMARY KEY,
12-
`code` VARCHAR(3) UNIQUE,
11+
`number` VARCHAR(5) PRIMARY KEY,
12+
`code` VARCHAR(5) UNIQUE,
1313
`name` VARCHAR(100),
1414
`off_campus` BOOLEAN DEFAULT TRUE
1515
) Engine=InnoDb;

schema/tables/courses.sql

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
-- -------------------------------------------------------------------------
2+
-- Courses table
3+
--
4+
-- @author Benjamin Russell ([email protected])
5+
-- @descrip Table for courses. These are linked to departments and quarters
6+
-- in a one quarter/department to many courses. These are also linked
7+
-- to sections in a one course to many sections fashion.
8+
-- -------------------------------------------------------------------------
9+
10+
-- TABLE CREATION ----------------------------------------------------------
11+
CREATE TABLE courses (
12+
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
13+
`quarter` SMALLINT UNSIGNED NOT NULL,
14+
`department` INT UNSIGNED NOT NULL,
15+
`course` VARCHAR(4) NOT NULL,
16+
`credits` TINYINT(2) UNSIGNED NOT NULL DEFAULT 0,
17+
`title` VARCHAR(50) NOT NULL,
18+
`description` TEXT NOT NULL
19+
)ENGINE=InnoDb;
20+
21+
-- INDEXING ----------------------------------------------------------------
22+
ALTER TABLE `courses`
23+
ADD CONSTRAINT UQ_courses_quarter_department_course
24+
UNIQUE (`quarter`, `department`, `course`);
25+
26+
-- FOREIGN KEYS ------------------------------------------------------------
27+
ALTER TABLE `courses`
28+
ADD FOREIGN KEY FK_courses_quarter(`quarter`)
29+
REFERENCES `quarters`(`quarter`)
30+
ON DELETE CASCADE
31+
ON UPDATE CASCADE;
32+
33+
ALTER TABLE `courses`
34+
ADD FOREIGN KEY FK_courses_dept(`department`)
35+
REFERENCES `departments`(`id`)
36+
ON DELETE CASCADE
37+
ON UPDATE CASCADE;

schema/tables/departments.sql

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,21 @@ DROP TABLE IF EXISTS departments;
1111
CREATE TABLE departments (
1212
`id` INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
1313
`school` INT UNSIGNED NOT NULL,
14-
`number` SMALLINT(4) UNSIGNED NULL DEFAULT NULL,
14+
`number` SMALLINT(4) UNSIGNED ZEROFILL NULL DEFAULT NULL,
1515
`code` VARCHAR(4) NULL DEFAULT NULL,
1616
`title` VARCHAR(100) NOT NULL,
1717
`qtrnums` VARCHAR(20) NULL DEFAULT NULL -- Group of corresponding department numbers from quarters
1818
) Engine=InnoDb;
1919

20+
-- UNIQUE CONSTRAINTS ------------------------------------------------------
21+
ALTER TABLE `departments`
22+
ADD CONSTRAINT UQ_departments_number
23+
UNIQUE (`number`);
24+
25+
ALTER TABLE `departments`
26+
ADD CONSTRAINT UQ_departments_code
27+
UNIQUE (`code`);
28+
2029
-- FOREIGN KEYS
2130
ALTER TABLE departments ADD INDEX departments(school);
2231
ALTER TABLE departments ADD CONSTRAINT fk_school FOREIGN KEY departments(school)

0 commit comments

Comments
 (0)