[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 3 require_once('../config.php'); 4 //require_once($CFG->dirroot.'/course/lib.php'); 5 require_once($CFG->dirroot.'/calendar/lib.php'); 6 require_once($CFG->libdir.'/bennu/bennu.inc.php'); 7 8 $userid = optional_param('userid', 0, PARAM_INT); 9 $username = optional_param('username', '', PARAM_TEXT); 10 $authtoken = required_param('authtoken', PARAM_ALPHANUM); 11 $generateurl = optional_param('generateurl', '', PARAM_TEXT); 12 13 if (empty($CFG->enablecalendarexport)) { 14 die('no export'); 15 } 16 17 //Fetch user information 18 $checkuserid = !empty($userid) && $user = $DB->get_record('user', array('id' => $userid), 'id,password'); 19 //allowing for fallback check of old url - MDL-27542 20 $checkusername = !empty($username) && $user = $DB->get_record('user', array('username' => $username), 'id,password'); 21 if (!$checkuserid && !$checkusername) { 22 //No such user 23 die('Invalid authentication'); 24 } 25 26 //Check authentication token 27 $authuserid = !empty($userid) && $authtoken == sha1($userid . $user->password . $CFG->calendar_exportsalt); 28 //allowing for fallback check of old url - MDL-27542 29 $authusername = !empty($username) && $authtoken == sha1($username . $user->password . $CFG->calendar_exportsalt); 30 if (!$authuserid && !$authusername) { 31 die('Invalid authentication'); 32 } 33 34 // Get the calendar type we are using. 35 $calendartype = \core_calendar\type_factory::get_calendar_instance(); 36 37 $what = optional_param('preset_what', 'all', PARAM_ALPHA); 38 $time = optional_param('preset_time', 'weeknow', PARAM_ALPHA); 39 40 $now = $calendartype->timestamp_to_date_array(time()); 41 42 // Let's see if we have sufficient and correct data 43 $allowed_what = array('all', 'user', 'groups', 'courses'); 44 $allowed_time = array('weeknow', 'weeknext', 'monthnow', 'monthnext', 'recentupcoming', 'custom'); 45 46 if (!empty($generateurl)) { 47 $authtoken = sha1($user->id . $user->password . $CFG->calendar_exportsalt); 48 $params = array(); 49 $params['preset_what'] = $what; 50 $params['preset_time'] = $time; 51 $params['userid'] = $userid; 52 $params['authtoken'] = $authtoken; 53 $params['generateurl'] = true; 54 55 $link = new moodle_url('/calendar/export.php', $params); 56 redirect($link->out()); 57 die; 58 } 59 60 if(!empty($what) && !empty($time)) { 61 if(in_array($what, $allowed_what) && in_array($time, $allowed_time)) { 62 $courses = enrol_get_users_courses($user->id, true, 'id, visible, shortname'); 63 // Array of courses that we will pass to calendar_get_events() which is initially set to the list of the user's courses. 64 $paramcourses = $courses; 65 if ($what == 'all' || $what == 'groups') { 66 $groups = array(); 67 foreach ($courses as $course) { 68 $course_groups = groups_get_all_groups($course->id, $user->id); 69 $groups = array_merge($groups, array_keys($course_groups)); 70 } 71 if (empty($groups)) { 72 $groups = false; 73 } 74 } 75 if ($what == 'all') { 76 $users = $user->id; 77 $courses[SITEID] = new stdClass; 78 $courses[SITEID]->shortname = get_string('globalevents', 'calendar'); 79 $paramcourses[SITEID] = $courses[SITEID]; 80 } else if ($what == 'groups') { 81 $users = false; 82 $paramcourses = array(); 83 } else if ($what == 'user') { 84 $users = $user->id; 85 $groups = false; 86 $paramcourses = array(); 87 } else { 88 $users = false; 89 $groups = false; 90 } 91 92 // Store the number of days in the week. 93 $numberofdaysinweek = $calendartype->get_num_weekdays(); 94 95 switch($time) { 96 case 'weeknow': 97 $startweekday = calendar_get_starting_weekday(); 98 $startmonthday = find_day_in_month($now['mday'] - ($numberofdaysinweek - 1), $startweekday, $now['mon'], $now['year']); 99 $startmonth = $now['mon']; 100 $startyear = $now['year']; 101 if($startmonthday > calendar_days_in_month($startmonth, $startyear)) { 102 list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear); 103 $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear); 104 } 105 $gregoriandate = $calendartype->convert_to_gregorian($startyear, $startmonth, $startmonthday); 106 $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'], 107 $gregoriandate['hour'], $gregoriandate['minute']); 108 109 $endmonthday = $startmonthday + $numberofdaysinweek; 110 $endmonth = $startmonth; 111 $endyear = $startyear; 112 if($endmonthday > calendar_days_in_month($endmonth, $endyear)) { 113 list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear); 114 $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear); 115 } 116 $gregoriandate = $calendartype->convert_to_gregorian($endyear, $endmonth, $endmonthday); 117 $timeend = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'], 118 $gregoriandate['hour'], $gregoriandate['minute']); 119 break; 120 case 'weeknext': 121 $startweekday = calendar_get_starting_weekday(); 122 $startmonthday = find_day_in_month($now['mday'] + 1, $startweekday, $now['mon'], $now['year']); 123 $startmonth = $now['mon']; 124 $startyear = $now['year']; 125 if($startmonthday > calendar_days_in_month($startmonth, $startyear)) { 126 list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear); 127 $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear); 128 } 129 $gregoriandate = $calendartype->convert_to_gregorian($startyear, $startmonth, $startmonthday); 130 $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'], 131 $gregoriandate['hour'], $gregoriandate['minute']); 132 133 $endmonthday = $startmonthday + $numberofdaysinweek; 134 $endmonth = $startmonth; 135 $endyear = $startyear; 136 if($endmonthday > calendar_days_in_month($endmonth, $endyear)) { 137 list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear); 138 $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear); 139 } 140 $gregoriandate = $calendartype->convert_to_gregorian($endyear, $endmonth, $endmonthday); 141 $timeend = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'], 142 $gregoriandate['hour'], $gregoriandate['minute']); 143 break; 144 case 'monthnow': 145 // Convert to gregorian. 146 $gregoriandate = $calendartype->convert_to_gregorian($now['year'], $now['mon'], 1); 147 148 $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'], 149 $gregoriandate['hour'], $gregoriandate['minute']); 150 $timeend = $timestart + (calendar_days_in_month($now['mon'], $now['year']) * DAYSECS); 151 break; 152 case 'monthnext': 153 // Get the next month for this calendar. 154 list($nextmonth, $nextyear) = calendar_add_month($now['mon'], $now['year']); 155 156 // Convert to gregorian. 157 $gregoriandate = $calendartype->convert_to_gregorian($nextyear, $nextmonth, 1); 158 159 // Create the timestamps. 160 $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'], 161 $gregoriandate['hour'], $gregoriandate['minute']); 162 $timeend = $timestart + (calendar_days_in_month($nextmonth, $nextyear) * DAYSECS); 163 break; 164 case 'recentupcoming': 165 //Events in the last 5 or next 60 days 166 $timestart = time() - 432000; 167 $timeend = time() + 5184000; 168 break; 169 case 'custom': 170 // Events based on custom date range. 171 $timestart = time() - $CFG->calendar_exportlookback * DAYSECS; 172 $timeend = time() + $CFG->calendar_exportlookahead * DAYSECS; 173 break; 174 } 175 } 176 else { 177 // Parameters given but incorrect, redirect back to export page 178 redirect($CFG->wwwroot.'/calendar/export.php'); 179 die(); 180 } 181 } 182 $events = calendar_get_events($timestart, $timeend, $users, $groups, array_keys($paramcourses), false); 183 184 $ical = new iCalendar; 185 $ical->add_property('method', 'PUBLISH'); 186 foreach($events as $event) { 187 if (!empty($event->modulename)) { 188 $cm = get_coursemodule_from_instance($event->modulename, $event->instance); 189 if (!\core_availability\info_module::is_user_visible($cm, $userid, false)) { 190 continue; 191 } 192 } 193 $hostaddress = str_replace('http://', '', $CFG->wwwroot); 194 $hostaddress = str_replace('https://', '', $hostaddress); 195 196 $ev = new iCalendar_event; 197 $ev->add_property('uid', $event->id.'@'.$hostaddress); 198 $ev->add_property('summary', $event->name); 199 $ev->add_property('description', clean_param($event->description, PARAM_NOTAGS)); 200 $ev->add_property('class', 'PUBLIC'); // PUBLIC / PRIVATE / CONFIDENTIAL 201 $ev->add_property('last-modified', Bennu::timestamp_to_datetime($event->timemodified)); 202 $ev->add_property('dtstamp', Bennu::timestamp_to_datetime()); // now 203 if ($event->timeduration > 0) { 204 //dtend is better than duration, because it works in Microsoft Outlook and works better in Korganizer 205 $ev->add_property('dtstart', Bennu::timestamp_to_datetime($event->timestart)); // when event starts. 206 $ev->add_property('dtend', Bennu::timestamp_to_datetime($event->timestart + $event->timeduration)); 207 } else { 208 // When no duration is present, ie an all day event, VALUE should be date instead of time and dtend = dtstart + 1 day. 209 $ev->add_property('dtstart', Bennu::timestamp_to_date($event->timestart), array('value' => 'DATE')); // All day event. 210 $ev->add_property('dtend', Bennu::timestamp_to_date($event->timestart + DAYSECS), array('value' => 'DATE')); // All day event. 211 } 212 if ($event->courseid != 0) { 213 $coursecontext = context_course::instance($event->courseid); 214 $ev->add_property('categories', format_string($courses[$event->courseid]->shortname, true, array('context' => $coursecontext))); 215 } 216 $ical->add_component($ev); 217 } 218 219 $serialized = $ical->serialize(); 220 if(empty($serialized)) { 221 // TODO 222 die('bad serialization'); 223 } 224 225 $filename = 'icalexport.ics'; 226 227 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT'); 228 header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0'); 229 header('Expires: '. gmdate('D, d M Y H:i:s', 0) .'GMT'); 230 header('Pragma: no-cache'); 231 header('Accept-Ranges: none'); // Comment out if PDFs do not work... 232 header('Content-disposition: attachment; filename='.$filename); 233 header('Content-length: '.strlen($serialized)); 234 header('Content-type: text/calendar; charset=utf-8'); 235 236 echo $serialized;
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |