Students cannot enrol with PayPal plugin when using PHP4 MDL-7529
[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
8require_login();
9if(isguest()) {
10 redirect($CFG->wwwroot.'/calendar/view.php');
11}
12
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);
18
19$what = optional_param('preset_what', '', PARAM_ALPHA);
20$time = optional_param('preset_time', '', PARAM_ALPHA);
21
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');
26
27if(!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);
40 }
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);
48 }
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);
59 }
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);
67 }
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;
79 }
80
81 /*
82 print_object($now);
83 print_object('start: '. $timestart);
84 print_object('end: '. $timeend);
85 */
86 }
87 else {
88 // Parameters given but incorrect, redirect back to export page
89 redirect($CFG->wwwroot.'/calendar/export.php');
90 echo "aa";
91 die();
92 }
93}
94
95$whereclause = calendar_sql_where($timestart, $timeend, false, false, $courses, false);
96if($whereclause === false) {
97 $events = array();
98}
99else {
100 $events = get_records_select('event', $whereclause, 'timestart');
101}
102
103if(empty($events)) {
104 // TODO
105 die('no events');
106}
107
108$ical = new iCalendar;
109$ical->add_property('method', 'PUBLISH');
110foreach($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);
120}
121
122$serialized = $ical->serialize();
123if(empty($serialized)) {
124 // TODO
125 die('bad serialization');
126}
127
128//IE compatibiltiy HACK!
129if(ini_get('zlib.output_compression')) {
130 ini_set('zlib.output_compression', 'Off');
131}
132
133$filename = 'icalexport.ics';
134
135header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
136header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
137header('Expires: '. gmdate('D, d M Y H:i:s', 0) .'GMT');
138header('Pragma: no-cache');
139header('Accept-Ranges: none'); // Comment out if PDFs do not work...
140header('Content-disposition: attachment; filename='.$filename);
141header('Content-length: '.strlen($serialized));
142header('Content-type: text/plain');
143
144echo $serialized;
145
146?>