Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
// This file is part of Moodle - http://moodle.org/
4
//
5
// Moodle is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
9
//
10
// Moodle is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
// GNU General Public License for more details.
14
//
15
// You should have received a copy of the GNU General Public License
16
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17
 
18
/**
19
 * @package    moodlecore
20
 * @subpackage backup-output
21
 * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 *
24
 * TODO: Finish phpdocs
25
 */
26
 
27
/**
28
 * This class decides, based in environment/backup controller settings about
29
 * the best way to send information to output, independently of the process
30
 * and the loggers. Instantiated/configured by @backup_controller constructor
31
 *
32
 * Mainly used by backup_helper::log() (that receives all the log requests from
33
 * the rest of backup objects) to split messages both to loggers and to output.
34
 *
35
 * This class adopts the singleton pattern to be able to provide some persistency
36
 * and global access.
37
 */
38
class output_controller {
39
 
40
    private static $instance; // The unique instance of output_controller available along the request
41
    private $list;            // progress_trace object we are going to use for output
42
    private $active;          // To be able to stop output completely or active it again
43
 
44
    private function __construct() { // Private constructor
45
        if (defined('STDOUT')) { // text mode
46
            $this->list = new text_progress_trace();
47
        } else {
48
            $this->list = new html_list_progress_trace();
49
        }
50
        $this->active = false; // Somebody has to active me before outputing anything
51
    }
52
 
53
    public static function get_instance() {
54
        if (!isset(self::$instance)) {
55
            self::$instance = new output_controller();
56
        }
57
        return self::$instance;
58
    }
59
 
60
    public function set_active($active) {
61
        if ($this->active && (bool)$active == false) { // Stopping, call finished()
62
            $this->list->finished();
63
        }
64
        $this->active = (bool)$active;
65
    }
66
 
67
    public function output($message, $langfile, $a, $depth) {
68
        if ($this->active) {
69
            $stringkey = preg_replace('/\s/', '', $message); // String key is message without whitespace
70
            $message = get_string($stringkey, $langfile, $a);
71
            $this->list->output($message, $depth);
72
        }
73
    }
74
}