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
8 $info = $SESSION->info;
9 $course_header = $SESSION->course_header;
10 $restore = $SESSION->restore;
18 if (!isteacher($id)) {
19 error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
23 error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
28 if (!$site = get_site()) {
29 error("Site not found!");
32 //Checks for the required files/functions to restore every module
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>";
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
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
67 echo "<li>".$course_header->course_fullname." (".$course_header->course_shortname.")";
68 echo "<li>".get_string("category").": ".$course_header->category->name;
70 //Put the destination course_id
71 $restore->course_id = $course_header->course_id;
74 $course = get_record("course","id",$restore->course_id);
76 echo "<li>".get_string("usingexistingcourse");
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");
83 echo "<li>".get_string("addingdatatoexisting");
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");
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;
96 notify("An error occurred while deleting some of the course contents.");
104 //Now create the course_sections and their associated course_modules
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);
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
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'");
136 $coursecreator_count = 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) {
147 if (strpos($record->info,"exists") !== false) {
150 if (strpos($record->info,"student")) {
152 } else if (strpos($record->info,"teacher")) {
154 } else if (strpos($record->info,"admin")) {
156 } else if (strpos($record->info,"coursecreator")) {
157 $coursecreator_count++;
158 } else if ($record->info == "new" or $record->info == "exists") {
163 if ($counter % 10 == 0) {
165 if ($counter % 200 == 0) {
171 //Now print information gathered
172 echo " (".get_string("new").": ".$new_count.", ".get_string("existing").": ".$exists_count.")";
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;
181 //Something is wrong. There is no users !!
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);
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
201 echo "<li>".get_string("userzones").": ".$status;
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
214 echo "<li>".get_string("filesfolders").": ".$status;
219 //Now create course modules as needed
221 echo "<li>".get_string("creatingcoursemodules");
222 $status = restore_create_modules($restore,$xml_file);
225 //Now create log entries as needed
226 if ($status and ($restore->logs)) {
227 echo "<li>".get_string("creatinglogentries")."<b>(not implemented!!)</b>";
230 //Now, if all is OK, adjust the instance field in course_modules !!
232 echo "<li>".get_string("checkinginstances");
233 $status = restore_check_instances($restore);
236 //Now if all is OK, update:
237 // - course modinfo field
238 // - categories table
240 echo "<li>".get_string("checkingcourse");
242 rebuild_course_cache($restore->course_id);
244 $course = get_record("course","id",$restore->course_id);
245 fix_course_sortorder($course->category);
248 //Cleanup temps (files and db)
250 echo "<li>".get_string("cleaningtempdata");
251 $status = clean_temp_data ($restore);
263 error ("An error has ocurred");
266 //Print final message
267 print_simple_box(get_string("restorefinished"),"CENTER");
268 print_continue("$CFG->wwwroot/course/view.php?id=".$restore->course_id);