MDL-33682 backup: keep old behavior, aka, use section number on empty section name.
[moodle.git] / backup / util / factories / backup_factory.class.php
1 <?php
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/>.
18 /**
19  * @package    moodlecore
20  * @subpackage backup-factories
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  */
25 /**
26  * Non instantiable factory class providing different backup object instances
27  *
28  * This class contains various methods available in order to easily
29  * create different parts of the backup architecture in an easy way
30  *
31  * TODO: Finish phpdocs
32  */
33 abstract class backup_factory {
35     public static function get_destination_chain($type, $id, $mode, $execution) {
36         return null;
37     }
39     public static function get_logger_chain($interactive, $execution, $backupid) {
40         global $CFG;
42         $dfltloglevel = backup::LOG_WARNING; // Default logging level
43         if (debugging('', DEBUG_DEVELOPER)) { // Debug developer raises default logging level
44             $dfltloglevel = backup::LOG_DEBUG;
45         }
47         $enabledloggers = array(); // Array to store all enabled loggers
49         // Create error_log_logger, observing $CFG->backup_error_log_logger_level,
50         // defaulting to $dfltloglevel
51         $elllevel = isset($CFG->backup_error_log_logger_level) ? $CFG->backup_error_log_logger_level : $dfltloglevel;
52         $enabledloggers[] = new error_log_logger($elllevel);
54         // Create output_indented_logger, observing $CFG->backup_output_indented_logger_level and $CFG->debugdisplay,
55         // defaulting to LOG_ERROR. Only if interactive and inmediate
56         if ($CFG->debugdisplay && $interactive == backup::INTERACTIVE_YES && $execution == backup::EXECUTION_INMEDIATE) {
57             $oillevel = isset($CFG->backup_output_indented_logger_level) ? $CFG->backup_output_indented_logger_level : backup::LOG_ERROR;
58             $enabledloggers[] = new output_indented_logger($oillevel, false, false);
59         }
61         // Create file_logger, observing $CFG->backup_file_logger_level
62         // defaulting to $dfltloglevel
63         check_dir_exists($CFG->tempdir . '/backup', true, true); // need to ensure that temp/backup already exists
64         $fllevel = isset($CFG->backup_file_logger_level) ? $CFG->backup_file_logger_level : $dfltloglevel;
65         $enabledloggers[] = new file_logger($fllevel, true, true, $CFG->tempdir . '/backup/' . $backupid . '.log');
67         // Create database_logger, observing $CFG->backup_database_logger_level and defaulting to LOG_WARNING
68         // and pointing to the backup_logs table
69         $dllevel = isset($CFG->backup_database_logger_level) ? $CFG->backup_database_logger_level : backup::LOG_WARNING;
70         $columns = array('backupid' => $backupid);
71         $enabledloggers[] = new database_logger($dllevel, 'timecreated', 'loglevel', 'message', 'backup_logs', $columns);
73         // Create extra file_logger, observing $CFG->backup_file_logger_extra and $CFG->backup_file_logger_extra_level
74         // defaulting to $fllevel (normal file logger)
75         if (isset($CFG->backup_file_logger_extra)) {
76             $flelevel = isset($CFG->backup_file_logger_extra_level) ? $CFG->backup_file_logger_extra_level : $fllevel;
77             $enabledloggers[] =  new file_logger($flelevel, true, true, $CFG->backup_file_logger_extra);
78         }
80         // Build the chain
81         $loggers = null;
82         foreach ($enabledloggers as $currentlogger) {
83             if ($loggers == null) {
84                 $loggers = $currentlogger;
85             } else {
86                 $lastlogger->set_next($currentlogger);
87             }
88             $lastlogger = $currentlogger;
89         }
91         return $loggers;
92     }
95     /**
96      * Given one format and one course module id, return the corresponding
97      * backup_xxxx_activity_task()
98      */
99     public static function get_backup_activity_task($format, $moduleid) {
100         global $CFG, $DB;
102         // Check moduleid exists
103         if (!$coursemodule = get_coursemodule_from_id(false, $moduleid)) {
104             throw new backup_task_exception('activity_task_coursemodule_not_found', $moduleid);
105         }
106         $classname = 'backup_' . $coursemodule->modname . '_activity_task';
107         return new $classname($coursemodule->name, $moduleid);
108     }
110     /**
111      * Given one format, one block id and, optionally, one moduleid, return the corresponding backup_xxx_block_task()
112      */
113     public static function get_backup_block_task($format, $blockid, $moduleid = null) {
114         global $CFG, $DB;
116         // Check blockid exists
117         if (!$block = $DB->get_record('block_instances', array('id' => $blockid))) {
118             throw new backup_task_exception('block_task_block_instance_not_found', $blockid);
119         }
121         // Set default block backup task
122         $classname = 'backup_default_block_task';
123         $testname  = 'backup_' . $block->blockname . '_block_task';
124         // If the block has custom backup/restore task class (testname), use it
125         if (class_exists($testname)) {
126             $classname = $testname;
127         }
128         return new $classname($block->blockname, $blockid, $moduleid);
129     }
131     /**
132      * Given one format and one section id, return the corresponding backup_section_task()
133      */
134     public static function get_backup_section_task($format, $sectionid) {
135         global $DB;
137         // Check section exists
138         if (!$section = $DB->get_record('course_sections', array('id' => $sectionid))) {
139             throw new backup_task_exception('section_task_section_not_found', $sectionid);
140         }
142         return new backup_section_task((string)$section->name === '' ? $section->section : $section->name, $sectionid);
143     }
145     /**
146      * Given one format and one course id, return the corresponding backup_course_task()
147      */
148     public static function get_backup_course_task($format, $courseid) {
149         global $DB;
151         // Check course exists
152         if (!$course = $DB->get_record('course', array('id' => $courseid))) {
153             throw new backup_task_exception('course_task_course_not_found', $courseid);
154         }
156         return new backup_course_task($course->shortname, $courseid);
157     }
159     /**
160      * Dispatches the creation of the @backup_plan to the proper format builder
161      */
162     static public function build_plan($controller) {
163         backup_plan_builder::build_plan($controller);
164     }