MDL-45758 tool_monitor: Initial version of rule form.
authorSimey Lameze <simey@moodle.com>
Fri, 4 Jul 2014 02:28:27 +0000 (10:28 +0800)
committerAnkit Agarwal <ankit@moodle.com>
Wed, 15 Oct 2014 02:11:04 +0000 (07:41 +0530)
Original issue - MDL-45938

admin/tool/monitor/classes/rule_form.php [new file with mode: 0644]
admin/tool/monitor/edit.php [new file with mode: 0644]

diff --git a/admin/tool/monitor/classes/rule_form.php b/admin/tool/monitor/classes/rule_form.php
new file mode 100644 (file)
index 0000000..95aab3e
--- /dev/null
@@ -0,0 +1,74 @@
+<?php
+/**
+ * The mform for creating and editing a rule
+ *
+ * @copyright 2014 onwards Simey Lameze <lameze@gmail.com>
+ * @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');
+
+class rule_form extends \moodleform {
+
+    function definition () {
+        global $CFG, $USER, $OUTPUT;
+        $mform = $this->_form;
+
+        // 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
+        $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"');
+        $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->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
+        $this->add_action_buttons(false, get_string('savechanges'));
+    }
+
+    /**
+     * Form validation
+     *
+     * @param array $data data from the form.
+     * @param array $files files uploaded.
+     * @return array of errors.
+     */
+    function validation($data, $files) {
+
+    }
+}
+?>
\ No newline at end of file
diff --git a/admin/tool/monitor/edit.php b/admin/tool/monitor/edit.php
new file mode 100644 (file)
index 0000000..558821c
--- /dev/null
@@ -0,0 +1,109 @@
+<?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/>.
+
+/**
+ * This file gives an overview of the monitors present in site.
+ *
+ * @package    tool_monitor
+ * @copyright  2014 onwards Simey Lameze <simey@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+require('../../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);
+
+$ruledata = new \stdClass();
+
+// Validate course id
+if (empty($courseid)) {
+    require_login();
+    $context = context_system::instance();
+    $coursename = format_string($SITE->fullname, true, array('context' => $context));
+    $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));
+}
+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));
+
+$PAGE->set_url($url);
+$PAGE->set_pagelayout('report');
+$PAGE->set_title($title);
+$PAGE->set_heading($title);
+$PAGE->requires->js('/tool/monitor/event.js');
+
+// Site level report.
+if (empty($courseid)) {
+    admin_externalpage_setup('toolmonitorrules', '', null, '', array('pagelayout' => 'report'));
+}
+
+$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'];
+
+    if (empty($mformdata->ruleid)) {
+        \tool_monitor\rule_manager::add_rule($ruledata);
+    } else {
+        $ruledata->id = $mformdata->ruleid;
+        \tool_monitor\rule_manager::update_rule($ruledata);
+    }
+    $courseid = $mformdata->courseid;
+    $url = new moodle_url("/admin/tool/monitor/managerules.php", array('id' => $courseid));
+    redirect($url);
+} else {
+    echo $OUTPUT->header();
+    $mform->set_data($ruledata);
+    $mform->display();
+}
+echo $OUTPUT->footer();
\ No newline at end of file