Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/calendar/export_execute.php
Go to the documentation of this file.
00001 <?php
00002 
00003 require_once('../config.php');
00004 //require_once($CFG->dirroot.'/course/lib.php');
00005 require_once($CFG->dirroot.'/calendar/lib.php');
00006 require_once($CFG->libdir.'/bennu/bennu.inc.php');
00007 
00008 $userid = optional_param('userid', 0, PARAM_INT);
00009 $username = optional_param('username', '', PARAM_TEXT);
00010 $authtoken = required_param('authtoken', PARAM_ALPHANUM);
00011 $generateurl = optional_param('generateurl', '', PARAM_TEXT);
00012 
00013 if (empty($CFG->enablecalendarexport)) {
00014     die('no export');
00015 }
00016 
00017 //Fetch user information
00018 $checkuserid = !empty($userid) && $user = $DB->get_record('user', array('id' => $userid), 'id,password');
00019 //allowing for fallback check of old url - MDL-27542
00020 $checkusername = !empty($username) && $user = $DB->get_record('user', array('username' => $username), 'id,password');
00021 if (!$checkuserid && !$checkusername) {
00022     //No such user
00023     die('Invalid authentication');
00024 }
00025 
00026 //Check authentication token
00027 $authuserid = !empty($userid) && $authtoken == sha1($userid . $user->password . $CFG->calendar_exportsalt);
00028 //allowing for fallback check of old url - MDL-27542
00029 $authusername = !empty($username) && $authtoken == sha1($username . $user->password . $CFG->calendar_exportsalt);
00030 if (!$authuserid && !$authusername) {
00031     die('Invalid authentication');
00032 }
00033 
00034 $what = optional_param('preset_what', 'all', PARAM_ALPHA);
00035 $time = optional_param('preset_time', 'weeknow', PARAM_ALPHA);
00036 
00037 $now = usergetdate(time());
00038 // Let's see if we have sufficient and correct data
00039 $allowed_what = array('all', 'courses');
00040 $allowed_time = array('weeknow', 'weeknext', 'monthnow', 'monthnext', 'recentupcoming');
00041 
00042 if (!empty($generateurl)) {
00043     $authtoken = sha1($user->id . $user->password . $CFG->calendar_exportsalt);
00044     $params = array();
00045     $params['preset_what'] = $what;
00046     $params['preset_time'] = $time;
00047     $params['userid'] = $userid;
00048     $params['authtoken'] = $authtoken;
00049     $params['generateurl'] = true;
00050 
00051     $link = new moodle_url('/calendar/export.php', $params);
00052     redirect($link->out());
00053     die;
00054 }
00055 
00056 if(!empty($what) && !empty($time)) {
00057     if(in_array($what, $allowed_what) && in_array($time, $allowed_time)) {
00058         $courses = enrol_get_users_courses($user->id, true, 'id, visible, shortname');
00059 
00060         if ($what == 'all') {
00061             $users = $user->id;
00062             $groups = array();
00063             foreach ($courses as $course) {
00064                 $course_groups = groups_get_all_groups($course->id, $user->id);
00065                 $groups = array_merge($groups, array_keys($course_groups));
00066             }
00067             if (empty($groups)) {
00068                 $groups = false;
00069             }
00070             $courses[SITEID] = new stdClass;
00071             $courses[SITEID]->shortname = get_string('globalevents', 'calendar');
00072         } else {
00073             $users = false;
00074             $groups = false;
00075         }
00076 
00077         switch($time) {
00078             case 'weeknow':
00079                 $startweekday  = get_user_preferences('calendar_startwday', calendar_get_starting_weekday());
00080                 $startmonthday = find_day_in_month($now['mday'] - 6, $startweekday, $now['mon'], $now['year']);
00081                 $startmonth    = $now['mon'];
00082                 $startyear     = $now['year'];
00083                 if($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
00084                     list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
00085                     $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
00086                 }
00087                 $timestart = make_timestamp($startyear, $startmonth, $startmonthday);
00088                 $endmonthday = $startmonthday + 7;
00089                 $endmonth    = $startmonth;
00090                 $endyear     = $startyear;
00091                 if($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
00092                     list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
00093                     $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
00094                 }
00095                 $timeend = make_timestamp($endyear, $endmonth, $endmonthday) - 1;
00096             break;
00097             case 'weeknext':
00098                 $startweekday  = get_user_preferences('calendar_startwday', calendar_get_starting_weekday());
00099                 $startmonthday = find_day_in_month($now['mday'] + 1, $startweekday, $now['mon'], $now['year']);
00100                 $startmonth    = $now['mon'];
00101                 $startyear     = $now['year'];
00102                 if($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
00103                     list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
00104                     $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
00105                 }
00106                 $timestart = make_timestamp($startyear, $startmonth, $startmonthday);
00107                 $endmonthday = $startmonthday + 7;
00108                 $endmonth    = $startmonth;
00109                 $endyear     = $startyear;
00110                 if($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
00111                     list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
00112                     $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
00113                 }
00114                 $timeend = make_timestamp($endyear, $endmonth, $endmonthday) - 1;
00115             break;
00116             case 'monthnow':
00117                 $timestart = make_timestamp($now['year'], $now['mon'], 1);
00118                 $timeend   = make_timestamp($now['year'], $now['mon'], calendar_days_in_month($now['mon'], $now['year']), 23, 59, 59);
00119             break;
00120             case 'monthnext':
00121                 list($nextmonth, $nextyear) = calendar_add_month($now['mon'], $now['year']);
00122                 $timestart = make_timestamp($nextyear, $nextmonth, 1);
00123                 $timeend   = make_timestamp($nextyear, $nextmonth, calendar_days_in_month($nextmonth, $nextyear), 23, 59, 59);
00124             break;
00125             case 'recentupcoming':
00126                 //Events in the last 5 or next 60 days
00127                 $timestart = time() - 432000;
00128                 $timeend = time() + 5184000;
00129             break;
00130         }
00131     }
00132     else {
00133         // Parameters given but incorrect, redirect back to export page
00134         redirect($CFG->wwwroot.'/calendar/export.php');
00135         die();
00136     }
00137 }
00138 $events = calendar_get_events($timestart, $timeend, $users, $groups, array_keys($courses), false);
00139 
00140 $ical = new iCalendar;
00141 $ical->add_property('method', 'PUBLISH');
00142 foreach($events as $event) {
00143    if (!empty($event->modulename)) {
00144         $cm = get_coursemodule_from_instance($event->modulename, $event->instance);
00145         if (!groups_course_module_visible($cm)) {
00146             continue;
00147         }
00148     }
00149     $hostaddress = str_replace('http://', '', $CFG->wwwroot);
00150     $hostaddress = str_replace('https://', '', $hostaddress);
00151 
00152     $ev = new iCalendar_event;
00153     $ev->add_property('uid', $event->id.'@'.$hostaddress);
00154     $ev->add_property('summary', $event->name);
00155     $ev->add_property('description', $event->description);
00156     $ev->add_property('class', 'PUBLIC'); // PUBLIC / PRIVATE / CONFIDENTIAL
00157     $ev->add_property('last-modified', Bennu::timestamp_to_datetime($event->timemodified));
00158     $ev->add_property('dtstamp', Bennu::timestamp_to_datetime()); // now
00159     $ev->add_property('dtstart', Bennu::timestamp_to_datetime($event->timestart)); // when event starts
00160     if ($event->timeduration > 0) {
00161         //dtend is better than duration, because it works in Microsoft Outlook and works better in Korganizer
00162         $ev->add_property('dtend', Bennu::timestamp_to_datetime($event->timestart + $event->timeduration));
00163     }
00164     if ($event->courseid != 0) {
00165         $coursecontext = get_context_instance(CONTEXT_COURSE, $event->courseid);
00166         $ev->add_property('categories', format_string($courses[$event->courseid]->shortname, true, array('context' => $coursecontext)));
00167     }
00168     $ical->add_component($ev);
00169 }
00170 
00171 $serialized = $ical->serialize();
00172 if(empty($serialized)) {
00173     // TODO
00174     die('bad serialization');
00175 }
00176 
00177 //IE compatibility HACK!
00178 if (ini_get_bool('zlib.output_compression')) {
00179     ini_set('zlib.output_compression', 'Off');
00180 }
00181 
00182 $filename = 'icalexport.ics';
00183 
00184 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
00185 header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
00186 header('Expires: '. gmdate('D, d M Y H:i:s', 0) .'GMT');
00187 header('Pragma: no-cache');
00188 header('Accept-Ranges: none'); // Comment out if PDFs do not work...
00189 header('Content-disposition: attachment; filename='.$filename);
00190 header('Content-length: '.strlen($serialized));
00191 header('Content-type: text/calendar; charset=utf-8');
00192 
00193 echo $serialized;
 All Data Structures Namespaces Files Functions Variables Enumerations