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_course_task
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
/**
29
 * course task that provides all the properties and common steps to be performed
30
 * when one course is being backup
31
 *
32
 * TODO: Finish phpdocs
33
 */
34
class backup_course_task extends backup_task {
35
 
36
    protected $courseid;
37
    protected $contextid;
38
 
39
    /**
40
     * Constructor - instantiates one object of this class
41
     */
42
    public function __construct($name, $courseid, $plan = null) {
43
 
44
        $this->courseid   = $courseid;
45
        $this->contextid  = context_course::instance($this->courseid)->id;
46
 
47
        parent::__construct($name, $plan);
48
    }
49
 
50
    public function get_contextid() {
51
        return $this->contextid;
52
    }
53
 
54
    /**
55
     * Course tasks have their own directory to write files
56
     */
57
    public function get_taskbasepath() {
58
 
59
        return $this->get_basepath() . '/course';
60
    }
61
 
62
    /**
63
     * Create all the steps that will be part of this task
64
     */
65
    public function build() {
66
 
67
        // Add some extra settings that related processors are going to need
68
        $this->add_setting(new backup_activity_generic_setting(backup::VAR_COURSEID, base_setting::IS_INTEGER, $this->get_courseid()));
69
        $this->add_setting(new backup_activity_generic_setting(backup::VAR_CONTEXTID, base_setting::IS_INTEGER, $this->contextid));
70
 
71
        // Create the course directory
72
        $this->add_step(new create_taskbasepath_directory('create_course_directory'));
73
 
74
        // Create the course.xml file with course & category information
75
        // annotating some bits, tags and module restrictions
76
        $this->add_step(new backup_course_structure_step('course_info', 'course.xml'));
77
 
78
        // Generate the enrolment file (conditionally, prevent it in any IMPORT/HUB operation)
1441 ariadna 79
        if ($this->plan->get_mode() != backup::MODE_IMPORT) {
1 efrain 80
            $this->add_step(new backup_enrolments_structure_step('course_enrolments', 'enrolments.xml'));
81
        }
82
 
83
        // Annotate enrolment custom fields.
84
        $this->add_step(new backup_enrolments_execution_step('annotate_enrol_custom_fields'));
85
 
86
        // Annotate all the groups and groupings belonging to the course. This can be optional.
87
        if ($this->get_setting_value('groups')) {
88
            $this->add_step(new backup_annotate_course_groups_and_groupings('annotate_course_groups'));
89
        }
90
 
91
        // Annotate the groups used in already annotated groupings (note this may be
92
        // unnecessary now that we are annotating all the course groups and groupings in the
93
        // step above). This is here to support course->defaultgroupingid.
94
        // This may not be required to annotate if groups are not being backed up.
95
        if ($this->get_setting_value('groups')) {
96
            $this->add_step(new backup_annotate_groups_from_groupings('annotate_groups_from_groupings'));
97
        }
98
 
99
 
100
        // Generate the roles file (optionally role assignments and always role overrides)
101
        $this->add_step(new backup_roles_structure_step('course_roles', 'roles.xml'));
102
 
103
        // Generate the filter file (conditionally)
104
        if ($this->get_setting_value('filters')) {
105
            $this->add_step(new backup_filters_structure_step('course_filters', 'filters.xml'));
106
        }
107
 
108
        // Generate the comments file (conditionally)
109
        if ($this->get_setting_value('comments')) {
110
            $this->add_step(new backup_comments_structure_step('course_comments', 'comments.xml'));
111
        }
112
 
113
        // Generate the calender events file (conditionally)
114
        if ($this->get_setting_value('calendarevents')) {
115
            $this->add_step(new backup_calendarevents_structure_step('course_calendar', 'calendar.xml'));
116
        }
117
 
118
        // Generate the logs file (conditionally)
119
        if ($this->get_setting_value('logs')) {
120
            // Legacy logs.
121
            $this->add_step(new backup_course_logs_structure_step('course_logs', 'logs.xml'));
122
            // New log stores.
123
            $this->add_step(new backup_course_logstores_structure_step('course_logstores', 'logstores.xml'));
124
            // Last access to course logs.
125
            $this->add_step(new backup_course_loglastaccess_structure_step('course_loglastaccess', 'loglastaccess.xml'));
126
        }
127
 
128
        // Generate the course competencies.
129
        $this->add_step(new backup_course_competencies_structure_step('course_competencies', 'competencies.xml'));
130
 
131
        // Annotate activity completion defaults.
132
        $this->add_step(new backup_completion_defaults_structure_step('course_completion_defaults', 'completiondefaults.xml'));
133
 
134
        // Generate the inforef file (must be after ALL steps gathering annotations of ANY type)
135
        $this->add_step(new backup_inforef_structure_step('course', 'inforef.xml'));
136
 
137
        // Migrate the already exported inforef entries to final ones
138
        $this->add_step(new move_inforef_annotations_to_final('migrate_inforef'));
139
 
140
        // Generate the content bank file (conditionally).
141
        if ($this->get_setting_value('contentbankcontent')) {
142
            $this->add_step(new backup_contentbankcontent_structure_step('course_contentbank', 'contentbank.xml'));
143
        }
144
 
145
        // At the end, mark it as built
146
        $this->built = true;
147
    }
148
 
149
    /**
150
     * Code the transformations to perform in the course in
151
     * order to get transportable (encoded) links
152
     * @param string $content content in which to encode links.
153
     * @return string content with links encoded.
154
     */
155
    public static function encode_content_links($content) {
156
 
157
        // Link to the course main page (it also covers "&topic=xx" and "&week=xx"
158
        // because they don't become transformed (section number) in backup/restore.
159
        $content = self::encode_links_helper($content, 'COURSEVIEWBYID',       '/course/view.php?id=');
1441 ariadna 160
        $content = self::encode_links_helper($content, 'COURSESECTIONBYID',    '/course/section.php?id=');
1 efrain 161
 
162
        // A few other key course links.
163
        $content = self::encode_links_helper($content, 'GRADEINDEXBYID',       '/grade/index.php?id=');
164
        $content = self::encode_links_helper($content, 'GRADEREPORTINDEXBYID', '/grade/report/index.php?id=');
1441 ariadna 165
        $content = self::encode_links_helper($content, 'BADGESVIEWBYID',       '/badges/index.php?type=2&id=');
1 efrain 166
        $content = self::encode_links_helper($content, 'USERINDEXVIEWBYID',    '/user/index.php?id=');
167
        $content = self::encode_links_helper($content, 'PLUGINFILEBYCONTEXT',  '/pluginfile.php/');
168
        $content = self::encode_links_helper($content, 'PLUGINFILEBYCONTEXTURLENCODED', '/pluginfile.php/', true);
169
 
170
        return $content;
171
    }
172
 
173
    /**
174
     * Helper method, used by encode_content_links.
175
     * @param string $content content in which to encode links.
176
     * @param string $name the name of this type of encoded link.
177
     * @param string $path the path that identifies this type of link, up
178
     *      to the ?paramname= bit.
179
     * @param bool $urlencoded whether to use urlencode() before replacing the path.
180
     * @return string content with one type of link encoded.
181
     */
182
    private static function encode_links_helper(string $content, string $name, string $path, bool $urlencoded = false) {
183
        global $CFG;
184
        // We want to convert both http and https links.
185
        $root = $CFG->wwwroot;
186
        $httpsroot = str_replace('http://', 'https://', $root);
187
        $httproot = str_replace('https://', 'http://', $root);
188
 
189
        $httpsbase = $httpsroot . $path;
190
        $httpbase = $httproot . $path;
191
 
192
        if ($urlencoded) {
193
            $httpsbase = urlencode($httpsbase);
194
            $httpbase = urlencode($httpbase);
195
        }
196
 
197
        $httpsbase = preg_quote($httpsbase, '/');
198
        $httpbase = preg_quote($httpbase, '/');
199
 
200
        $return = preg_replace('/(' . $httpsbase . ')([0-9]+)/', '$@' . $name . '*$2@$', $content);
201
        $return = preg_replace('/(' . $httpbase . ')([0-9]+)/', '$@' . $name . '*$2@$', $return);
202
 
203
        return $return;
204
    }
205
 
206
// Protected API starts here
207
 
208
    /**
209
     * Define the common setting that any backup section will have
210
     */
211
    protected function define_settings() {
212
 
213
        // Nothing to add, sections doesn't have common settings (for now)
214
 
215
    }
216
}