MDL-81714 output: Update stored progress bar APIs

Expand the methods available in the stored_progress_bar output component
and stored_progress_task_trait to allow a progress bar to be created in
a "pending" state before the associated task is executed.
This commit is contained in:
Mark Johnson
2025-03-10 12:41:29 +00:00
parent 91e69cee7f
commit b94b1ed5ff
3 changed files with 114 additions and 25 deletions
+42 -12
View File
@@ -16,6 +16,10 @@
namespace core\task;
use core\progress\db_updater;
use core\progress\stored;
use core\output\stored_progress_bar;
/**
* Trait to use in tasks to automatically add stored progress functionality.
*
@@ -25,10 +29,44 @@ namespace core\task;
* @author Conn Warwicker <[email protected]>
*/
trait stored_progress_task_trait {
/** @var \core\output\stored_progress_bar|null $progress */
/** @var ?stored_progress_bar $progress */
protected $progress = null;
/**
* Construct a unique name for the progress bar.
*
* For adhoc tasks, this will need the ID in it. For scheduled tasks just the class name.
*
* @return string
*/
protected function get_progress_name(): string {
if (method_exists($this, 'get_id')) {
return get_class($this) . '_' . $this->get_id();
} else {
return get_class($this);
}
}
/**
* Initialise a stored progress record.
*/
public function initialise_stored_progress(): void {
$this->progress = new stored_progress_bar(
stored_progress_bar::convert_to_idnumber($this->get_progress_name()),
autostart: false,
);
$this->progress->store_pending();
}
/**
* Get a stored object for the stored progress record.
*
* @return stored
*/
public function get_progress(): stored {
return new stored($this->progress);
}
/**
* Start a stored progress bar implementation for the task this trait is used in.
*
@@ -40,16 +78,8 @@ trait stored_progress_task_trait {
// To get around the issue in MDL-80770, we are manually setting the renderer to cli.
$OUTPUT = $PAGE->get_renderer('core', null, 'cli');
// Construct a unique name for the progress bar.
// For adhoc tasks, this will need the ID in it. For scheduled tasks just the class name.
if (method_exists($this, 'get_id')) {
$name = get_class($this) . '_' . $this->get_id();
} else {
$name = get_class($this);
}
$this->progress = new \core\output\stored_progress_bar(
\core\output\stored_progress_bar::convert_to_idnumber($name)
$this->progress = new stored_progress_bar(
stored_progress_bar::convert_to_idnumber($this->get_progress_name())
);
// Start the progress.