Merge branch 'MDL-47465-master' of git://github.com/ankitagarwal/moodle
[moodle.git] / admin / tool / monitor / edit.php
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/>.
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');
27 $ruleid = optional_param('ruleid', 0, PARAM_INT);
28 $courseid = optional_param('courseid', 0, PARAM_INT);
30 // Validate course id.
31 if (empty($courseid)) {
32     require_login();
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 }
43 // Check for caps.
44 require_capability('tool/monitor:managerules', $context);
46 // Set up the page.
47 $a = new stdClass();
48 $a->coursename = $coursename;
49 $a->reportname = get_string('pluginname', 'tool_monitor');
50 $title = get_string('title', 'tool_monitor', $a);
51 $url = new moodle_url("/admin/tool/monitor/edit.php", array('courseid' => $courseid, 'ruleid' => $ruleid));
52 $manageurl = new moodle_url("/admin/tool/monitor/managerules.php", array('courseid' => $courseid));
54 $PAGE->set_url($url);
55 $PAGE->set_pagelayout('report');
56 $PAGE->set_title($title);
57 $PAGE->set_heading($title);
59 // Get data ready for mform.
60 $eventlist = tool_monitor\eventlist::get_all_eventlist(true);
61 $pluginlist = tool_monitor\eventlist::get_plugin_list();
62 $eventlist = array_merge(array('' => get_string('choosedots')), $eventlist);
63 $pluginlist = array_merge(array('' => get_string('choosedots')), $pluginlist);
65 // Set up the yui module.
66 $PAGE->requires->yui_module('moodle-tool_monitor-dropdown', 'Y.M.tool_monitor.DropDown.init',
67         array(array('eventlist' => $eventlist)));
69 // Site level report.
70 if (empty($courseid)) {
71     admin_externalpage_setup('toolmonitorrules', '', null, '', array('pagelayout' => 'report'));
72 } else {
73     // Course level report.
74     $PAGE->navigation->override_active_url($manageurl);
75 }
77 // Mform setup.
78 if (!empty($ruleid)) {
79     $rule = \tool_monitor\rule_manager::get_rule($ruleid)->get_mform_set_data();
80     $rule->minutes = $rule->timewindow / MINSECS;
81     $subscriptioncount = \tool_monitor\subscription_manager::count_rule_subscriptions($ruleid);
82 } else {
83     $rule = new stdClass();
84     $subscriptioncount = 0;
85 }
87 $mform = new tool_monitor\rule_form(null, array('eventlist' => $eventlist, 'pluginlist' => $pluginlist, 'rule' => $rule,
88         'courseid' => $courseid, 'subscriptioncount' => $subscriptioncount));
90 if ($mformdata = $mform->get_data()) {
91     $rule = \tool_monitor\rule_manager::clean_ruledata_form($mformdata);
93     if (empty($rule->id)) {
94         \tool_monitor\rule_manager::add_rule($rule);
95     } else {
96         \tool_monitor\rule_manager::update_rule($rule);
97     }
99     redirect($manageurl);
100 } else {
101     echo $OUTPUT->header();
102     $mform->set_data($rule);
103     // If there's any subscription for this rule, display an information message.
104     if ($subscriptioncount > 0) {
105         echo $OUTPUT->notification(get_string('disablefieldswarning', 'tool_monitor'), 'notifyproblem');
106     }
107     $mform->display();
108     echo $OUTPUT->footer();