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
 * @package moodlecore
20
 * @subpackage backup-plan
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
 * Implementable class defining the needed stuf for one restore plan
27
 *
28
 * TODO: Finish phpdocs
29
 */
30
class restore_plan extends base_plan implements loggable {
31
 
32
    /**
33
     *
34
     * @var restore_controller
35
     */
36
    protected $controller; // The restore controller building/executing this plan
37
    protected $basepath;   // Fullpath to dir where backup is available
38
    protected $preloaded;  // When executing the plan, do we have preloaded (from checks) info
39
    protected $decoder;    // restore_decode_processor in charge of decoding all the interlinks
40
    protected $missingmodules; // to flag if restore has detected some missing module
41
    protected $excludingdactivities; // to flag if restore settings are excluding any activity
42
 
43
    /**
44
     * Constructor - instantiates one object of this class
45
     */
46
    public function __construct($controller) {
47
        global $CFG;
48
 
49
        if (! $controller instanceof restore_controller) {
50
            throw new restore_plan_exception('wrong_restore_controller_specified');
51
        }
52
        $backuptempdir    = make_backup_temp_directory('');
53
        $this->controller = $controller;
54
        $this->basepath   = $backuptempdir . '/' . $controller->get_tempdir();
55
        $this->preloaded  = false;
56
        $this->decoder    = new restore_decode_processor($this->get_restoreid(), $this->get_info()->original_wwwroot, $CFG->wwwroot);
57
        $this->missingmodules = false;
58
        $this->excludingdactivities = false;
59
 
60
        parent::__construct('restore_plan');
61
    }
62
 
63
    /**
64
     * Destroy all circular references. It helps PHP 5.2 a lot!
65
     */
66
    public function destroy() {
67
        // No need to destroy anything recursively here, direct reset
68
        $this->controller = null;
69
        // Delegate to base plan the rest
70
        parent::destroy();
71
    }
72
 
73
    public function build() {
74
        restore_plan_builder::build_plan($this->controller); // We are moodle2 always, go straight to builder
75
        $this->built = true;
76
    }
77
 
78
    public function get_restoreid() {
79
        return $this->controller->get_restoreid();
80
    }
81
 
82
    public function get_courseid() {
83
        return $this->controller->get_courseid();
84
    }
85
 
86
    public function get_mode() {
87
        return $this->controller->get_mode();
88
    }
89
 
90
    public function get_basepath() {
91
        return $this->basepath;
92
    }
93
 
94
    public function get_logger() {
95
        return $this->controller->get_logger();
96
    }
97
 
98
    /**
99
     * Gets the progress reporter, which can be used to report progress within
100
     * the backup or restore process.
101
     *
102
     * @return \core\progress\base Progress reporting object
103
     */
104
    public function get_progress() {
105
        return $this->controller->get_progress();
106
    }
107
 
108
    public function get_info() {
109
        return $this->controller->get_info();
110
    }
111
 
112
    public function get_target() {
113
        return $this->controller->get_target();
114
    }
115
 
116
    public function get_userid() {
117
        return $this->controller->get_userid();
118
    }
119
 
120
    public function get_decoder() {
121
        return $this->decoder;
122
    }
123
 
124
    public function is_samesite() {
125
        return $this->controller->is_samesite();
126
    }
127
 
128
    public function is_missing_modules() {
129
        return $this->missingmodules;
130
    }
131
 
132
    public function is_excluding_activities() {
133
        return $this->excludingdactivities;
134
    }
135
 
136
    public function set_preloaded_information() {
137
        $this->preloaded = true;
138
    }
139
 
140
    public function get_preloaded_information() {
141
        return $this->preloaded;
142
    }
143
 
144
    public function get_tempdir() {
145
        return $this->controller->get_tempdir();
146
    }
147
 
148
    public function set_missing_modules() {
149
        $this->missingmodules = true;
150
    }
151
 
152
    public function set_excluding_activities() {
153
        $this->excludingdactivities = true;
154
    }
155
 
156
    public function log($message, $level, $a = null, $depth = null, $display = false) {
157
        backup_helper::log($message, $level, $a, $depth, $display, $this->get_logger());
158
    }
159
 
160
    /**
161
     * Function responsible for executing the tasks of any plan
162
     */
163
    public function execute() {
164
        if ($this->controller->get_status() != backup::STATUS_AWAITING) {
165
            throw new restore_controller_exception('restore_not_executable_awaiting_required', $this->controller->get_status());
166
        }
167
        $this->controller->set_status(backup::STATUS_EXECUTING);
168
        parent::execute();
169
        $this->controller->set_status(backup::STATUS_FINISHED_OK);
170
 
171
        // Check if we are restoring a course.
172
        if ($this->controller->get_type() === backup::TYPE_1COURSE) {
173
 
174
            // Check to see if we are on the same site to pass original course info.
175
            $issamesite = $this->controller->is_samesite();
176
 
177
            $otherarray = array('type' => $this->controller->get_type(),
178
                                'target' => $this->controller->get_target(),
179
                                'mode' => $this->controller->get_mode(),
180
                                'operation' => $this->controller->get_operation(),
181
                                'samesite' => $issamesite
182
            );
183
 
184
            if ($this->controller->is_samesite()) {
185
                $otherarray['originalcourseid'] = $this->controller->get_info()->original_course_id;
186
            }
187
 
1441 ariadna 188
            // Add the settings to the event data.
189
            $otherarray['settings'] = $this->get_settings();
190
 
1 efrain 191
            // Trigger a course restored event.
192
            $event = \core\event\course_restored::create(array(
193
                'objectid' => $this->get_courseid(),
194
                'userid' => $this->get_userid(),
195
                'context' => context_course::instance($this->get_courseid()),
196
                'other' => $otherarray
197
            ));
198
            $event->trigger();
199
        }
200
    }
201
 
202
    /**
203
     * Execute the after_restore methods of all the executed tasks in the plan
204
     */
205
    public function execute_after_restore() {
206
        // Simply iterate over each task in the plan and delegate to them the execution
207
        $progress = $this->get_progress();
208
        $progress->start_progress($this->get_name() .
209
                ': executing execute_after_restore for all tasks', count($this->tasks));
210
 
211
        /** @var base_task $task */
212
        foreach ($this->tasks as $task) {
213
            $task->execute_after_restore();
214
            $progress->increment_progress();
215
        }
216
        $progress->end_progress();
217
    }
218
 
219
    /**
220
     * Compares the provided moodle version with the one the backup was taken from.
221
     *
222
     * @param int $version Moodle version number (YYYYMMDD or YYYYMMDDXX)
223
     * @param string $operator Operator to compare the provided version to the backup version. {@see version_compare()}
224
     * @return bool True if the comparison passes.
225
     */
226
    public function backup_version_compare(int $version, string $operator): bool {
227
        preg_match('/(\d{' . strlen($version) . '})/', $this->get_info()->moodle_version, $matches);
228
        $backupbuild = (int)$matches[1];
229
        return version_compare($backupbuild, $version, $operator);
230
    }
231
 
232
    /**
233
     * Compares the provided moodle release with the one the backup was taken from.
234
     *
235
     * @param string $release Moodle release (X.Y or X.Y.Z)
236
     * @param string $operator Operator to compare the provided release to the backup release. {@see version_compare()}
237
     * @return bool True if the comparison passes.
238
     */
239
    public function backup_release_compare(string $release, string $operator): bool {
240
        return version_compare($this->get_info()->backup_release, $release, $operator);
241
    }
242
}
243
 
244
/*
245
 * Exception class used by all the @restore_plan stuff
246
 */
247
class restore_plan_exception extends base_plan_exception {
248
 
249
    public function __construct($errorcode, $a=NULL, $debuginfo=null) {
250
        parent::__construct($errorcode, $a, $debuginfo);
251
    }
252
}