Merge branch 'wip-MDL-42238-master' of git://github.com/abgreeve/moodle
[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
43$allowed_what = array('all', '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');
941939ef 63
64 if ($what == 'all') {
65 $users = $user->id;
66 $groups = array();
67 foreach ($courses as $course) {
68 $course_groups = groups_get_all_groups($course->id, $user->id);
4a965824 69 $groups = array_merge($groups, array_keys($course_groups));
941939ef 70 }
e36cd5d3 71 if (empty($groups)) {
72 $groups = false;
73 }
ea185313 74 $courses[SITEID] = new stdClass;
75 $courses[SITEID]->shortname = get_string('globalevents', 'calendar');
941939ef 76 } else {
77 $users = false;
78 $groups = false;
ea185313 79 }
cba168a6 80
da304137
MN
81 // Store the number of days in the week.
82 $numberofdaysinweek = $calendartype->get_num_weekdays();
83
b5f1b8b1 84 switch($time) {
85 case 'weeknow':
da304137
MN
86 $startweekday = calendar_get_starting_weekday();
87 $startmonthday = find_day_in_month($now['mday'] - ($numberofdaysinweek - 1), $startweekday, $now['mon'], $now['year']);
88 $startmonth = $now['mon'];
89 $startyear = $now['year'];
b5f1b8b1 90 if($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
91 list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
92 $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
93 }
94 $timestart = make_timestamp($startyear, $startmonth, $startmonthday);
da304137
MN
95 $endmonthday = $startmonthday + $numberofdaysinweek;
96 $endmonth = $startmonth;
97 $endyear = $startyear;
b5f1b8b1 98 if($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
99 list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
100 $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
101 }
102 $timeend = make_timestamp($endyear, $endmonth, $endmonthday) - 1;
103 break;
104 case 'weeknext':
da304137 105 $startweekday = calendar_get_starting_weekday();
b5f1b8b1 106 $startmonthday = find_day_in_month($now['mday'] + 1, $startweekday, $now['mon'], $now['year']);
da304137
MN
107 $startmonth = $now['mon'];
108 $startyear = $now['year'];
b5f1b8b1 109 if($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
110 list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
111 $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
112 }
113 $timestart = make_timestamp($startyear, $startmonth, $startmonthday);
da304137
MN
114 $endmonthday = $startmonthday + $numberofdaysinweek;
115 $endmonth = $startmonth;
116 $endyear = $startyear;
b5f1b8b1 117 if($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
118 list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
119 $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
120 }
121 $timeend = make_timestamp($endyear, $endmonth, $endmonthday) - 1;
122 break;
123 case 'monthnow':
da304137
MN
124 // Convert to gregorian.
125 $gregoriandate = $calendartype->convert_to_gregorian($now['year'], $now['mon'], 1);
126
127 $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day']);
128 $timeend = make_timestamp($gregoriandate['year'], $gregoriandate['month'],
129 calendar_days_in_month($now['mon'], $now['year']), 23, 59, 59);
b5f1b8b1 130 break;
131 case 'monthnext':
da304137 132 // Get the next month for this calendar.
b5f1b8b1 133 list($nextmonth, $nextyear) = calendar_add_month($now['mon'], $now['year']);
da304137
MN
134
135 // Convert to gregorian.
136 $gregoriandate = $calendartype->convert_to_gregorian($nextyear, $nextmonth, 1);
137
138 // Create the timestamps.
139 $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day']);
140 $timeend = make_timestamp($gregoriandate['year'], $gregoriandate['month'],
141 calendar_days_in_month($nextmonth, $nextyear), 23, 59, 59);
b5f1b8b1 142 break;
ea185313 143 case 'recentupcoming':
144 //Events in the last 5 or next 60 days
145 $timestart = time() - 432000;
146 $timeend = time() + 5184000;
147 break;
af5d990b
AA
148 case 'custom':
149 // Events based on custom date range.
150 $timestart = time() - $CFG->calendar_exportlookback * DAYSECS;
151 $timeend = time() + $CFG->calendar_exportlookahead * DAYSECS;
152 break;
b5f1b8b1 153 }
b5f1b8b1 154 }
155 else {
156 // Parameters given but incorrect, redirect back to export page
157 redirect($CFG->wwwroot.'/calendar/export.php');
b5f1b8b1 158 die();
159 }
160}
941939ef 161$events = calendar_get_events($timestart, $timeend, $users, $groups, array_keys($courses), false);
b5f1b8b1 162
163$ical = new iCalendar;
164$ical->add_property('method', 'PUBLISH');
165foreach($events as $event) {
13534ef7
ML
166 if (!empty($event->modulename)) {
167 $cm = get_coursemodule_from_instance($event->modulename, $event->instance);
168 if (!groups_course_module_visible($cm)) {
169 continue;
170 }
171 }
04f1715e 172 $hostaddress = str_replace('http://', '', $CFG->wwwroot);
173 $hostaddress = str_replace('https://', '', $hostaddress);
174
b5f1b8b1 175 $ev = new iCalendar_event;
04f1715e 176 $ev->add_property('uid', $event->id.'@'.$hostaddress);
b5f1b8b1 177 $ev->add_property('summary', $event->name);
35b7dd7b 178 $ev->add_property('description', clean_param($event->description, PARAM_NOTAGS));
ea185313 179 $ev->add_property('class', 'PUBLIC'); // PUBLIC / PRIVATE / CONFIDENTIAL
180 $ev->add_property('last-modified', Bennu::timestamp_to_datetime($event->timemodified));
b5f1b8b1 181 $ev->add_property('dtstamp', Bennu::timestamp_to_datetime()); // now
182 $ev->add_property('dtstart', Bennu::timestamp_to_datetime($event->timestart)); // when event starts
ea185313 183 if ($event->timeduration > 0) {
184 //dtend is better than duration, because it works in Microsoft Outlook and works better in Korganizer
185 $ev->add_property('dtend', Bennu::timestamp_to_datetime($event->timestart + $event->timeduration));
186 }
187 if ($event->courseid != 0) {
6ca657a7 188 $coursecontext = context_course::instance($event->courseid);
8ebbb06a 189 $ev->add_property('categories', format_string($courses[$event->courseid]->shortname, true, array('context' => $coursecontext)));
ea185313 190 }
b5f1b8b1 191 $ical->add_component($ev);
192}
193
194$serialized = $ical->serialize();
195if(empty($serialized)) {
196 // TODO
197 die('bad serialization');
198}
199
b5f1b8b1 200$filename = 'icalexport.ics';
201
202header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
203header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
204header('Expires: '. gmdate('D, d M Y H:i:s', 0) .'GMT');
205header('Pragma: no-cache');
206header('Accept-Ranges: none'); // Comment out if PDFs do not work...
207header('Content-disposition: attachment; filename='.$filename);
208header('Content-length: '.strlen($serialized));
8a7703ce 209header('Content-type: text/calendar; charset=utf-8');
b5f1b8b1 210
211echo $serialized;