MDL-29793 Advanced grading forms attached to activity modules are included in the...
[moodle.git] / backup / moodle2 / backup_activity_task.class.php
CommitLineData
77547b46
EL
1<?php
2
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/>.
17
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 */
24
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 */
31abstract class backup_activity_task extends backup_task {
32
33 protected $moduleid;
34 protected $sectionid;
35 protected $modulename;
36 protected $activityid;
37 protected $contextid;
38
39 /**
40 * Constructor - instantiates one object of this class
41 */
42 public function __construct($name, $moduleid, $plan = null) {
43
44 // Check moduleid exists
45 if (!$coursemodule = get_coursemodule_from_id(false, $moduleid)) {
2d3522ae 46 throw new backup_task_exception('activity_task_coursemodule_not_found', $moduleid);
77547b46
EL
47 }
48 // Check activity supports this moodle2 backup format
49 if (!plugin_supports('mod', $coursemodule->modname, FEATURE_BACKUP_MOODLE2)) {
2d3522ae 50 throw new backup_task_exception('activity_task_activity_lacks_moodle2_backup_support', $coursemodule->modname);
77547b46
EL
51 }
52
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;
58
59 parent::__construct($name, $plan);
60 }
61
62 public function get_moduleid() {
63 return $this->moduleid;
64 }
65
66 public function get_sectionid() {
67 return $this->sectionid;
68 }
69
70 public function get_modulename() {
71 return $this->modulename;
72 }
73
74 public function get_activityid() {
75 return $this->activityid;
76 }
77
78 public function get_contextid() {
79 return $this->contextid;
80 }
81
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 }
88
89 /**
90 * Create all the steps that will be part of this task
91 */
92 public function build() {
93
1c9e93c7
EL
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 }
99
77547b46
EL
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));
107
108 // Create the activity directory
109 $this->add_step(new create_taskbasepath_directory('create_activity_directory'));
110
39b5371c 111 // Generate the module.xml file, containing general information for the
77547b46
EL
112 // activity and from its related course_modules record and availability
113 $this->add_step(new backup_module_structure_step('module_info', 'module.xml'));
114
115 // Annotate the groups used in already annotated groupings
116 $this->add_step(new backup_annotate_groups_from_groupings('annotate_groups'));
117
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();
121
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'));
124
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 }
129
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 }
134
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 }
139
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 }
144
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'));
147
148 // Generate the grades file
149 $this->add_step(new backup_activity_grades_structure_step('activity_grades', 'grades.xml'));
150
9b553d47
DM
151 // Generate the grading file (conditionally)
152 $this->add_step(new backup_activity_grading_structure_step('activity_grading', 'grading.xml'));
153
77547b46
EL
154 // Annotate the scales used in already annotated outcomes
155 $this->add_step(new backup_annotate_scales_from_outcomes('annotate_scales'));
156
157 // NOTE: Historical grade information is saved completely at course level only (see 1.9)
158 // not per activity nor per selected activities (all or nothing).
159
160 // Generate the inforef file (must be after ALL steps gathering annotations of ANY type)
161 $this->add_step(new backup_inforef_structure_step('activity_inforef', 'inforef.xml'));
162
163 // Migrate the already exported inforef entries to final ones
164 $this->add_step(new move_inforef_annotations_to_final('migrate_inforef'));
165
166 // At the end, mark it as built
167 $this->built = true;
168 }
169
170 /**
171 * Exceptionally override the execute method, so, based in the activity_included setting, we are able
172 * to skip the execution of one task completely
173 */
174 public function execute() {
175
176 // Find activity_included_setting
177 if (!$this->get_setting_value('included')) {
178 $this->log('activity skipped by _included setting', backup::LOG_DEBUG, $this->name);
bd39b6f2 179 $this->plan->set_excluding_activities();
77547b46
EL
180 } else { // Setting tells us it's ok to execute
181 parent::execute();
182 }
183 }
184
185
186 /**
187 * Specialisation that, first of all, looks for the setting within
188 * the task with the the prefix added and later, delegates to parent
189 * without adding anything
190 */
191 public function get_setting($name) {
192 $namewithprefix = $this->modulename . '_' . $this->moduleid . '_' . $name;
193 $result = null;
194 foreach ($this->settings as $key => $setting) {
195 if ($setting->get_name() == $namewithprefix) {
196 if ($result != null) {
197 throw new base_task_exception('multiple_settings_by_name_found', $namewithprefix);
198 } else {
199 $result = $setting;
200 }
201 }
202 }
203 if ($result) {
204 return $result;
205 } else {
206 // Fallback to parent
207 return parent::get_setting($name);
208 }
209 }
210
211// Protected API starts here
212
213 /**
214 * Define the common setting that any backup activity will have
215 */
216 protected function define_settings() {
217
218 // All the settings related to this activity will include this prefix
219 $settingprefix = $this->modulename . '_' . $this->moduleid . '_';
220
221 // All these are common settings to be shared by all activities
222
223 // Define activity_include (to decide if the whole task must be really executed)
b65e0220
EL
224 // Dependent of:
225 // - activities root setting
226 // - section_included setting (if exists)
77547b46 227 $settingname = $settingprefix . 'included';
1c9e93c7 228 $activity_included = new backup_activity_generic_setting($settingname, base_setting::IS_BOOLEAN, true);
83c000d9 229 $activity_included->get_ui()->set_icon(new pix_icon('icon', get_string('pluginname', $this->modulename), $this->modulename));
1c9e93c7
EL
230 $this->add_setting($activity_included);
231 // Look for "activities" root setting
232 $activities = $this->plan->get_setting('activities');
233 $activities->add_dependency($activity_included);
234 // Look for "section_included" section setting (if exists)
235 $settingname = 'section_' . $this->sectionid . '_included';
236 if ($this->plan->setting_exists($settingname)) {
237 $section_included = $this->plan->get_setting($settingname);
238 $section_included->add_dependency($activity_included);
239 }
77547b46 240
b65e0220
EL
241 // Define activity_userinfo. Dependent of:
242 // - users root setting
243 // - section_userinfo setting (if exists)
244 // - activity_included setting
77547b46 245 $settingname = $settingprefix . 'userinfo';
77547b46 246 $activity_userinfo = new backup_activity_userinfo_setting($settingname, base_setting::IS_BOOLEAN, true);
83c000d9
SH
247 //$activity_userinfo->get_ui()->set_label(get_string('includeuserinfo','backup'));
248 $activity_userinfo->get_ui()->set_label('-');
77547b46
EL
249 $this->add_setting($activity_userinfo);
250 // Look for "users" root setting
251 $users = $this->plan->get_setting('users');
252 $users->add_dependency($activity_userinfo);
1c9e93c7
EL
253 // Look for "section_userinfo" section setting (if exists)
254 $settingname = 'section_' . $this->sectionid . '_userinfo';
255 if ($this->plan->setting_exists($settingname)) {
256 $section_userinfo = $this->plan->get_setting($settingname);
257 $section_userinfo->add_dependency($activity_userinfo);
258 }
b65e0220
EL
259 // Look for "activity_included" setting
260 $activity_included->add_dependency($activity_userinfo);
77547b46
EL
261
262 // End of common activity settings, let's add the particular ones
263 $this->define_my_settings();
264 }
265
266 /**
267 * Define (add) particular settings that each activity can have
268 */
269 abstract protected function define_my_settings();
270
271 /**
272 * Define (add) particular steps that each activity can have
273 */
274 abstract protected function define_my_steps();
275
276 /**
277 * Code the transformations to perform in the activity in
278 * order to get transportable (encoded) links
279 */
7c109ea3
PS
280 static public function encode_content_links($content) {
281 throw new coding_exception('encode_content_links() method needs to be overridden in each subclass of backup_activity_task');
282 }
77547b46
EL
283
284}