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 file gives an overview of the monitors present in site.
19 *
20 * @package    tool_monitor
21 * @copyright  2014 onwards Simey Lameze <simey@moodle.com>
22 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24require(__DIR__ . '/../../../config.php');
25require_once($CFG->libdir.'/adminlib.php');
26
27$ruleid = optional_param('ruleid', 0, PARAM_INT);
28$courseid = optional_param('courseid', 0, PARAM_INT);
29
30// Validate course id.
31if (empty($courseid)) {
32    require_login(null, false);
33    $context = context_system::instance();
34    $coursename = format_string($SITE->fullname, true, array('context' => $context));
35    $PAGE->set_context($context);
36} else {
37    $course = get_course($courseid);
38    require_login($course);
39    $context = context_course::instance($course->id);
40    $coursename = format_string($course->fullname, true, array('context' => $context));
41}
42
43// Check for caps.
44require_capability('tool/monitor:managerules', $context);
45
46// Set up the page.
47$url = new moodle_url("/admin/tool/monitor/edit.php", array('courseid' => $courseid, 'ruleid' => $ruleid));
48$manageurl = new moodle_url("/admin/tool/monitor/managerules.php", array('courseid' => $courseid));
49$PAGE->set_url($url);
50$PAGE->set_pagelayout('report');
51$PAGE->set_title($coursename);
52$PAGE->set_heading($coursename);
53
54// Get data ready for mform.
55$eventlist = tool_monitor\eventlist::get_all_eventlist(true);
56$pluginlist = tool_monitor\eventlist::get_plugin_list();
57
58// Site level report.
59if (empty($courseid)) {
60    admin_externalpage_setup('toolmonitorrules', '', null, '', array('pagelayout' => 'report'));
61} else {
62    // Course level report.
63    $PAGE->navigation->override_active_url($manageurl);
64}
65
66// Mform setup.
67if (!empty($ruleid)) {
68    $rule = \tool_monitor\rule_manager::get_rule($ruleid)->get_mform_set_data();
69    $rule->minutes = $rule->timewindow / MINSECS;
70    $subscriptioncount = \tool_monitor\subscription_manager::count_rule_subscriptions($ruleid);
71
72    // Filter out events which cannot be triggered for some reason.
73    $eventlist = array_filter($eventlist, function($classname) use ($rule) {
74        // Filter out all deprecated events, except for the current one.
75        return $classname === $rule->eventname || !$classname::is_deprecated();
76    }, ARRAY_FILTER_USE_KEY);
77} else {
78    $rule = new stdClass();
79    $subscriptioncount = 0;
80
81    // Filter out events which cannot be triggered for some reason.
82    $eventlist = array_filter($eventlist, function($classname) {
83        return !$classname::is_deprecated();
84    }, ARRAY_FILTER_USE_KEY);
85}
86
87// Modify the lists to add the choosers.
88$eventlist = array_merge(array('' => get_string('choosedots')), $eventlist);
89$pluginlist = array_merge(array('' => get_string('choosedots')), $pluginlist);
90$mform = new tool_monitor\rule_form(null, array('eventlist' => $eventlist, 'pluginlist' => $pluginlist, 'rule' => $rule,
91        'courseid' => $courseid, 'subscriptioncount' => $subscriptioncount));
92
93if ($mform->is_cancelled()) {
94    redirect(new moodle_url('/admin/tool/monitor/managerules.php', array('courseid' => $courseid)));
95    exit();
96}
97
98if ($mformdata = $mform->get_data()) {
99    $rule = \tool_monitor\rule_manager::clean_ruledata_form($mformdata);
100
101    if (empty($rule->id)) {
102        \tool_monitor\rule_manager::add_rule($rule);
103    } else {
104        \tool_monitor\rule_manager::update_rule($rule);
105    }
106
107    redirect($manageurl);
108} else {
109    // Set up the yui module.
110    $PAGE->requires->yui_module('moodle-tool_monitor-dropdown', 'Y.M.tool_monitor.DropDown.init',
111            array(array('eventlist' => $eventlist)));
112
113    echo $OUTPUT->header();
114    $mform->set_data($rule);
115    // If there's any subscription for this rule, display an information message.
116    if ($subscriptioncount > 0) {
117        echo $OUTPUT->notification(get_string('disablefieldswarning', 'tool_monitor'), 'notifyproblem');
118    }
119    $mform->display();
120    echo $OUTPUT->footer();
121    exit;
122}
123
124echo $OUTPUT->header();
125if (!empty($ruleid)) {
126    echo $OUTPUT->heading(get_string('editrule', 'tool_monitor'));
127} else {
128    echo $OUTPUT->heading(get_string('addrule', 'tool_monitor'));
129}
130$mform->set_data($rule);
131$mform->display();
132echo $OUTPUT->footer();
133