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