/
schedule.php
464 lines (398 loc) · 13.8 KB
/
schedule.php
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
<?php
////////////////////////////////////////////////////////////////////////////
// SCHEDULE LOOKUP
//
// @author Ben Russell (benrr101@csh.rit.edu)
//
// @file schedule.php
// @descrip Loads up the requested schedule from the database.
////////////////////////////////////////////////////////////////////////////
// FUNCTIONS ///////////////////////////////////////////////////////////////
function drawCourse($course, $startTime, $endTime, $startDay, $endDay, $color, $bldg) {
$code = "";
// Iterate over the times that the couse has session
foreach($course['times'] as $time) {
// Skip times that aren't part of the displayed days
if($time['day'] < $startDay || $time['day'] > $endDay) {
continue;
}
// Skip times that aren't part of displayed hours
if($time['start'] < $startTime || $time['start'] > $endTime || $time['end'] > $endTime) {
continue;
}
// Add a div for the time
$code .= "<div class='day" . ($time['day'] - $startDay) . " color{$color}' style = '";
$height = (ceil(($time['end'] - $time['start']) / 30) * 20) - 1;
$topOffset = (floor(($time['start'] - $startTime) / 30) * 20) + 20;
$code .= "height: {$height}px; top: {$topOffset}px";
$code .= "'>";
// Add information about the course
$code .= "<h4";
if($height <= 40) {
// Include code to shorten the header for short classes
$code .= " class='shortHeader'";
}
$code .= ">{$course['title']}</h4><div>";
if($course['courseNum'] != "non") {
if($height > 40) {
$code .= $course['courseNum'] . "<br />";
$code .= $course['instructor'] . "<br />";
}
$code .= $time['bldg'][$bldg] . "-" . $time['room'];
}
$code .= "</div>";
$code .= "</div>";
}
return $code;
}
function drawHeaders($startTime, $endTime, $startDay, $endDay) {
// Draw the days of the week. We're doing it the fancy way.
$code = "";
switch($startDay) {
case 0:
$code .= "<div class='weekday day0'>Sunday</div>";
if($endDay == 0) {break;}
case 1:
$code .= "<div class='weekday day" . (1 - $startDay) . "'>Monday</div>";
if($endDay == 1) {break;}
case 2:
$code .= "<div class='weekday day" . (2 - $startDay) . "'>Tuesday</div>";
if($endDay == 2) {break;}
case 3:
$code .= "<div class='weekday day" . (3 - $startDay) . "'>Wednesday</div>";
if($endDay == 3) {break;}
case 4:
$code .= "<div class='weekday day" . (4 - $startDay) . "'>Thursday</div>";
if($endDay == 4) {break;}
case 5:
$code .= "<div class='weekday day" . (5 - $startDay) . "'>Friday</div>";
if($endDay == 5) {break;}
case 6:
$code .= "<div class='weekday day" . (6 - $startDay) . "'>Saturday</div>";
if($endDay == 6) {break;}
break;
}
// Draw the time divs
for($time = $startTime; $time < $endTime; $time += 30) {
$code .= "<div class='daytime' style='top:" . (floor((($time - $startTime) / 30) * 20) + 20) . "px'>";
$code .= translateTime($time);
$code .= "</div>";
}
return $code;
}
function icalFormatTime($time) {
// Get the GMT difference
$gmtDiff = substr(date("O"), 0, 3);
// Minutes->hrs mins
$hr = (int)($time / 60);
$min = $time % 60;
return str_pad($hr % 24, 2, '0', STR_PAD_LEFT)
. str_pad($min, 2, '0', STR_PAD_LEFT)
. "00";
}
function generateIcal($schedule) {
// Globals
global $HTTPROOTADDRESS;
// We need to lookup the information about the quarter
$quarter = mysql_real_escape_string($schedule['quarter']);
$query = "SELECT start, end, breakstart, breakend FROM quarters WHERE quarter='{$quarter}'";
$result = mysql_query($query);
$quarter = mysql_fetch_assoc($result);
$qtrStart = strtotime($quarter['start']);
$qtrEnd = date("Ymd", strtotime($quarter['end']));
// Start generating code
$code = "";
// Header
$code .= "BEGIN:VCALENDAR\r\n";
$code .= "VERSION:2.0\r\n";
$code .= "PRODID: -//CSH ScheduleMaker//iCal4j 1.0//EN\r\n";
$code .= "METHOD:PUBLISH\r\n";
$code .= "CALSCALE:GREGORIAN\r\n";
// Iterate over all the courses
foreach($schedule['courses'][0] as $course) {
// Skip classes that don't meet
if(empty($course['times'])) {
continue;
}
// Iterate over all the times
foreach($course['times'] as $time) {
$code .= "BEGIN:VEVENT\r\n";
$code .= "UID:" . md5(uniqid(mt_rand(), true) . " @{$HTTPROOTADDRESS}");
$code .= "\r\n";
$code .= "TZID:America/New_York\r\n";
$code .= "DTSTAMP:" . gmdate('Ymd') . "T" . gmdate("His") . "Z\r\n";
$startTime = icalFormatTime($time['start']);
$endTime = icalFormatTime($time['end']);
// The start day of the event MUST be offset by it's day
// the -1 is b/c quarter starts are on Monday(=1)
// This /could/ be done via the RRULE WKST param, but that means
// translating days from numbers to some other esoteric format.
$day = date("Ymd", $qtrStart + ((60*60*24)*($time['day']-1)));
$code .= "DTSTART:" . $day . "T{$startTime}\r\n";
$code .= "DTEND:" . $day . "T{$endTime}\r\n";
$code .= "RRULE:FREQ=WEEKLY;UNTIL={$qtrEnd}\r\n";
$code .= "ORGANIZER:RIT\r\n";
// Course name
$code .= "SUMMARY:{$course['title']}";
if($course['courseNum'] != 'non') {
$code .= " ({$course['courseNum']})";
}
$code .= "\r\n";
// Meeting location
if($course['courseNum'] != 'non') {
$bldg = $time['bldg'][$schedule['building']];
$code .= "LOCATION:{$bldg}-{$time['room']}\r\n";
}
$code .= "END:VEVENT\r\n";
}
}
$code .= "END:VCALENDAR\r\n";
return $code;
}
function generateScheduleFromCourses($courses) {
// Grab the start/end time/day
$courseList = $courses['courses'][0];
$startTime = $courses['startTime'];
$endTime = $courses['endTime'];
$startDay = $courses['startDay'];
$endDay = $courses['endDay'];
// Do some calculations for height/width
$schedHeight = floor(($endTime - $startTime) / 30) * 20 + 20;
$schedWidth = (($endDay - $startDay) * 100) + 200;
// Start outputting the code
$code = "<div class='schedSupaWrapper'>";
$code .= "<div class='scheduleWrapper' style='height:{$schedHeight}px; width:{$schedWidth}px'>";
$code .= "<div class='schedule' style='height:{$schedHeight}px; width:{$schedWidth}px'>";
$code .= "<img src='img/grid.png'>";
$code .= drawHeaders($startTime, $endTime, $startDay, $endDay);
// Storage for potential online courses
$onlineCourses = array();
// Output each of the courses in the schedule
for($i = 0; $i < count($courseList); $i++) {
if($courseList[$i]['courseNum'] != 'non' && $courseList[$i]['online']) {
// Add it to the list of online courses
$onlineCourses[] = $courseList[$i];
continue;
}
$color = $i % 4;
$code .= drawCourse($courseList[$i], $startTime, $endTime, $startDay, $endDay, $color, $courses['building']);
}
$code .= "</div></div>";
// Output a notice if there were online courses
if(count($onlineCourses)) {
$code .= "<div class='schedNotes' style='width:{$schedWidth}px'>";
$code .= "<p>Notice: This schedule contains online courses: ";
foreach($onlineCourses as $course) {
$code .= $course['courseNum'];
}
$code .= "</p></div>";
}
$code .= "</div>";
return $code;
}
function getScheduleFromId($id) {
// Query to see if the id exists, if we can update the last accessed time,
// then the id most definitely exists.
$query = "UPDATE schedules SET datelastaccessed = NOW() WHERE id={$id}";
$result = mysql_query($query);
$query = "SELECT startday, endday, starttime, endtime, building, quarter FROM schedules WHERE id={$id}";
$result = mysql_query($query);
$scheduleInfo = mysql_fetch_assoc($result);
if(!$scheduleInfo) {
return NULL;
}
// Grab the metadata of the schedule
$startDay = (int)$scheduleInfo['startday'];
$endDay = (int)$scheduleInfo['endday'];
$startTime = (int)$scheduleInfo['starttime'];
$endTime = (int)$scheduleInfo['endtime'];
$building = $scheduleInfo['building'];
$quarter = $scheduleInfo['quarter'];
// Create storage for the courses that will be returned
$schedule = array();
// It exists, so grab all the courses that exist for this schedule
$query = "SELECT section FROM schedulecourses WHERE schedule = {$id}";
$result = mysql_query($query);
while($course = mysql_fetch_assoc($result)) {
$schedule[] = getCourseBySectionId($course['section']);
}
// Grab all the non courses that exist for this schedule
$query = "SELECT * FROM schedulenoncourses WHERE schedule = $id";
$result = mysql_query($query);
if(!$result) {
echo mysql_error();
}
while($nonCourseInfo = mysql_fetch_assoc($result)) {
$schedule[] = array(
"title" => $nonCourseInfo['title'],
"courseNum" => "non",
"times" => array(array(
"day" => $nonCourseInfo['day'],
"start" => $nonCourseInfo['start'],
"end" => $nonCourseInfo['end']
))
);
}
return array(
//@TODO: Fix this hackish error below
"courses" => array($schedule),
"startTime" => $startTime,
"endTime" => $endTime,
"startDay" => $startDay,
"endDay" => $endDay,
"building" => $building,
"quarter" => $quarter
);
}
function getScheduleFromOldId($id) {
$query = "SELECT id FROM schedules WHERE oldid = '{$id}'";
$result = mysql_query($query);
if(!$result || mysql_num_rows($result) != 1) {
return NULL;
} else {
$newId = mysql_fetch_assoc($result);
$newId = $newId['id'];
$schedule = getScheduleFromId($newId);
$schedule['id'] = $newId;
return $schedule;
}
}
function queryOldId($id) {
// Grab all the courses that match the id
$query = "SELECT c.section FROM schedules AS s, schedulecourses AS c WHERE s.id = c.section AND s.oldid = '{$id}'";
}
// MAIN EXECUTION //////////////////////////////////////////////////////////
// Determine the output mode
$mode = (empty($_REQUEST['mode'])) ? "schedule" : $_REQUEST['mode'];
// Switch on the mode
switch($mode) {
case "print":
// PRINTABLE SCHEDULE //////////////////////////////////////////////
// No header, no footer, just the schedule
?>
<html>
<head>
<title>Your Schedule - Schedule Maker</title>
<link href='./inc/style_print.css' rel='stylesheet' type='text/css' />
<script src='./js/jquery.js' type='text/javascript'></script>
<script src='./js/schedule.js' type='text/javascript'></script>
</head>
<body>
<h1 id='header' style='text-align:center'></h1>
<div id='schedules'></div>
<? require "inc/footer_print.inc"; ?>
</body>
<script type='text/javascript'>
// Load the data out of the local storage and store it in the
// global fields
data = eval("(" + window.sessionStorage.getItem("scheduleJson") + ")");
schedules = data.courses;
startday = data.startDay;
endday = data.endDay;
starttime = data.startTime;
endtime = data.endTime;
SCHEDPERPAGE= 1;
// Calculate the quarter for header purposes
if(data.quarter > 20130) {
// @TODO: Figure out what in the fuck to do for semesters
} else {
// Split it up and store it as the header
var year = data.quarter.substring(0,4);
var quarter = data.quarter.substring(4);
switch(quarter) {
case '1': quarter = "Fall"; break;
case '2': quarter = "Winter"; break;
case '3': quarter = "Spring"; break;
case '4': quarter = "Summer"; break;
}
$("#header").html("My " + quarter + " " + year + "-" + (parseInt(year)+1) + " Schedule");
}
// Calculate the schedule height and width
schedHeight = (Math.floor((endtime - starttime) / 30) * 20) + 20;
schedWidth = ((endday - startday) * 100) + 200;
// Run the show schedules thing
drawPage(0, true);
// Load the print dialog
$(document).ready(function() {
if($("div").length) {
window.print();
}
});
</script>
</html>
<?
break;
case "ical":
// iCAL FORMAT SCHEDULE ////////////////////////////////////////////
// If we don't have a schedule, die!
if(empty($_GET['id'])) {
die("You must provide a schedule");
}
// Database connection is required
require_once("inc/databaseConn.php");
require_once("inc/timeFunctions.php");
// Decode the schedule
$schedule = getScheduleFromId(hexdec($_GET['id']));
// Set header for ical mime, output the xml
header("Content-Type: text/calendar");
header("Content-Disposition: attachment; filename=generated_schedule" . md5(serialize($schedule)) . ".ics");
echo generateIcal($schedule);
break;
case "old":
// OLD SCHEDULE FORMAT /////////////////////////////////////////////
require "./inc/header.inc";
// Grab the schedule
$schedule = getScheduleFromOldId($_GET['id']);
if($schedule == NULL) {
?>
<div class='schedUrl error'>
<p><span style='font-weight:bold'>Fatal Error:</span> The requested schedule does not exist!</p>
</div>
<?
} else {
?>
<div class='schedUrl'>
<p>This schedule was created using the old schedule maker!</p>
<p>You should now access this schedule at:
<a href="<?= $HTTPROOTADDRESS ?>schedule.php?id=<?= dechex($schedule['id']) ?>">
<?= $HTTPROOTADDRESS ?>schedule.php?id=<?= dechex($schedule['id']) ?>
</a>
</p>
</div>
<?
echo generateScheduleFromCourses($schedule);
}
require "./inc/footer.inc";
break;
case "schedule":
// DEFAULT SCHEDULE FORMAT /////////////////////////////////////////
require "./inc/header.inc";
$schedule = getScheduleFromId(hexdec($_GET['id']));
if($schedule == NULL) {
?>
<div class='schedUrl error'>
<p><span style='font-weight:bold'>Fatal Error:</span> The requested schedule does not exist!</p>
</div>
<?
} else {
echo generateScheduleFromCourses($schedule);
}
// Translate the schedule into json and escape '
$json = json_encode($schedule);
$json = htmlentities($json, ENT_COMPAT);
?>
<div id='savedControls'>
<input type='hidden' id='schedJson' value="<?= $json ?>" name='schedJson' />
<button type='button' id='forkButton'>Copy and Edit</button>
<button type='button' id='printButton'>Print Schedule</button>
</div>
<script src='js/savedSchedule.js' type='text/javascript'></script>
<?
require "./inc/footer.inc";
break;
default:
// INVALID OPTION //////////////////////////////////////////////////
echo "Invalid option!";
break;
}
?>