Changed function name
[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)) {
18 if (!isteacher($id)) {
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");
2b968407 63 $status = restore_create_new_course($restore,$course_header);
a2c7397c 64 //Print course fullname and shortname and category
65 if ($status) {
66 echo "<ul>";
67 echo "<li>".$course_header->course_fullname." (".$course_header->course_shortname.")";
68 echo "<li>".get_string("category").": ".$course_header->category->name;
69 echo "</ul>";
70 //Put the destination course_id
71 $restore->course_id = $course_header->course_id;
72 }
73 } else {
74 $course = get_record("course","id",$restore->course_id);
75 if ($course) {
67805f89 76 echo "<li>".get_string("usingexistingcourse");
a2c7397c 77 echo "<ul>";
67805f89 78 echo "<li>".get_string("from").": ".$course_header->course_fullname." (".$course_header->course_shortname.")";
79 echo "<li>".get_string("to").": ".$course->fullname." (".$course->shortname.")";
be2a6fc6 80 if (($restore->deleting)) {
81 echo "<li>".get_string("deletingexistingcoursedata");
82 } else {
83 echo "<li>".get_string("addingdatatoexisting");
84 }
a2c7397c 85 echo "</ul>";
be2a6fc6 86 //If we have selected to restore deleting, we do it now.
87 if ($restore->deleting) {
51de923e 88 echo "<li>".get_string("deletingolddata");
be2a6fc6 89 $status = remove_course_contents($restore->course_id,false) and
90 delete_dir_contents($CFG->dataroot."/".$restore->course_id,"backupdata");
91 if ($status) {
92 //Now , this situation is equivalent to the "restore to new course" one (we
93 //have a course record and nothing more), so define it as "to new course"
94 $restore->restoreto = 2;
95 } else {
96 notify("An error occurred while deleting some of the course contents.");
97 }
98 }
99 } else {
100 $status = false;
a2c7397c 101 }
102 }
103
104 //Now create the course_sections and their associated course_modules
64eed73f 105 if ($status) {
be2a6fc6 106 //Into new course
107 if ($restore->restoreto == 2) {
67805f89 108 echo "<li>".get_string("creatingsections");
64eed73f 109 $status = restore_create_sections($restore,$xml_file);
be2a6fc6 110 //Into existing course
111 } else if ($restore->restoreto == 0 or $restore->restoreto == 1) {
67805f89 112 echo "<li>".get_string("checkingsections");
64eed73f 113 $status = restore_create_sections($restore,$xml_file);
be2a6fc6 114 //Error
64eed73f 115 } else {
116 $status = false;
117 }
a2c7397c 118 }
119
64eed73f 120
a2c7397c 121 //Now create users as needed
122 if ($status and ($restore->users == 0 or $restore->users == 1)) {
67805f89 123 echo "<li>".get_string("creatingusers")."<br>";
a2c7397c 124 $status = restore_create_users($restore,$xml_file);
3f676bea 125 //Now print info about the work done
126 if ($status) {
adc8b0c7 127 $recs = get_records_sql("select old_id, new_id from {$CFG->prefix}backup_ids
3f676bea 128 where backup_code = '$restore->backup_unique_code' and
129 table_name = 'user'");
130 //We've records
131 if ($recs) {
132 $new_count = 0;
133 $exists_count = 0;
134 $student_count = 0;
135 $teacher_count = 0;
136 $coursecreator_count = 0;
137 $admin_count = 0;
138 $other_count = 0;
11c1d525 139 $counter = 0;
3f676bea 140 //Iterate, filling counters
141 foreach ($recs as $rec) {
adc8b0c7 142 //Get full record, using backup_getids
143 $record = backup_getid($restore->backup_unique_code,"user",$rec->old_id);
144 if (strpos($record->info,"new") !== false) {
3f676bea 145 $new_count++;
146 }
adc8b0c7 147 if (strpos($record->info,"exists") !== false) {
3f676bea 148 $exists_count++;
149 }
adc8b0c7 150 if (strpos($record->info,"student")) {
5c191b96 151 $student_count++;
adc8b0c7 152 } else if (strpos($record->info,"teacher")) {
5c191b96 153 $teacher_count++;
adc8b0c7 154 } else if (strpos($record->info,"admin")) {
3f676bea 155 $admin_count++;
adc8b0c7 156 } else if (strpos($record->info,"coursecreator")) {
3f676bea 157 $coursecreator_count++;
adc8b0c7 158 } else if ($record->info == "new" or $record->info == "exists") {
3f676bea 159 $other_count++;
160 }
11c1d525 161 //Do some output
162 $counter++;
163 if ($counter % 10 == 0) {
164 echo ".";
165 if ($counter % 200 == 0) {
166 echo "<br>";
167 }
168 backup_flush(300);
169 }
3f676bea 170 }
171 //Now print information gathered
67805f89 172 echo " (".get_string("new").": ".$new_count.", ".get_string("existing").": ".$exists_count.")";
3f676bea 173 echo "<ul>";
67805f89 174 echo "<li>".get_string("students").": ".$student_count;
175 echo "<li>".get_string("teachers").": ".$teacher_count;
176 echo "<li>".get_string("coursecreators").": ".$coursecreator_count;
177 echo "<li>".get_string("administrators").": ".$admin_count;
178 echo "<li>".get_string("other").": ".$other_count;
3f676bea 179 echo "</ul>";
180 } else {
181 //Something is wrong. There is no users !!
182 $status = false;
183 }
184 }
a2c7397c 185 }
186
56f56ca0 187 //Now create categories and questions as needed (STEP1)
188 if ($status and ($restore->mods[quiz]->restore)) {
67805f89 189 echo "<li>".get_string("creatingcategoriesandquestions")."<br>";
56f56ca0 190 $status = restore_create_questions($restore,$xml_file);
191 }
192
1bdeb2ca 193 //Now create user_files as needed
3f676bea 194 if ($status and ($restore->user_files)) {
67805f89 195 echo "<li>".get_string("copyinguserfiles")."<br>";
1bdeb2ca 196 $status = restore_user_files($restore);
197 //If all is ok (and we have a counter)
198 if ($status and ($status !== true)) {
199 //Inform about user dirs created from backup
200 echo "<ul>";
67805f89 201 echo "<li>".get_string("userzones").": ".$status;
1bdeb2ca 202 echo "</ul>";
203 }
204 }
a2c7397c 205
1bdeb2ca 206 //Now create course files as needed
207 if ($status and ($restore->course_files)) {
67805f89 208 echo "<li>".get_string("copyingcoursefiles")."<br>";
1bdeb2ca 209 $status = restore_course_files($restore);
210 //If all is ok (and we have a counter)
211 if ($status and ($status !== true)) {
212 //Inform about user dirs created from backup
213 echo "<ul>";
67805f89 214 echo "<li>".get_string("filesfolders").": ".$status;
1bdeb2ca 215 echo "</ul>";
216 }
3f676bea 217 }
a2c7397c 218
e3df3ece 219 //Now create course modules as needed
220 if ($status) {
67805f89 221 echo "<li>".get_string("creatingcoursemodules");
e3df3ece 222 $status = restore_create_modules($restore,$xml_file);
e3df3ece 223 }
224
1bdeb2ca 225 //Now create log entries as needed
226 if ($status and ($restore->logs)) {
67805f89 227 echo "<li>".get_string("creatinglogentries")."<b>(not implemented!!)</b>";
1bdeb2ca 228 }
a2c7397c 229
230 //Now, if all is OK, adjust the instance field in course_modules !!
5146bf9f 231 if ($status) {
67805f89 232 echo "<li>".get_string("checkinginstances");
5146bf9f 233 $status = restore_check_instances($restore);
234 }
a2c7397c 235
101735cc 236 //Now if all is OK, update:
237 // - course modinfo field
238 // - categories table
5146bf9f 239 if ($status) {
67805f89 240 echo "<li>".get_string("checkingcourse");
101735cc 241 //modinfo field
5146bf9f 242 rebuild_course_cache($restore->course_id);
101735cc 243 //categories table
244 $course = get_record("course","id",$restore->course_id);
6a2d42e6 245 fix_course_sortorder($course->category);
5146bf9f 246 }
247
248 //Cleanup temps (files and db)
bd19b8fa 249 if ($status) {
67805f89 250 echo "<li>".get_string("cleaningtempdata");
7d40e78c 251 $status = clean_temp_data ($restore);
bd19b8fa 252 }
a2c7397c 253
254 //End the main ul
255 echo "</ul>";
256
257
258 //End the main table
259 echo "</tr></td>";
260 echo "</table>";
261
262 if (!$status) {
263 error ("An error has ocurred");
264 }
265
11c1d525 266 //Print final message
267 print_simple_box(get_string("restorefinished"),"CENTER");
9c1ae5ae 268 print_continue("$CFG->wwwroot/course/view.php?id=".$restore->course_id);
11c1d525 269
a2c7397c 270?>