weekly release 3.4dev
[moodle.git] / calendar / export_execute.php
CommitLineData
e7521559 1<?php
b5f1b8b1 2
3require_once('../config.php');
4//require_once($CFG->dirroot.'/course/lib.php');
5require_once($CFG->dirroot.'/calendar/lib.php');
6require_once($CFG->libdir.'/bennu/bennu.inc.php');
7
d52777b4
RW
8$userid = optional_param('userid', 0, PARAM_INT);
9$username = optional_param('username', '', PARAM_TEXT);
ea185313 10$authtoken = required_param('authtoken', PARAM_ALPHANUM);
d52777b4 11$generateurl = optional_param('generateurl', '', PARAM_TEXT);
ea185313 12
dbf9d4cb 13if (empty($CFG->enablecalendarexport)) {
14 die('no export');
15}
faf0e91a 16
ea185313 17//Fetch user information
d52777b4
RW
18$checkuserid = !empty($userid) && $user = $DB->get_record('user', array('id' => $userid), 'id,password');
19//allowing for fallback check of old url - MDL-27542
20$checkusername = !empty($username) && $user = $DB->get_record('user', array('username' => $username), 'id,password');
21if (!$checkuserid && !$checkusername) {
22 //No such user
804656ff 23 die('Invalid authentication');
b5f1b8b1 24}
25
ea185313 26//Check authentication token
d52777b4
RW
27$authuserid = !empty($userid) && $authtoken == sha1($userid . $user->password . $CFG->calendar_exportsalt);
28//allowing for fallback check of old url - MDL-27542
29$authusername = !empty($username) && $authtoken == sha1($username . $user->password . $CFG->calendar_exportsalt);
30if (!$authuserid && !$authusername) {
804656ff 31 die('Invalid authentication');
ea185313 32}
b5f1b8b1 33
da304137
MN
34// Get the calendar type we are using.
35$calendartype = \core_calendar\type_factory::get_calendar_instance();
36
cba168a6 37$what = optional_param('preset_what', 'all', PARAM_ALPHA);
38$time = optional_param('preset_time', 'weeknow', PARAM_ALPHA);
b5f1b8b1 39
da304137
MN
40$now = $calendartype->timestamp_to_date_array(time());
41
b5f1b8b1 42// Let's see if we have sufficient and correct data
65521981 43$allowed_what = array('all', 'user', 'groups', 'courses');
af5d990b 44$allowed_time = array('weeknow', 'weeknext', 'monthnow', 'monthnext', 'recentupcoming', 'custom');
b5f1b8b1 45
d52777b4
RW
46if (!empty($generateurl)) {
47 $authtoken = sha1($user->id . $user->password . $CFG->calendar_exportsalt);
48 $params = array();
49 $params['preset_what'] = $what;
50 $params['preset_time'] = $time;
51 $params['userid'] = $userid;
52 $params['authtoken'] = $authtoken;
53 $params['generateurl'] = true;
54
55 $link = new moodle_url('/calendar/export.php', $params);
56 redirect($link->out());
57 die;
58}
59
b5f1b8b1 60if(!empty($what) && !empty($time)) {
61 if(in_array($what, $allowed_what) && in_array($time, $allowed_time)) {
df997f84 62 $courses = enrol_get_users_courses($user->id, true, 'id, visible, shortname');
37c38dfc 63 // Array of courses that we will pass to calendar_get_legacy_events() which
12cbce0a 64 // is initially set to the list of the user's courses.
3de445bb 65 $paramcourses = $courses;
65521981 66 if ($what == 'all' || $what == 'groups') {
941939ef 67 $groups = array();
68 foreach ($courses as $course) {
69 $course_groups = groups_get_all_groups($course->id, $user->id);
4a965824 70 $groups = array_merge($groups, array_keys($course_groups));
941939ef 71 }
e36cd5d3 72 if (empty($groups)) {
73 $groups = false;
74 }
65521981
AS
75 }
76 if ($what == 'all') {
77 $users = $user->id;
ea185313 78 $courses[SITEID] = new stdClass;
79 $courses[SITEID]->shortname = get_string('globalevents', 'calendar');
3de445bb 80 $paramcourses[SITEID] = $courses[SITEID];
65521981
AS
81 } else if ($what == 'groups') {
82 $users = false;
3de445bb 83 $paramcourses = array();
65521981
AS
84 } else if ($what == 'user') {
85 $users = $user->id;
86 $groups = false;
3de445bb 87 $paramcourses = array();
941939ef 88 } else {
89 $users = false;
90 $groups = false;
ea185313 91 }
cba168a6 92
da304137
MN
93 // Store the number of days in the week.
94 $numberofdaysinweek = $calendartype->get_num_weekdays();
95
b5f1b8b1 96 switch($time) {
97 case 'weeknow':
23a29de7 98 $startweekday = calendar_get_starting_weekday();
da304137
MN
99 $startmonthday = find_day_in_month($now['mday'] - ($numberofdaysinweek - 1), $startweekday, $now['mon'], $now['year']);
100 $startmonth = $now['mon'];
101 $startyear = $now['year'];
23a29de7
MN
102 if ($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
103 list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
b5f1b8b1 104 $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
105 }
1032966c
MN
106 $gregoriandate = $calendartype->convert_to_gregorian($startyear, $startmonth, $startmonthday);
107 $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
108 $gregoriandate['hour'], $gregoriandate['minute']);
109
da304137
MN
110 $endmonthday = $startmonthday + $numberofdaysinweek;
111 $endmonth = $startmonth;
112 $endyear = $startyear;
23a29de7
MN
113 if ($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
114 list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
b5f1b8b1 115 $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
116 }
1032966c
MN
117 $gregoriandate = $calendartype->convert_to_gregorian($endyear, $endmonth, $endmonthday);
118 $timeend = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
119 $gregoriandate['hour'], $gregoriandate['minute']);
b5f1b8b1 120 break;
121 case 'weeknext':
23a29de7 122 $startweekday = calendar_get_starting_weekday();
b5f1b8b1 123 $startmonthday = find_day_in_month($now['mday'] + 1, $startweekday, $now['mon'], $now['year']);
da304137
MN
124 $startmonth = $now['mon'];
125 $startyear = $now['year'];
23a29de7
MN
126 if ($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
127 list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
b5f1b8b1 128 $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
129 }
1032966c
MN
130 $gregoriandate = $calendartype->convert_to_gregorian($startyear, $startmonth, $startmonthday);
131 $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
132 $gregoriandate['hour'], $gregoriandate['minute']);
133
da304137
MN
134 $endmonthday = $startmonthday + $numberofdaysinweek;
135 $endmonth = $startmonth;
136 $endyear = $startyear;
23a29de7
MN
137 if ($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
138 list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
b5f1b8b1 139 $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
140 }
1032966c
MN
141 $gregoriandate = $calendartype->convert_to_gregorian($endyear, $endmonth, $endmonthday);
142 $timeend = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
143 $gregoriandate['hour'], $gregoriandate['minute']);
b5f1b8b1 144 break;
145 case 'monthnow':
da304137
MN
146 // Convert to gregorian.
147 $gregoriandate = $calendartype->convert_to_gregorian($now['year'], $now['mon'], 1);
148
1032966c
MN
149 $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
150 $gregoriandate['hour'], $gregoriandate['minute']);
23a29de7 151 $timeend = $timestart + (calendar_days_in_month($now['mon'], $now['year']) * DAYSECS);
b5f1b8b1 152 break;
153 case 'monthnext':
da304137 154 // Get the next month for this calendar.
23a29de7 155 list($nextmonth, $nextyear) = calendar_add_month($now['mon'], $now['year']);
da304137
MN
156
157 // Convert to gregorian.
158 $gregoriandate = $calendartype->convert_to_gregorian($nextyear, $nextmonth, 1);
159
160 // Create the timestamps.
1032966c
MN
161 $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
162 $gregoriandate['hour'], $gregoriandate['minute']);
23a29de7 163 $timeend = $timestart + (calendar_days_in_month($nextmonth, $nextyear) * DAYSECS);
b5f1b8b1 164 break;
ea185313 165 case 'recentupcoming':
166 //Events in the last 5 or next 60 days
167 $timestart = time() - 432000;
168 $timeend = time() + 5184000;
169 break;
af5d990b
AA
170 case 'custom':
171 // Events based on custom date range.
172 $timestart = time() - $CFG->calendar_exportlookback * DAYSECS;
173 $timeend = time() + $CFG->calendar_exportlookahead * DAYSECS;
174 break;
b5f1b8b1 175 }
b5f1b8b1 176 }
177 else {
178 // Parameters given but incorrect, redirect back to export page
179 redirect($CFG->wwwroot.'/calendar/export.php');
b5f1b8b1 180 die();
181 }
182}
22753c8c 183$events = calendar_get_legacy_events($timestart, $timeend, $users, $groups, array_keys($paramcourses), false);
b5f1b8b1 184
185$ical = new iCalendar;
186$ical->add_property('method', 'PUBLISH');
187foreach($events as $event) {
5900513f
MG
188 if (!empty($event->modulename)) {
189 $instances = get_fast_modinfo($event->courseid, $userid)->get_instances_of($event->modulename);
190 if (empty($instances[$event->instance]->uservisible)) {
13534ef7
ML
191 continue;
192 }
193 }
04f1715e 194 $hostaddress = str_replace('http://', '', $CFG->wwwroot);
195 $hostaddress = str_replace('https://', '', $hostaddress);
196
b5f1b8b1 197 $ev = new iCalendar_event;
04f1715e 198 $ev->add_property('uid', $event->id.'@'.$hostaddress);
b5f1b8b1 199 $ev->add_property('summary', $event->name);
35b7dd7b 200 $ev->add_property('description', clean_param($event->description, PARAM_NOTAGS));
ea185313 201 $ev->add_property('class', 'PUBLIC'); // PUBLIC / PRIVATE / CONFIDENTIAL
202 $ev->add_property('last-modified', Bennu::timestamp_to_datetime($event->timemodified));
b5f1b8b1 203 $ev->add_property('dtstamp', Bennu::timestamp_to_datetime()); // now
ea185313 204 if ($event->timeduration > 0) {
205 //dtend is better than duration, because it works in Microsoft Outlook and works better in Korganizer
2be327dc 206 $ev->add_property('dtstart', Bennu::timestamp_to_datetime($event->timestart)); // when event starts.
ea185313 207 $ev->add_property('dtend', Bennu::timestamp_to_datetime($event->timestart + $event->timeduration));
2be327dc 208 } else {
6ae33efc 209 // When no duration is present, ie an all day event, VALUE should be date instead of time and dtend = dtstart + 1 day.
2be327dc 210 $ev->add_property('dtstart', Bennu::timestamp_to_date($event->timestart), array('value' => 'DATE')); // All day event.
6ae33efc 211 $ev->add_property('dtend', Bennu::timestamp_to_date($event->timestart + DAYSECS), array('value' => 'DATE')); // All day event.
ea185313 212 }
213 if ($event->courseid != 0) {
6ca657a7 214 $coursecontext = context_course::instance($event->courseid);
8ebbb06a 215 $ev->add_property('categories', format_string($courses[$event->courseid]->shortname, true, array('context' => $coursecontext)));
ea185313 216 }
b5f1b8b1 217 $ical->add_component($ev);
218}
219
220$serialized = $ical->serialize();
221if(empty($serialized)) {
222 // TODO
223 die('bad serialization');
224}
225
b5f1b8b1 226$filename = 'icalexport.ics';
227
228header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
229header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
230header('Expires: '. gmdate('D, d M Y H:i:s', 0) .'GMT');
231header('Pragma: no-cache');
232header('Accept-Ranges: none'); // Comment out if PDFs do not work...
233header('Content-disposition: attachment; filename='.$filename);
234header('Content-length: '.strlen($serialized));
8a7703ce 235header('Content-type: text/calendar; charset=utf-8');
b5f1b8b1 236
237echo $serialized;