MDL-70237 payment: Allow html tags in gateway description
[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');
8d70a113 21if ((!$checkuserid && !$checkusername) || !$user) {
d52777b4 22 //No such user
804656ff 23 die('Invalid authentication');
b5f1b8b1 24}
25
ea185313 26//Check authentication token
ea9c822f 27$authuserid = !empty($userid) && $authtoken == calendar_get_export_token($user);
d52777b4
RW
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
f19f6cce
SL
43$allowedwhat = ['all', 'user', 'groups', 'courses', 'categories'];
44$allowedtime = ['weeknow', 'weeknext', 'monthnow', 'monthnext', 'recentupcoming', 'custom'];
b5f1b8b1 45
d52777b4 46if (!empty($generateurl)) {
ea9c822f 47 $authtoken = calendar_get_export_token($user);
d52777b4
RW
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}
f19f6cce 59$paramcategory = false;
b5f1b8b1 60if(!empty($what) && !empty($time)) {
f19f6cce 61 if(in_array($what, $allowedwhat) && in_array($time, $allowedtime)) {
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;
375d82ca 79 $courses[SITEID]->shortname = get_string('siteevents', 'calendar');
3de445bb 80 $paramcourses[SITEID] = $courses[SITEID];
f19f6cce 81 $paramcategory = true;
65521981
AS
82 } else if ($what == 'groups') {
83 $users = false;
3de445bb 84 $paramcourses = array();
65521981
AS
85 } else if ($what == 'user') {
86 $users = $user->id;
87 $groups = false;
3de445bb 88 $paramcourses = array();
f19f6cce
SL
89 } else if ($what == 'categories') {
90 $users = $user->id;
91 $groups = false;
92 $paramcourses = array();
93 $paramcategory = true;
941939ef 94 } else {
95 $users = false;
96 $groups = false;
ea185313 97 }
cba168a6 98
da304137
MN
99 // Store the number of days in the week.
100 $numberofdaysinweek = $calendartype->get_num_weekdays();
101
b5f1b8b1 102 switch($time) {
103 case 'weeknow':
23a29de7 104 $startweekday = calendar_get_starting_weekday();
da304137
MN
105 $startmonthday = find_day_in_month($now['mday'] - ($numberofdaysinweek - 1), $startweekday, $now['mon'], $now['year']);
106 $startmonth = $now['mon'];
107 $startyear = $now['year'];
23a29de7
MN
108 if ($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
109 list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
b5f1b8b1 110 $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
111 }
1032966c
MN
112 $gregoriandate = $calendartype->convert_to_gregorian($startyear, $startmonth, $startmonthday);
113 $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
114 $gregoriandate['hour'], $gregoriandate['minute']);
115
da304137
MN
116 $endmonthday = $startmonthday + $numberofdaysinweek;
117 $endmonth = $startmonth;
118 $endyear = $startyear;
23a29de7
MN
119 if ($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
120 list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
b5f1b8b1 121 $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
122 }
1032966c
MN
123 $gregoriandate = $calendartype->convert_to_gregorian($endyear, $endmonth, $endmonthday);
124 $timeend = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
125 $gregoriandate['hour'], $gregoriandate['minute']);
b5f1b8b1 126 break;
127 case 'weeknext':
23a29de7 128 $startweekday = calendar_get_starting_weekday();
b5f1b8b1 129 $startmonthday = find_day_in_month($now['mday'] + 1, $startweekday, $now['mon'], $now['year']);
da304137
MN
130 $startmonth = $now['mon'];
131 $startyear = $now['year'];
23a29de7
MN
132 if ($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
133 list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
b5f1b8b1 134 $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
135 }
1032966c
MN
136 $gregoriandate = $calendartype->convert_to_gregorian($startyear, $startmonth, $startmonthday);
137 $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
138 $gregoriandate['hour'], $gregoriandate['minute']);
139
da304137
MN
140 $endmonthday = $startmonthday + $numberofdaysinweek;
141 $endmonth = $startmonth;
142 $endyear = $startyear;
23a29de7
MN
143 if ($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
144 list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
b5f1b8b1 145 $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
146 }
1032966c
MN
147 $gregoriandate = $calendartype->convert_to_gregorian($endyear, $endmonth, $endmonthday);
148 $timeend = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
149 $gregoriandate['hour'], $gregoriandate['minute']);
b5f1b8b1 150 break;
151 case 'monthnow':
da304137
MN
152 // Convert to gregorian.
153 $gregoriandate = $calendartype->convert_to_gregorian($now['year'], $now['mon'], 1);
154
1032966c
MN
155 $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
156 $gregoriandate['hour'], $gregoriandate['minute']);
23a29de7 157 $timeend = $timestart + (calendar_days_in_month($now['mon'], $now['year']) * DAYSECS);
b5f1b8b1 158 break;
159 case 'monthnext':
da304137 160 // Get the next month for this calendar.
23a29de7 161 list($nextmonth, $nextyear) = calendar_add_month($now['mon'], $now['year']);
da304137
MN
162
163 // Convert to gregorian.
164 $gregoriandate = $calendartype->convert_to_gregorian($nextyear, $nextmonth, 1);
165
166 // Create the timestamps.
1032966c
MN
167 $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
168 $gregoriandate['hour'], $gregoriandate['minute']);
23a29de7 169 $timeend = $timestart + (calendar_days_in_month($nextmonth, $nextyear) * DAYSECS);
b5f1b8b1 170 break;
ea185313 171 case 'recentupcoming':
172 //Events in the last 5 or next 60 days
173 $timestart = time() - 432000;
174 $timeend = time() + 5184000;
175 break;
af5d990b
AA
176 case 'custom':
177 // Events based on custom date range.
178 $timestart = time() - $CFG->calendar_exportlookback * DAYSECS;
179 $timeend = time() + $CFG->calendar_exportlookahead * DAYSECS;
180 break;
b5f1b8b1 181 }
b5f1b8b1 182 }
183 else {
184 // Parameters given but incorrect, redirect back to export page
185 redirect($CFG->wwwroot.'/calendar/export.php');
b5f1b8b1 186 die();
187 }
188}
e099259a 189$limitnum = 0;
f19f6cce 190$events = calendar_get_legacy_events($timestart, $timeend, $users, $groups, array_keys($paramcourses), false, true,
e099259a 191 $paramcategory, $limitnum);
b5f1b8b1 192
193$ical = new iCalendar;
194$ical->add_property('method', 'PUBLISH');
5e0af1da 195$ical->add_property('prodid', '-//Moodle Pty Ltd//NONSGML Moodle Version ' . $CFG->version . '//EN');
b5f1b8b1 196foreach($events as $event) {
5900513f
MG
197 if (!empty($event->modulename)) {
198 $instances = get_fast_modinfo($event->courseid, $userid)->get_instances_of($event->modulename);
199 if (empty($instances[$event->instance]->uservisible)) {
13534ef7
ML
200 continue;
201 }
202 }
04f1715e 203 $hostaddress = str_replace('http://', '', $CFG->wwwroot);
204 $hostaddress = str_replace('https://', '', $hostaddress);
205
231ddd5a
SS
206 $me = new calendar_event($event); // To use moodle calendar event services.
207 $ev = new iCalendar_event; // To export in ical format.
04f1715e 208 $ev->add_property('uid', $event->id.'@'.$hostaddress);
231ddd5a 209
e083ac1c
JP
210 // Set iCal event summary from event name.
211 $ev->add_property('summary', format_string($event->name, true, ['context' => $me->context]));
212
213 // Format the description text.
214 $description = format_text($me->description, $me->format, ['context' => $me->context]);
215 // Then convert it to plain text, since it's the only format allowed for the event description property.
216 // We use html_to_text in order to convert <br> and <p> tags to new line characters for descriptions in HTML format.
217 $description = html_to_text($description, 0);
218 $ev->add_property('description', $description);
231ddd5a 219
ea185313 220 $ev->add_property('class', 'PUBLIC'); // PUBLIC / PRIVATE / CONFIDENTIAL
221 $ev->add_property('last-modified', Bennu::timestamp_to_datetime($event->timemodified));
ef4e04ee
MH
222
223 if (!empty($event->location)) {
224 $ev->add_property('location', $event->location);
225 }
226
b5f1b8b1 227 $ev->add_property('dtstamp', Bennu::timestamp_to_datetime()); // now
ea185313 228 if ($event->timeduration > 0) {
229 //dtend is better than duration, because it works in Microsoft Outlook and works better in Korganizer
2be327dc 230 $ev->add_property('dtstart', Bennu::timestamp_to_datetime($event->timestart)); // when event starts.
ea185313 231 $ev->add_property('dtend', Bennu::timestamp_to_datetime($event->timestart + $event->timeduration));
8aaba9e4
AA
232 } else if ($event->timeduration == 0) {
233 // When no duration is present, the event is instantaneous event, ex - Due date of a module.
234 // Moodle doesn't support all day events yet. See MDL-56227.
235 $ev->add_property('dtstart', Bennu::timestamp_to_datetime($event->timestart));
236 $ev->add_property('dtend', Bennu::timestamp_to_datetime($event->timestart));
2be327dc 237 } else {
8aaba9e4
AA
238 // This can be used to represent all day events in future.
239 throw new coding_exception("Negative duration is not supported yet.");
ea185313 240 }
241 if ($event->courseid != 0) {
6ca657a7 242 $coursecontext = context_course::instance($event->courseid);
8ebbb06a 243 $ev->add_property('categories', format_string($courses[$event->courseid]->shortname, true, array('context' => $coursecontext)));
ea185313 244 }
b5f1b8b1 245 $ical->add_component($ev);
246}
247
248$serialized = $ical->serialize();
249if(empty($serialized)) {
250 // TODO
251 die('bad serialization');
252}
253
b5f1b8b1 254$filename = 'icalexport.ics';
255
256header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
257header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
258header('Expires: '. gmdate('D, d M Y H:i:s', 0) .'GMT');
259header('Pragma: no-cache');
260header('Accept-Ranges: none'); // Comment out if PDFs do not work...
261header('Content-disposition: attachment; filename='.$filename);
262header('Content-length: '.strlen($serialized));
8a7703ce 263header('Content-type: text/calendar; charset=utf-8');
b5f1b8b1 264
265echo $serialized;