Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / backup / restore_check.html
blob933901fa0bf840b78d0448b0b4a00691d3fddd37
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 = stripslashes_safe($form1->shortname); //"update_ShortName";
22 $course_header->course_fullname = stripslashes_safe($form1->fullname); //"update_FullName";
23 /// Roll dates only if the backup course has a start date
24 /// (some formats like main page, social..., haven't it and rolling dates
25 /// from 0 produces crazy dates. MDL-10125
26 if ($course_header->course_startdate) {
27 $form1->startdate = make_timestamp($form1->startyear, $form1->startmonth, $form1->startday);
28 $currentcoursestartdate = $course_header->course_startdate;
29 $coursestartdatedateoffset = $form1->startdate - $currentcoursestartdate;
30 $restore->course_startdateoffset = $coursestartdatedateoffset; //change to restore
31 } else { // don't roll if the course hasn't start date
32 $coursestartdatedateoffset = 0;
33 $restore->course_startdateoffset = 0;
37 ///Enforce SESSION->course_header rewrite (PHP 4.x needed because assigns are by value) MDL-8298
38 $SESSION->course_header = $course_header;
40 //If restore session info exists, but we are coming from the form
41 //it has prioriry
42 if (isset($restore) and !empty($fromform)) {
43 unset($restore);
46 // check for session objects
47 if (empty($info) or empty($course_header)) {
48 error( 'important information missing from SESSION' );
52 //If the restore object doesn't exist, we are going
53 //to check every variable individually and create it
55 if (!isset($restore)) {
56 //Check that we have all we need
57 //backup_unique_code
58 $backup_unique_code = required_param( 'backup_unique_code' );
59 //file
60 $file = required_param( 'file');
61 //Checks for the required restoremod parameters
62 if ($allmods = get_records("modules")) {
63 foreach ($allmods as $mod) {
64 $modname = $mod->name;
65 $var = "restore_".$modname;
66 $$var = optional_param( $var,0);
67 $var = "restore_user_info_".$modname;
68 $$var = optional_param( $var,0);
69 $instances = !empty($info->mods[$mod->name]->instances) ? $info->mods[$mod->name]->instances : NULL;
70 if ($instances === NULL) {
71 continue;
73 foreach ($instances as $instance) {
74 $var = 'restore_'.$modname.'_instance_'.$instance->id;
75 $$var = optional_param($var,0,PARAM_INT);
76 $var = 'restore_user_info_'.$modname.'_instance_'.$instance->id;
77 $$var = optional_param($var,0,PARAM_INT);
81 //restoreto
82 $restore_restoreto = required_param('restore_restoreto', PARAM_INT);
83 //restore_metacourse
84 $restore_metacourse = required_param('restore_metacourse', PARAM_INT);
85 //restore_users
86 $restore_users = required_param('restore_users', PARAM_INT);
88 $restore_groups = required_param('restore_groups', PARAM_INT);
89 //restore_logs
90 $restore_logs = required_param('restore_logs', PARAM_INT);
91 //restore_user_files
92 $restore_user_files = required_param('restore_user_files', PARAM_INT);
93 //restore_course_files
94 $restore_course_files = required_param('restore_course_files', PARAM_INT);
95 //restore_site_files
96 $restore_site_files = required_param('restore_site_files', PARAM_INT);
97 //restore_gradebook_history
98 $restore_gradebook_history = required_param('restore_gradebook_history', PARAM_INT);
99 //restore_messages
100 $restore_messages = required_param('restore_messages', PARAM_INT);
101 //restore_blogs
102 $restore_blogs = required_param('restore_blogs', PARAM_INT);
104 //Check we've selected a course to restore to
105 $course_id = optional_param('course_id', 0, PARAM_INT);
107 //We are here, having all we need !!
108 //Create the restore object and put it in the session
109 $restore->backup_unique_code = $backup_unique_code;
110 $restore->file = $file;
111 if ($allmods = get_records("modules")) {
112 foreach ($allmods as $mod) {
113 $modname = $mod->name;
114 $var = "restore_".$modname;
115 $restore->mods[$modname]->restore=$$var;
116 $var = "restore_user_info_".$modname;
117 $restore->mods[$modname]->userinfo=$$var;
118 $instances = !empty($info->mods[$mod->name]->instances) ? $info->mods[$mod->name]->instances : NULL;
119 if ($instances === NULL) {
120 continue;
122 foreach ($instances as $instance) {
123 $var = 'restore_'.$modname.'_instance_'.$instance->id;
124 $restore->mods[$modname]->instances[$instance->id]->restore = $$var;
125 $var = 'restore_user_info_'.$modname.'_instance_'.$instance->id;
126 $restore->mods[$modname]->instances[$instance->id]->userinfo = $$var;
130 $restore->restoreto=$restore_restoreto;
131 $restore->metacourse=$restore_metacourse;
132 $restore->users=$restore_users;
133 $restore->groups=$restore_groups;
134 $restore->logs=$restore_logs;
135 $restore->user_files=$restore_user_files;
136 $restore->course_files=$restore_course_files;
137 $restore->site_files=$restore_site_files;
138 $restore->messages=$restore_messages;
139 $restore->blogs=$restore_blogs;
140 $restore->restore_gradebook_history=$restore_gradebook_history;
141 $restore->course_id=$course_id;
142 //add new vars to restore object
143 $restore->course_startdateoffset = $coursestartdatedateoffset;
144 $restore->course_shortname = $currentcourseshortname;
146 // create role mappings, not sure all should be here
147 if ($data2 = data_submitted()) {
148 foreach ($data2 as $tempname=>$tempdata) {
149 if (strstr($tempname, 'roles_')) {
150 $temprole = explode('_', $tempname);
151 $oldroleid = $temprole[1];
152 $newroleid = $tempdata;
153 $restore->rolesmapping[$oldroleid] = $newroleid;
158 // default role mapping for moodle < 1.7
159 if ($defaultteacheredit = optional_param('defaultteacheredit', 0, PARAM_INT)) {
160 $restore->rolesmapping['defaultteacheredit'] = $defaultteacheredit;
162 if ($defaultteacher = optional_param('defaultteacher', 0, PARAM_INT)) {
163 $restore->rolesmapping['defaultteacher'] = $defaultteacher;
165 if ($defaultstudent = optional_param('defaultstudent', 0, PARAM_INT)) {
166 $restore->rolesmapping['defaultstudent'] = $defaultstudent;
169 } else {
170 //We have the object, so check if we have a new course_id
171 //passed as parammeter
172 $course_id = optional_param('course_id', 0, PARAM_INT);
173 if ($course_id) {
174 $restore->course_id=$course_id;
178 // pass in the course category param
179 $restore->restore_restorecatto = optional_param('restore_restorecatto', 0, PARAM_INT);
181 //We have the object with data, put it in the session
182 $SESSION->restore = $restore;
184 //From here to the end of the script, only use the $restore object
186 //Check login
187 require_login();
189 //Check admin
190 if (!empty($id)) {
191 if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $id))) {
192 error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
194 } else {
195 if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_SYSTEM))) {
196 error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
200 //Check site
201 if (!$site = get_site()) {
202 error("Site not found!");
205 //Depending the selected restoreto:
206 // If user is a teacher (and not creator) or we are restoring from within SITEID:
207 // 0-Current course, deleting: Put $restore->course_id and $restore->deleting (true), create the restore object
208 // 1-Current course, adding: Put $restore->course_id and $restore->deleting (false), create the restore object
209 // If the uses is a creator:
210 // 0-Existing course, deleting: Select the destination course and launch the check again, then
211 // put $restore->course_id and $restore->deleting (true), create the restore object.
212 // 1-Existing course, adding: Select the destination course and launch the check again, then
213 // put $restore->course_id and $restore->deleting (false), create the restore object.
214 // 2-New course: Create the restore object and launch the execute.
216 //If the user is a teacher and not a creator or we are restoring from within SITEID
218 if (!user_can_create_courses() || $id == SITEID) {
219 $restore->course_id = $id;
220 if ($restore->restoreto == 0) {
221 $restore->deleting = true;
222 } else {
223 $restore->deleting = false;
227 //If the user is a creator (or admin)
228 if (user_can_create_courses()) {
229 //Set restore->deleting as needed
230 if ($restore->restoreto == 0) {
231 $restore->deleting = true;
232 } else {
233 $restore->deleting = false;
239 // Non-cached - get accessinfo
240 if (isset($USER->access)) {
241 $accessinfo = $USER->access;
242 } else {
243 $accessinfo = get_user_access_sitewide($USER->id);
245 $mycourses = get_user_courses_bycap($USER->id, 'moodle/site:restore', $accessinfo, true, 'c.sortorder ASC', array('id', 'fullname', 'shortname', 'visible'));
247 //Now, select the course if needed
248 //if (($restore->restoreto == 0 or $restore->restoreto == 1) and ($restore->course_id == 0) and get_capability_courses('moodle/site:restore')) {
249 if (($restore->restoreto == 0 or $restore->restoreto == 1) and ($restore->course_id == 0) && count($mycourses) > 1) {
251 if ($courses = $mycourses) {
252 print_heading(get_string("choosecourse"));
253 print_simple_box_start("center");
254 foreach ($courses as $course) {
255 if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $course->id))) {
256 continue;
258 if (empty($course->visible)) {
259 $optdimmed = ' class="dimmed" ';
260 } else {
261 $optdimmed = '';
263 echo "<a $optdimmed href=\"restore.php?course_id=$course->id&amp;launch=check&amp;id=$id&amp;file=$file\">".format_string($course->fullname).' ('.format_string($course->shortname).')</a><br />'."\n";
265 print_simple_box_end();
266 } else {
267 print_heading(get_string("nocoursesyet"));
268 print_continue("$CFG->wwwroot/$CFG->admin/index.php");
270 //Checks everything and execute restore
271 } else if ((($restore->restoreto == 0 or $restore->restoreto == 1) and ($restore->course_id != 0)) or ($restore->restoreto == 2)) {
272 //Final access control check
273 if ($restore->course_id == 0 and !user_can_create_courses()) {
274 error("You need to be a creator or admin to restore into new course!");
275 } else if ($restore->course_id != 0 and !has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $restore->course_id))) {
276 error("You need to be an edit teacher or admin to restore into selected course!");
278 $show_continue_button = true;
279 //Check if we've selected any mod's user info and restore->users
280 //is set to none. Change it to course and inform.
281 if ($restore->users == 2) {
282 $changed = false;
283 $mods = $restore->mods;
284 foreach ($mods as $mod) {
285 if ($mod->userinfo) {
286 $changed = true;
289 //If we have selected user files or messages or blogs, then users must be restored too
290 if ($restore->user_files || $restore->messages || $restore->blogs) {
291 $changed = 1;
293 if ($changed) {
294 echo get_string ("noteuserschangednonetocourse");
295 echo "<hr noshade size=\"1\">";
296 $restore->users = 1;
299 //Save the restore session object
300 $SESSION->restore = $restore;
301 if ($show_continue_button) {
302 //Print the continue button to execute the restore NOW !!!!
303 //All is prepared !!!
304 echo "<div style='text-align:center'>";
305 $hidden["launch"] = "execute";
306 $hidden["file"] = $file;
307 $hidden["id"] = $id;
308 print_string('longtimewarning','admin');
310 if ($restore->users && !empty($info->mnet_externalusers)
311 && $info->mnet_externalusers === 'true') {
312 if ($info->original_wwwroot === $CFG->wwwroot) {
313 print '<p>'.get_string('mnetrestore_extusers','admin').'</p>';
314 } else {
315 print '<p>'. get_string('mnetrestore_extusers_mismatch','admin').'</p>';
318 print_single_button("restore.php", $hidden, get_string("restorecoursenow"),"post");
319 echo "</div>";
320 } else {
321 //Show error
322 error ("Something was wrong checking restore preferences");
325 //If we are here. Something must be wrong. Debug !!!
326 } else {
327 print_object($restore);
328 print_object($info);
329 print_object($course_header);
330 error ("Something must be wrong");