MDL-10286 Implementing the view feedback page. Added links to the user, module and...
[moodle-pu.git] / calendar / export_execute.php
blob72904e11b16bd0cbdfbec4413ee4d9b3536f3ebf
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);
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();
103 $whereclause = calendar_sql_where($timestart, $timeend, $include_user ? array($user->id) : false, false, array_keys($courses), false);
104 if($whereclause === false) {
105 $events = array();
107 else {
108 $events = get_records_select('event', $whereclause, 'timestart');
111 if ($events === false) {
112 $events = array();
115 $ical = new iCalendar;
116 $ical->add_property('method', 'PUBLISH');
117 foreach($events as $event) {
118 $ev = new iCalendar_event;
119 $ev->add_property('summary', $event->name);
120 $ev->add_property('description', $event->description);
121 $ev->add_property('class', 'PUBLIC'); // PUBLIC / PRIVATE / CONFIDENTIAL
122 $ev->add_property('last-modified', Bennu::timestamp_to_datetime($event->timemodified));
123 $ev->add_property('dtstamp', Bennu::timestamp_to_datetime()); // now
124 $ev->add_property('dtstart', Bennu::timestamp_to_datetime($event->timestart)); // when event starts
125 if ($event->timeduration > 0) {
126 //dtend is better than duration, because it works in Microsoft Outlook and works better in Korganizer
127 $ev->add_property('dtend', Bennu::timestamp_to_datetime($event->timestart + $event->timeduration));
129 if ($event->courseid != 0) {
130 $ev->add_property('categories', $courses[$event->courseid]->shortname);
132 $ical->add_component($ev);
135 $serialized = $ical->serialize();
136 if(empty($serialized)) {
137 // TODO
138 die('bad serialization');
141 //IE compatibility HACK!
142 if(ini_get('zlib.output_compression')) {
143 ini_set('zlib.output_compression', 'Off');
146 $filename = 'icalexport.ics';
148 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
149 header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
150 header('Expires: '. gmdate('D, d M Y H:i:s', 0) .'GMT');
151 header('Pragma: no-cache');
152 header('Accept-Ranges: none'); // Comment out if PDFs do not work...
153 header('Content-disposition: attachment; filename='.$filename);
154 header('Content-length: '.strlen($serialized));
155 header('Content-type: text/calendar');
157 echo $serialized;