weekly release 2.6dev
[moodle.git] / backup / util / progress / core_backup_display_progress.class.php
CommitLineData
f06abc2a 1<?php
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17/**
18 * Progress handler that uses a standard Moodle progress bar to display
19 * progress. The Moodle progress bar cannot show indeterminate progress,
20 * so we do extra output in addition to the bar.
21 *
22 * @package core_backup
23 * @copyright 2013 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26class core_backup_display_progress extends core_backup_progress {
27 /**
28 * @var int Number of wibble states (state0...stateN-1 classes in CSS)
29 */
30 const WIBBLE_STATES = 13;
31
32 /**
33 * @var progress_bar Current progress bar.
34 */
35 private $bar;
36
37 private $lastwibble, $currentstate = 0, $direction = 1;
38
39 /**
40 * @var bool True to display names
41 */
42 protected $displaynames = false;
43
44 /**
45 * Constructs the progress reporter. This will output HTML code for the
46 * progress bar, and an indeterminate wibbler below it.
47 *
48 * @param bool $startnow If true, outputs HTML immediately.
49 */
50 public function __construct($startnow = true) {
51 if ($startnow) {
52 $this->start_html();
53 }
54 }
55
56 /**
57 * By default, the progress section names do not display because (in backup)
58 * these are usually untranslated and incomprehensible. To make them
59 * display, call this method.
60 *
61 * @param bool $displaynames True to display names
62 */
63 public function set_display_names($displaynames = true) {
64 $this->displaynames = $displaynames;
65 }
66
67 /**
68 * Starts to output progress.
69 *
70 * Called in constructor and in update_progress if required.
71 *
72 * @throws coding_exception If already started
73 */
74 public function start_html() {
75 if ($this->bar) {
76 throw new coding_exception('Already started');
77 }
78 $this->bar = new progress_bar();
79 $this->bar->create();
80 echo html_writer::start_div('wibbler');
81 }
82
83 /**
84 * Finishes output. (Progress can begin again later if there are more
85 * calls to update_progress.)
86 *
87 * Automatically called from update_progress when progress finishes.
88 */
89 public function end_html() {
90 // Finish progress bar.
91 $this->bar->update_full(100, '');
92 $this->bar = null;
93
94 // End wibbler div.
95 echo html_writer::end_div();
96 }
97
98 public function update_progress() {
99 // If finished...
100 if (!$this->is_in_progress_section()) {
101 if ($this->bar) {
102 $this->end_html();
103 }
104 } else {
105 if (!$this->bar) {
106 $this->start_html();
107 }
108 // In case of indeterminate or small progress, update the wibbler
109 // (up to once per second).
110 if (time() != $this->lastwibble) {
111 $this->lastwibble = time();
112 echo html_writer::div('', 'wibble state' . $this->currentstate);
113
114 // Go on to next colour.
115 $this->currentstate += $this->direction;
116 if ($this->currentstate < 0 || $this->currentstate >= self::WIBBLE_STATES) {
117 $this->direction = -$this->direction;
118 $this->currentstate += 2 * $this->direction;
119 }
120 }
121
122 // Get progress.
123 list ($min, $max) = $this->get_progress_proportion_range();
124
125 // Update progress bar.
126 $message = '';
127 if ($this->displaynames) {
128 $message = $this->get_current_description();
129 }
130 $this->bar->update_full($min * 100, $message);
131
132 // Flush output.
133 flush();
134 }
135 }
136}