Skip to content

Commit 8f1d735

Browse files
authored
Merge pull request #1080 from cwarwicker/MDL-70854
MDL-70854: Add docs for progress_bar and stored_progress_bar.
2 parents eb3b95a + 207f3f7 commit 8f1d735

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

docs/apis/subsystems/output/index.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,101 @@ In the standard Boost theme this method will output a span using the [Bootstrap
346346
<span class="sr-only">Contents</span>
347347
```
348348

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.
374+
for($cur_task = 0; $cur_task <= $num_tasks; $cur_task++)
375+
{
376+
for ($i=0; $i<100000; $i++)
377+
{
378+
;;;
379+
}
380+
$pb->update($cur_task, $num_tasks, 'this is'. $cur_task . 'h');
381+
}
382+
383+
echo $OUTPUT->footer();
384+
```
385+
386+
##### Stored Progress Bars
387+
388+
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');
403+
$progress->start();
404+
405+
for($cur_task = 0; $cur_task <= $num_tasks; $cur_task++)
406+
{
407+
$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.
443+
349444
## See also
350445

351446
- [HTML Guidelines](https://docs.moodle.org/dev/HTML_Guidelines)

0 commit comments

Comments
 (0)