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.
   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   * Admin setting to show current scheduled task's status.
  19   *
  20   * @package core
  21   * @copyright 2021 Universitat Rovira i Virgili
  22   * @author Jordi Pujol-Ahulló <jpahullo@gmail.com>
  23   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  namespace core_admin\local\settings;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once($CFG->libdir . '/adminlib.php');
  31  require_once($CFG->libdir . '/moodlelib.php');
  32  
  33  use admin_setting_description;
  34  use core\task\manager;
  35  use core\task\scheduled_task;
  36  use html_writer;
  37  use lang_string;
  38  use moodle_url;
  39  use stdClass;
  40  
  41  /**
  42   * This admin setting tells whether a given scheduled task is enabled, providing a link to its configuration page.
  43   *
  44   * The goal of this setting is to help contextualizing the configuration settings with related scheduled task status,
  45   * providing the big picture of that part of the system.
  46   *
  47   * @package core
  48   * @copyright 2021 Universitat Rovira i Virgili
  49   * @author Jordi Pujol-Ahulló <jpahullo@gmail.com>
  50   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  51   */
  52  class setting_scheduled_task_status extends admin_setting_description {
  53      /**
  54       * @var string fully qualified class name of a scheduled task.
  55       */
  56      private $classname;
  57      /**
  58       * @var string additional text to append to the description.
  59       */
  60      private $extradescription;
  61  
  62      /**
  63       * setting_scheduled_task_status constructor.
  64       * @param string $name unique setting name.
  65       * @param string $scheduledtaskclassname full classpath class name of the scheduled task.
  66       * @param string $extradescription extra detail to append to the scheduled task status to add context in the setting
  67       * page.
  68       */
  69      public function __construct(string $name, string $scheduledtaskclassname, string $extradescription = '') {
  70          $visiblename = new lang_string('task_status', 'admin');
  71          $this->classname = $scheduledtaskclassname;
  72          $this->extradescription = $extradescription;
  73  
  74          parent::__construct($name, $visiblename, '');
  75      }
  76  
  77      /**
  78       * Calculates lazily the content of the description.
  79       * @param mixed $data nothing expected in this case.
  80       * @param string $query nothing expected in this case.
  81       * @return string the HTML content to print for this setting.
  82       */
  83      public function output_html($data, $query = ''): string {
  84          if (empty($this->description)) {
  85              $this->description = $this->get_task_description();
  86          }
  87  
  88          return parent::output_html($data, $query);
  89      }
  90  
  91      /**
  92       * Returns the HTML to print as the description.
  93       * @return string description to be printed.
  94       */
  95      private function get_task_description(): string {
  96          $task = manager::get_scheduled_task($this->classname);
  97          if ($task->is_enabled()) {
  98              $taskenabled = get_string('enabled', 'admin');
  99          } else {
 100              $taskenabled = get_string('disabled', 'admin');
 101          }
 102          $taskenabled = strtolower($taskenabled);
 103          $gotourl = new moodle_url(
 104              '/admin/tool/task/scheduledtasks.php',
 105              [],
 106              scheduled_task::get_html_id($this->classname)
 107          );
 108          if (!empty($this->extradescription)) {
 109              $this->extradescription = '<br />' . $this->extradescription;
 110          }
 111  
 112          $taskdetail = new stdClass();
 113          $taskdetail->class = $this->classname;
 114          $taskdetail->name = $task->get_name();
 115          $taskdetail->status = $taskenabled;
 116          $taskdetail->gotourl = $gotourl->out(false);
 117          $taskdetail->extradescription = $this->extradescription;
 118  
 119          return html_writer::tag('p', get_string('task_status_desc', 'admin', $taskdetail));
 120      }
 121  }