Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 402] [Versions 400 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          $mform = $this->_form;
  45          $eventlist = $this->_customdata['eventlist'];
  46          $pluginlist = $this->_customdata['pluginlist'];
  47          $rule = $this->_customdata['rule'];
  48          $courseid = $this->_customdata['courseid'];
  49          $subscriptioncount = $this->_customdata['subscriptioncount'];
  50  
  51          // General section header.
  52          $mform->addElement('header', 'general', get_string('general'));
  53  
  54          // Hidden course ID.
  55          $mform->addElement('hidden', 'courseid');
  56          $mform->setType('courseid', PARAM_INT);
  57  
  58          // We are editing a existing rule.
  59          if (!empty($rule->id)) {
  60              // Hidden rule id.
  61              $mform->addElement('hidden', 'ruleid');
  62              $mform->setType('ruleid', PARAM_INT);
  63              $mform->setConstant('ruleid', $rule->id);
  64  
  65              // Force course id.
  66              $courseid = $rule->courseid;
  67          }
  68  
  69          // Make course id a constant.
  70          $mform->setConstant('courseid', $courseid);
  71  
  72          if (empty($courseid)) {
  73              $context = \context_system::instance();
  74          } else {
  75              $context = \context_course::instance($courseid);
  76          }
  77  
  78          $editoroptions = array(
  79              'subdirs' => 0,
  80              'maxbytes' => 0,
  81              'maxfiles' => 0,
  82              'changeformat' => 0,
  83              'context' => $context,
  84              'noclean' => 0,
  85              'trusttext' => 0
  86          );
  87  
  88          // Name field.
  89          $mform->addElement('text', 'name', get_string('rulename', 'tool_monitor'), 'size="50"');
  90          $mform->addRule('name', get_string('required'), 'required');
  91          $mform->setType('name', PARAM_TEXT);
  92  
  93          // Plugin field.
  94          $mform->addElement('select', 'plugin', get_string('areatomonitor', 'tool_monitor'), $pluginlist);
  95          $mform->addRule('plugin', get_string('required'), 'required');
  96  
  97          // Event field.
  98          $mform->addElement('select', 'eventname', get_string('event', 'tool_monitor'), $eventlist);
  99          $mform->addRule('eventname', get_string('required'), 'required');
 100  
 101          // Freeze plugin and event fields for editing if there's a subscription for this rule.
 102          if ($subscriptioncount > 0) {
 103              $mform->freeze('plugin');
 104              $mform->setConstant('plugin', $rule->plugin);
 105              $mform->freeze('eventname');
 106              $mform->setConstant('eventname', $rule->eventname);
 107          }
 108  
 109          // Description field.
 110          $mform->addElement('editor', 'description', get_string('description'), $editoroptions);
 111  
 112          // Filters.
 113          $freq = array(1 => 1, 5 => 5, 10 => 10, 20 => 20, 30 => 30, 40 => 40, 50 => 50, 60 => 60, 70 => 70, 80 => 80, 90 => 90,
 114                  100 => 100, 1000 => 1000);
 115          $mform->addElement('select', 'frequency', get_string('frequency', 'tool_monitor'), $freq);
 116          $mform->addRule('frequency', get_string('required'), 'required');
 117          $mform->addHelpButton('frequency', 'frequency', 'tool_monitor');
 118  
 119          $mins = array(1 => 1, 5 => 5, 10 => 10, 15 => 15, 20 => 20, 25 => 25, 30 => 30, 35 => 35, 40 => 40, 45 => 45, 50 => 50,
 120                  55 => 55,  60 => 60);
 121          $mform->addElement('select', 'minutes', get_string('inminutes', 'tool_monitor'), $mins);
 122          $mform->addRule('minutes', get_string('required'), 'required');
 123  
 124          // Message template.
 125          $mform->addElement('editor', 'template', get_string('messagetemplate', 'tool_monitor'), $editoroptions);
 126          $mform->setDefault('template', array('text' => get_string('defaultmessagetemplate', 'tool_monitor'),
 127                  'format' => FORMAT_HTML));
 128          $mform->addRule('template', get_string('required'), 'required');
 129          $mform->addHelpButton('template', 'messagetemplate', 'tool_monitor');
 130  
 131          // Action buttons.
 132          $this->add_action_buttons(true, get_string('savechanges'));
 133      }
 134  
 135      /**
 136       * Form validation
 137       *
 138       * @param array $data data from the form.
 139       * @param array $files files uploaded.
 140       *
 141       * @return array of errors.
 142       */
 143      public function validation($data, $files) {
 144          $errors = parent::validation($data, $files);
 145  
 146          if (!eventlist::validate_event_plugin($data['plugin'], $data['eventname'])) {
 147              $errors['eventname'] = get_string('errorincorrectevent', 'tool_monitor');
 148          }
 149  
 150          return $errors;
 151      }
 152  }