1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Calendar export
|
|
|
19 |
*
|
|
|
20 |
* @package core_calendar
|
|
|
21 |
* @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
define('NO_MOODLE_COOKIES', true);
|
|
|
26 |
|
|
|
27 |
require_once('../config.php');
|
|
|
28 |
require_once($CFG->dirroot.'/calendar/lib.php');
|
|
|
29 |
require_once($CFG->libdir.'/bennu/bennu.inc.php');
|
|
|
30 |
|
|
|
31 |
raise_memory_limit(MEMORY_HUGE);
|
|
|
32 |
|
|
|
33 |
$userid = optional_param('userid', 0, PARAM_INT);
|
|
|
34 |
$username = optional_param('username', '', PARAM_TEXT);
|
|
|
35 |
$authtoken = required_param('authtoken', PARAM_ALPHANUM);
|
|
|
36 |
$generateurl = optional_param('generateurl', '', PARAM_TEXT);
|
|
|
37 |
|
|
|
38 |
if (empty($CFG->enablecalendarexport)) {
|
|
|
39 |
die('no export');
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
$checkuserid = !empty($userid) && $user = \core_user::get_user($userid);
|
|
|
43 |
// Allowing for fallback check of old url - MDL-27542.
|
|
|
44 |
$checkusername = !empty($username) && $user = \core_user::get_user_by_username($username);
|
|
|
45 |
if ((!$checkuserid && !$checkusername) || !$user) {
|
|
|
46 |
//No such user
|
|
|
47 |
die('Invalid authentication');
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
// Check authentication token.
|
|
|
51 |
$authuserid = !empty($userid) && $authtoken == calendar_get_export_token($user);
|
|
|
52 |
// Allowing for fallback check of old url - MDL-27542.
|
|
|
53 |
$authusername = !empty($username) && $authtoken == sha1($username . $user->password . $CFG->calendar_exportsalt);
|
|
|
54 |
if (!$authuserid && !$authusername) {
|
|
|
55 |
die('Invalid authentication');
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
// Setup up the user including web access logging.
|
|
|
59 |
\core\session\manager::set_user($user);
|
|
|
60 |
|
|
|
61 |
$PAGE->set_context(context_system::instance());
|
|
|
62 |
|
|
|
63 |
// Get the calendar type we are using.
|
|
|
64 |
$calendartype = \core_calendar\type_factory::get_calendar_instance();
|
|
|
65 |
|
|
|
66 |
$what = optional_param('preset_what', 'all', PARAM_ALPHA);
|
|
|
67 |
$time = optional_param('preset_time', 'weeknow', PARAM_ALPHA);
|
|
|
68 |
|
|
|
69 |
$now = $calendartype->timestamp_to_date_array(time());
|
|
|
70 |
|
|
|
71 |
// Let's see if we have sufficient and correct data
|
|
|
72 |
$allowedwhat = ['all', 'user', 'groups', 'courses', 'categories'];
|
|
|
73 |
$allowedtime = ['weeknow', 'weeknext', 'monthnow', 'monthnext', 'recentupcoming', 'custom'];
|
|
|
74 |
|
|
|
75 |
if (!empty($generateurl)) {
|
|
|
76 |
$authtoken = calendar_get_export_token($user);
|
|
|
77 |
$params = array();
|
|
|
78 |
$params['preset_what'] = $what;
|
|
|
79 |
$params['preset_time'] = $time;
|
|
|
80 |
$params['userid'] = $userid;
|
|
|
81 |
$params['authtoken'] = $authtoken;
|
|
|
82 |
$params['generateurl'] = true;
|
|
|
83 |
|
|
|
84 |
$link = new moodle_url('/calendar/export.php', $params);
|
|
|
85 |
redirect($link->out());
|
|
|
86 |
die;
|
|
|
87 |
}
|
|
|
88 |
$paramcategory = false;
|
|
|
89 |
if(!empty($what) && !empty($time)) {
|
|
|
90 |
if(in_array($what, $allowedwhat) && in_array($time, $allowedtime)) {
|
|
|
91 |
$courses = enrol_get_users_courses($user->id, true, 'id, visible, shortname');
|
|
|
92 |
// Array of courses that we will pass to calendar_get_legacy_events() which
|
|
|
93 |
// is initially set to the list of the user's courses.
|
|
|
94 |
$paramcourses = $courses;
|
|
|
95 |
if ($what == 'all' || $what == 'groups') {
|
|
|
96 |
$groups = array();
|
|
|
97 |
foreach ($courses as $course) {
|
|
|
98 |
$course_groups = groups_get_all_groups($course->id, $user->id);
|
|
|
99 |
$groups = array_merge($groups, array_keys($course_groups));
|
|
|
100 |
}
|
|
|
101 |
if (empty($groups)) {
|
|
|
102 |
$groups = false;
|
|
|
103 |
}
|
|
|
104 |
}
|
|
|
105 |
if ($what == 'all') {
|
|
|
106 |
$users = $user->id;
|
|
|
107 |
$courses[SITEID] = new stdClass;
|
|
|
108 |
$courses[SITEID]->shortname = get_string('siteevents', 'calendar');
|
|
|
109 |
$paramcourses[SITEID] = $courses[SITEID];
|
|
|
110 |
$paramcategory = true;
|
|
|
111 |
} else if ($what == 'groups') {
|
|
|
112 |
$users = false;
|
|
|
113 |
$paramcourses = array();
|
|
|
114 |
} else if ($what == 'user') {
|
|
|
115 |
$users = $user->id;
|
|
|
116 |
$groups = false;
|
|
|
117 |
$paramcourses = array();
|
|
|
118 |
} else if ($what == 'categories') {
|
|
|
119 |
$users = $user->id;
|
|
|
120 |
$groups = false;
|
|
|
121 |
$paramcourses = array();
|
|
|
122 |
$paramcategory = true;
|
|
|
123 |
} else {
|
|
|
124 |
$users = false;
|
|
|
125 |
$groups = false;
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
// Store the number of days in the week.
|
|
|
129 |
$numberofdaysinweek = $calendartype->get_num_weekdays();
|
|
|
130 |
|
|
|
131 |
switch($time) {
|
|
|
132 |
case 'weeknow':
|
|
|
133 |
$startweekday = calendar_get_starting_weekday();
|
|
|
134 |
$startmonthday = find_day_in_month($now['mday'] - ($numberofdaysinweek - 1), $startweekday, $now['mon'], $now['year']);
|
|
|
135 |
$startmonth = $now['mon'];
|
|
|
136 |
$startyear = $now['year'];
|
|
|
137 |
if ($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
|
|
|
138 |
list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
|
|
|
139 |
$startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
|
|
|
140 |
}
|
|
|
141 |
$gregoriandate = $calendartype->convert_to_gregorian($startyear, $startmonth, $startmonthday);
|
|
|
142 |
$timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
|
|
|
143 |
$gregoriandate['hour'], $gregoriandate['minute']);
|
|
|
144 |
|
|
|
145 |
$endmonthday = $startmonthday + $numberofdaysinweek;
|
|
|
146 |
$endmonth = $startmonth;
|
|
|
147 |
$endyear = $startyear;
|
|
|
148 |
if ($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
|
|
|
149 |
list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
|
|
|
150 |
$endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
|
|
|
151 |
}
|
|
|
152 |
$gregoriandate = $calendartype->convert_to_gregorian($endyear, $endmonth, $endmonthday);
|
|
|
153 |
$timeend = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
|
|
|
154 |
$gregoriandate['hour'], $gregoriandate['minute']);
|
|
|
155 |
break;
|
|
|
156 |
case 'weeknext':
|
|
|
157 |
$startweekday = calendar_get_starting_weekday();
|
|
|
158 |
$startmonthday = find_day_in_month($now['mday'] + 1, $startweekday, $now['mon'], $now['year']);
|
|
|
159 |
$startmonth = $now['mon'];
|
|
|
160 |
$startyear = $now['year'];
|
|
|
161 |
if ($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
|
|
|
162 |
list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
|
|
|
163 |
$startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
|
|
|
164 |
}
|
|
|
165 |
$gregoriandate = $calendartype->convert_to_gregorian($startyear, $startmonth, $startmonthday);
|
|
|
166 |
$timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
|
|
|
167 |
$gregoriandate['hour'], $gregoriandate['minute']);
|
|
|
168 |
|
|
|
169 |
$endmonthday = $startmonthday + $numberofdaysinweek;
|
|
|
170 |
$endmonth = $startmonth;
|
|
|
171 |
$endyear = $startyear;
|
|
|
172 |
if ($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
|
|
|
173 |
list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
|
|
|
174 |
$endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
|
|
|
175 |
}
|
|
|
176 |
$gregoriandate = $calendartype->convert_to_gregorian($endyear, $endmonth, $endmonthday);
|
|
|
177 |
$timeend = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
|
|
|
178 |
$gregoriandate['hour'], $gregoriandate['minute']);
|
|
|
179 |
break;
|
|
|
180 |
case 'monthnow':
|
|
|
181 |
// Convert to gregorian.
|
|
|
182 |
$gregoriandate = $calendartype->convert_to_gregorian($now['year'], $now['mon'], 1);
|
|
|
183 |
|
|
|
184 |
$timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
|
|
|
185 |
$gregoriandate['hour'], $gregoriandate['minute']);
|
|
|
186 |
$timeend = $timestart + (calendar_days_in_month($now['mon'], $now['year']) * DAYSECS);
|
|
|
187 |
break;
|
|
|
188 |
case 'monthnext':
|
|
|
189 |
// Get the next month for this calendar.
|
|
|
190 |
list($nextmonth, $nextyear) = calendar_add_month($now['mon'], $now['year']);
|
|
|
191 |
|
|
|
192 |
// Convert to gregorian.
|
|
|
193 |
$gregoriandate = $calendartype->convert_to_gregorian($nextyear, $nextmonth, 1);
|
|
|
194 |
|
|
|
195 |
// Create the timestamps.
|
|
|
196 |
$timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'],
|
|
|
197 |
$gregoriandate['hour'], $gregoriandate['minute']);
|
|
|
198 |
$timeend = $timestart + (calendar_days_in_month($nextmonth, $nextyear) * DAYSECS);
|
|
|
199 |
break;
|
|
|
200 |
case 'recentupcoming':
|
|
|
201 |
//Events in the last 5 or next 60 days
|
|
|
202 |
$timestart = time() - 432000;
|
|
|
203 |
$timeend = time() + 5184000;
|
|
|
204 |
break;
|
|
|
205 |
case 'custom':
|
|
|
206 |
// Events based on custom date range.
|
|
|
207 |
$timestart = time() - $CFG->calendar_exportlookback * DAYSECS;
|
|
|
208 |
$timeend = time() + $CFG->calendar_exportlookahead * DAYSECS;
|
|
|
209 |
break;
|
|
|
210 |
}
|
|
|
211 |
}
|
|
|
212 |
else {
|
|
|
213 |
// Parameters given but incorrect, redirect back to export page
|
|
|
214 |
redirect($CFG->wwwroot.'/calendar/export.php');
|
|
|
215 |
die();
|
|
|
216 |
}
|
|
|
217 |
}
|
|
|
218 |
$limitnum = 0;
|
|
|
219 |
$events = calendar_get_legacy_events($timestart, $timeend, $users, $groups, array_keys($paramcourses), false, true,
|
|
|
220 |
$paramcategory, $limitnum);
|
|
|
221 |
|
|
|
222 |
$ical = new iCalendar;
|
|
|
223 |
$ical->add_property('method', 'PUBLISH');
|
|
|
224 |
$ical->add_property('prodid', '-//Moodle Pty Ltd//NONSGML Moodle Version ' . $CFG->version . '//EN');
|
|
|
225 |
foreach($events as $event) {
|
|
|
226 |
if (!empty($event->modulename)) {
|
|
|
227 |
$instances = get_fast_modinfo($event->courseid, $userid)->get_instances_of($event->modulename);
|
|
|
228 |
if (empty($instances[$event->instance]->uservisible)) {
|
|
|
229 |
continue;
|
|
|
230 |
}
|
|
|
231 |
}
|
|
|
232 |
$hostaddress = str_replace('http://', '', $CFG->wwwroot);
|
|
|
233 |
$hostaddress = str_replace('https://', '', $hostaddress);
|
|
|
234 |
|
|
|
235 |
$me = new calendar_event($event); // To use moodle calendar event services.
|
|
|
236 |
$ev = new iCalendar_event; // To export in ical format.
|
|
|
237 |
$ev->add_property('uid', $event->id.'@'.$hostaddress);
|
|
|
238 |
|
|
|
239 |
// Set iCal event summary from event name.
|
|
|
240 |
$ev->add_property('summary', format_string($event->name, true, ['context' => $me->context]));
|
|
|
241 |
|
|
|
242 |
// Format the description text.
|
|
|
243 |
$description = format_text($me->description, $me->format, ['context' => $me->context]);
|
|
|
244 |
// Then convert it to plain text, since it's the only format allowed for the event description property.
|
|
|
245 |
// We use html_to_text in order to convert <br> and <p> tags to new line characters for descriptions in HTML format.
|
|
|
246 |
$description = html_to_text($description, 0);
|
|
|
247 |
$ev->add_property('description', $description);
|
|
|
248 |
|
|
|
249 |
$ev->add_property('class', 'PUBLIC'); // PUBLIC / PRIVATE / CONFIDENTIAL
|
|
|
250 |
$ev->add_property('last-modified', Bennu::timestamp_to_datetime($event->timemodified));
|
|
|
251 |
|
|
|
252 |
if (!empty($event->location)) {
|
|
|
253 |
$ev->add_property('location', $event->location);
|
|
|
254 |
}
|
|
|
255 |
|
|
|
256 |
$ev->add_property('dtstamp', Bennu::timestamp_to_datetime()); // now
|
|
|
257 |
if ($event->timeduration > 0) {
|
|
|
258 |
//dtend is better than duration, because it works in Microsoft Outlook and works better in Korganizer
|
|
|
259 |
$ev->add_property('dtstart', Bennu::timestamp_to_datetime($event->timestart)); // when event starts.
|
|
|
260 |
$ev->add_property('dtend', Bennu::timestamp_to_datetime($event->timestart + $event->timeduration));
|
|
|
261 |
} else if ($event->timeduration == 0) {
|
|
|
262 |
// When no duration is present, the event is instantaneous event, ex - Due date of a module.
|
|
|
263 |
// Moodle doesn't support all day events yet. See MDL-56227.
|
|
|
264 |
$ev->add_property('dtstart', Bennu::timestamp_to_datetime($event->timestart));
|
|
|
265 |
$ev->add_property('dtend', Bennu::timestamp_to_datetime($event->timestart));
|
|
|
266 |
} else {
|
|
|
267 |
// This can be used to represent all day events in future.
|
|
|
268 |
throw new coding_exception("Negative duration is not supported yet.");
|
|
|
269 |
}
|
|
|
270 |
if ($event->courseid != 0) {
|
|
|
271 |
$coursecontext = context_course::instance($event->courseid);
|
|
|
272 |
$ev->add_property('categories', format_string($courses[$event->courseid]->shortname, true, array('context' => $coursecontext)));
|
|
|
273 |
}
|
|
|
274 |
$ical->add_component($ev);
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
$serialized = $ical->serialize();
|
|
|
278 |
if(empty($serialized)) {
|
|
|
279 |
// TODO
|
|
|
280 |
die('bad serialization');
|
|
|
281 |
}
|
|
|
282 |
|
|
|
283 |
$filename = 'icalexport.ics';
|
|
|
284 |
|
|
|
285 |
if (!defined('BEHAT_SITE_RUNNING')) {
|
|
|
286 |
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
|
|
|
287 |
header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
|
|
|
288 |
header('Expires: ' . gmdate('D, d M Y H:i:s', 0) . 'GMT');
|
|
|
289 |
header('Pragma: no-cache');
|
|
|
290 |
header('Accept-Ranges: none'); // Comment out if PDFs do not work...
|
|
|
291 |
header('Content-disposition: attachment; filename=' . $filename);
|
|
|
292 |
header('Content-length: ' . strlen($serialized));
|
|
|
293 |
header('Content-type: text/calendar; charset=utf-8');
|
|
|
294 |
}
|
|
|
295 |
|
|
|
296 |
echo $serialized;
|