| 1 | efrain | 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 |  */
 | 
        
           |  |  | 24 | require(__DIR__ . '/../../../config.php');
 | 
        
           |  |  | 25 | require_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.
 | 
        
           |  |  | 31 | if (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 |     $PAGE->set_primary_active_tab('siteadminnode');
 | 
        
           |  |  | 37 |     $PAGE->set_secondary_active_tab('reports');
 | 
        
           |  |  | 38 | } else {
 | 
        
           |  |  | 39 |     $course = get_course($courseid);
 | 
        
           |  |  | 40 |     require_login($course);
 | 
        
           |  |  | 41 |     $context = context_course::instance($course->id);
 | 
        
           |  |  | 42 |     $coursename = format_string($course->fullname, true, array('context' => $context));
 | 
        
           |  |  | 43 | }
 | 
        
           |  |  | 44 |   | 
        
           |  |  | 45 | // Check for caps.
 | 
        
           |  |  | 46 | require_capability('tool/monitor:managerules', $context);
 | 
        
           |  |  | 47 |   | 
        
           |  |  | 48 | // Set up the page.
 | 
        
           |  |  | 49 | $url = new moodle_url("/admin/tool/monitor/edit.php", array('courseid' => $courseid, 'ruleid' => $ruleid));
 | 
        
           |  |  | 50 | $manageurl = new moodle_url("/admin/tool/monitor/managerules.php", array('courseid' => $courseid));
 | 
        
           |  |  | 51 | $PAGE->set_url($url);
 | 
        
           |  |  | 52 | $PAGE->set_pagelayout('report');
 | 
        
           |  |  | 53 | $PAGE->set_title($coursename);
 | 
        
           |  |  | 54 | $PAGE->set_heading($coursename);
 | 
        
           |  |  | 55 |   | 
        
           |  |  | 56 | // Get data ready for mform.
 | 
        
           |  |  | 57 | $eventlist = tool_monitor\eventlist::get_all_eventlist(true);
 | 
        
           |  |  | 58 | $pluginlist = tool_monitor\eventlist::get_plugin_list();
 | 
        
           |  |  | 59 |   | 
        
           |  |  | 60 | // Site level report.
 | 
        
           |  |  | 61 | if (empty($courseid)) {
 | 
        
           |  |  | 62 |     admin_externalpage_setup('toolmonitorrules', '', null, '', array('pagelayout' => 'report'));
 | 
        
           |  |  | 63 | } else {
 | 
        
           |  |  | 64 |     // Course level report.
 | 
        
           |  |  | 65 |     $PAGE->navigation->override_active_url($manageurl);
 | 
        
           |  |  | 66 | }
 | 
        
           |  |  | 67 |   | 
        
           |  |  | 68 | // Mform setup.
 | 
        
           |  |  | 69 | if (!empty($ruleid)) {
 | 
        
           |  |  | 70 |     $PAGE->navbar->add(get_string('editrule', 'tool_monitor'), $PAGE->url);
 | 
        
           |  |  | 71 |     $rule = \tool_monitor\rule_manager::get_rule($ruleid)->get_mform_set_data();
 | 
        
           |  |  | 72 |     $rule->minutes = $rule->timewindow / MINSECS;
 | 
        
           |  |  | 73 |     $subscriptioncount = \tool_monitor\subscription_manager::count_rule_subscriptions($ruleid);
 | 
        
           |  |  | 74 |   | 
        
           |  |  | 75 |     // Filter out events which cannot be triggered for some reason.
 | 
        
           |  |  | 76 |     $eventlist = array_filter($eventlist, function($classname) use ($rule) {
 | 
        
           |  |  | 77 |         // Filter out all deprecated events, except for the current one.
 | 
        
           |  |  | 78 |         return $classname === $rule->eventname || !$classname::is_deprecated();
 | 
        
           |  |  | 79 |     }, ARRAY_FILTER_USE_KEY);
 | 
        
           |  |  | 80 | } else {
 | 
        
           |  |  | 81 |     $PAGE->navbar->add(get_string('addrule', 'tool_monitor'), $PAGE->url);
 | 
        
           |  |  | 82 |     $rule = new stdClass();
 | 
        
           |  |  | 83 |     $subscriptioncount = 0;
 | 
        
           |  |  | 84 |   | 
        
           |  |  | 85 |     // Filter out events which cannot be triggered for some reason.
 | 
        
           |  |  | 86 |     $eventlist = array_filter($eventlist, function($classname) {
 | 
        
           |  |  | 87 |         return !$classname::is_deprecated();
 | 
        
           |  |  | 88 |     }, ARRAY_FILTER_USE_KEY);
 | 
        
           |  |  | 89 | }
 | 
        
           |  |  | 90 |   | 
        
           |  |  | 91 | // Modify the lists to add the choosers.
 | 
        
           |  |  | 92 | $eventlist = array_merge(array('' => get_string('choosedots')), $eventlist);
 | 
        
           |  |  | 93 | $pluginlist = array_merge(['' => [0 => get_string('choosedots')]], $pluginlist);
 | 
        
           |  |  | 94 | $mform = new tool_monitor\rule_form(null, array('eventlist' => $eventlist, 'pluginlist' => $pluginlist, 'rule' => $rule,
 | 
        
           |  |  | 95 |         'courseid' => $courseid, 'subscriptioncount' => $subscriptioncount));
 | 
        
           |  |  | 96 |   | 
        
           |  |  | 97 | if ($mform->is_cancelled()) {
 | 
        
           |  |  | 98 |     redirect(new moodle_url('/admin/tool/monitor/managerules.php', array('courseid' => $courseid)));
 | 
        
           |  |  | 99 |     exit();
 | 
        
           |  |  | 100 | }
 | 
        
           |  |  | 101 |   | 
        
           |  |  | 102 | if ($mformdata = $mform->get_data()) {
 | 
        
           |  |  | 103 |     $rule = \tool_monitor\rule_manager::clean_ruledata_form($mformdata);
 | 
        
           |  |  | 104 |   | 
        
           |  |  | 105 |     if (empty($rule->id)) {
 | 
        
           |  |  | 106 |         \tool_monitor\rule_manager::add_rule($rule);
 | 
        
           |  |  | 107 |     } else {
 | 
        
           |  |  | 108 |         \tool_monitor\rule_manager::update_rule($rule);
 | 
        
           |  |  | 109 |     }
 | 
        
           |  |  | 110 |   | 
        
           |  |  | 111 |     redirect($manageurl);
 | 
        
           |  |  | 112 | } else {
 | 
        
           |  |  | 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 |   | 
        
           |  |  | 124 | echo $OUTPUT->header();
 | 
        
           |  |  | 125 | if (!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();
 | 
        
           |  |  | 132 | echo $OUTPUT->footer();
 |