MDL-45758 tool_monitor: Add support for editing/creating a rule

Original issue - MDL-45938
This commit is contained in:
Ankit Agarwal
2014-10-15 07:41:04 +05:30
parent 2fd010b886
commit 58097ddf9b
5 changed files with 183 additions and 90 deletions
+1 -1
View File
@@ -117,7 +117,7 @@ class rule {
}
}
}
$url = new \moodle_url($CFG->wwwroot. '/tool/monitor/index.php', array('id' => $courseid, 'ruleid' => $this->id,
$url = new \moodle_url($CFG->wwwroot. '/admin/tool/monitor/index.php', array('courseid' => $courseid, 'ruleid' => $this->id,
'action' => 'subscribe'));
return new \single_select($url, 'cmid', $options, '', $nothing = array('' => 'choosedots'));
}
+119 -43
View File
@@ -1,62 +1,132 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* The mform for creating and editing a rule
* The mform for creating and editing a rule.
*
* @copyright 2014 onwards Simey Lameze <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package tool_monitor
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package tool_monitor
*/
namespace tool_monitor;
require_once($CFG->dirroot.'/lib/formslib.php');
/**
* The mform for creating and editing a rule.
*
* @since Moodle 2.8
* @copyright 2014 onwards Simey Lameze <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package tool_monitor
*/
class rule_form extends \moodleform {
function definition () {
global $CFG, $USER, $OUTPUT;
/**
* Mform class definition
*
*/
public function definition () {
$mform = $this->_form;
$eventlist = $this->_customdata['eventlist'];
$pluginlist = $this->_customdata['pluginlist'];
$rule = $this->_customdata['rule'];
$courseid = $this->_customdata['courseid'];
$eventlist = array_merge(array('' => get_string('choosedots')), $eventlist);
$pluginlist = array_merge(array('' => get_string('choosedots')), $pluginlist);
// General section header
// General section header.
$mform->addElement('header', 'general', get_string('general'));
// Hidden rule ID
$mform->addElement('hidden', 'ruleid');
$mform->setType('ruleid', PARAM_INT);
$mform->setDefault('ruleid', '');
// Hidden course ID
// Hidden course ID.
$mform->addElement('hidden', 'courseid');
$mform->setType('courseid', PARAM_INT);
$mform->setDefault('courseid', '');
// Name field
$mform->addElement('text', 'name', get_string('name','report_monitor'), 'size="50"');
// We are editing a existing rule.
if (!empty($rule->id)) {
// Hidden rule id.
$mform->addElement('hidden', 'ruleid');
$mform->setType('ruleid', PARAM_INT);
$mform->setConstant('ruleid', $rule->id);
// Force course id.
$courseid = $rule->courseid;
}
// Make course id a constant.
$mform->setConstant('courseid', $courseid);
if (empty($courseid)) {
$context = \context_system::instance();
} else {
$context = \context_course::instance($courseid);
}
$editoroptions = array(
'subdirs' => 0,
'maxbytes' => 0,
'maxfiles' => 0,
'changeformat' => 0,
'context' => $context,
'noclean' => 0,
'trusttext' => 0
);
// Name field.
$mform->addElement('text', 'name', get_string('name', 'tool_monitor'), 'size="50"');
$mform->addRule('name', get_string('required'), 'required');
$mform->setType('name', PARAM_TEXT);
$mform->addHelpButton('name', 'name', 'report_monitor');
// Plugin field
$mform->addElement('select', 'plugin', get_string('plugin', 'report_monitor'), $pluginslist);
$mform->addHelpButton('name', 'name', 'tool_monitor');
// Plugin field.
$mform->addElement('select', 'plugin', get_string('selectplugin', 'tool_monitor'), $pluginlist);
$mform->addRule('plugin', get_string('required'), 'required');
$mform->addHelpButton('plugin', 'plugin', 'report_monitor');
// Event field
$mform->addElement('select', 'event', get_string('event', 'report_monitor'), $eventoption);
$mform->addRule('event', get_string('required'), 'required');
$mform->addHelpButton('event', 'event', 'report_monitor');
// Description field
$mform->addElement('editor', 'description', get_string('description', 'report_monitor'));
$mform->addHelpButton('description', 'description', 'report_monitor');
// Customize your trigger section
$mform->addElement('header', 'customize', get_string('customize', 'report_monitor'));
// Call the filters
$filter = new filter_manager();
foreach ($filter->get_filters() as $filterobj) {
$filterobj->add_form_elements($mform);
}
// Customize your trigger message section
$mform->addElement('header', 'message', get_string('message_header', 'report_monitor'));
// Message template field
$mform->addElement('editor', 'message_template', get_string('message_template', 'report_monitor'));
$mform->setDefault('message_template', get_string('defaultmessagetpl', 'report_monitor'));
$mform->addRule('message_template', get_string('required'), 'required');
$mform->addHelpButton('message_template', 'message_template', 'report_monitor');
// Submit button
$mform->addHelpButton('plugin', 'selectplugin', 'tool_monitor');
// Event field.
$mform->addElement('select', 'eventname', get_string('selectevent', 'tool_monitor'), $eventlist);
$mform->addRule('eventname', get_string('required'), 'required');
$mform->addHelpButton('eventname', 'selectevent', 'tool_monitor');
// Description field.
$mform->addElement('editor', 'description', get_string('description', 'tool_monitor'), $editoroptions);
$mform->addHelpButton('description', 'description', 'tool_monitor');
// Filters.
$mform->addElement('header', 'customizefilters', get_string('customizefilters', 'tool_monitor'));
$freq = array(1 => 1, 5 => 5, 10 => 10, 20 => 20, 30 => 30, 40 => 40, 50 => 50, 60 => 60, 70 => 70, 80 => 80, 90 => 90,
100 => 100, 1000 => 1000);
$mform->addElement('select', 'frequency', get_string('selectfrequency', 'tool_monitor'), $freq);
$mform->addRule('frequency', get_string('required'), 'required');
$mform->addHelpButton('frequency', 'selectfrequency', 'tool_monitor');
$mins = array(1 => 1, 5 => 5, 10 => 10, 15 => 15, 20 => 20, 25 => 25, 30 => 30, 35 => 35, 40 => 40, 45 => 45, 50 => 50,
55 => 55, 60 => 60);
$mform->addElement('select', 'minutes', get_string('selectminutes', 'tool_monitor'), $mins);
$mform->addRule('minutes', get_string('required'), 'required');
// Message template.
$mform->addElement('header', 'customizemessage', get_string('customizemessage', 'tool_monitor'));
$mform->addElement('editor', 'template', get_string('messagetemplate', 'tool_monitor'), $editoroptions);
$mform->setDefault('template', get_string('defaultmessagetpl', 'tool_monitor'));
$mform->addRule('template', get_string('required'), 'required');
$mform->addHelpButton('template', 'messagetemplate', 'tool_monitor');
// Action buttons.
$this->add_action_buttons(false, get_string('savechanges'));
}
@@ -65,10 +135,16 @@ class rule_form extends \moodleform {
*
* @param array $data data from the form.
* @param array $files files uploaded.
*
* @return array of errors.
*/
function validation($data, $files) {
public function validation($data, $files) {
$errors = parent::validation($data, $files);
if (!eventlist::validate_event_plugin($data['plugin'], $data['eventname'])) {
$errors['eventname'] = get_string('errorincorrectevent', 'tool_monitor');
}
return $errors;
}
}
?>
}
+34 -46
View File
@@ -21,17 +21,13 @@
* @copyright 2014 onwards Simey Lameze <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require('../../config.php');
require(__DIR__ . '/../../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->dirroot.'/course/lib.php');
require_once('locallib.php');
$ruleid = optional_param('ruleid', 0, PARAM_INT);
$courseid = optional_param('id', 0, PARAM_INT);
$courseid = optional_param('courseid', 0, PARAM_INT);
$ruledata = new \stdClass();
// Validate course id
// Validate course id.
if (empty($courseid)) {
require_login();
$context = context_system::instance();
@@ -39,71 +35,63 @@ if (empty($courseid)) {
$PAGE->set_context($context);
} else {
$course = get_course($courseid);
$ruledata->courseid = $course->id;
require_login($course);
$context = context_course::instance($course->id);
$coursename = format_string($course->fullname, true, array('context' => $context));
}
// Check for caps.
require_capability('tool/monitor:managerules', $context);
// Get rule data to edit form
if ($ruleid) {
$rule = \tool_monitor\rule_manager::get_rule($ruleid);
$ruledata->ruleid = $rule->id;
$ruledata->courseid = $rule->courseid;
$ruledata->name = $rule->name;
$ruledata->plugin = $rule->plugin;
$ruledata->event = $rule->event;
$ruledata->description['text'] = $rule->description;
$ruledata->rule['frequency'] = $rule->frequency;
$ruledata->rule['minutes'] = $rule->minutes;
$ruledata->message_template['text'] = $rule->message_template;
}
// Set up the page.
$a = new stdClass();
$a->coursename = $coursename;
$a->reportname = get_string('pluginname', 'tool_monitor');
$title = get_string('title', 'tool_monitor', $a);
$url = new moodle_url("/admin/tool/monitor/edit.php", array('id' => $courseid));
$indexurl = new moodle_url("/admin/tool/monitor/index.php", array('id' => $courseid));
$url = new moodle_url("/admin/tool/monitor/edit.php", array('courseid' => $courseid, 'ruleid' => $ruleid));
$manageurl = new moodle_url("/admin/tool/monitor/managerules.php", array('courseid' => $courseid));
$PAGE->set_url($url);
$PAGE->set_pagelayout('report');
$PAGE->set_title($title);
$PAGE->set_heading($title);
$PAGE->requires->js('/tool/monitor/event.js');
$PAGE->requires->yui_module('moodle-tool_monitor-dropdown', 'Y.M.tool_monitor.DropDown.init');
// Site level report.
if (empty($courseid)) {
admin_externalpage_setup('toolmonitorrules', '', null, '', array('pagelayout' => 'report'));
} else {
// Course level report.
$PAGE->navigation->override_active_url($manageurl);
}
$mform = new tool_monitor\rule_form();
if ($mformdata = $mform->get_data()) {
$ruledata = new \stdClass();
$ruledata->courseid = $mformdata->courseid;
$ruledata->name = $mformdata->name;
$ruledata->plugin = $mformdata->plugin;
$ruledata->event = $mformdata->event;
$ruledata->description = $mformdata->description['text'];
$ruledata->frequency = $mformdata->rule['frequency'];
$ruledata->minutes = $mformdata->rule['minutes'];
$ruledata->message_template = $mformdata->message_template['text'];
// Get data ready for mform.
$eventlist = tool_monitor\eventlist::get_all_eventlist(true);
$pluginlist = tool_monitor\eventlist::get_plugin_list();
if (!empty($ruleid)) {
$rule = \tool_monitor\rule_manager::get_rule($ruleid)->get_mform_set_data();
$rule->minutes = $rule->timewindow / MINSECS;
} else {
$rule = new stdClass();
}
if (empty($mformdata->ruleid)) {
\tool_monitor\rule_manager::add_rule($ruledata);
$mform = new tool_monitor\rule_form(null, array('eventlist' => $eventlist, 'pluginlist' => $pluginlist, 'rule' => $rule,
'courseid' => $courseid));
if ($mformdata = $mform->get_data()) {
$rule = \tool_monitor\rule_manager::clean_ruledata_form($mformdata);
if (empty($rule->id)) {
\tool_monitor\rule_manager::add_rule($rule);
} else {
$ruledata->id = $mformdata->ruleid;
\tool_monitor\rule_manager::update_rule($ruledata);
\tool_monitor\rule_manager::update_rule($rule);
}
$courseid = $mformdata->courseid;
$url = new moodle_url("/admin/tool/monitor/managerules.php", array('id' => $courseid));
redirect($url);
redirect($manageurl);
} else {
echo $OUTPUT->header();
$mform->set_data($ruledata);
$mform->set_data($rule);
$mform->display();
echo $OUTPUT->footer();
}
echo $OUTPUT->footer();
@@ -27,11 +27,40 @@
$string['allevents'] = 'All events';
$string['allmodules'] = 'All modules';
$string['core'] = 'Core';
$string['customizefilters'] = 'Select the frequency of the events';
$string['customizemessage'] = 'Cutomize the notification message';
$string['description'] = 'Description:';
$string['description_help'] = "Description is displayed to users when they want to subscribe to this rule. This helps them understand what the rule is about.";
$string['defaultmessagetpl'] = 'Rule "{rulename}" has happened. You can find further details at {link}';
$string['eventnotfound'] = 'Event not found';
$string['errorincorrectevent'] = 'Please select an event related to the selected plugin';
$string['freqdesc'] = '{$a->freq} times in {$a->mins} minutes';
$string['managesubscriptions'] = 'Manage subscriptions';
$string['managerules'] = 'Manage rules';
$string['messageheader'] = 'Customize your notification message';
$string['messagetemplate'] = 'Message template';
$string['messagetemplate_help'] = 'This is the content of the message that will be sent to users, when the given conditions of the rule are met. You are allowed to use following templates in this.
<br /> {link} - Link to the location where the event happened.
<br /> {modulelink} - Link to the module where the event has happened.
<br /> {rulename} - Name of this rule.
<br /> {description} - Rule description.
<br /> {eventname} - Name of the event associated with the rule.';
$string['minutes'] = 'in minutes:';
$string['name'] = 'Name of the rule: ';
$string['name_help'] = "Choose a name for the rule.";
$string['norules'] = 'There are no rules you can subscribe to.';
$string['manageruleslink'] = 'You can manage rules from {$a} page.';
$string['pluginname'] = 'Event monitor';
$string['processevents'] = 'Process events';
$string['selectcourse'] = 'Visit this report at course level to get a list of possible modules';
$string['selectevent'] = 'Select an event:';
$string['selectevent_help'] = "Select an event to monitor.";
$string['selectfrequency'] = 'Frequency of events:';
$string['selectfrequency_help'] = "Frequency defines the denisty of the event occurance. Select criterias to define how frequently the event should happen to trigger the notification.";
$string['selectminutes'] = 'in minutes:';
$string['selectplugin'] = 'Select the plugin type:';
$string['selectplugin_help'] = "Select a plugin that you are interested in monitoring.";
$string['title'] = '{$a->coursename} : {$a->reportname}';
$string['tool/monitor:managerules'] = 'Manage event monitor rules';
$string['tool/monitor:subscribe'] = 'Subscribe to event monitor rules';
View File