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/>.
19 * Defines restore_block_task class
21 * @package core_backup
24 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') ||
die();
31 * abstract block task that provides all the properties and common steps to be performed
32 * when one block is being restored
34 * TODO: Finish phpdocs
36 abstract class restore_block_task
extends restore_task
{
38 protected $taskbasepath; // To store the basepath of this block
39 protected $blockname; // Name of the block
40 protected $contextid; // new (target) context of the block
41 protected $oldcontextid;// old (original) context of the block
42 protected $blockid; // new (target) id of the block
43 protected $oldblockid; // old (original) id of the block
46 * Constructor - instantiates one object of this class
48 public function __construct($name, $taskbasepath, $plan = null) {
49 $this->taskbasepath
= $taskbasepath;
50 $this->blockname
= '';
52 $this->oldcontextid
= 0;
54 $this->oldblockid
= 0;
55 parent
::__construct($name, $plan);
59 * Block tasks have their own directory to write files
61 public function get_taskbasepath() {
62 return $this->taskbasepath
;
66 * Create all the steps that will be part of this task
68 public function build() {
70 // If we have decided not to backup blocks, prevent anything to be built
71 if (!$this->get_setting_value('blocks')) {
76 // If "child" of activity task and it has been excluded, nothing to do
77 $parent = basename(dirname(dirname($this->taskbasepath
)));
78 if ($parent != 'course') {
79 $includedsetting = $parent . '_included';
80 if (!$this->get_setting_value($includedsetting)) {
86 // Process the block.xml common file (instance + positions)
87 $this->add_step(new restore_block_instance_structure_step('block_commons', 'block.xml'));
89 // Here we add all the common steps for any block and, in the point of interest
90 // we call to define_my_steps() in order to get the particular ones inserted in place.
91 $this->define_my_steps();
93 // Restore block role assignments and overrides (internally will observe the role_assignments setting)
94 $this->add_step(new restore_ras_and_caps_structure_step('block_ras_and_caps', 'roles.xml'));
96 // Restore block comments (conditionally)
97 if ($this->get_setting_value('comments')) {
98 $this->add_step(new restore_comments_structure_step('block_comments', 'comments.xml'));
101 // Search reindexing (if enabled).
102 if (\core_search\manager
::is_indexing_enabled()) {
103 $wholecourse = $this->get_target() == backup
::TARGET_NEW_COURSE
;
104 $wholecourse = $wholecourse ||
$this->setting_exists('overwrite_conf') && $this->get_setting_value('overwrite_conf');
106 $this->add_step(new restore_block_search_index('block_search_index'));
110 // At the end, mark it as built
114 public function set_blockname($blockname) {
115 $this->blockname
= $blockname;
118 public function get_blockname() {
119 return $this->blockname
;
122 public function set_blockid($blockid) {
123 $this->blockid
= $blockid;
126 public function get_blockid() {
127 return $this->blockid
;
130 public function set_old_blockid($blockid) {
131 $this->oldblockid
= $blockid;
134 public function get_old_blockid() {
135 return $this->oldblockid
;
138 public function set_contextid($contextid) {
139 $this->contextid
= $contextid;
142 public function get_contextid() {
143 return $this->contextid
;
146 public function set_old_contextid($contextid) {
147 $this->oldcontextid
= $contextid;
150 public function get_old_contextid() {
151 return $this->oldcontextid
;
155 * Define one array() of fileareas that each block controls
157 abstract public function get_fileareas();
160 * Define one array() of configdata attributes
161 * that need to be decoded
163 abstract public function get_configdata_encoded_attributes();
166 * Helper method to safely unserialize block configuration during restore
168 * @param string $configdata The original base64 encoded block config, as retrieved from the block_instances table
171 protected function decode_configdata(string $configdata): stdClass
{
172 return unserialize_object(base64_decode($configdata));
176 * Define the contents in the activity that must be
177 * processed by the link decoder
179 static public function define_decode_contents() {
180 throw new coding_exception('define_decode_contents() method needs to be overridden in each subclass of restore_block_task');
184 * Define the decoding rules for links belonging
185 * to the activity to be executed by the link decoder
187 static public function define_decode_rules() {
188 throw new coding_exception('define_decode_rules() method needs to be overridden in each subclass of restore_block_task');
191 // Protected API starts here
194 * Define the common setting that any backup block will have
196 protected function define_settings() {
198 // Nothing to add, blocks doesn't have common settings (for now)
200 // End of common activity settings, let's add the particular ones
201 $this->define_my_settings();
205 * Define (add) particular settings that each block can have
207 abstract protected function define_my_settings();
210 * Define (add) particular steps that each block can have
212 abstract protected function define_my_steps();