MDL-21432 backup - all sort of restore tasks
[moodle.git] / backup / moodle2 / restore_activity_task.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-moodle2
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  * abstract activity task that provides all the properties and common tasks to be performed
27  * when one activity is being restored
28  *
29  * TODO: Finish phpdocs
30  */
31 abstract class restore_activity_task extends restore_task {
33     protected $info; // info related to activity gathered from backup file
35     /**
36      * Constructor - instantiates one object of this class
37      */
38     public function __construct($name, $info, $plan = null) {
40         $this->info = $info;
41         parent::__construct($name, $plan);
42     }
44     /**
45      * Activity tasks have their own directory to read files
46      */
47     public function get_taskbasepath() {
48         return $this->get_basepath() . '/activities/' . $info->modulename . '_' . $info->moduleid;
49     }
51     /**
52      * Create all the steps that will be part of this task
53      */
54     public function build() {
56         // If we have decided not to restore activities, prevent anything to be built
57         if (!$this->get_setting_value('activities')) {
58             $this->built = true;
59             return;
60         }
62         // TODO: Link all the section steps here
64         // At the end, mark it as built
65         $this->built = true;
66     }
68     /**
69      * Exceptionally override the execute method, so, based in the activity_included setting, we are able
70      * to skip the execution of one task completely
71      */
72     public function execute() {
74         // Find activity_included_setting
75         if (!$this->get_setting_value('included')) {
76             $this->log('activity skipped by _included setting', backup::LOG_DEBUG, $this->name);
78         } else { // Setting tells us it's ok to execute
79             parent::execute();
80         }
81     }
84     /**
85      * Specialisation that, first of all, looks for the setting within
86      * the task with the the prefix added and later, delegates to parent
87      * without adding anything
88      */
89     public function get_setting($name) {
90         $namewithprefix = $this->info->modulename . '_' . $this->info->moduleid . '_' . $name;
91         $result = null;
92         foreach ($this->settings as $key => $setting) {
93             if ($setting->get_name() == $namewithprefix) {
94                 if ($result != null) {
95                     throw new base_task_exception('multiple_settings_by_name_found', $namewithprefix);
96                 } else {
97                     $result = $setting;
98                 }
99             }
100         }
101         if ($result) {
102             return $result;
103         } else {
104             // Fallback to parent
105             return parent::get_setting($name);
106         }
107     }
109 // Protected API starts here
111     /**
112      * Define the common setting that any restore activity will have
113      */
114     protected function define_settings() {
116         // All the settings related to this activity will include this prefix
117         $settingprefix = $this->info->modulename . '_' . $this->info->moduleid . '_';
119         // All these are common settings to be shared by all activities
121         // Define activity_include (to decide if the whole task must be really executed)
122         // Dependent of:
123         // - activities root setting
124         // - section_included setting (if exists)
125         $settingname = $settingprefix . 'included';
126         $activity_included = new restore_activity_generic_setting($settingname, base_setting::IS_BOOLEAN, true);
127         $this->add_setting($activity_included);
128         // Look for "activities" root setting
129         $activities = $this->plan->get_setting('activities');
130         $activities->add_dependency($activity_included);
131         // Look for "section_included" section setting (if exists)
132         $settingname = 'section_' . $this->info->sectionid . '_included';
133         if ($this->plan->setting_exists($settingname)) {
134             $section_included = $this->plan->get_setting($settingname);
135             $section_included->add_dependency($activity_included);
136         }
138         // Define activity_userinfo. Dependent of:
139         // - users root setting
140         // - section_userinfo setting (if exists)
141         // - activity_included setting
142         $settingname = $settingprefix . 'userinfo';
143         $selectvalues = array(0=>get_string('no')); // Safer options
144         $defaultvalue = false;                      // Safer default
145         if (isset($info->settings[$settingname]) && $info->settings[$settingname]) { // Only enabled when available
146             $selectvalues = array(1=>get_string('yes'), 0=>get_string('no'));
147             $defaultvalue = true;
148         }
149         $activity_userinfo = new restore_activity_userinfo_setting($settingname, base_setting::IS_BOOLEAN, $defaultvalue);
150         $activity_userinfo->set_ui(new backup_setting_ui_select($activity_userinfo, get_string('includeuserinfo','backup'), $selectvalues));
151         $this->add_setting($activity_userinfo);
152         // Look for "users" root setting
153         $users = $this->plan->get_setting('users');
154         $users->add_dependency($activity_userinfo);
155         // Look for "section_userinfo" section setting (if exists)
156         $settingname = 'section_' . $this->info->sectionid . '_userinfo';
157         if ($this->plan->setting_exists($settingname)) {
158             $section_userinfo = $this->plan->get_setting($settingname);
159             $section_userinfo->add_dependency($activity_userinfo);
160         }
161         // Look for "activity_included" setting
162         $activity_included->add_dependency($activity_userinfo);
164         // End of common activity settings, let's add the particular ones
165         $this->define_my_settings();
166     }
168     /**
169      * Define (add) particular settings that each activity can have
170      */
171     abstract protected function define_my_settings();
173     /**
174      * Define (add) particular steps that each activity can have
175      */
176     abstract protected function define_my_steps();
178      /**
179      * Code the transformations to perform in the course in
180      * order to get encoded transformed back to working links
181      */
182     abstract static public function decode_content_links($content);