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 backup_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
 * Provides all the settings and steps to perform one complete backup of the activity
32
 *
33
 * Activities are supposed to provide the subclass of this class in their file
34
 * mod/MODULENAME/backup/moodle2/backup_MODULENAME_activity_task.class.php
35
 * The expected name of the subclass is backup_MODULENAME_activity_task
36
 */
37
abstract class backup_activity_task extends backup_task {
38
 
39
    protected $moduleid;
40
    protected $sectionid;
1441 ariadna 41
 
42
    /** @var stdClass the section object */
43
    protected $section;
1 efrain 44
    protected $modulename;
45
    protected $activityid;
46
    protected $contextid;
47
 
48
    /**
49
     * Constructor - instantiates one object of this class
50
     *
51
     * @param string $name the task identifier
52
     * @param int $moduleid course module id (id in course_modules table)
53
     * @param backup_plan|null $plan the backup plan instance this task is part of
54
     */
55
    public function __construct($name, $moduleid, $plan = null) {
1441 ariadna 56
        global $DB;
1 efrain 57
 
58
        // Check moduleid exists
59
        if (!$coursemodule = get_coursemodule_from_id(false, $moduleid)) {
60
            throw new backup_task_exception('activity_task_coursemodule_not_found', $moduleid);
61
        }
62
        // Check activity supports this moodle2 backup format
63
        if (!plugin_supports('mod', $coursemodule->modname, FEATURE_BACKUP_MOODLE2)) {
64
            throw new backup_task_exception('activity_task_activity_lacks_moodle2_backup_support', $coursemodule->modname);
65
        }
66
 
67
        $this->moduleid   = $moduleid;
68
        $this->sectionid  = $coursemodule->section;
69
        $this->modulename = $coursemodule->modname;
70
        $this->activityid = $coursemodule->instance;
71
        $this->contextid  = context_module::instance($this->moduleid)->id;
1441 ariadna 72
        $this->section = $DB->get_record('course_sections', ['id' => $this->sectionid]);
1 efrain 73
 
74
        parent::__construct($name, $plan);
75
    }
76
 
77
    /**
78
     * @return int the course module id (id in the course_modules table)
79
     */
80
    public function get_moduleid() {
81
        return $this->moduleid;
82
    }
83
 
84
    /**
85
     * @return int the course section id (id in the course_sections table)
86
     */
87
    public function get_sectionid() {
88
        return $this->sectionid;
89
    }
90
 
91
    /**
92
     * @return string the name of the module, eg 'workshop' (from the modules table)
93
     */
94
    public function get_modulename() {
95
        return $this->modulename;
96
    }
97
 
98
    /**
1441 ariadna 99
     * Return if the activity is inside a subsection.
100
     *
101
     * @return bool
102
     */
103
    public function is_in_subsection(): bool {
104
        return !empty($this->section->component);
105
    }
106
 
107
 
108
    /**
1 efrain 109
     * @return int the id of the activity instance (id in the activity's instances table)
110
     */
111
    public function get_activityid() {
112
        return $this->activityid;
113
    }
114
 
115
    /**
116
     * @return int the id of the associated CONTEXT_MODULE instance
117
     */
118
    public function get_contextid() {
119
        return $this->contextid;
120
    }
121
 
122
    /**
123
     * @return string full path to the directory where this task writes its files
124
     */
125
    public function get_taskbasepath() {
126
        return $this->get_basepath() . '/activities/' . $this->modulename . '_' . $this->moduleid;
127
    }
128
 
129
    /**
130
     * Create all the steps that will be part of this task
131
     */
132
    public function build() {
133
 
134
        // If we have decided not to backup activities, prevent anything to be built
135
        if (!$this->get_setting_value('activities')) {
136
            $this->built = true;
137
            return;
138
        }
139
 
140
        // Add some extra settings that related processors are going to need
1441 ariadna 141
        $this->add_section_setting(backup::VAR_MODID, base_setting::IS_INTEGER, $this->moduleid);
142
        $this->add_section_setting(backup::VAR_COURSEID, base_setting::IS_INTEGER, $this->get_courseid());
143
        $this->add_section_setting(backup::VAR_SECTIONID, base_setting::IS_INTEGER, $this->sectionid);
144
        $this->add_section_setting(backup::VAR_MODNAME, base_setting::IS_FILENAME, $this->modulename);
145
        $this->add_section_setting(backup::VAR_ACTIVITYID, base_setting::IS_INTEGER, $this->activityid);
146
        $this->add_section_setting(backup::VAR_CONTEXTID, base_setting::IS_INTEGER, $this->contextid);
1 efrain 147
 
148
        // Create the activity directory
149
        $this->add_step(new create_taskbasepath_directory('create_activity_directory'));
150
 
151
        // Generate the module.xml file, containing general information for the
152
        // activity and from its related course_modules record and availability
153
        $this->add_step(new backup_module_structure_step('module_info', 'module.xml'));
154
 
155
        // Annotate the groups used in already annotated groupings if groups are to be backed up.
156
        if ($this->get_setting_value('groups')) {
157
            $this->add_step(new backup_annotate_groups_from_groupings('annotate_groups'));
158
        }
159
 
160
        // Here we add all the common steps for any activity and, in the point of interest
161
        // we call to define_my_steps() is order to get the particular ones inserted in place.
162
        $this->define_my_steps();
163
 
164
        // Generate the roles file (optionally role assignments and always role overrides)
165
        $this->add_step(new backup_roles_structure_step('activity_roles', 'roles.xml'));
166
 
167
        // Generate the filter file (conditionally)
168
        if ($this->get_setting_value('filters')) {
169
            $this->add_step(new backup_filters_structure_step('activity_filters', 'filters.xml'));
170
        }
171
 
172
        // Generate the comments file (conditionally)
173
        if ($this->get_setting_value('comments')) {
174
            $this->add_step(new backup_comments_structure_step('activity_comments', 'comments.xml'));
175
        }
176
 
177
        // Generate the userscompletion file (conditionally)
178
        if ($this->get_setting_value('userscompletion')) {
179
            $this->add_step(new backup_userscompletion_structure_step('activity_userscompletion', 'completion.xml'));
180
        }
181
 
182
        // Generate the logs file (conditionally)
183
        if ($this->get_setting_value('logs')) {
184
            // Legacy logs.
185
            $this->add_step(new backup_activity_logs_structure_step('activity_logs', 'logs.xml'));
186
            // New log stores.
187
            $this->add_step(new backup_activity_logstores_structure_step('activity_logstores', 'logstores.xml'));
188
        }
189
 
190
        // Generate the calendar events file (conditionally)
191
        if ($this->get_setting_value('calendarevents')) {
192
            $this->add_step(new backup_calendarevents_structure_step('activity_calendar', 'calendar.xml'));
193
        }
194
 
195
        // Fetch all the activity grade items and put them to backup_ids
196
        $this->add_step(new backup_activity_grade_items_to_ids('fetch_activity_grade_items'));
197
 
198
        // Generate the grades file
199
        $this->add_step(new backup_activity_grades_structure_step('activity_grades', 'grades.xml'));
200
 
201
        // Generate the grading file (conditionally)
202
        $this->add_step(new backup_activity_grading_structure_step('activity_grading', 'grading.xml'));
203
 
204
        // Generate the grade history file. The setting 'grade_histories' is handled in the step.
205
        $this->add_step(new backup_activity_grade_history_structure_step('activity_grade_history', 'grade_history.xml'));
206
 
207
        // Generate the competency file.
208
        $this->add_step(new backup_activity_competencies_structure_step('activity_competencies', 'competencies.xml'));
209
 
210
        // Annotate the scales used in already annotated outcomes
211
        $this->add_step(new backup_annotate_scales_from_outcomes('annotate_scales'));
212
 
213
        // NOTE: Historical grade information is saved completely at course level only (see 1.9)
214
        // not per activity nor per selected activities (all or nothing).
215
 
216
        // Generate the inforef file (must be after ALL steps gathering annotations of ANY type)
217
        $this->add_step(new backup_inforef_structure_step('activity_inforef', 'inforef.xml'));
218
 
219
        // Migrate the already exported inforef entries to final ones
220
        $this->add_step(new move_inforef_annotations_to_final('migrate_inforef'));
221
 
222
        // Generate the xAPI state file (conditionally).
223
        if ($this->get_setting_value('xapistate')) {
224
            $this->add_step(new backup_xapistate_structure_step('activity_xapistate', 'xapistate.xml'));
225
        }
226
 
227
        // At the end, mark it as built
228
        $this->built = true;
229
    }
230
 
231
    /**
232
     * Exceptionally override the execute method, so, based in the activity_included setting, we are able
233
     * to skip the execution of one task completely
234
     */
235
    public function execute() {
236
 
237
        // Find activity_included_setting
238
        if (!$this->get_setting_value('included')) {
239
            $this->log('activity skipped by _included setting', backup::LOG_DEBUG, $this->name);
240
            $this->plan->set_excluding_activities();
241
        } else { // Setting tells us it's ok to execute
242
            parent::execute();
243
        }
244
    }
245
 
246
 
247
    /**
248
     * Tries to look for the instance specific setting value, task specific setting value or the
249
     * common plan setting value - in that order
250
     *
251
     * @param string $name the name of the setting
252
     * @return mixed|null the value of the setting or null if not found
253
     */
254
    public function get_setting($name) {
255
        $namewithprefix = $this->modulename . '_' . $this->moduleid . '_' . $name;
256
        $result = null;
257
        foreach ($this->settings as $key => $setting) {
258
            if ($setting->get_name() == $namewithprefix) {
259
                if ($result != null) {
260
                    throw new base_task_exception('multiple_settings_by_name_found', $namewithprefix);
261
                } else {
262
                    $result = $setting;
263
                }
264
            }
265
        }
266
        if ($result) {
267
            return $result;
268
        } else {
269
            // Fallback to parent
270
            return parent::get_setting($name);
271
        }
272
    }
273
 
274
// Protected API starts here
275
 
276
    /**
1441 ariadna 277
     * Defines the common setting that any backup activity will have.
1 efrain 278
     */
279
    protected function define_settings() {
280
        global $CFG;
281
        require_once($CFG->libdir.'/questionlib.php');
282
 
1441 ariadna 283
        // All the settings related to this activity will include this prefix.
1 efrain 284
        $settingprefix = $this->modulename . '_' . $this->moduleid . '_';
285
 
1441 ariadna 286
        // All these are common settings to be shared by all activities.
287
        $activityincluded = $this->add_activity_included_setting($settingprefix);
1 efrain 288
 
1441 ariadna 289
 
290
        $this->add_activity_userinfo_setting($settingprefix, $activityincluded);
291
 
292
        // End of common activity settings, let's add the particular ones.
293
        $this->define_my_settings();
294
    }
295
 
296
    /**
297
     * Add a setting to the task. This method is used to add a setting to the task
298
     *
299
     * @param int|string $identifier the identifier of the setting
300
     * @param string $type the type of the setting
301
     * @param string|int $value the value of the setting
302
     * @return section_backup_setting the setting added
303
     */
304
    protected function add_section_setting(int|string $identifier, string $type, string|int $value): activity_backup_setting {
305
        if ($this->is_in_subsection()) {
306
            $setting = new backup_subactivity_generic_setting($identifier, $type, $value);
307
        } else {
308
            $setting = new backup_activity_generic_setting($identifier, $type, $value);
309
        }
310
        $this->add_setting($setting);
311
        return $setting;
312
    }
313
 
314
    /**
315
     * Add the section include setting to the task.
316
     *
317
     * @param string $settingprefix the identifier of the setting
318
     * @return activity_backup_setting the setting added
319
     */
320
    protected function add_activity_included_setting(string $settingprefix): activity_backup_setting {
1 efrain 321
        // Define activity_include (to decide if the whole task must be really executed)
322
        // Dependent of:
1441 ariadna 323
        // - activities root setting.
324
        // - sectionincluded setting (if exists).
1 efrain 325
        $settingname = $settingprefix . 'included';
1441 ariadna 326
        if ($this->is_in_subsection()) {
327
            $activityincluded = new backup_subactivity_generic_setting($settingname, base_setting::IS_BOOLEAN, true);
328
        } else {
329
            $activityincluded = new backup_activity_generic_setting($settingname, base_setting::IS_BOOLEAN, true);
330
        }
331
        $activityincluded->get_ui()->set_icon(new image_icon('monologo', get_string('pluginname', $this->modulename),
332
            $this->modulename, array('class' => 'ms-1')));
333
        $this->add_setting($activityincluded);
334
 
335
        // Look for "activities" root setting.
1 efrain 336
        $activities = $this->plan->get_setting('activities');
1441 ariadna 337
        $activities->add_dependency($activityincluded);
1 efrain 338
 
1441 ariadna 339
        // Look for "sectionincluded" section setting (if exists).
1 efrain 340
        $settingname = 'section_' . $this->sectionid . '_included';
341
        if ($this->plan->setting_exists($settingname)) {
1441 ariadna 342
            $sectionincluded = $this->plan->get_setting($settingname);
343
            $sectionincluded->add_dependency($activityincluded);
1 efrain 344
        }
1441 ariadna 345
        return $activityincluded;
346
    }
1 efrain 347
 
1441 ariadna 348
    /**
349
     * Add the section userinfo setting to the task.
350
     *
351
     * @param string $settingprefix the identifier of the setting
352
     * @param activity_backup_setting $includefield the setting to depend on
353
     * @return activity_backup_setting the setting added
354
     */
355
    protected function add_activity_userinfo_setting(
356
        string $settingprefix,
357
        activity_backup_setting $includefield
358
    ): activity_backup_setting {
1 efrain 359
        // Define activity_userinfo. Dependent of:
1441 ariadna 360
        // - users root setting.
361
        // - sectionuserinfo setting (if exists).
362
        // - includefield setting.
1 efrain 363
        $settingname = $settingprefix . 'userinfo';
1441 ariadna 364
        if ($this->is_in_subsection()) {
365
            $activityuserinfo = new backup_subactivity_userinfo_setting($settingname, base_setting::IS_BOOLEAN, true);
366
        } else {
367
            $activityuserinfo = new backup_activity_userinfo_setting($settingname, base_setting::IS_BOOLEAN, true);
368
        }
369
 
370
        $activityuserinfo->get_ui()->set_label('-');
371
        $activityuserinfo->get_ui()->set_visually_hidden_label(
372
            get_string('includeuserinfo_instance', 'core_backup', $this->name)
373
        );
374
        $this->add_setting($activityuserinfo);
375
        // Look for "users" root setting.
1 efrain 376
        $users = $this->plan->get_setting('users');
1441 ariadna 377
        $users->add_dependency($activityuserinfo);
378
 
379
        // Look for "sectionuserinfo" section setting (if exists).
1 efrain 380
        $settingname = 'section_' . $this->sectionid . '_userinfo';
381
        if ($this->plan->setting_exists($settingname)) {
1441 ariadna 382
            $sectionuserinfo = $this->plan->get_setting($settingname);
383
            $sectionuserinfo->add_dependency($activityuserinfo);
1 efrain 384
        }
385
 
1441 ariadna 386
        $includefield->add_dependency($activityuserinfo);
387
        return $activityuserinfo;
1 efrain 388
    }
389
 
390
    /**
391
     * Defines activity specific settings to be added to the common ones
392
     *
393
     * This method is called from {@link self::define_settings()}. The activity module
394
     * author may use it to define additional settings that influence the execution of
395
     * the backup.
396
     *
397
     * Most activities just leave the method empty.
398
     *
399
     * @see self::define_settings() for the example how to define own settings
400
     */
401
    abstract protected function define_my_settings();
402
 
403
    /**
404
     * Defines activity specific steps for this task
405
     *
406
     * This method is called from {@link self::build()}. Activities are supposed
407
     * to call {self::add_step()} in it to include their specific steps in the
408
     * backup plan.
409
     */
410
    abstract protected function define_my_steps();
411
 
412
    /**
413
     * Encodes URLs to the activity instance's scripts into a site-independent form
414
     *
415
     * The current instance of the activity may be referenced from other places in
416
     * the course by URLs like http://my.moodle.site/mod/workshop/view.php?id=42
417
     * Obvisouly, such URLs are not valid any more once the course is restored elsewhere.
418
     * For this reason the backup file does not store the original URLs but encodes them
419
     * into a transportable form. During the restore, the reverse process is applied and
420
     * the encoded URLs are replaced with the new ones valid for the target site.
421
     *
422
     * Every plugin must override this method in its subclass.
423
     *
424
     * @see backup_xml_transformer class that actually runs the transformation
425
     * @param string $content some HTML text that eventually contains URLs to the activity instance scripts
426
     * @return string the content with the URLs encoded
427
     */
428
    public static function encode_content_links($content) {
429
        throw new coding_exception('encode_content_links() method needs to be overridden in each subclass of backup_activity_task');
430
    }
431
}