Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
    /**
1441 ariadna 104
     * Return if the activity is inside a subsection.
105
     *
106
     * @return bool
107
     */
108
    public function is_in_subsection(): bool {
109
        return !empty($this->info->insubsection);
110
    }
111
 
112
    /**
1 efrain 113
     * Returns the old course module id (cmid of activity which will be restored)
114
     *
115
     * @return int
116
     */
117
    public function get_old_moduleid() {
118
        return $this->oldmoduleid;
119
    }
120
 
121
    public function get_old_moduleversion() {
122
        return $this->oldmoduleversion;
123
    }
124
 
125
    public function get_activityid() {
126
        return $this->activityid;
127
    }
128
 
129
    public function get_old_activityid() {
130
        return $this->oldactivityid;
131
    }
132
 
133
    public function get_contextid() {
134
        return $this->contextid;
135
    }
136
 
137
    public function get_old_contextid() {
138
        return $this->oldcontextid;
139
    }
140
 
141
    /**
142
     * Create all the steps that will be part of this task
143
     */
144
    public function build() {
145
 
146
        // If we have decided not to restore activities, prevent anything to be built
147
        if (!$this->get_setting_value('activities')) {
148
            $this->built = true;
149
            return;
150
        }
151
 
152
        // Load he course_module estructure, generating it (with instance = 0)
153
        // but allowing the creation of the target context needed in following steps
154
        $this->add_step(new restore_module_structure_step('module_info', 'module.xml'));
155
 
156
        // Here we add all the common steps for any activity and, in the point of interest
157
        // we call to define_my_steps() is order to get the particular ones inserted in place.
158
        $this->define_my_steps();
159
 
160
        // Roles (optionally role assignments and always role overrides)
161
        $this->add_step(new restore_ras_and_caps_structure_step('course_ras_and_caps', 'roles.xml'));
162
 
163
        // Filters (conditionally)
164
        if ($this->get_setting_value('filters')) {
165
            $this->add_step(new restore_filters_structure_step('activity_filters', 'filters.xml'));
166
        }
167
 
168
        // Comments (conditionally)
169
        if ($this->get_setting_value('comments')) {
170
            $this->add_step(new restore_comments_structure_step('activity_comments', 'comments.xml'));
171
        }
172
 
173
        // Calendar events (conditionally)
174
        if ($this->get_setting_value('calendarevents')) {
175
            $this->add_step(new restore_calendarevents_structure_step('activity_calendar', 'calendar.xml'));
176
        }
177
 
178
        // Grades (module-related, rest of gradebook is restored later if possible: cats, calculations...)
179
        $this->add_step(new restore_activity_grades_structure_step('activity_grades', 'grades.xml'));
180
 
181
        // Advanced grading methods attached to the module
182
        $this->add_step(new restore_activity_grading_structure_step('activity_grading', 'grading.xml'));
183
 
184
        // Grade history. The setting 'grade_history' is handled in the step.
185
        $this->add_step(new restore_activity_grade_history_structure_step('activity_grade_history', 'grade_history.xml'));
186
 
187
        // Userscompletion (conditionally)
188
        if ($this->get_setting_value('userscompletion')) {
189
            $this->add_step(new restore_userscompletion_structure_step('activity_userscompletion', 'completion.xml'));
190
        }
191
 
192
        // Logs (conditionally)
193
        if ($this->get_setting_value('logs')) {
194
            // Legacy logs.
195
            $this->add_step(new restore_activity_logs_structure_step('activity_logs', 'logs.xml'));
196
            // New log stores.
197
            $this->add_step(new restore_activity_logstores_structure_step('activity_logstores', 'logstores.xml'));
198
        }
199
 
200
        // Activity competencies.
201
        $this->add_step(new restore_activity_competencies_structure_step('activity_competencies', 'competencies.xml'));
202
 
203
        // Search reindexing, if enabled and if not restoring entire course.
204
        if (\core_search\manager::is_indexing_enabled()) {
205
            $wholecourse = $this->get_target() == backup::TARGET_NEW_COURSE;
206
            $wholecourse = $wholecourse || ($this->setting_exists('overwrite_conf') && $this->get_setting_value('overwrite_conf'));
207
            if (!$wholecourse) {
208
                $this->add_step(new restore_activity_search_index('activity_search_index'));
209
            }
210
        }
211
 
212
        // The xAPI state (conditionally).
213
        if ($this->get_setting_value('xapistate')) {
214
            $this->add_step(new restore_xapistate_structure_step('activity_xapistate', 'xapistate.xml'));
215
        }
216
 
217
        // At the end, mark it as built
218
        $this->built = true;
219
    }
220
 
221
    /**
222
     * Exceptionally override the execute method, so, based in the activity_included setting, we are able
223
     * to skip the execution of one task completely
224
     */
225
    public function execute() {
226
 
227
        // Find activity_included_setting
228
        if (!$this->get_setting_value('included')) {
229
            $this->log('activity skipped by _included setting', backup::LOG_DEBUG, $this->name);
230
            $this->plan->set_excluding_activities(); // Inform plan we are excluding actvities
231
 
232
        } else { // Setting tells us it's ok to execute
233
            parent::execute();
234
        }
235
    }
236
 
237
 
238
    /**
239
     * Specialisation that, first of all, looks for the setting within
240
     * the task with the the prefix added and later, delegates to parent
241
     * without adding anything
242
     */
243
    public function get_setting($name) {
244
        $namewithprefix = $this->info->modulename . '_' . $this->info->moduleid . '_' . $name;
245
        $result = null;
246
        foreach ($this->settings as $key => $setting) {
247
            if ($setting->get_name() == $namewithprefix) {
248
                if ($result != null) {
249
                    throw new base_task_exception('multiple_settings_by_name_found', $namewithprefix);
250
                } else {
251
                    $result = $setting;
252
                }
253
            }
254
        }
255
        if ($result) {
256
            return $result;
257
        } else {
258
            // Fallback to parent
259
            return parent::get_setting($name);
260
        }
261
    }
262
 
263
    /**
264
     * Define (add) particular steps that each activity can have
265
     */
266
    abstract protected function define_my_steps();
267
 
268
    /**
269
     * Define the contents in the activity that must be
270
     * processed by the link decoder
271
     */
272
    public static function define_decode_contents() {
273
        throw new coding_exception('define_decode_contents() method needs to be overridden in each subclass of restore_activity_task');
274
    }
275
 
276
    /**
277
     * Define the decoding rules for links belonging
278
     * to the activity to be executed by the link decoder
279
     */
280
    public static function define_decode_rules() {
281
        throw new coding_exception('define_decode_rules() method needs to be overridden in each subclass of restore_activity_task');
282
    }
283
 
284
    /**
285
     * Define the restore log rules that will be applied
286
     * by the {@link restore_logs_processor} when restoring
287
     * activity logs. It must return one array
288
     * of {@link restore_log_rule} objects
289
     */
290
    public static function define_restore_log_rules() {
291
        throw new coding_exception('define_restore_log_rules() method needs to be overridden in each subclass of restore_activity_task');
292
    }
293
 
294
// Protected API starts here
295
 
296
    /**
297
     * Define the common setting that any restore activity will have
298
     */
299
    protected function define_settings() {
300
        // All the settings related to this activity will include this prefix
301
        $settingprefix = $this->info->modulename . '_' . $this->info->moduleid . '_';
302
 
303
        // All these are common settings to be shared by all activities
1441 ariadna 304
        $activityincluded = $this->add_activity_included_setting($settingprefix);
305
        $this->add_activity_userinfo_setting($settingprefix, $activityincluded);
1 efrain 306
 
1441 ariadna 307
        // End of common activity settings, let's add the particular ones.
308
        $this->define_my_settings();
309
    }
310
 
311
    /**
312
     * Add the activity included setting to the task.
313
     *
314
     * @param string $settingprefix the identifier of the setting
315
     * @return activity_backup_setting the setting added
316
     */
317
    protected function add_activity_included_setting(string $settingprefix): activity_backup_setting {
318
        // Define activity_included (to decide if the whole task must be really executed)
1 efrain 319
        // Dependent of:
1441 ariadna 320
        // - activities root setting.
321
        // - sectionincluded setting (if exists).
1 efrain 322
        $settingname = $settingprefix . 'included';
1441 ariadna 323
 
324
        if ($this->is_in_subsection()) {
325
            $activityincluded = new restore_subactivity_generic_setting($settingname, base_setting::IS_BOOLEAN, true);
326
        } else {
327
            $activityincluded = new restore_activity_generic_setting($settingname, base_setting::IS_BOOLEAN, true);
328
        }
329
 
330
        $activityincluded->get_ui()->set_icon(new image_icon('monologo', get_string('pluginname', $this->modulename),
331
            $this->modulename, ['class' => 'ms-1']));
332
        $this->add_setting($activityincluded);
333
        // Look for "activities" root setting.
1 efrain 334
        $activities = $this->plan->get_setting('activities');
1441 ariadna 335
        $activities->add_dependency($activityincluded);
336
        // Look for "sectionincluded" section setting (if exists).
1 efrain 337
        $settingname = 'section_' . $this->info->sectionid . '_included';
338
        if ($this->plan->setting_exists($settingname)) {
1441 ariadna 339
            $sectionincluded = $this->plan->get_setting($settingname);
340
            $sectionincluded->add_dependency($activityincluded);
1 efrain 341
        }
342
 
1441 ariadna 343
        return $activityincluded;
344
    }
345
 
346
    /**
347
     * Add the activity userinfo setting to the task.
348
     *
349
     * @param string $settingprefix the identifier of the setting
350
     * @param activity_backup_setting $includefield the activity included setting
351
     * @return activity_backup_setting the setting added
352
     */
353
    protected function add_activity_userinfo_setting(
354
        string $settingprefix,
355
        activity_backup_setting $includefield
356
    ): activity_backup_setting {
357
        // Define activityuserinfo. Dependent of:
358
        // - users root setting.
359
        // - sectionuserinfo setting (if exists).
360
        // - activity included setting.
1 efrain 361
        $settingname = $settingprefix . 'userinfo';
362
        $defaultvalue = false;
363
        if (isset($this->info->settings[$settingname]) && $this->info->settings[$settingname]) { // Only enabled when available
364
            $defaultvalue = true;
365
        }
366
 
1441 ariadna 367
        if ($this->is_in_subsection()) {
368
            $activityuserinfo = new restore_subactivity_userinfo_setting($settingname, base_setting::IS_BOOLEAN, $defaultvalue);
369
        } else {
370
            $activityuserinfo = new restore_activity_userinfo_setting($settingname, base_setting::IS_BOOLEAN, $defaultvalue);
371
        }
372
 
1 efrain 373
        if (!$defaultvalue) {
374
            // This is a bit hacky, but if there is no user data to restore, then
375
            // we replace the standard check-box with a select menu with the
376
            // single choice 'No', and the select menu is clever enough that if
377
            // there is only one choice, it just displays a static string.
378
            //
379
            // It would probably be better design to have a special UI class
380
            // setting_ui_checkbox_or_no, rather than this hack, but I am not
381
            // going to do that today.
1441 ariadna 382
            $activityuserinfo->set_ui(
383
                new backup_setting_ui_select(
384
                    $activityuserinfo,
385
                    '-',
386
                    [0 => get_string('no')]
387
                )
388
            );
1 efrain 389
        } else {
1441 ariadna 390
            $activityuserinfo->get_ui()->set_label('-');
1 efrain 391
        }
392
 
1441 ariadna 393
        $this->add_setting($activityuserinfo);
1 efrain 394
 
1441 ariadna 395
        // Look for "users" root setting.
1 efrain 396
        $users = $this->plan->get_setting('users');
1441 ariadna 397
        $users->add_dependency($activityuserinfo);
1 efrain 398
 
1441 ariadna 399
        // Look for "sectionuserinfo" section setting (if exists).
1 efrain 400
        $settingname = 'section_' . $this->info->sectionid . '_userinfo';
401
        if ($this->plan->setting_exists($settingname)) {
1441 ariadna 402
            $sectionuserinfo = $this->plan->get_setting($settingname);
403
            $sectionuserinfo->add_dependency($activityuserinfo);
1 efrain 404
        }
405
 
1441 ariadna 406
        // Look for "activity included" setting.
407
        $includefield->add_dependency($activityuserinfo);
1 efrain 408
 
1441 ariadna 409
        return $activityuserinfo;
1 efrain 410
    }
411
 
412
    /**
413
     * Define (add) particular settings that each activity can have
414
     */
415
    abstract protected function define_my_settings();
416
}