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.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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   * Backend generic code.
  19   *
  20   * @package tool_generator
  21   * @copyright 2013 The Open University
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Backend generic code for all tool_generator commands.
  29   *
  30   * @abstract
  31   * @package tool_generator
  32   * @copyright 2013 The Open University
  33   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  abstract class tool_generator_backend {
  36      /**
  37       * @var int Lowest (smallest) size index
  38       */
  39      const MIN_SIZE = 0;
  40      /**
  41       * @var int Highest (largest) size index
  42       */
  43      const MAX_SIZE = 5;
  44      /**
  45       * @var int Default size index
  46       */
  47      const DEFAULT_SIZE = 3;
  48  
  49      /**
  50       * @var bool True if we want a fixed dataset or false to generate random data
  51       */
  52      protected $fixeddataset;
  53  
  54      /**
  55       * @var int|bool Maximum number of bytes for file.
  56       */
  57      protected $filesizelimit;
  58  
  59      /**
  60       * @var bool True if displaying progress
  61       */
  62      protected $progress;
  63  
  64      /**
  65       * @var int Epoch time at which last dot was displayed
  66       */
  67      protected $lastdot;
  68  
  69      /**
  70       * @var int Epoch time at which last percentage was displayed
  71       */
  72      protected $lastpercentage;
  73  
  74      /**
  75       * @var int Epoch time at which current step (current set of dots) started
  76       */
  77      protected $starttime;
  78  
  79      /**
  80       * @var int Size code (index in the above arrays)
  81       */
  82      protected $size;
  83  
  84      /**
  85       * Generic generator class
  86       *
  87       * @param int $size Size as numeric index
  88       * @param bool $fixeddataset To use fixed or random data
  89       * @param int|bool $filesizelimit The max number of bytes for a generated file
  90       * @param bool $progress True if progress information should be displayed
  91       * @throws coding_exception If parameters are invalid
  92       */
  93      public function __construct($size, $fixeddataset = false, $filesizelimit = false, $progress = true) {
  94  
  95          // Check parameter.
  96          if ($size < self::MIN_SIZE || $size > self::MAX_SIZE) {
  97              throw new coding_exception('Invalid size');
  98          }
  99  
 100          // Set parameters.
 101          $this->size = $size;
 102          $this->fixeddataset = $fixeddataset;
 103          $this->filesizelimit = $filesizelimit;
 104          $this->progress = $progress;
 105      }
 106  
 107      /**
 108       * Converts a size name into the numeric constant.
 109       *
 110       * @param string $sizename Size name e.g. 'L'
 111       * @return int Numeric version
 112       * @throws coding_exception If the size name is not known
 113       */
 114      public static function size_for_name($sizename) {
 115          for ($size = self::MIN_SIZE; $size <= self::MAX_SIZE; $size++) {
 116              if ($sizename == get_string('shortsize_' . $size, 'tool_generator')) {
 117                  return $size;
 118              }
 119          }
 120          throw new coding_exception("Unknown size name '$sizename'");
 121      }
 122  
 123      /**
 124       * Displays information as part of progress.
 125       *
 126       * @param string $langstring Part of langstring (after progress_)
 127       * @param mixed $a Optional lang string parameters
 128       * @param bool $leaveopen If true, doesn't close LI tag (ready for dots)
 129       * @param string $module module for language string
 130       */
 131      public function log(string $langstring, $a = null, bool $leaveopen = false, string $module = 'tool_generator'): void {
 132          if (!$this->progress) {
 133              return;
 134          }
 135          if (CLI_SCRIPT) {
 136              echo '* ';
 137          } else {
 138              echo html_writer::start_tag('li');
 139          }
 140          echo get_string('progress_' . $langstring, $module, $a);
 141          if (!$leaveopen) {
 142              if (CLI_SCRIPT) {
 143                  echo "\n";
 144              } else {
 145                  echo html_writer::end_tag('li');
 146              }
 147          } else {
 148              echo ': ';
 149              $this->lastdot = time();
 150              $this->lastpercentage = $this->lastdot;
 151              $this->starttime = microtime(true);
 152          }
 153      }
 154  
 155      /**
 156       * Outputs dots. There is up to one dot per second. Once a minute, it
 157       * displays a percentage.
 158       *
 159       * @param int $number Number of completed items
 160       * @param int $total Total number of items to complete
 161       */
 162      public function dot(int $number, int $total): void {
 163          if (!$this->progress) {
 164              return;
 165          }
 166          $now = time();
 167          if ($now == $this->lastdot) {
 168              return;
 169          }
 170          $this->lastdot = $now;
 171          if (CLI_SCRIPT) {
 172              echo '.';
 173          } else {
 174              echo ' . ';
 175          }
 176          if ($now - $this->lastpercentage >= 30) {
 177              echo round(100.0 * $number / $total, 1) . '%';
 178              $this->lastpercentage = $now;
 179          }
 180  
 181          // Update time limit so PHP doesn't time out.
 182          if (!CLI_SCRIPT) {
 183              core_php_time_limit::raise(120);
 184          }
 185      }
 186  
 187      /**
 188       * Ends a log string that was started using log function with $leaveopen.
 189       */
 190      public function end_log(): void {
 191          if (!$this->progress) {
 192              return;
 193          }
 194          echo get_string('done', 'tool_generator', round(microtime(true) - $this->starttime, 1));
 195          if (CLI_SCRIPT) {
 196              echo "\n";
 197          } else {
 198              echo html_writer::end_tag('li');
 199          }
 200      }
 201  }