Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

   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   * The mform for creating and editing a rule.
  19   *
  20   * @copyright 2014 onwards Simey Lameze <lameze@gmail.com>
  21   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   * @package   tool_monitor
  23   */
  24  
  25  namespace tool_monitor;
  26  
  27  require_once($CFG->dirroot.'/lib/formslib.php');
  28  
  29  /**
  30   * The mform for creating and editing a rule.
  31   *
  32   * @since     Moodle 2.8
  33   * @copyright 2014 onwards Simey Lameze <lameze@gmail.com>
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   * @package   tool_monitor
  36   */
  37  class rule_form extends \moodleform {
  38  
  39      /**
  40       * Mform class definition
  41       *
  42       */
  43      public function definition () {
  44          global $PAGE;
  45  
  46          $mform = $this->_form;
  47          $eventlist = $this->_customdata['eventlist'];
  48          $pluginlist = $this->_customdata['pluginlist'];
  49          $rule = $this->_customdata['rule'];
  50          $courseid = $this->_customdata['courseid'];
  51          $subscriptioncount = $this->_customdata['subscriptioncount'];
  52  
  53          // General section header.
  54          $mform->addElement('header', 'general', get_string('general'));
  55  
  56          // Hidden course ID.
  57          $mform->addElement('hidden', 'courseid');
  58          $mform->setType('courseid', PARAM_INT);
  59  
  60          // We are editing a existing rule.
  61          if (!empty($rule->id)) {
  62              // Hidden rule id.
  63              $mform->addElement('hidden', 'ruleid');
  64              $mform->setType('ruleid', PARAM_INT);
  65              $mform->setConstant('ruleid', $rule->id);
  66  
  67              // Force course id.
  68              $courseid = $rule->courseid;
  69          }
  70  
  71          // Make course id a constant.
  72          $mform->setConstant('courseid', $courseid);
  73  
  74          if (empty($courseid)) {
  75              $context = \context_system::instance();
  76          } else {
  77              $context = \context_course::instance($courseid);
  78          }
  79  
  80          $editoroptions = array(
  81              'subdirs' => 0,
  82              'maxbytes' => 0,
  83              'maxfiles' => 0,
  84              'changeformat' => 0,
  85              'context' => $context,
  86              'noclean' => 0,
  87              'trusttext' => 0
  88          );
  89  
  90          // Name field.
  91          $mform->addElement('text', 'name', get_string('rulename', 'tool_monitor'), 'size="50"');
  92          $mform->addRule('name', get_string('required'), 'required');
  93          $mform->setType('name', PARAM_TEXT);
  94  
  95          // Plugin field.
  96          $mform->addElement('selectgroups', 'plugin', get_string('areatomonitor', 'tool_monitor'), $pluginlist, [
  97              'data-field' => 'component',
  98          ]);
  99          $mform->addRule('plugin', get_string('required'), 'required');
 100  
 101          // Event field.
 102          $mform->addElement('select', 'eventname', get_string('event', 'tool_monitor'), $eventlist, [
 103              'data-field' => 'eventname',
 104              'data-eventlist' => json_encode($eventlist),
 105          ]);
 106          $mform->addRule('eventname', get_string('required'), 'required');
 107  
 108          // Set up the client-side dropdown handler.
 109          $PAGE->requires->js_call_amd('tool_monitor/dropdown', 'init');
 110  
 111          // Freeze plugin and event fields for editing if there's a subscription for this rule.
 112          if ($subscriptioncount > 0) {
 113              $mform->freeze('plugin');
 114              $mform->setConstant('plugin', $rule->plugin);
 115              $mform->freeze('eventname');
 116              $mform->setConstant('eventname', $rule->eventname);
 117          }
 118  
 119          // Description field.
 120          $mform->addElement('editor', 'description', get_string('description'), $editoroptions);
 121  
 122          // Filters.
 123          $freq = array(1 => 1, 5 => 5, 10 => 10, 20 => 20, 30 => 30, 40 => 40, 50 => 50, 60 => 60, 70 => 70, 80 => 80, 90 => 90,
 124                  100 => 100, 1000 => 1000);
 125          $mform->addElement('select', 'frequency', get_string('frequency', 'tool_monitor'), $freq);
 126          $mform->addRule('frequency', get_string('required'), 'required');
 127          $mform->addHelpButton('frequency', 'frequency', 'tool_monitor');
 128  
 129          $mins = array(1 => 1, 5 => 5, 10 => 10, 15 => 15, 20 => 20, 25 => 25, 30 => 30, 35 => 35, 40 => 40, 45 => 45, 50 => 50,
 130                  55 => 55,  60 => 60);
 131          $mform->addElement('select', 'minutes', get_string('inminutes', 'tool_monitor'), $mins);
 132          $mform->addRule('minutes', get_string('required'), 'required');
 133  
 134          // Message template.
 135          $mform->addElement('editor', 'template', get_string('messagetemplate', 'tool_monitor'), $editoroptions);
 136          $mform->setDefault('template', array('text' => get_string('defaultmessagetemplate', 'tool_monitor'),
 137                  'format' => FORMAT_HTML));
 138          $mform->addRule('template', get_string('required'), 'required');
 139          $mform->addHelpButton('template', 'messagetemplate', 'tool_monitor');
 140  
 141          // Action buttons.
 142          $this->add_action_buttons(true, get_string('savechanges'));
 143      }
 144  
 145      /**
 146       * Form validation
 147       *
 148       * @param array $data data from the form.
 149       * @param array $files files uploaded.
 150       *
 151       * @return array of errors.
 152       */
 153      public function validation($data, $files) {
 154          $errors = parent::validation($data, $files);
 155  
 156          if (!eventlist::validate_event_plugin($data['plugin'], $data['eventname'])) {
 157              $errors['eventname'] = get_string('errorincorrectevent', 'tool_monitor');
 158          }
 159  
 160          return $errors;
 161      }
 162  }