Skip to content

Commit

Permalink
MDL-67895 task admin: show defaults when editing a task
Browse files Browse the repository at this point in the history
Also show the task classname, and the component info above the form.

With some refactoring to eliminate duplicated logic.
  • Loading branch information
timhunt committed Mar 28, 2020
1 parent 6fdc0f8 commit 9bf4d19
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 71 deletions.
67 changes: 40 additions & 27 deletions admin/tool/task/classes/edit_scheduled_task_form.php
Expand Up @@ -34,46 +34,59 @@
*/
class tool_task_edit_scheduled_task_form extends moodleform {
public function definition() {
global $PAGE;

$mform = $this->_form;
/** @var \core\task\scheduled_task $task */
$task = $this->_customdata;
$defaulttask = \core\task\manager::get_default_scheduled_task(get_class($task), false);
$renderer = $PAGE->get_renderer('tool_task');

$plugininfo = core_plugin_manager::instance()->get_plugin_info($task->get_component());
$plugindisabled = $plugininfo && $plugininfo->is_enabled() === false && !$task->get_run_if_component_disabled();

$lastrun = $task->get_last_run_time() ? userdate($task->get_last_run_time()) : get_string('never');
$nextrun = $task->get_next_run_time();
if ($plugindisabled) {
$nextrun = get_string('plugindisabled', 'tool_task');
} else if ($task->get_disabled()) {
$nextrun = get_string('taskdisabled', 'tool_task');
} else if ($nextrun > time()) {
$nextrun = userdate($nextrun);
} else {
$nextrun = get_string('asap', 'tool_task');
}
$mform->addElement('static', 'lastrun', get_string('lastruntime', 'tool_task'), $lastrun);
$mform->addElement('static', 'nextrun', get_string('nextruntime', 'tool_task'), $nextrun);
$mform->addElement('static', 'lastrun', get_string('lastruntime', 'tool_task'),
$renderer->last_run_time($task));

$mform->addElement('static', 'nextrun', get_string('nextruntime', 'tool_task'),
$renderer->next_run_time($task));

$mform->addElement('text', 'minute', get_string('taskscheduleminute', 'tool_task'));
$mform->addGroup([
$mform->createElement('text', 'minute'),
$mform->createElement('static', 'minutedefault', '',
get_string('defaultx', 'tool_task', $defaulttask->get_minute())),
], 'minutegroup', get_string('taskscheduleminute', 'tool_task'), null, false);
$mform->setType('minute', PARAM_RAW);
$mform->addHelpButton('minute', 'taskscheduleminute', 'tool_task');
$mform->addHelpButton('minutegroup', 'taskscheduleminute', 'tool_task');

$mform->addElement('text', 'hour', get_string('taskschedulehour', 'tool_task'));
$mform->addGroup([
$mform->createElement('text', 'hour'),
$mform->createElement('static', 'hourdefault', '',
get_string('defaultx', 'tool_task', $defaulttask->get_hour())),
], 'hourgroup', get_string('taskschedulehour', 'tool_task'), null, false);
$mform->setType('hour', PARAM_RAW);
$mform->addHelpButton('hour', 'taskschedulehour', 'tool_task');
$mform->addHelpButton('hourgroup', 'taskschedulehour', 'tool_task');

$mform->addElement('text', 'day', get_string('taskscheduleday', 'tool_task'));
$mform->addGroup([
$mform->createElement('text', 'day'),
$mform->createElement('static', 'daydefault', '',
get_string('defaultx', 'tool_task', $defaulttask->get_day())),
], 'daygroup', get_string('taskscheduleday', 'tool_task'), null, false);
$mform->setType('day', PARAM_RAW);
$mform->addHelpButton('day', 'taskscheduleday', 'tool_task');
$mform->addHelpButton('daygroup', 'taskscheduleday', 'tool_task');

$mform->addElement('text', 'month', get_string('taskschedulemonth', 'tool_task'));
$mform->addGroup([
$mform->createElement('text', 'month'),
$mform->createElement('static', 'monthdefault', '',
get_string('defaultx', 'tool_task', $defaulttask->get_month())),
], 'monthgroup', get_string('taskschedulemonth', 'tool_task'), null, false);
$mform->setType('month', PARAM_RAW);
$mform->addHelpButton('month', 'taskschedulemonth', 'tool_task');
$mform->addHelpButton('monthgroup', 'taskschedulemonth', 'tool_task');

$mform->addElement('text', 'dayofweek', get_string('taskscheduledayofweek', 'tool_task'));
$mform->addGroup([
$mform->createElement('text', 'dayofweek'),
$mform->createElement('static', 'dayofweekdefault', '',
get_string('defaultx', 'tool_task', $defaulttask->get_day_of_week())),
], 'dayofweekgroup', get_string('taskscheduledayofweek', 'tool_task'), null, false);
$mform->setType('dayofweek', PARAM_RAW);
$mform->addHelpButton('dayofweek', 'taskscheduledayofweek', 'tool_task');
$mform->addHelpButton('dayofweekgroup', 'taskscheduledayofweek', 'tool_task');

$mform->addElement('advcheckbox', 'disabled', get_string('disabled', 'tool_task'));
$mform->addHelpButton('disabled', 'disabled', 'tool_task');
Expand Down Expand Up @@ -111,7 +124,7 @@ public function validation($data, $files) {
$fields = array('minute', 'hour', 'day', 'month', 'dayofweek');
foreach ($fields as $field) {
if (!self::validate_fields($field, $data[$field])) {
$error[$field] = get_string('invaliddata', 'core_error');
$error[$field . 'group'] = get_string('invaliddata', 'core_error');
}
}
return $error;
Expand Down
1 change: 1 addition & 0 deletions admin/tool/task/lang/en/tool_task.php
Expand Up @@ -37,6 +37,7 @@
$string['enablerunnow'] = 'Allow \'Run now\' for scheduled tasks';
$string['enablerunnow_desc'] = 'Allows administrators to run a single scheduled task immediately, rather than waiting for it to run as scheduled. The feature requires \'Path to PHP CLI\' (pathtophp) to be set in System paths. The task runs on the web server, so you may wish to disable this feature to avoid potential performance issues.';
$string['faildelay'] = 'Fail delay';
$string['fromcomponent'] = 'From component: {$a}';
$string['lastruntime'] = 'Last run';
$string['nextruntime'] = 'Next run';
$string['plugindisabled'] = 'Plugin disabled';
Expand Down
120 changes: 78 additions & 42 deletions admin/tool/task/renderer.php
Expand Up @@ -25,6 +25,9 @@

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

use core\task\scheduled_task;


/**
* Implements the plugin renderer
*
Expand Down Expand Up @@ -72,11 +75,7 @@ public function scheduled_tasks_table($tasks, $lastchanged = '') {
$data = [];
$yes = get_string('yes');
$no = get_string('no');
$never = get_string('never');
$asap = get_string('asap', 'tool_task');
$disabledstr = get_string('taskdisabled', 'tool_task');
$plugindisabledstr = get_string('plugindisabled', 'tool_task');
$runnabletasks = tool_task\run_from_cli::is_runnable();
$canruntasks = tool_task\run_from_cli::is_runnable();
foreach ($tasks as $task) {
$classname = get_class($task);
$defaulttask = \core\task\manager::get_default_scheduled_task($classname, false);
Expand Down Expand Up @@ -104,42 +103,13 @@ public function scheduled_tasks_table($tasks, $lastchanged = '') {
html_writer::span('\\' . $classname, 'task-class text-ltr'));
$namecell->header = true;

$component = $task->get_component();
$plugininfo = null;
list($type) = core_component::normalize_component($component);
if ($type === 'core') {
$componentcell = new html_table_cell(get_string('corecomponent', 'tool_task'));
} else {
if ($plugininfo = core_plugin_manager::instance()->get_plugin_info($component)) {
$plugininfo->init_display_name();
$componentcell = new html_table_cell($plugininfo->displayname);
if (!$plugininfo->is_enabled()) {
$componentcell->text .= ' ' . html_writer::span(
get_string('disabled', 'tool_task'), 'badge badge-secondary');
}
$componentcell->text .= "\n" . html_writer::span($plugininfo->component, 'task-class text-ltr');
} else {
$componentcell = new html_table_cell($component);
}
}

$lastrun = $task->get_last_run_time() ? userdate($task->get_last_run_time()) : $never;
$nextrun = $task->get_next_run_time();
$disabled = false;
if ($plugininfo && $plugininfo->is_enabled() === false && !$task->get_run_if_component_disabled()) {
$disabled = true;
$nextrun = $plugindisabledstr;
} else if ($task->get_disabled()) {
$disabled = true;
$nextrun = $disabledstr;
} else if ($nextrun > time()) {
$nextrun = userdate($nextrun);
} else {
$nextrun = $asap;
}
$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();

$runnow = '';
if (!$disabled && get_config('tool_task', 'enablerunnow') && $runnabletasks ) {
if (!$disabled && get_config('tool_task', 'enablerunnow') && $canruntasks ) {
$runnow = html_writer::div(html_writer::link(
new moodle_url('/admin/tool/task/schedule_task.php',
['task' => $classname]),
Expand All @@ -157,11 +127,11 @@ public function scheduled_tasks_table($tasks, $lastchanged = '') {

$row = new html_table_row([
$namecell,
$componentcell,
new html_table_cell($this->component_name($task->get_component())),
new html_table_cell($editlink),
new html_table_cell($loglink),
new html_table_cell($lastrun . $runnow),
new html_table_cell($nextrun),
new html_table_cell($this->last_run_time($task) . $runnow),
new html_table_cell($this->next_run_time($task)),
$this->time_cell($task->get_minute(), $defaulttask->get_minute()),
$this->time_cell($task->get_hour(), $defaulttask->get_hour()),
$this->time_cell($task->get_day(), $defaulttask->get_day()),
Expand All @@ -188,6 +158,72 @@ public function scheduled_tasks_table($tasks, $lastchanged = '') {
return html_writer::table($table);
}

/**
* Nicely display the name of a component, with its disabled status and internal name.
*
* @param string $component component name, e.g. 'core' or 'mod_forum'.
* @return string HTML.
*/
public function component_name(string $component): string {
list($type) = core_component::normalize_component($component);
if ($type === 'core') {
return get_string('corecomponent', 'tool_task');
}

$plugininfo = core_plugin_manager::instance()->get_plugin_info($component);
if (!$plugininfo) {
return $component;
}

$plugininfo->init_display_name();

$componentname = $plugininfo->displayname;
if (!$plugininfo->is_enabled()) {
$componentname .= ' ' . html_writer::span(
get_string('disabled', 'tool_task'), 'badge badge-secondary');
}
$componentname .= "\n" . html_writer::span($plugininfo->component, 'task-class text-ltr');

return $componentname;
}

/**
* Standard display of a tasks last run time.
*
* @param scheduled_task $task
* @return string HTML.
*/
public function last_run_time(scheduled_task $task): string {
if ($task->get_last_run_time()) {
return userdate($task->get_last_run_time());
} else {
return get_string('never');
}
}

/**
* Standard display of a tasks next run time.
*
* @param scheduled_task $task
* @return string HTML.
*/
public function next_run_time(scheduled_task $task): string {
$plugininfo = core_plugin_manager::instance()->get_plugin_info($task->get_component());

$nextrun = $task->get_next_run_time();
if ($plugininfo && $plugininfo->is_enabled() === false && !$task->get_run_if_component_disabled()) {
$nextrun = get_string('plugindisabled', 'tool_task');
} else if ($task->get_disabled()) {
$nextrun = get_string('taskdisabled', 'tool_task');
} else if ($nextrun > time()) {
$nextrun = userdate($nextrun);
} else {
$nextrun = get_string('asap', 'tool_task');
}

return $nextrun;
}

/**
* Get a table cell to show one time, comparing it to the default.
*
Expand Down
6 changes: 5 additions & 1 deletion admin/tool/task/scheduledtasks.php
Expand Up @@ -51,6 +51,8 @@
$nexturl = new moodle_url($PAGE->url, ['lastchanged' => $taskname]);
}

$renderer = $PAGE->get_renderer('tool_task');

if ($mform && ($mform->is_cancelled() || !empty($CFG->preventscheduledtaskchanges))) {
redirect($nexturl);
} else if ($action == 'edit' && empty($CFG->preventscheduledtaskchanges)) {
Expand Down Expand Up @@ -84,12 +86,14 @@
} else {
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('edittaskschedule', 'tool_task', $task->get_name()));
echo html_writer::div('\\' . get_class($task), 'task-class text-ltr');
echo html_writer::div(get_string('fromcomponent', 'tool_task',
$renderer->component_name($task->get_component())));
$mform->display();
echo $OUTPUT->footer();
}

} else {
$renderer = $PAGE->get_renderer('tool_task');
echo $OUTPUT->header();
$tasks = core\task\manager::get_all_scheduled_tasks();
echo $renderer->scheduled_tasks_table($tasks, $lastchanged);
Expand Down
11 changes: 10 additions & 1 deletion admin/tool/task/tests/behat/manage_tasks.feature
Expand Up @@ -31,14 +31,23 @@ Feature: Manage scheduled tasks
Scenario: Edit scheduled task
When I click on "Edit task schedule: Log table cleanup" "link" in the "Log table cleanup" "table_row"
Then I should see "Edit task schedule: Log table cleanup"
And I should see "\logstore_standard\task\cleanup_task"
And I should see "From component: Standard log"
And I should see "logstore_standard"
And I should see "Default: R" in the "Minute" "fieldset"
And I should see "Default: *" in the "Day" "fieldset"
And I set the following fields to these values:
| minute | frog |
And I press "Save changes"
And I should see "Data submitted is invalid"
And I set the following fields to these values:
| minute | */5 |
| hour | 1 |
| day | 2 |
| month | 3 |
| dayofweek | 4 |
And I press "Save changes"
Then I should see "Changes saved"
And I should see "Changes saved"
And the following should exist in the "admintable" table:
| Component | Minute | Hour | Day | Day of week | Month |
| Standard log logstore_standard | */5 Default: R | 1 Default: 4 | 2 Default: * | 4 Default: * | 3 Default: * |
Expand Down

0 comments on commit 9bf4d19

Please sign in to comment.