More logical wording on button to import from local file
[moodle-linuxchix.git] / calendar / export_execute.php
blob0419727691982e4a2a6c360f97deebdfe4b3b1ea
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 require_login();
9 if(isguest()) {
10 redirect($CFG->wwwroot.'/calendar/view.php');
13 $action = optional_param('action', '', PARAM_ALPHA);
14 $course = optional_param('course', 0);
15 $day = optional_param('cal_d', 0, PARAM_INT);
16 $mon = optional_param('cal_m', 0, PARAM_INT);
17 $yr = optional_param('cal_y', 0, PARAM_INT);
19 $what = optional_param('preset_what', '', PARAM_ALPHA);
20 $time = optional_param('preset_time', '', PARAM_ALPHA);
22 $now = usergetdate(time());
23 // Let's see if we have sufficient and correct data
24 $allowed_what = array('all', 'courses');
25 $allowed_time = array('weeknow', 'weeknext', 'monthnow', 'monthnext');
27 if(!empty($what) && !empty($time)) {
28 if(in_array($what, $allowed_what) && in_array($time, $allowed_time)) {
29 $courses = array() + $USER->student + $USER->teacher;
30 $courses = array_keys($courses);
31 switch($time) {
32 case 'weeknow':
33 $startweekday = get_user_preferences('calendar_startwday', CALENDAR_STARTING_WEEKDAY);
34 $startmonthday = find_day_in_month($now['mday'] - 6, $startweekday, $now['mon'], $now['year']);
35 $startmonth = $now['mon'];
36 $startyear = $now['year'];
37 if($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
38 list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
39 $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
41 $timestart = make_timestamp($startyear, $startmonth, $startmonthday);
42 $endmonthday = $startmonthday + 7;
43 $endmonth = $startmonth;
44 $endyear = $startyear;
45 if($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
46 list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
47 $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
49 $timeend = make_timestamp($endyear, $endmonth, $endmonthday) - 1;
50 break;
51 case 'weeknext':
52 $startweekday = get_user_preferences('calendar_startwday', CALENDAR_STARTING_WEEKDAY);
53 $startmonthday = find_day_in_month($now['mday'] + 1, $startweekday, $now['mon'], $now['year']);
54 $startmonth = $now['mon'];
55 $startyear = $now['year'];
56 if($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
57 list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
58 $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
60 $timestart = make_timestamp($startyear, $startmonth, $startmonthday);
61 $endmonthday = $startmonthday + 7;
62 $endmonth = $startmonth;
63 $endyear = $startyear;
64 if($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
65 list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
66 $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
68 $timeend = make_timestamp($endyear, $endmonth, $endmonthday) - 1;
69 break;
70 case 'monthnow':
71 $timestart = make_timestamp($now['year'], $now['mon'], 1);
72 $timeend = make_timestamp($now['year'], $now['mon'], calendar_days_in_month($now['mon'], $now['year']), 23, 59, 59);
73 break;
74 case 'monthnext':
75 list($nextmonth, $nextyear) = calendar_add_month($now['mon'], $now['year']);
76 $timestart = make_timestamp($nextyear, $nextmonth, 1);
77 $timeend = make_timestamp($nextyear, $nextmonth, calendar_days_in_month($nextmonth, $nextyear), 23, 59, 59);
78 break;
81 /*
82 print_object($now);
83 print_object('start: '. $timestart);
84 print_object('end: '. $timeend);
87 else {
88 // Parameters given but incorrect, redirect back to export page
89 redirect($CFG->wwwroot.'/calendar/export.php');
90 echo "aa";
91 die();
95 $whereclause = calendar_sql_where($timestart, $timeend, false, false, $courses, false);
96 if($whereclause === false) {
97 $events = array();
99 else {
100 $events = get_records_select('event', $whereclause, 'timestart');
103 if(empty($events)) {
104 // TODO
105 die('no events');
108 $ical = new iCalendar;
109 $ical->add_property('method', 'PUBLISH');
110 foreach($events as $event) {
111 $ev = new iCalendar_event;
112 $ev->add_property('summary', $event->name);
113 $ev->add_property('description', $event->description);
114 $ev->add_property('class', 'public'); // PUBLIC / PRIVATE / CONFIDENTIAL
115 $ev->add_property('last-modified', 0); // lastmodified
116 $ev->add_property('dtstamp', Bennu::timestamp_to_datetime()); // now
117 $ev->add_property('dtstart', Bennu::timestamp_to_datetime($event->timestart)); // when event starts
118 $ev->add_property('duration', 0); // when event starts
119 $ical->add_component($ev);
122 $serialized = $ical->serialize();
123 if(empty($serialized)) {
124 // TODO
125 die('bad serialization');
128 //IE compatibiltiy HACK!
129 if(ini_get('zlib.output_compression')) {
130 ini_set('zlib.output_compression', 'Off');
133 $filename = 'icalexport.ics';
135 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
136 header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
137 header('Expires: '. gmdate('D, d M Y H:i:s', 0) .'GMT');
138 header('Pragma: no-cache');
139 header('Accept-Ranges: none'); // Comment out if PDFs do not work...
140 header('Content-disposition: attachment; filename='.$filename);
141 header('Content-length: '.strlen($serialized));
142 header('Content-type: text/plain');
144 echo $serialized;