Skip to content

Commit

Permalink
MDL-70230 admin: add setting sched. task status
Browse files Browse the repository at this point in the history
When defining settings that are used by scheduled tasks,
it is also useful, or even needed, to know the status
of that scheduled task to have the whole big picture of
that part of the system.

Based on the admin_setting_description, this new setting
reports its name, its status, a link to the configuration.

When adding a new setting of this type, the user can add
an extra description field to complete the whole meaning.
  • Loading branch information
jpahullo committed Jun 4, 2021
1 parent 411150a commit 9c4510a
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 8 deletions.
121 changes: 121 additions & 0 deletions admin/classes/local/settings/setting_scheduled_task_status.php
@@ -0,0 +1,121 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Admin setting to show current scheduled task's status.
*
* @package core
* @copyright 2021 Universitat Rovira i Virgili
* @author Jordi Pujol-Ahulló <jpahullo@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_admin\local\settings;

defined('MOODLE_INTERNAL') || die();

global $CFG;
require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->libdir . '/moodlelib.php');

use admin_setting_description;
use core\task\manager;
use core\task\scheduled_task;
use html_writer;
use lang_string;
use moodle_url;
use stdClass;

/**
* This admin setting tells whether a given scheduled task is enabled, providing a link to its configuration page.
*
* The goal of this setting is to help contextualizing the configuration settings with related scheduled task status,
* providing the big picture of that part of the system.
*
* @package core
* @copyright 2021 Universitat Rovira i Virgili
* @author Jordi Pujol-Ahulló <jpahullo@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class setting_scheduled_task_status extends admin_setting_description {
/**
* @var string fully qualified class name of a scheduled task.
*/
private $classname;
/**
* @var string additional text to append to the description.
*/
private $extradescription;

/**
* setting_scheduled_task_status constructor.
* @param string $name unique setting name.
* @param string $scheduledtaskclassname full classpath class name of the scheduled task.
* @param string $extradescription extra detail to append to the scheduled task status to add context in the setting
* page.
*/
public function __construct(string $name, string $scheduledtaskclassname, string $extradescription = '') {
$visiblename = new lang_string('task_status', 'admin');
$this->classname = $scheduledtaskclassname;
$this->extradescription = $extradescription;

parent::__construct($name, $visiblename, '');
}

/**
* Calculates lazily the content of the description.
* @param mixed $data nothing expected in this case.
* @param string $query nothing expected in this case.
* @return string the HTML content to print for this setting.
*/
public function output_html($data, $query = ''): string {
if (empty($this->description)) {
$this->description = $this->get_task_description();
}

return parent::output_html($data, $query);
}

/**
* Returns the HTML to print as the description.
* @return string description to be printed.
*/
private function get_task_description(): string {
$task = manager::get_scheduled_task($this->classname);
if ($task->is_enabled()) {
$taskenabled = get_string('enabled', 'admin');
} else {
$taskenabled = get_string('disabled', 'admin');
}
$taskenabled = strtolower($taskenabled);
$gotourl = new moodle_url(
'/admin/tool/task/scheduledtasks.php',
[],
scheduled_task::get_html_id($this->classname)
);
if (!empty($this->extradescription)) {
$this->extradescription = '<br />' . $this->extradescription;
}

$taskdetail = new stdClass();
$taskdetail->class = $this->classname;
$taskdetail->name = $task->get_name();
$taskdetail->status = $taskenabled;
$taskdetail->gotourl = $gotourl->out(false);
$taskdetail->extradescription = $this->extradescription;

return html_writer::tag('p', get_string('task_status_desc', 'admin', $taskdetail));
}
}
3 changes: 3 additions & 0 deletions admin/settings/language.php
Expand Up @@ -2,6 +2,8 @@

// This file defines settingpages and externalpages under the "appearance" category

use core_admin\local\settings\setting_scheduled_task_status;

if ($hassiteconfig) {

// "languageandlocation" settingpage
Expand All @@ -16,6 +18,7 @@
$temp->add(new admin_setting_configcheckbox('langstringcache', new lang_string('langstringcache', 'admin'), new lang_string('configlangstringcache', 'admin'), 1));
$temp->add(new admin_setting_configtext('locale', new lang_string('localetext', 'admin'), new lang_string('configlocale', 'admin'), '', PARAM_FILE));
$temp->add(new admin_setting_configselect('latinexcelexport', new lang_string('latinexcelexport', 'admin'), new lang_string('configlatinexcelexport', 'admin'), '0', array('0'=>'Unicode','1'=>'Latin')));
$temp->add(new setting_scheduled_task_status('langimporttaskstatus', '\tool_langimport\task\update_langpacks_task'));

$ADMIN->add('language', $temp);

Expand Down
13 changes: 5 additions & 8 deletions admin/tool/task/renderer.php
Expand Up @@ -76,7 +76,7 @@ public function scheduled_tasks_table($tasks, $lastchanged = '') {
$data = [];
$yes = get_string('yes');
$no = get_string('no');
$canruntasks = \core\task\manager::is_runnable();
$canruntasks = \core\task\manager::is_runnable() && get_config('tool_task', 'enablerunnow');
foreach ($tasks as $task) {
$classname = get_class($task);
$defaulttask = \core\task\manager::get_default_scheduled_task($classname, false);
Expand Down Expand Up @@ -108,14 +108,11 @@ public function scheduled_tasks_table($tasks, $lastchanged = '') {
}
$namecell = new html_table_cell($namecellcontent);
$namecell->header = true;

$plugininfo = core_plugin_manager::instance()->get_plugin_info($task->get_component());
$plugindisabled = $plugininfo && $plugininfo->is_enabled() === false &&
!$task->get_run_if_component_disabled();
$disabled = $plugindisabled || $task->get_disabled();
$namecell->id = scheduled_task::get_html_id($classname);

$runnow = '';
if (!$plugindisabled && get_config('tool_task', 'enablerunnow') && $canruntasks ) {
$canrunthistask = $canruntasks && $task->can_run();
if ($canrunthistask) {
$runnow = html_writer::div(html_writer::link(
new moodle_url('/admin/tool/task/schedule_task.php',
['task' => $classname]),
Expand Down Expand Up @@ -147,7 +144,7 @@ public function scheduled_tasks_table($tasks, $lastchanged = '') {
new html_table_cell($customised)]);

$classes = [];
if ($disabled) {
if (!$task->is_enabled()) {
$classes[] = 'disabled';
}
if (get_class($task) == $lastchanged) {
Expand Down
4 changes: 4 additions & 0 deletions admin/tool/task/styles.css
Expand Up @@ -15,3 +15,7 @@
#page-admin-tool-task-scheduledtasks .task-clearfaildelay {
font-size: 0.75em;
}

#page-admin-tool-task-scheduledtasks :target {
scroll-margin-top: 60px;
}
2 changes: 2 additions & 0 deletions lang/en/admin.php
Expand Up @@ -1300,6 +1300,8 @@
$string['task_result:failed'] = 'Fail';
$string['task_stats:dbreads'] = '{$a} reads';
$string['task_stats:dbwrites'] = '{$a} writes';
$string['task_status'] = 'Task status';
$string['task_status_desc'] = 'The task <q>{$a->name}</q> is <strong>{$a->status}</strong>.<br />See its <a href="{$a->gotourl}">details</a>.<br />Class: {$a->class}{$a->extradescription}';
$string['task_starttime'] = 'Start time';
$string['task_duration'] = 'Duration';
$string['task_dbstats'] = 'Database';
Expand Down
27 changes: 27 additions & 0 deletions lib/classes/task/scheduled_task.php
Expand Up @@ -429,6 +429,33 @@ public function get_next_scheduled_time() {
return $nexttime;
}

/**
* Informs whether this task can be run.
* @return bool true when this task can be run. false otherwise.
*/
public function can_run(): bool {
return $this->is_component_enabled() || $this->get_run_if_component_disabled();
}

/**
* Checks whether the component and the task disabled flag enables to run this task.
* This do not checks whether the task manager allows running them or if the
* site allows tasks to "run now".
* @return bool true if task is enabled. false otherwise.
*/
public function is_enabled(): bool {
return $this->can_run() && !$this->get_disabled();
}

/**
* Produces a valid id string to use as id attribute based on the given FQCN class name.
* @param string $classname FQCN of a task.
* @return string valid string to be used as id attribute.
*/
public static function get_html_id(string $classname): string {
return str_replace('\\', '-', ltrim($classname, '\\'));
}

/**
* Get a descriptive name for this task (shown to admins).
*
Expand Down
9 changes: 9 additions & 0 deletions lib/classes/task/task_base.php
Expand Up @@ -208,4 +208,13 @@ public function set_pid($pid = null) {
public function get_pid() {
return $this->pid;
}

/**
* Informs whether the task's component is enabled.
* @return bool true when enabled. false otherwise.
*/
public function is_component_enabled(): bool {
$plugininfo = \core_plugin_manager::instance()->get_plugin_info($this->get_component());
return $plugininfo && $plugininfo->is_enabled();
}
}

0 comments on commit 9c4510a

Please sign in to comment.