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
)) {
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) ||
!$user) {
23 die('Invalid authentication');
26 //Check authentication token
27 $authuserid = !empty($userid) && $authtoken == calendar_get_export_token($user);
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 $PAGE->set_context(context_system
::instance());
36 // Get the calendar type we are using.
37 $calendartype = \core_calendar\type_factory
::get_calendar_instance();
39 $what = optional_param('preset_what', 'all', PARAM_ALPHA
);
40 $time = optional_param('preset_time', 'weeknow', PARAM_ALPHA
);
42 $now = $calendartype->timestamp_to_date_array(time());
44 // Let's see if we have sufficient and correct data
45 $allowedwhat = ['all', 'user', 'groups', 'courses', 'categories'];
46 $allowedtime = ['weeknow', 'weeknext', 'monthnow', 'monthnext', 'recentupcoming', 'custom'];
48 if (!empty($generateurl)) {
49 $authtoken = calendar_get_export_token($user);
51 $params['preset_what'] = $what;
52 $params['preset_time'] = $time;
53 $params['userid'] = $userid;
54 $params['authtoken'] = $authtoken;
55 $params['generateurl'] = true;
57 $link = new moodle_url('/calendar/export.php', $params);
58 redirect($link->out());
61 $paramcategory = false;
62 if(!empty($what) && !empty($time)) {
63 if(in_array($what, $allowedwhat) && in_array($time, $allowedtime)) {
64 $courses = enrol_get_users_courses($user->id
, true, 'id, visible, shortname');
65 // Array of courses that we will pass to calendar_get_legacy_events() which
66 // is initially set to the list of the user's courses.
67 $paramcourses = $courses;
68 if ($what == 'all' ||
$what == 'groups') {
70 foreach ($courses as $course) {
71 $course_groups = groups_get_all_groups($course->id
, $user->id
);
72 $groups = array_merge($groups, array_keys($course_groups));
80 $courses[SITEID
] = new stdClass
;
81 $courses[SITEID
]->shortname
= get_string('siteevents', 'calendar');
82 $paramcourses[SITEID
] = $courses[SITEID
];
83 $paramcategory = true;
84 } else if ($what == 'groups') {
86 $paramcourses = array();
87 } else if ($what == 'user') {
90 $paramcourses = array();
91 } else if ($what == 'categories') {
94 $paramcourses = array();
95 $paramcategory = true;
101 // Store the number of days in the week.
102 $numberofdaysinweek = $calendartype->get_num_weekdays();
106 $startweekday = calendar_get_starting_weekday();
107 $startmonthday = find_day_in_month($now['mday'] - ($numberofdaysinweek - 1), $startweekday, $now['mon'], $now['year']);
108 $startmonth = $now['mon'];
109 $startyear = $now['year'];
110 if ($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
111 list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
112 $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
114 $gregoriandate = $calendartype->convert_to_gregorian($startyear, $startmonth, $startmonthday);
115 $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
116 $gregoriandate['hour'], $gregoriandate['minute']);
118 $endmonthday = $startmonthday +
$numberofdaysinweek;
119 $endmonth = $startmonth;
120 $endyear = $startyear;
121 if ($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
122 list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
123 $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
125 $gregoriandate = $calendartype->convert_to_gregorian($endyear, $endmonth, $endmonthday);
126 $timeend = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
127 $gregoriandate['hour'], $gregoriandate['minute']);
130 $startweekday = calendar_get_starting_weekday();
131 $startmonthday = find_day_in_month($now['mday'] +
1, $startweekday, $now['mon'], $now['year']);
132 $startmonth = $now['mon'];
133 $startyear = $now['year'];
134 if ($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
135 list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
136 $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
138 $gregoriandate = $calendartype->convert_to_gregorian($startyear, $startmonth, $startmonthday);
139 $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
140 $gregoriandate['hour'], $gregoriandate['minute']);
142 $endmonthday = $startmonthday +
$numberofdaysinweek;
143 $endmonth = $startmonth;
144 $endyear = $startyear;
145 if ($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
146 list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
147 $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
149 $gregoriandate = $calendartype->convert_to_gregorian($endyear, $endmonth, $endmonthday);
150 $timeend = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
151 $gregoriandate['hour'], $gregoriandate['minute']);
154 // Convert to gregorian.
155 $gregoriandate = $calendartype->convert_to_gregorian($now['year'], $now['mon'], 1);
157 $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
158 $gregoriandate['hour'], $gregoriandate['minute']);
159 $timeend = $timestart +
(calendar_days_in_month($now['mon'], $now['year']) * DAYSECS
);
162 // Get the next month for this calendar.
163 list($nextmonth, $nextyear) = calendar_add_month($now['mon'], $now['year']);
165 // Convert to gregorian.
166 $gregoriandate = $calendartype->convert_to_gregorian($nextyear, $nextmonth, 1);
168 // Create the timestamps.
169 $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
170 $gregoriandate['hour'], $gregoriandate['minute']);
171 $timeend = $timestart +
(calendar_days_in_month($nextmonth, $nextyear) * DAYSECS
);
173 case 'recentupcoming':
174 //Events in the last 5 or next 60 days
175 $timestart = time() - 432000;
176 $timeend = time() +
5184000;
179 // Events based on custom date range.
180 $timestart = time() - $CFG->calendar_exportlookback
* DAYSECS
;
181 $timeend = time() +
$CFG->calendar_exportlookahead
* DAYSECS
;
186 // Parameters given but incorrect, redirect back to export page
187 redirect($CFG->wwwroot
.'/calendar/export.php');
192 $events = calendar_get_legacy_events($timestart, $timeend, $users, $groups, array_keys($paramcourses), false, true,
193 $paramcategory, $limitnum);
195 $ical = new iCalendar
;
196 $ical->add_property('method', 'PUBLISH');
197 $ical->add_property('prodid', '-//Moodle Pty Ltd//NONSGML Moodle Version ' . $CFG->version
. '//EN');
198 foreach($events as $event) {
199 if (!empty($event->modulename
)) {
200 $instances = get_fast_modinfo($event->courseid
, $userid)->get_instances_of($event->modulename
);
201 if (empty($instances[$event->instance
]->uservisible
)) {
205 $hostaddress = str_replace('http://', '', $CFG->wwwroot
);
206 $hostaddress = str_replace('https://', '', $hostaddress);
208 $me = new calendar_event($event); // To use moodle calendar event services.
209 $ev = new iCalendar_event
; // To export in ical format.
210 $ev->add_property('uid', $event->id
.'@'.$hostaddress);
212 // Set iCal event summary from event name.
213 $ev->add_property('summary', format_string($event->name
, true, ['context' => $me->context
]));
215 // Format the description text.
216 $description = format_text($me->description
, $me->format
, ['context' => $me->context
]);
217 // Then convert it to plain text, since it's the only format allowed for the event description property.
218 // We use html_to_text in order to convert <br> and <p> tags to new line characters for descriptions in HTML format.
219 $description = html_to_text($description, 0);
220 $ev->add_property('description', $description);
222 $ev->add_property('class', 'PUBLIC'); // PUBLIC / PRIVATE / CONFIDENTIAL
223 $ev->add_property('last-modified', Bennu
::timestamp_to_datetime($event->timemodified
));
225 if (!empty($event->location
)) {
226 $ev->add_property('location', $event->location
);
229 $ev->add_property('dtstamp', Bennu
::timestamp_to_datetime()); // now
230 if ($event->timeduration
> 0) {
231 //dtend is better than duration, because it works in Microsoft Outlook and works better in Korganizer
232 $ev->add_property('dtstart', Bennu
::timestamp_to_datetime($event->timestart
)); // when event starts.
233 $ev->add_property('dtend', Bennu
::timestamp_to_datetime($event->timestart +
$event->timeduration
));
234 } else if ($event->timeduration
== 0) {
235 // When no duration is present, the event is instantaneous event, ex - Due date of a module.
236 // Moodle doesn't support all day events yet. See MDL-56227.
237 $ev->add_property('dtstart', Bennu
::timestamp_to_datetime($event->timestart
));
238 $ev->add_property('dtend', Bennu
::timestamp_to_datetime($event->timestart
));
240 // This can be used to represent all day events in future.
241 throw new coding_exception("Negative duration is not supported yet.");
243 if ($event->courseid
!= 0) {
244 $coursecontext = context_course
::instance($event->courseid
);
245 $ev->add_property('categories', format_string($courses[$event->courseid
]->shortname
, true, array('context' => $coursecontext)));
247 $ical->add_component($ev);
250 $serialized = $ical->serialize();
251 if(empty($serialized)) {
253 die('bad serialization');
256 $filename = 'icalexport.ics';
258 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
259 header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
260 header('Expires: '. gmdate('D, d M Y H:i:s', 0) .'GMT');
261 header('Pragma: no-cache');
262 header('Accept-Ranges: none'); // Comment out if PDFs do not work...
263 header('Content-disposition: attachment; filename='.$filename);
264 header('Content-length: '.strlen($serialized));
265 header('Content-type: text/calendar; charset=utf-8');