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 * Allows the user to manage calendar subscriptions.
20 * @copyright 2012 Jonathan Harker
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once('../config.php');
26 require_once($CFG->libdir
.'/bennu/bennu.inc.php');
27 require_once($CFG->dirroot
.'/course/lib.php');
28 require_once($CFG->dirroot
.'/calendar/lib.php');
29 require_once($CFG->dirroot
.'/calendar/managesubscriptions_form.php');
32 $courseid = optional_param('course', SITEID
, PARAM_INT
);
33 // Used for processing subscription actions.
34 $subscriptionid = optional_param('id', 0, PARAM_INT
);
35 $pollinterval = optional_param('pollinterval', 0, PARAM_INT
);
36 $action = optional_param('action', '', PARAM_INT
);
38 $url = new moodle_url('/calendar/managesubscriptions.php');
39 if ($courseid != SITEID
) {
40 $url->param('course', $courseid);
42 navigation_node
::override_active_url(new moodle_url('/calendar/view.php', array('view' => 'month')));
44 $PAGE->set_pagelayout('admin');
45 $PAGE->navbar
->add(get_string('managesubscriptions', 'calendar'));
47 if ($courseid != SITEID
&& !empty($courseid)) {
48 $course = $DB->get_record('course', array('id' => $courseid));
49 $courses = array($course->id
=> $course);
52 $courses = calendar_get_default_courses();
54 require_course_login($course);
55 if (!calendar_user_can_add_event($course)) {
56 print_error('errorcannotimport', 'calendar');
59 $form = new calendar_addsubscription_form(null);
60 $form->set_data(array(
61 'course' => $course->id
66 $formdata = $form->get_data();
67 if (!empty($formdata)) {
68 require_sesskey(); // Must have sesskey for all actions.
69 $subscriptionid = calendar_add_subscription($formdata);
70 if ($formdata->importfrom
== CALENDAR_IMPORT_FROM_FILE
) {
71 // Blank the URL if it's a file import.
73 $calendar = $form->get_file_content('importfile');
74 $ical = new iCalendar();
75 $ical->unserialize($calendar);
76 $importresults = calendar_import_icalendar_events($ical, $courseid, $subscriptionid);
79 $importresults = calendar_update_subscription_events($subscriptionid);
80 } catch (moodle_exception
$e) {
81 // Delete newly added subscription and show invalid url error.
82 calendar_delete_subscription($subscriptionid);
83 print_error($e->errorcode
, $e->module
, $PAGE->url
);
86 // Redirect to prevent refresh issues.
87 redirect($PAGE->url
, $importresults);
88 } else if (!empty($subscriptionid)) {
89 // The user is wanting to perform an action upon an existing subscription.
90 require_sesskey(); // Must have sesskey for all actions.
91 if (calendar_can_edit_subscription($subscriptionid)) {
93 $importresults = calendar_process_subscription_row($subscriptionid, $pollinterval, $action);
94 } catch (moodle_exception
$e) {
95 // If exception caught, then user should be redirected to page where he/she came from.
96 print_error($e->errorcode
, $e->module
, $PAGE->url
);
99 print_error('nopermissions', 'error', $PAGE->url
, get_string('managesubscriptions', 'calendar'));
104 FROM {event_subscriptions}
105 WHERE courseid = :courseid
106 OR (courseid = 0 AND userid = :userid)';
107 $params = array('courseid' => $courseid, 'userid' => $USER->id
);
108 $subscriptions = $DB->get_records_sql($sql, $params);
110 // Print title and header.
111 $PAGE->set_title("$course->shortname: ".get_string('calendar', 'calendar').": ".get_string('subscriptions', 'calendar'));
112 $PAGE->set_heading($course->fullname
);
113 $PAGE->set_button(calendar_preferences_button($course));
115 $renderer = $PAGE->get_renderer('core_calendar');
117 echo $OUTPUT->header();
119 // Filter subscriptions which user can't edit.
120 foreach($subscriptions as $subscription) {
121 if (!calendar_can_edit_subscription($subscription)) {
122 unset($subscriptions[$subscription->id
]);
126 // Display a table of subscriptions.
127 echo $renderer->subscription_details($courseid, $subscriptions, $importresults);
128 // Display the add subscription form.
130 echo $OUTPUT->footer();