[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/calendar/ -> export_execute.php (source)

   1  <?php // $Id: export_execute.php,v 1.5.2.5 2008/10/08 19:09:43 skodak Exp $
   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  $username = required_param('username', PARAM_TEXT);
   9  $authtoken = required_param('authtoken', PARAM_ALPHANUM);
  10  
  11  //Fetch user information
  12  if (!$user = get_complete_user_data('username', $username)) {
  13     //No such user
  14     die("No such user '$username'");
  15  }
  16  
  17  //Check authentication token
  18  if ($authtoken != sha1($username . $user->password)) {
  19      die('Invalid authentication token');
  20  }
  21  
  22  $what = optional_param('preset_what', 'all', PARAM_ALPHA);
  23  $time = optional_param('preset_time', 'weeknow', PARAM_ALPHA);
  24  
  25  $now = usergetdate(time());
  26  // Let's see if we have sufficient and correct data
  27  $allowed_what = array('all', 'courses');
  28  $allowed_time = array('weeknow', 'weeknext', 'monthnow', 'monthnext', 'recentupcoming');
  29  
  30  if(!empty($what) && !empty($time)) {
  31      if(in_array($what, $allowed_what) && in_array($time, $allowed_time)) {
  32          $courses = get_my_courses($user->id, NULL, 'id, visible, shortname');
  33  
  34          if ($what == 'all') {
  35              $users = $user->id;
  36              $groups = array();
  37              foreach ($courses as $course) {
  38                  $course_groups = groups_get_all_groups($course->id, $user->id);
  39                  if ($course_groups) {
  40                      $groups = array_merge($groups, array_keys($course_groups));
  41                  }
  42              }
  43              if (empty($groups)) {
  44                  $groups = false;
  45              }
  46              $courses[SITEID] = new stdClass;
  47              $courses[SITEID]->shortname = get_string('globalevents', 'calendar');
  48          } else {
  49              $users = false;
  50              $groups = false;
  51          }
  52  
  53          switch($time) {
  54              case 'weeknow':
  55                  $startweekday  = get_user_preferences('calendar_startwday', CALENDAR_STARTING_WEEKDAY);
  56                  $startmonthday = find_day_in_month($now['mday'] - 6, $startweekday, $now['mon'], $now['year']);
  57                  $startmonth    = $now['mon'];
  58                  $startyear     = $now['year'];
  59                  if($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
  60                      list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
  61                      $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
  62                  }
  63                  $timestart = make_timestamp($startyear, $startmonth, $startmonthday);
  64                  $endmonthday = $startmonthday + 7;
  65                  $endmonth    = $startmonth;
  66                  $endyear     = $startyear;
  67                  if($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
  68                      list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
  69                      $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
  70                  }
  71                  $timeend = make_timestamp($endyear, $endmonth, $endmonthday) - 1;
  72              break;
  73              case 'weeknext':
  74                  $startweekday  = get_user_preferences('calendar_startwday', CALENDAR_STARTING_WEEKDAY);
  75                  $startmonthday = find_day_in_month($now['mday'] + 1, $startweekday, $now['mon'], $now['year']);
  76                  $startmonth    = $now['mon'];
  77                  $startyear     = $now['year'];
  78                  if($startmonthday > calendar_days_in_month($startmonth, $startyear)) {
  79                      list($startmonth, $startyear) = calendar_add_month($startmonth, $startyear);
  80                      $startmonthday = find_day_in_month(1, $startweekday, $startmonth, $startyear);
  81                  }
  82                  $timestart = make_timestamp($startyear, $startmonth, $startmonthday);
  83                  $endmonthday = $startmonthday + 7;
  84                  $endmonth    = $startmonth;
  85                  $endyear     = $startyear;
  86                  if($endmonthday > calendar_days_in_month($endmonth, $endyear)) {
  87                      list($endmonth, $endyear) = calendar_add_month($endmonth, $endyear);
  88                      $endmonthday = find_day_in_month(1, $startweekday, $endmonth, $endyear);
  89                  }
  90                  $timeend = make_timestamp($endyear, $endmonth, $endmonthday) - 1;
  91              break;
  92              case 'monthnow':
  93                  $timestart = make_timestamp($now['year'], $now['mon'], 1);
  94                  $timeend   = make_timestamp($now['year'], $now['mon'], calendar_days_in_month($now['mon'], $now['year']), 23, 59, 59);
  95              break;
  96              case 'monthnext':
  97                  list($nextmonth, $nextyear) = calendar_add_month($now['mon'], $now['year']);
  98                  $timestart = make_timestamp($nextyear, $nextmonth, 1);
  99                  $timeend   = make_timestamp($nextyear, $nextmonth, calendar_days_in_month($nextmonth, $nextyear), 23, 59, 59);
 100              break;
 101              case 'recentupcoming':
 102                  //Events in the last 5 or next 60 days
 103                  $timestart = time() - 432000;
 104                  $timeend = time() + 5184000;
 105              break;
 106          }
 107      }
 108      else {
 109          // Parameters given but incorrect, redirect back to export page
 110          redirect($CFG->wwwroot.'/calendar/export.php');
 111          die();
 112      }
 113  }
 114  $events = calendar_get_events($timestart, $timeend, $users, $groups, array_keys($courses), false);
 115  
 116  $ical = new iCalendar;
 117  $ical->add_property('method', 'PUBLISH');
 118  foreach($events as $event) {
 119     if (!empty($event->modulename)) {
 120          $cm = get_coursemodule_from_instance($event->modulename, $event->instance);
 121          if (!groups_course_module_visible($cm)) {
 122              continue;
 123          }
 124      }
 125      $ev = new iCalendar_event;
 126      $ev->add_property('summary', $event->name);
 127      $ev->add_property('description', $event->description);
 128      $ev->add_property('class', 'PUBLIC'); // PUBLIC / PRIVATE / CONFIDENTIAL
 129      $ev->add_property('last-modified', Bennu::timestamp_to_datetime($event->timemodified));
 130      $ev->add_property('dtstamp', Bennu::timestamp_to_datetime()); // now
 131      $ev->add_property('dtstart', Bennu::timestamp_to_datetime($event->timestart)); // when event starts
 132      if ($event->timeduration > 0) {
 133          //dtend is better than duration, because it works in Microsoft Outlook and works better in Korganizer
 134          $ev->add_property('dtend', Bennu::timestamp_to_datetime($event->timestart + $event->timeduration));
 135      }
 136      if ($event->courseid != 0) {
 137          $ev->add_property('categories', $courses[$event->courseid]->shortname);
 138      }
 139      $ical->add_component($ev);
 140  }
 141  
 142  $serialized = $ical->serialize();
 143  if(empty($serialized)) {
 144      // TODO
 145      die('bad serialization');
 146  }
 147  
 148  //IE compatibility HACK!
 149  if(ini_get('zlib.output_compression')) {
 150      ini_set('zlib.output_compression', 'Off');
 151  }
 152  
 153  $filename = 'icalexport.ics';
 154  
 155  header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
 156  header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
 157  header('Expires: '. gmdate('D, d M Y H:i:s', 0) .'GMT');
 158  header('Pragma: no-cache');
 159  header('Accept-Ranges: none'); // Comment out if PDFs do not work...
 160  header('Content-disposition: attachment; filename='.$filename);
 161  header('Content-length: '.strlen($serialized));
 162  header('Content-type: text/calendar');
 163  
 164  echo $serialized;
 165  
 166  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7