Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * Defines restore_activity_task class
20
 *
21
 * @package     core_backup
22
 * @subpackage  moodle2
23
 * @category    backup
24
 * @copyright   2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
25
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
27
 
28
defined('MOODLE_INTERNAL') || die();
29
 
30
/**
31
 * abstract activity task that provides all the properties and common tasks to be performed
32
 * when one activity is being restored
33
 *
34
 * TODO: Finish phpdocs
35
 */
36
abstract class restore_activity_task extends restore_task {
37
 
38
    protected $info; // info related to activity gathered from backup file
39
    protected $modulename;  // name of the module
40
    protected $moduleid;    // new (target) id of the course module
41
    protected $oldmoduleid; // old (original) id of the course module
42
    protected $oldmoduleversion; // old (original) version of the module
43
    protected $contextid;   // new (target) context of the activity
44
    protected $oldcontextid;// old (original) context of the activity
45
    protected $activityid;  // new (target) id of the activity
46
    protected $oldactivityid;// old (original) id of the activity
47
 
48
    /**
49
     * Constructor - instantiates one object of this class
50
     */
51
    public function __construct($name, $info, $plan = null) {
52
        $this->info = $info;
53
        $this->modulename = $this->info->modulename;
54
        $this->moduleid = 0;
55
        $this->oldmoduleid = $this->info->moduleid;
56
        $this->oldmoduleversion = 0;
57
        $this->contextid = 0;
58
        $this->oldcontextid = 0;
59
        $this->activityid = 0;
60
        $this->oldactivityid = 0;
61
        parent::__construct($name, $plan);
62
    }
63
 
64
    /**
65
     * Activity tasks have their own directory to read files
66
     */
67
    public function get_taskbasepath() {
68
        return $this->get_basepath() . '/' . $this->info->directory;
69
    }
70
 
71
    public function set_moduleid($moduleid) {
72
        $this->moduleid = $moduleid;
73
    }
74
 
75
    public function set_old_moduleversion($oldmoduleversion) {
76
        $this->oldmoduleversion = $oldmoduleversion;
77
    }
78
 
79
    public function set_activityid($activityid) {
80
        $this->activityid = $activityid;
81
    }
82
 
83
    public function set_old_activityid($activityid) {
84
        $this->oldactivityid = $activityid;
85
    }
86
 
87
    public function set_contextid($contextid) {
88
        $this->contextid = $contextid;
89
    }
90
 
91
    public function set_old_contextid($contextid) {
92
        $this->oldcontextid = $contextid;
93
    }
94
 
95
    public function get_modulename() {
96
        return $this->modulename;
97
    }
98
 
99
    public function get_moduleid() {
100
        return $this->moduleid;
101
    }
102
 
103
    /**
104
     * Returns the old course module id (cmid of activity which will be restored)
105
     *
106
     * @return int
107
     */
108
    public function get_old_moduleid() {
109
        return $this->oldmoduleid;
110
    }
111
 
112
    public function get_old_moduleversion() {
113
        return $this->oldmoduleversion;
114
    }
115
 
116
    public function get_activityid() {
117
        return $this->activityid;
118
    }
119
 
120
    public function get_old_activityid() {
121
        return $this->oldactivityid;
122
    }
123
 
124
    public function get_contextid() {
125
        return $this->contextid;
126
    }
127
 
128
    public function get_old_contextid() {
129
        return $this->oldcontextid;
130
    }
131
 
132
    /**
133
     * Create all the steps that will be part of this task
134
     */
135
    public function build() {
136
 
137
        // If we have decided not to restore activities, prevent anything to be built
138
        if (!$this->get_setting_value('activities')) {
139
            $this->built = true;
140
            return;
141
        }
142
 
143
        // Load he course_module estructure, generating it (with instance = 0)
144
        // but allowing the creation of the target context needed in following steps
145
        $this->add_step(new restore_module_structure_step('module_info', 'module.xml'));
146
 
147
        // Here we add all the common steps for any activity and, in the point of interest
148
        // we call to define_my_steps() is order to get the particular ones inserted in place.
149
        $this->define_my_steps();
150
 
151
        // Roles (optionally role assignments and always role overrides)
152
        $this->add_step(new restore_ras_and_caps_structure_step('course_ras_and_caps', 'roles.xml'));
153
 
154
        // Filters (conditionally)
155
        if ($this->get_setting_value('filters')) {
156
            $this->add_step(new restore_filters_structure_step('activity_filters', 'filters.xml'));
157
        }
158
 
159
        // Comments (conditionally)
160
        if ($this->get_setting_value('comments')) {
161
            $this->add_step(new restore_comments_structure_step('activity_comments', 'comments.xml'));
162
        }
163
 
164
        // Calendar events (conditionally)
165
        if ($this->get_setting_value('calendarevents')) {
166
            $this->add_step(new restore_calendarevents_structure_step('activity_calendar', 'calendar.xml'));
167
        }
168
 
169
        // Grades (module-related, rest of gradebook is restored later if possible: cats, calculations...)
170
        $this->add_step(new restore_activity_grades_structure_step('activity_grades', 'grades.xml'));
171
 
172
        // Advanced grading methods attached to the module
173
        $this->add_step(new restore_activity_grading_structure_step('activity_grading', 'grading.xml'));
174
 
175
        // Grade history. The setting 'grade_history' is handled in the step.
176
        $this->add_step(new restore_activity_grade_history_structure_step('activity_grade_history', 'grade_history.xml'));
177
 
178
        // Userscompletion (conditionally)
179
        if ($this->get_setting_value('userscompletion')) {
180
            $this->add_step(new restore_userscompletion_structure_step('activity_userscompletion', 'completion.xml'));
181
        }
182
 
183
        // Logs (conditionally)
184
        if ($this->get_setting_value('logs')) {
185
            // Legacy logs.
186
            $this->add_step(new restore_activity_logs_structure_step('activity_logs', 'logs.xml'));
187
            // New log stores.
188
            $this->add_step(new restore_activity_logstores_structure_step('activity_logstores', 'logstores.xml'));
189
        }
190
 
191
        // Activity competencies.
192
        $this->add_step(new restore_activity_competencies_structure_step('activity_competencies', 'competencies.xml'));
193
 
194
        // Search reindexing, if enabled and if not restoring entire course.
195
        if (\core_search\manager::is_indexing_enabled()) {
196
            $wholecourse = $this->get_target() == backup::TARGET_NEW_COURSE;
197
            $wholecourse = $wholecourse || ($this->setting_exists('overwrite_conf') && $this->get_setting_value('overwrite_conf'));
198
            if (!$wholecourse) {
199
                $this->add_step(new restore_activity_search_index('activity_search_index'));
200
            }
201
        }
202
 
203
        // The xAPI state (conditionally).
204
        if ($this->get_setting_value('xapistate')) {
205
            $this->add_step(new restore_xapistate_structure_step('activity_xapistate', 'xapistate.xml'));
206
        }
207
 
208
        // At the end, mark it as built
209
        $this->built = true;
210
    }
211
 
212
    /**
213
     * Exceptionally override the execute method, so, based in the activity_included setting, we are able
214
     * to skip the execution of one task completely
215
     */
216
    public function execute() {
217
 
218
        // Find activity_included_setting
219
        if (!$this->get_setting_value('included')) {
220
            $this->log('activity skipped by _included setting', backup::LOG_DEBUG, $this->name);
221
            $this->plan->set_excluding_activities(); // Inform plan we are excluding actvities
222
 
223
        } else { // Setting tells us it's ok to execute
224
            parent::execute();
225
        }
226
    }
227
 
228
 
229
    /**
230
     * Specialisation that, first of all, looks for the setting within
231
     * the task with the the prefix added and later, delegates to parent
232
     * without adding anything
233
     */
234
    public function get_setting($name) {
235
        $namewithprefix = $this->info->modulename . '_' . $this->info->moduleid . '_' . $name;
236
        $result = null;
237
        foreach ($this->settings as $key => $setting) {
238
            if ($setting->get_name() == $namewithprefix) {
239
                if ($result != null) {
240
                    throw new base_task_exception('multiple_settings_by_name_found', $namewithprefix);
241
                } else {
242
                    $result = $setting;
243
                }
244
            }
245
        }
246
        if ($result) {
247
            return $result;
248
        } else {
249
            // Fallback to parent
250
            return parent::get_setting($name);
251
        }
252
    }
253
 
254
    /**
255
     * Define (add) particular steps that each activity can have
256
     */
257
    abstract protected function define_my_steps();
258
 
259
    /**
260
     * Define the contents in the activity that must be
261
     * processed by the link decoder
262
     */
263
    public static function define_decode_contents() {
264
        throw new coding_exception('define_decode_contents() method needs to be overridden in each subclass of restore_activity_task');
265
    }
266
 
267
    /**
268
     * Define the decoding rules for links belonging
269
     * to the activity to be executed by the link decoder
270
     */
271
    public static function define_decode_rules() {
272
        throw new coding_exception('define_decode_rules() method needs to be overridden in each subclass of restore_activity_task');
273
    }
274
 
275
    /**
276
     * Define the restore log rules that will be applied
277
     * by the {@link restore_logs_processor} when restoring
278
     * activity logs. It must return one array
279
     * of {@link restore_log_rule} objects
280
     */
281
    public static function define_restore_log_rules() {
282
        throw new coding_exception('define_restore_log_rules() method needs to be overridden in each subclass of restore_activity_task');
283
    }
284
 
285
// Protected API starts here
286
 
287
    /**
288
     * Define the common setting that any restore activity will have
289
     */
290
    protected function define_settings() {
291
 
292
        // All the settings related to this activity will include this prefix
293
        $settingprefix = $this->info->modulename . '_' . $this->info->moduleid . '_';
294
 
295
        // All these are common settings to be shared by all activities
296
 
297
        // Define activity_include (to decide if the whole task must be really executed)
298
        // Dependent of:
299
        // - activities root setting
300
        // - section_included setting (if exists)
301
        $settingname = $settingprefix . 'included';
302
        $activity_included = new restore_activity_generic_setting($settingname, base_setting::IS_BOOLEAN, true);
303
        $activity_included->get_ui()->set_icon(new image_icon('monologo', get_string('pluginname', $this->modulename),
304
            $this->modulename, array('class' => 'iconlarge icon-post ml-1')));
305
        $this->add_setting($activity_included);
306
        // Look for "activities" root setting
307
        $activities = $this->plan->get_setting('activities');
308
        $activities->add_dependency($activity_included);
309
        // Look for "section_included" section setting (if exists)
310
        $settingname = 'section_' . $this->info->sectionid . '_included';
311
        if ($this->plan->setting_exists($settingname)) {
312
            $section_included = $this->plan->get_setting($settingname);
313
            $section_included->add_dependency($activity_included);
314
        }
315
 
316
        // Define activity_userinfo. Dependent of:
317
        // - users root setting
318
        // - section_userinfo setting (if exists)
319
        // - activity_included setting.
320
        $settingname = $settingprefix . 'userinfo';
321
        $defaultvalue = false;
322
        if (isset($this->info->settings[$settingname]) && $this->info->settings[$settingname]) { // Only enabled when available
323
            $defaultvalue = true;
324
        }
325
 
326
        $activity_userinfo = new restore_activity_userinfo_setting($settingname, base_setting::IS_BOOLEAN, $defaultvalue);
327
        if (!$defaultvalue) {
328
            // This is a bit hacky, but if there is no user data to restore, then
329
            // we replace the standard check-box with a select menu with the
330
            // single choice 'No', and the select menu is clever enough that if
331
            // there is only one choice, it just displays a static string.
332
            //
333
            // It would probably be better design to have a special UI class
334
            // setting_ui_checkbox_or_no, rather than this hack, but I am not
335
            // going to do that today.
336
            $activity_userinfo->set_ui(new backup_setting_ui_select($activity_userinfo, '-',
337
                    array(0 => get_string('no'))));
338
        } else {
339
            $activity_userinfo->get_ui()->set_label('-');
340
        }
341
 
342
        $this->add_setting($activity_userinfo);
343
 
344
        // Look for "users" root setting
345
        $users = $this->plan->get_setting('users');
346
        $users->add_dependency($activity_userinfo);
347
 
348
        // Look for "section_userinfo" section setting (if exists)
349
        $settingname = 'section_' . $this->info->sectionid . '_userinfo';
350
        if ($this->plan->setting_exists($settingname)) {
351
            $section_userinfo = $this->plan->get_setting($settingname);
352
            $section_userinfo->add_dependency($activity_userinfo);
353
        }
354
 
355
        // Look for "activity_included" setting.
356
        $activity_included->add_dependency($activity_userinfo);
357
 
358
        // End of common activity settings, let's add the particular ones.
359
        $this->define_my_settings();
360
    }
361
 
362
    /**
363
     * Define (add) particular settings that each activity can have
364
     */
365
    abstract protected function define_my_settings();
366
}