f9a14d4b6c00f3e5bae68318db2606a095b784af
[moodle.git] / backup / moodle2 / backup_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 backup
28  *
29  * TODO: Finish phpdocs
30  */
31 abstract class backup_activity_task extends backup_task {
33     protected $moduleid;
34     protected $sectionid;
35     protected $modulename;
36     protected $activityid;
37     protected $contextid;
39     /**
40      * Constructor - instantiates one object of this class
41      */
42     public function __construct($name, $moduleid, $plan = null) {
44         // Check moduleid exists
45         if (!$coursemodule = get_coursemodule_from_id(false, $moduleid)) {
46             throw new backup_task_exception('activity_task_coursemodule_not_found', $moduleid);
47         }
48         // Check activity supports this moodle2 backup format
49         if (!plugin_supports('mod', $coursemodule->modname, FEATURE_BACKUP_MOODLE2)) {
50             throw new backup_task_exception('activity_task_activity_lacks_moodle2_backup_support', $coursemodule->modname);
51         }
53         $this->moduleid   = $moduleid;
54         $this->sectionid  = $coursemodule->section;
55         $this->modulename = $coursemodule->modname;
56         $this->activityid = $coursemodule->instance;
57         $this->contextid  = get_context_instance(CONTEXT_MODULE, $this->moduleid)->id;
59         parent::__construct($name, $plan);
60     }
62     public function get_moduleid() {
63         return $this->moduleid;
64     }
66     public function get_sectionid() {
67         return $this->sectionid;
68     }
70     public function get_modulename() {
71         return $this->modulename;
72     }
74     public function get_activityid() {
75         return $this->activityid;
76     }
78     public function get_contextid() {
79         return $this->contextid;
80     }
82     /**
83      * Activity tasks have their own directory to write files
84      */
85     public function get_taskbasepath() {
86         return $this->get_basepath() . '/activities/' . $this->modulename . '_' . $this->moduleid;
87     }
89     /**
90      * Create all the steps that will be part of this task
91      */
92     public function build() {
94         // If we have decided not to backup activities, prevent anything to be built
95         if (!$this->get_setting_value('activities')) {
96             $this->built = true;
97             return;
98         }
100         // Add some extra settings that related processors are going to need
101         $this->add_setting(new backup_activity_generic_setting(backup::VAR_MODID, base_setting::IS_INTEGER, $this->moduleid));
102         $this->add_setting(new backup_activity_generic_setting(backup::VAR_COURSEID, base_setting::IS_INTEGER, $this->get_courseid()));
103         $this->add_setting(new backup_activity_generic_setting(backup::VAR_SECTIONID, base_setting::IS_INTEGER, $this->sectionid));
104         $this->add_setting(new backup_activity_generic_setting(backup::VAR_MODNAME, base_setting::IS_FILENAME, $this->modulename));
105         $this->add_setting(new backup_activity_generic_setting(backup::VAR_ACTIVITYID, base_setting::IS_INTEGER, $this->activityid));
106         $this->add_setting(new backup_activity_generic_setting(backup::VAR_CONTEXTID, base_setting::IS_INTEGER, $this->contextid));
108         // Create the activity directory
109         $this->add_step(new create_taskbasepath_directory('create_activity_directory'));
111         // Generate the module.xml file, containing general information for the
112         // activity and from its related course_modules record and availability
113         $this->add_step(new backup_module_structure_step('module_info', 'module.xml'));
115         // Annotate the groups used in already annotated groupings
116         $this->add_step(new backup_annotate_groups_from_groupings('annotate_groups'));
118         // Here we add all the common steps for any activity and, in the point of interest
119         // we call to define_my_steps() is order to get the particular ones inserted in place.
120         $this->define_my_steps();
122         // Generate the roles file (optionally role assignments and always role overrides)
123         $this->add_step(new backup_roles_structure_step('activity_roles', 'roles.xml'));
125         // Generate the filter file (conditionally)
126         if ($this->get_setting_value('filters')) {
127             $this->add_step(new backup_filters_structure_step('activity_filters', 'filters.xml'));
128         }
130         // Generate the comments file (conditionally)
131         if ($this->get_setting_value('comments')) {
132             $this->add_step(new backup_comments_structure_step('activity_comments', 'comments.xml'));
133         }
135         // Generate the userscompletion file (conditionally)
136         if ($this->get_setting_value('userscompletion')) {
137             $this->add_step(new backup_userscompletion_structure_step('activity_userscompletion', 'completion.xml'));
138         }
140         // Generate the logs file (conditionally)
141         if ($this->get_setting_value('logs')) {
142             $this->add_step(new backup_activity_logs_structure_step('activity_logs', 'logs.xml'));
143         }
145         // Fetch all the activity grade items and put them to backup_ids
146         $this->add_step(new backup_activity_grade_items_to_ids('fetch_activity_grade_items'));
148         // Generate the grades file
149         $this->add_step(new backup_activity_grades_structure_step('activity_grades', 'grades.xml'));
151         // Annotate the scales used in already annotated outcomes
152         $this->add_step(new backup_annotate_scales_from_outcomes('annotate_scales'));
154         // NOTE: Historical grade information is saved completely at course level only (see 1.9)
155         // not per activity nor per selected activities (all or nothing).
157         // Generate the inforef file (must be after ALL steps gathering annotations of ANY type)
158         $this->add_step(new backup_inforef_structure_step('activity_inforef', 'inforef.xml'));
160         // Migrate the already exported inforef entries to final ones
161         $this->add_step(new move_inforef_annotations_to_final('migrate_inforef'));
163         // At the end, mark it as built
164         $this->built = true;
165     }
167     /**
168      * Exceptionally override the execute method, so, based in the activity_included setting, we are able
169      * to skip the execution of one task completely
170      */
171     public function execute() {
173         // Find activity_included_setting
174         if (!$this->get_setting_value('included')) {
175             $this->log('activity skipped by _included setting', backup::LOG_DEBUG, $this->name);
176             $this->plan->set_excluding_activities();
177         } else { // Setting tells us it's ok to execute
178             parent::execute();
179         }
180     }
183     /**
184      * Specialisation that, first of all, looks for the setting within
185      * the task with the the prefix added and later, delegates to parent
186      * without adding anything
187      */
188     public function get_setting($name) {
189         $namewithprefix = $this->modulename . '_' . $this->moduleid . '_' . $name;
190         $result = null;
191         foreach ($this->settings as $key => $setting) {
192             if ($setting->get_name() == $namewithprefix) {
193                 if ($result != null) {
194                     throw new base_task_exception('multiple_settings_by_name_found', $namewithprefix);
195                 } else {
196                     $result = $setting;
197                 }
198             }
199         }
200         if ($result) {
201             return $result;
202         } else {
203             // Fallback to parent
204             return parent::get_setting($name);
205         }
206     }
208 // Protected API starts here
210     /**
211      * Define the common setting that any backup activity will have
212      */
213     protected function define_settings() {
215         // All the settings related to this activity will include this prefix
216         $settingprefix = $this->modulename . '_' . $this->moduleid . '_';
218         // All these are common settings to be shared by all activities
220         // Define activity_include (to decide if the whole task must be really executed)
221         // Dependent of:
222         // - activities root setting
223         // - section_included setting (if exists)
224         $settingname = $settingprefix . 'included';
225         $activity_included = new backup_activity_generic_setting($settingname, base_setting::IS_BOOLEAN, true);
226         $activity_included->get_ui()->set_icon(new pix_icon('icon', get_string('pluginname', $this->modulename), $this->modulename));
227         $this->add_setting($activity_included);
228         // Look for "activities" root setting
229         $activities = $this->plan->get_setting('activities');
230         $activities->add_dependency($activity_included);
231         // Look for "section_included" section setting (if exists)
232         $settingname = 'section_' . $this->sectionid . '_included';
233         if ($this->plan->setting_exists($settingname)) {
234             $section_included = $this->plan->get_setting($settingname);
235             $section_included->add_dependency($activity_included);
236         }
238         // Define activity_userinfo. Dependent of:
239         // - users root setting
240         // - section_userinfo setting (if exists)
241         // - activity_included setting
242         $settingname = $settingprefix . 'userinfo';
243         $activity_userinfo = new backup_activity_userinfo_setting($settingname, base_setting::IS_BOOLEAN, true);
244         //$activity_userinfo->get_ui()->set_label(get_string('includeuserinfo','backup'));
245         $activity_userinfo->get_ui()->set_label('-');
246         $this->add_setting($activity_userinfo);
247         // Look for "users" root setting
248         $users = $this->plan->get_setting('users');
249         $users->add_dependency($activity_userinfo);
250         // Look for "section_userinfo" section setting (if exists)
251         $settingname = 'section_' . $this->sectionid . '_userinfo';
252         if ($this->plan->setting_exists($settingname)) {
253             $section_userinfo = $this->plan->get_setting($settingname);
254             $section_userinfo->add_dependency($activity_userinfo);
255         }
256         // Look for "activity_included" setting
257         $activity_included->add_dependency($activity_userinfo);
259         // End of common activity settings, let's add the particular ones
260         $this->define_my_settings();
261     }
263     /**
264      * Define (add) particular settings that each activity can have
265      */
266     abstract protected function define_my_settings();
268     /**
269      * Define (add) particular steps that each activity can have
270      */
271     abstract protected function define_my_steps();
273     /**
274      * Code the transformations to perform in the activity in
275      * order to get transportable (encoded) links
276      */
277     static public function encode_content_links($content) {
278         throw new coding_exception('encode_content_links() method needs to be overridden in each subclass of backup_activity_task');
279     }