3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Import backup file or select existing backup file from moodle
21 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once('../config.php');
26 require_once(dirname(__FILE__
) . '/restorefile_form.php');
27 require_once($CFG->dirroot
. '/backup/util/includes/restore_includes.php');
30 $contextid = required_param('contextid', PARAM_INT
);
31 $filecontextid = optional_param('filecontextid', 0, PARAM_INT
);
33 $action = optional_param('action', '', PARAM_ALPHA
);
35 // non js interface may require these parameters
36 $component = optional_param('component', null, PARAM_COMPONENT
);
37 $filearea = optional_param('filearea', null, PARAM_AREA
);
38 $itemid = optional_param('itemid', null, PARAM_INT
);
39 $filepath = optional_param('filepath', null, PARAM_PATH
);
40 $filename = optional_param('filename', null, PARAM_FILE
);
42 list($context, $course, $cm) = get_context_info_array($contextid);
44 // will be used when restore
45 if (!empty($filecontextid)) {
46 $filecontext = context
::instance_by_id($filecontextid);
49 $url = new moodle_url('/backup/restorefile.php', array('contextid'=>$contextid));
51 switch ($context->contextlevel
) {
53 $heading = get_string('restoreactivity', 'backup');
57 $heading = get_string('restorecourse', 'backup');
61 require_login($course, false, $cm);
62 require_capability('moodle/restore:restorecourse', $context);
64 $browser = get_file_browser();
66 // check if tmp dir exists
67 $tmpdir = $CFG->tempdir
. '/backup';
68 if (!check_dir_exists($tmpdir, true, true)) {
69 throw new restore_controller_exception('cannot_create_backup_temp_dir');
72 // choose the backup file from backup files tree
73 if ($action == 'choosebackupfile') {
74 if ($fileinfo = $browser->get_file_info($filecontext, $component, $filearea, $itemid, $filepath, $filename)) {
75 $filename = restore_controller
::get_tempdir_name($course->id
, $USER->id
);
76 $pathname = $tmpdir . '/' . $filename;
77 $fileinfo->copy_to_pathname($pathname);
78 $restore_url = new moodle_url('/backup/restore.php', array('contextid'=>$contextid, 'filename'=>$filename));
79 redirect($restore_url);
81 redirect($url, get_string('filenotfound', 'error'));
87 $PAGE->set_context($context);
88 $PAGE->set_title(get_string('course') . ': ' . $course->fullname
);
89 $PAGE->set_heading($heading);
90 $PAGE->set_pagelayout('admin');
92 $form = new course_restore_form(null, array('contextid'=>$contextid));
93 $data = $form->get_data();
94 if ($data && has_capability('moodle/restore:uploadfile', $context)) {
95 $filename = restore_controller
::get_tempdir_name($course->id
, $USER->id
);
96 $pathname = $tmpdir . '/' . $filename;
97 $form->save_file('backupfile', $pathname);
98 $restore_url = new moodle_url('/backup/restore.php', array('contextid'=>$contextid, 'filename'=>$filename));
99 redirect($restore_url);
105 echo $OUTPUT->header();
107 // require uploadfile cap to use file picker
108 if (has_capability('moodle/restore:uploadfile', $context)) {
109 echo $OUTPUT->heading(get_string('importfile', 'backup'));
110 echo $OUTPUT->container_start();
112 echo $OUTPUT->container_end();
115 if ($context->contextlevel
== CONTEXT_MODULE
) {
116 echo $OUTPUT->heading_with_help(get_string('choosefilefromactivitybackup', 'backup'), 'choosefilefromuserbackup', 'backup');
117 echo $OUTPUT->container_start();
118 $treeview_options = array();
119 $user_context = context_user
::instance($USER->id
);
120 $treeview_options['filecontext'] = $context;
121 $treeview_options['currentcontext'] = $context;
122 $treeview_options['component'] = 'backup';
123 $treeview_options['context'] = $context;
124 $treeview_options['filearea'] = 'activity';
125 $renderer = $PAGE->get_renderer('core', 'backup');
126 echo $renderer->backup_files_viewer($treeview_options);
127 echo $OUTPUT->container_end();
130 echo $OUTPUT->heading_with_help(get_string('choosefilefromcoursebackup', 'backup'), 'choosefilefromcoursebackup', 'backup');
131 echo $OUTPUT->container_start();
132 $treeview_options = array();
133 $treeview_options['filecontext'] = $context;
134 $treeview_options['currentcontext'] = $context;
135 $treeview_options['component'] = 'backup';
136 $treeview_options['context'] = $context;
137 $treeview_options['filearea'] = 'course';
138 $renderer = $PAGE->get_renderer('core', 'backup');
139 echo $renderer->backup_files_viewer($treeview_options);
140 echo $OUTPUT->container_end();
142 echo $OUTPUT->heading_with_help(get_string('choosefilefromuserbackup', 'backup'), 'choosefilefromuserbackup', 'backup');
143 echo $OUTPUT->container_start();
144 $treeview_options = array();
145 $user_context = context_user
::instance($USER->id
);
146 $treeview_options['filecontext'] = $user_context;
147 $treeview_options['currentcontext'] = $context;
148 $treeview_options['component'] = 'user';
149 $treeview_options['context'] = 'backup';
150 $treeview_options['filearea'] = 'backup';
151 $renderer = $PAGE->get_renderer('core', 'backup');
152 echo $renderer->backup_files_viewer($treeview_options);
153 echo $OUTPUT->container_end();
155 $automatedbackups = get_config('backup', 'backup_auto_active');
156 if (!empty($automatedbackups)) {
157 echo $OUTPUT->heading_with_help(get_string('choosefilefromautomatedbackup', 'backup'), 'choosefilefromautomatedbackup', 'backup');
158 echo $OUTPUT->container_start();
159 $treeview_options = array();
160 $user_context = context_user
::instance($USER->id
);
161 $treeview_options['filecontext'] = $context;
162 $treeview_options['currentcontext'] = $context;
163 $treeview_options['component'] = 'backup';
164 $treeview_options['context'] = $context;
165 $treeview_options['filearea'] = 'automated';
166 $renderer = $PAGE->get_renderer('core', 'backup');
167 echo $renderer->backup_files_viewer($treeview_options);
168 echo $OUTPUT->container_end();
171 echo $OUTPUT->footer();