Exemple #1
0
 /**
  * Returns a SingleCalendar object with all events of the given owner or
  * SingleCalendar object for one day set by timestamp.
  *
  * @param string|SingleCalendar $owner The user id of calendar owner or a calendar object.
  * @param int $time A unix timestamp of this day.
  * @param string $user_id The id of the user who gets access to the calendar (optional, default current user)
  * @param array $restrictions An array with key value pairs of properties to filter the result (optional).
  * @param array $class_names Array of class names. The class must implement Event (optional).
  * @return \SingleCalendar Calendar Object with all events of given day.
  */
 public static function getDayCalendar($owner, $time, $user_id = null, $restrictions = null, $class_names = null)
 {
     $user_id = $user_id ?: $GLOBALS['user']->id;
     if (!is_array($class_names)) {
         $class_names = array('CalendarEvent', 'CourseEvent', 'CourseCancelledEvent', 'CourseMarkedEvent');
     }
     $day = date('Y-m-d-', $time);
     $start = DateTime::createFromFormat('Y-m-d-H:i:s', $day . '00:00:00');
     $end = DateTime::createFromFormat('Y-m-d-H:i:s', $day . '23:59:59');
     if (is_object($owner)) {
         if ($owner instanceof SingleCalendar) {
             $calendar = $owner;
             $calendar->setStart($start->format('U'))->setEnd($end->format('U'));
         } else {
             throw new InvalidArgumentException('The owner must be a user id or an object of type SingleCalendar.');
         }
     } else {
         $calendar = new SingleCalendar($owner, $start->format('U'), $end->format('U'));
     }
     $calendar->getEvents($class_names)->sortEvents();
     $dow = date('w', $calendar->getStart());
     $month = date('n', $calendar->getStart());
     $year = date('Y', $calendar->getStart());
     $events_created = array();
     foreach ($calendar->events as $event) {
         if (!$calendar->havePermission(Calendar::PERMISSION_READABLE, $user_id) && $event->getAccessibility() != 'PUBLIC') {
             continue;
         }
         if (!$event->havePermission(Event::PERMISSION_CONFIDENTIAL, $user_id)) {
             continue;
         }
         if (!SingleCalendar::checkRestriction($event, $restrictions)) {
             continue;
         }
         $properties = $event->getProperties();
         $ts = mktime(12, 0, 0, date('n', $calendar->start), date('j', $calendar->start), date('Y', $calendar->start));
         $rep = $properties['RRULE'];
         $duration = (int) ((mktime(12, 0, 0, date('n', $properties['DTEND']), date('j', $properties['DTEND']), date('Y', $properties['DTEND'])) - mktime(12, 0, 0, date('n', $properties['DTSTART']), date('j', $properties['DTSTART']), date('Y', $properties['DTSTART']))) / 86400);
         // single events or first event
         if ($properties['DTSTART'] >= $calendar->getStart() && $properties['DTEND'] <= $calendar->getEnd()) {
             self::createDayViewEvent($event, $properties['DTSTART'], $properties['DTEND'], $calendar->getStart(), $calendar->getEnd(), $events_created);
         } elseif ($properties['DTSTART'] >= $calendar->getStart() && $properties['DTSTART'] <= $calendar->getEnd()) {
             self::createDayViewEvent($event, $properties['DTSTART'], $properties['DTEND'], $calendar->getStart(), $calendar->getEnd(), $events_created);
         } elseif ($properties['DTSTART'] < $calendar->getStart() && $properties['DTEND'] > $calendar->getEnd()) {
             self::createDayViewEvent($event, $properties['DTSTART'], $properties['DTEND'], $calendar->getStart(), $calendar->getEnd(), $events_created);
         } elseif ($properties['DTEND'] > $calendar->getStart() && $properties['DTEND'] <= $calendar->getEnd()) {
             self::createDayViewEvent($event, $properties['DTSTART'], $properties['DTEND'], $calendar->getStart(), $calendar->getEnd(), $events_created);
         }
         switch ($rep['rtype']) {
             case 'DAILY':
                 /*
                                     if ($calendar->getEnd() > $rep['expire'] + $duration * 86400) {
                    continue;
                                     }
                 *
                 */
                 if ($end > $rep['expire'] + $duration * 86400) {
                     continue;
                 }
                 $ts = $ts + date('I', $rep['ts']) * 3600;
                 $pos = ($ts - $rep['ts']) / 86400 % $rep['linterval'];
                 $start = $ts - $pos * 86400;
                 $end = $start + $duration * 86400;
                 self::createDayViewEvent($event, $start, $end, $calendar->getStart(), $calendar->getEnd(), $events_created);
                 break;
             case 'WEEKLY':
                 $rep['ts'] = $rep['ts'] + (date('I', $rep['ts']) - date('I', $ts)) * 3600;
                 for ($i = 0; $i < strlen($rep['wdays']); $i++) {
                     $pos = (($ts - $dow * 86400 - $rep['ts']) / 86400 - ($rep['wdays'][$i] - 1) + $dow) % ($rep['linterval'] * 7);
                     $start = $ts - $pos * 86400;
                     $end = $start + $duration * 86400;
                     if ($start >= $properties['DTSTART'] && $start <= $ts && $end >= $ts) {
                         self::createDayViewEvent($event, $start, $end, $calendar->getStart(), $calendar->getEnd(), $events_created);
                     }
                 }
                 break;
             case 'MONTHLY':
                 if ($rep['day']) {
                     $lwst = mktime(12, 0, 0, $month - (($year - date('Y', $rep['ts'])) * 12 + ($month - date('n', $rep['ts']))) % $rep['linterval'], $rep['day'], $year);
                     $hgst = $lwst + $duration * 86400;
                     self::createDayViewEvent($event, $lwst, $hgst, $calendar->getStart(), $calendar->getEnd(), $events_created);
                     break;
                 }
                 if ($rep['sinterval']) {
                     $mon = $month - $rep['linterval'];
                     do {
                         $lwst = mktime(12, 0, 0, $mon - (($year - date('Y', $rep['ts'])) * 12 + ($mon - date('n', $rep['ts']))) % $rep['linterval'], 1, $year) + ($rep['sinterval'] - 1) * 604800;
                         $aday = strftime('%u', $lwst);
                         $lwst -= ($aday - $rep['wdays']) * 86400;
                         if ($rep['sinterval'] == 5) {
                             if (date('j', $lwst) < 10) {
                                 $lwst -= 604800;
                             }
                             if (date('n', $lwst) == date('n', $lwst + 604800)) {
                                 $lwst += 604800;
                             }
                         } else {
                             if ($aday > $rep['wdays']) {
                                 $lwst += 604800;
                             }
                         }
                         $hgst = $lwst + $duration * 86400;
                         if ($ts >= $lwst && $ts <= $hgst) {
                             self::createDayViewEvent($event, $lwst, $hgst, $calendar->getStart(), $calendar->getEnd(), $events_created);
                         }
                         $mon += $rep['linterval'];
                     } while ($lwst < $ts);
                 }
                 break;
             case 'YEARLY':
                 if ($ts < $rep['ts']) {
                     break;
                 }
                 if ($rep['day']) {
                     if (date('Y', $properties['DTEND']) - date('Y', $properties['DTSTART'])) {
                         $lwst = mktime(12, 0, 0, $rep['month'], $rep['day'], $year - ($year - date('Y', $rep['ts'])) % $rep['linterval'] - $rep['linterval']);
                         $hgst = $lwst + 86400 * $duration;
                         if ($ts >= $lwst && $ts <= $hgst) {
                             self::createDayViewEvent($event, $lwst, $hgst, $calendar->getStart(), $calendar->getEnd(), $events_created);
                             break;
                         }
                     }
                     $lwst = mktime(12, 0, 0, $rep['month'], $rep['day'], $year - ($year - date('Y', $rep['ts'])) % $rep['linterval']);
                     $hgst = $lwst + 86400 * $duration;
                     self::createDayViewEvent($event, $lwst, $hgst, $calendar->getStart(), $calendar->getEnd(), $events_created);
                     break;
                 }
                 $ayear = $year - 1;
                 do {
                     if ($rep['sinterval']) {
                         $lwst = mktime(12, 0, 0, $rep['month'], 1 + ($rep['sinterval'] - 1) * 7, $ayear);
                         $aday = strftime('%u', $lwst);
                         $lwst -= ($aday - $rep['wdays']) * 86400;
                         if ($rep['sinterval'] == 5) {
                             if (date('j', $lwst) < 10) {
                                 $lwst -= 604800;
                             }
                             if (date('n', $lwst) == date('n', $lwst + 604800)) {
                                 $lwst += 604800;
                             }
                         } elseif ($aday > $rep['wdays']) {
                             $lwst += 604800;
                         }
                         $ayear++;
                         $hgst = $lwst + $duration * 86400;
                         if ($ts >= $lwst && $ts <= $hgst) {
                             self::createDayViewEvent($event, $lwst, $hgst, $calendar->getStart(), $calendar->getEnd(), $events_created);
                         }
                     }
                 } while ($lwst < $ts);
         }
     }
     $calendar->events->exchangeArray(array_values($events_created));
     return $calendar;
 }