@@ -34,6 +34,13 @@ private function firstDayAfterDate($weekday, $startDate) {
34
34
return $ startDate + (60 *60 *24 *($ weekday -$ weekdayOfStart ));
35
35
}
36
36
37
+ private function hashTime ($ time , $ schedule ) {
38
+ return ($ time ['start ' ] . "- " .
39
+ $ time ['end ' ] . "- " .
40
+ $ time ['bldg ' ][$ schedule ['bldgStyle ' ]] . "- " .
41
+ $ time ['room ' ]);
42
+ }
43
+
37
44
public function generateIcal ($ schedule ) {
38
45
date_default_timezone_set ('America/New_York ' );
39
46
// Globals
@@ -64,27 +71,44 @@ public function generateIcal($schedule) {
64
71
continue ;
65
72
}
66
73
67
- // Iterate over all the times
74
+ $ scheduleByTime = array ();
68
75
foreach ($ course ['times ' ] as $ time ) {
76
+ $ hash = $ this ->hashTime ($ time , $ schedule );
77
+ // Add to scheduleByTime array
78
+ if (array_key_exists ($ hash , $ scheduleByTime )) {
79
+ $ scheduleByTime [$ hash ][] = $ time ;
80
+ } else {
81
+ $ scheduleByTime [$ hash ] = array ($ time );
82
+ }
83
+ }
84
+
85
+ // Iterate over all the times
86
+ foreach ($ scheduleByTime as $ times ) {
69
87
$ code .= "BEGIN:VEVENT \r\n" ;
70
88
$ code .= "UID: " . md5 (uniqid (mt_rand (), true ) . " @ {$ HTTPROOTADDRESS }" );
71
89
$ code .= "\r\n" ;
72
90
$ code .= "TZID:America/New_York \r\n" ;
73
91
$ code .= "DTSTAMP: " . gmdate ('Ymd ' ) . "T " . gmdate ("His " ) . "Z \r\n" ;
74
92
75
- $ startTime = $ this ->icalFormatTime ($ time ['start ' ]);
76
- $ endTime = $ this ->icalFormatTime ($ time ['end ' ]);
93
+ $ startTime = $ this ->icalFormatTime ($ times [ 0 ] ['start ' ]);
94
+ $ endTime = $ this ->icalFormatTime ($ times [ 0 ] ['end ' ]);
77
95
78
96
// The start day of the event MUST be offset by it's day
79
97
// the -1 is b/c quarter starts are on Monday(=1)
80
98
// This /could/ be done via the RRULE WKST param, but that means
81
99
// translating days from numbers to some other esoteric format.
82
100
// @TODO: Retrieve the timezone from php or the config file
83
- $ day = date ("Ymd " , $ this ->firstDayAfterDate ($ time ['day ' ], $ termStart ));
101
+ $ day = date ("Ymd " , $ this ->firstDayAfterDate ($ times [ 0 ] ['day ' ], $ termStart ));
84
102
85
103
$ code .= "DTSTART;TZID=America/New_York: {$ day }T {$ startTime }\r\n" ;
86
104
$ code .= "DTEND;TZID=America/New_York: {$ day }T {$ endTime }\r\n" ;
87
- $ code .= "RRULE:FREQ=WEEKLY;UNTIL= {$ termEnd }\r\n" ;
105
+ $ dayCodes = array ('SU ' , 'MO ' , 'TU ' , 'WE ' , 'TH ' , 'FR ' , 'SA ' );
106
+ $ days = array ();
107
+ foreach ($ times as $ time ) {
108
+ $ days [] = $ dayCodes [$ time ['day ' ]];
109
+ }
110
+ $ dayString = implode (', ' , $ days );
111
+ $ code .= "RRULE:FREQ=WEEKLY;INTERVAL=1;WKST=SU;BYDAY= {$ dayString };UNTIL= {$ termEnd }\r\n" ;
88
112
$ code .= "ORGANIZER:RIT \r\n" ;
89
113
90
114
// Course name
@@ -96,8 +120,8 @@ public function generateIcal($schedule) {
96
120
97
121
// Meeting location
98
122
if ($ course ['courseNum ' ] != 'non ' ) {
99
- $ bldg = $ time ['bldg ' ][$ schedule ['bldgStyle ' ]];
100
- $ code .= "LOCATION: {$ bldg }- {$ time ['room ' ]}\r\n" ;
123
+ $ bldg = $ times [ 0 ] ['bldg ' ][$ schedule ['bldgStyle ' ]];
124
+ $ code .= "LOCATION: {$ bldg }- {$ times [ 0 ] ['room ' ]}\r\n" ;
101
125
}
102
126
103
127
$ code .= "END:VEVENT \r\n" ;
0 commit comments