1<?php
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17/**
18 * This page lets users to manage rules for a given course.
19 *
20 * @package    tool_monitor
21 * @copyright  2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
22 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25require_once(__DIR__ . '/../../../config.php');
26require_once($CFG->libdir.'/adminlib.php');
27require_once($CFG->dirroot.'/'.$CFG->admin.'/tool/monitor/lib.php');
28
29$courseid = optional_param('courseid', 0, PARAM_INT);
30$action = optional_param('action', '', PARAM_ALPHA);
31$cmid = optional_param('cmid', 0, PARAM_INT);
32$ruleid = optional_param('ruleid', 0, PARAM_INT);
33$subscriptionid = optional_param('subscriptionid', 0, PARAM_INT);
34$confirm = optional_param('confirm', false, PARAM_BOOL);
35
36$choose = false;
37// Validate course id.
38if (empty($courseid)) {
39    require_login(null, false);
40    $context = context_system::instance();
41    // check system level capability.
42    if (!has_capability('tool/monitor:subscribe', $context)) {
43        // If not system level then check to see if they have access to any course level rules.
44        if (tool_monitor_can_subscribe()) {
45            // Make them choose a course.
46            $choose = true;
47        } else {
48            // return error.
49            print_error('rulenopermission', 'tool_monitor');
50        }
51    }
52} else {
53    // They might want to see rules for this course.
54    $course = get_course($courseid);
55    require_login($course);
56    $context = context_course::instance($course->id);
57    // Check for caps.
58    require_capability('tool/monitor:subscribe', $context);
59}
60
61if (!get_config('tool_monitor', 'enablemonitor')) {
62    // This should never happen as the this page does not appear in navigation when the tool is disabled.
63    throw new coding_exception('Event monitoring is disabled');
64}
65
66// Use the user context here so that the header shows user information.
67$PAGE->set_context(context_user::instance($USER->id));
68
69// Set up the page.
70$indexurl = new moodle_url('/admin/tool/monitor/index.php', array('courseid' => $courseid));
71$PAGE->set_url($indexurl);
72$PAGE->set_pagelayout('report');
73$PAGE->set_title(get_string('managesubscriptions', 'tool_monitor'));
74$PAGE->set_heading(fullname($USER));
75$settingsnode = $PAGE->settingsnav->find('monitor', null);
76if ($settingsnode) {
77    $settingsnode->make_active();
78}
79
80// Create/delete subscription if needed.
81if (!empty($action)) {
82    require_sesskey();
83    switch ($action) {
84        case 'subscribe' :
85            $rule = \tool_monitor\rule_manager::get_rule($ruleid);
86            $rule->subscribe_user($courseid, $cmid);
87            echo $OUTPUT->header();
88            echo $OUTPUT->notification(get_string('subcreatesuccess', 'tool_monitor'), 'notifysuccess');
89            break;
90        case 'unsubscribe' :
91            // If the subscription does not exist, then redirect back as the subscription must have already been deleted.
92            if (!$subscription = $DB->record_exists('tool_monitor_subscriptions', array('id' => $subscriptionid))) {
93                redirect(new moodle_url('/admin/tool/monitor/index.php', array('courseid' => $courseid)));
94            }
95
96            // Set the URLs.
97            $confirmurl = new moodle_url('/admin/tool/monitor/index.php', array('subscriptionid' => $subscriptionid,
98                'courseid' => $courseid, 'action' => 'unsubscribe', 'confirm' => true,
99                'sesskey' => sesskey()));
100            $cancelurl = new moodle_url('/admin/tool/monitor/index.php', array('subscriptionid' => $subscriptionid,
101                'courseid' => $courseid, 'sesskey' => sesskey()));
102            if ($confirm) {
103                \tool_monitor\subscription_manager::delete_subscription($subscriptionid);
104                echo $OUTPUT->header();
105                echo $OUTPUT->notification(get_string('subdeletesuccess', 'tool_monitor'), 'notifysuccess');
106            } else {
107                $subscription = \tool_monitor\subscription_manager::get_subscription($subscriptionid);
108                echo $OUTPUT->header();
109                echo $OUTPUT->confirm(get_string('subareyousure', 'tool_monitor', $subscription->get_name($context)),
110                    $confirmurl, $cancelurl);
111                echo $OUTPUT->footer();
112                exit();
113            }
114            break;
115        default:
116    }
117} else {
118    echo $OUTPUT->header();
119}
120
121$renderer = $PAGE->get_renderer('tool_monitor', 'managesubs');
122
123// Render the course selector.
124$totalrules = \tool_monitor\rule_manager::count_rules_by_courseid($courseid);
125$rules = new \tool_monitor\output\managesubs\rules('toolmonitorrules', $indexurl, $courseid);
126
127$usercourses = $rules->get_user_courses_select($choose);
128// There must be user courses otherwise we wouldn't make it this far.
129echo $renderer->render($usercourses);
130
131// Render the current subscriptions list.
132$totalsubs = \tool_monitor\subscription_manager::count_user_subscriptions();
133if (!empty($totalsubs) && !$choose) {
134    // Show the subscriptions section only if there are subscriptions.
135    $subs = new \tool_monitor\output\managesubs\subs('toolmonitorsubs', $indexurl, $courseid);
136    echo $OUTPUT->heading(get_string('currentsubscriptions', 'tool_monitor'), 3);
137    echo $renderer->render($subs);
138}
139
140// Render the potential rules list.
141if (!$choose) {
142    echo $OUTPUT->heading(get_string('rulescansubscribe', 'tool_monitor'), 3);
143    echo $renderer->render($rules);
144}
145
146// Check if the user can manage the course rules we are viewing.
147$canmanagerules = has_capability('tool/monitor:managerules', $context);
148
149if (empty($totalrules)) {
150    // No rules present. Show a link to manage rules page if permissions permit.
151    echo html_writer::start_div();
152    echo html_writer::tag('span', get_string('norules', 'tool_monitor'));
153    if ($canmanagerules) {
154        $manageurl = new moodle_url("/admin/tool/monitor/managerules.php", array('courseid' => $courseid));
155        $a = html_writer::link($manageurl, get_string('managerules', 'tool_monitor'));
156        $link = "&nbsp;";
157        $link .= html_writer::tag('span', get_string('manageruleslink', 'tool_monitor', $a));
158        echo $link;
159    }
160    echo html_writer::end_div();
161} else if ($canmanagerules) {
162    $manageurl = new moodle_url("/admin/tool/monitor/managerules.php", array('courseid' => $courseid));
163    echo $renderer->render_rules_link($manageurl);
164}
165echo $OUTPUT->footer();
166