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-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
26 * abstract activity task that provides all the properties and common tasks to be performed
27 * when one activity is being backup
29 * TODO: Finish phpdocs
31 abstract class backup_activity_task extends backup_task {
35 protected $modulename;
36 protected $activityid;
40 * Constructor - instantiates one object of this class
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);
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);
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);
62 public function get_moduleid() {
63 return $this->moduleid;
66 public function get_sectionid() {
67 return $this->sectionid;
70 public function get_modulename() {
71 return $this->modulename;
74 public function get_activityid() {
75 return $this->activityid;
78 public function get_contextid() {
79 return $this->contextid;
83 * Activity tasks have their own directory to write files
85 public function get_taskbasepath() {
86 return $this->get_basepath() . '/activities/' . $this->modulename . '_' . $this->moduleid;
90 * Create all the steps that will be part of this task
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')) {
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'));
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'));
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'));
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'));
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 // Generate the grading file (conditionally)
152 $this->add_step(new backup_activity_grading_structure_step('activity_grading', 'grading.xml'));
154 // Annotate the scales used in already annotated outcomes
155 $this->add_step(new backup_annotate_scales_from_outcomes('annotate_scales'));
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).
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'));
163 // Migrate the already exported inforef entries to final ones
164 $this->add_step(new move_inforef_annotations_to_final('migrate_inforef'));
166 // At the end, mark it as built
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
174 public function execute() {
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);
179 $this->plan->set_excluding_activities();
180 } else { // Setting tells us it's ok to execute
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
191 public function get_setting($name) {
192 $namewithprefix = $this->modulename . '_' . $this->moduleid . '_' . $name;
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);
206 // Fallback to parent
207 return parent::get_setting($name);
211 // Protected API starts here
214 * Define the common setting that any backup activity will have
216 protected function define_settings() {
218 // All the settings related to this activity will include this prefix
219 $settingprefix = $this->modulename . '_' . $this->moduleid . '_';
221 // All these are common settings to be shared by all activities
223 // Define activity_include (to decide if the whole task must be really executed)
225 // - activities root setting
226 // - section_included setting (if exists)
227 $settingname = $settingprefix . 'included';
228 $activity_included = new backup_activity_generic_setting($settingname, base_setting::IS_BOOLEAN, true);
229 $activity_included->get_ui()->set_icon(new pix_icon('icon', get_string('pluginname', $this->modulename), $this->modulename));
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);
241 // Define activity_userinfo. Dependent of:
242 // - users root setting
243 // - section_userinfo setting (if exists)
244 // - activity_included setting
245 $settingname = $settingprefix . 'userinfo';
246 $activity_userinfo = new backup_activity_userinfo_setting($settingname, base_setting::IS_BOOLEAN, true);
247 //$activity_userinfo->get_ui()->set_label(get_string('includeuserinfo','backup'));
248 $activity_userinfo->get_ui()->set_label('-');
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);
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);
259 // Look for "activity_included" setting
260 $activity_included->add_dependency($activity_userinfo);
262 // End of common activity settings, let's add the particular ones
263 $this->define_my_settings();
267 * Define (add) particular settings that each activity can have
269 abstract protected function define_my_settings();
272 * Define (add) particular steps that each activity can have
274 abstract protected function define_my_steps();
277 * Code the transformations to perform in the activity in
278 * order to get transportable (encoded) links
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');