From: martinlanghoff Date: Thu, 23 Nov 2006 20:21:29 +0000 (+0000) Subject: Cleaning up, completing, bugfixing, adding features to iCalendar export of X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=ea185313019e9220e21b51d72e54277ee6cb35dd;p=moodle.git Cleaning up, completing, bugfixing, adding features to iCalendar export of Moodle calendar. * Adding strings * Adding more information to iCalendar output * Making iCalendar output compatible with a wider range of calender software * Removing (non-functional) advanced options for now, and other superfluous code * Paying attention to all / course events option * Adding authentication token system to allow subscription * Adding JavaScript to generate URL for subscription * Adding links from calendar page Author: Andrew Walbran --- diff --git a/calendar/export.php b/calendar/export.php index fb636f3d67..af7029804a 100644 --- a/calendar/export.php +++ b/calendar/export.php @@ -11,6 +11,8 @@ $day = optional_param('cal_d', 0, PARAM_INT); $mon = optional_param('cal_m', 0, PARAM_INT); $yr = optional_param('cal_y', 0, PARAM_INT); +require_login(); + if(!$site = get_site()) { redirect($CFG->wwwroot.'/'.$CFG->admin.'/index.php'); } @@ -54,6 +56,9 @@ echo ''; echo ''; +$username = $USER->username; +$usernameencoded = urlencode($USER->username); +$authtoken = sha1($USER->username . $USER->password); switch($action) { case 'advanced': @@ -67,7 +72,7 @@ switch($action) { $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')); + echo '
' . get_string('export', 'calendar') . '
'; include('export_basic.html'); } diff --git a/calendar/export_basic.html b/calendar/export_basic.html index e05ac05472..9b7c70ca79 100644 --- a/calendar/export_basic.html +++ b/calendar/export_basic.html @@ -18,27 +18,49 @@
+
- + + + +
-
-
- -
- -
- - - - - + - -
\ No newline at end of file diff --git a/calendar/export_execute.php b/calendar/export_execute.php index 0419727691..87286f0c01 100644 --- a/calendar/export_execute.php +++ b/calendar/export_execute.php @@ -5,16 +5,19 @@ require_once('../config.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'); +$username = required_param('username', PARAM_TEXT); +$authtoken = required_param('authtoken', PARAM_ALPHANUM); + +//Fetch user information +if (!$user = get_complete_user_data('username', $username)) { + //No such user + die("No such user '$username'"); } -$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); +//Check authentication token +if ($authtoken != sha1($username . $user->password)) { + die('Invalid authentication token'); +} $what = optional_param('preset_what', '', PARAM_ALPHA); $time = optional_param('preset_time', '', PARAM_ALPHA); @@ -22,12 +25,19 @@ $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'); +$allowed_time = array('weeknow', 'weeknext', 'monthnow', 'monthnext', 'recentupcoming'); 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); + $courses = get_my_courses($user->id); + + $include_user = ($what == 'all'); + if ($include_user) { + //Also include site (global) events + $courses[SITEID] = new stdClass; + $courses[SITEID]->shortname = get_string('globalevents', 'calendar'); + } + switch($time) { case 'weeknow': $startweekday = get_user_preferences('calendar_startwday', CALENDAR_STARTING_WEEKDAY); @@ -76,23 +86,21 @@ if(!empty($what) && !empty($time)) { $timestart = make_timestamp($nextyear, $nextmonth, 1); $timeend = make_timestamp($nextyear, $nextmonth, calendar_days_in_month($nextmonth, $nextyear), 23, 59, 59); break; + case 'recentupcoming': + //Events in the last 5 or next 60 days + $timestart = time() - 432000; + $timeend = time() + 5184000; + 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); +$whereclause = calendar_sql_where($timestart, $timeend, $include_user ? array($user->id) : false, false, array_keys($courses), false); if($whereclause === false) { $events = array(); } @@ -100,9 +108,8 @@ else { $events = get_records_select('event', $whereclause, 'timestart'); } -if(empty($events)) { - // TODO - die('no events'); +if ($events === false) { + $events = array(); } $ical = new iCalendar; @@ -111,11 +118,17 @@ 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('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 - $ev->add_property('duration', 0); // 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)); + } + if ($event->courseid != 0) { + $ev->add_property('categories', $courses[$event->courseid]->shortname); + } $ical->add_component($ev); } @@ -125,7 +138,7 @@ if(empty($serialized)) { die('bad serialization'); } -//IE compatibiltiy HACK! +//IE compatibility HACK! if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); } @@ -139,7 +152,7 @@ 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'); +header('Content-type: text/calendar'); echo $serialized; diff --git a/calendar/view.php b/calendar/view.php index cfb04af524..75a69de976 100644 --- a/calendar/view.php +++ b/calendar/view.php @@ -150,6 +150,15 @@ calendar_show_upcoming_events($courses, $groups, $users, get_user_preferences('calendar_lookahead', CALENDAR_UPCOMING_DAYS), get_user_preferences('calendar_maxevents', CALENDAR_UPCOMING_MAXEVENTS)); break; } + + //Link to calendar export page + echo '

' . get_string('exportcalendar', 'calendar') . '

'; + + if (!empty($USER->id)) { + $authtoken = sha1($USER->username . $USER->password); + $usernameencoded = urlencode($USER->username); + echo "

" . get_string('quickdownloadcalendar', 'calendar') . '

'; + } echo ''; diff --git a/lang/en_utf8/calendar.php b/lang/en_utf8/calendar.php index 53c97fca3d..f4b4c590f0 100644 --- a/lang/en_utf8/calendar.php +++ b/lang/en_utf8/calendar.php @@ -1,11 +1,12 @@