Whoops - new strings now taken from correct lang file.
[moodle.git] / backup / restore_execute.html
CommitLineData
7ef0797d 1<?php //$Id$
a2c7397c 2 //This page receives the required info and executes the restore
3 //with the parameters suplied. Whe finished, delete temporary
4 //data from backup_tables and temp directory
5
6 //Get objects from session
7 if ($SESSION) {
8 $info = $SESSION->info;
9 $course_header = $SESSION->course_header;
10 $restore = $SESSION->restore;
11 }
12
c9c89845 13 //Add info->original_wwwroot to $restore to be able to use it in all the restore process
14 //(mainly when decoding internal links)
15 $restore->original_wwwroot = $info->original_wwwroot;
16
a2c7397c 17 //Check login
18 require_login();
19
20 //Check admin
be2a6fc6 21 if (!empty($id)) {
63bcc9b5 22 if (!isteacheredit($id)) {
873bf875 23 if (empty($to)) {
24 error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
25 } else {
26 if (!isteacheredit($to)) {
27 error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
28 }
29 }
be2a6fc6 30 }
31 } else {
32 if (!isadmin()) {
33 error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
34 }
a2c7397c 35 }
36
37 //Check site
38 if (!$site = get_site()) {
39 error("Site not found!");
40 }
41
5146bf9f 42 //Checks for the required files/functions to restore every module
43 //and include them
44 if ($allmods = get_records("modules") ) {
45 foreach ($allmods as $mod) {
46 $modname = $mod->name;
9c1ae5ae 47 $modfile = "$CFG->dirroot/mod/$modname/restorelib.php";
5146bf9f 48 //If file exists and we have selected to restore that type of module
49 if ((file_exists($modfile)) and ($restore->mods[$modname]->restore)) {
50 include_once($modfile);
51 }
52 }
53 }
54
a2c7397c 55 //Start the main table
7ef0797d 56 echo "<table cellpadding=\"5\">";
a2c7397c 57 echo "<tr><td>";
58
59 //Start the main ul
60 echo "<ul>";
61
62 //Init status
63 $status = true;
64
65 //Localtion of the xml file
66 $xml_file = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/moodle.xml";
67
68 //If we've selected to restore into new course
69 //create it (course)
70 //Saving conversion id variables into backup_tables
be2a6fc6 71 if ($restore->restoreto == 2) {
67805f89 72 echo "<li>".get_string("creatingnewcourse");
68ba62d2 73 $oldidnumber = $course_header->course_idnumber;
ae5f8de6 74 if (!$status = restore_create_new_course($restore,$course_header)) {
75 notify("Error while creating the new empty course.");
76 }
a2c7397c 77 //Print course fullname and shortname and category
78 if ($status) {
79 echo "<ul>";
1bbf415f 80 echo "<li>".$course_header->course_fullname." (".$course_header->course_shortname.")".'</li>';
81 echo "<li>".get_string("category").": ".$course_header->category->name.'</li>';
68ba62d2 82 if (!empty($oldidnumber)) {
83 echo "<li>".get_string("nomoreidnumber","moodle",$oldidnumber)."</li>";
84 }
1bbf415f 85 echo "</ul></li>";
a2c7397c 86 //Put the destination course_id
87 $restore->course_id = $course_header->course_id;
88 }
89 } else {
90 $course = get_record("course","id",$restore->course_id);
91 if ($course) {
67805f89 92 echo "<li>".get_string("usingexistingcourse");
a2c7397c 93 echo "<ul>";
1bbf415f 94 echo "<li>".get_string("from").": ".$course_header->course_fullname." (".$course_header->course_shortname.")".'</li>';
95 echo "<li>".get_string("to").": ".$course->fullname." (".$course->shortname.")".'</li>';
be2a6fc6 96 if (($restore->deleting)) {
1bbf415f 97 echo "<li>".get_string("deletingexistingcoursedata").'</li>';
be2a6fc6 98 } else {
1bbf415f 99 echo "<li>".get_string("addingdatatoexisting").'</li>';
be2a6fc6 100 }
1bbf415f 101 echo "</ul></li>";
be2a6fc6 102 //If we have selected to restore deleting, we do it now.
103 if ($restore->deleting) {
1bbf415f 104 echo "<li>".get_string("deletingolddata").'</li>';
be2a6fc6 105 $status = remove_course_contents($restore->course_id,false) and
106 delete_dir_contents($CFG->dataroot."/".$restore->course_id,"backupdata");
107 if ($status) {
108 //Now , this situation is equivalent to the "restore to new course" one (we
109 //have a course record and nothing more), so define it as "to new course"
110 $restore->restoreto = 2;
111 } else {
112 notify("An error occurred while deleting some of the course contents.");
113 }
114 }
115 } else {
ae5f8de6 116 notify("Error opening existing course.");
be2a6fc6 117 $status = false;
a2c7397c 118 }
119 }
120
121 //Now create the course_sections and their associated course_modules
64eed73f 122 if ($status) {
be2a6fc6 123 //Into new course
124 if ($restore->restoreto == 2) {
1bbf415f 125 echo "<li>".get_string("creatingsections").'</li>';
ae5f8de6 126 if (!$status = restore_create_sections($restore,$xml_file)) {
127 notify("Error creating sections in the existing course.");
128 }
be2a6fc6 129 //Into existing course
130 } else if ($restore->restoreto == 0 or $restore->restoreto == 1) {
1bbf415f 131 echo "<li>".get_string("checkingsections").'</li>';
ae5f8de6 132 if (!$status = restore_create_sections($restore,$xml_file)) {
133 notify("Error creating sections in the existing course.");
134 }
be2a6fc6 135 //Error
64eed73f 136 } else {
ae5f8de6 137 notify("Neither a new course or an existing one was specified.");
64eed73f 138 $status = false;
139 }
a2c7397c 140 }
141
142 //Now create users as needed
143 if ($status and ($restore->users == 0 or $restore->users == 1)) {
7ef0797d 144 echo "<li>".get_string("creatingusers")."<br />";
ae5f8de6 145 if (!$status = restore_create_users($restore,$xml_file)) {
146 notify("Could not restore users.");
147 }
3f676bea 148 //Now print info about the work done
149 if ($status) {
adc8b0c7 150 $recs = get_records_sql("select old_id, new_id from {$CFG->prefix}backup_ids
3f676bea 151 where backup_code = '$restore->backup_unique_code' and
152 table_name = 'user'");
153 //We've records
154 if ($recs) {
155 $new_count = 0;
156 $exists_count = 0;
157 $student_count = 0;
158 $teacher_count = 0;
11c1d525 159 $counter = 0;
3f676bea 160 //Iterate, filling counters
161 foreach ($recs as $rec) {
adc8b0c7 162 //Get full record, using backup_getids
163 $record = backup_getid($restore->backup_unique_code,"user",$rec->old_id);
164 if (strpos($record->info,"new") !== false) {
3f676bea 165 $new_count++;
166 }
adc8b0c7 167 if (strpos($record->info,"exists") !== false) {
3f676bea 168 $exists_count++;
169 }
0611af10 170 if (strpos($record->info,"student") !== false) {
5c191b96 171 $student_count++;
0611af10 172 } else if (strpos($record->info,"teacher") !== false) {
5c191b96 173 $teacher_count++;
3f676bea 174 }
11c1d525 175 //Do some output
176 $counter++;
177 if ($counter % 10 == 0) {
178 echo ".";
179 if ($counter % 200 == 0) {
7ef0797d 180 echo "<br />";
11c1d525 181 }
182 backup_flush(300);
183 }
3f676bea 184 }
185 //Now print information gathered
67805f89 186 echo " (".get_string("new").": ".$new_count.", ".get_string("existing").": ".$exists_count.")";
3f676bea 187 echo "<ul>";
1bbf415f 188 echo "<li>".get_string("students").": ".$student_count.'</li>';
189 echo "<li>".get_string("teachers").": ".$teacher_count.'</li>';
3f676bea 190 echo "</ul>";
191 } else {
d497a2a2 192 notify("No users were found!");
3f676bea 193 }
194 }
a2c7397c 195 }
196
cee6968f 197 //Now create metacourse info
198 if ($status and $restore->metacourse) {
199 //Only to new courses!
200 if ($restore->restoreto == 2) {
1bbf415f 201 echo "</li><li>".get_string("creatingmetacoursedata");
cee6968f 202 if (!$status = restore_create_metacourse($restore,$xml_file)) {
203 notify("Error creating metacourse in the course.");
204 }
205 }
206 }
207
208
56f56ca0 209 //Now create categories and questions as needed (STEP1)
830ce824 210 if ($status and ($restore->mods['quiz']->restore)) {
0183a6c4 211 echo "</li><li>".get_string("creatingcategoriesandquestions");
212 echo "<ul>";
1bbf415f 213 if (!$status = restore_create_questions($restore,$xml_file)) {
214 notify("Could not restore categories and questions!");
215 }
0183a6c4 216 echo "</ul></li>";
56f56ca0 217 }
218
1bdeb2ca 219 //Now create user_files as needed
3f676bea 220 if ($status and ($restore->user_files)) {
1bbf415f 221 echo "<li>".get_string("copyinguserfiles");
ae5f8de6 222 if (!$status = restore_user_files($restore)) {
223 notify("Could not restore user files!");
224 }
1bdeb2ca 225 //If all is ok (and we have a counter)
226 if ($status and ($status !== true)) {
227 //Inform about user dirs created from backup
228 echo "<ul>";
67805f89 229 echo "<li>".get_string("userzones").": ".$status;
1bbf415f 230 echo "</li></ul>";
1bdeb2ca 231 }
232 }
a2c7397c 233
1bdeb2ca 234 //Now create course files as needed
235 if ($status and ($restore->course_files)) {
1bbf415f 236 echo "</li><li>".get_string("copyingcoursefiles")."</li>";
ae5f8de6 237 if (!$status = restore_course_files($restore)) {
238 notify("Could not restore course files!");
239 }
1bdeb2ca 240 //If all is ok (and we have a counter)
241 if ($status and ($status !== true)) {
242 //Inform about user dirs created from backup
243 echo "<ul>";
67805f89 244 echo "<li>".get_string("filesfolders").": ".$status;
1bdeb2ca 245 echo "</ul>";
246 }
3f676bea 247 }
a2c7397c 248
6d1e4162 249 //Now create messages as needed
250 if ($status and ($restore->messages)) {
251 echo "<li>".get_string("creatingmessagesinfo");
252 if (!$status = restore_create_messages($restore,$xml_file)) {
253 notify("Could not restore messages!");
254 }
255 echo "</li>";
256 }
257
42f1ff47 258 //Now create scales as needed
259 if ($status) {
1bbf415f 260 echo "<li>".get_string("creatingscales").'</li>';
ae5f8de6 261 if (!$status = restore_create_scales($restore,$xml_file)) {
262 notify("Could not restore custom scales!");
263 }
42f1ff47 264 }
265
dfa14bc9 266 //Now create groups as needed
267 if ($status) {
1bbf415f 268 echo "<li>".get_string("creatinggroups").'</li>';
dfa14bc9 269 if (!$status = restore_create_groups($restore,$xml_file)) {
270 notify("Could not restore groups!");
271 }
272 }
273
cd44fdcb 274 //Now create events as needed
275 if ($status) {
1bbf415f 276 echo "<li>".get_string("creatingevents").'</li>';
cd44fdcb 277 if (!$status = restore_create_events($restore,$xml_file)) {
278 notify("Could not restore course events!");
279 }
280 }
281
e3df3ece 282 //Now create course modules as needed
283 if ($status) {
67805f89 284 echo "<li>".get_string("creatingcoursemodules");
ae5f8de6 285 if (!$status = restore_create_modules($restore,$xml_file)) {
bbc69d99 286 notify("Could not restore modules!");
ae5f8de6 287 }
e3df3ece 288 }
289
1fa95628 290 //Bring back the course blocks -- do it AFTER the modules!!!
291 if($status) {
292 //If we are deleting and bringing into a course or making a new course, same situation
293 if($restore->restoreto == 0 || $restore->restoreto == 2) {
294 echo '<li>'.get_string('creatingblocks').'</li>';
295 if (!$status = restore_create_blocks($restore, $info->backup_block_format, $course_header->blockinfo, $xml_file)) {
296 notify('Error while creating the course blocks');
297 }
298 }
299 }
300
1bdeb2ca 301 //Now create log entries as needed
302 if ($status and ($restore->logs)) {
1bbf415f 303 echo "</li><li>".get_string("creatinglogentries");
2ecad742 304 if (!$status = restore_create_logs($restore,$xml_file)) {
305 notify("Could not restore logs!");
306 }
1bdeb2ca 307 }
a2c7397c 308
309 //Now, if all is OK, adjust the instance field in course_modules !!
5146bf9f 310 if ($status) {
1bbf415f 311 echo "</li><li>".get_string("checkinginstances").'</li>';
ae5f8de6 312 if (!$status = restore_check_instances($restore)) {
313 notify("Could not adjust instances in course_modules!");
314 }
5146bf9f 315 }
a2c7397c 316
df9a165e 317 //Now, if all is OK, adjust activity events
318 if ($status) {
1bbf415f 319 echo "<li>".get_string("refreshingevents").'</li>';
df9a165e 320 if (!$status = restore_refresh_events($restore)) {
321 notify("Could not refresh events for activities!");
322 }
323 }
324
56eea82e 325 //Now, if all is OK, adjust inter-activity links
326 if ($status) {
327 echo "<li>".get_string("decodinginternallinks");
328 if (!$status = restore_decode_content_links($restore)) {
329 notify("Could not refresh events for activities!");
330 }
331 }
332
101735cc 333 //Now if all is OK, update:
334 // - course modinfo field
335 // - categories table
50fc80d7 336 // - add user as teacher
5146bf9f 337 if ($status) {
1bbf415f 338 echo "</li><li>".get_string("checkingcourse").'</li>';
101735cc 339 //modinfo field
5146bf9f 340 rebuild_course_cache($restore->course_id);
101735cc 341 //categories table
342 $course = get_record("course","id",$restore->course_id);
6bc502cc 343 fix_course_sortorder();
50fc80d7 344 //Make the user a teacher if the course hasn't teachers (bug 2381)
345 if (!isadmin()) {
346 if (!$checktea = get_records('user_teachers','course', $restore->course_id)) {
347 //Add the teacher to the course
348 $status = add_teacher($USER->id, $restore->course_id);
349 }
350 }
5146bf9f 351 }
352
353 //Cleanup temps (files and db)
bd19b8fa 354 if ($status) {
1bbf415f 355 echo "<li>".get_string("cleaningtempdata").'</li>';
300ae26c 356 if (!$status = clean_temp_data ($restore)) {
357 notify("Could not clean up temporary data from files and database");
358 }
bd19b8fa 359 }
a2c7397c 360
361 //End the main ul
362 echo "</ul>";
363
364
365 //End the main table
1bbf415f 366 echo "</td></tr>";
a2c7397c 367 echo "</table>";
368
369 if (!$status) {
ae5f8de6 370 error ("An error has occurred and the restore could not be completed!");
a2c7397c 371 }
372
873bf875 373 if (!$restore->importing) {
374 echo $output;
375 //Print final message
1bbf415f 376 print_simple_box(get_string("restorefinished"),"center");
873bf875 377 } else {
1bbf415f 378 print_simple_box(get_string("importdatafinished"),"center");
873bf875 379 unset($SESSION->restore);
380 }
9c1ae5ae 381 print_continue("$CFG->wwwroot/course/view.php?id=".$restore->course_id);
11c1d525 382
a2c7397c 383?>