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(); // Must have sesskey for all actions. if (calendar_can_edit_subscription($subscriptionid)) { try { $importresults = calendar_process_subscription_row($subscriptionid, $pollinterval, $action); } catch (moodle_exception $e) { // If exception caught, then user should be redirected to page where he/she came from.
/** * Tests for calendar_subscription_deleted event. */ public function test_calendar_subscription_deleted() { global $CFG; require_once $CFG->dirroot . '/calendar/lib.php'; $this->resetAfterTest(true); // Create a mock subscription. $subscription = new stdClass(); $subscription->eventtype = 'site'; $subscription->name = 'test'; $subscription->courseid = $this->course->id; $subscription->id = calendar_add_subscription($subscription); // Trigger and capture the event. $sink = $this->redirectEvents(); calendar_delete_subscription($subscription); $events = $sink->get_events(); $event = reset($events); // Check that the event data is valid. $this->assertInstanceOf('\\core\\event\\calendar_subscription_deleted', $event); $this->assertEquals($subscription->id, $event->objectid); $this->assertEquals($subscription->courseid, $event->other['courseid']); $this->assertDebuggingNotCalled(); $sink->close(); }
/** * Update a subscription from the form data in one of the rows in the existing subscriptions table. * * @param int $subscriptionid The ID of the subscription we are acting upon. * @param int $pollinterval The poll interval to use. * @param int $action The action to be performed. One of update or remove. * @throws dml_exception if invalid subscriptionid is provided * @return string A log of the import progress, including errors */ function calendar_process_subscription_row($subscriptionid, $pollinterval, $action) { // Fetch the subscription from the database making sure it exists. $sub = calendar_get_subscription($subscriptionid); // Update or remove the subscription, based on action. switch ($action) { case CALENDAR_SUBSCRIPTION_UPDATE: // Skip updating file subscriptions. if (empty($sub->url)) { break; } $sub->pollinterval = $pollinterval; calendar_update_subscription($sub); // Update the events. return "<p>" . get_string('subscriptionupdated', 'calendar', $sub->name) . "</p>" . calendar_update_subscription_events($subscriptionid); case CALENDAR_SUBSCRIPTION_REMOVE: calendar_delete_subscription($subscriptionid); return get_string('subscriptionremoved', 'calendar', $sub->name); break; default: break; } return ''; }
/** * Update a subscription from the form data in one of the rows in the existing subscriptions table. * * @param int $subscriptionid The ID of the subscription we are acting upon. * @param int $pollinterval The poll interval to use. * @param int $action The action to be performed. One of update or remove. * @throws dml_exception if invalid subscriptionid is provided * @return string A log of the import progress, including errors */ function calendar_process_subscription_row($subscriptionid, $pollinterval, $action) { global $DB; // Fetch the subscription from the database making sure it exists. $sub = calendar_get_subscription($subscriptionid); $strupdate = get_string('update'); $strremove = get_string('remove'); // Update or remove the subscription, based on action. switch ($action) { case $strupdate: // Skip updating file subscriptions. if (empty($sub->url)) { break; } $sub->pollinterval = $pollinterval; $DB->update_record('event_subscriptions', $sub); // update the cache. $cache = cache::make('core', 'calendar_subscriptions'); $cache->set($sub->id, $sub); // Update the events. return "<p>" . get_string('subscriptionupdated', 'calendar', $sub->name) . "</p>" . calendar_update_subscription_events($subscriptionid); case $strremove: calendar_delete_subscription($subscriptionid); return get_string('subscriptionremoved', 'calendar', $sub->name); break; default: break; } return ''; }