~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to backup/util/progress/core_backup_display_progress.class.php

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 */
 
26
class 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
    /**
 
99
     * When progress is updated, updates the bar.
 
100
     *
 
101
     * @see core_backup_progress::update_progress()
 
102
     */
 
103
    public function update_progress() {
 
104
        // If finished...
 
105
        if (!$this->is_in_progress_section()) {
 
106
            if ($this->bar) {
 
107
                $this->end_html();
 
108
            }
 
109
        } else {
 
110
            if (!$this->bar) {
 
111
                $this->start_html();
 
112
            }
 
113
            // In case of indeterminate or small progress, update the wibbler
 
114
            // (up to once per second).
 
115
            if (time() != $this->lastwibble) {
 
116
                $this->lastwibble = time();
 
117
                echo html_writer::div('', 'wibble state' . $this->currentstate);
 
118
 
 
119
                // Go on to next colour.
 
120
                $this->currentstate += $this->direction;
 
121
                if ($this->currentstate < 0 || $this->currentstate >= self::WIBBLE_STATES) {
 
122
                    $this->direction = -$this->direction;
 
123
                    $this->currentstate += 2 * $this->direction;
 
124
                }
 
125
            }
 
126
 
 
127
            // Get progress.
 
128
            list ($min, $max) = $this->get_progress_proportion_range();
 
129
 
 
130
            // Update progress bar.
 
131
            $message = '';
 
132
            if ($this->displaynames) {
 
133
                $message = $this->get_current_description();
 
134
            }
 
135
            $this->bar->update_full($min * 100, $message);
 
136
 
 
137
            // Flush output.
 
138
            flush();
 
139
        }
 
140
    }
 
141
}