. /** * Moodle calendar import * * @package core_calendar * @copyright Moodle Pty Ltd * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ require_once('../config.php'); require_once($CFG->libdir . '/bennu/bennu.inc.php'); require_once($CFG->dirroot . '/course/lib.php'); require_once($CFG->dirroot . '/calendar/lib.php'); $courseid = optional_param('course', SITEID, PARAM_INT); $groupcourseid = optional_param('groupcourseid', 0, PARAM_INT); $category = optional_param('category', 0, PARAM_INT); $data = []; $pageurl = new moodle_url('/calendar/import.php'); $managesubscriptionsurl = new moodle_url('/calendar/managesubscriptions.php'); if ($courseid != SITEID && !empty($courseid)) { $course = get_course($courseid); $data['eventtype'] = 'course'; $data['courseid'] = $course->id; $pageurl->param('course', $course->id); $managesubscriptionsurl->param('course', $course->id); navigation_node::override_active_url(new moodle_url('/course/view.php', ['id' => $course->id])); $PAGE->navbar->add( get_string('calendar', 'calendar'), new moodle_url('/calendar/view.php', ['view' => 'month', 'course' => $course->id]) ); } else if (!empty($category)) { $course = get_site(); $pageurl->param('category', $category); $managesubscriptionsurl->param('category', $category); $data['category'] = $category; $data['eventtype'] = 'category'; navigation_node::override_active_url(new moodle_url('/course/index.php', ['categoryid' => $category])); $PAGE->set_category_by_id($category); $PAGE->navbar->add( get_string('calendar', 'calendar'), new moodle_url('/calendar/view.php', ['view' => 'month', 'category' => $category]) ); } else { $course = get_site(); navigation_node::override_active_url(new moodle_url('/calendar/view.php', ['view' => 'month'])); } require_login($course, false); if (!calendar_user_can_add_event($course)) { throw new \moodle_exception('errorcannotimport', 'calendar'); } $heading = get_string('importcalendar', 'calendar'); $pagetitle = $course->shortname . ': ' . get_string('calendar', 'calendar') . ': ' . $heading; $PAGE->set_title($pagetitle); $PAGE->set_heading($heading); $PAGE->set_url($pageurl); $PAGE->set_pagelayout('admin'); $PAGE->navbar->add(get_string('managesubscriptions', 'calendar'), $managesubscriptionsurl); $PAGE->navbar->add($heading); // Populate the 'group' select box based on the given 'groupcourseid', if necessary. $groups = []; if (!empty($groupcourseid)) { require_once($CFG->libdir . '/grouplib.php'); $groupcoursedata = groups_get_course_data($groupcourseid); if (!empty($groupcoursedata->groups)) { foreach ($groupcoursedata->groups as $groupid => $groupdata) { $groups[$groupid] = $groupdata->name; } } $data['groupcourseid'] = $groupcourseid; $data['eventtype'] = 'group'; $pageurl->param('groupcourseid', $groupcourseid); } $customdata = [ 'courseid' => $course->id, 'groups' => $groups, ]; $form = new \core_calendar\local\event\forms\managesubscriptions(null, $customdata); $form->set_data($data); $formdata = $form->get_data(); if (!empty($formdata)) { require_sesskey(); $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, null, $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); throw new \moodle_exception($e->errorcode, $e->module, $PAGE->url); } } if (!empty($formdata->courseid)) { $managesubscriptionsurl->param('course', $formdata->courseid); } if (!empty($formdata->categoryid)) { $managesubscriptionsurl->param('category', $formdata->categoryid); } redirect($managesubscriptionsurl, $importresults); } $renderer = $PAGE->get_renderer('core_calendar'); echo $OUTPUT->header(); echo $renderer->start_layout(); echo $OUTPUT->heading($heading); $form->display(); echo $renderer->complete_layout(); echo $OUTPUT->footer();