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 (!isteacheredit($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 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
69 echo
"<li>".$course_header-
>course_fullname.
" (".$course_header-
>course_shortname.
")";
70 echo
"<li>".get_string(
"category").
": ".$course_header-
>category-
>name;
72 //Put the destination course_id
73 $restore-
>course_id = $course_header-
>course_id;
76 $course = get_record(
"course",
"id",$restore-
>course_id);
78 echo
"<li>".get_string(
"usingexistingcourse");
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");
85 echo
"<li>".get_string(
"addingdatatoexisting");
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");
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;
98 notify(
"An error occurred while deleting some of the course contents.");
102 notify(
"Error opening existing course.");
107 //Now create the course_sections and their associated course_modules
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.");
123 notify(
"Neither a new course or an existing one was specified.");
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
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'");
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) {
154 if (strpos($record-
>info,
"exists") !== false) {
157 if (strpos($record-
>info,
"student") !== false) {
159 } else if (strpos($record-
>info,
"teacher") !== false) {
164 if ($counter %
10 ==
0) {
166 if ($counter %
200 ==
0) {
172 //Now print information gathered
173 echo
" (".get_string(
"new").
": ".$new_count.
", ".get_string(
"existing").
": ".$exists_count.
")";
175 echo
"<li>".get_string(
"students").
": ".$student_count;
176 echo
"<li>".get_string(
"teachers").
": ".$teacher_count;
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
202 echo
"<li>".get_string(
"userzones").
": ".$status;
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
217 echo
"<li>".get_string(
"filesfolders").
": ".$status;
222 //Now create scales as needed
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
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
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 !!
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
263 echo
"<li>".get_string(
"checkingcourse");
265 rebuild_course_cache($restore-
>course_id);
267 $course = get_record(
"course",
"id",$restore-
>course_id);
268 fix_course_sortorder($course-
>category);
271 //Cleanup temps (files and db)
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");
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);