Skip to content

Commit 68fd7c0

Browse files
authored
Merge pull request #343 from ComputerScienceHouse/feature/mstrodl/ical-repeat-weekly
repeated calendar events
2 parents 52ecd36 + 7bcb3a6 commit 68fd7c0

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

api/src/Schedule.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ private function firstDayAfterDate($weekday, $startDate) {
3434
return $startDate + (60*60*24*($weekday-$weekdayOfStart));
3535
}
3636

37+
private function hashTime($time, $schedule) {
38+
return ($time['start'] . "-" .
39+
$time['end'] . "-" .
40+
$time['bldg'][$schedule['bldgStyle']] . "-" .
41+
$time['room']);
42+
}
43+
3744
public function generateIcal($schedule) {
3845
date_default_timezone_set('America/New_York');
3946
// Globals
@@ -64,27 +71,44 @@ public function generateIcal($schedule) {
6471
continue;
6572
}
6673

67-
// Iterate over all the times
74+
$scheduleByTime = array();
6875
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) {
6987
$code .= "BEGIN:VEVENT\r\n";
7088
$code .= "UID:" . md5(uniqid(mt_rand(), true) . " @{$HTTPROOTADDRESS}");
7189
$code .= "\r\n";
7290
$code .= "TZID:America/New_York\r\n";
7391
$code .= "DTSTAMP:" . gmdate('Ymd') . "T" . gmdate("His") . "Z\r\n";
7492

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']);
7795

7896
// The start day of the event MUST be offset by it's day
7997
// the -1 is b/c quarter starts are on Monday(=1)
8098
// This /could/ be done via the RRULE WKST param, but that means
8199
// translating days from numbers to some other esoteric format.
82100
// @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));
84102

85103
$code .= "DTSTART;TZID=America/New_York:{$day}T{$startTime}\r\n";
86104
$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";
88112
$code .= "ORGANIZER:RIT\r\n";
89113

90114
// Course name
@@ -96,8 +120,8 @@ public function generateIcal($schedule) {
96120

97121
// Meeting location
98122
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";
101125
}
102126

103127
$code .= "END:VEVENT\r\n";

0 commit comments

Comments
 (0)