MDL-75256 task: move abstract get_name method to base class.

Allow adhoc tasks to implement this method, so they too can have
descriptive names for themselves. Default implementation added to
return the class name itself.
This commit is contained in:
Paul Holden
2022-07-22 09:13:53 +01:00
parent 4ce642e8ba
commit d0b5241786
9 changed files with 67 additions and 13 deletions
+5 -5
View File
@@ -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;
+1
View File
@@ -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';
+14
View File
@@ -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
-8
View File
@@ -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();
}
+7
View File
@@ -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
+20
View File
@@ -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.
*/
+9
View File
@@ -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.
*/
+2
View File
@@ -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 ===
@@ -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
*/