MDL-21432 backup - restore of activity grade items added. Blame MDL-23701 for the...
[moodle.git] / backup / moodle2 / restore_activity_task.class.php
CommitLineData
864842aa
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 restored
28 *
29 * TODO: Finish phpdocs
30 */
31abstract class restore_activity_task extends restore_task {
32
33 protected $info; // info related to activity gathered from backup file
5ab8d2de
EL
34 protected $modulename; // name of the module
35 protected $moduleid; // new (target) id of the course module
36 protected $oldmoduleid; // old (original) id of the course module
37 protected $contextid; // new (target) context of the activity
38 protected $oldcontextid;// old (original) context of the activity
39 protected $activityid; // new (target) id of the activity
40 protected $oldactivityid;// old (original) id of the activity
864842aa
EL
41
42 /**
43 * Constructor - instantiates one object of this class
44 */
45 public function __construct($name, $info, $plan = null) {
46
47 $this->info = $info;
5ab8d2de
EL
48 $this->modulename = $this->info->modulename;
49 $this->moduleid = 0;
50 $this->oldmoduleid = $this->info->moduleid;
51 $this->contextid = 0;
52 $this->oldcontextid = 0;
53 $this->activityid = 0;
54 $this->oldactivityid = 0;
864842aa
EL
55 parent::__construct($name, $plan);
56 }
57
58 /**
59 * Activity tasks have their own directory to read files
60 */
61 public function get_taskbasepath() {
5ab8d2de
EL
62 return $this->get_basepath() . '/' . $this->info->directory;
63 }
64
65 public function set_moduleid($moduleid) {
66 $this->moduleid = $moduleid;
67 }
68
69 public function set_activityid($activityid) {
70 $this->activityid = $activityid;
71 }
72
73 public function set_old_activityid($activityid) {
74 $this->oldactivityid = $activityid;
75 }
76
77 public function set_contextid($contextid) {
78 $this->contextid = $contextid;
79 }
80
81 public function set_old_contextid($contextid) {
82 $this->oldcontextid = $contextid;
83 }
84
85 public function get_modulename() {
86 return $this->modulename;
87 }
88
89 public function get_moduleid() {
90 return $this->moduleid;
91 }
92
93 public function get_activityid() {
94 return $this->activityid;
95 }
96
97 public function get_old_activityid() {
98 return $this->oldactivityid;
99 }
100
101 public function get_contextid() {
102 return $this->contextid;
103 }
104
105 public function get_old_contextid() {
106 return $this->oldcontextid;
864842aa
EL
107 }
108
109 /**
110 * Create all the steps that will be part of this task
111 */
112 public function build() {
113
114 // If we have decided not to restore activities, prevent anything to be built
115 if (!$this->get_setting_value('activities')) {
116 $this->built = true;
117 return;
118 }
119
5ab8d2de
EL
120 // Load he course_module estructure, generating it (with instance = 0)
121 // but allowing the creation of the target context needed in following steps
122 $this->add_step(new restore_module_structure_step('module_info', 'module.xml'));
123
124 // Here we add all the common steps for any activity and, in the point of interest
125 // we call to define_my_steps() is order to get the particular ones inserted in place.
126 $this->define_my_steps();
127
128 // Roles (optionally role assignments and always role overrides)
129 $this->add_step(new restore_ras_and_caps_structure_step('course_ras_and_caps', 'roles.xml'));
130
131 // Filters (conditionally)
132 if ($this->get_setting_value('filters')) {
133 $this->add_step(new restore_filters_structure_step('activity_filters', 'filters.xml'));
134 }
135
136 // Comments (conditionally)
137 if ($this->get_setting_value('comments')) {
138 $this->add_step(new restore_comments_structure_step('activity_comments', 'comments.xml'));
139 }
140
9a1cfcbc
EL
141 // Grades (module-related, rest of gradebook is restored later if possible: cats, calculations...)
142 $this->add_step(new restore_activity_grades_structure_step('activity_grades', 'grades.xml'));
5ab8d2de
EL
143
144 // TODO: Userscompletion (conditionally)
145 if ($this->get_setting_value('userscompletion')) {
146 //$this->add_step(new restore_userscompletion_structure_step('activity_userscompletion', 'completion.xml'));
147 }
148
149 // TODO: Logs (conditionally)
150 if ($this->get_setting_value('logs')) {
151 //$this->add_step(new restore_activity_logs_structure_step('activity_logs', 'logs.xml'));
152 }
864842aa
EL
153
154 // At the end, mark it as built
155 $this->built = true;
156 }
157
158 /**
159 * Exceptionally override the execute method, so, based in the activity_included setting, we are able
160 * to skip the execution of one task completely
161 */
162 public function execute() {
163
164 // Find activity_included_setting
165 if (!$this->get_setting_value('included')) {
166 $this->log('activity skipped by _included setting', backup::LOG_DEBUG, $this->name);
167
168 } else { // Setting tells us it's ok to execute
169 parent::execute();
170 }
171 }
172
173
174 /**
175 * Specialisation that, first of all, looks for the setting within
176 * the task with the the prefix added and later, delegates to parent
177 * without adding anything
178 */
179 public function get_setting($name) {
180 $namewithprefix = $this->info->modulename . '_' . $this->info->moduleid . '_' . $name;
181 $result = null;
182 foreach ($this->settings as $key => $setting) {
183 if ($setting->get_name() == $namewithprefix) {
184 if ($result != null) {
185 throw new base_task_exception('multiple_settings_by_name_found', $namewithprefix);
186 } else {
187 $result = $setting;
188 }
189 }
190 }
191 if ($result) {
192 return $result;
193 } else {
194 // Fallback to parent
195 return parent::get_setting($name);
196 }
197 }
198
39aa0280
EL
199 /**
200 * Given a commment area, return the itemname that contains the itemid mappings
201 *
202 * By default both are the same (commentarea = itemname), so return it. If some
203 * module uses a different approach, this method can be overriden in its taks
204 */
205 public function get_comment_mapping_itemname($commentarea) {
206 return $commentarea;
207 }
208
5ab8d2de
EL
209 /**
210 * Define (add) particular steps that each activity can have
211 */
212 abstract protected function define_my_steps();
213
214 /**
215 * Define the contents in the activity that must be
216 * processed by the link decoder
217 */
218 abstract static public function define_decode_contents();
219
220 /**
221 * Define the decoding rules for links belonging
222 * to the activity to be executed by the link decoder
223 */
224 abstract static public function define_decode_rules();
225
864842aa
EL
226// Protected API starts here
227
228 /**
229 * Define the common setting that any restore activity will have
230 */
231 protected function define_settings() {
232
233 // All the settings related to this activity will include this prefix
234 $settingprefix = $this->info->modulename . '_' . $this->info->moduleid . '_';
235
236 // All these are common settings to be shared by all activities
237
238 // Define activity_include (to decide if the whole task must be really executed)
239 // Dependent of:
240 // - activities root setting
241 // - section_included setting (if exists)
242 $settingname = $settingprefix . 'included';
243 $activity_included = new restore_activity_generic_setting($settingname, base_setting::IS_BOOLEAN, true);
244 $this->add_setting($activity_included);
245 // Look for "activities" root setting
246 $activities = $this->plan->get_setting('activities');
247 $activities->add_dependency($activity_included);
248 // Look for "section_included" section setting (if exists)
249 $settingname = 'section_' . $this->info->sectionid . '_included';
250 if ($this->plan->setting_exists($settingname)) {
251 $section_included = $this->plan->get_setting($settingname);
252 $section_included->add_dependency($activity_included);
253 }
254
255 // Define activity_userinfo. Dependent of:
256 // - users root setting
257 // - section_userinfo setting (if exists)
258 // - activity_included setting
259 $settingname = $settingprefix . 'userinfo';
260 $selectvalues = array(0=>get_string('no')); // Safer options
261 $defaultvalue = false; // Safer default
5ab8d2de 262 if (isset($this->info->settings[$settingname]) && $this->info->settings[$settingname]) { // Only enabled when available
864842aa
EL
263 $selectvalues = array(1=>get_string('yes'), 0=>get_string('no'));
264 $defaultvalue = true;
265 }
266 $activity_userinfo = new restore_activity_userinfo_setting($settingname, base_setting::IS_BOOLEAN, $defaultvalue);
267 $activity_userinfo->set_ui(new backup_setting_ui_select($activity_userinfo, get_string('includeuserinfo','backup'), $selectvalues));
268 $this->add_setting($activity_userinfo);
269 // Look for "users" root setting
270 $users = $this->plan->get_setting('users');
271 $users->add_dependency($activity_userinfo);
272 // Look for "section_userinfo" section setting (if exists)
273 $settingname = 'section_' . $this->info->sectionid . '_userinfo';
274 if ($this->plan->setting_exists($settingname)) {
275 $section_userinfo = $this->plan->get_setting($settingname);
276 $section_userinfo->add_dependency($activity_userinfo);
277 }
278 // Look for "activity_included" setting
279 $activity_included->add_dependency($activity_userinfo);
280
281 // End of common activity settings, let's add the particular ones
282 $this->define_my_settings();
283 }
284
285 /**
286 * Define (add) particular settings that each activity can have
287 */
288 abstract protected function define_my_settings();
864842aa 289}