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