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-plan
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
26 * Implementable class defining the needed stuf for one restore plan
28 * TODO: Finish phpdocs
30 class restore_plan extends base_plan implements loggable {
34 * @var restore_controller
36 protected $controller; // The restore controller building/executing this plan
37 protected $basepath; // Fullpath to dir where backup is available
38 protected $preloaded; // When executing the plan, do we have preloaded (from checks) info
39 protected $decoder; // restore_decode_processor in charge of decoding all the interlinks
40 protected $missingmodules; // to flag if restore has detected some missing module
41 protected $excludingdactivities; // to flag if restore settings are excluding any activity
44 * Constructor - instantiates one object of this class
46 public function __construct($controller) {
49 if (! $controller instanceof restore_controller) {
50 throw new restore_plan_exception('wrong_restore_controller_specified');
52 $this->controller = $controller;
53 $this->basepath = $CFG->tempdir . '/backup/' . $controller->get_tempdir();
54 $this->preloaded = false;
55 $this->decoder = new restore_decode_processor($this->get_restoreid(), $this->get_info()->original_wwwroot, $CFG->wwwroot);
56 $this->missingmodules = false;
57 $this->excludingdactivities = false;
59 parent::__construct('restore_plan');
63 * Destroy all circular references. It helps PHP 5.2 a lot!
65 public function destroy() {
66 // No need to destroy anything recursively here, direct reset
67 $this->controller = null;
68 // Delegate to base plan the rest
72 public function build() {
73 restore_plan_builder::build_plan($this->controller); // We are moodle2 always, go straight to builder
77 public function get_restoreid() {
78 return $this->controller->get_restoreid();
81 public function get_courseid() {
82 return $this->controller->get_courseid();
85 public function get_mode() {
86 return $this->controller->get_mode();
89 public function get_basepath() {
90 return $this->basepath;
93 public function get_logger() {
94 return $this->controller->get_logger();
97 public function get_info() {
98 return $this->controller->get_info();
101 public function get_target() {
102 return $this->controller->get_target();
105 public function get_userid() {
106 return $this->controller->get_userid();
109 public function get_decoder() {
110 return $this->decoder;
113 public function is_samesite() {
114 return $this->controller->is_samesite();
117 public function is_missing_modules() {
118 return $this->missingmodules;
121 public function is_excluding_activities() {
122 return $this->excludingdactivities;
125 public function set_preloaded_information() {
126 $this->preloaded = true;
129 public function get_preloaded_information() {
130 return $this->preloaded;
133 public function get_tempdir() {
134 return $this->controller->get_tempdir();
137 public function set_missing_modules() {
138 $this->missingmodules = true;
141 public function set_excluding_activities() {
142 $this->excludingdactivities = true;
145 public function log($message, $level, $a = null, $depth = null, $display = false) {
146 backup_helper::log($message, $level, $a, $depth, $display, $this->get_logger());
150 * Function responsible for executing the tasks of any plan
152 public function execute() {
153 if ($this->controller->get_status() != backup::STATUS_AWAITING) {
154 throw new restore_controller_exception('restore_not_executable_awaiting_required', $this->controller->get_status());
156 $this->controller->set_status(backup::STATUS_EXECUTING);
158 $this->controller->set_status(backup::STATUS_FINISHED_OK);
160 // Trigger a course restored event.
161 $event = \core\event\course_restored::create(array(
162 'objectid' => $this->get_courseid(),
163 'userid' => $this->get_userid(),
164 'context' => context_course::instance($this->get_courseid()),
165 'other' => array('type' => $this->controller->get_type(),
166 'target' => $this->controller->get_target(),
167 'mode' => $this->controller->get_mode(),
168 'operation' => $this->controller->get_operation(),
169 'samesite' => $this->controller->is_samesite())
175 * Execute the after_restore methods of all the executed tasks in the plan
177 public function execute_after_restore() {
178 // Simply iterate over each task in the plan and delegate to them the execution
179 foreach ($this->tasks as $task) {
180 $task->execute_after_restore();
186 * Exception class used by all the @restore_plan stuff
188 class restore_plan_exception extends base_plan_exception {
190 public function __construct($errorcode, $a=NULL, $debuginfo=null) {
191 parent::__construct($errorcode, $a, $debuginfo);