PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/tool/monitor/classes/rule_form.php

https://github.com/dongsheng/moodle
PHP | 152 lines | 73 code | 24 blank | 55 comment | 5 complexity | 058e76593563c904a75a7399aefc3e24 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, GPL-3.0, Apache-2.0, LGPL-2.1
  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. * The mform for creating and editing a rule.
  18. *
  19. * @copyright 2014 onwards Simey Lameze <lameze@gmail.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @package tool_monitor
  22. */
  23. namespace tool_monitor;
  24. require_once($CFG->dirroot.'/lib/formslib.php');
  25. /**
  26. * The mform for creating and editing a rule.
  27. *
  28. * @since Moodle 2.8
  29. * @copyright 2014 onwards Simey Lameze <lameze@gmail.com>
  30. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31. * @package tool_monitor
  32. */
  33. class rule_form extends \moodleform {
  34. /**
  35. * Mform class definition
  36. *
  37. */
  38. public function definition () {
  39. $mform = $this->_form;
  40. $eventlist = $this->_customdata['eventlist'];
  41. $pluginlist = $this->_customdata['pluginlist'];
  42. $rule = $this->_customdata['rule'];
  43. $courseid = $this->_customdata['courseid'];
  44. $subscriptioncount = $this->_customdata['subscriptioncount'];
  45. // General section header.
  46. $mform->addElement('header', 'general', get_string('general'));
  47. // Hidden course ID.
  48. $mform->addElement('hidden', 'courseid');
  49. $mform->setType('courseid', PARAM_INT);
  50. // We are editing a existing rule.
  51. if (!empty($rule->id)) {
  52. // Hidden rule id.
  53. $mform->addElement('hidden', 'ruleid');
  54. $mform->setType('ruleid', PARAM_INT);
  55. $mform->setConstant('ruleid', $rule->id);
  56. // Force course id.
  57. $courseid = $rule->courseid;
  58. }
  59. // Make course id a constant.
  60. $mform->setConstant('courseid', $courseid);
  61. if (empty($courseid)) {
  62. $context = \context_system::instance();
  63. } else {
  64. $context = \context_course::instance($courseid);
  65. }
  66. $editoroptions = array(
  67. 'subdirs' => 0,
  68. 'maxbytes' => 0,
  69. 'maxfiles' => 0,
  70. 'changeformat' => 0,
  71. 'context' => $context,
  72. 'noclean' => 0,
  73. 'trusttext' => 0
  74. );
  75. // Name field.
  76. $mform->addElement('text', 'name', get_string('rulename', 'tool_monitor'), 'size="50"');
  77. $mform->addRule('name', get_string('required'), 'required');
  78. $mform->setType('name', PARAM_TEXT);
  79. // Plugin field.
  80. $mform->addElement('select', 'plugin', get_string('areatomonitor', 'tool_monitor'), $pluginlist);
  81. $mform->addRule('plugin', get_string('required'), 'required');
  82. // Event field.
  83. $mform->addElement('select', 'eventname', get_string('event', 'tool_monitor'), $eventlist);
  84. $mform->addRule('eventname', get_string('required'), 'required');
  85. // Freeze plugin and event fields for editing if there's a subscription for this rule.
  86. if ($subscriptioncount > 0) {
  87. $mform->freeze('plugin');
  88. $mform->setConstant('plugin', $rule->plugin);
  89. $mform->freeze('eventname');
  90. $mform->setConstant('eventname', $rule->eventname);
  91. }
  92. // Description field.
  93. $mform->addElement('editor', 'description', get_string('description'), $editoroptions);
  94. // Filters.
  95. $freq = array(1 => 1, 5 => 5, 10 => 10, 20 => 20, 30 => 30, 40 => 40, 50 => 50, 60 => 60, 70 => 70, 80 => 80, 90 => 90,
  96. 100 => 100, 1000 => 1000);
  97. $mform->addElement('select', 'frequency', get_string('frequency', 'tool_monitor'), $freq);
  98. $mform->addRule('frequency', get_string('required'), 'required');
  99. $mform->addHelpButton('frequency', 'frequency', 'tool_monitor');
  100. $mins = array(1 => 1, 5 => 5, 10 => 10, 15 => 15, 20 => 20, 25 => 25, 30 => 30, 35 => 35, 40 => 40, 45 => 45, 50 => 50,
  101. 55 => 55, 60 => 60);
  102. $mform->addElement('select', 'minutes', get_string('inminutes', 'tool_monitor'), $mins);
  103. $mform->addRule('minutes', get_string('required'), 'required');
  104. // Message template.
  105. $mform->addElement('editor', 'template', get_string('messagetemplate', 'tool_monitor'), $editoroptions);
  106. $mform->setDefault('template', array('text' => get_string('defaultmessagetemplate', 'tool_monitor'),
  107. 'format' => FORMAT_HTML));
  108. $mform->addRule('template', get_string('required'), 'required');
  109. $mform->addHelpButton('template', 'messagetemplate', 'tool_monitor');
  110. // Action buttons.
  111. $this->add_action_buttons(true, get_string('savechanges'));
  112. }
  113. /**
  114. * Form validation
  115. *
  116. * @param array $data data from the form.
  117. * @param array $files files uploaded.
  118. *
  119. * @return array of errors.
  120. */
  121. public function validation($data, $files) {
  122. $errors = parent::validation($data, $files);
  123. if (!eventlist::validate_event_plugin($data['plugin'], $data['eventname'])) {
  124. $errors['eventname'] = get_string('errorincorrectevent', 'tool_monitor');
  125. }
  126. return $errors;
  127. }
  128. }