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 error(
"You need to be an admin user to use this page.",
"$CFG->wwwroot/login/index.php");
22 if (!$site = get_site()) {
23 error(
"Site not found!");
26 //Checks for the required files/functions to restore every module
28 if ($allmods = get_records(
"modules") ) {
29 foreach ($allmods as $mod) {
30 $modname = $mod-
>name;
31 $modfile =
"$mods_home/$modname/restorelib.php";
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);
39 //Start the main table
40 echo
"<table cellpadding=5>";
49 //Localtion of the xml file
50 $xml_file = $CFG-
>dataroot.
"/temp/backup/".$restore-
>backup_unique_code.
"/moodle.xml";
52 //If we've selected to restore into new course
54 //Saving conversion id variables into backup_tables
55 if ($restore-
>restoreto ==
1) {
56 echo
"<li>Creating new course";
57 $status = restore_create_new_course($restore,&$course_header);
58 //Print course fullname and shortname and category
61 echo
"<li>".$course_header-
>course_fullname.
" (".$course_header-
>course_shortname.
")";
62 echo
"<li>".get_string(
"category").
": ".$course_header-
>category-
>name;
64 //Put the destination course_id
65 $restore-
>course_id = $course_header-
>course_id;
68 $course = get_record(
"course",
"id",$restore-
>course_id);
70 echo
"<li>Using existing course";
72 echo
"<li>From: ".$course_header-
>course_fullname.
" (".$course_header-
>course_shortname.
")";
73 echo
"<li>To: ".$course-
>fullname.
" (".$course-
>shortname.
")";
78 //Now create the course_sections and their associated course_modules
80 if ($restore-
>restoreto ==
1) {
81 echo
"<li>Creating sections";
82 $status = restore_create_sections($restore,$xml_file);
83 } else if ($restore-
>restoreto ==
0) {
84 echo
"<li>Checking sections";
85 $status = restore_create_sections($restore,$xml_file);
92 //Now create users as needed
93 if ($status and ($restore-
>users ==
0 or $restore-
>users ==
1)) {
94 echo
"<li>Creating users<br>";
95 $status = restore_create_users($restore,$xml_file);
96 //Now print info about the work done
98 $recs = get_records_sql(
"select old_id, new_id from {$CFG->prefix}backup_ids
99 where backup_code = '$restore->backup_unique_code' and
100 table_name = 'user'");
107 $coursecreator_count =
0;
111 //Iterate, filling counters
112 foreach ($recs as $rec) {
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) {
118 if (strpos($record-
>info,
"exists") !== false) {
121 if (strpos($record-
>info,
"student")) {
123 } else if (strpos($record-
>info,
"teacher")) {
125 } else if (strpos($record-
>info,
"admin")) {
127 } else if (strpos($record-
>info,
"coursecreator")) {
128 $coursecreator_count++;
129 } else if ($record-
>info ==
"new" or $record-
>info ==
"exists") {
134 if ($counter %
10 ==
0) {
136 if ($counter %
200 ==
0) {
142 //Now print information gathered
143 echo
" (new: ".$new_count.
", existing: ".$exists_count.
")";
145 echo
"<li>Students: ".$student_count;
146 echo
"<li>Teachers: ".$teacher_count;
147 echo
"<li>Course Creators: ".$coursecreator_count;
148 echo
"<li>Admins: ".$admin_count;
149 echo
"<li>Other: ".$other_count;
152 //Something is wrong. There is no users !!
158 //Now create categories and questions as needed (STEP1)
159 if ($status and ($restore-
>mods[quiz]-
>restore)) {
160 echo
"<li>Creating Categories and Questions<br>";
161 $status = restore_create_questions($restore,$xml_file);
164 //Now create user_files as needed
165 if ($status and ($restore-
>user_files)) {
166 echo
"<li>Copying User Files<br>";
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
172 echo
"<li>User Zones: ".$status;
177 //Now create course files as needed
178 if ($status and ($restore-
>course_files)) {
179 echo
"<li>Copying Course Files<br>";
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
185 echo
"<li>Main Files/Folders: ".$status;
190 //Now create course modules as needed
192 echo
"<li>Creating Course Modules";
193 $status = restore_create_modules($restore,$xml_file);
211 //Now create log entries as needed
212 if ($status and ($restore-
>logs)) {
213 echo
"<li>Creating Log Entries <b>(not implemented!!)</b>";
216 //Now, if all is OK, adjust the instance field in course_modules !!
218 echo
"<li>Checking Instances";
219 $status = restore_check_instances($restore);
222 //Now if all is OK, update course modinfo field !!
224 echo
"<li>Checking Course";
225 rebuild_course_cache($restore-
>course_id);
228 //Cleanup temps (files and db)
230 echo
"<li>Cleaning up temp data";
231 $status = clean_temp_data ($restore);
243 error (
"An error has ocurred");
246 //Print final message
247 print_simple_box(get_string(
"restorefinished"),
"CENTER");
248 print_continue($moodle_home.
"/course/view.php?id=".$restore-
>course_id);