} $form = new calendar_addsubscription_form(null); $form->set_data(array('course' => $course->id)); $importresults = ''; $formdata = $form->get_data(); if (!empty($formdata)) { require_sesskey(); // Must have sesskey for all actions. $subscriptionid = calendar_add_subscription($formdata); if ($formdata->importfrom == CALENDAR_IMPORT_FROM_FILE) { // Blank the URL if it's a file import. $formdata->url = ''; $calendar = $form->get_file_content('importfile'); $ical = new iCalendar(); $ical->unserialize($calendar); $importresults = calendar_import_icalendar_events($ical, $courseid, $subscriptionid); } else { try { $importresults = calendar_update_subscription_events($subscriptionid); } catch (moodle_exception $e) { // Delete newly added subscription and show invalid url error. calendar_delete_subscription($subscriptionid); print_error($e->errorcode, $e->module, $PAGE->url); } } // Redirect to prevent refresh issues. redirect($PAGE->url, $importresults); } else { if (!empty($subscriptionid)) { // The user is wanting to perform an action upon an existing subscription. require_sesskey();
/** * Fetch a calendar subscription and update the events in the calendar. * * @param int $subscriptionid The course ID for the calendar. * @return string A log of the import progress, including errors. */ function calendar_update_subscription_events($subscriptionid) { global $DB; $sub = calendar_get_subscription($subscriptionid); // Don't update a file subscription. TODO: Update from a new uploaded file. if (empty($sub->url)) { return 'File subscription not updated.'; } $ical = calendar_get_icalendar($sub->url); $return = calendar_import_icalendar_events($ical, $sub->courseid, $subscriptionid); $sub->lastupdated = time(); calendar_update_subscription($sub); return $return; }
public function test_calendar_add_subscription() { global $DB, $CFG; require_once $CFG->dirroot . '/lib/bennu/bennu.inc.php'; $this->resetAfterTest(true); // Test for Microsoft Outlook 2010. $subscription = new stdClass(); $subscription->name = 'Microsoft Outlook 2010'; $subscription->importfrom = CALENDAR_IMPORT_FROM_FILE; $subscription->eventtype = 'site'; $id = calendar_add_subscription($subscription); $calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/ms_outlook_2010.ics'); $ical = new iCalendar(); $ical->unserialize($calendar); $this->assertEquals($ical->parser_errors, array()); $sub = calendar_get_subscription($id); $result = calendar_import_icalendar_events($ical, $sub->courseid, $sub->id); $count = $DB->count_records('event', array('subscriptionid' => $sub->id)); $this->assertEquals($count, 1); // Test for OSX Yosemite. $subscription = new stdClass(); $subscription->name = 'OSX Yosemite'; $subscription->importfrom = CALENDAR_IMPORT_FROM_FILE; $subscription->eventtype = 'site'; $id = calendar_add_subscription($subscription); $calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/osx_yosemite.ics'); $ical = new iCalendar(); $ical->unserialize($calendar); $this->assertEquals($ical->parser_errors, array()); $sub = calendar_get_subscription($id); $result = calendar_import_icalendar_events($ical, $sub->courseid, $sub->id); $count = $DB->count_records('event', array('subscriptionid' => $sub->id)); $this->assertEquals($count, 1); // Test for Google Gmail. $subscription = new stdClass(); $subscription->name = 'Google Gmail'; $subscription->importfrom = CALENDAR_IMPORT_FROM_FILE; $subscription->eventtype = 'site'; $id = calendar_add_subscription($subscription); $calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/google_gmail.ics'); $ical = new iCalendar(); $ical->unserialize($calendar); $this->assertEquals($ical->parser_errors, array()); $sub = calendar_get_subscription($id); $result = calendar_import_icalendar_events($ical, $sub->courseid, $sub->id); $count = $DB->count_records('event', array('subscriptionid' => $sub->id)); $this->assertEquals($count, 1); }
/** * Fetch a calendar subscription and update the events in the calendar. * * @param int $subscriptionid The course ID for the calendar. * @return string A log of the import progress, including errors. */ function calendar_update_subscription_events($subscriptionid) { global $DB; $sub = $DB->get_record('event_subscriptions', array('id' => $subscriptionid)); if (empty($sub)) { print_error('errorbadsubscription', 'calendar'); } // Don't update a file subscription. TODO: Update from a new uploaded file. if (empty($sub->url)) { return 'File subscription not updated.'; } $ical = calendar_get_icalendar($sub->url); $return = calendar_import_icalendar_events($ical, $sub->courseid, $subscriptionid); $sub->lastupdated = time(); $DB->update_record('event_subscriptions', $sub); return $return; }
/** * Fetch a calendar subscription and update the events in the calendar. * * @param int $subscriptionid The course ID for the calendar. * @return string A log of the import progress, including errors. */ function calendar_update_subscription_events($subscriptionid) { global $DB; $sub = calendar_get_subscription($subscriptionid); // Don't update a file subscription. TODO: Update from a new uploaded file. if (empty($sub->url)) { return 'File subscription not updated.'; } $ical = calendar_get_icalendar($sub->url); $return = calendar_import_icalendar_events($ical, $sub->courseid, $subscriptionid); $sub->lastupdated = time(); $DB->update_record('event_subscriptions', $sub); // Update the cache. $cache = cache::make('core', 'calendar_subscriptions'); $cache->set($subscriptionid, $sub); return $return; }