Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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 current step (current set of dots) started
66
     */
67
    protected $starttime;
68
 
69
    /**
70
     * @var int Size code (index in the above arrays)
71
     */
72
    protected $size;
73
 
74
    /**
75
     * @var progress_bar progressbar
76
     */
77
    protected $progressbar;
78
 
79
    /**
80
     * @var string Part of the language string.
81
     */
82
    protected $langstring;
83
    /**
84
     * @var string Module for the language string.
85
     */
86
    protected $module;
87
    /**
88
     * @var string|object|array|int Optional language string parameters.
89
     */
90
    protected $aparam;
91
 
92
    /**
93
     * Generic generator class
94
     *
95
     * @param int $size Size as numeric index
96
     * @param bool $fixeddataset To use fixed or random data
97
     * @param int|bool $filesizelimit The max number of bytes for a generated file
98
     * @param bool $progress True if progress information should be displayed
99
     * @throws coding_exception If parameters are invalid
100
     */
101
    public function __construct($size, $fixeddataset = false, $filesizelimit = false, $progress = true) {
102
 
103
        // Check parameter.
104
        if ($size < self::MIN_SIZE || $size > self::MAX_SIZE) {
105
            throw new coding_exception('Invalid size');
106
        }
107
 
108
        // Set parameters.
109
        $this->size = $size;
110
        $this->fixeddataset = $fixeddataset;
111
        $this->filesizelimit = $filesizelimit;
112
        $this->progress = $progress;
113
    }
114
 
115
    /**
116
     * Converts a size name into the numeric constant.
117
     *
118
     * @param string $sizename Size name e.g. 'L'
119
     * @return int Numeric version
120
     * @throws coding_exception If the size name is not known
121
     */
122
    public static function size_for_name($sizename) {
123
        for ($size = self::MIN_SIZE; $size <= self::MAX_SIZE; $size++) {
124
            if ($sizename == get_string('shortsize_' . $size, 'tool_generator')) {
125
                return $size;
126
            }
127
        }
128
        throw new coding_exception("Unknown size name '$sizename'");
129
    }
130
 
131
    /**
132
     * Displays information as part of progress.
133
     *
134
     * @param string $langstring Part of langstring (after progress_)
135
     * @param mixed $a Optional lang string parameters
136
     * @param bool $leaveopen If true, doesn't close LI tag (ready for dots)
137
     * @param string $module module for language string
138
     */
139
    public function log(string $langstring, $a = null, bool $leaveopen = false, string $module = 'tool_generator'): void {
140
        if (!$this->progress) {
141
            return;
142
        }
143
 
144
        $this->langstring = $langstring;
145
        $this->module = $module;
146
        $this->aparam = $a;
147
 
148
        $this->starttime = microtime(true);
149
        $this->progressbar = new progress_bar();
150
        $this->progressbar->create();
151
    }
152
 
153
    /**
154
     * Outputs dots. There is up to one dot per second. Once a minute, it
155
     * displays a percentage.
156
     *
157
     * @param int $number Number of completed items
158
     * @param int $total Total number of items to complete
159
     */
160
    public function dot(int $number, int $total): void {
161
        if (!$this->progress) {
162
            return;
163
        }
164
        $now = time();
165
 
166
        // Update time limit so PHP doesn't time out.
167
        if (!CLI_SCRIPT) {
168
            core_php_time_limit::raise(120);
169
        }
170
        $status = get_string('progress_' . $this->langstring, $this->module, $number);
171
        $this->progressbar->update($number, $total, $status);
172
    }
173
 
174
    /**
175
     * Ends a log string that was started using log function with $leaveopen.
176
     */
177
    public function end_log(): void {
178
        if (!$this->progress) {
179
            return;
180
        }
181
        $status = get_string('progress_' . $this->langstring, $this->module, $this->aparam);
182
        $done = get_string('done', 'tool_generator', round(microtime(true) - $this->starttime, 1));
183
        $this->progressbar->update_full(100, $status . ' - ' . $done);
184
    }
185
}