calendar/lib: calendar_set_filters() use pre-fetched context and course recs
[moodle-pu.git] / calendar / export_execute.php
blob2afb1ff6d8c6da7675b196bd78af85c4cfbcc78e
1 <?php // $Id$
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 $username = required_param('username', PARAM_TEXT);
9 $authtoken = required_param('authtoken', PARAM_ALPHANUM);
11 //Fetch user information
12 if (!$user = get_complete_user_data('username', $username)) {
13 //No such user
14 die("No such user '$username'");
17 //Check authentication token
18 if ($authtoken != sha1($username . $user->password)) {
19 die('Invalid authentication token');
22 $what = optional_param('preset_what', 'all', PARAM_ALPHA);
23 $time = optional_param('preset_time', 'weeknow', PARAM_ALPHA);
25 $now = usergetdate(time());
26 // Let's see if we have sufficient and correct data
27 $allowed_what = array('all', 'courses');
28 $allowed_time = array('weeknow', 'weeknext', 'monthnow', 'monthnext', 'recentupcoming');
30 if(!empty($what) && !empty($time)) {
31 if(in_array($what, $allowed_what) && in_array($time, $allowed_time)) {
32 $courses = get_my_courses($user->id, NULL, 'id, visible, shortname');
34 $include_user = ($what == 'all');
35 if ($include_user) {
36 //Also include site (global) events
37 $courses[SITEID] = new stdClass;
38 $courses[SITEID]->shortname = get_string('globalevents', 'calendar');
41 switch($time) {
42 case 'weeknow':
43 $startweekday = get_user_preferences('calendar_startwday', CALENDAR_STARTING_WEEKDAY);
44 $startmonthday = find_day_in_month($now['mday'] - 6, $startweekday, $now['mon'], $now['year']);
45 $startmonth = $now['mon'];
46 $startyear = $now['year'];
47 if($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
48 list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
49 $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
51 $timestart = make_timestamp($startyear, $startmonth, $startmonthday);
52 $endmonthday = $startmonthday + 7;
53 $endmonth = $startmonth;
54 $endyear = $startyear;
55 if($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
56 list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
57 $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
59 $timeend = make_timestamp($endyear, $endmonth, $endmonthday) - 1;
60 break;
61 case 'weeknext':
62 $startweekday = get_user_preferences('calendar_startwday', CALENDAR_STARTING_WEEKDAY);
63 $startmonthday = find_day_in_month($now['mday'] + 1, $startweekday, $now['mon'], $now['year']);
64 $startmonth = $now['mon'];
65 $startyear = $now['year'];
66 if($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
67 list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
68 $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
70 $timestart = make_timestamp($startyear, $startmonth, $startmonthday);
71 $endmonthday = $startmonthday + 7;
72 $endmonth = $startmonth;
73 $endyear = $startyear;
74 if($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
75 list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
76 $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
78 $timeend = make_timestamp($endyear, $endmonth, $endmonthday) - 1;
79 break;
80 case 'monthnow':
81 $timestart = make_timestamp($now['year'], $now['mon'], 1);
82 $timeend = make_timestamp($now['year'], $now['mon'], calendar_days_in_month($now['mon'], $now['year']), 23, 59, 59);
83 break;
84 case 'monthnext':
85 list($nextmonth, $nextyear) = calendar_add_month($now['mon'], $now['year']);
86 $timestart = make_timestamp($nextyear, $nextmonth, 1);
87 $timeend = make_timestamp($nextyear, $nextmonth, calendar_days_in_month($nextmonth, $nextyear), 23, 59, 59);
88 break;
89 case 'recentupcoming':
90 //Events in the last 5 or next 60 days
91 $timestart = time() - 432000;
92 $timeend = time() + 5184000;
93 break;
96 else {
97 // Parameters given but incorrect, redirect back to export page
98 redirect($CFG->wwwroot.'/calendar/export.php');
99 die();
102 $whereclause = calendar_sql_where($timestart, $timeend, $include_user ? array($user->id) : false, false, array_keys($courses), false);
103 if($whereclause === false) {
104 $events = array();
106 else {
107 $events = get_records_select('event', $whereclause, 'timestart');
110 if ($events === false) {
111 $events = array();
114 $ical = new iCalendar;
115 $ical->add_property('method', 'PUBLISH');
116 foreach($events as $event) {
117 if (!empty($event->modulename)) {
118 $cm = get_coursemodule_from_instance($event->modulename, $event->instance);
119 if (!groups_course_module_visible($cm)) {
120 continue;
123 $ev = new iCalendar_event;
124 $ev->add_property('summary', $event->name);
125 $ev->add_property('description', $event->description);
126 $ev->add_property('class', 'PUBLIC'); // PUBLIC / PRIVATE / CONFIDENTIAL
127 $ev->add_property('last-modified', Bennu::timestamp_to_datetime($event->timemodified));
128 $ev->add_property('dtstamp', Bennu::timestamp_to_datetime()); // now
129 $ev->add_property('dtstart', Bennu::timestamp_to_datetime($event->timestart)); // when event starts
130 if ($event->timeduration > 0) {
131 //dtend is better than duration, because it works in Microsoft Outlook and works better in Korganizer
132 $ev->add_property('dtend', Bennu::timestamp_to_datetime($event->timestart + $event->timeduration));
134 if ($event->courseid != 0) {
135 $ev->add_property('categories', $courses[$event->courseid]->shortname);
137 $ical->add_component($ev);
140 $serialized = $ical->serialize();
141 if(empty($serialized)) {
142 // TODO
143 die('bad serialization');
146 //IE compatibility HACK!
147 if(ini_get('zlib.output_compression')) {
148 ini_set('zlib.output_compression', 'Off');
151 $filename = 'icalexport.ics';
153 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
154 header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
155 header('Expires: '. gmdate('D, d M Y H:i:s', 0) .'GMT');
156 header('Pragma: no-cache');
157 header('Accept-Ranges: none'); // Comment out if PDFs do not work...
158 header('Content-disposition: attachment; filename='.$filename);
159 header('Content-length: '.strlen($serialized));
160 header('Content-type: text/calendar');
162 echo $serialized;