Skip to content

Commit

Permalink
Super-basic export to iCal functionality, still very raw. But works!
Browse files Browse the repository at this point in the history
  • Loading branch information
defacer committed Jan 13, 2006
1 parent e58e1a9 commit b5f1b8b
Show file tree
Hide file tree
Showing 3 changed files with 301 additions and 0 deletions.
111 changes: 111 additions & 0 deletions calendar/export.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php // $Id$

require_once('../config.php');
require_once($CFG->dirroot.'/course/lib.php');
require_once($CFG->dirroot.'/calendar/lib.php');
//require_once($CFG->libdir.'/bennu/bennu.inc.php');

$action = optional_param('action', '', PARAM_ALPHA);
$course = optional_param('course', 0);
$day = optional_param('cal_d', 0, PARAM_INT);
$mon = optional_param('cal_m', 0, PARAM_INT);
$yr = optional_param('cal_y', 0, PARAM_INT);

if(!$site = get_site()) {
redirect($CFG->wwwroot.'/'.$CFG->admin.'/index.php');
}

$pagetitle = get_string('export', 'calendar');
$now = usergetdate(time());
$nav = calendar_get_link_tag(get_string('calendar', 'calendar'), CALENDAR_URL.'view.php?view=upcoming&amp;', $now['mday'], $now['mon'], $now['year']).' -> '.$pagetitle;

if(!checkdate($mon, $day, $yr)) {
$day = intval($now['mday']);
$mon = intval($now['mon']);
$yr = intval($now['year']);
}
$time = make_timestamp($yr, $mon, $day);

$SESSION->cal_courses_shown = calendar_get_default_courses(true);
calendar_set_referring_course(0);

if (empty($USER->id) or isguest()) {
$defaultcourses = calendar_get_default_courses();
calendar_set_filters($courses, $groups, $users, $defaultcourses, $defaultcourses);

} else {
calendar_set_filters($courses, $groups, $users);
}

$strcalendar = get_string('calendar', 'calendar');
$prefsbutton = calendar_preferences_button();

// Print title and header
print_header("$site->shortname: $strcalendar: $pagetitle", $strcalendar, $nav,
'', '', true, $prefsbutton, user_login_string($site));

echo calendar_overlib_html();

// Layout the whole page as three big columns.
echo '<table id="calendar">';
echo '<tr>';

// START: Main column

echo '<td class="maincalendar">';


switch($action) {
case 'advanced':
break;
case '':
default:
// Let's populate some vars to let "common tasks" be somewhat smart...
// If today it's weekend, give the "next week" option
$allownextweek = CALENDAR_WEEKEND & (1 << $now['wday']);
// If it's the last week of the month, give the "next month" option
$allownextmonth = calendar_days_in_month($now['mon'], $now['year']) - $now['mday'] < 7;
// If today it's weekend but tomorrow it isn't, do NOT give the "this week" option
$allowthisweek = !((CALENDAR_WEEKEND & (1 << $now['wday'])) && !(CALENDAR_WEEKEND & (1 << (($now['wday'] + 1) % 7))));
print_heading(get_string('export', 'calendar'));
include('export_basic.html');
}



echo '</td>';

// END: Main column

// START: Last column (3-month display)
echo '<td class="sidecalendar">';
echo '<div class="header">'.get_string('monthlyview', 'calendar').'</div>';

list($prevmon, $prevyr) = calendar_sub_month($mon, $yr);
list($nextmon, $nextyr) = calendar_add_month($mon, $yr);
$getvars = 'cal_d='.$day.'&amp;cal_m='.$mon.'&amp;cal_y='.$yr; // For filtering

echo '<div class="filters">';
echo calendar_filter_controls('export', $getvars);
echo '</div>';

echo '<div class="minicalendarblock">';
echo calendar_top_controls('display', array('m' => $prevmon, 'y' => $prevyr));
echo calendar_get_mini($courses, $groups, $users, $prevmon, $prevyr);
echo '</div><div class="minicalendarblock">';
echo calendar_top_controls('display', array('m' => $mon, 'y' => $yr));
echo calendar_get_mini($courses, $groups, $users, $mon, $yr);
echo '</div><div class="minicalendarblock">';
echo calendar_top_controls('display', array('m' => $nextmon, 'y' => $nextyr));
echo calendar_get_mini($courses, $groups, $users, $nextmon, $nextyr);
echo '</div>';

echo '</td>';

echo '</tr></table>';

print_footer();



?>
44 changes: 44 additions & 0 deletions calendar/export_basic.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<fieldset>
<legend><?php print_string('commontasks', 'calendar'); ?></legend>
<form method="get" action="export_execute.php">
<?php print_string('iwanttoexport', 'calendar'); ?>:
<div class="indent">
<input type="radio" name="preset_what" id="pw_all" value="all" /><label for="pw_all"><?php print_string('eventsall', 'calendar'); ?></label><br />
<input type="radio" name="preset_what" id="pw_course" value="courses" /><label for="pw_course"><?php print_string('eventsrelatedtocourses', 'calendar'); ?></label><br />
</div>
<?php print_string('for', 'calendar'); ?>:
<div class="indent">
<?php if($allowthisweek) { ?>
<input type="radio" name="preset_time" id="pt_wknow" value="weeknow" /><label for="pt_wknow"><?php print_string('weekthis', 'calendar'); ?></label><br />
<?php } ?>
<?php if($allownextweek) { ?>
<input type="radio" name="preset_time" id="pt_wknext" value="weeknext" /><label for="pt_wknext"><?php print_string('weeknext', 'calendar'); ?></label><br />
<?php } ?>
<input type="radio" name="preset_time" id="pt_monnow" value="monthnow" /><label for="pt_monnow"><?php print_string('monththis', 'calendar'); ?></label><br />
<?php if($allownextmonth) { ?>
<input type="radio" name="preset_time" id="pt_monnext" value="monthnext" /><label for="pt_monnext"><?php print_string('monthnext', 'calendar'); ?></label><br />
<?php } ?>
</div>
<div style="text-align: right;">
<input type="hidden" name="cal_d" value="" />
<input type="hidden" name="cal_m" value="" />
<input type="hidden" name="cal_y" value="" />
<input type="hidden" name="action" value="export" />
<input type="submit" value="<?php print_string('exportbutton', 'calendar'); ?>" />
</div>
</form>
</fieldset>
<br />
<fieldset>
<legend><?php print_string('advancedoptions', 'calendar'); ?></legend>
<form method="get" action="export.php">
<?php print_string('advancedoptionsexplain', 'calendar'); ?>
<div style="text-align: right;">
<input type="hidden" name="cal_d" value="" />
<input type="hidden" name="cal_m" value="" />
<input type="hidden" name="cal_y" value="" />
<input type="hidden" name="action" value="advanced" />
<input type="submit" value="<?php print_string('advancedoptions', 'calendar'); ?>" />
</div>
</form>
</fieldset>
146 changes: 146 additions & 0 deletions calendar/export_execute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php // $Id$

require_once('../config.php');
//require_once($CFG->dirroot.'/course/lib.php');
require_once($CFG->dirroot.'/calendar/lib.php');
require_once($CFG->libdir.'/bennu/bennu.inc.php');

require_login();
if(isguest()) {
redirect($CFG->wwwroot.'/calendar/view.php');
}

$action = optional_param('action', '', PARAM_ALPHA);
$course = optional_param('course', 0);
$day = optional_param('cal_d', 0, PARAM_INT);
$mon = optional_param('cal_m', 0, PARAM_INT);
$yr = optional_param('cal_y', 0, PARAM_INT);

$what = optional_param('preset_what', '', PARAM_ALPHA);
$time = optional_param('preset_time', '', PARAM_ALPHA);

$now = usergetdate(time());
// Let's see if we have sufficient and correct data
$allowed_what = array('all', 'courses');
$allowed_time = array('weeknow', 'weeknext', 'monthnow', 'monthnext');

if(!empty($what) && !empty($time)) {
if(in_array($what, $allowed_what) && in_array($time, $allowed_time)) {
$courses = array() + $USER->student + $USER->teacher;
$courses = array_keys($courses);
switch($time) {
case 'weeknow':
$startweekday = get_user_preferences('calendar_startwday', CALENDAR_STARTING_WEEKDAY);
$startmonthday = find_day_in_month($now['mday'] - 6, $startweekday, $now['mon'], $now['year']);
$startmonth = $now['mon'];
$startyear = $now['year'];
if($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
$startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
}
$timestart = make_timestamp($startyear, $startmonth, $startmonthday);
$endmonthday = $startmonthday + 7;
$endmonth = $startmonth;
$endyear = $startyear;
if($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
$endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
}
$timeend = make_timestamp($endyear, $endmonth, $endmonthday) - 1;
break;
case 'weeknext':
$startweekday = get_user_preferences('calendar_startwday', CALENDAR_STARTING_WEEKDAY);
$startmonthday = find_day_in_month($now['mday'] + 1, $startweekday, $now['mon'], $now['year']);
$startmonth = $now['mon'];
$startyear = $now['year'];
if($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
$startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
}
$timestart = make_timestamp($startyear, $startmonth, $startmonthday);
$endmonthday = $startmonthday + 7;
$endmonth = $startmonth;
$endyear = $startyear;
if($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
$endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
}
$timeend = make_timestamp($endyear, $endmonth, $endmonthday) - 1;
break;
case 'monthnow':
$timestart = make_timestamp($now['year'], $now['mon'], 1);
$timeend = make_timestamp($now['year'], $now['mon'], calendar_days_in_month($now['mon'], $now['year']), 23, 59, 59);
break;
case 'monthnext':
list($nextmonth, $nextyear) = calendar_add_month($now['mon'], $now['year']);
$timestart = make_timestamp($nextyear, $nextmonth, 1);
$timeend = make_timestamp($nextyear, $nextmonth, calendar_days_in_month($nextmonth, $nextyear), 23, 59, 59);
break;
}

/*
print_object($now);
print_object('start: '. $timestart);
print_object('end: '. $timeend);
*/
}
else {
// Parameters given but incorrect, redirect back to export page
redirect($CFG->wwwroot.'/calendar/export.php');
echo "aa";
die();
}
}

$whereclause = calendar_sql_where($timestart, $timeend, false, false, $courses, false);
if($whereclause === false) {
$events = array();
}
else {
$events = get_records_select('event', $whereclause, 'timestart');
}

if(empty($events)) {
// TODO
die('no events');
}

$ical = new iCalendar;
$ical->add_property('method', 'PUBLISH');
foreach($events as $event) {
$ev = new iCalendar_event;
$ev->add_property('summary', $event->name);
$ev->add_property('description', $event->description);
$ev->add_property('class', 'public'); // PUBLIC / PRIVATE / CONFIDENTIAL
$ev->add_property('last-modified', 0); // lastmodified
$ev->add_property('dtstamp', Bennu::timestamp_to_datetime()); // now
$ev->add_property('dtstart', Bennu::timestamp_to_datetime($event->timestart)); // when event starts
$ev->add_property('duration', 0); // when event starts
$ical->add_component($ev);
}

$serialized = $ical->serialize();
if(empty($serialized)) {
// TODO
die('bad serialization');
}

//IE compatibiltiy HACK!
if(ini_get('zlib.output_compression')) {
ini_set('zlib.output_compression', 'Off');
}

$filename = 'icalexport.ics';

header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
header('Expires: '. gmdate('D, d M Y H:i:s', 0) .'GMT');
header('Pragma: no-cache');
header('Accept-Ranges: none'); // Comment out if PDFs do not work...
header('Content-disposition: attachment; filename='.$filename);
header('Content-length: '.strlen($serialized));
header('Content-type: text/plain');

echo $serialized;

?>

0 comments on commit b5f1b8b

Please sign in to comment.