Exemple #1
0
/**
 * Add an iCalendar event to the Moodle calendar.
 *
 * @param stdClass $event The RFC-2445 iCalendar event
 * @param int $courseid The course ID
 * @param int $subscriptionid The iCalendar subscription ID
 * @param string $timezone The X-WR-TIMEZONE iCalendar property if provided
 * @throws dml_exception A DML specific exception is thrown for invalid subscriptionids.
 * @return int Code: CALENDAR_IMPORT_EVENT_UPDATED = updated,  CALENDAR_IMPORT_EVENT_INSERTED = inserted, 0 = error
 */
function calendar_add_icalendar_event($event, $courseid, $subscriptionid, $timezone = 'UTC')
{
    global $DB;
    // Probably an unsupported X-MICROSOFT-CDO-BUSYSTATUS event.
    if (empty($event->properties['SUMMARY'])) {
        return 0;
    }
    $name = $event->properties['SUMMARY'][0]->value;
    $name = str_replace('\\n', '<br />', $name);
    $name = str_replace('\\', '', $name);
    $name = preg_replace('/\\s+/u', ' ', $name);
    $eventrecord = new stdClass();
    $eventrecord->name = clean_param($name, PARAM_NOTAGS);
    if (empty($event->properties['DESCRIPTION'][0]->value)) {
        $description = '';
    } else {
        $description = $event->properties['DESCRIPTION'][0]->value;
        $description = clean_param($description, PARAM_NOTAGS);
        $description = str_replace('\\n', '<br />', $description);
        $description = str_replace('\\', '', $description);
        $description = preg_replace('/\\s+/u', ' ', $description);
    }
    $eventrecord->description = $description;
    // Probably a repeating event with RRULE etc. TODO: skip for now.
    if (empty($event->properties['DTSTART'][0]->value)) {
        return 0;
    }
    $tz = isset($event->properties['DTSTART'][0]->parameters['TZID']) ? $event->properties['DTSTART'][0]->parameters['TZID'] : $timezone;
    $tz = core_date::normalise_timezone($tz);
    $eventrecord->timestart = strtotime($event->properties['DTSTART'][0]->value . ' ' . $tz);
    if (empty($event->properties['DTEND'])) {
        $eventrecord->timeduration = 0;
        // no duration if no end time specified
    } else {
        $endtz = isset($event->properties['DTEND'][0]->parameters['TZID']) ? $event->properties['DTEND'][0]->parameters['TZID'] : $timezone;
        $endtz = core_date::normalise_timezone($endtz);
        $eventrecord->timeduration = strtotime($event->properties['DTEND'][0]->value . ' ' . $endtz) - $eventrecord->timestart;
    }
    // Check to see if it should be treated as an all day event.
    if ($eventrecord->timeduration == DAYSECS) {
        // Check to see if the event started at Midnight on the imported calendar.
        date_default_timezone_set($timezone);
        if (date('H:i:s', $eventrecord->timestart) === "00:00:00") {
            // This event should be an all day event.
            $eventrecord->timeduration = 0;
        }
        core_date::set_default_server_timezone();
    }
    $eventrecord->uuid = $event->properties['UID'][0]->value;
    $eventrecord->timemodified = time();
    // Add the iCal subscription details if required.
    // We should never do anything with an event without a subscription reference.
    $sub = calendar_get_subscription($subscriptionid);
    $eventrecord->subscriptionid = $subscriptionid;
    $eventrecord->userid = $sub->userid;
    $eventrecord->groupid = $sub->groupid;
    $eventrecord->courseid = $sub->courseid;
    $eventrecord->eventtype = $sub->eventtype;
    if ($updaterecord = $DB->get_record('event', array('uuid' => $eventrecord->uuid))) {
        $eventrecord->id = $updaterecord->id;
        $return = CALENDAR_IMPORT_EVENT_UPDATED;
        // Update.
    } else {
        $return = CALENDAR_IMPORT_EVENT_INSERTED;
        // Insert.
    }
    if ($createdevent = calendar_event::create($eventrecord, false)) {
        if (!empty($event->properties['RRULE'])) {
            // Repeating events.
            date_default_timezone_set($tz);
            // Change time zone to parse all events.
            $rrule = new \core_calendar\rrule_manager($event->properties['RRULE'][0]->value);
            $rrule->parse_rrule();
            $rrule->create_events($createdevent);
            core_date::set_default_server_timezone();
            // Change time zone back to what it was.
        }
        return $return;
    } else {
        return 0;
    }
}
Exemple #2
0
 /**
  * Sanity test for PHP stuff.
  */
 public function test_php_gmt_offsets()
 {
     $this->resetAfterTest();
     $this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
     for ($i = -12; $i < 0; $i++) {
         $date = new DateTime('now', new DateTimeZone("Etc/GMT{$i}"));
         $this->assertSame(-$i * 60 * 60, $date->getOffset());
         $date = new DateTime('now', new DateTimeZone(core_date::normalise_timezone("GMT{$i}")));
         $this->assertSame($i * 60 * 60, $date->getOffset());
         $date = new DateTime('now', new DateTimeZone(core_date::normalise_timezone("UTC{$i}")));
         $this->assertSame($i * 60 * 60, $date->getOffset());
     }
     $date = new DateTime('now', new DateTimeZone('Etc/GMT'));
     $this->assertSame(0, $date->getOffset());
     for ($i = 1; $i <= 12; $i++) {
         $date = new DateTime('now', new DateTimeZone("Etc/GMT+{$i}"));
         $this->assertSame(-$i * 60 * 60, $date->getOffset());
         $date = new DateTime('now', new DateTimeZone(core_date::normalise_timezone("GMT+{$i}")));
         $this->assertSame($i * 60 * 60, $date->getOffset());
         $date = new DateTime('now', new DateTimeZone(core_date::normalise_timezone("UTC+{$i}")));
         $this->assertSame($i * 60 * 60, $date->getOffset());
     }
 }
Exemple #3
0
/**
 * Returns an int which represents the systems's timezone difference from GMT in seconds
 * @deprecated since Moodle 2.9
 * @param float|int|string $tz timezone for which offset is required.
 *        {@link http://docs.moodle.org/dev/Time_API#Timezone}
 * @return int|bool if found, false is timezone 99 or error
 */
function get_timezone_offset($tz)
{
    debugging('get_timezone_offset() is deprecated, use PHP DateTimeZone instead', DEBUG_DEVELOPER);
    $date = new DateTime('now', new DateTimeZone(core_date::normalise_timezone($tz)));
    return $date->getOffset() - dst_offset_on(time(), $tz);
}
 /**
  * Creates the block's main content
  *
  * @return string
  */
 public function get_content()
 {
     global $USER, $OUTPUT, $CFG;
     if (isset($this->content)) {
         return $this->content;
     }
     // Establish settings variables based on instance config.
     $showserverclock = !isset($this->config->show_clocks) || $this->config->show_clocks == B_SIMPLE_CLOCK_SHOW_BOTH || $this->config->show_clocks == B_SIMPLE_CLOCK_SHOW_SERVER_ONLY;
     $showuserclock = !isset($this->config->show_clocks) || $this->config->show_clocks == B_SIMPLE_CLOCK_SHOW_BOTH || $this->config->show_clocks == B_SIMPLE_CLOCK_SHOW_USER_ONLY;
     $showicons = !isset($this->config->show_icons) || $this->config->show_icons == 1;
     $showseconds = isset($this->config->show_seconds) && $this->config->show_seconds == 1;
     $showday = isset($this->config->show_day) && $this->config->show_day == 1;
     $show24hrtime = isset($this->config->twenty_four_hour_time) && $this->config->twenty_four_hour_time == 1;
     // Start the content, which is primarily a table.
     $this->content = new stdClass();
     $this->content->text = '';
     $this->content->footer = '';
     $table = new html_table();
     $table->attributes = array('class' => 'clockTable');
     // First item added is the server's clock.
     if ($showserverclock) {
         $row = array();
         if ($showicons) {
             $alt = get_string('server', 'block_simple_clock');
             $usingie = false;
             if (class_exists('core_useragent')) {
                 $usingie = core_useragent::is_ie();
             } else {
                 $usingie = check_browser_version('MSIE');
             }
             if ($usingie) {
                 $servericon = $OUTPUT->pix_icon('server', $alt, 'block_simple_clock');
             } else {
                 $servericon = $OUTPUT->pix_icon('favicon', $alt, 'theme');
             }
             $row[] = $servericon;
         }
         $row[] = get_string('server', 'block_simple_clock') . ':';
         $attributes = array();
         $attributes['class'] = 'clock';
         $attributes['id'] = 'block_progress_serverTime';
         $attributes['value'] = get_string('loading', 'block_simple_clock');
         $row[] = HTML_WRITER::empty_tag('input', $attributes);
         $table->data[] = $row;
     }
     // Next item is the user's clock.
     if ($showuserclock) {
         $row = array();
         if ($showicons) {
             if ($USER->id != 0) {
                 $userpictureparams = array('size' => 16, 'link' => false, 'alt' => 'User');
                 $userpicture = $OUTPUT->user_picture($USER, $userpictureparams);
                 $row[] = $userpicture;
             } else {
                 $row[] = '';
             }
         }
         $row[] = get_string('you', 'block_simple_clock') . ':';
         $attributes = array();
         $attributes['class'] = 'clock';
         $attributes['id'] = 'block_progress_youTime';
         $attributes['value'] = get_string('loading', 'block_simple_clock');
         $row[] = HTML_WRITER::empty_tag('input', $attributes);
         $table->data[] = $row;
     }
     $this->content->text .= HTML_WRITER::table($table);
     // Set up JavaScript code needed to keep the clock going.
     $noscriptstring = get_string('javascript_disabled', 'block_simple_clock');
     $this->content->text .= HTML_WRITER::tag('noscript', $noscriptstring);
     if ($CFG->timezone != 99) {
         // Ensure that the Moodle timezone is set correctly.
         $date = new DateTime('now', new DateTimeZone(core_date::normalise_timezone($CFG->timezone)));
         $moodletimeoffset = $date->getOffset();
         // + dst_offset_on(time(), $CFG->timezone);
         $servertimeoffset = date_offset_get(new DateTime());
         $timearray = localtime(time() + $moodletimeoffset - $servertimeoffset, true);
     } else {
         // Ensure that the server timezone is set.
         // From 2.9 onwards, this should never happen.
         $timearray = localtime(time(), true);
     }
     $arguments = array($showserverclock, $showuserclock, $showseconds, $showday, $show24hrtime, $timearray['tm_year'] + 1900, $timearray['tm_mon'], $timearray['tm_mday'], $timearray['tm_hour'], $timearray['tm_min'], $timearray['tm_sec'] + 2);
     $jsmodule = array('name' => 'block_simple_clock', 'fullpath' => '/blocks/simple_clock/module.js', 'requires' => array(), 'strings' => array(array('clock_separator', 'block_simple_clock'), array('before_noon', 'block_simple_clock'), array('after_noon', 'block_simple_clock'), array('day_names', 'block_simple_clock')));
     $this->page->requires->js_init_call('M.block_simple_clock.initSimpleClock', $arguments, false, $jsmodule);
     $this->content->footer = '';
     return $this->content;
 }