MDL-53632 competency: Consistently use the terms frameworks, plans, ...
[moodle.git] / calendar / export_execute.php
blobb999205db5284d7250aac148391d48cbfd9ba9d5
1 <?php
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 $userid = optional_param('userid', 0, PARAM_INT);
9 $username = optional_param('username', '', PARAM_TEXT);
10 $authtoken = required_param('authtoken', PARAM_ALPHANUM);
11 $generateurl = optional_param('generateurl', '', PARAM_TEXT);
13 if (empty($CFG->enablecalendarexport)) {
14 die('no export');
17 //Fetch user information
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');
21 if (!$checkuserid && !$checkusername) {
22 //No such user
23 die('Invalid authentication');
26 //Check authentication token
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);
30 if (!$authuserid && !$authusername) {
31 die('Invalid authentication');
34 // Get the calendar type we are using.
35 $calendartype = \core_calendar\type_factory::get_calendar_instance();
37 $what = optional_param('preset_what', 'all', PARAM_ALPHA);
38 $time = optional_param('preset_time', 'weeknow', PARAM_ALPHA);
40 $now = $calendartype->timestamp_to_date_array(time());
42 // Let's see if we have sufficient and correct data
43 $allowed_what = array('all', 'courses');
44 $allowed_time = array('weeknow', 'weeknext', 'monthnow', 'monthnext', 'recentupcoming', 'custom');
46 if (!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;
55 $link = new moodle_url('/calendar/export.php', $params);
56 redirect($link->out());
57 die;
60 if(!empty($what) && !empty($time)) {
61 if(in_array($what, $allowed_what) && in_array($time, $allowed_time)) {
62 $courses = enrol_get_users_courses($user->id, true, 'id, visible, shortname');
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);
69 $groups = array_merge($groups, array_keys($course_groups));
71 if (empty($groups)) {
72 $groups = false;
74 $courses[SITEID] = new stdClass;
75 $courses[SITEID]->shortname = get_string('globalevents', 'calendar');
76 } else {
77 $users = false;
78 $groups = false;
81 // Store the number of days in the week.
82 $numberofdaysinweek = $calendartype->get_num_weekdays();
84 switch($time) {
85 case 'weeknow':
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'];
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);
94 $gregoriandate = $calendartype->convert_to_gregorian($startyear, $startmonth, $startmonthday);
95 $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
96 $gregoriandate['hour'], $gregoriandate['minute']);
98 $endmonthday = $startmonthday + $numberofdaysinweek;
99 $endmonth = $startmonth;
100 $endyear = $startyear;
101 if($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
102 list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
103 $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
105 $gregoriandate = $calendartype->convert_to_gregorian($endyear, $endmonth, $endmonthday);
106 $timeend = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
107 $gregoriandate['hour'], $gregoriandate['minute']);
108 break;
109 case 'weeknext':
110 $startweekday = calendar_get_starting_weekday();
111 $startmonthday = find_day_in_month($now['mday'] + 1, $startweekday, $now['mon'], $now['year']);
112 $startmonth = $now['mon'];
113 $startyear = $now['year'];
114 if($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
115 list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
116 $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
118 $gregoriandate = $calendartype->convert_to_gregorian($startyear, $startmonth, $startmonthday);
119 $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
120 $gregoriandate['hour'], $gregoriandate['minute']);
122 $endmonthday = $startmonthday + $numberofdaysinweek;
123 $endmonth = $startmonth;
124 $endyear = $startyear;
125 if($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
126 list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
127 $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
129 $gregoriandate = $calendartype->convert_to_gregorian($endyear, $endmonth, $endmonthday);
130 $timeend = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
131 $gregoriandate['hour'], $gregoriandate['minute']);
132 break;
133 case 'monthnow':
134 // Convert to gregorian.
135 $gregoriandate = $calendartype->convert_to_gregorian($now['year'], $now['mon'], 1);
137 $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
138 $gregoriandate['hour'], $gregoriandate['minute']);
139 $timeend = $timestart + (calendar_days_in_month($now['mon'], $now['year']) * DAYSECS);
140 break;
141 case 'monthnext':
142 // Get the next month for this calendar.
143 list($nextmonth, $nextyear) = calendar_add_month($now['mon'], $now['year']);
145 // Convert to gregorian.
146 $gregoriandate = $calendartype->convert_to_gregorian($nextyear, $nextmonth, 1);
148 // Create the timestamps.
149 $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
150 $gregoriandate['hour'], $gregoriandate['minute']);
151 $timeend = $timestart + (calendar_days_in_month($nextmonth, $nextyear) * DAYSECS);
152 break;
153 case 'recentupcoming':
154 //Events in the last 5 or next 60 days
155 $timestart = time() - 432000;
156 $timeend = time() + 5184000;
157 break;
158 case 'custom':
159 // Events based on custom date range.
160 $timestart = time() - $CFG->calendar_exportlookback * DAYSECS;
161 $timeend = time() + $CFG->calendar_exportlookahead * DAYSECS;
162 break;
165 else {
166 // Parameters given but incorrect, redirect back to export page
167 redirect($CFG->wwwroot.'/calendar/export.php');
168 die();
171 $events = calendar_get_events($timestart, $timeend, $users, $groups, array_keys($courses), false);
173 $ical = new iCalendar;
174 $ical->add_property('method', 'PUBLISH');
175 foreach($events as $event) {
176 if (!empty($event->modulename)) {
177 $cm = get_coursemodule_from_instance($event->modulename, $event->instance);
178 if (!\core_availability\info_module::is_user_visible($cm, $userid, false)) {
179 continue;
182 $hostaddress = str_replace('http://', '', $CFG->wwwroot);
183 $hostaddress = str_replace('https://', '', $hostaddress);
185 $ev = new iCalendar_event;
186 $ev->add_property('uid', $event->id.'@'.$hostaddress);
187 $ev->add_property('summary', $event->name);
188 $ev->add_property('description', clean_param($event->description, PARAM_NOTAGS));
189 $ev->add_property('class', 'PUBLIC'); // PUBLIC / PRIVATE / CONFIDENTIAL
190 $ev->add_property('last-modified', Bennu::timestamp_to_datetime($event->timemodified));
191 $ev->add_property('dtstamp', Bennu::timestamp_to_datetime()); // now
192 if ($event->timeduration > 0) {
193 //dtend is better than duration, because it works in Microsoft Outlook and works better in Korganizer
194 $ev->add_property('dtstart', Bennu::timestamp_to_datetime($event->timestart)); // when event starts.
195 $ev->add_property('dtend', Bennu::timestamp_to_datetime($event->timestart + $event->timeduration));
196 } else {
197 // When no duration is present, ie an all day event, VALUE should be date instead of time and dtend = dtstart + 1 day.
198 $ev->add_property('dtstart', Bennu::timestamp_to_date($event->timestart), array('value' => 'DATE')); // All day event.
199 $ev->add_property('dtend', Bennu::timestamp_to_date($event->timestart + DAYSECS), array('value' => 'DATE')); // All day event.
201 if ($event->courseid != 0) {
202 $coursecontext = context_course::instance($event->courseid);
203 $ev->add_property('categories', format_string($courses[$event->courseid]->shortname, true, array('context' => $coursecontext)));
205 $ical->add_component($ev);
208 $serialized = $ical->serialize();
209 if(empty($serialized)) {
210 // TODO
211 die('bad serialization');
214 $filename = 'icalexport.ics';
216 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
217 header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
218 header('Expires: '. gmdate('D, d M Y H:i:s', 0) .'GMT');
219 header('Pragma: no-cache');
220 header('Accept-Ranges: none'); // Comment out if PDFs do not work...
221 header('Content-disposition: attachment; filename='.$filename);
222 header('Content-length: '.strlen($serialized));
223 header('Content-type: text/calendar; charset=utf-8');
225 echo $serialized;