Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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   * 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          global $PAGE;
  38  
  39          $mform = $this->_form;
  40          /** @var \core\task\scheduled_task $task */
  41          $task = $this->_customdata;
  42          $defaulttask = \core\task\manager::get_default_scheduled_task(get_class($task), false);
  43          $renderer = $PAGE->get_renderer('tool_task');
  44  
  45          $mform->addElement('static', 'lastrun', get_string('lastruntime', 'tool_task'),
  46                  $renderer->last_run_time($task));
  47  
  48          $mform->addElement('static', 'nextrun', get_string('nextruntime', 'tool_task'),
  49                  $renderer->next_run_time($task));
  50  
  51          $mform->addGroup([
  52                  $mform->createElement('text', 'minute'),
  53                  $mform->createElement('static', 'minutedefault', '',
  54                          get_string('defaultx', 'tool_task', $defaulttask->get_minute())),
  55              ], 'minutegroup', get_string('taskscheduleminute', 'tool_task'), null, false);
  56          $mform->setType('minute', PARAM_RAW);
  57          $mform->addHelpButton('minutegroup', 'taskscheduleminute', 'tool_task');
  58  
  59          $mform->addGroup([
  60                  $mform->createElement('text', 'hour'),
  61                  $mform->createElement('static', 'hourdefault', '',
  62                          get_string('defaultx', 'tool_task', $defaulttask->get_hour())),
  63          ], 'hourgroup', get_string('taskschedulehour', 'tool_task'), null, false);
  64          $mform->setType('hour', PARAM_RAW);
  65          $mform->addHelpButton('hourgroup', 'taskschedulehour', 'tool_task');
  66  
  67          $mform->addGroup([
  68                  $mform->createElement('text', 'day'),
  69                  $mform->createElement('static', 'daydefault', '',
  70                          get_string('defaultx', 'tool_task', $defaulttask->get_day())),
  71          ], 'daygroup', get_string('taskscheduleday', 'tool_task'), null, false);
  72          $mform->setType('day', PARAM_RAW);
  73          $mform->addHelpButton('daygroup', 'taskscheduleday', 'tool_task');
  74  
  75          $mform->addGroup([
  76                  $mform->createElement('text', 'month'),
  77                  $mform->createElement('static', 'monthdefault', '',
  78                          get_string('defaultx', 'tool_task', $defaulttask->get_month())),
  79          ], 'monthgroup', get_string('taskschedulemonth', 'tool_task'), null, false);
  80          $mform->setType('month', PARAM_RAW);
  81          $mform->addHelpButton('monthgroup', 'taskschedulemonth', 'tool_task');
  82  
  83          $mform->addGroup([
  84                  $mform->createElement('text', 'dayofweek'),
  85                  $mform->createElement('static', 'dayofweekdefault', '',
  86                          get_string('defaultx', 'tool_task', $defaulttask->get_day_of_week())),
  87          ], 'dayofweekgroup', get_string('taskscheduledayofweek', 'tool_task'), null, false);
  88          $mform->setType('dayofweek', PARAM_RAW);
  89          $mform->addHelpButton('dayofweekgroup', 'taskscheduledayofweek', 'tool_task');
  90  
  91          $mform->addElement('advcheckbox', 'disabled', get_string('disabled', 'tool_task'));
  92          $mform->addHelpButton('disabled', 'disabled', 'tool_task');
  93  
  94          $mform->addElement('advcheckbox', 'resettodefaults', get_string('resettasktodefaults', 'tool_task'));
  95          $mform->addHelpButton('resettodefaults', 'resettasktodefaults', 'tool_task');
  96  
  97          $mform->disabledIf('minute', 'resettodefaults', 'checked');
  98          $mform->disabledIf('hour', 'resettodefaults', 'checked');
  99          $mform->disabledIf('day', 'resettodefaults', 'checked');
 100          $mform->disabledIf('dayofweek', 'resettodefaults', 'checked');
 101          $mform->disabledIf('month', 'resettodefaults', 'checked');
 102          $mform->disabledIf('disabled', 'resettodefaults', 'checked');
 103  
 104          $mform->addElement('hidden', 'task', get_class($task));
 105          $mform->setType('task', PARAM_RAW);
 106          $mform->addElement('hidden', 'action', 'edit');
 107          $mform->setType('action', PARAM_ALPHANUMEXT);
 108          $this->add_action_buttons(true, get_string('savechanges'));
 109  
 110          // Do not use defaults for existing values, the set_data() is the correct way.
 111          $this->set_data(\core\task\manager::record_from_scheduled_task($task));
 112      }
 113  
 114      /**
 115       * Custom validations.
 116       *
 117       * @param array $data
 118       * @param array $files
 119       *
 120       * @return array
 121       */
 122      public function validation($data, $files) {
 123          $error = parent::validation($data, $files);
 124          $fields = array('minute', 'hour', 'day', 'month', 'dayofweek');
 125          foreach ($fields as $field) {
 126              if (!self::validate_fields($field, $data[$field])) {
 127                  $error[$field . 'group'] = get_string('invaliddata', 'core_error');
 128              }
 129          }
 130          return $error;
 131      }
 132  
 133      /**
 134       * Helper function that validates the submitted data.
 135       *
 136       * Explanation of the regex:-
 137       *
 138       * \A\*\z - matches *
 139       * \A[0-5]?[0-9]\z - matches entries like 23
 140       * \A\*\/[0-5]?[0-9]\z - matches entries like * / 5
 141       * \A[0-5]?[0-9](,[0-5]?[0-9])*\z - matches entries like 1,2,3
 142       * \A[0-5]?[0-9]-[0-5]?[0-9]\z - matches entries like 2-10
 143       *
 144       * @param string $field field to validate
 145       * @param string $value value
 146       *
 147       * @return bool true if validation passes, false other wise.
 148       */
 149      public static function validate_fields($field, $value) {
 150          switch ($field) {
 151              case 'minute' :
 152              case 'hour' :
 153                  $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/";
 154                  break;
 155              case 'day':
 156                  $regex = "/\A\*\z|\A([1-2]?[0-9]|3[0-1])\z|\A\*\/([1-2]?[0-9]|3[0-1])\z|";
 157                  $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/";
 158                  break;
 159              case 'month':
 160                  $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|";
 161                  $regex .= "\A([0-9]|1[0-2])-([0-9]|1[0-2])\z/";
 162                  break;
 163              case 'dayofweek':
 164                  $regex = "/\A\*\z|\A[0-6]\z|\A\*\/[0-6]\z|\A[0-6](,[0-6])*\z|\A[0-6]-[0-6]\z/";
 165                  break;
 166              default:
 167                  return false;
 168          }
 169          return (bool)preg_match($regex, $value);
 170      }
 171  }
 172