MDL-22164 backup - clean important parts of old code. Leaving rest for reference
[moodle.git] / backup / restore_check.html
CommitLineData
795b6945 1<?php
94ba4c9e 2
3f50cab5 3 //This page receive all the restore_form data. Then, if existing course
4 //has been selected, shows a list of courses to select one.
5 //It cheks that the parammeter from restore_form are coherent.
6 //It puts all the restore info in the session.
7 //Finally, it calls restore_execute to do the hard work
3f50cab5 8 //Get objects from session
ad4375ac 9
c4c7d936 10 global $DB, $OUTPUT;
ad4375ac 11
91d7f813 12 if ($SESSION) {
13 $info = $SESSION->info;
14 $course_header = $SESSION->course_header;
e3713e1a 15 if (isset($SESSION->restore)) {
16 $restore = $SESSION->restore;
17 }
91d7f813 18 }
3f50cab5 19
c3cb69e5 20 //Detect if we are coming from the restore form
21 $fromform = optional_param ('fromform', 0, PARAM_INT);
22
63013fb8 23 ///Enforce SESSION->course_header rewrite (PHP 4.x needed because assigns are by value) MDL-8298
24 $SESSION->course_header = $course_header;
25
c3cb69e5 26 //If restore session info exists, but we are coming from the form
27 //it has prioriry
28 if (isset($restore) and !empty($fromform)) {
7c934d06 29 unset($restore);
30 }
91d7f813 31
35a4a791 32 // check for session objects
f65329a6 33 if (empty($info) or empty($course_header)) {
795b6945 34 print_error('sessionmissing', 'debug', '',
d3b7ea93 35 'info and course_header');
b78fbdac 36 }
3bee1ead 37
91d7f813 38
7c934d06 39 //If the restore object doesn't exist, we are going
40 //to check every variable individually and create it
41
e3713e1a 42 if (!isset($restore)) {
7c934d06 43 //Check that we have all we need
44 //backup_unique_code
d4b5bd43 45 $backup_unique_code = required_param('backup_unique_code', PARAM_INT);
7c934d06 46 //file
d4b5bd43 47 $file = required_param('file', PARAM_PATH);
7c934d06 48 //Checks for the required restoremod parameters
ad4375ac 49 if ($allmods = $DB->get_records("modules")) {
7c934d06 50 foreach ($allmods as $mod) {
51 $modname = $mod->name;
52 $var = "restore_".$modname;
92ebcabe 53 $$var = optional_param( $var,0, PARAM_CLEAN);
7c934d06 54 $var = "restore_user_info_".$modname;
92ebcabe 55 $$var = optional_param( $var,0, PARAM_CLEAN);
5f8556bc 56 $instances = !empty($info->mods[$mod->name]->instances) ? $info->mods[$mod->name]->instances : NULL;
0beabf48 57 if ($instances === NULL) {
58 continue;
59 }
1ecea976 60 foreach ($instances as $instance) {
61 $var = 'restore_'.$modname.'_instance_'.$instance->id;
7f9bd149 62 $$var = optional_param($var,0,PARAM_INT);
1ecea976 63 $var = 'restore_user_info_'.$modname.'_instance_'.$instance->id;
7f9bd149 64 $$var = optional_param($var,0,PARAM_INT);
1ecea976 65 }
7c934d06 66 }
67 }
68 //restoreto
7f9bd149 69 $restore_restoreto = required_param('restore_restoreto', PARAM_INT);
7c934d06 70 //restore_users
7f9bd149 71 $restore_users = required_param('restore_users', PARAM_INT);
b0dc3e56 72
7f9bd149 73 $restore_groups = required_param('restore_groups', PARAM_INT);
7c934d06 74 //restore_logs
7f9bd149 75 $restore_logs = required_param('restore_logs', PARAM_INT);
7c934d06 76 //restore_user_files
7f9bd149 77 $restore_user_files = required_param('restore_user_files', PARAM_INT);
7c934d06 78 //restore_course_files
7f9bd149 79 $restore_course_files = required_param('restore_course_files', PARAM_INT);
3bee1ead 80 //restore_site_files
7f9bd149 81 $restore_site_files = required_param('restore_site_files', PARAM_INT);
e5d30fdc 82 //restore_gradebook_history
7f9bd149 83 $restore_gradebook_history = required_param('restore_gradebook_history', PARAM_INT);
6d1e4162 84 //restore_messages
7f9bd149 85 $restore_messages = required_param('restore_messages', PARAM_INT);
8b6ccde7 86 //restore_blogs
87 $restore_blogs = required_param('restore_blogs', PARAM_INT);
88
c3cb69e5 89 //Check we've selected a course to restore to
90 $course_id = optional_param('course_id', 0, PARAM_INT);
3bee1ead 91
7c934d06 92 //We are here, having all we need !!
93 //Create the restore object and put it in the session
94 $restore->backup_unique_code = $backup_unique_code;
95 $restore->file = $file;
ad4375ac 96 if ($allmods = $DB->get_records("modules")) {
7c934d06 97 foreach ($allmods as $mod) {
98 $modname = $mod->name;
99 $var = "restore_".$modname;
100 $restore->mods[$modname]->restore=$$var;
101 $var = "restore_user_info_".$modname;
102 $restore->mods[$modname]->userinfo=$$var;
5f8556bc 103 $instances = !empty($info->mods[$mod->name]->instances) ? $info->mods[$mod->name]->instances : NULL;
0beabf48 104 if ($instances === NULL) {
105 continue;
106 }
1ecea976 107 foreach ($instances as $instance) {
108 $var = 'restore_'.$modname.'_instance_'.$instance->id;
109 $restore->mods[$modname]->instances[$instance->id]->restore = $$var;
110 $var = 'restore_user_info_'.$modname.'_instance_'.$instance->id;
111 $restore->mods[$modname]->instances[$instance->id]->userinfo = $$var;
112 }
7c934d06 113 }
114 }
115 $restore->restoreto=$restore_restoreto;
116 $restore->users=$restore_users;
b0dc3e56 117 $restore->groups=$restore_groups;
7c934d06 118 $restore->logs=$restore_logs;
119 $restore->user_files=$restore_user_files;
120 $restore->course_files=$restore_course_files;
3bee1ead 121 $restore->site_files=$restore_site_files;
6d1e4162 122 $restore->messages=$restore_messages;
8b6ccde7 123 $restore->blogs=$restore_blogs;
e5d30fdc 124 $restore->restore_gradebook_history=$restore_gradebook_history;
7c934d06 125 $restore->course_id=$course_id;
3bee1ead 126 //add new vars to restore object
c3b307b7 127 $restore->course_startdateoffset = $coursestartdatedateoffset;
128 $restore->course_shortname = $currentcourseshortname;
3bee1ead 129
8020a016 130 // create role mappings, not sure all should be here
131 if ($data2 = data_submitted()) {
132 foreach ($data2 as $tempname=>$tempdata) {
133 if (strstr($tempname, 'roles_')) {
134 $temprole = explode('_', $tempname);
135 $oldroleid = $temprole[1];
136 $newroleid = $tempdata;
137 $restore->rolesmapping[$oldroleid] = $newroleid;
3bee1ead 138 }
139 }
5e67946d 140 }
3bee1ead 141
5e67946d 142 // default role mapping for moodle < 1.7
143 if ($defaultteacheredit = optional_param('defaultteacheredit', 0, PARAM_INT)) {
3bee1ead 144 $restore->rolesmapping['defaultteacheredit'] = $defaultteacheredit;
5e67946d 145 }
146 if ($defaultteacher = optional_param('defaultteacher', 0, PARAM_INT)) {
3bee1ead 147 $restore->rolesmapping['defaultteacher'] = $defaultteacher;
5e67946d 148 }
149 if ($defaultstudent = optional_param('defaultstudent', 0, PARAM_INT)) {
3bee1ead 150 $restore->rolesmapping['defaultstudent'] = $defaultstudent;
5e67946d 151 }
3bee1ead 152
7c934d06 153 } else {
154 //We have the object, so check if we have a new course_id
155 //passed as parammeter
c3cb69e5 156 $course_id = optional_param('course_id', 0, PARAM_INT);
7c934d06 157 if ($course_id) {
158 $restore->course_id=$course_id;
3f50cab5 159 }
49860445 160 }
3bee1ead 161
49860445 162 // pass in the course category param
6a775a80 163 $restore->restore_restorecatto = optional_param('restore_restorecatto', 0, PARAM_INT);
3bee1ead 164
7c934d06 165 //We have the object with data, put it in the session
166 $SESSION->restore = $restore;
167
cee6968f 168 //From here to the end of the script, only use the $restore object
3f50cab5 169
170 //Check login
171 require_login();
172
93f66983 173 $loginurl = get_login_url();
174
f6459d12
EL
175 //Init restoreuserinfo
176 $restoreuserinfo = false;
177
178
3f50cab5 179 //Check admin
be2a6fc6 180 if (!empty($id)) {
d2940003 181 if (!has_capability('moodle/restore:restorecourse', get_context_instance(CONTEXT_COURSE, $id))) {
93f66983 182 print_error("cannotuseadminadminorteacher", '', $loginurl);
be2a6fc6 183 }
f6459d12 184 $restoreuserinfo = has_capability('moodle/restore:userinfo', get_context_instance(CONTEXT_COURSE, $id));
be2a6fc6 185 } else {
d2940003 186 if (!has_capability('moodle/restore:restorecourse', get_context_instance(CONTEXT_SYSTEM))) {
93f66983 187 print_error("cannotuseadmin", '', $loginurl);
be2a6fc6 188 }
f6459d12 189 $restoreuserinfo = has_capability('moodle/restore:userinfo', get_context_instance(CONTEXT_SYSTEM));
3f50cab5 190 }
191
192 //Check site
3f77c158 193 $site = get_site();
3bee1ead 194
94ba4c9e 195 // Get all the courses the user is able to restore to
df997f84
PS
196 //TODO: use better function which includes all courses for admins
197 $mycourses = get_user_courses_bycap($USER->id, 'moodle/restore:restorecourse');
94ba4c9e 198
199 // Calculate if the user can create courses
200 $cancreatecourses = user_can_create_courses();
201
64acc5a5 202 if (empty($restore->course_id) && ($restore->restoreto == RESTORETO_CURRENT_DELETING || $restore->restoreto == RESTORETO_CURRENT_ADDING)) {
94ba4c9e 203 $restore->course_id = $id; /// Force restore to current course, disabling pick course from list
be2a6fc6 204 }
205
df73f8d4 206 //Set restore->deleting as needed
64acc5a5 207 if ($restore->restoreto == RESTORETO_CURRENT_DELETING || $restore->restoreto == RESTORETO_EXISTING_DELETING) {
df73f8d4 208 $restore->deleting = true;
209 } else {
210 $restore->deleting = false;
be2a6fc6 211 }
7c934d06 212
be2a6fc6 213 //Now, select the course if needed
64acc5a5 214 if (empty($restore->course_id) && ($restore->restoreto == RESTORETO_EXISTING_DELETING || $restore->restoreto == RESTORETO_EXISTING_ADDING)) {
49860445 215
05a71b33 216 if ($courses = $mycourses) {
e11adf48 217 echo $OUTPUT->heading(get_string("choosecourse"));
9549b986 218 echo $OUTPUT->box_start();
7c934d06 219 foreach ($courses as $course) {
d2940003 220 if (!has_capability('moodle/restore:restorecourse', get_context_instance(CONTEXT_COURSE, $course->id))) {
ad822b36 221 continue;
222 }
5db72c05 223 if (empty($course->visible)) {
224 $optdimmed = ' class="dimmed" ';
225 } else {
226 $optdimmed = '';
227 }
db451e99 228 echo "<a $optdimmed href=\"restore.php?course_id=$course->id&amp;launch=check&amp;id=$id&amp;file=$file\">".format_string($course->fullname).' ('.format_string($course->shortname).')</a><br />'."\n";
3f50cab5 229 }
9549b986 230 echo $OUTPUT->box_end();
3f50cab5 231 } else {
e11adf48 232 echo $OUTPUT->heading(get_string("nocoursesyet"));
9549b986 233 echo $OUTPUT->continue_button("$CFG->wwwroot/$CFG->admin/index.php");
3f50cab5 234 }
7c934d06 235 //Checks everything and execute restore
64acc5a5 236 } else if (($restore->restoreto != RESTORETO_NEW_COURSE and !empty($restore->course_id)) or ($restore->restoreto == RESTORETO_NEW_COURSE)) {
ad822b36 237 //Final access control check
64acc5a5 238 if (empty($restore->course_id) and !$cancreatecourses) {
d3b7ea93 239 print_error("cannotrestoreadminorcreator");
d2940003 240 } else if (!empty($restore->course_id) and !has_capability('moodle/restore:restorecourse', get_context_instance(CONTEXT_COURSE, $restore->course_id))) {
d3b7ea93 241 print_error("cannotrestoreadminoredit");
ad822b36 242 }
7c934d06 243 $show_continue_button = true;
b1ee0c86 244 $messages = array();
245
a34947c2 246 //Check and warn if we are restoring over frontpage (SITEID) course. MDL-19163
247 if ($restore->course_id == SITEID) {
64acc5a5 248 if ($restore->restoreto == RESTORETO_CURRENT_DELETING) {
a34947c2 249 $messages[] = get_string ('restoretositedeleting');
64acc5a5 250 } else if ($restore->restoreto == RESTORETO_CURRENT_ADDING) {
a34947c2 251 $messages[] = get_string ('restoretositeadding');
252 }
253 }
254
3bee1ead 255 //Check if we've selected any mod's user info and restore->users
7c934d06 256 //is set to none. Change it to course and inform.
257 if ($restore->users == 2) {
258 $changed = false;
259 $mods = $restore->mods;
260 foreach ($mods as $mod) {
261 if ($mod->userinfo) {
262 $changed = true;
263 }
264 }
8b6ccde7 265 //If we have selected user files or messages or blogs, then users must be restored too
266 if ($restore->user_files || $restore->messages || $restore->blogs) {
6d1e4162 267 $changed = 1;
268 }
7c934d06 269 if ($changed) {
b1ee0c86 270 $messages[] = get_string ("noteuserschangednonetocourse");
7c934d06 271 $restore->users = 1;
272 }
3f50cab5 273 }
b1ee0c86 274
d2940003
EL
275 // Re-enforce 'moodle/restore:restorecourse' at system context to be able to restore all users
276 if ($restore->users == 0 and !has_capability('moodle/restore:restorecourse', get_context_instance(CONTEXT_SYSTEM))) {
a89fcd7a
EL
277 $restore->users = 1; // users => course
278 }
279
f6459d12
EL
280 // Re-enforce moodle/restore:userinfo capability
281 if (!$restoreuserinfo) {
282 $userinfocheck = true;
283 // Confirm that all the settings are properly set to no users
284 // if anything is wrong, message and stop
285 // First global settings
286 if ($restore->users != 2 or $restore->user_files or $restore->messages or $restore->blogs) {
287 $userinfocheck = false;
288
289 // Now all modules userinfo flag
290 } else {
291 $mods = $restore->mods;
292 foreach ($mods as $mod) {
293 if ($mod->userinfo) {
294 $userinfocheck = false;
295 }
296 }
297 }
298
299 if (!$userinfocheck) { // Something was wrong
300 $messages[] = get_string('restoreuserinfofailed');
301 $show_continue_button = false;
302 }
303 }
304
ac94303a
EL
305 // Re-check all the roles creation/mapping stuff
306 if (!empty($restore->course_id)) {
307 $contexttosearch = get_context_instance(CONTEXT_COURSE, $restore->course_id);
308 // if we know the target category, check capability on it
309 } else if (!empty($restore->restore_restorecatto)) {
310 $contexttosearch = get_context_instance(CONTEXT_COURSECAT, $restore->restore_restorecatto);
311 } else {
312 $contexttosearch = get_context_instance(CONTEXT_SYSTEM);
313 }
314 $cancreateroles = has_capability('moodle/role:manage', get_context_instance(CONTEXT_SYSTEM));
315 $canassignroles = has_capability('moodle/role:assign', $contexttosearch);
316 $canoverridecaps= has_capability('moodle/role:override', $contexttosearch) || has_capability('moodle/role:safeoverride', $contexttosearch);
317 if (!empty($restore->rolesmapping)) {
318 foreach ($restore->rolesmapping as $originalrole => $targetrole) {
319 // First, if new role is selected, check used really can do that
320 if (empty($targetrole) && !$cancreateroles) {
321 $messages[] = get_string('restorecannotcreateorassignroles');
322 $show_continue_button = false;
323 break;
324 }
325 // Now, check that the target role is available for role assignments
326 if (!empty($targetrole) && !$canassignroles) {
327 $messages[] = get_string('restorecannotassignroles');
328 $show_continue_button = false;
329 break;
330 }
331 // Now, check that the target role is available for capability overrides
332 if (!empty($targetrole) && !$canoverridecaps) {
333 $messages[] = get_string('restorecannotoverrideperms');
334 $show_continue_button = false;
335 break;
336 }
337 }
338 }
f6459d12 339
b1ee0c86 340 /// If restoring users and backup has mnet remote users and we are restoring to different site, forbid restore to non-admins. MDL-17009
341 if ($restore->users != 2 && /// If restoring users
342 !empty($info->mnet_remoteusers) && $info->mnet_remoteusers === 'true' && /// and backup contains remote users
d61f7b76 343 !backup_is_same_site($info)) { /// and backup is being restored to different site
b1ee0c86 344
345 /// If user is admin (by 'moodle/user:create' cap), warn about conversion to local auth if missing mnet hosts and continue restore
346 if (has_capability('moodle/user:create', get_context_instance(CONTEXT_SYSTEM))) {
347 $messages[] = get_string('mnetrestore_extusers_admin', 'admin');
348 $show_continue_button = true;
349
350 /// else, notify about the thing (suggesting to ask admin) and stop restore
351 } else {
352 $messages[] = get_string('mnetrestore_extusers_noadmin', 'admin');
353 $show_continue_button = false;
354 }
355 }
356
7c934d06 357 //Save the restore session object
358 $SESSION->restore = $restore;
b1ee0c86 359
cf59fbc0 360 // Calculate all session objects checksum and store them in session too
361 // so restore_execute.html (used by manual restore and import) will be
362 // able to detect any problem in session info.
363 restore_save_session_object_checksums($restore, $info, $course_header);
364
b1ee0c86 365 echo "<div style='text-align:center'>";
366
367 /// Printout messages
368 foreach ($messages as $message) {
369 echo '<p>' . $message . '</p>';
370 }
371
372 /// Warning and button
7c934d06 373 if ($show_continue_button) {
374 //Print the continue button to execute the restore NOW !!!!
375 //All is prepared !!!
7c934d06 376 $hidden["launch"] = "execute";
377 $hidden["file"] = $file;
be2a6fc6 378 $hidden["id"] = $id;
f54c4273 379 $hidden["sesskey"] = sesskey();
4e3ea0e0 380 print_string('longtimewarning','admin');
43f853c9 381
b1ee0c86 382
383 /// Restore button
5c2ed7e2 384 echo $OUTPUT->single_button(new moodle_url("restore.php", $hidden), get_string("restorecoursenow"));
3bee1ead 385 }
b1ee0c86 386 echo "</div>";
7c934d06 387
388 //If we are here. Something must be wrong. Debug !!!
389 } else {
390 print_object($restore);
391 print_object($info);
392 print_object($course_header);
771dc7b2 393 print_error('error');
3f50cab5 394 }
7c934d06 395