3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
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
24 * TODO: Finish phpdocs
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
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.
35 * This class adopts the singleton pattern to be able to provide some persistency
38 class output_controller {
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
44 private function __construct() { // Private constructor
45 if (defined('STDOUT')) { // text mode
46 $this->list = new text_progress_trace();
48 $this->list = new html_list_progress_trace();
50 $this->active = false; // Somebody has to active me before outputing anything
53 public static function get_instance() {
54 if (!isset(self::$instance)) {
55 self::$instance = new output_controller();
57 return self::$instance;
60 public function set_active($active) {
61 if ($this->active && (bool)$active == false) { // Stopping, call finished()
62 $this->list->finished();
64 $this->active = (bool)$active;
67 public function output($message, $langfile, $a, $depth) {
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);