Changed function name
[moodle.git] / backup / restore_execute.html
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;
11     }
13     //Check login
14     require_login();
16     //Check admin
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         }
25     }
27     //Check site
28     if (!$site = get_site()) {
29         error("Site not found!");
30     }
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);
41             }
42         }
43     }
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         $status = restore_create_new_course($restore,$course_header);
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) {
76             echo "<li>".get_string("usingexistingcourse"); 
77             echo "<ul>";
78             echo "<li>".get_string("from").": ".$course_header->course_fullname." (".$course_header->course_shortname.")";
79             echo "<li>".get_string("to").": ".$course->fullname." (".$course->shortname.")";
80             if (($restore->deleting)) {
81                 echo "<li>".get_string("deletingexistingcoursedata");
82             } else {
83                 echo "<li>".get_string("addingdatatoexisting");
84             }
85             echo "</ul>";
86             //If we have selected to restore deleting, we do it now.
87             if ($restore->deleting) {
88                 echo "<li>".get_string("deletingolddata");
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;
101         }
102     }
104     //Now create the course_sections and their associated course_modules
105     if ($status) {
106         //Into new course
107         if ($restore->restoreto == 2) {
108             echo "<li>".get_string("creatingsections");
109             $status = restore_create_sections($restore,$xml_file);
110         //Into existing course
111         } else if ($restore->restoreto == 0 or $restore->restoreto == 1) {
112             echo "<li>".get_string("checkingsections");
113             $status = restore_create_sections($restore,$xml_file);
114         //Error
115         } else {
116             $status = false;
117         }
118     }
121     //Now create users as needed 
122     if ($status and ($restore->users == 0 or $restore->users == 1)) {
123         echo "<li>".get_string("creatingusers")."<br>";
124         $status = restore_create_users($restore,$xml_file);
125         //Now print info about the work done
126         if ($status) {
127             $recs = get_records_sql("select old_id, new_id from {$CFG->prefix}backup_ids
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;
139                 $counter = 0;
140                 //Iterate, filling counters
141                 foreach ($recs as $rec) {
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) {
145                         $new_count++;
146                     } 
147                     if (strpos($record->info,"exists") !== false) {
148                         $exists_count++;
149                     }
150                     if (strpos($record->info,"student")) {
151                         $student_count++;
152                     } else if (strpos($record->info,"teacher")) {
153                         $teacher_count++;
154                     } else if (strpos($record->info,"admin")) {
155                         $admin_count++;
156                     } else if (strpos($record->info,"coursecreator")) {
157                         $coursecreator_count++;
158                     } else if ($record->info == "new" or $record->info == "exists") {
159                         $other_count++;
160                     }
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                     }
170                 }
171                 //Now print information gathered
172                 echo " (".get_string("new").": ".$new_count.", ".get_string("existing").": ".$exists_count.")";
173                 echo "<ul>";
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;
179                 echo "</ul>";
180             } else {
181                 //Something is wrong. There is no users !!
182                 $status = false;
183             }
184         }
185     }
187     //Now create categories and questions as needed (STEP1)
188     if ($status and ($restore->mods[quiz]->restore)) {
189          echo "<li>".get_string("creatingcategoriesandquestions")."<br>";
190          $status = restore_create_questions($restore,$xml_file);
191     }
193     //Now create user_files as needed
194     if ($status and ($restore->user_files)) {
195         echo "<li>".get_string("copyinguserfiles")."<br>";
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>";
201             echo "<li>".get_string("userzones").": ".$status;
202             echo "</ul>";
203         }
204     }
206     //Now create course files as needed
207     if ($status and ($restore->course_files)) {
208         echo "<li>".get_string("copyingcoursefiles")."<br>";
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>";
214             echo "<li>".get_string("filesfolders").": ".$status;
215             echo "</ul>";
216         }       
217     }
219     //Now create course modules as needed
220     if ($status) {
221         echo "<li>".get_string("creatingcoursemodules");
222         $status = restore_create_modules($restore,$xml_file);
223     }
225     //Now create log entries as needed
226     if ($status and ($restore->logs)) {
227         echo "<li>".get_string("creatinglogentries")."<b>(not implemented!!)</b>";
228     }    
230     //Now, if all is OK, adjust the instance field in course_modules !!
231     if ($status) {
232         echo "<li>".get_string("checkinginstances");
233         $status = restore_check_instances($restore);
234     }
236     //Now if all is OK, update:
237     //   - course modinfo field 
238     //   - categories table
239     if ($status) {
240         echo "<li>".get_string("checkingcourse");
241         //modinfo field
242         rebuild_course_cache($restore->course_id);
243         //categories table
244         $course = get_record("course","id",$restore->course_id); 
245         fix_course_sortorder($course->category);
246     }
248     //Cleanup temps (files and db)
249     if ($status) {
250         echo "<li>".get_string("cleaningtempdata");
251         $status = clean_temp_data ($restore);
252     }
254     //End the main ul
255     echo "</ul>";
258     //End the main table     
259     echo "</tr></td>";
260     echo "</table>";
262     if (!$status) {
263         error ("An error has ocurred");
264     }
266     //Print final message
267     print_simple_box(get_string("restorefinished"),"CENTER");
268     print_continue("$CFG->wwwroot/course/view.php?id=".$restore->course_id);
270 ?>