You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/apis/subsystems/output/index.md
+95Lines changed: 95 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -346,6 +346,101 @@ In the standard Boost theme this method will output a span using the [Bootstrap
346
346
<spanclass="sr-only">Contents</span>
347
347
```
348
348
349
+
### Other
350
+
351
+
#### Progress Bars
352
+
353
+
There are two types of progress bars you can use in Moodle.
354
+
355
+
##### Standard Progress Bars
356
+
357
+
The first is the old standard progress bar which updates as the page loads (ie. the page won't fully load until the progress bar is finished). It can be used to render the current process on the page, or via cli to render the progress of a script.
358
+
359
+
Example:
360
+
361
+
```php
362
+
<?php
363
+
define('NO_OUTPUT_BUFFERING', true); // Required if not used via cli.
364
+
require 'config.php';
365
+
366
+
$PAGE->set_context(context_system::instance());
367
+
$PAGE->set_url('/');
368
+
369
+
echo $OUTPUT->header();
370
+
371
+
$pb = new \core\output\progress_bar('bar', 500);
372
+
$pb->create();
373
+
$num_tasks = 500; // the number of tasks to be completed.
The other type of progress bar you can use is a stored progress bar, which can be used to store and update the progress of a long-running task in the database, and render live updates to the web page via AJAX web service polling.
389
+
390
+
It can be implemented into a CLI script. Example:
391
+
392
+
```php
393
+
<?php
394
+
395
+
define('CLI_SCRIPT', true);
396
+
397
+
require_once('config.php');
398
+
require_once($CFG->libdir . '/clilib.php');
399
+
400
+
$num_tasks = 5000; // the number of tasks to be completed.
401
+
402
+
$progress = new \core\output\stored_progress_bar('example-cli-bar');
$progress->update($cur_task, $num_tasks, 'this is '. $cur_task . '/' . $num_tasks);
408
+
}
409
+
410
+
mtrace('DONE');
411
+
```
412
+
413
+
Or a scheduled or adhoc task, via a trait. Example:
414
+
415
+
```php
416
+
class stored_progress_scheduled_task_example extends \core\task\scheduled_task {
417
+
418
+
use \core\task\stored_progress_task_trait;
419
+
420
+
public function execute() {
421
+
422
+
// This simulates a specific count of iterations the task will do, e.g. x number of courses to loop through and do something.
423
+
$iterations = 100;
424
+
425
+
$this->start_stored_progress(); // This creates the stored progress record for the named task.
426
+
427
+
for ($i = 1; $i <= $iterations; $i++) {
428
+
429
+
// Here we just update and tell it which one we are on and it will work out % from those.
430
+
$this->progress->update($i, $iterations, 'i am at ' . $i . ' of ' . $iterations);
431
+
sleep(1);
432
+
433
+
}
434
+
435
+
return true;
436
+
437
+
}
438
+
439
+
}
440
+
```
441
+
442
+
With the stored progress bars, you can update the progress either via iterations, by passing in the total amount expected and then the current iteration, using `->update()`(see: previous example), this will calculate the percentage complete for you. Or you can use `->update_full()` to manually set the percentage complete.
0 commit comments