From ff035c0aa73bc249ed94ce179bee8b33f6940d32 Mon Sep 17 00:00:00 2001 From: Dongsheng Cai Date: Tue, 22 Jun 2021 19:33:33 +1000 Subject: [PATCH] MDL-71790 calendar: Create a new calendar import page The commit was originally from MDL-71808 --- .../local/event/forms/managesubscriptions.php | 4 +- calendar/import.php | 131 ++++++++++++++++++ calendar/managesubscriptions.php | 47 +------ lang/en/calendar.php | 1 + 4 files changed, 134 insertions(+), 49 deletions(-) create mode 100644 calendar/import.php diff --git a/calendar/classes/local/event/forms/managesubscriptions.php b/calendar/classes/local/event/forms/managesubscriptions.php index 354ed1c72ec..6b79b631047 100644 --- a/calendar/classes/local/event/forms/managesubscriptions.php +++ b/calendar/classes/local/event/forms/managesubscriptions.php @@ -47,8 +47,6 @@ class managesubscriptions extends \moodleform { print_error('nopermissiontoupdatecalendar'); } - $mform->addElement('header', 'addsubscriptionform', get_string('importcalendarheading', 'calendar')); - // Name. $mform->addElement('text', 'name', get_string('subscriptionname', 'calendar'), array('maxsize' => '255', 'size' => '40')); $mform->addRule('name', get_string('required'), 'required'); @@ -86,7 +84,7 @@ class managesubscriptions extends \moodleform { $this->add_event_type_elements($mform, $eventtypes); // Eventtype: 0 = user, 1 = site, anything else = course ID. - $mform->addElement('submit', 'add', get_string('add')); + $mform->addElement('submit', 'add', get_string('importcalendar', 'calendar')); // Add the javascript required to enhance this mform. $PAGE->requires->js_call_amd('core_calendar/event_form', 'init', [$mform->getAttribute('id')]); diff --git a/calendar/import.php b/calendar/import.php new file mode 100644 index 00000000000..eaa0c274a4e --- /dev/null +++ b/calendar/import.php @@ -0,0 +1,131 @@ +. + +/** + * 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'); +$calendarurl = new moodle_url('/calendar/view.php', ['view' => 'month']); +navigation_node::override_active_url($calendarurl); + +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); +} else { + $course = get_site(); +} +require_login($course, false); +if (!calendar_user_can_add_event($course)) { + throw new \moodle_exception('errorcannotimport', 'calendar'); +} + +// 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); +} +if (!empty($category)) { + $pageurl->param('category', $category); + $managesubscriptionsurl->param('category', $category); + $data['category'] = $category; + $data['eventtype'] = 'category'; +} + +$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); + +$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); +} + +$calendarsubscriptions = get_string('calendarsubscriptions', 'calendar'); + +$renderer = $PAGE->get_renderer('core_calendar'); + +echo $OUTPUT->header(); +echo $renderer->start_layout(); +echo $OUTPUT->heading($heading); +$form->display(); +echo $OUTPUT->spacer(null, true); +echo $OUTPUT->action_link($managesubscriptionsurl, $OUTPUT->larrow() . ' ' . $calendarsubscriptions); +echo $renderer->complete_layout(); +echo $OUTPUT->footer(); diff --git a/calendar/managesubscriptions.php b/calendar/managesubscriptions.php index 91fbf6595cd..6fa6129bd2c 100644 --- a/calendar/managesubscriptions.php +++ b/calendar/managesubscriptions.php @@ -33,7 +33,6 @@ $categoryid = optional_param('category', null, PARAM_INT); // Used for processing subscription actions. $subscriptionid = optional_param('id', 0, PARAM_INT); $pollinterval = optional_param('pollinterval', 0, PARAM_INT); -$groupcourseid = optional_param('groupcourseid', 0, PARAM_INT); $action = optional_param('action', '', PARAM_INT); $url = new moodle_url('/calendar/managesubscriptions.php'); @@ -62,51 +61,9 @@ if (!calendar_user_can_add_event($course)) { print_error('errorcannotimport', 'calendar'); } -// 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; - } - } -} -$customdata = [ - 'courseid' => $course->id, - 'groups' => $groups, -]; -$form = new \core_calendar\local\event\forms\managesubscriptions($url, $customdata); -$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, 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); - print_error($e->errorcode, $e->module, $PAGE->url); - } - } - // Redirect to prevent refresh issues. - redirect($PAGE->url, $importresults); -} else if (!empty($subscriptionid)) { +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)) { @@ -206,6 +163,4 @@ foreach($subscriptions as $subscription) { // Display a table of subscriptions. echo $renderer->subscription_details($courseid, $subscriptions, $importresults); -// Display the add subscription form. -$form->display(); echo $OUTPUT->footer(); diff --git a/lang/en/calendar.php b/lang/en/calendar.php index e10e573a840..fa38aee112c 100644 --- a/lang/en/calendar.php +++ b/lang/en/calendar.php @@ -31,6 +31,7 @@ $string['calendar'] = 'Calendar'; $string['calendarheading'] = '{$a} Calendar'; $string['calendarpreferences'] = 'Calendar preferences'; $string['calendartypes'] = 'Calendar types'; +$string['calendarsubscriptions'] = 'Calendar subscriptions'; $string['calendarurl'] = 'Calendar URL: {$a}'; $string['category'] = 'Category'; $string['categoryevent'] = 'Category event';