MDL-9268
[moodle-pu.git] / backup / restore_check.html
blob8e8fd102006d640d500ca6af47e6d6a45113485a
1 <?php //$Id$
2 //This page receive all the restore_form data. Then, if existing course
3 //has been selected, shows a list of courses to select one.
4 //It cheks that the parammeter from restore_form are coherent.
5 //It puts all the restore info in the session.
6 //Finally, it calls restore_execute to do the hard work
7 //Get objects from session
8 if ($SESSION) {
9 $info = $SESSION->info;
10 $course_header = $SESSION->course_header;
11 if (isset($SESSION->restore)) {
12 $restore = $SESSION->restore;
16 //Detect if we are coming from the restore form
17 $fromform = optional_param ('fromform', 0, PARAM_INT);
19 if ($form1 = data_submitted()) {
20 $currentcourseshortname = $course_header->course_shortname; //"store_ShortName";
21 $course_header->course_shortname = $form1->shortname; //"update_ShortName";
22 $course_header->course_fullname = $form1->fullname; //"update_FullName";
23 $form1->startdate = make_timestamp($form1->startyear, $form1->startmonth, $form1->startday);
24 $currentcoursestartdate = $course_header->course_startdate;
25 $coursestartdatedateoffset = $form1->startdate - $currentcoursestartdate;
26 $restore->course_startdateoffset = $coursestartdatedateoffset; //change to restore
29 ///Enforce SESSION->course_header rewrite (PHP 4.x needed because assigns are by value) MDL-8298
30 $SESSION->course_header = $course_header;
32 //If restore session info exists, but we are coming from the form
33 //it has prioriry
34 if (isset($restore) and !empty($fromform)) {
35 unset($restore);
38 // check for session objects
39 if (empty($info) or empty($course_header)) {
40 error( 'important information missing from SESSION' );
44 //If the restore object doesn't exist, we are going
45 //to check every variable individually and create it
47 if (!isset($restore)) {
48 //Check that we have all we need
49 //backup_unique_code
50 $backup_unique_code = required_param( 'backup_unique_code' );
51 //file
52 $file = required_param( 'file');
53 //Checks for the required restoremod parameters
54 if ($allmods = get_records("modules")) {
55 foreach ($allmods as $mod) {
56 $modname = $mod->name;
57 $var = "restore_".$modname;
58 $$var = optional_param( $var,0);
59 $var = "restore_user_info_".$modname;
60 $$var = optional_param( $var,0);
61 $instances = !empty($info->mods[$mod->name]->instances) ? $info->mods[$mod->name]->instances : NULL;
62 if ($instances === NULL) {
63 continue;
65 foreach ($instances as $instance) {
66 $var = 'restore_'.$modname.'_instance_'.$instance->id;
67 $$var = optional_param($var,0);
68 $var = 'restore_user_info_'.$modname.'_instance_'.$instance->id;
69 $$var = optional_param($var,0);
73 //restoreto
74 $restore_restoreto = required_param('restore_restoreto');
75 //restore_metacourse
76 $restore_metacourse = required_param('restore_metacourse');
77 //restore_users
78 $restore_users = required_param('restore_users');
79 //restore_logs
80 $restore_logs = required_param('restore_logs');
81 //restore_user_files
82 $restore_user_files = required_param('restore_user_files');
83 //restore_course_files
84 $restore_course_files = required_param('restore_course_files');
85 //restore_messages
86 $restore_messages = required_param('restore_messages');
88 //Check we've selected a course to restore to
89 $course_id = optional_param('course_id', 0, PARAM_INT);
91 //We are here, having all we need !!
92 //Create the restore object and put it in the session
93 $restore->backup_unique_code = $backup_unique_code;
94 $restore->file = $file;
95 if ($allmods = get_records("modules")) {
96 foreach ($allmods as $mod) {
97 $modname = $mod->name;
98 $var = "restore_".$modname;
99 $restore->mods[$modname]->restore=$$var;
100 $var = "restore_user_info_".$modname;
101 $restore->mods[$modname]->userinfo=$$var;
102 $instances = !empty($info->mods[$mod->name]->instances) ? $info->mods[$mod->name]->instances : NULL;
103 if ($instances === NULL) {
104 continue;
106 foreach ($instances as $instance) {
107 $var = 'restore_'.$modname.'_instance_'.$instance->id;
108 $restore->mods[$modname]->instances[$instance->id]->restore = $$var;
109 $var = 'restore_user_info_'.$modname.'_instance_'.$instance->id;
110 $restore->mods[$modname]->instances[$instance->id]->userinfo = $$var;
114 $restore->restoreto=$restore_restoreto;
115 $restore->metacourse=$restore_metacourse;
116 $restore->users=$restore_users;
117 $restore->logs=$restore_logs;
118 $restore->user_files=$restore_user_files;
119 $restore->course_files=$restore_course_files;
120 $restore->messages=$restore_messages;
121 $restore->course_id=$course_id;
122 //add new vars to restore object
123 $restore->course_startdateoffset = $coursestartdatedateoffset;
124 $restore->course_shortname = $currentcourseshortname;
126 // create role mappings, not sure all should be here
127 if ($data2 = data_submitted()) {
128 foreach ($data2 as $tempname=>$tempdata) {
129 if (strstr($tempname, 'roles_')) {
130 $temprole = explode('_', $tempname);
131 $oldroleid = $temprole[1];
132 $newroleid = $tempdata;
133 $restore->rolesmapping[$oldroleid] = $newroleid;
138 // default role mapping for moodle < 1.7
139 if ($defaultteacheredit = optional_param('defaultteacheredit', 0, PARAM_INT)) {
140 $restore->rolesmapping['defaultteacheredit'] = $defaultteacheredit;
142 if ($defaultteacher = optional_param('defaultteacher', 0, PARAM_INT)) {
143 $restore->rolesmapping['defaultteacher'] = $defaultteacher;
145 if ($defaultstudent = optional_param('defaultstudent', 0, PARAM_INT)) {
146 $restore->rolesmapping['defaultstudent'] = $defaultstudent;
149 } else {
150 //We have the object, so check if we have a new course_id
151 //passed as parammeter
152 $course_id = optional_param('course_id', 0, PARAM_INT);
153 if ($course_id) {
154 $restore->course_id=$course_id;
158 // pass in the course category param
159 $cat_id = optional_param('restore_restorecatto', 0, PARAM_INT);
160 if ($cat_id) {
161 $restore->restore_restorecatto = $cat_id;
164 //We have the object with data, put it in the session
165 $SESSION->restore = $restore;
167 //From here to the end of the script, only use the $restore object
169 //Check login
170 require_login();
172 //Check admin
173 if (!empty($id)) {
174 if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $id))) {
175 error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
177 } else {
178 if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
179 error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
183 //Check site
184 if (!$site = get_site()) {
185 error("Site not found!");
188 //Depending the selected restoreto:
189 // If user is a teacher (and not creator):
190 // 0-Current course, deleting: Put $restore->course_id and $restore->deleting (true), create the restore object
191 // 1-Current course, adding: Put $restore->course_id and $restore->deleting (false), create the restore object
192 // If the uses is a creator:
193 // 0-Existing course, deleting: Select the destination course and launch the check again, then
194 // put $restore->course_id and $restore->deleting (true), create the restore object.
195 // 1-Existing course, adding: Select the destination course and launch the check again, then
196 // put $restore->course_id and $restore->deleting (false), create the restore object.
197 // 2-New course: Create the restore object and launch the execute.
199 //If the user is a teacher and not a creator
202 //if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
204 if (!user_can_create_courses()) {
205 $restore->course_id = $id;
206 if ($restore->restoreto == 0) {
207 $restore->deleting = true;
208 } else {
209 $restore->deleting = false;
213 //If the user is a creator (or admin)
214 //if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
215 if (user_can_create_courses()) {
216 //Set restore->deleting as needed
217 if ($restore->restoreto == 0) {
218 $restore->deleting = true;
219 } else {
220 $restore->deleting = false;
224 //Now, select the course if needed
225 //if (($restore->restoreto == 0 or $restore->restoreto == 1) and ($restore->course_id == 0) and get_capability_courses('moodle/site:restore')) {
226 if (($restore->restoreto == 0 or $restore->restoreto == 1) and ($restore->course_id == 0)) {
228 if ($courses = get_courses("all","c.fullname","c.id,c.fullname,c.shortname,c.visible")) {
229 print_heading(get_string("choosecourse"));
230 print_simple_box_start("center");
231 foreach ($courses as $course) {
232 if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $course->id))) {
233 continue;
235 if (empty($course->visible)) {
236 $optdimmed = ' class="dimmed" ';
237 } else {
238 $optdimmed = '';
240 echo "<a $optdimmed href=\"restore.php?course_id=$course->id&launch=check&id=$id&file=$file\">".format_string($course->fullname).' ('.format_string($course->shortname).')</a><br />'."\n";
242 print_simple_box_end();
243 } else {
244 print_heading(get_string("nocoursesyet"));
245 print_continue("$CFG->wwwroot/$CFG->admin/index.php");
247 //Checks everything and execute restore
248 } else if ((($restore->restoreto == 0 or $restore->restoreto == 1) and ($restore->course_id != 0)) or ($restore->restoreto == 2)) {
249 //Final access control check
250 if ($restore->course_id == 0 and !user_can_create_courses()) {
251 error("You need to be a creator or admin to restore into new course!");
252 } else if ($restore->course_id != 0 and !has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $restore->course_id))) {
253 error("You need to be an edit teacher or admin to restore into selected course!");
255 $show_continue_button = true;
256 //Check if we've selected any mod's user info and restore->users
257 //is set to none. Change it to course and inform.
258 if ($restore->users == 2) {
259 $changed = false;
260 $mods = $restore->mods;
261 foreach ($mods as $mod) {
262 if ($mod->userinfo) {
263 $changed = true;
266 //If we have selected user files or messages, then users must be restored too
267 if ($restore->user_files || $restore->messages) {
268 $changed = 1;
270 if ($changed) {
271 echo get_string ("noteuserschangednonetocourse");
272 echo "<hr noshade size=\"1\">";
273 $restore->users = 1;
276 //Save the restore session object
277 $SESSION->restore = $restore;
278 if ($show_continue_button) {
279 //Print the continue button to execute the restore NOW !!!!
280 //All is prepared !!!
281 echo "<div style='text-align:center'>";
282 $hidden["launch"] = "execute";
283 $hidden["file"] = $file;
284 $hidden["id"] = $id;
285 print_string('longtimewarning','admin');
287 if ($restore->users && !empty($info->mnet_externalusers)
288 && $info->mnet_externalusers === 'true') {
289 if ($info->original_wwwroot === $CFG->wwwroot) {
290 print '<p>'.get_string('mnetrestore_extusers','admin').'</p>';
291 } else {
292 print '<p>'. get_string('mnetrestore_extusers_mismatch','admin').'</p>';
295 print_single_button("restore.php", $hidden, get_string("restorecoursenow"),"post");
296 echo "</div>";
297 } else {
298 //Show error
299 error ("Something was wrong checking restore preferences");
302 //If we are here. Something must be wrong. Debug !!!
303 } else {
304 print_object($restore);
305 print_object($info);
306 print_object($course_header);
307 error ("Something must be wrong");