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_ALPHA
);
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('standard');
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);
78 $importresults = calendar_update_subscription_events($subscriptionid);
80 // Redirect to prevent refresh issues.
82 } else if (!empty($subscriptionid)) {
83 // The user is wanting to perform an action upon an existing subscription.
84 require_sesskey(); // Must have sesskey for all actions.
85 $importresults = calendar_process_subscription_row($subscriptionid, $pollinterval, $action);
89 FROM {event_subscriptions}
90 WHERE courseid = :courseid
91 OR (courseid = 0 AND userid = :userid)';
92 $params = array('courseid' => $courseid, 'userid' => $USER->id
);
93 $subscriptions = $DB->get_records_sql($sql, $params);
95 // Print title and header.
96 $PAGE->set_title("$course->shortname: ".get_string('calendar', 'calendar').": ".get_string('subscriptions', 'calendar'));
97 $PAGE->set_heading($course->fullname
);
98 $PAGE->set_button(calendar_preferences_button($course));
100 $renderer = $PAGE->get_renderer('core_calendar');
102 echo $OUTPUT->header();
103 // Display a table of subscriptions.
104 echo $renderer->subscription_details($courseid, $subscriptions, $importresults);
105 // Display the add subscription form.
107 echo $OUTPUT->footer();