2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 namespace core\progress
;
19 defined('MOODLE_INTERNAL') ||
die();
22 * Progress handler that uses a standard Moodle progress bar to display
23 * progress. The Moodle progress bar cannot show indeterminate progress,
24 * so we do extra output in addition to the bar.
26 * @package core_progress
27 * @copyright 2013 The Open University
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 class display
extends base
{
32 * @var int Number of wibble states (state0...stateN-1 classes in CSS)
34 const WIBBLE_STATES
= 13;
37 * @var \progress_bar Current progress bar.
41 protected $lastwibble, $currentstate = 0, $direction = 1;
44 * @var bool True to display names
46 protected $displaynames = false;
49 * Constructs the progress reporter. This will output HTML code for the
50 * progress bar, and an indeterminate wibbler below it.
52 * @param bool $startnow If true, outputs HTML immediately.
54 public function __construct($startnow = true) {
61 * By default, the progress section names do not display because
62 * these will probably be untranslated and incomprehensible. To make them
63 * display, call this method.
65 * @param bool $displaynames True to display names
67 public function set_display_names($displaynames = true) {
68 $this->displaynames
= $displaynames;
72 * Starts to output progress.
74 * Called in constructor and in update_progress if required.
76 * @throws \coding_exception If already started
78 public function start_html() {
80 throw new \
coding_exception('Already started');
82 $this->bar
= new \
progress_bar();
84 echo \html_writer
::start_div('wibbler');
88 * Finishes output. (Progress can begin again later if there are more
89 * calls to update_progress.)
91 * Automatically called from update_progress when progress finishes.
93 public function end_html() {
94 // Finish progress bar.
95 $this->bar
->update_full(100, '');
99 echo \html_writer
::end_div();
103 * When progress is updated, updates the bar.
105 * @see \core\progress\base::update_progress()
107 public function update_progress() {
109 if (!$this->is_in_progress_section()) {
117 // In case of indeterminate or small progress, update the wibbler
118 // (up to once per second).
119 if (time() != $this->lastwibble
) {
120 $this->lastwibble
= time();
121 echo \html_writer
::div('', 'wibble state' . $this->currentstate
);
123 // Go on to next colour.
124 $this->currentstate +
= $this->direction
;
125 if ($this->currentstate
< 0 ||
$this->currentstate
>= self
::WIBBLE_STATES
) {
126 $this->direction
= -$this->direction
;
127 $this->currentstate +
= 2 * $this->direction
;
129 $buffersize = ini_get('output_buffering');
130 if (!is_numeric($buffersize)) {
131 // The output_buffering setting can be a number, but can also be "On" or "Off".
132 // If "Off", there's no buffer, if "On", there's no limit to the size. In either case,
133 // there's no point in trying to fill the buffer.
137 // Force the buffer full.
138 echo str_pad('', $buffersize);
143 list ($min, $max) = $this->get_progress_proportion_range();
145 // Update progress bar.
147 if ($this->displaynames
) {
148 $message = $this->get_current_description();
150 $this->bar
->update_full($min * 100, $message);