3 require_once('../config.php');
4 //require_once($CFG->dirroot.'/course/lib.php');
5 require_once($CFG->dirroot
.'/calendar/lib.php');
6 require_once($CFG->libdir
.'/bennu/bennu.inc.php');
8 $username = required_param('username', PARAM_TEXT
);
9 $authtoken = required_param('authtoken', PARAM_ALPHANUM
);
11 //Fetch user information
12 if (!$user = get_complete_user_data('username', $username)) {
14 die("No such user '$username'");
17 //Check authentication token
18 if ($authtoken != sha1($username . $user->password
)) {
19 die('Invalid authentication token');
22 $what = optional_param('preset_what', 'all', PARAM_ALPHA
);
23 $time = optional_param('preset_time', 'weeknow', PARAM_ALPHA
);
25 $now = usergetdate(time());
26 // Let's see if we have sufficient and correct data
27 $allowed_what = array('all', 'courses');
28 $allowed_time = array('weeknow', 'weeknext', 'monthnow', 'monthnext', 'recentupcoming');
30 if(!empty($what) && !empty($time)) {
31 if(in_array($what, $allowed_what) && in_array($time, $allowed_time)) {
32 $courses = get_my_courses($user->id
, NULL, 'id, visible, shortname');
34 $include_user = ($what == 'all');
36 //Also include site (global) events
37 $courses[SITEID
] = new stdClass
;
38 $courses[SITEID
]->shortname
= get_string('globalevents', 'calendar');
43 $startweekday = get_user_preferences('calendar_startwday', CALENDAR_STARTING_WEEKDAY
);
44 $startmonthday = find_day_in_month($now['mday'] - 6, $startweekday, $now['mon'], $now['year']);
45 $startmonth = $now['mon'];
46 $startyear = $now['year'];
47 if($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
48 list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
49 $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
51 $timestart = make_timestamp($startyear, $startmonth, $startmonthday);
52 $endmonthday = $startmonthday +
7;
53 $endmonth = $startmonth;
54 $endyear = $startyear;
55 if($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
56 list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
57 $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
59 $timeend = make_timestamp($endyear, $endmonth, $endmonthday) - 1;
62 $startweekday = get_user_preferences('calendar_startwday', CALENDAR_STARTING_WEEKDAY
);
63 $startmonthday = find_day_in_month($now['mday'] +
1, $startweekday, $now['mon'], $now['year']);
64 $startmonth = $now['mon'];
65 $startyear = $now['year'];
66 if($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
67 list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
68 $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
70 $timestart = make_timestamp($startyear, $startmonth, $startmonthday);
71 $endmonthday = $startmonthday +
7;
72 $endmonth = $startmonth;
73 $endyear = $startyear;
74 if($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
75 list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
76 $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
78 $timeend = make_timestamp($endyear, $endmonth, $endmonthday) - 1;
81 $timestart = make_timestamp($now['year'], $now['mon'], 1);
82 $timeend = make_timestamp($now['year'], $now['mon'], calendar_days_in_month($now['mon'], $now['year']), 23, 59, 59);
85 list($nextmonth, $nextyear) = calendar_add_month($now['mon'], $now['year']);
86 $timestart = make_timestamp($nextyear, $nextmonth, 1);
87 $timeend = make_timestamp($nextyear, $nextmonth, calendar_days_in_month($nextmonth, $nextyear), 23, 59, 59);
89 case 'recentupcoming':
90 //Events in the last 5 or next 60 days
91 $timestart = time() - 432000;
92 $timeend = time() +
5184000;
97 // Parameters given but incorrect, redirect back to export page
98 redirect($CFG->wwwroot
.'/calendar/export.php');
102 $whereclause = calendar_sql_where($timestart, $timeend, $include_user ?
array($user->id
) : false, false, array_keys($courses), false);
103 if($whereclause === false) {
107 $events = get_records_select('event', $whereclause, 'timestart');
110 if ($events === false) {
114 $ical = new iCalendar
;
115 $ical->add_property('method', 'PUBLISH');
116 foreach($events as $event) {
117 $ev = new iCalendar_event
;
118 $ev->add_property('summary', $event->name
);
119 $ev->add_property('description', $event->description
);
120 $ev->add_property('class', 'PUBLIC'); // PUBLIC / PRIVATE / CONFIDENTIAL
121 $ev->add_property('last-modified', Bennu
::timestamp_to_datetime($event->timemodified
));
122 $ev->add_property('dtstamp', Bennu
::timestamp_to_datetime()); // now
123 $ev->add_property('dtstart', Bennu
::timestamp_to_datetime($event->timestart
)); // when event starts
124 if ($event->timeduration
> 0) {
125 //dtend is better than duration, because it works in Microsoft Outlook and works better in Korganizer
126 $ev->add_property('dtend', Bennu
::timestamp_to_datetime($event->timestart +
$event->timeduration
));
128 if ($event->courseid
!= 0) {
129 $ev->add_property('categories', $courses[$event->courseid
]->shortname
);
131 $ical->add_component($ev);
134 $serialized = $ical->serialize();
135 if(empty($serialized)) {
137 die('bad serialization');
140 //IE compatibility HACK!
141 if(ini_get('zlib.output_compression')) {
142 ini_set('zlib.output_compression', 'Off');
145 $filename = 'icalexport.ics';
147 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
148 header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
149 header('Expires: '. gmdate('D, d M Y H:i:s', 0) .'GMT');
150 header('Pragma: no-cache');
151 header('Accept-Ranges: none'); // Comment out if PDFs do not work...
152 header('Content-disposition: attachment; filename='.$filename);
153 header('Content-length: '.strlen($serialized));
154 header('Content-type: text/calendar');