MDL-16553 Assignment, student view: whitespace fix
[moodle.git] / calendar / export_execute.php
blobb82574939ff136ebe1e376199ab8f04efb5109a0
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 if (empty($CFG->enablecalendarexport)) {
12 die('no export');
15 //Fetch user information
16 if (!$user = get_complete_user_data('username', $username)) {
17 //No such user
18 die('Invalid authentication');
21 //Check authentication token
22 if ($authtoken != sha1($username . $user->password . $CFG->calendar_exportsalt)) {
23 die('Invalid authentication');
26 $what = optional_param('preset_what', 'all', PARAM_ALPHA);
27 $time = optional_param('preset_time', 'weeknow', PARAM_ALPHA);
29 $now = usergetdate(time());
30 // Let's see if we have sufficient and correct data
31 $allowed_what = array('all', 'courses');
32 $allowed_time = array('weeknow', 'weeknext', 'monthnow', 'monthnext', 'recentupcoming');
34 if(!empty($what) && !empty($time)) {
35 if(in_array($what, $allowed_what) && in_array($time, $allowed_time)) {
36 $courses = get_my_courses($user->id, NULL, 'id, visible, shortname');
38 if ($what == 'all') {
39 $users = $user->id;
40 $groups = array();
41 foreach ($courses as $course) {
42 $course_groups = groups_get_all_groups($course->id, $user->id);
43 if ($course_groups) {
44 $groups = array_merge($groups, array_keys($course_groups));
47 if (empty($groups)) {
48 $groups = false;
50 $courses[SITEID] = new stdClass;
51 $courses[SITEID]->shortname = get_string('globalevents', 'calendar');
52 } else {
53 $users = false;
54 $groups = false;
57 switch($time) {
58 case 'weeknow':
59 $startweekday = get_user_preferences('calendar_startwday', CALENDAR_STARTING_WEEKDAY);
60 $startmonthday = find_day_in_month($now['mday'] - 6, $startweekday, $now['mon'], $now['year']);
61 $startmonth = $now['mon'];
62 $startyear = $now['year'];
63 if($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
64 list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
65 $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
67 $timestart = make_timestamp($startyear, $startmonth, $startmonthday);
68 $endmonthday = $startmonthday + 7;
69 $endmonth = $startmonth;
70 $endyear = $startyear;
71 if($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
72 list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
73 $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
75 $timeend = make_timestamp($endyear, $endmonth, $endmonthday) - 1;
76 break;
77 case 'weeknext':
78 $startweekday = get_user_preferences('calendar_startwday', CALENDAR_STARTING_WEEKDAY);
79 $startmonthday = find_day_in_month($now['mday'] + 1, $startweekday, $now['mon'], $now['year']);
80 $startmonth = $now['mon'];
81 $startyear = $now['year'];
82 if($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
83 list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
84 $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
86 $timestart = make_timestamp($startyear, $startmonth, $startmonthday);
87 $endmonthday = $startmonthday + 7;
88 $endmonth = $startmonth;
89 $endyear = $startyear;
90 if($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
91 list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
92 $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
94 $timeend = make_timestamp($endyear, $endmonth, $endmonthday) - 1;
95 break;
96 case 'monthnow':
97 $timestart = make_timestamp($now['year'], $now['mon'], 1);
98 $timeend = make_timestamp($now['year'], $now['mon'], calendar_days_in_month($now['mon'], $now['year']), 23, 59, 59);
99 break;
100 case 'monthnext':
101 list($nextmonth, $nextyear) = calendar_add_month($now['mon'], $now['year']);
102 $timestart = make_timestamp($nextyear, $nextmonth, 1);
103 $timeend = make_timestamp($nextyear, $nextmonth, calendar_days_in_month($nextmonth, $nextyear), 23, 59, 59);
104 break;
105 case 'recentupcoming':
106 //Events in the last 5 or next 60 days
107 $timestart = time() - 432000;
108 $timeend = time() + 5184000;
109 break;
112 else {
113 // Parameters given but incorrect, redirect back to export page
114 redirect($CFG->wwwroot.'/calendar/export.php');
115 die();
118 $events = calendar_get_events($timestart, $timeend, $users, $groups, array_keys($courses), false);
120 $ical = new iCalendar;
121 $ical->add_property('method', 'PUBLISH');
122 foreach($events as $event) {
123 if (!empty($event->modulename)) {
124 $cm = get_coursemodule_from_instance($event->modulename, $event->instance);
125 if (!groups_course_module_visible($cm)) {
126 continue;
129 $hostaddress = str_replace('http://', '', $CFG->wwwroot);
130 $hostaddress = str_replace('https://', '', $hostaddress);
132 $ev = new iCalendar_event;
133 $ev->add_property('uid', $event->id.'@'.$hostaddress);
134 $ev->add_property('summary', $event->name);
135 $ev->add_property('description', $event->description);
136 $ev->add_property('class', 'PUBLIC'); // PUBLIC / PRIVATE / CONFIDENTIAL
137 $ev->add_property('last-modified', Bennu::timestamp_to_datetime($event->timemodified));
138 $ev->add_property('dtstamp', Bennu::timestamp_to_datetime()); // now
139 $ev->add_property('dtstart', Bennu::timestamp_to_datetime($event->timestart)); // when event starts
140 if ($event->timeduration > 0) {
141 //dtend is better than duration, because it works in Microsoft Outlook and works better in Korganizer
142 $ev->add_property('dtend', Bennu::timestamp_to_datetime($event->timestart + $event->timeduration));
144 if ($event->courseid != 0) {
145 $ev->add_property('categories', $courses[$event->courseid]->shortname);
147 $ical->add_component($ev);
150 $serialized = $ical->serialize();
151 if(empty($serialized)) {
152 // TODO
153 die('bad serialization');
156 //IE compatibility HACK!
157 if(ini_get('zlib.output_compression')) {
158 ini_set('zlib.output_compression', 'Off');
161 $filename = 'icalexport.ics';
163 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
164 header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
165 header('Expires: '. gmdate('D, d M Y H:i:s', 0) .'GMT');
166 header('Pragma: no-cache');
167 header('Accept-Ranges: none'); // Comment out if PDFs do not work...
168 header('Content-disposition: attachment; filename='.$filename);
169 header('Content-length: '.strlen($serialized));
170 header('Content-type: text/calendar');
172 echo $serialized;