Differences Between: [Versions 311 and 402] [Versions 311 and 403]
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 * @param string $langstring Part of langstring (after progress_) 126 * @param mixed $a Optional lang string parameters 127 * @param bool $leaveopen If true, doesn't close LI tag (ready for dots) 128 */ 129 protected function log($langstring, $a = null, $leaveopen = false) { 130 if (!$this->progress) { 131 return; 132 } 133 if (CLI_SCRIPT) { 134 echo '* '; 135 } else { 136 echo html_writer::start_tag('li'); 137 } 138 echo get_string('progress_' . $langstring, 'tool_generator', $a); 139 if (!$leaveopen) { 140 if (CLI_SCRIPT) { 141 echo "\n"; 142 } else { 143 echo html_writer::end_tag('li'); 144 } 145 } else { 146 echo ': '; 147 $this->lastdot = time(); 148 $this->lastpercentage = $this->lastdot; 149 $this->starttime = microtime(true); 150 } 151 } 152 153 /** 154 * Outputs dots. There is up to one dot per second. Once a minute, it 155 * displays a percentage. 156 * @param int $number Number of completed items 157 * @param int $total Total number of items to complete 158 */ 159 protected function dot($number, $total) { 160 if (!$this->progress) { 161 return; 162 } 163 $now = time(); 164 if ($now == $this->lastdot) { 165 return; 166 } 167 $this->lastdot = $now; 168 if (CLI_SCRIPT) { 169 echo '.'; 170 } else { 171 echo ' . '; 172 } 173 if ($now - $this->lastpercentage >= 30) { 174 echo round(100.0 * $number / $total, 1) . '%'; 175 $this->lastpercentage = $now; 176 } 177 178 // Update time limit so PHP doesn't time out. 179 if (!CLI_SCRIPT) { 180 core_php_time_limit::raise(120); 181 } 182 } 183 184 /** 185 * Ends a log string that was started using log function with $leaveopen. 186 */ 187 protected function end_log() { 188 if (!$this->progress) { 189 return; 190 } 191 echo get_string('done', 'tool_generator', round(microtime(true) - $this->starttime, 1)); 192 if (CLI_SCRIPT) { 193 echo "\n"; 194 } else { 195 echo html_writer::end_tag('li'); 196 } 197 } 198 199 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body