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
 * This script is used to configure and execute the import proccess.
20
 *
21
 * @package    core
22
 * @subpackage backup
23
 * @copyright  Moodle
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
 
27
define('NO_OUTPUT_BUFFERING', true);
28
 
29
// Require both the backup and restore libs
30
require_once('../config.php');
31
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
32
require_once($CFG->dirroot . '/backup/moodle2/backup_plan_builder.class.php');
33
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
34
require_once($CFG->dirroot . '/backup/util/ui/import_extensions.php');
35
 
36
// The courseid we are importing to
37
$courseid = required_param('id', PARAM_INT);
38
// The id of the course we are importing FROM (will only be set if past first stage
39
$importcourseid = optional_param('importid', false, PARAM_INT);
40
// We just want to check if a search has been run. True if anything is there.
41
$searchcourses = optional_param('searchcourses', false, PARAM_BOOL);
42
// The target method for the restore (adding or deleting)
43
$restoretarget = optional_param('target', backup::TARGET_CURRENT_ADDING, PARAM_INT);
44
 
45
// Load the course and context
46
$course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
47
$context = context_course::instance($courseid);
48
 
49
// Must pass login
50
require_login($course);
51
// Must hold restoretargetimport in the current course
52
require_capability('moodle/restore:restoretargetimport', $context);
53
 
54
// Set up the page
55
$PAGE->set_title($course->shortname . ': ' . get_string('import'));
56
$PAGE->set_heading($course->fullname);
57
$PAGE->set_url(new moodle_url('/backup/import.php', array('id'=>$courseid)));
58
$PAGE->set_context($context);
59
$PAGE->set_pagelayout('incourse');
60
 
61
// Prepare the backup renderer
62
$renderer = $PAGE->get_renderer('core','backup');
63
 
64
// Check if we already have a import course id
65
if ($importcourseid === false || $searchcourses) {
66
    // Obviously not... show the selector so one can be chosen
67
    $url = new moodle_url('/backup/import.php', array('id'=>$courseid));
68
    $search = new import_course_search(array('url'=>$url));
69
 
70
    // show the course selector
71
    echo $OUTPUT->header();
72
    \backup_helper::print_coursereuse_selector('import');
73
 
74
    echo html_writer::tag('div', get_string('importinfo'), ['class' => 'pb-3']);
75
 
76
    $backup = new import_ui(false, array());
77
    echo $renderer->progress_bar($backup->get_progress_bar());
78
    $html = $renderer->import_course_selector($url, $search);
79
    echo $html;
80
    echo $OUTPUT->footer();
81
    die();
82
}
83
 
84
// Load the course +context to import from
85
$importcourse = $DB->get_record('course', array('id'=>$importcourseid), '*', MUST_EXIST);
86
$importcontext = context_course::instance($importcourseid);
87
 
88
// Make sure the user can backup from that course
89
require_capability('moodle/backup:backuptargetimport', $importcontext);
90
 
91
// Attempt to load the existing backup controller (backupid will be false if there isn't one)
92
$backupid = optional_param('backup', false, PARAM_ALPHANUM);
93
if (!($bc = backup_ui::load_controller($backupid))) {
94
    $bc = new backup_controller(backup::TYPE_1COURSE, $importcourse->id, backup::FORMAT_MOODLE,
95
                            backup::INTERACTIVE_YES, backup::MODE_IMPORT, $USER->id);
96
    $bc->get_plan()->get_setting('users')->set_status(backup_setting::LOCKED_BY_CONFIG);
97
    $settings = $bc->get_plan()->get_settings();
98
 
99
    // For the initial stage we want to hide all locked settings and if there are
100
    // no visible settings move to the next stage
101
    $visiblesettings = false;
102
    foreach ($settings as $setting) {
103
        if ($setting->get_status() !== backup_setting::NOT_LOCKED) {
104
            $setting->set_visibility(backup_setting::HIDDEN);
105
        } else {
106
            $visiblesettings = true;
107
        }
108
    }
109
    import_ui::skip_current_stage(!$visiblesettings);
110
}
111
 
112
// Prepare the import UI
113
$backup = new import_ui($bc, array('importid'=>$importcourse->id, 'target'=>$restoretarget));
114
// Process the current stage
115
$backup->process();
116
 
117
// If this is the confirmation stage remove the filename setting
118
if ($backup->get_stage() == backup_ui::STAGE_CONFIRMATION) {
119
    $backup->get_setting('filename')->set_visibility(backup_setting::HIDDEN);
120
}
121
 
122
// If it's the final stage process the import
123
if ($backup->get_stage() == backup_ui::STAGE_FINAL) {
124
    echo $OUTPUT->header();
125
    \backup_helper::print_coursereuse_selector('import');
126
 
127
    echo html_writer::tag('div', get_string('importinfo'), ['class' => 'pb-3']);
128
 
129
    // Display an extra progress bar so that we can show the current stage.
130
    echo html_writer::start_div('', array('id' => 'executionprogress'));
131
    echo $renderer->progress_bar($backup->get_progress_bar());
132
 
133
    // Start the progress display - we split into 2 chunks for backup and restore.
134
    $progress = new \core\progress\display();
135
    $progress->start_progress('', 2);
136
    $backup->get_controller()->set_progress($progress);
137
 
138
    // Prepare logger for backup.
139
    $logger = new core_backup_html_logger($CFG->debugdeveloper ? backup::LOG_DEBUG : backup::LOG_INFO);
140
    $backup->get_controller()->add_logger($logger);
141
 
142
    // First execute the backup
143
    $backup->execute();
144
    // Before destroying the backup object, we still need to generate the progress bar.
145
    $progressbar = $renderer->progress_bar($backup->get_progress_bar());
146
    $backup->destroy();
147
    unset($backup);
148
 
149
    // Note that we've done that progress.
150
    $progress->progress(1);
151
 
152
    // Check whether the backup directory still exists. If missing, something
153
    // went really wrong in backup, throw error. Note that backup::MODE_IMPORT
154
    // backups don't store resulting files ever
155
    $tempdestination = make_backup_temp_directory($backupid, false);
156
    if (!file_exists($tempdestination) || !is_dir($tempdestination)) {
157
        throw new \moodle_exception('unknownbackupexporterror'); // Shouldn't happen ever.
158
    }
159
 
160
    // Prepare the restore controller. We don't need a UI here as we will just use what
161
    // ever the restore has (the user has just chosen).
162
    $rc = new restore_controller($backupid, $course->id, backup::INTERACTIVE_YES, backup::MODE_IMPORT, $USER->id, $restoretarget);
163
 
164
    // Start a progress section for the restore, which will consist of 2 steps
165
    // (the precheck and then the actual restore).
166
    $progress->start_progress('Restore process', 2);
167
    $rc->set_progress($progress);
168
 
169
    // Set logger for restore.
170
    $rc->add_logger($logger);
171
 
172
    // Convert the backup if required.... it should NEVER happed
173
    if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) {
174
        $rc->convert();
175
    }
176
    // Mark the UI finished.
177
    $rc->finish_ui();
178
    // Execute prechecks
179
    $warnings = false;
180
    if (!$rc->execute_precheck()) {
181
        $precheckresults = $rc->get_precheck_results();
182
        if (is_array($precheckresults)) {
183
            if (!empty($precheckresults['errors'])) { // If errors are found, terminate the import.
184
                fulldelete($tempdestination);
185
 
186
                echo $renderer->precheck_notices($precheckresults);
187
                echo $OUTPUT->continue_button(new moodle_url('/course/view.php', array('id'=>$course->id)));
188
                echo $OUTPUT->footer();
189
                die();
190
            }
191
            if (!empty($precheckresults['warnings'])) { // If warnings are found, go ahead but display warnings later.
192
                $warnings = $precheckresults['warnings'];
193
            }
194
        }
195
    }
196
    if ($restoretarget == backup::TARGET_CURRENT_DELETING || $restoretarget == backup::TARGET_EXISTING_DELETING) {
197
        restore_dbops::delete_course_content($course->id);
198
    }
199
    // Execute the restore.
200
    $rc->execute_plan();
201
 
202
    // Delete the temp directory now
203
    fulldelete($tempdestination);
204
 
205
    // End restore section of progress tracking (restore/precheck).
206
    $progress->end_progress();
207
 
208
    // All progress complete. Hide progress area.
209
    $progress->end_progress();
210
    echo html_writer::end_div();
211
    echo html_writer::script('document.getElementById("executionprogress").style.display = "none";');
212
 
213
    // Display a notification and a continue button
214
    if ($warnings) {
215
        echo $OUTPUT->box_start();
216
        echo $OUTPUT->notification(get_string('warning'), 'notifyproblem');
217
        echo html_writer::start_tag('ul', array('class'=>'list'));
218
        foreach ($warnings as $warning) {
219
            echo html_writer::tag('li', $warning);
220
        }
221
        echo html_writer::end_tag('ul');
222
        echo $OUTPUT->box_end();
223
    }
224
    echo $progressbar;
225
    echo $OUTPUT->notification(get_string('importsuccess', 'backup'), 'notifysuccess');
226
    echo $OUTPUT->continue_button(new moodle_url('/course/view.php', array('id'=>$course->id)));
227
 
228
    // Get and display log data if there was any.
229
    $loghtml = $logger->get_html();
230
    if ($loghtml != '') {
231
        echo $renderer->log_display($loghtml);
232
    }
233
 
234
    echo $OUTPUT->footer();
235
 
236
    die();
237
 
238
} else {
239
    // Otherwise save the controller and progress
240
    $backup->save_controller();
241
}
242
 
243
// Display the current stage.
244
echo $OUTPUT->header();
245
\backup_helper::print_coursereuse_selector('import');
246
 
247
echo html_writer::tag('div', get_string('importinfo'), ['class' => 'pb-3']);
248
 
249
if ($backup->enforce_changed_dependencies()) {
250
    debugging('Your settings have been altered due to unmet dependencies', DEBUG_DEVELOPER);
251
}
252
echo $renderer->progress_bar($backup->get_progress_bar());
253
echo $backup->display($renderer);
254
$backup->destroy();
255
unset($backup);
256
echo $OUTPUT->footer();