diff --git a/admin/classes/local/entities/task_log.php b/admin/classes/local/entities/task_log.php index 059f1196b64..41e7dc26f10 100644 --- a/admin/classes/local/entities/task_log.php +++ b/admin/classes/local/entities/task_log.php @@ -107,15 +107,15 @@ class task_log extends base { ->set_type(column::TYPE_TEXT) ->add_field("$tablealias.classname") ->set_is_sortable(true) - ->add_callback(static function(string $value): string { + ->add_callback(static function(string $classname): string { $output = ''; - if (class_exists($value)) { - $task = new $value; - if ($task instanceof \core\task\scheduled_task) { + if (class_exists($classname)) { + $task = new $classname; + if ($task instanceof \core\task\task_base) { $output = $task->get_name(); } } - $output .= \html_writer::tag('div', "\\{$value}", [ + $output .= \html_writer::tag('div', "\\{$classname}", [ 'class' => 'task-class', ]); return $output; diff --git a/lang/en/reportbuilder.php b/lang/en/reportbuilder.php index 54ac706d2ae..dc15edcb3f9 100644 --- a/lang/en/reportbuilder.php +++ b/lang/en/reportbuilder.php @@ -255,6 +255,7 @@ $string['sorting'] = 'Sorting'; $string['sorting_help'] = 'Sorting defines the initial sort order of columns in the report. The order can be reversed by toggling the Up/down icon. Users can then define their own sort order by clicking on a column name.'; $string['switchedit'] = 'Switch to edit mode'; $string['switchpreview'] = 'Switch to preview mode'; +$string['tasksendschedule'] = 'Send report schedule'; $string['tasksendschedules'] = 'Send report schedules'; $string['timeadded'] = 'Time added'; $string['timecreated'] = 'Time created'; diff --git a/lib/classes/task/adhoc_task.php b/lib/classes/task/adhoc_task.php index bd3e8afa4a6..1036ced2bba 100644 --- a/lib/classes/task/adhoc_task.php +++ b/lib/classes/task/adhoc_task.php @@ -46,6 +46,20 @@ abstract class adhoc_task extends task_base { /** @var \core\lock\lock The concurrency task lock for this task. */ private $concurrencylock = null; + /** + * Provide default implementation of the task name for backward compatibility. Extending classes are expected to implement + * this method to provide a descriptive name for the task (shown to admins) + * + * @return string + */ + public function get_name() { + $classparts = explode('\\', get_called_class()); + $classname = end($classparts); + + // Try to make human readable, capitalized and with spaces. + return ucfirst(str_replace('_', ' ', $classname)); + } + /** * Setter for $id. * @param int|null $id diff --git a/lib/classes/task/scheduled_task.php b/lib/classes/task/scheduled_task.php index a204ed8a0dd..0eadddb6a71 100644 --- a/lib/classes/task/scheduled_task.php +++ b/lib/classes/task/scheduled_task.php @@ -562,12 +562,4 @@ abstract class scheduled_task extends task_base { public static function get_html_id(string $classname): string { return str_replace('\\', '-', ltrim($classname, '\\')); } - - /** - * Get a descriptive name for this task (shown to admins). - * - * @return string - */ - abstract public function get_name(); - } diff --git a/lib/classes/task/task_base.php b/lib/classes/task/task_base.php index cfe202db99f..92af0ace79b 100644 --- a/lib/classes/task/task_base.php +++ b/lib/classes/task/task_base.php @@ -62,6 +62,13 @@ abstract class task_base { /** @var int $pid - PHP process ID that is running the task */ private $pid = null; + /** + * Get a descriptive name for the task (shown to admins) + * + * @return string + */ + abstract public function get_name(); + /** * Set the current lock for this task. * @param \core\lock\lock $lock diff --git a/lib/tests/adhoc_task_test.php b/lib/tests/adhoc_task_test.php index a45e05e74cb..bb5e3a9bfc4 100644 --- a/lib/tests/adhoc_task_test.php +++ b/lib/tests/adhoc_task_test.php @@ -38,6 +38,26 @@ require_once(__DIR__ . '/fixtures/task_fixtures.php'); */ class core_adhoc_task_testcase extends advanced_testcase { + /** + * Test getting name of task that implements it's own get_name method + * + * @covers \core\task\adhoc_task::get_name + */ + public function test_get_name(): void { + $task = new \core\task\adhoc_test_task(); + $this->assertEquals('Test adhoc class', $task->get_name()); + } + + /** + * Test getting name of task that uses the default implementation of get_name + * + * @covers \core\task\adhoc_task::get_name + */ + public function test_get_name_default(): void { + $task = new \mod_fake\task\adhoc_component_task(); + $this->assertEquals('Adhoc component task', $task->get_name()); + } + /** * Test basic adhoc task execution. */ diff --git a/lib/tests/fixtures/task_fixtures.php b/lib/tests/fixtures/task_fixtures.php index eb910106e92..efaaa98b54b 100644 --- a/lib/tests/fixtures/task_fixtures.php +++ b/lib/tests/fixtures/task_fixtures.php @@ -51,6 +51,15 @@ class adhoc_test_task extends \core\task\adhoc_task { } } + /** + * Get task name + * + * @return string + */ + public function get_name() { + return 'Test adhoc class'; + } + /** * Execute. */ diff --git a/lib/upgrade.txt b/lib/upgrade.txt index cded6d8f4b1..99baf7a0bef 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -37,6 +37,8 @@ information provided here is intended especially for developers. * Added $CFG->proxylogunsafe and proxyfixunsafe to detect code which doesn't honor the proxy config * Function admin_externalpage_setup() now has additional option 'nosearch' allowing to remove Site administration search form. * The function print_error has been deprecated. Kindly use moodle_exception. +* The abstract `get_name` method has been moved to the `\core\task\task_base` class and should now be implemented by adhoc tasks. For + backwards compatibility, a default implementation has been added to `\core\task\adhoc_task` to return the class name * The function get_module_metadata() has been finally deprecated and can not be used anymore. === 4.0 === diff --git a/reportbuilder/classes/task/send_schedule.php b/reportbuilder/classes/task/send_schedule.php index fb7968f1379..b978961977c 100644 --- a/reportbuilder/classes/task/send_schedule.php +++ b/reportbuilder/classes/task/send_schedule.php @@ -34,6 +34,15 @@ class send_schedule extends adhoc_task { use \core\task\logging_trait; + /** + * Return name of the task + * + * @return string + */ + public function get_name(): string { + return get_string('tasksendschedule', 'core_reportbuilder'); + } + /** * Execute the task */