Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]
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 raise_memory_limit(MEMORY_HUGE); 9 10 $userid = optional_param('userid', 0, PARAM_INT); 11 $username = optional_param('username', '', PARAM_TEXT); 12 $authtoken = required_param('authtoken', PARAM_ALPHANUM); 13 $generateurl = optional_param('generateurl', '', PARAM_TEXT); 14 15 if (empty($CFG->enablecalendarexport)) { 16 die('no export'); 17 } 18 19 //Fetch user information 20 $checkuserid = !empty($userid) && $user = $DB->get_record('user', array('id' => $userid), 'id,password'); 21 //allowing for fallback check of old url - MDL-27542 22 $checkusername = !empty($username) && $user = $DB->get_record('user', array('username' => $username), 'id,password'); 23 if ((!$checkuserid && !$checkusername) || !$user) { 24 //No such user 25 die('Invalid authentication'); 26 } 27 28 //Check authentication token 29 $authuserid = !empty($userid) && $authtoken == calendar_get_export_token($user); 30 //allowing for fallback check of old url - MDL-27542 31 $authusername = !empty($username) && $authtoken == sha1($username . $user->password . $CFG->calendar_exportsalt); 32 if (!$authuserid && !$authusername) { 33 die('Invalid authentication'); 34 } 35 36 $PAGE->set_context(context_system::instance()); 37 38 // Get the calendar type we are using. 39 $calendartype = \core_calendar\type_factory::get_calendar_instance(); 40 41 $what = optional_param('preset_what', 'all', PARAM_ALPHA); 42 $time = optional_param('preset_time', 'weeknow', PARAM_ALPHA); 43 44 $now = $calendartype->timestamp_to_date_array(time()); 45 46 // Let's see if we have sufficient and correct data 47 $allowedwhat = ['all', 'user', 'groups', 'courses', 'categories']; 48 $allowedtime = ['weeknow', 'weeknext', 'monthnow', 'monthnext', 'recentupcoming', 'custom']; 49 50 if (!empty($generateurl)) { 51 $authtoken = calendar_get_export_token($user); 52 $params = array(); 53 $params['preset_what'] = $what; 54 $params['preset_time'] = $time; 55 $params['userid'] = $userid; 56 $params['authtoken'] = $authtoken; 57 $params['generateurl'] = true; 58 59 $link = new moodle_url('/calendar/export.php', $params); 60 redirect($link->out()); 61 die; 62 } 63 $paramcategory = false; 64 if(!empty($what) && !empty($time)) { 65 if(in_array($what, $allowedwhat) && in_array($time, $allowedtime)) { 66 $courses = enrol_get_users_courses($user->id, true, 'id, visible, shortname'); 67 // Array of courses that we will pass to calendar_get_legacy_events() which 68 // is initially set to the list of the user's courses. 69 $paramcourses = $courses; 70 if ($what == 'all' || $what == 'groups') { 71 $groups = array(); 72 foreach ($courses as $course) { 73 $course_groups = groups_get_all_groups($course->id, $user->id); 74 $groups = array_merge($groups, array_keys($course_groups)); 75 } 76 if (empty($groups)) { 77 $groups = false; 78 } 79 } 80 if ($what == 'all') { 81 $users = $user->id; 82 $courses[SITEID] = new stdClass; 83 $courses[SITEID]->shortname = get_string('siteevents', 'calendar'); 84 $paramcourses[SITEID] = $courses[SITEID]; 85 $paramcategory = true; 86 } else if ($what == 'groups') { 87 $users = false; 88 $paramcourses = array(); 89 } else if ($what == 'user') { 90 $users = $user->id; 91 $groups = false; 92 $paramcourses = array(); 93 } else if ($what == 'categories') { 94 $users = $user->id; 95 $groups = false; 96 $paramcourses = array(); 97 $paramcategory = true; 98 } else { 99 $users = false; 100 $groups = false; 101 } 102 103 // Store the number of days in the week. 104 $numberofdaysinweek = $calendartype->get_num_weekdays(); 105 106 switch($time) { 107 case 'weeknow': 108 $startweekday = calendar_get_starting_weekday(); 109 $startmonthday = find_day_in_month($now['mday'] - ($numberofdaysinweek - 1), $startweekday, $now['mon'], $now['year']); 110 $startmonth = $now['mon']; 111 $startyear = $now['year']; 112 if ($startmonthday > calendar_days_in_month($startmonth, $startyear)) { 113 list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear); 114 $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear); 115 } 116 $gregoriandate = $calendartype->convert_to_gregorian($startyear, $startmonth, $startmonthday); 117 $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'], 118 $gregoriandate['hour'], $gregoriandate['minute']); 119 120 $endmonthday = $startmonthday + $numberofdaysinweek; 121 $endmonth = $startmonth; 122 $endyear = $startyear; 123 if ($endmonthday > calendar_days_in_month($endmonth, $endyear)) { 124 list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear); 125 $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear); 126 } 127 $gregoriandate = $calendartype->convert_to_gregorian($endyear, $endmonth, $endmonthday); 128 $timeend = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'], 129 $gregoriandate['hour'], $gregoriandate['minute']); 130 break; 131 case 'weeknext': 132 $startweekday = calendar_get_starting_weekday(); 133 $startmonthday = find_day_in_month($now['mday'] + 1, $startweekday, $now['mon'], $now['year']); 134 $startmonth = $now['mon']; 135 $startyear = $now['year']; 136 if ($startmonthday > calendar_days_in_month($startmonth, $startyear)) { 137 list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear); 138 $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear); 139 } 140 $gregoriandate = $calendartype->convert_to_gregorian($startyear, $startmonth, $startmonthday); 141 $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'], 142 $gregoriandate['hour'], $gregoriandate['minute']); 143 144 $endmonthday = $startmonthday + $numberofdaysinweek; 145 $endmonth = $startmonth; 146 $endyear = $startyear; 147 if ($endmonthday > calendar_days_in_month($endmonth, $endyear)) { 148 list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear); 149 $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear); 150 } 151 $gregoriandate = $calendartype->convert_to_gregorian($endyear, $endmonth, $endmonthday); 152 $timeend = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'], 153 $gregoriandate['hour'], $gregoriandate['minute']); 154 break; 155 case 'monthnow': 156 // Convert to gregorian. 157 $gregoriandate = $calendartype->convert_to_gregorian($now['year'], $now['mon'], 1); 158 159 $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'], 160 $gregoriandate['hour'], $gregoriandate['minute']); 161 $timeend = $timestart + (calendar_days_in_month($now['mon'], $now['year']) * DAYSECS); 162 break; 163 case 'monthnext': 164 // Get the next month for this calendar. 165 list($nextmonth, $nextyear) = calendar_add_month($now['mon'], $now['year']); 166 167 // Convert to gregorian. 168 $gregoriandate = $calendartype->convert_to_gregorian($nextyear, $nextmonth, 1); 169 170 // Create the timestamps. 171 $timestart = make_timestamp($gregoriandate['year'], $gregoriandate['month'], $gregoriandate['day'], 172 $gregoriandate['hour'], $gregoriandate['minute']); 173 $timeend = $timestart + (calendar_days_in_month($nextmonth, $nextyear) * DAYSECS); 174 break; 175 case 'recentupcoming': 176 //Events in the last 5 or next 60 days 177 $timestart = time() - 432000; 178 $timeend = time() + 5184000; 179 break; 180 case 'custom': 181 // Events based on custom date range. 182 $timestart = time() - $CFG->calendar_exportlookback * DAYSECS; 183 $timeend = time() + $CFG->calendar_exportlookahead * DAYSECS; 184 break; 185 } 186 } 187 else { 188 // Parameters given but incorrect, redirect back to export page 189 redirect($CFG->wwwroot.'/calendar/export.php'); 190 die(); 191 } 192 } 193 $limitnum = 0; 194 $events = calendar_get_legacy_events($timestart, $timeend, $users, $groups, array_keys($paramcourses), false, true, 195 $paramcategory, $limitnum); 196 197 $ical = new iCalendar; 198 $ical->add_property('method', 'PUBLISH'); 199 $ical->add_property('prodid', '-//Moodle Pty Ltd//NONSGML Moodle Version ' . $CFG->version . '//EN'); 200 foreach($events as $event) { 201 if (!empty($event->modulename)) { 202 $instances = get_fast_modinfo($event->courseid, $userid)->get_instances_of($event->modulename); 203 if (empty($instances[$event->instance]->uservisible)) { 204 continue; 205 } 206 } 207 $hostaddress = str_replace('http://', '', $CFG->wwwroot); 208 $hostaddress = str_replace('https://', '', $hostaddress); 209 210 $me = new calendar_event($event); // To use moodle calendar event services. 211 $ev = new iCalendar_event; // To export in ical format. 212 $ev->add_property('uid', $event->id.'@'.$hostaddress); 213 214 // Set iCal event summary from event name. 215 $ev->add_property('summary', format_string($event->name, true, ['context' => $me->context])); 216 217 // Format the description text. 218 $description = format_text($me->description, $me->format, ['context' => $me->context]); 219 // Then convert it to plain text, since it's the only format allowed for the event description property. 220 // We use html_to_text in order to convert <br> and <p> tags to new line characters for descriptions in HTML format. 221 $description = html_to_text($description, 0); 222 $ev->add_property('description', $description); 223 224 $ev->add_property('class', 'PUBLIC'); // PUBLIC / PRIVATE / CONFIDENTIAL 225 $ev->add_property('last-modified', Bennu::timestamp_to_datetime($event->timemodified)); 226 227 if (!empty($event->location)) { 228 $ev->add_property('location', $event->location); 229 } 230 231 $ev->add_property('dtstamp', Bennu::timestamp_to_datetime()); // now 232 if ($event->timeduration > 0) { 233 //dtend is better than duration, because it works in Microsoft Outlook and works better in Korganizer 234 $ev->add_property('dtstart', Bennu::timestamp_to_datetime($event->timestart)); // when event starts. 235 $ev->add_property('dtend', Bennu::timestamp_to_datetime($event->timestart + $event->timeduration)); 236 } else if ($event->timeduration == 0) { 237 // When no duration is present, the event is instantaneous event, ex - Due date of a module. 238 // Moodle doesn't support all day events yet. See MDL-56227. 239 $ev->add_property('dtstart', Bennu::timestamp_to_datetime($event->timestart)); 240 $ev->add_property('dtend', Bennu::timestamp_to_datetime($event->timestart)); 241 } else { 242 // This can be used to represent all day events in future. 243 throw new coding_exception("Negative duration is not supported yet."); 244 } 245 if ($event->courseid != 0) { 246 $coursecontext = context_course::instance($event->courseid); 247 $ev->add_property('categories', format_string($courses[$event->courseid]->shortname, true, array('context' => $coursecontext))); 248 } 249 $ical->add_component($ev); 250 } 251 252 $serialized = $ical->serialize(); 253 if(empty($serialized)) { 254 // TODO 255 die('bad serialization'); 256 } 257 258 $filename = 'icalexport.ics'; 259 260 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT'); 261 header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0'); 262 header('Expires: '. gmdate('D, d M Y H:i:s', 0) .'GMT'); 263 header('Pragma: no-cache'); 264 header('Accept-Ranges: none'); // Comment out if PDFs do not work... 265 header('Content-disposition: attachment; filename='.$filename); 266 header('Content-length: '.strlen($serialized)); 267 header('Content-type: text/calendar; charset=utf-8'); 268 269 echo $serialized;
title
Description
Body
title
Description
Body
title
Description
Body
title
Body