Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   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  namespace core\progress;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  /**
  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.
  25   *
  26   * @package core_progress
  27   * @copyright 2013 The Open University
  28   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  class display extends base {
  31      /**
  32       * @var int Number of wibble states (state0...stateN-1 classes in CSS)
  33       */
  34      const WIBBLE_STATES = 13;
  35  
  36      /**
  37       * @var \progress_bar Current progress bar.
  38       */
  39      private $bar;
  40  
  41      protected $lastwibble, $currentstate = 0, $direction = 1;
  42  
  43      /**
  44       * @var bool True to display names
  45       */
  46      protected $displaynames = false;
  47  
  48      /**
  49       * Constructs the progress reporter. This will output HTML code for the
  50       * progress bar, and an indeterminate wibbler below it.
  51       *
  52       * @param bool $startnow If true, outputs HTML immediately.
  53       */
  54      public function __construct($startnow = true) {
  55          if ($startnow) {
  56              $this->start_html();
  57          }
  58      }
  59  
  60      /**
  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.
  64       *
  65       * @param bool $displaynames True to display names
  66       */
  67      public function set_display_names($displaynames = true) {
  68          $this->displaynames = $displaynames;
  69      }
  70  
  71      /**
  72       * Starts to output progress.
  73       *
  74       * Called in constructor and in update_progress if required.
  75       *
  76       * @throws \coding_exception If already started
  77       */
  78      public function start_html() {
  79          if ($this->bar) {
  80              throw new \coding_exception('Already started');
  81          }
  82          $this->bar = new \progress_bar();
  83          $this->bar->create();
  84          echo \html_writer::start_div('wibbler');
  85      }
  86  
  87      /**
  88       * Finishes output. (Progress can begin again later if there are more
  89       * calls to update_progress.)
  90       *
  91       * Automatically called from update_progress when progress finishes.
  92       */
  93      public function end_html() {
  94          // Finish progress bar.
  95          $this->bar->update_full(100, '');
  96          $this->bar = null;
  97  
  98          // End wibbler div.
  99          echo \html_writer::end_div();
 100      }
 101  
 102      /**
 103       * When progress is updated, updates the bar.
 104       *
 105       * @see \core\progress\base::update_progress()
 106       */
 107      public function update_progress() {
 108          // If finished...
 109          if (!$this->is_in_progress_section()) {
 110              if ($this->bar) {
 111                  $this->end_html();
 112              }
 113          } else {
 114              if (!$this->bar) {
 115                  $this->start_html();
 116              }
 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);
 122  
 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;
 128                  }
 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.
 134                      $buffersize = 0;
 135                  }
 136                  if ($buffersize) {
 137                      // Force the buffer full.
 138                      echo str_pad('', $buffersize);
 139                  }
 140              }
 141  
 142              // Get progress.
 143              list ($min, $max) = $this->get_progress_proportion_range();
 144  
 145              // Update progress bar.
 146              $message = '';
 147              if ($this->displaynames) {
 148                  $message = $this->get_current_description();
 149              }
 150              $this->bar->update_full($min * 100, $message);
 151  
 152              // Flush output.
 153              flush();
 154          }
 155      }
 156  }