delete_dir_contents function now allows excluding a dir
[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
17 if (!isadmin()) {
18 error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
19 }
20
21 //Check site
22 if (!$site = get_site()) {
23 error("Site not found!");
24 }
25
5146bf9f 26 //Checks for the required files/functions to restore every module
27 //and include them
28 if ($allmods = get_records("modules") ) {
29 foreach ($allmods as $mod) {
30 $modname = $mod->name;
9c1ae5ae 31 $modfile = "$CFG->dirroot/mod/$modname/restorelib.php";
5146bf9f 32 //If file exists and we have selected to restore that type of module
33 if ((file_exists($modfile)) and ($restore->mods[$modname]->restore)) {
34 include_once($modfile);
35 }
36 }
37 }
38
a2c7397c 39 //Start the main table
40 echo "<table cellpadding=5>";
41 echo "<tr><td>";
42
43 //Start the main ul
44 echo "<ul>";
45
46 //Init status
47 $status = true;
48
49 //Localtion of the xml file
50 $xml_file = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/moodle.xml";
51
52 //If we've selected to restore into new course
53 //create it (course)
54 //Saving conversion id variables into backup_tables
55 if ($restore->restoreto ==1) {
67805f89 56 echo "<li>".get_string("creatingnewcourse");
2b968407 57 $status = restore_create_new_course($restore,$course_header);
a2c7397c 58 //Print course fullname and shortname and category
59 if ($status) {
60 echo "<ul>";
61 echo "<li>".$course_header->course_fullname." (".$course_header->course_shortname.")";
62 echo "<li>".get_string("category").": ".$course_header->category->name;
63 echo "</ul>";
64 //Put the destination course_id
65 $restore->course_id = $course_header->course_id;
66 }
67 } else {
68 $course = get_record("course","id",$restore->course_id);
69 if ($course) {
67805f89 70 echo "<li>".get_string("usingexistingcourse");
a2c7397c 71 echo "<ul>";
67805f89 72 echo "<li>".get_string("from").": ".$course_header->course_fullname." (".$course_header->course_shortname.")";
73 echo "<li>".get_string("to").": ".$course->fullname." (".$course->shortname.")";
a2c7397c 74 echo "</ul>";
75 }
76 }
77
78 //Now create the course_sections and their associated course_modules
64eed73f 79 if ($status) {
80 if ($restore->restoreto == 1) {
67805f89 81 echo "<li>".get_string("creatingsections");
64eed73f 82 $status = restore_create_sections($restore,$xml_file);
83 } else if ($restore->restoreto == 0) {
67805f89 84 echo "<li>".get_string("checkingsections");
64eed73f 85 $status = restore_create_sections($restore,$xml_file);
86 } else {
87 $status = false;
88 }
a2c7397c 89 }
90
64eed73f 91
a2c7397c 92 //Now create users as needed
93 if ($status and ($restore->users == 0 or $restore->users == 1)) {
67805f89 94 echo "<li>".get_string("creatingusers")."<br>";
a2c7397c 95 $status = restore_create_users($restore,$xml_file);
3f676bea 96 //Now print info about the work done
97 if ($status) {
adc8b0c7 98 $recs = get_records_sql("select old_id, new_id from {$CFG->prefix}backup_ids
3f676bea 99 where backup_code = '$restore->backup_unique_code' and
100 table_name = 'user'");
101 //We've records
102 if ($recs) {
103 $new_count = 0;
104 $exists_count = 0;
105 $student_count = 0;
106 $teacher_count = 0;
107 $coursecreator_count = 0;
108 $admin_count = 0;
109 $other_count = 0;
11c1d525 110 $counter = 0;
3f676bea 111 //Iterate, filling counters
112 foreach ($recs as $rec) {
adc8b0c7 113 //Get full record, using backup_getids
114 $record = backup_getid($restore->backup_unique_code,"user",$rec->old_id);
115 if (strpos($record->info,"new") !== false) {
3f676bea 116 $new_count++;
117 }
adc8b0c7 118 if (strpos($record->info,"exists") !== false) {
3f676bea 119 $exists_count++;
120 }
adc8b0c7 121 if (strpos($record->info,"student")) {
5c191b96 122 $student_count++;
adc8b0c7 123 } else if (strpos($record->info,"teacher")) {
5c191b96 124 $teacher_count++;
adc8b0c7 125 } else if (strpos($record->info,"admin")) {
3f676bea 126 $admin_count++;
adc8b0c7 127 } else if (strpos($record->info,"coursecreator")) {
3f676bea 128 $coursecreator_count++;
adc8b0c7 129 } else if ($record->info == "new" or $record->info == "exists") {
3f676bea 130 $other_count++;
131 }
11c1d525 132 //Do some output
133 $counter++;
134 if ($counter % 10 == 0) {
135 echo ".";
136 if ($counter % 200 == 0) {
137 echo "<br>";
138 }
139 backup_flush(300);
140 }
3f676bea 141 }
142 //Now print information gathered
67805f89 143 echo " (".get_string("new").": ".$new_count.", ".get_string("existing").": ".$exists_count.")";
3f676bea 144 echo "<ul>";
67805f89 145 echo "<li>".get_string("students").": ".$student_count;
146 echo "<li>".get_string("teachers").": ".$teacher_count;
147 echo "<li>".get_string("coursecreators").": ".$coursecreator_count;
148 echo "<li>".get_string("administrators").": ".$admin_count;
149 echo "<li>".get_string("other").": ".$other_count;
3f676bea 150 echo "</ul>";
151 } else {
152 //Something is wrong. There is no users !!
153 $status = false;
154 }
155 }
a2c7397c 156 }
157
56f56ca0 158 //Now create categories and questions as needed (STEP1)
159 if ($status and ($restore->mods[quiz]->restore)) {
67805f89 160 echo "<li>".get_string("creatingcategoriesandquestions")."<br>";
56f56ca0 161 $status = restore_create_questions($restore,$xml_file);
162 }
163
1bdeb2ca 164 //Now create user_files as needed
3f676bea 165 if ($status and ($restore->user_files)) {
67805f89 166 echo "<li>".get_string("copyinguserfiles")."<br>";
1bdeb2ca 167 $status = restore_user_files($restore);
168 //If all is ok (and we have a counter)
169 if ($status and ($status !== true)) {
170 //Inform about user dirs created from backup
171 echo "<ul>";
67805f89 172 echo "<li>".get_string("userzones").": ".$status;
1bdeb2ca 173 echo "</ul>";
174 }
175 }
a2c7397c 176
1bdeb2ca 177 //Now create course files as needed
178 if ($status and ($restore->course_files)) {
67805f89 179 echo "<li>".get_string("copyingcoursefiles")."<br>";
1bdeb2ca 180 $status = restore_course_files($restore);
181 //If all is ok (and we have a counter)
182 if ($status and ($status !== true)) {
183 //Inform about user dirs created from backup
184 echo "<ul>";
67805f89 185 echo "<li>".get_string("filesfolders").": ".$status;
1bdeb2ca 186 echo "</ul>";
187 }
3f676bea 188 }
a2c7397c 189
e3df3ece 190 //Now create course modules as needed
191 if ($status) {
67805f89 192 echo "<li>".get_string("creatingcoursemodules");
e3df3ece 193 $status = restore_create_modules($restore,$xml_file);
e3df3ece 194 }
195
1bdeb2ca 196 //Now create log entries as needed
197 if ($status and ($restore->logs)) {
67805f89 198 echo "<li>".get_string("creatinglogentries")."<b>(not implemented!!)</b>";
1bdeb2ca 199 }
a2c7397c 200
201 //Now, if all is OK, adjust the instance field in course_modules !!
5146bf9f 202 if ($status) {
67805f89 203 echo "<li>".get_string("checkinginstances");
5146bf9f 204 $status = restore_check_instances($restore);
205 }
a2c7397c 206
101735cc 207 //Now if all is OK, update:
208 // - course modinfo field
209 // - categories table
5146bf9f 210 if ($status) {
67805f89 211 echo "<li>".get_string("checkingcourse");
101735cc 212 //modinfo field
5146bf9f 213 rebuild_course_cache($restore->course_id);
101735cc 214 //categories table
215 $course = get_record("course","id",$restore->course_id);
216 fix_category_courses($course->category);
5146bf9f 217 }
218
219 //Cleanup temps (files and db)
bd19b8fa 220 if ($status) {
67805f89 221 echo "<li>".get_string("cleaningtempdata");
7d40e78c 222 $status = clean_temp_data ($restore);
bd19b8fa 223 }
a2c7397c 224
225 //End the main ul
226 echo "</ul>";
227
228
229 //End the main table
230 echo "</tr></td>";
231 echo "</table>";
232
233 if (!$status) {
234 error ("An error has ocurred");
235 }
236
11c1d525 237 //Print final message
238 print_simple_box(get_string("restorefinished"),"CENTER");
9c1ae5ae 239 print_continue("$CFG->wwwroot/course/view.php?id=".$restore->course_id);
11c1d525 240
a2c7397c 241?>