// Parameters given but incorrect, redirect back to export page
        redirect($CFG->wwwroot . '/calendar/export.php');
        die;
    }
}
$whereclause = calendar_sql_where($timestart, $timeend, $users, $groups, array_keys($courses), false);
if ($whereclause === false) {
    $events = array();
} else {
    $events = get_records_select('event', $whereclause, 'timestart');
}
if ($events === false) {
    $events = array();
}
$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', Bennu::timestamp_to_datetime($event->timemodified));
    $ev->add_property('dtstamp', Bennu::timestamp_to_datetime());
    // now
    $ev->add_property('dtstart', Bennu::timestamp_to_datetime($event->timestart));
    // when event starts
    if ($event->timeduration > 0) {
        //dtend is better than duration, because it works in Microsoft Outlook and works better in Korganizer
        $ev->add_property('dtend', Bennu::timestamp_to_datetime($event->timestart + $event->timeduration));
    }