weekly release 3.1dev
[moodle.git] / lib / classes / progress / display_if_slow.php
CommitLineData
a7a64d77 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
809fdb83
JP
17namespace core\progress;
18
19defined('MOODLE_INTERNAL') || die();
20
a7a64d77 21/**
22 * Progress handler that uses a standard Moodle progress bar to display
809fdb83 23 * progress. Same as \core\progress\display, but the bar does not
a7a64d77 24 * appear until a certain time has elapsed, and disappears automatically
25 * after it finishes.
26 *
27 * The bar can be re-used, i.e. if you end all sections it will disappear,
28 * but if you start all sections, a new bar will be output.
29 *
809fdb83 30 * @package core_progress
a7a64d77 31 * @copyright 2013 The Open University
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
809fdb83 34class display_if_slow extends display {
a7a64d77 35 /**
36 * @var int Waits this many seconds before displaying progress bar
37 */
38 const DEFAULT_DISPLAY_DELAY = 5;
39
40 /**
41 * @var int Number in the next id to use
42 */
43 private static $nextid = 1;
44
45 /**
46 * @var string HTML id for containing div
47 */
48 protected $id;
49
4fb31ae6 50 /**
51 * @var string Text to display in heading if bar appears
52 */
53 protected $heading;
54
a7a64d77 55 /**
56 * @var int Time at which the progress bar should display (if it isn't yet)
57 */
58 protected $starttime;
59
60 /**
61 * Constructs the progress reporter. This will not output HTML just yet,
62 * until the required delay time expires.
63 *
8da6fc9d 64 * @param string $heading Text to display above bar (if it appears); '' for none (default)
a7a64d77 65 * @param int $delay Delay time (default 5 seconds)
66 */
8da6fc9d 67 public function __construct($heading = '', $delay = self::DEFAULT_DISPLAY_DELAY) {
a7a64d77 68 // Set start time based on delay.
69 $this->starttime = time() + $delay;
4fb31ae6 70 $this->heading = $heading;
a7a64d77 71 parent::__construct(false);
72 }
73
74 /**
4fb31ae6 75 * Starts displaying the progress bar, with optional heading and a special
76 * div so it can be hidden later.
a7a64d77 77 *
809fdb83 78 * @see \core\progress\display::start_html()
a7a64d77 79 */
80 public function start_html() {
4fb31ae6 81 global $OUTPUT;
809fdb83 82 $this->id = 'core_progress_display_if_slow' . self::$nextid;
a7a64d77 83 self::$nextid++;
4fb31ae6 84
85 // Containing div includes a CSS class so that it can be themed if required,
86 // and an id so it can be automatically hidden at end.
809fdb83 87 echo \html_writer::start_div('core_progress_display_if_slow',
4fb31ae6 88 array('id' => $this->id));
89
90 // Display optional heading.
91 if ($this->heading !== '') {
92 echo $OUTPUT->heading($this->heading, 3);
93 }
94
95 // Use base class to display progress bar.
a7a64d77 96 parent::start_html();
97 }
98
99 /**
100 * When progress is updated, after a certain time, starts actually displaying
101 * the progress bar.
102 *
809fdb83 103 * @see \core\progress\base::update_progress()
a7a64d77 104 */
105 public function update_progress() {
106 // If we haven't started yet, consider starting.
107 if ($this->starttime) {
108 if (time() > $this->starttime) {
109 $this->starttime = 0;
110 } else {
111 // Do nothing until start time.
112 return;
113 }
114 }
115
116 // We have started, so handle as default.
117 parent::update_progress();
118 }
119
120 /**
121 * Finishes parent display then closes div and hides it.
122 *
809fdb83 123 * @see \core\progress\display::end_html()
a7a64d77 124 */
125 public function end_html() {
126 parent::end_html();
809fdb83
JP
127 echo \html_writer::end_div();
128 echo \html_writer::script('document.getElementById("' . $this->id .
a7a64d77 129 '").style.display = "none"');
130 }
131}