3 //This page receive all the restore_form data. Then, if existing course
4 //has been selected, shows a list of courses to select one.
5 //It cheks that the parammeter from restore_form are coherent.
6 //It puts all the restore info in the session.
7 //Finally, it calls restore_execute to do the hard work
8 //Get objects from session
10 $info = $SESSION->info
;
11 $course_header = $SESSION->course_header
;
12 if (isset($SESSION->restore
)) {
13 $restore = $SESSION->restore
;
17 //Detect if we are coming from the restore form
18 $fromform = optional_param ('fromform', 0, PARAM_INT
);
20 if ($form1 = data_submitted()) {
21 $currentcourseshortname = $course_header->course_shortname
; //"store_ShortName";
22 $course_header->course_shortname
= !empty($form1->shortname
) ?
stripslashes_safe($form1->shortname
) : ''; //"update_ShortName";
23 $course_header->course_fullname
= !empty($form1->fullname
) ?
stripslashes_safe($form1->fullname
) : ''; //"update_FullName";
24 /// Roll dates only if the backup course has a start date
25 /// (some formats like main page, social..., haven't it and rolling dates
26 /// from 0 produces crazy dates. MDL-10125 and we have passed some custom startyear/month/day. MDL-12922
27 if ($course_header->course_startdate
&& !empty($form1->startyear
)) {
28 $form1->startdate
= make_timestamp($form1->startyear
, $form1->startmonth
, $form1->startday
);
29 $currentcoursestartdate = $course_header->course_startdate
;
30 $coursestartdatedateoffset = $form1->startdate
- $currentcoursestartdate;
31 $restore->course_startdateoffset
= $coursestartdatedateoffset; //change to restore
32 // Only apply rolling of dates if differences are bigger than one day
33 // that should solve current problems with daylight changes between
35 if (abs($restore->course_startdateoffset
) < 24 * 60 * 60) {
36 $coursestartdatedateoffset = 0;
37 $restore->course_startdateoffset
= 0;
39 } else { // don't roll if the course hasn't start date
40 $coursestartdatedateoffset = 0;
41 $restore->course_startdateoffset
= 0;
45 ///Enforce SESSION->course_header rewrite (PHP 4.x needed because assigns are by value) MDL-8298
46 $SESSION->course_header
= $course_header;
48 //If restore session info exists, but we are coming from the form
50 if (isset($restore) and !empty($fromform)) {
54 // check for session objects
55 if (empty($info) or empty($course_header)) {
56 error( 'important information missing from SESSION' );
60 //If the restore object doesn't exist, we are going
61 //to check every variable individually and create it
63 if (!isset($restore)) {
64 //Check that we have all we need
66 $backup_unique_code = required_param( 'backup_unique_code' );
68 $file = required_param( 'file');
69 //Checks for the required restoremod parameters
70 if ($allmods = get_records("modules")) {
71 foreach ($allmods as $mod) {
72 $modname = $mod->name
;
73 $var = "restore_".$modname;
74 $
$var = optional_param( $var,0);
75 $var = "restore_user_info_".$modname;
76 $
$var = optional_param( $var,0);
77 $instances = !empty($info->mods
[$mod->name
]->instances
) ?
$info->mods
[$mod->name
]->instances
: NULL;
78 if ($instances === NULL) {
81 foreach ($instances as $instance) {
82 $var = 'restore_'.$modname.'_instance_'.$instance->id
;
83 $
$var = optional_param($var,0,PARAM_INT
);
84 $var = 'restore_user_info_'.$modname.'_instance_'.$instance->id
;
85 $
$var = optional_param($var,0,PARAM_INT
);
90 $restore_restoreto = required_param('restore_restoreto', PARAM_INT
);
92 $restore_metacourse = required_param('restore_metacourse', PARAM_INT
);
94 $restore_users = required_param('restore_users', PARAM_INT
);
96 $restore_groups = required_param('restore_groups', PARAM_INT
);
98 $restore_logs = required_param('restore_logs', PARAM_INT
);
100 $restore_user_files = required_param('restore_user_files', PARAM_INT
);
101 //restore_course_files
102 $restore_course_files = required_param('restore_course_files', PARAM_INT
);
104 $restore_site_files = required_param('restore_site_files', PARAM_INT
);
105 //restore_gradebook_history
106 $restore_gradebook_history = required_param('restore_gradebook_history', PARAM_INT
);
108 $restore_messages = required_param('restore_messages', PARAM_INT
);
110 $restore_blogs = required_param('restore_blogs', PARAM_INT
);
112 //Check we've selected a course to restore to
113 $course_id = optional_param('course_id', 0, PARAM_INT
);
115 //We are here, having all we need !!
116 //Create the restore object and put it in the session
117 $restore->backup_unique_code
= $backup_unique_code;
118 $restore->file
= $file;
119 if ($allmods = get_records("modules")) {
120 foreach ($allmods as $mod) {
121 $modname = $mod->name
;
122 $var = "restore_".$modname;
123 $restore->mods
[$modname]->restore
=$
$var;
124 $var = "restore_user_info_".$modname;
125 $restore->mods
[$modname]->userinfo
=$
$var;
126 $instances = !empty($info->mods
[$mod->name
]->instances
) ?
$info->mods
[$mod->name
]->instances
: NULL;
127 if ($instances === NULL) {
130 foreach ($instances as $instance) {
131 $var = 'restore_'.$modname.'_instance_'.$instance->id
;
132 $restore->mods
[$modname]->instances
[$instance->id
]->restore
= $
$var;
133 $var = 'restore_user_info_'.$modname.'_instance_'.$instance->id
;
134 $restore->mods
[$modname]->instances
[$instance->id
]->userinfo
= $
$var;
138 $restore->restoreto
=$restore_restoreto;
139 $restore->metacourse
=$restore_metacourse;
140 $restore->users
=$restore_users;
141 $restore->groups
=$restore_groups;
142 $restore->logs
=$restore_logs;
143 $restore->user_files
=$restore_user_files;
144 $restore->course_files
=$restore_course_files;
145 $restore->site_files
=$restore_site_files;
146 $restore->messages
=$restore_messages;
147 $restore->blogs
=$restore_blogs;
148 $restore->restore_gradebook_history
=$restore_gradebook_history;
149 $restore->course_id
=$course_id;
150 //add new vars to restore object
151 $restore->course_startdateoffset
= $coursestartdatedateoffset;
152 $restore->course_shortname
= $currentcourseshortname;
154 // create role mappings, not sure all should be here
155 if ($data2 = data_submitted()) {
156 foreach ($data2 as $tempname=>$tempdata) {
157 if (strstr($tempname, 'roles_')) {
158 $temprole = explode('_', $tempname);
159 $oldroleid = $temprole[1];
160 $newroleid = $tempdata;
161 $restore->rolesmapping
[$oldroleid] = $newroleid;
166 // default role mapping for moodle < 1.7
167 if ($defaultteacheredit = optional_param('defaultteacheredit', 0, PARAM_INT
)) {
168 $restore->rolesmapping
['defaultteacheredit'] = $defaultteacheredit;
170 if ($defaultteacher = optional_param('defaultteacher', 0, PARAM_INT
)) {
171 $restore->rolesmapping
['defaultteacher'] = $defaultteacher;
173 if ($defaultstudent = optional_param('defaultstudent', 0, PARAM_INT
)) {
174 $restore->rolesmapping
['defaultstudent'] = $defaultstudent;
178 //We have the object, so check if we have a new course_id
179 //passed as parammeter
180 $course_id = optional_param('course_id', 0, PARAM_INT
);
182 $restore->course_id
=$course_id;
186 // pass in the course category param
187 $restore->restore_restorecatto
= optional_param('restore_restorecatto', 0, PARAM_INT
);
189 //We have the object with data, put it in the session
190 $SESSION->restore
= $restore;
192 //From here to the end of the script, only use the $restore object
199 if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE
, $id))) {
200 error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
203 if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_SYSTEM
))) {
204 error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
209 if (!$site = get_site()) {
210 error("Site not found!");
213 // Non-cached - get accessinfo
214 if (isset($USER->access
)) {
215 $accessinfo = $USER->access
;
217 $accessinfo = get_user_access_sitewide($USER->id
);
220 // Get all the courses the user is able to restore to
221 $mycourses = get_user_courses_bycap($USER->id
, 'moodle/site:restore', $accessinfo, true, 'c.sortorder ASC', array('id', 'fullname', 'shortname', 'visible'));
223 // Calculate if the user can create courses
224 $cancreatecourses = user_can_create_courses();
226 if (empty($restore->course_id
) && ($restore->restoreto
== RESTORETO_CURRENT_DELETING ||
$restore->restoreto
== RESTORETO_CURRENT_ADDING
)) {
227 $restore->course_id
= $id; /// Force restore to current course, disabling pick course from list
230 //Set restore->deleting as needed
231 if ($restore->restoreto
== RESTORETO_CURRENT_DELETING ||
$restore->restoreto
== RESTORETO_EXISTING_DELETING
) {
232 $restore->deleting
= true;
234 $restore->deleting
= false;
237 //Now, select the course if needed
238 if (empty($restore->course_id
) && ($restore->restoreto
== RESTORETO_EXISTING_DELETING ||
$restore->restoreto
== RESTORETO_EXISTING_ADDING
)) {
240 if ($courses = $mycourses) {
241 print_heading(get_string("choosecourse"));
242 print_simple_box_start("center");
243 foreach ($courses as $course) {
244 if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE
, $course->id
))) {
247 if (empty($course->visible
)) {
248 $optdimmed = ' class="dimmed" ';
252 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";
254 print_simple_box_end();
256 print_heading(get_string("nocoursesyet"));
257 print_continue("$CFG->wwwroot/$CFG->admin/index.php");
259 //Checks everything and execute restore
260 } else if (($restore->restoreto
!= RESTORETO_NEW_COURSE
and !empty($restore->course_id
)) or ($restore->restoreto
== RESTORETO_NEW_COURSE
)) {
261 //Final access control check
262 if (empty($restore->course_id
) and !$cancreatecourses) {
263 error("You need to be a creator or admin to restore into new course!");
264 } else if (!empty($restore->course_id
) and !has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE
, $restore->course_id
))) {
265 error("You need to be an edit teacher or admin to restore into selected course!");
267 $show_continue_button = true;
270 //Check and warn if we are restoring over frontpage (SITEID) course. MDL-19163
271 if ($restore->course_id
== SITEID
) {
272 if ($restore->restoreto
== RESTORETO_CURRENT_DELETING
) {
273 $messages[] = get_string ('restoretositedeleting');
274 } else if ($restore->restoreto
== RESTORETO_CURRENT_ADDING
) {
275 $messages[] = get_string ('restoretositeadding');
279 // If we have selected to roll dates on restore and the user is missing
280 // such capability (moodle/restore:rolldates) in the target course/category,
281 // disable roll of dates and warn
282 if ($restore->course_startdateoffset
!= 0) {
283 $canrolldates = false; // init to false
285 // if we know the target course, check the capability on it
286 if (!empty($restore->course_id
)) {
287 $canrolldates = has_capability('moodle/restore:rolldates', get_context_instance(CONTEXT_COURSE
, $restore->course_id
));
288 // if we know the target category, check capability on it
289 } else if (!empty($restore->restore_restorecatto
)) {
290 $canrolldates = has_capability('moodle/restore:rolldates', get_context_instance(CONTEXT_COURSECAT
, $restore->restore_restorecatto
));
293 if (!$canrolldates) {
294 $messages[] = get_string ("noteusercannotrolldatesoncontext");
295 $restore->course_startdateoffset
= 0;
299 //Check if we've selected any mod's user info and restore->users
300 //is set to none. Change it to course and inform.
301 if ($restore->users
== 2) {
303 $mods = $restore->mods
;
304 foreach ($mods as $mod) {
305 if ($mod->userinfo
) {
309 //If we have selected user files or messages or blogs, then users must be restored too
310 if ($restore->user_files ||
$restore->messages ||
$restore->blogs
) {
314 $messages[] = get_string ("noteuserschangednonetocourse");
319 /// If restoring users and backup has mnet remote users and we are restoring to different site, forbid restore to non-admins. MDL-17009
320 if ($restore->users
!= 2 && /// If restoring users
321 !empty($info->mnet_remoteusers
) && $info->mnet_remoteusers
=== 'true' && /// and backup contains remote users
322 !backup_is_same_site($info)) { /// and backup is being restored to different site
324 /// If user is admin (by 'moodle/user:create' cap), warn about conversion to local auth if missing mnet hosts and continue restore
325 if (has_capability('moodle/user:create', get_context_instance(CONTEXT_SYSTEM
))) {
326 $messages[] = get_string('mnetrestore_extusers_admin', 'admin');
327 $show_continue_button = true;
329 /// else, notify about the thing (suggesting to ask admin) and stop restore
331 $messages[] = get_string('mnetrestore_extusers_noadmin', 'admin');
332 $show_continue_button = false;
336 //Save the restore session object
337 $SESSION->restore
= $restore;
339 // Calculate all session objects checksum and store them in session too
340 // so restore_execute.html (used by manual restore and import) will be
341 // able to detect any problem in session info.
342 restore_save_session_object_checksums($restore, $info, $course_header);
344 echo "<div style='text-align:center'>";
346 /// Printout messages
347 foreach ($messages as $message) {
348 echo '<p>' . $message . '</p>';
351 /// Warning and button
352 if ($show_continue_button) {
353 //Print the continue button to execute the restore NOW !!!!
354 //All is prepared !!!
355 $hidden["launch"] = "execute";
356 $hidden["file"] = $file;
358 print_string('longtimewarning','admin');
362 print_single_button("restore.php", $hidden, get_string("restorecoursenow"),"post");
366 //If we are here. Something must be wrong. Debug !!!
368 print_object($restore);
370 print_object($course_header);
371 error ("Something must be wrong");