Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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. Same as \core\progress\display, but the bar does not
  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   *
  30   * @package core_progress
  31   * @copyright 2013 The Open University
  32   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class display_if_slow extends display {
  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  
  50      /**
  51       * @var string Text to display in heading if bar appears
  52       */
  53      protected $heading;
  54  
  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       *
  64       * @param string $heading Text to display above bar (if it appears); '' for none (default)
  65       * @param int $delay Delay time (default 5 seconds)
  66       */
  67      public function __construct($heading = '', $delay = self::DEFAULT_DISPLAY_DELAY) {
  68          // Set start time based on delay.
  69          $this->starttime = time() + $delay;
  70          $this->heading = $heading;
  71          parent::__construct(false);
  72      }
  73  
  74      /**
  75       * Starts displaying the progress bar, with optional heading and a special
  76       * div so it can be hidden later.
  77       *
  78       * @see \core\progress\display::start_html()
  79       */
  80      public function start_html() {
  81          global $OUTPUT;
  82          $this->id = 'core_progress_display_if_slow' . self::$nextid;
  83          self::$nextid++;
  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.
  87          echo \html_writer::start_div('core_progress_display_if_slow',
  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.
  96          parent::start_html();
  97      }
  98  
  99      /**
 100       * When progress is updated, after a certain time, starts actually displaying
 101       * the progress bar.
 102       *
 103       * @see \core\progress\base::update_progress()
 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       *
 123       * @see \core\progress\display::end_html()
 124       */
 125      public function end_html() {
 126          parent::end_html();
 127          echo \html_writer::end_div();
 128          echo \html_writer::script('document.getElementById("' . $this->id .
 129                  '").style.display = "none"');
 130      }
 131  }