PageRenderTime 47ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/tool/task/classes/edit_scheduled_task_form.php

https://bitbucket.org/moodle/moodle
PHP | 172 lines | 99 code | 20 blank | 53 comment | 3 complexity | e2481c92dc112a75405dc1d61bd8b7c7 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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. * Form for scheduled tasks admin pages.
  18. *
  19. * @package tool_task
  20. * @copyright 2013 Damyon Wiese <damyon@moodle.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. require_once($CFG->libdir.'/formslib.php');
  25. /**
  26. * Edit scheduled task form.
  27. *
  28. * @copyright 2013 Damyon Wiese <damyon@moodle.com>
  29. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30. */
  31. class tool_task_edit_scheduled_task_form extends moodleform {
  32. public function definition() {
  33. global $PAGE;
  34. $mform = $this->_form;
  35. /** @var \core\task\scheduled_task $task */
  36. $task = $this->_customdata;
  37. $defaulttask = \core\task\manager::get_default_scheduled_task(get_class($task), false);
  38. $renderer = $PAGE->get_renderer('tool_task');
  39. $mform->addElement('static', 'lastrun', get_string('lastruntime', 'tool_task'),
  40. $renderer->last_run_time($task));
  41. $mform->addElement('static', 'nextrun', get_string('nextruntime', 'tool_task'),
  42. $renderer->next_run_time($task));
  43. $mform->addGroup([
  44. $mform->createElement('text', 'minute'),
  45. $mform->createElement('static', 'minutedefault', '',
  46. get_string('defaultx', 'tool_task', $defaulttask->get_minute())),
  47. ], 'minutegroup', get_string('taskscheduleminute', 'tool_task'), null, false);
  48. $mform->setType('minute', PARAM_RAW);
  49. $mform->addHelpButton('minutegroup', 'taskscheduleminute', 'tool_task');
  50. $mform->addGroup([
  51. $mform->createElement('text', 'hour'),
  52. $mform->createElement('static', 'hourdefault', '',
  53. get_string('defaultx', 'tool_task', $defaulttask->get_hour())),
  54. ], 'hourgroup', get_string('taskschedulehour', 'tool_task'), null, false);
  55. $mform->setType('hour', PARAM_RAW);
  56. $mform->addHelpButton('hourgroup', 'taskschedulehour', 'tool_task');
  57. $mform->addGroup([
  58. $mform->createElement('text', 'day'),
  59. $mform->createElement('static', 'daydefault', '',
  60. get_string('defaultx', 'tool_task', $defaulttask->get_day())),
  61. ], 'daygroup', get_string('taskscheduleday', 'tool_task'), null, false);
  62. $mform->setType('day', PARAM_RAW);
  63. $mform->addHelpButton('daygroup', 'taskscheduleday', 'tool_task');
  64. $mform->addGroup([
  65. $mform->createElement('text', 'month'),
  66. $mform->createElement('static', 'monthdefault', '',
  67. get_string('defaultx', 'tool_task', $defaulttask->get_month())),
  68. ], 'monthgroup', get_string('taskschedulemonth', 'tool_task'), null, false);
  69. $mform->setType('month', PARAM_RAW);
  70. $mform->addHelpButton('monthgroup', 'taskschedulemonth', 'tool_task');
  71. $mform->addGroup([
  72. $mform->createElement('text', 'dayofweek'),
  73. $mform->createElement('static', 'dayofweekdefault', '',
  74. get_string('defaultx', 'tool_task', $defaulttask->get_day_of_week())),
  75. ], 'dayofweekgroup', get_string('taskscheduledayofweek', 'tool_task'), null, false);
  76. $mform->setType('dayofweek', PARAM_RAW);
  77. $mform->addHelpButton('dayofweekgroup', 'taskscheduledayofweek', 'tool_task');
  78. $mform->addElement('advcheckbox', 'disabled', get_string('disabled', 'tool_task'));
  79. $mform->addHelpButton('disabled', 'disabled', 'tool_task');
  80. $mform->addElement('advcheckbox', 'resettodefaults', get_string('resettasktodefaults', 'tool_task'));
  81. $mform->addHelpButton('resettodefaults', 'resettasktodefaults', 'tool_task');
  82. $mform->disabledIf('minute', 'resettodefaults', 'checked');
  83. $mform->disabledIf('hour', 'resettodefaults', 'checked');
  84. $mform->disabledIf('day', 'resettodefaults', 'checked');
  85. $mform->disabledIf('dayofweek', 'resettodefaults', 'checked');
  86. $mform->disabledIf('month', 'resettodefaults', 'checked');
  87. $mform->disabledIf('disabled', 'resettodefaults', 'checked');
  88. $mform->addElement('hidden', 'task', get_class($task));
  89. $mform->setType('task', PARAM_RAW);
  90. $mform->addElement('hidden', 'action', 'edit');
  91. $mform->setType('action', PARAM_ALPHANUMEXT);
  92. $this->add_action_buttons(true, get_string('savechanges'));
  93. // Do not use defaults for existing values, the set_data() is the correct way.
  94. $this->set_data(\core\task\manager::record_from_scheduled_task($task));
  95. }
  96. /**
  97. * Custom validations.
  98. *
  99. * @param array $data
  100. * @param array $files
  101. *
  102. * @return array
  103. */
  104. public function validation($data, $files) {
  105. $error = parent::validation($data, $files);
  106. $fields = array('minute', 'hour', 'day', 'month', 'dayofweek');
  107. foreach ($fields as $field) {
  108. if (!self::validate_fields($field, $data[$field])) {
  109. $error[$field . 'group'] = get_string('invaliddata', 'core_error');
  110. }
  111. }
  112. return $error;
  113. }
  114. /**
  115. * Helper function that validates the submitted data.
  116. *
  117. * Explanation of the regex:-
  118. *
  119. * \A\*\z - matches *
  120. * \A[0-5]?[0-9]\z - matches entries like 23
  121. * \A\*\/[0-5]?[0-9]\z - matches entries like * / 5
  122. * \A[0-5]?[0-9](,[0-5]?[0-9])*\z - matches entries like 1,2,3
  123. * \A[0-5]?[0-9]-[0-5]?[0-9]\z - matches entries like 2-10
  124. *
  125. * @param string $field field to validate
  126. * @param string $value value
  127. *
  128. * @return bool true if validation passes, false other wise.
  129. */
  130. public static function validate_fields($field, $value) {
  131. switch ($field) {
  132. case 'minute' :
  133. case 'hour' :
  134. $regex = "/\A\*\z|\A[0-5]?[0-9]\z|\A\*\/[0-5]?[0-9]\z|\A[0-5]?[0-9](,[0-5]?[0-9])*\z|\A[0-5]?[0-9]-[0-5]?[0-9]\z/";
  135. break;
  136. case 'day':
  137. $regex = "/\A\*\z|\A([1-2]?[0-9]|3[0-1])\z|\A\*\/([1-2]?[0-9]|3[0-1])\z|";
  138. $regex .= "\A([1-2]?[0-9]|3[0-1])(,([1-2]?[0-9]|3[0-1]))*\z|\A([1-2]?[0-9]|3[0-1])-([1-2]?[0-9]|3[0-1])\z/";
  139. break;
  140. case 'month':
  141. $regex = "/\A\*\z|\A([0-9]|1[0-2])\z|\A\*\/([0-9]|1[0-2])\z|\A([0-9]|1[0-2])(,([0-9]|1[0-2]))*\z|";
  142. $regex .= "\A([0-9]|1[0-2])-([0-9]|1[0-2])\z/";
  143. break;
  144. case 'dayofweek':
  145. $regex = "/\A\*\z|\A[0-6]\z|\A\*\/[0-6]\z|\A[0-6](,[0-6])*\z|\A[0-6]-[0-6]\z/";
  146. break;
  147. default:
  148. return false;
  149. }
  150. return (bool)preg_match($regex, $value);
  151. }
  152. }