source: moodle/trunk/fuentes/admin/tool/monitor/index.php @ 136

Last change on this file since 136 was 136, checked in by mabarracus, 4 years ago

Ported code to xenial

File size: 6.2 KB
Line 
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');
27
28$courseid = optional_param('courseid', 0, PARAM_INT);
29$action = optional_param('action', '', PARAM_ALPHA);
30$cmid = optional_param('cmid', 0, PARAM_INT);
31$ruleid = optional_param('ruleid', 0, PARAM_INT);
32$subscriptionid = optional_param('subscriptionid', 0, PARAM_INT);
33$confirm = optional_param('confirm', false, PARAM_BOOL);
34
35// Validate course id.
36if (empty($courseid)) {
37    require_login();
38} else {
39    // They might want to see rules for this course.
40    $course = get_course($courseid);
41    require_login($course);
42    $coursecontext = context_course::instance($course->id);
43    // Check for caps.
44    require_capability('tool/monitor:subscribe', $coursecontext);
45    $coursename = format_string($course->fullname, true, array('context' => $coursecontext));
46}
47
48if (!get_config('tool_monitor', 'enablemonitor')) {
49    // This should never happen as the this page does not appear in navigation when the tool is disabled.
50    throw new coding_exception('Event monitoring is disabled');
51}
52
53// Always build the page in site context.
54$context = context_system::instance();
55$sitename = format_string($SITE->fullname, true, array('context' => $context));
56$PAGE->set_context($context);
57
58// Set up the page.
59$indexurl = new moodle_url('/admin/tool/monitor/index.php', array('courseid' => $courseid));
60$PAGE->set_url($indexurl);
61$PAGE->set_pagelayout('report');
62$PAGE->set_title($sitename);
63$PAGE->set_heading($sitename);
64
65// Create/delete subscription if needed.
66if (!empty($action)) {
67    require_sesskey();
68    switch ($action) {
69        case 'subscribe' :
70            $rule = \tool_monitor\rule_manager::get_rule($ruleid);
71            $rule->subscribe_user($courseid, $cmid);
72            echo $OUTPUT->header();
73            echo $OUTPUT->notification(get_string('subcreatesuccess', 'tool_monitor'), 'notifysuccess');
74            break;
75        case 'unsubscribe' :
76            // If the subscription does not exist, then redirect back as the subscription must have already been deleted.
77            if (!$subscription = $DB->record_exists('tool_monitor_subscriptions', array('id' => $subscriptionid))) {
78                redirect(new moodle_url('/admin/tool/monitor/index.php', array('courseid' => $courseid)));
79            }
80
81            // Set the URLs.
82            $confirmurl = new moodle_url('/admin/tool/monitor/index.php', array('subscriptionid' => $subscriptionid,
83                'courseid' => $courseid, 'action' => 'unsubscribe', 'confirm' => true,
84                'sesskey' => sesskey()));
85            $cancelurl = new moodle_url('/admin/tool/monitor/index.php', array('subscriptionid' => $subscriptionid,
86                'courseid' => $courseid, 'sesskey' => sesskey()));
87            if ($confirm) {
88                \tool_monitor\subscription_manager::delete_subscription($subscriptionid);
89                echo $OUTPUT->header();
90                echo $OUTPUT->notification(get_string('subdeletesuccess', 'tool_monitor'), 'notifysuccess');
91            } else {
92                $subscription = \tool_monitor\subscription_manager::get_subscription($subscriptionid);
93                echo $OUTPUT->header();
94                echo $OUTPUT->confirm(get_string('subareyousure', 'tool_monitor', $subscription->get_name($context)),
95                    $confirmurl, $cancelurl);
96                echo $OUTPUT->footer();
97                exit();
98            }
99            break;
100        default:
101    }
102} else {
103    echo $OUTPUT->header();
104}
105
106// Render the current subscriptions list.
107$totalsubs = \tool_monitor\subscription_manager::count_user_subscriptions();
108$renderer = $PAGE->get_renderer('tool_monitor', 'managesubs');
109if (!empty($totalsubs)) {
110    // Show the subscriptions section only if there are subscriptions.
111    $subs = new \tool_monitor\output\managesubs\subs('toolmonitorsubs', $indexurl, $courseid);
112    echo $OUTPUT->heading(get_string('currentsubscriptions', 'tool_monitor'), 3);
113    echo $renderer->render($subs);
114}
115
116// Render the potential rules list.
117$totalrules = \tool_monitor\rule_manager::count_rules_by_courseid($courseid);
118echo $OUTPUT->heading(get_string('rulescansubscribe', 'tool_monitor'), 3);
119$rules = new \tool_monitor\output\managesubs\rules('toolmonitorrules', $indexurl, $courseid);
120echo $renderer->render($rules);
121
122// Check if the user can manage the course rules we are viewing.
123if (empty($courseid)) {
124    $canmanagerules = has_capability('tool/monitor:managerules', $context);
125} else {
126    $canmanagerules = has_capability('tool/monitor:managerules', $coursecontext);
127}
128if (empty($totalrules)) {
129    // No rules present. Show a link to manage rules page if permissions permit.
130    echo html_writer::start_div();
131    echo html_writer::tag('span', get_string('norules', 'tool_monitor'));
132    if ($canmanagerules) {
133        $manageurl = new moodle_url("/admin/tool/monitor/managerules.php", array('courseid' => $courseid));
134        $a = html_writer::link($manageurl, get_string('managerules', 'tool_monitor'));
135        $link = "&nbsp;";
136        $link .= html_writer::tag('span', get_string('manageruleslink', 'tool_monitor', $a));
137        echo $link;
138    }
139    echo html_writer::end_div();
140} else if ($canmanagerules) {
141    $manageurl = new moodle_url("/admin/tool/monitor/managerules.php", array('courseid' => $courseid));
142    echo $renderer->render_rules_link($manageurl);
143}
144echo $OUTPUT->footer();
Note: See TracBrowser for help on using the repository browser.