Skip to content

Commit 6a49950

Browse files
committed
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.
1 parent c08824b commit 6a49950

File tree

3 files changed

+95
-18
lines changed

3 files changed

+95
-18
lines changed

lib/classes/stored_progress_bar.php

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,25 @@ class stored_progress_bar extends \progress_bar {
3838
/**
3939
* This overwrites the progress_bar::__construct method.
4040
*
41+
* The stored progress bar does not need to check NO_OUTPUT_BUFFERING since it outputs to the page
42+
* then polls for updates asynchronously, rather than waiting for synchronous updates in later output.
43+
*
4144
* @param string $idnumber
45+
* @param int $width
46+
* @param bool $autostart
4247
*/
43-
public function __construct($idnumber) {
44-
// Construct from the parent.
45-
parent::__construct($idnumber, 0, true);
48+
public function __construct(string $idnumber, int $width = 0, bool $autostart = true) {
49+
if (!empty($idnumber)) {
50+
$this->idnumber = $idnumber;
51+
} else {
52+
$this->idnumber = 'pbar_'.uniqid();
53+
}
54+
55+
$this->width = $width;
56+
57+
if ($autostart) {
58+
$this->create();
59+
}
4660
}
4761

4862
/**
@@ -118,10 +132,10 @@ protected function set_record_id(int $id): void {
118132
/**
119133
* Set the time we started the process.
120134
*
121-
* @param int $value
135+
* @param ?int $value
122136
* @return void
123137
*/
124-
protected function set_time_started(int $value): void {
138+
protected function set_time_started(?int $value): void {
125139
$this->timestart = $value;
126140
}
127141

@@ -219,8 +233,19 @@ public function export_for_template(\renderer_base $output): array {
219233
public function start(): int {
220234
global $DB;
221235

222-
// Delete any existing records for this.
223-
$this->end();
236+
$record = $DB->get_record('stored_progress', ['idnumber' => $this->idnumber]);
237+
if ($record) {
238+
if ($record->timestart == 0) {
239+
// Set the timestart now and return.
240+
$record->timestart = $this->timestart;
241+
$DB->update_record('stored_progress', $record);
242+
$this->recordid = $record->id;
243+
return $this->recordid;
244+
} else {
245+
// Delete any existing records for this.
246+
$this->end();
247+
}
248+
}
224249

225250
// Create new progress record.
226251
$this->recordid = $DB->insert_record('stored_progress', [
@@ -359,4 +384,25 @@ public static function get_timeout(): int {
359384
return $CFG->progresspollinterval ?? 5;
360385
}
361386

387+
/**
388+
* Store a progress bar record in a pending state.
389+
*
390+
* @return int ID of the DB record
391+
*/
392+
public function store_pending(): int {
393+
global $DB;
394+
395+
// Delete any existing records for this.
396+
$this->end();
397+
398+
// Create new progress record.
399+
$this->recordid = $DB->insert_record('stored_progress', [
400+
'idnumber' => $this->idnumber,
401+
'timestart' => $this->timestart,
402+
'message' => 'Task pending',
403+
]);
404+
405+
return $this->recordid;
406+
}
407+
362408
}

lib/classes/task/stored_progress_task_trait.php

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
namespace core\task;
1818

19+
use core\progress\db_updater;
20+
use core\progress\stored;
21+
use core\stored_progress_bar;
22+
1923
/**
2024
* Trait to use in tasks to automatically add stored progress functionality.
2125
*
@@ -26,9 +30,44 @@
2630
*/
2731
trait stored_progress_task_trait {
2832

29-
/** @var \core\stored_progress_bar|null $progress */
33+
/** @var ?\core\stored_progress_bar $progress */
3034
protected $progress = null;
3135

36+
/**
37+
* Construct a unique name for the progress bar.
38+
*
39+
* For adhoc tasks, this will need the ID in it. For scheduled tasks just the class name.
40+
*
41+
* @return string
42+
*/
43+
protected function get_progress_name(): string {
44+
if (method_exists($this, 'get_id')) {
45+
return get_class($this) . '_' . $this->get_id();
46+
} else {
47+
return get_class($this);
48+
}
49+
}
50+
51+
/**
52+
* Initialise a stored progress record.
53+
*/
54+
public function initialise_stored_progress(): void {
55+
$this->progress = new \core\stored_progress_bar(
56+
\core\stored_progress_bar::convert_to_idnumber($this->get_progress_name()),
57+
autostart: false,
58+
);
59+
$this->progress->store_pending();
60+
}
61+
62+
/**
63+
* Get a db_updater object for the stored progress record.
64+
*
65+
* @return stored
66+
*/
67+
public function get_progress(): stored {
68+
return new stored($this->progress);
69+
}
70+
3271
/**
3372
* Start a stored progress bar implementation for the task this trait is used in.
3473
*
@@ -40,16 +79,8 @@ protected function start_stored_progress(): void {
4079
// To get around the issue in MDL-80770, we are manually setting the renderer to cli.
4180
$OUTPUT = $PAGE->get_renderer('core', null, 'cli');
4281

43-
// Construct a unique name for the progress bar.
44-
// For adhoc tasks, this will need the ID in it. For scheduled tasks just the class name.
45-
if (method_exists($this, 'get_id')) {
46-
$name = get_class($this) . '_' . $this->get_id();
47-
} else {
48-
$name = get_class($this);
49-
}
50-
5182
$this->progress = new \core\stored_progress_bar(
52-
\core\stored_progress_bar::convert_to_idnumber($name)
83+
\core\stored_progress_bar::convert_to_idnumber($this->get_progress_name())
5384
);
5485

5586
// Start the progress.

lib/templates/progress_bar.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
</div>
3232
<div class="d-flex">
3333
<div style="flex: 1 1 0; min-width: 0;">
34-
<div id="{{idnumber}}_status" class="text-truncate">&nbsp;</div>
34+
<div id="{{idnumber}}_status" class="text-truncate">{{message}}</div>
3535
</div>
3636
<div class="text-right pl-3" style="flex: 0 0 content">
3737
<span id="{{idnumber}}_estimate" class="">&nbsp;</span>

0 commit comments

Comments
 (0)