Fixes bug MDL-8454, "students can't post to forums"
[moodle-pu.git] / backup / restore_check.html
blobe8d55b38b5112976b5847fc03abd4e17279be13b
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 //If restore session info exists, but we are coming from the form
30 //it has prioriry
31 if (isset($restore) and !empty($fromform)) {
32 unset($restore);
35 // check for session objects
36 if (empty($info) or empty($course_header)) {
37 error( 'important information missing from SESSION' );
40 //If the restore object doesn't exist, we are going
41 //to check every variable individually and create it
43 if (!isset($restore)) {
44 //Check that we have all we need
45 //backup_unique_code
46 $backup_unique_code = required_param( 'backup_unique_code' );
47 //file
48 $file = required_param( 'file');
49 //Checks for the required restoremod parameters
50 if ($allmods = get_records("modules")) {
51 foreach ($allmods as $mod) {
52 $modname = $mod->name;
53 $var = "restore_".$modname;
54 $$var = optional_param( $var,0);
55 $var = "restore_user_info_".$modname;
56 $$var = optional_param( $var,0);
57 $instances = !empty($info->mods[$mod->name]->instances) ? $info->mods[$mod->name]->instances : NULL;
58 if ($instances === NULL) {
59 continue;
61 foreach ($instances as $instance) {
62 $var = 'restore_'.$modname.'_instance_'.$instance->id;
63 $$var = optional_param($var,0);
64 $var = 'restore_user_info_'.$modname.'_instance_'.$instance->id;
65 $$var = optional_param($var,0);
69 //restoreto
70 $restore_restoreto = required_param('restore_restoreto');
71 //restore_metacourse
72 $restore_metacourse = required_param('restore_metacourse');
73 //restore_users
74 $restore_users = required_param('restore_users');
75 //restore_logs
76 $restore_logs = required_param('restore_logs');
77 //restore_user_files
78 $restore_user_files = required_param('restore_user_files');
79 //restore_course_files
80 $restore_course_files = required_param('restore_course_files');
81 //restore_messages
82 $restore_messages = required_param('restore_messages');
84 //Check we've selected a course to restore to
85 $course_id = optional_param('course_id', 0, PARAM_INT);
87 //We are here, having all we need !!
88 //Create the restore object and put it in the session
89 $restore->backup_unique_code = $backup_unique_code;
90 $restore->file = $file;
91 if ($allmods = get_records("modules")) {
92 foreach ($allmods as $mod) {
93 $modname = $mod->name;
94 $var = "restore_".$modname;
95 $restore->mods[$modname]->restore=$$var;
96 $var = "restore_user_info_".$modname;
97 $restore->mods[$modname]->userinfo=$$var;
98 $instances = !empty($info->mods[$mod->name]->instances) ? $info->mods[$mod->name]->instances : NULL;
99 if ($instances === NULL) {
100 continue;
102 foreach ($instances as $instance) {
103 $var = 'restore_'.$modname.'_instance_'.$instance->id;
104 $restore->mods[$modname]->instances[$instance->id]->restore = $$var;
105 $var = 'restore_user_info_'.$modname.'_instance_'.$instance->id;
106 $restore->mods[$modname]->instances[$instance->id]->userinfo = $$var;
110 $restore->restoreto=$restore_restoreto;
111 $restore->metacourse=$restore_metacourse;
112 $restore->users=$restore_users;
113 $restore->logs=$restore_logs;
114 $restore->user_files=$restore_user_files;
115 $restore->course_files=$restore_course_files;
116 $restore->messages=$restore_messages;
117 $restore->course_id=$course_id;
118 //add new vars to restore object
119 $restore->course_startdateoffset = $coursestartdatedateoffset;
120 $restore->course_shortname = $currentcourseshortname;
122 // create role mappings, not sure all should be here
123 if ($data2 = data_submitted()) {
124 foreach ($data2 as $tempname=>$tempdata) {
125 if (strstr($tempname, 'roles_')) {
126 $temprole = explode('_', $tempname);
127 $oldroleid = $temprole[1];
128 $newroleid = $tempdata;
129 $restore->rolesmapping[$oldroleid] = $newroleid;
134 // default role mapping for moodle < 1.7
135 if ($defaultteacheredit = optional_param('defaultteacheredit', 0, PARAM_INT)) {
136 $restore->rolesmapping['defaultteacheredit'] = $defaultteacheredit;
138 if ($defaultteacher = optional_param('defaultteacher', 0, PARAM_INT)) {
139 $restore->rolesmapping['defaultteacher'] = $defaultteacher;
141 if ($defaultstudent = optional_param('defaultstudent', 0, PARAM_INT)) {
142 $restore->rolesmapping['defaultstudent'] = $defaultstudent;
145 } else {
146 //We have the object, so check if we have a new course_id
147 //passed as parammeter
148 $course_id = optional_param('course_id', 0, PARAM_INT);
149 if ($course_id) {
150 $restore->course_id=$course_id;
153 //We have the object with data, put it in the session
154 $SESSION->restore = $restore;
156 //From here to the end of the script, only use the $restore object
158 //Check login
159 require_login();
161 //Check admin
162 if (!empty($id)) {
163 if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $id))) {
164 error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
166 } else {
167 if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
168 error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
172 //Check site
173 if (!$site = get_site()) {
174 error("Site not found!");
177 //Depending the selected restoreto:
178 // If user is a teacher (and not creator):
179 // 0-Current course, deleting: Put $restore->course_id and $restore->deleting (true), create the restore object
180 // 1-Current course, adding: Put $restore->course_id and $restore->deleting (false), create the restore object
181 // If the uses is a creator:
182 // 0-Existing course, deleting: Select the destination course and launch the check again, then
183 // put $restore->course_id and $restore->deleting (true), create the restore object.
184 // 1-Existing course, adding: Select the destination course and launch the check again, then
185 // put $restore->course_id and $restore->deleting (false), create the restore object.
186 // 2-New course: Create the restore object and launch the execute.
188 //If the user is a teacher and not a creator
189 if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
190 $restore->course_id = $id;
191 if ($restore->restoreto == 0) {
192 $restore->deleting = true;
193 } else {
194 $restore->deleting = false;
198 //If the user is a creator (or admin)
199 if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
200 //Set restore->deleting as needed
201 if ($restore->restoreto == 0) {
202 $restore->deleting = true;
203 } else {
204 $restore->deleting = false;
208 //Now, select the course if needed
209 if (($restore->restoreto == 0 or $restore->restoreto == 1) and ($restore->course_id == 0) and has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
210 if ($courses = get_courses("all","c.fullname","c.id,c.fullname,c.shortname,c.visible")) {
211 print_heading(get_string("choosecourse"));
212 print_simple_box_start("center");
213 foreach ($courses as $course) {
214 if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $course->id))) {
215 continue;
217 if (empty($course->visible)) {
218 $optdimmed = ' class="dimmed" ';
219 } else {
220 $optdimmed = '';
222 echo "<a $optdimmed href=\"restore.php?course_id=$course->id&launch=check&id=$id&file=$file\">".s($course->fullname).' ('.s($course->shortname).')</a><br />'."\n";
224 print_simple_box_end();
225 } else {
226 print_heading(get_string("nocoursesyet"));
227 print_continue("$CFG->wwwroot/$CFG->admin/index.php");
229 //Checks everything and execute restore
230 } else if ((($restore->restoreto == 0 or $restore->restoreto == 1) and ($restore->course_id != 0)) or ($restore->restoreto == 2)) {
231 //Final access control check
232 if ($restore->course_id == 0 and !has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
233 error("You need to be a creator or admin to restore into new course!");
234 } else if ($restore->course_id != 0 and !has_capability('moodle/site:backup', get_context_instance(CONTEXT_COURSE, $restore->course_id))) {
235 error("You need to be an edit teacher or admin to restore into selected course!");
237 $show_continue_button = true;
238 //Check if we've selected any mod's user info and restore->users
239 //is set to none. Change it to course and inform.
240 if ($restore->users == 2) {
241 $changed = false;
242 $mods = $restore->mods;
243 foreach ($mods as $mod) {
244 if ($mod->userinfo) {
245 $changed = true;
248 //If we have selected user files or messages, then users must be restored too
249 if ($restore->user_files || $restore->messages) {
250 $changed = 1;
252 if ($changed) {
253 echo get_string ("noteuserschangednonetocourse");
254 echo "<hr noshade size=\"1\">";
255 $restore->users = 1;
258 //Save the restore session object
259 $SESSION->restore = $restore;
260 if ($show_continue_button) {
261 //Print the continue button to execute the restore NOW !!!!
262 //All is prepared !!!
263 echo "<center>";
264 $hidden["launch"] = "execute";
265 $hidden["file"] = $file;
266 $hidden["id"] = $id;
267 print_string('longtimewarning','admin');
269 if ($restore->users && !empty($info->mnet_externalusers)
270 && $info->mnet_externalusers === 'true') {
271 if ($info->original_wwwroot === $CFG->wwwroot) {
272 print '<p>'.get_string('mnetrestore_extusers','admin').'</p>';
273 } else {
274 print '<p>'. get_string('mnetrestore_extusers_mismatch','admin').'</p>';
277 print_single_button("restore.php", $hidden, get_string("restorecoursenow"),"post");
278 echo "</center>";
279 } else {
280 //Show error
281 error ("Something was wrong checking restore preferences");
284 //If we are here. Something must be wrong. Debug !!!
285 } else {
286 print_object($restore);
287 print_object($info);
288 print_object($course_header);
289 error ("Something must be wrong");