|
11 | 11 |
|
12 | 12 | // REQUIRED FILES ////////////////////////////////////////////////////////// |
13 | 13 | if (file_exists('../inc/config.php')) { |
14 | | - require_once "../inc/config.php"; |
| 14 | + include_once "../inc/config.php"; |
15 | 15 | } else { |
16 | | - require_once "../inc/config.env.php"; |
| 16 | + include_once "../inc/config.env.php"; |
17 | 17 | } |
18 | 18 | require_once "../inc/databaseConn.php"; |
19 | 19 | require_once "../inc/timeFunctions.php"; |
|
28 | 28 | // MAIN EXECUTION ////////////////////////////////////////////////////////// |
29 | 29 |
|
30 | 30 | // Switch on the action |
31 | | -switch(getAction()) { |
32 | | - case "getCourses": |
33 | | - // Query for the courses in this department |
34 | | - |
35 | | - // Verify that we have department to get courses for and a quarter |
36 | | - if(empty($_POST['department']) || !is_numeric($_POST['department'])) { |
37 | | - die(json_encode(array("error" => "argument", "msg" => "You must provide a valid department"))); |
38 | | - } elseif(empty($_POST['term']) || !is_numeric($_POST['term'])) { |
39 | | - die(json_encode(array("error" => "argument", "msg" => "You must provide a valid term"))); |
40 | | - } |
41 | | - |
42 | | - // Do the query |
43 | | - $query = "SELECT c.title, c.course, c.description, c.id, d.number, d.code |
| 31 | +switch (getAction()) { |
| 32 | + case "getCourses": |
| 33 | + // Query for the courses in this department |
| 34 | + |
| 35 | + // Verify that we have department to get courses for and a quarter |
| 36 | + if (empty($_POST['department']) || !is_numeric($_POST['department'])) { |
| 37 | + die(json_encode(["error" => "argument", "msg" => "You must provide a valid department"])); |
| 38 | + } elseif (empty($_POST['term']) || !is_numeric($_POST['term'])) { |
| 39 | + die(json_encode(["error" => "argument", "msg" => "You must provide a valid term"])); |
| 40 | + } |
| 41 | + |
| 42 | + // Do the query |
| 43 | + $query = "SELECT c.title, c.course, c.description, c.id, d.number, d.code |
44 | 44 | FROM sections AS s |
45 | 45 | JOIN courses AS c ON s.course = c.id |
46 | 46 | JOIN departments AS d ON d.id = c.department |
|
49 | 49 | AND s.status != 'X' |
50 | 50 | GROUP BY c.id |
51 | 51 | ORDER BY course"; |
52 | | - $result = $dbConn->query($query); |
53 | | - if(!$result) { |
54 | | - die(json_encode(array("error" => "mysql", "msg" => $dbConn->error))); |
55 | | - } |
56 | | - |
57 | | - // Collect the courses and turn it into a json |
58 | | - $courses = array(); |
59 | | - while($course = $result->fetch_assoc()) { |
60 | | - $courses[] = array( |
| 52 | + $result = $dbConn->query($query); |
| 53 | + if (!$result) { |
| 54 | + die(json_encode(["error" => "mysql", "msg" => $dbConn->error])); |
| 55 | + } |
| 56 | + |
| 57 | + // Collect the courses and turn it into a json |
| 58 | + $courses = []; |
| 59 | + while ($course = $result->fetch_assoc()) { |
| 60 | + $courses[] = [ |
61 | 61 | "id" => $course['id'], |
62 | 62 | "course" => $course['course'], |
63 | | - "department" => array("code" => $course['code'], "number" =>$course['number']), |
| 63 | + "department" => ["code" => $course['code'], "number" => $course['number']], |
64 | 64 | "title" => $course['title'], |
65 | 65 | "description" => htmlentities($course['description']) |
66 | | - ); |
67 | | - } |
| 66 | + ]; |
| 67 | + } |
68 | 68 |
|
69 | | - echo json_encode(array("courses" => $courses)); |
| 69 | + echo json_encode(["courses" => $courses]); |
| 70 | + |
| 71 | + break; |
70 | 72 |
|
71 | | - break; |
| 73 | + case "getDepartments": |
| 74 | + // Query for the departments of the school |
72 | 75 |
|
73 | | - case "getDepartments": |
74 | | - // Query for the departments of the school |
75 | | - |
76 | | - // Verify that we have a school to get departments for |
77 | | - if(empty($_POST['school']) || !is_numeric($_POST['school'])) { |
78 | | - die(json_encode(array("error" => "argument", "msg" => "You must provide a school"))); |
79 | | - } |
| 76 | + // Verify that we have a school to get departments for |
| 77 | + if (empty($_POST['school']) || !is_numeric($_POST['school'])) { |
| 78 | + die(json_encode(["error" => "argument", "msg" => "You must provide a school"])); |
| 79 | + } |
80 | 80 |
|
81 | | - // Verify that we have a quarter to make sure there are |
82 | | - // courses in the department. |
83 | | - if(empty($_POST['term']) || !is_numeric($_POST['term'])) { |
84 | | - die(json_encode(array("error" => "argument", "msg" => "You must provide a term"))); |
85 | | - } |
| 81 | + // Verify that we have a quarter to make sure there are |
| 82 | + // courses in the department. |
| 83 | + if (empty($_POST['term']) || !is_numeric($_POST['term'])) { |
| 84 | + die(json_encode(["error" => "argument", "msg" => "You must provide a term"])); |
| 85 | + } |
86 | 86 |
|
87 | | - // Do the query |
88 | | - if($_POST['term'] > 20130) { |
| 87 | + // Do the query |
| 88 | + if ($_POST['term'] > 20130) { |
89 | 89 | // Get the department code and concat the numbers |
90 | 90 | $query = "SELECT id, title, code, GROUP_CONCAT(number, ', ') AS number |
91 | 91 | FROM departments AS d |
|
102 | 102 | AND number IS NOT NULL |
103 | 103 | ORDER BY id"; |
104 | 104 | } |
105 | | - $result = $dbConn->query($query); |
106 | | - if(!$result) { |
107 | | - die(json_encode(array("error" => "mysql", "msg" => $dbConn->error))); |
108 | | - } |
109 | | - |
110 | | - // Collect the departments and turn it into a json |
111 | | - $departments = array(); |
112 | | - while($department = $result->fetch_assoc()) { |
113 | | - $departments[] = array( |
| 105 | + $result = $dbConn->query($query); |
| 106 | + if (!$result) { |
| 107 | + die(json_encode(["error" => "mysql", "msg" => $dbConn->error])); |
| 108 | + } |
| 109 | + |
| 110 | + // Collect the departments and turn it into a json |
| 111 | + $departments = []; |
| 112 | + while ($department = $result->fetch_assoc()) { |
| 113 | + $departments[] = [ |
114 | 114 | "id" => $department['id'], |
115 | 115 | "title" => $department['title'], |
116 | | - "code" => isset($department['code']) ? $department['code'] : NULL, |
| 116 | + "code" => isset($department['code']) ? $department['code'] : null, |
117 | 117 | "number" => trim($department['number'], " ,") |
118 | | - ); |
119 | | - } |
| 118 | + ]; |
| 119 | + } |
120 | 120 |
|
121 | | - echo json_encode(array("departments" => $departments)); |
| 121 | + echo json_encode(["departments" => $departments]); |
122 | 122 |
|
123 | | - break; |
| 123 | + break; |
124 | 124 |
|
125 | 125 | case "getSchools": |
126 | 126 | // REQUEST FOR LIST OF SCHOOLS ///////////////////////////////////// |
127 | 127 | // Query for the schools |
128 | 128 | $query = "SELECT `id`, `number`, `code`, `title` FROM schools"; |
129 | 129 | $result = $dbConn->query($query); |
130 | | - if(!$result) { |
131 | | - die(json_encode(array("error" => "database", "msg" => "The list of schools could not be retrieved at this time."))); |
| 130 | + if (!$result) { |
| 131 | + die(json_encode(["error" => "database", "msg" => "The list of schools could not be retrieved at this time."])); |
132 | 132 | } |
133 | 133 |
|
134 | 134 | // Build an array of schools |
135 | | - $schools = array(); |
136 | | - while($school = $result->fetch_assoc()) { |
| 135 | + $schools = []; |
| 136 | + while ($school = $result->fetch_assoc()) { |
137 | 137 | $schools[] = $school; |
138 | 138 | } |
139 | 139 |
|
140 | 140 | // Return it to the user |
141 | 141 | echo(json_encode($schools)); |
142 | 142 | break; |
143 | | - |
| 143 | + |
144 | 144 | case "getSchoolsForTerm": |
145 | 145 | // REQUEST FOR LIST OF SCHOOLS FOR TERM //////////////////////////// |
146 | | - if(empty($_POST['term'])) { |
147 | | - die(json_encode(array("error" => "argument", "msg" => "You must provide a term"))); |
148 | | - } |
149 | | - |
150 | | - $term = (int) $_POST['term']; |
151 | | - |
152 | | - // Determine if term was before quarters |
153 | | - if ($term > 20130) { |
154 | | - // School codes |
155 | | - $query = "SELECT id, code AS code, title FROM schools WHERE code IS NOT NULL ORDER BY code"; |
156 | | - } else { |
157 | | - // School numbers |
158 | | - $query = "SELECT id, number AS code, title FROM schools WHERE number IS NOT NULL ORDER BY number"; |
159 | | - } |
160 | | - // Query for the schools |
| 146 | + if (empty($_POST['term'])) { |
| 147 | + die(json_encode(["error" => "argument", "msg" => "You must provide a term"])); |
| 148 | + } |
| 149 | + |
| 150 | + $term = (int)$_POST['term']; |
| 151 | + |
| 152 | + // Determine if term was before quarters |
| 153 | + if ($term > 20130) { |
| 154 | + // School codes |
| 155 | + $query = "SELECT id, code AS code, title FROM schools WHERE code IS NOT NULL ORDER BY code"; |
| 156 | + } else { |
| 157 | + // School numbers |
| 158 | + $query = "SELECT id, number AS code, title FROM schools WHERE number IS NOT NULL ORDER BY number"; |
| 159 | + } |
| 160 | + // Query for the schools |
161 | 161 | $result = $dbConn->query($query); |
162 | | - if(!$result) { |
163 | | - die(json_encode(array("error" => "database", "msg" => "The list of schools could not be retrieved at this time."))); |
| 162 | + if (!$result) { |
| 163 | + die(json_encode(["error" => "database", "msg" => "The list of schools could not be retrieved at this time."])); |
164 | 164 | } |
165 | | - |
| 165 | + |
166 | 166 | // Build an array of schools |
167 | | - $schools = array(); |
168 | | - while($school = $result->fetch_assoc()) { |
169 | | - $schools[] = $school; |
| 167 | + $schools = []; |
| 168 | + while ($school = $result->fetch_assoc()) { |
| 169 | + $schools[] = $school; |
170 | 170 | } |
171 | | - |
| 171 | + |
172 | 172 | // Return it to the user |
173 | 173 | echo(json_encode($schools)); |
174 | 174 | break; |
175 | 175 |
|
176 | | - case "getSections": |
177 | | - // Query for the sections and times of a given course |
178 | | - |
179 | | - // Verify that we have a course to get sections for |
180 | | - if(empty($_POST['course']) || !is_numeric($_POST['course'])) { |
181 | | - die(json_encode(array("error" => "argument", "msg" => "You must provide a course"))); |
182 | | - } |
| 176 | + case "getSections": |
| 177 | + // Query for the sections and times of a given course |
183 | 178 |
|
184 | | - // Do the query |
185 | | - $query = "SELECT c.title AS coursetitle, c.course, d.number, d.code, s.section, |
| 179 | + // Verify that we have a course to get sections for |
| 180 | + if (empty($_POST['course']) || !is_numeric($_POST['course'])) { |
| 181 | + die(json_encode(["error" => "argument", "msg" => "You must provide a course"])); |
| 182 | + } |
| 183 | + |
| 184 | + // Do the query |
| 185 | + $query = "SELECT c.title AS coursetitle, c.course, d.number, d.code, s.section, |
186 | 186 | s.instructor, s.id, s.type, s.maxenroll, s.curenroll, s.title AS sectiontitle |
187 | 187 | FROM sections AS s |
188 | 188 | JOIN courses AS c ON s.course = c.id |
189 | 189 | JOIN departments AS d ON d.id = c.department |
190 | 190 | WHERE s.course = '{$_POST['course']}' |
191 | 191 | AND s.status != 'X' |
192 | 192 | ORDER BY c.course, s.section"; |
193 | | - $sectionResult = $dbConn->query($query); |
194 | | - if(!$sectionResult) { |
195 | | - die(json_encode(array("error" => "mysql", "msg" => $dbConn->error))); |
196 | | - } |
197 | | - |
198 | | - // Collect the sections and their times, modify the section inline |
199 | | - $sections = array(); |
200 | | - while($section = $sectionResult->fetch_assoc()) { |
201 | | - $section['times'] = array(); |
202 | | - |
203 | | - // Set the course title depending on its section title |
| 193 | + $sectionResult = $dbConn->query($query); |
| 194 | + if (!$sectionResult) { |
| 195 | + die(json_encode(["error" => "mysql", "msg" => $dbConn->error])); |
| 196 | + } |
| 197 | + |
| 198 | + // Collect the sections and their times, modify the section inline |
| 199 | + $sections = []; |
| 200 | + while ($section = $sectionResult->fetch_assoc()) { |
| 201 | + $section['times'] = []; |
| 202 | + |
| 203 | + // Set the course title depending on its section title |
204 | 204 | // @TODO: Replace this with a conditional column in the query |
205 | | - if($section['sectiontitle'] != NULL) { |
206 | | - $section['title'] = $section['sectiontitle']; |
207 | | - } else { |
208 | | - $section['title'] = $section['coursetitle']; |
209 | | - } |
210 | | - unset($section['sectiontitle']); |
211 | | - unset($section['coursetitle']); |
212 | | - |
213 | | - // If it's online, don't bother looking up the times |
214 | | - if($section['type'] == "OL") { |
215 | | - $section['online'] = true; |
216 | | - } else { |
| 205 | + if ($section['sectiontitle'] != null) { |
| 206 | + $section['title'] = $section['sectiontitle']; |
| 207 | + } else { |
| 208 | + $section['title'] = $section['coursetitle']; |
| 209 | + } |
| 210 | + unset($section['sectiontitle']); |
| 211 | + unset($section['coursetitle']); |
| 212 | + |
| 213 | + // If it's online, don't bother looking up the times |
| 214 | + if ($section['type'] == "OL") { |
| 215 | + $section['online'] = true; |
| 216 | + } else { |
217 | 217 | // Look up the times the section meets |
218 | 218 | $query = "SELECT day, start, end, b.code, b.number, b.off_campus AS off, room |
219 | 219 | FROM times AS t |
220 | 220 | JOIN buildings AS b ON b.number=t.building |
221 | 221 | WHERE t.section = '{$section['id']}' |
222 | 222 | ORDER BY day, start"; |
223 | 223 | $timeResult = $dbConn->query($query); |
224 | | - if(!$timeResult) { |
225 | | - die(json_encode(array("error" => "mysql", "msg" => $dbConn->error))); |
| 224 | + if (!$timeResult) { |
| 225 | + die(json_encode(["error" => "mysql", "msg" => $dbConn->error])); |
226 | 226 | } |
227 | 227 |
|
228 | | - while($time = $timeResult->fetch_assoc()) { |
229 | | - $timeOutput = array( |
230 | | - 'start' => $time['start'], |
231 | | - 'end' => $time['end'], |
232 | | - 'day' => $time['day'], |
233 | | - 'bldg' => array( |
234 | | - 'code' => $time['code'], |
235 | | - 'number' => $time['number'], |
| 228 | + while ($time = $timeResult->fetch_assoc()) { |
| 229 | + $timeOutput = [ |
| 230 | + 'start' => $time['start'], |
| 231 | + 'end' => $time['end'], |
| 232 | + 'day' => $time['day'], |
| 233 | + 'bldg' => [ |
| 234 | + 'code' => $time['code'], |
| 235 | + 'number' => $time['number'], |
236 | 236 | 'off_campus' => $time['off'] == '1' |
237 | | - ), |
238 | | - 'room' => $time['room'] |
239 | | - ); |
| 237 | + ], |
| 238 | + 'room' => $time['room'] |
| 239 | + ]; |
240 | 240 | $section['times'][] = $timeOutput; |
241 | 241 | } |
242 | 242 | } |
243 | 243 |
|
244 | 244 | // Add the section to the result set |
245 | | - $sections[] = array( |
246 | | - "id" => $section['id'], |
247 | | - "department" => array("code" => $section['code'], "number" => $section['number']), |
248 | | - "course" => $section['course'], |
249 | | - "section" => $section['section'], |
250 | | - "title" => $section['title'], |
| 245 | + $sections[] = [ |
| 246 | + "id" => $section['id'], |
| 247 | + "department" => ["code" => $section['code'], "number" => $section['number']], |
| 248 | + "course" => $section['course'], |
| 249 | + "section" => $section['section'], |
| 250 | + "title" => $section['title'], |
251 | 251 | "instructor" => $section['instructor'], |
252 | | - "type" => $section['type'], |
253 | | - "maxenroll" => $section['maxenroll'], |
254 | | - "curenroll" => $section['curenroll'], |
255 | | - "times" => $section['times'] |
256 | | - ); |
257 | | - } |
| 252 | + "type" => $section['type'], |
| 253 | + "maxenroll" => $section['maxenroll'], |
| 254 | + "curenroll" => $section['curenroll'], |
| 255 | + "times" => $section['times'] |
| 256 | + ]; |
| 257 | + } |
258 | 258 |
|
259 | | - // Spit out the json |
260 | | - echo json_encode(array("sections" => $sections)); |
261 | | - break; |
| 259 | + // Spit out the json |
| 260 | + echo json_encode(["sections" => $sections]); |
| 261 | + break; |
262 | 262 |
|
263 | 263 | case "courseForSection": |
264 | 264 | echo json_encode(getCourseBySectionId($_POST['id'], true)); |
265 | 265 | break; |
266 | 266 |
|
267 | 267 | default: |
268 | | - die(json_encode(array("error" => "argument", "msg" => "You must provide a valid action."))); |
| 268 | + die(json_encode(["error" => "argument", "msg" => "You must provide a valid action."])); |
269 | 269 |
|
270 | 270 | } |
0 commit comments