2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Moodle calendar import
20 * @package core_calendar
21 * @copyright Moodle Pty Ltd
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 require_once('../config.php');
25 require_once($CFG->libdir
. '/bennu/bennu.inc.php');
26 require_once($CFG->dirroot
. '/course/lib.php');
27 require_once($CFG->dirroot
. '/calendar/lib.php');
29 $courseid = optional_param('course', SITEID
, PARAM_INT
);
30 $groupcourseid = optional_param('groupcourseid', 0, PARAM_INT
);
31 $category = optional_param('category', 0, PARAM_INT
);
33 $pageurl = new moodle_url('/calendar/import.php');
34 $managesubscriptionsurl = new moodle_url('/calendar/managesubscriptions.php');
36 if ($courseid != SITEID
&& !empty($courseid)) {
37 $course = get_course($courseid);
38 $data['eventtype'] = 'course';
39 $data['courseid'] = $course->id
;
40 $pageurl->param('course', $course->id
);
41 $managesubscriptionsurl->param('course', $course->id
);
42 navigation_node
::override_active_url(new moodle_url('/course/view.php', ['id' => $course->id
]));
44 get_string('calendar', 'calendar'),
45 new moodle_url('/calendar/view.php', ['view' => 'month', 'course' => $course->id
])
47 } else if (!empty($category)) {
49 $pageurl->param('category', $category);
50 $managesubscriptionsurl->param('category', $category);
51 $data['category'] = $category;
52 $data['eventtype'] = 'category';
53 navigation_node
::override_active_url(new moodle_url('/course/index.php', ['categoryid' => $category]));
54 $PAGE->set_category_by_id($category);
56 get_string('calendar', 'calendar'),
57 new moodle_url('/calendar/view.php', ['view' => 'month', 'category' => $category])
61 navigation_node
::override_active_url(new moodle_url('/calendar/view.php', ['view' => 'month']));
64 require_login($course, false);
65 if (!calendar_user_can_add_event($course)) {
66 throw new \
moodle_exception('errorcannotimport', 'calendar');
69 $heading = get_string('importcalendar', 'calendar');
70 $pagetitle = $course->shortname
. ': ' . get_string('calendar', 'calendar') . ': ' . $heading;
72 $PAGE->set_secondary_navigation(false);
73 $PAGE->set_title($pagetitle);
74 $PAGE->set_heading($heading);
75 $PAGE->set_url($pageurl);
76 $PAGE->set_pagelayout('admin');
77 $PAGE->navbar
->add(get_string('managesubscriptions', 'calendar'), $managesubscriptionsurl);
78 $PAGE->navbar
->add($heading);
80 // Populate the 'group' select box based on the given 'groupcourseid', if necessary.
82 if (!empty($groupcourseid)) {
83 require_once($CFG->libdir
. '/grouplib.php');
84 $groupcoursedata = groups_get_course_data($groupcourseid);
85 if (!empty($groupcoursedata->groups
)) {
86 foreach ($groupcoursedata->groups
as $groupid => $groupdata) {
87 $groups[$groupid] = $groupdata->name
;
90 $data['groupcourseid'] = $groupcourseid;
91 $data['eventtype'] = 'group';
92 $pageurl->param('groupcourseid', $groupcourseid);
94 if (!empty($category)) {
95 $pageurl->param('category', $category);
96 $managesubscriptionsurl->param('category', $category);
97 $data['category'] = $category;
98 $data['eventtype'] = 'category';
101 $heading = get_string('importcalendar', 'calendar');
102 $headingstr = get_string('calendar', 'core_calendar');
103 $headingstr = ($courseid != SITEID
&& !empty($courseid)) ?
"{$headingstr}: {$COURSE->shortname}" : $headingstr;
104 $pagetitle = $course->shortname
. ': ' . get_string('calendar', 'calendar') . ': ' . $heading;
106 $PAGE->set_title($pagetitle);
107 $PAGE->set_heading($headingstr);
108 $PAGE->set_url($pageurl);
109 $PAGE->set_pagelayout('admin');
110 $PAGE->navbar
->add($heading, $pageurl);
111 $renderer = $PAGE->get_renderer('core_calendar');
114 'courseid' => $course->id
,
118 $form = new \core_calendar\local\event\forms\
managesubscriptions(null, $customdata);
119 $form->set_data($data);
121 $formdata = $form->get_data();
122 if (!empty($formdata)) {
124 $subscriptionid = calendar_add_subscription($formdata);
125 if ($formdata->importfrom
== CALENDAR_IMPORT_FROM_FILE
) {
126 // Blank the URL if it's a file import.
128 $calendar = $form->get_file_content('importfile');
129 $ical = new iCalendar();
130 $ical->unserialize($calendar);
131 $importresults = calendar_import_events_from_ical($ical, $subscriptionid);
134 $importresults = calendar_update_subscription_events($subscriptionid);
135 } catch (\moodle_exception
$e) {
136 // Delete newly added subscription and show invalid url error.
137 calendar_delete_subscription($subscriptionid);
138 throw new \
moodle_exception($e->errorcode
, $e->module
, $PAGE->url
);
141 if (!empty($formdata->courseid
)) {
142 $managesubscriptionsurl->param('course', $formdata->courseid
);
144 if (!empty($formdata->categoryid
)) {
145 $managesubscriptionsurl->param('category', $formdata->categoryid
);
147 redirect($managesubscriptionsurl, $renderer->render_import_result($importresults));
150 echo $OUTPUT->header();
151 echo $renderer->start_layout();
152 echo $OUTPUT->heading($heading);
154 echo $renderer->complete_layout();
155 echo $OUTPUT->footer();