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
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
34 if (isset($restore) and !empty($fromform)) {
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
50 $backup_unique_code = required_param( 'backup_unique_code' );
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) {
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);
74 $restore_restoreto = required_param('restore_restoreto');
76 $restore_metacourse = required_param('restore_metacourse');
78 $restore_users = required_param('restore_users');
80 $restore_logs = required_param('restore_logs');
82 $restore_user_files = required_param('restore_user_files');
83 //restore_course_files
84 $restore_course_files = required_param('restore_course_files');
86 $restore_site_files = required_param('restore_site_files');
88 $restore_messages = required_param('restore_messages');
90 //Check we've selected a course to restore to
91 $course_id = optional_param('course_id', 0, PARAM_INT
);
93 //We are here, having all we need !!
94 //Create the restore object and put it in the session
95 $restore->backup_unique_code
= $backup_unique_code;
96 $restore->file
= $file;
97 if ($allmods = get_records("modules")) {
98 foreach ($allmods as $mod) {
99 $modname = $mod->name
;
100 $var = "restore_".$modname;
101 $restore->mods
[$modname]->restore
=$
$var;
102 $var = "restore_user_info_".$modname;
103 $restore->mods
[$modname]->userinfo
=$
$var;
104 $instances = !empty($info->mods
[$mod->name
]->instances
) ?
$info->mods
[$mod->name
]->instances
: NULL;
105 if ($instances === NULL) {
108 foreach ($instances as $instance) {
109 $var = 'restore_'.$modname.'_instance_'.$instance->id
;
110 $restore->mods
[$modname]->instances
[$instance->id
]->restore
= $
$var;
111 $var = 'restore_user_info_'.$modname.'_instance_'.$instance->id
;
112 $restore->mods
[$modname]->instances
[$instance->id
]->userinfo
= $
$var;
116 $restore->restoreto
=$restore_restoreto;
117 $restore->metacourse
=$restore_metacourse;
118 $restore->users
=$restore_users;
119 $restore->logs
=$restore_logs;
120 $restore->user_files
=$restore_user_files;
121 $restore->course_files
=$restore_course_files;
122 $restore->site_files
=$restore_site_files;
123 $restore->messages
=$restore_messages;
124 $restore->course_id
=$course_id;
125 //add new vars to restore object
126 $restore->course_startdateoffset
= $coursestartdatedateoffset;
127 $restore->course_shortname
= $currentcourseshortname;
129 // create role mappings, not sure all should be here
130 if ($data2 = data_submitted()) {
131 foreach ($data2 as $tempname=>$tempdata) {
132 if (strstr($tempname, 'roles_')) {
133 $temprole = explode('_', $tempname);
134 $oldroleid = $temprole[1];
135 $newroleid = $tempdata;
136 $restore->rolesmapping
[$oldroleid] = $newroleid;
141 // default role mapping for moodle < 1.7
142 if ($defaultteacheredit = optional_param('defaultteacheredit', 0, PARAM_INT
)) {
143 $restore->rolesmapping
['defaultteacheredit'] = $defaultteacheredit;
145 if ($defaultteacher = optional_param('defaultteacher', 0, PARAM_INT
)) {
146 $restore->rolesmapping
['defaultteacher'] = $defaultteacher;
148 if ($defaultstudent = optional_param('defaultstudent', 0, PARAM_INT
)) {
149 $restore->rolesmapping
['defaultstudent'] = $defaultstudent;
153 //We have the object, so check if we have a new course_id
154 //passed as parammeter
155 $course_id = optional_param('course_id', 0, PARAM_INT
);
157 $restore->course_id
=$course_id;
161 // pass in the course category param
162 $cat_id = optional_param('restore_restorecatto', 0, PARAM_INT
);
164 $restore->restore_restorecatto
= $cat_id;
167 //We have the object with data, put it in the session
168 $SESSION->restore
= $restore;
170 //From here to the end of the script, only use the $restore object
177 if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE
, $id))) {
178 error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
181 if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_SYSTEM
, SITEID
))) {
182 error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
187 if (!$site = get_site()) {
188 error("Site not found!");
191 //Depending the selected restoreto:
192 // If user is a teacher (and not creator):
193 // 0-Current course, deleting: Put $restore->course_id and $restore->deleting (true), create the restore object
194 // 1-Current course, adding: Put $restore->course_id and $restore->deleting (false), create the restore object
195 // If the uses is a creator:
196 // 0-Existing course, deleting: Select the destination course and launch the check again, then
197 // put $restore->course_id and $restore->deleting (true), create the restore object.
198 // 1-Existing course, adding: Select the destination course and launch the check again, then
199 // put $restore->course_id and $restore->deleting (false), create the restore object.
200 // 2-New course: Create the restore object and launch the execute.
202 //If the user is a teacher and not a creator
205 //if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
207 if (!user_can_create_courses()) {
208 $restore->course_id
= $id;
209 if ($restore->restoreto
== 0) {
210 $restore->deleting
= true;
212 $restore->deleting
= false;
216 //If the user is a creator (or admin)
217 //if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
218 if (user_can_create_courses()) {
219 //Set restore->deleting as needed
220 if ($restore->restoreto
== 0) {
221 $restore->deleting
= true;
223 $restore->deleting
= false;
227 //Now, select the course if needed
228 //if (($restore->restoreto == 0 or $restore->restoreto == 1) and ($restore->course_id == 0) and get_capability_courses('moodle/site:restore')) {
229 if (($restore->restoreto
== 0 or $restore->restoreto
== 1) and ($restore->course_id
== 0)) {
231 if ($courses = get_courses("all","c.fullname","c.id,c.fullname,c.shortname,c.visible")) {
232 print_heading(get_string("choosecourse"));
233 print_simple_box_start("center");
234 foreach ($courses as $course) {
235 if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE
, $course->id
))) {
238 if (empty($course->visible
)) {
239 $optdimmed = ' class="dimmed" ';
243 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";
245 print_simple_box_end();
247 print_heading(get_string("nocoursesyet"));
248 print_continue("$CFG->wwwroot/$CFG->admin/index.php");
250 //Checks everything and execute restore
251 } else if ((($restore->restoreto
== 0 or $restore->restoreto
== 1) and ($restore->course_id
!= 0)) or ($restore->restoreto
== 2)) {
252 //Final access control check
253 if ($restore->course_id
== 0 and !user_can_create_courses()) {
254 error("You need to be a creator or admin to restore into new course!");
255 } else if ($restore->course_id
!= 0 and !has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE
, $restore->course_id
))) {
256 error("You need to be an edit teacher or admin to restore into selected course!");
258 $show_continue_button = true;
259 //Check if we've selected any mod's user info and restore->users
260 //is set to none. Change it to course and inform.
261 if ($restore->users
== 2) {
263 $mods = $restore->mods
;
264 foreach ($mods as $mod) {
265 if ($mod->userinfo
) {
269 //If we have selected user files or messages, then users must be restored too
270 if ($restore->user_files ||
$restore->messages
) {
274 echo get_string ("noteuserschangednonetocourse");
275 echo "<hr noshade size=\"1\">";
279 //Save the restore session object
280 $SESSION->restore
= $restore;
281 if ($show_continue_button) {
282 //Print the continue button to execute the restore NOW !!!!
283 //All is prepared !!!
284 echo "<div style='text-align:center'>";
285 $hidden["launch"] = "execute";
286 $hidden["file"] = $file;
288 print_string('longtimewarning','admin');
290 if ($restore->users
&& !empty($info->mnet_externalusers
)
291 && $info->mnet_externalusers
=== 'true') {
292 if ($info->original_wwwroot
=== $CFG->wwwroot
) {
293 print '<p>'.get_string('mnetrestore_extusers','admin').'</p>';
295 print '<p>'. get_string('mnetrestore_extusers_mismatch','admin').'</p>';
298 print_single_button("restore.php", $hidden, get_string("restorecoursenow"),"post");
302 error ("Something was wrong checking restore preferences");
305 //If we are here. Something must be wrong. Debug !!!
307 print_object($restore);
309 print_object($course_header);
310 error ("Something must be wrong");