Skip to content

Commit

Permalink
MDL-71790 calendar: Create a new calendar import page
Browse files Browse the repository at this point in the history
The commit was originally from MDL-71808
  • Loading branch information
dcai authored and HuongNV13 committed Jul 13, 2021
1 parent c357779 commit ff035c0
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 49 deletions.
4 changes: 1 addition & 3 deletions calendar/classes/local/event/forms/managesubscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ public function definition() {
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');
Expand Down Expand Up @@ -86,7 +84,7 @@ public function definition() {
$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')]);
Expand Down
131 changes: 131 additions & 0 deletions calendar/import.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* 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();
47 changes: 1 addition & 46 deletions calendar/managesubscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
// 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');
Expand Down Expand Up @@ -62,51 +61,9 @@
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)) {
Expand Down Expand Up @@ -206,6 +163,4 @@

// Display a table of subscriptions.
echo $renderer->subscription_details($courseid, $subscriptions, $importresults);
// Display the add subscription form.
$form->display();
echo $OUTPUT->footer();
1 change: 1 addition & 0 deletions lang/en/calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
$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';
Expand Down

0 comments on commit ff035c0

Please sign in to comment.