From 9bf4d19930075270449c4f8980f8d472f10811d9 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Thu, 26 Mar 2020 19:49:32 +0000 Subject: [PATCH] MDL-67895 task admin: show defaults when editing a task Also show the task classname, and the component info above the form. With some refactoring to eliminate duplicated logic. --- .../task/classes/edit_scheduled_task_form.php | 65 ++++++---- admin/tool/task/lang/en/tool_task.php | 1 + admin/tool/task/renderer.php | 120 ++++++++++++------ admin/tool/task/scheduledtasks.php | 6 +- .../task/tests/behat/manage_tasks.feature | 11 +- 5 files changed, 133 insertions(+), 70 deletions(-) diff --git a/admin/tool/task/classes/edit_scheduled_task_form.php b/admin/tool/task/classes/edit_scheduled_task_form.php index 145b59dc9ba..1a76a3bbe2f 100644 --- a/admin/tool/task/classes/edit_scheduled_task_form.php +++ b/admin/tool/task/classes/edit_scheduled_task_form.php @@ -34,46 +34,59 @@ require_once($CFG->libdir.'/formslib.php'); */ 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(); + $mform->addElement('static', 'lastrun', get_string('lastruntime', 'tool_task'), + $renderer->last_run_time($task)); - $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', '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'); @@ -111,7 +124,7 @@ class tool_task_edit_scheduled_task_form extends moodleform { $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; diff --git a/admin/tool/task/lang/en/tool_task.php b/admin/tool/task/lang/en/tool_task.php index e48348a0e58..b0a553568cb 100644 --- a/admin/tool/task/lang/en/tool_task.php +++ b/admin/tool/task/lang/en/tool_task.php @@ -37,6 +37,7 @@ $string['edittaskschedule'] = 'Edit task schedule: {$a}'; $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'; diff --git a/admin/tool/task/renderer.php b/admin/tool/task/renderer.php index 418fd7e50b0..b3ec0d7ff66 100644 --- a/admin/tool/task/renderer.php +++ b/admin/tool/task/renderer.php @@ -25,6 +25,9 @@ defined('MOODLE_INTERNAL') || die(); +use core\task\scheduled_task; + + /** * Implements the plugin renderer * @@ -72,11 +75,7 @@ class tool_task_renderer extends plugin_renderer_base { $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); @@ -104,42 +103,13 @@ class tool_task_renderer extends plugin_renderer_base { 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]), @@ -157,11 +127,11 @@ class tool_task_renderer extends plugin_renderer_base { $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()), @@ -188,6 +158,72 @@ class tool_task_renderer extends plugin_renderer_base { 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. * diff --git a/admin/tool/task/scheduledtasks.php b/admin/tool/task/scheduledtasks.php index 78b0be56558..d256f3d7175 100644 --- a/admin/tool/task/scheduledtasks.php +++ b/admin/tool/task/scheduledtasks.php @@ -51,6 +51,8 @@ if ($task) { $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)) { @@ -84,12 +86,14 @@ if ($mform && ($mform->is_cancelled() || !empty($CFG->preventscheduledtaskchange } 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); diff --git a/admin/tool/task/tests/behat/manage_tasks.feature b/admin/tool/task/tests/behat/manage_tasks.feature index 6b97b6880f1..160451d7340 100644 --- a/admin/tool/task/tests/behat/manage_tasks.feature +++ b/admin/tool/task/tests/behat/manage_tasks.feature @@ -31,6 +31,15 @@ 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 | @@ -38,7 +47,7 @@ Feature: Manage scheduled tasks | 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: * |