-
Notifications
You must be signed in to change notification settings - Fork 2
/
quest.php
269 lines (238 loc) · 8.64 KB
/
quest.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
<?php
function uw_waterloo_quest_schedule($input, $format, $summary = '@code @type in @location', $description = '@code-@section: @name (@type) in @location with @prof') {
//icalendar day formats
$arr_days = array('m'=>'MO', 't'=>'TU', 'w'=>'WE', 'h'=>'TH', 'f'=>'FR', 's'=>'SA', 'u'=>'SU');
$arr_num_days = array('m'=>1, 't'=>2, 'w'=>3, 'h'=>4, 'f'=>5, 's'=>6, 'u'=>7);
//info that Quest gives us and schedule field it's useful for
$code='';
$name='';
$section='';
$type='';
$days='';
$time_start='';
$time_end='';
$location='';
$prof='';
$date_start='';
$date_end='';
//the result of this madness will be stored here
$ical_array=array();
$js_array=array();
//start at the beginning of the string
$pos=0;
//this is how long our quest schedule is
$total_length=strlen($input);
//detect whether MM and DD needs to be swapped according to locale
//if en-CA, swap
//otherwise, no swap
$swap = false;
$locale = $_SERVER["HTTP_ACCEPT_LANGUAGE"];
if (strpos($locale, 'en-CA') !== FALSE){
$swap = true;
}
//start the algorithm
while ( $pos < $total_length && $pos >= 0) {
//assume we didn't find anything
$found_what_we_need=false;
//the regex will match 1 of 3 completely different things - title, body, or partial body
//we have to process what we get in order
//detect if it's 24h time or 12h time!
$ampm = false;
if(preg_match('/(AM)|(PM)/', $input)){
$ampm = true;
}
$time = $ampm ?
'([1]{0,1}\d\:[0-5]\d[AP]M)\ -\
([1]{0,1}\d\:[0-5]\d[AP]M)\s+'
:'(\d{2}\:\d{2})\ -\ (\d{2}\:\d{2})\s+';
$regex = '/
(
(\w{2,5}\ \w{3,4})\ -\ #code
([^\r\n]+) #name
)
|
(
\d{4}\s+ #class number
(\d{3})\s+ #section
(\w{3})\s+ #component
([MThWF]{0,6})\s+ #days and times
'.$time.'
([\w\ ]+\s+[0-9]{1,5}[A-Z]?|TBA)\s+ #room
([\w\ \-\,\r\n]+)\s+ #instructor
(\d{2,4}\/\d{2,4}\/\d{2,4})\ -\
(\d{2,4}\/\d{2,4}\/\d{2,4})
)
|
(
([MThWF]{0,6})\s+
'.$time.'
([\w\ ]+\s+[0-9]{1,5}[A-Z]?|TBA)\s+
([\w\ \-\,\r\n]+)\s+
(\d{2,4}\/\d{2,4}\/\d{2,4})\ -\
(\d{2,4}\/\d{2,4}\/\d{2,4})
)/x';
if(preg_match($regex, $input, $matches, PREG_OFFSET_CAPTURE, $pos)){
//check what we found by seeing where the regex stopped
$number=count($matches);
switch($number){
//found title
case 4:
//get the strings and put them in the variables
$code=$matches[2][0];
$name=trim($matches[3][0]);
//reset the rest of the variables so if something goes wrong, the error doesn't perpetuate
$section='';
$type='';
$days='';
$time='';
$location='';
$prof='';
break;
//found body
case 14:
$found_what_we_need=true;
$section = $matches[5][0];
$type = $matches[6][0];
$days = strtolower(str_replace('Th','h',$matches[7][0]));
$time_start = _uw_build_time_string($matches[8][0], $ampm);
$time_end = _uw_build_time_string($matches[9][0], $ampm);
$location = trim(_uw_StripExtraSpace($matches[10][0]));
//$prof =substr(strrchr(' '.trim($matches[11][0]), ' '),1);//get only the last name
$prof = trim($matches[11][0]);
$date_start = strtotime(normalize_date($matches[12][0], $swap));
$date_end = strtotime(normalize_date($matches[13][0], $swap));
break;
//found partial body
case 22:
$found_what_we_need=true;
$days = strtolower(str_replace('Th','h',$matches[15][0]));
$time_start = _uw_build_time_string($matches[16][0], $ampm);
$time_end = _uw_build_time_string($matches[17][0], $ampm);
$location = trim(_uw_StripExtraSpace($matches[18][0]));
//$prof=substr(strrchr(' '.trim($matches[17][0]), ' '),1);//get only the last name
$prof = trim($matches[19][0]);
$date_start = strtotime(normalize_date($matches[20][0], $swap));
$date_end = strtotime(normalize_date($matches[21][0], $swap));
break;
}
//move to the end of what was matched and continue
$pos=$matches[0][1]+strlen($matches[0][0]);
} else $pos=-1; //this exits the while because we are done if we can't find anything we're looking for
//add to the array if we have all the info.
if($found_what_we_need && $days != ''){
if($format == 'icalendar'){
//format the days of the week
$formatted_days = '';
$number_days = array();
foreach(str_split($days) as $day){
$formatted_days .= ','.$arr_days[$day];
$number_days[] = $arr_num_days[$day];
}
$formatted_days = substr($formatted_days,1);
//move the start date to the first valid day of the week
while(!in_array(date('N',$date_start), $number_days)){
$date_start = strtotime('+1 day', $date_start);
}
//build the result
$string_data = array(
'@code' => $code,
'@section' => $section,
'@name' => $name,
'@type' => $type,
'@location' => $location,
'@prof' => $prof,
);
$result = array(
'summary' => strtr($summary, $string_data),
'description' => strtr($description, $string_data),
'start' => date('Ymd',$date_start).'T'.$time_start,
'end' => date('Ymd',$date_start).'T'.$time_end,
'location' => $location,
);
//this information is only relevant for repeating events
if($date_start != $date_end){
$result['until'] = date('Ymd',$date_end).'T'.$time_end;
$result['days'] = $formatted_days;
}
//escape commas
//see rfc2445 section 4.1
$result['description'] = str_replace("\r\n", "\r\n ", str_replace(',', '\,', $result['description']));
$result['summary'] = str_replace("\r\n", "\r\n ", str_replace(',', '\,', $result['summary']));
//add this class to the list
$ical_array[] = $result;
} elseif ($format == 'js'){
$location = explode(' ', $location);
$result = array(
'code' => $code,
'name' => $name,
'section' => $section,
'type' => $type,
'days' => $days,
'time_start' => $time_start,
'time_end' => $time_end,
'location' => $location[0],
'prof' => $prof,
'date_start' => $date_start,
'date_end' => $date_end,
);
$js_array[] = $result;
}
}
}
if($format == 'icalendar'){
$result_str = '';
foreach($ical_array as $class){
$result_str .= "\r\nBEGIN:VEVENT";
$result_str .= "\r\nDTSTART:$class[start]";
$result_str .= "\r\nDTEND:$class[end]";
if(array_key_exists('until', $class) && $class['until']){
$result_str .= "\r\nRRULE:FREQ=WEEKLY;UNTIL=$class[until];WKST=SU;BYDAY=$class[days]";
}
$result_str .= "\r\nSUMMARY:$class[summary]";
$result_str .= "\r\nLOCATION:$class[location]";
$result_str .= "\r\nDESCRIPTION:$class[description]";
$result_str .= "\r\nEND:VEVENT";
}
$result_str = "BEGIN:VCALENDAR".
"\r\nVERSION:2.0".
"\r\nPRODID:-//hacksw/handcal//NONSGML v1.0//EN".
$result_str.
"\r\nEND:VCALENDAR";
return $result_str;
} elseif ($format='js') {
return $js_array;
}
}
function _uw_StripExtraSpace($s){
$newstr = '';
for($i = 0; $i < strlen($s); $i++)
{
$newstr = $newstr . substr($s, $i, 1);
if(substr($s, $i, 1) == ' ')
while(substr($s, $i + 1, 1) == ' ')
$i++;
}
return $newstr;
}
function _uw_build_time_string($time, $ampm){
if($ampm){
$pm = strpos($time, 'PM') !== false;
$time = substr($time, 0, -2);
$parts = explode(':', $time);
if(strlen($parts[0]) != 2){
$parts[0] = '0'.$parts[0];
}
if(strlen($parts[1]) != 2){
$parts[1] = '0'.$parts[1];
}
$time = implode($parts).'00';
if($pm && $parts[0] != 12){
$time += 120000;
}
return $time;
} else {
$parts = explode(':', $time);
return implode($parts).'00';
}
}
?>