translated & polished some.
[moodle.git] / backup / restore_execute.html
blob500e33ad9bd57d2f5345d4028612a4b52dbae171
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
6 //Get objects from session
7 if ($SESSION) {
8 $info = $SESSION->info;
9 $course_header = $SESSION->course_header;
10 $restore = $SESSION->restore;
13 //Check login
14 require_login();
16 //Check admin
17 if (!empty($id)) {
18 if (!isteacheredit($id)) {
19 error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
21 } else {
22 if (!isadmin()) {
23 error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
27 //Check site
28 if (!$site = get_site()) {
29 error("Site not found!");
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;
37 $modfile = "$CFG->dirroot/mod/$modname/restorelib.php";
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);
45 //Start the main table
46 echo "<table cellpadding=5>";
47 echo "<tr><td>";
49 //Start the main ul
50 echo "<ul>";
52 //Init status
53 $status = true;
55 //Localtion of the xml file
56 $xml_file = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/moodle.xml";
58 //If we've selected to restore into new course
59 //create it (course)
60 //Saving conversion id variables into backup_tables
61 if ($restore->restoreto == 2) {
62 echo "<li>".get_string("creatingnewcourse");
63 if (!$status = restore_create_new_course($restore,$course_header)) {
64 notify("Error while creating the new empty course.");
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;
75 } else {
76 $course = get_record("course","id",$restore->course_id);
77 if ($course) {
78 echo "<li>".get_string("usingexistingcourse");
79 echo "<ul>";
80 echo "<li>".get_string("from").": ".$course_header->course_fullname." (".$course_header->course_shortname.")";
81 echo "<li>".get_string("to").": ".$course->fullname." (".$course->shortname.")";
82 if (($restore->deleting)) {
83 echo "<li>".get_string("deletingexistingcoursedata");
84 } else {
85 echo "<li>".get_string("addingdatatoexisting");
87 echo "</ul>";
88 //If we have selected to restore deleting, we do it now.
89 if ($restore->deleting) {
90 echo "<li>".get_string("deletingolddata");
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.");
101 } else {
102 notify("Error opening existing course.");
103 $status = false;
107 //Now create the course_sections and their associated course_modules
108 if ($status) {
109 //Into new course
110 if ($restore->restoreto == 2) {
111 echo "<li>".get_string("creatingsections");
112 if (!$status = restore_create_sections($restore,$xml_file)) {
113 notify("Error creating sections in the existing course.");
115 //Into existing course
116 } else if ($restore->restoreto == 0 or $restore->restoreto == 1) {
117 echo "<li>".get_string("checkingsections");
118 if (!$status = restore_create_sections($restore,$xml_file)) {
119 notify("Error creating sections in the existing course.");
121 //Error
122 } else {
123 notify("Neither a new course or an existing one was specified.");
124 $status = false;
129 //Now create users as needed
130 if ($status and ($restore->users == 0 or $restore->users == 1)) {
131 echo "<li>".get_string("creatingusers")."<br>";
132 if (!$status = restore_create_users($restore,$xml_file)) {
133 notify("Could not restore users.");
135 //Now print info about the work done
136 if ($status) {
137 $recs = get_records_sql("select old_id, new_id from {$CFG->prefix}backup_ids
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;
146 $counter = 0;
147 //Iterate, filling counters
148 foreach ($recs as $rec) {
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) {
152 $new_count++;
154 if (strpos($record->info,"exists") !== false) {
155 $exists_count++;
157 if (strpos($record->info,"student") !== false) {
158 $student_count++;
159 } else if (strpos($record->info,"teacher") !== false) {
160 $teacher_count++;
162 //Do some output
163 $counter++;
164 if ($counter % 10 == 0) {
165 echo ".";
166 if ($counter % 200 == 0) {
167 echo "<br>";
169 backup_flush(300);
172 //Now print information gathered
173 echo " (".get_string("new").": ".$new_count.", ".get_string("existing").": ".$exists_count.")";
174 echo "<ul>";
175 echo "<li>".get_string("students").": ".$student_count;
176 echo "<li>".get_string("teachers").": ".$teacher_count;
177 echo "</ul>";
178 } else {
179 notify("No users were found!");
184 //Now create categories and questions as needed (STEP1)
185 if ($status and ($restore->mods['quiz']->restore)) {
186 echo "<li>".get_string("creatingcategoriesandquestions")."<br>";
187 if (!$status = restore_create_questions($restore,$xml_file)) {
188 notify("Could not restore categories and questions!");
192 //Now create user_files as needed
193 if ($status and ($restore->user_files)) {
194 echo "<li>".get_string("copyinguserfiles")."<br>";
195 if (!$status = restore_user_files($restore)) {
196 notify("Could not restore user files!");
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>";
202 echo "<li>".get_string("userzones").": ".$status;
203 echo "</ul>";
207 //Now create course files as needed
208 if ($status and ($restore->course_files)) {
209 echo "<li>".get_string("copyingcoursefiles")."<br>";
210 if (!$status = restore_course_files($restore)) {
211 notify("Could not restore course files!");
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>";
217 echo "<li>".get_string("filesfolders").": ".$status;
218 echo "</ul>";
222 //Now create scales as needed
223 if ($status) {
224 echo "<li>".get_string("creatingscales");
225 if (!$status = restore_create_scales($restore,$xml_file)) {
226 notify("Could not restore custom scales!");
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!");
238 //Now create course modules as needed
239 if ($status) {
240 echo "<li>".get_string("creatingcoursemodules");
241 if (!$status = restore_create_modules($restore,$xml_file)) {
242 notify("Could not restore modules!");
246 //Now create log entries as needed
247 if ($status and ($restore->logs)) {
248 echo "<li>".get_string("creatinglogentries")."<b> (not implemented!!)</b>";
251 //Now, if all is OK, adjust the instance field in course_modules !!
252 if ($status) {
253 echo "<li>".get_string("checkinginstances");
254 if (!$status = restore_check_instances($restore)) {
255 notify("Could not adjust instances in course_modules!");
259 //Now if all is OK, update:
260 // - course modinfo field
261 // - categories table
262 if ($status) {
263 echo "<li>".get_string("checkingcourse");
264 //modinfo field
265 rebuild_course_cache($restore->course_id);
266 //categories table
267 $course = get_record("course","id",$restore->course_id);
268 fix_course_sortorder($course->category);
271 //Cleanup temps (files and db)
272 if ($status) {
273 echo "<li>".get_string("cleaningtempdata");
274 if (!$status = clean_temp_data ($restore)) {
275 notify("Could not clean up temporary data from files and database");
279 //End the main ul
280 echo "</ul>";
283 //End the main table
284 echo "</tr></td>";
285 echo "</table>";
287 if (!$status) {
288 error ("An error has occurred and the restore could not be completed!");
291 //Print final message
292 print_simple_box(get_string("restorefinished"),"CENTER");
293 print_continue("$CFG->wwwroot/course/view.php?id=".$restore->course_id);