MDL-15165 "Essay quiz Manual grading "these are no attempts to show" when there are...
[moodle.git] / calendar / export_execute.php
CommitLineData
b5f1b8b1 1<?php // $Id$
2
3require_once('../config.php');
4//require_once($CFG->dirroot.'/course/lib.php');
5require_once($CFG->dirroot.'/calendar/lib.php');
6require_once($CFG->libdir.'/bennu/bennu.inc.php');
7
ea185313 8$username = required_param('username', PARAM_TEXT);
9$authtoken = required_param('authtoken', PARAM_ALPHANUM);
10
11//Fetch user information
12if (!$user = get_complete_user_data('username', $username)) {
13 //No such user
14 die("No such user '$username'");
b5f1b8b1 15}
16
ea185313 17//Check authentication token
18if ($authtoken != sha1($username . $user->password)) {
19 die('Invalid authentication token');
20}
b5f1b8b1 21
cba168a6 22$what = optional_param('preset_what', 'all', PARAM_ALPHA);
23$time = optional_param('preset_time', 'weeknow', PARAM_ALPHA);
b5f1b8b1 24
25$now = usergetdate(time());
26// Let's see if we have sufficient and correct data
27$allowed_what = array('all', 'courses');
ea185313 28$allowed_time = array('weeknow', 'weeknext', 'monthnow', 'monthnext', 'recentupcoming');
b5f1b8b1 29
30if(!empty($what) && !empty($time)) {
31 if(in_array($what, $allowed_what) && in_array($time, $allowed_time)) {
e5797cd4 32 $courses = get_my_courses($user->id, NULL, 'id, visible, shortname');
ea185313 33
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');
39 }
cba168a6 40
b5f1b8b1 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);
50 }
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);
58 }
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);
69 }
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);
77 }
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;
ea185313 89 case 'recentupcoming':
90 //Events in the last 5 or next 60 days
91 $timestart = time() - 432000;
92 $timeend = time() + 5184000;
93 break;
b5f1b8b1 94 }
b5f1b8b1 95 }
96 else {
97 // Parameters given but incorrect, redirect back to export page
98 redirect($CFG->wwwroot.'/calendar/export.php');
b5f1b8b1 99 die();
100 }
101}
8263f802 102$events = calendar_get_events($timestart, $timeend, $include_user ? array($user->id) : false, false, array_keys($courses), false);
b5f1b8b1 103
104$ical = new iCalendar;
105$ical->add_property('method', 'PUBLISH');
106foreach($events as $event) {
13534ef7
ML
107 if (!empty($event->modulename)) {
108 $cm = get_coursemodule_from_instance($event->modulename, $event->instance);
109 if (!groups_course_module_visible($cm)) {
110 continue;
111 }
112 }
b5f1b8b1 113 $ev = new iCalendar_event;
114 $ev->add_property('summary', $event->name);
115 $ev->add_property('description', $event->description);
ea185313 116 $ev->add_property('class', 'PUBLIC'); // PUBLIC / PRIVATE / CONFIDENTIAL
117 $ev->add_property('last-modified', Bennu::timestamp_to_datetime($event->timemodified));
b5f1b8b1 118 $ev->add_property('dtstamp', Bennu::timestamp_to_datetime()); // now
119 $ev->add_property('dtstart', Bennu::timestamp_to_datetime($event->timestart)); // when event starts
ea185313 120 if ($event->timeduration > 0) {
121 //dtend is better than duration, because it works in Microsoft Outlook and works better in Korganizer
122 $ev->add_property('dtend', Bennu::timestamp_to_datetime($event->timestart + $event->timeduration));
123 }
124 if ($event->courseid != 0) {
125 $ev->add_property('categories', $courses[$event->courseid]->shortname);
126 }
b5f1b8b1 127 $ical->add_component($ev);
128}
129
130$serialized = $ical->serialize();
131if(empty($serialized)) {
132 // TODO
133 die('bad serialization');
134}
135
ea185313 136//IE compatibility HACK!
b5f1b8b1 137if(ini_get('zlib.output_compression')) {
138 ini_set('zlib.output_compression', 'Off');
139}
140
141$filename = 'icalexport.ics';
142
143header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
144header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
145header('Expires: '. gmdate('D, d M Y H:i:s', 0) .'GMT');
146header('Pragma: no-cache');
147header('Accept-Ranges: none'); // Comment out if PDFs do not work...
148header('Content-disposition: attachment; filename='.$filename);
149header('Content-length: '.strlen($serialized));
ea185313 150header('Content-type: text/calendar');
b5f1b8b1 151
152echo $serialized;
153
154?>