Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

   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          // Use a checker class.
 125          $checker = new \tool_task\scheduled_checker_task();
 126          $checker->set_minute($data['minute']);
 127          $checker->set_hour($data['hour']);
 128          $checker->set_month($data['month']);
 129          $checker->set_day_of_week($data['dayofweek']);
 130          $checker->set_day($data['day']);
 131          $checker->set_disabled(false);
 132          $checker->set_customised(false);
 133  
 134          if (!$checker->is_valid($checker::FIELD_MINUTE)) {
 135              $error['minutegroup'] = get_string('invaliddata', 'core_error');
 136          }
 137          if (!$checker->is_valid($checker::FIELD_HOUR)) {
 138              $error['hourgroup'] = get_string('invaliddata', 'core_error');
 139          }
 140          if (!$checker->is_valid($checker::FIELD_DAY)) {
 141              $error['daygroup'] = get_string('invaliddata', 'core_error');
 142          }
 143          if (!$checker->is_valid($checker::FIELD_MONTH)) {
 144              $error['monthgroup'] = get_string('invaliddata', 'core_error');
 145          }
 146          if (!$checker->is_valid($checker::FIELD_DAYOFWEEK)) {
 147              $error['dayofweekgroup'] = get_string('invaliddata', 'core_error');
 148          }
 149  
 150          return $error;
 151      }
 152  }
 153