[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/tool/task/classes/ -> edit_scheduled_task_form.php (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1