Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

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