2004-09-12 01:34:24 +00:00
< ?php //$Id$
2003-05-19 19:29:51 +00:00
//This page receive all the restore_form data. Then, if existing course
//has been selected, shows a list of courses to select one.
//It cheks that the parammeter from restore_form are coherent.
//It puts all the restore info in the session.
//Finally, it calls restore_execute to do the hard work
//Get objects from session
2003-05-19 22:34:12 +00:00
if ($SESSION) {
$info = $SESSION->info;
$course_header = $SESSION->course_header;
2003-10-14 17:06:58 +00:00
if (isset($SESSION->restore)) {
$restore = $SESSION->restore;
}
2003-05-19 22:34:12 +00:00
}
2003-05-19 19:29:51 +00:00
2006-04-10 19:02:39 +00:00
//Detect if we are coming from the restore form
$fromform = optional_param ('fromform', 0, PARAM_INT);
2006-10-31 07:34:03 +00:00
if ($form1 = data_submitted()) {
2006-09-13 09:40:55 +00:00
$currentcourseshortname = $course_header->course_shortname; //"store_ShortName";
2006-09-13 13:19:39 +00:00
$course_header->course_shortname = $form1->shortname; //"update_ShortName";
$course_header->course_fullname = $form1->fullname; //"update_FullName";
2006-09-13 09:40:55 +00:00
$form1->startdate = make_timestamp($form1->startyear, $form1->startmonth, $form1->startday);
2006-09-13 13:19:39 +00:00
$currentcoursestartdate = $course_header->course_startdate;
2006-09-13 09:40:55 +00:00
$coursestartdatedateoffset = $form1->startdate - $currentcoursestartdate;
$restore->course_startdateoffset = $coursestartdatedateoffset; //change to restore
}
2007-02-14 23:54:20 +00:00
///Enforce SESSION->course_header rewrite (PHP 4.x needed because assigns are by value) MDL-8298
$SESSION->course_header = $course_header;
2006-04-10 19:02:39 +00:00
//If restore session info exists, but we are coming from the form
//it has prioriry
if (isset($restore) and !empty($fromform)) {
2003-05-22 22:11:17 +00:00
unset($restore);
}
2003-05-19 22:34:12 +00:00
2005-06-09 09:41:06 +00:00
// check for session objects
2005-06-09 16:34:09 +00:00
if (empty($info) or empty($course_header)) {
2005-06-09 14:04:38 +00:00
error( 'important information missing from SESSION' );
}
2003-05-19 22:34:12 +00:00
2003-05-22 22:11:17 +00:00
//If the restore object doesn't exist, we are going
//to check every variable individually and create it
2003-10-14 17:06:58 +00:00
if (!isset($restore)) {
2003-05-22 22:11:17 +00:00
//Check that we have all we need
//backup_unique_code
2005-06-09 09:41:06 +00:00
$backup_unique_code = required_param( 'backup_unique_code' );
2003-05-22 22:11:17 +00:00
//file
2005-06-09 09:41:06 +00:00
$file = required_param( 'file');
2003-05-22 22:11:17 +00:00
//Checks for the required restoremod parameters
if ($allmods = get_records("modules")) {
foreach ($allmods as $mod) {
$modname = $mod->name;
$var = "restore_".$modname;
2005-06-29 16:58:33 +00:00
$$var = optional_param( $var,0);
2003-05-22 22:11:17 +00:00
$var = "restore_user_info_".$modname;
2005-06-29 16:58:33 +00:00
$$var = optional_param( $var,0);
2006-06-03 20:01:24 +00:00
$instances = !empty($info->mods[$mod->name]->instances) ? $info->mods[$mod->name]->instances : NULL;
2006-03-03 22:26:22 +00:00
if ($instances === NULL) {
continue;
}
2006-01-13 03:45:31 +00:00
foreach ($instances as $instance) {
$var = 'restore_'.$modname.'_instance_'.$instance->id;
$$var = optional_param($var,0);
$var = 'restore_user_info_'.$modname.'_instance_'.$instance->id;
$$var = optional_param($var,0);
}
2003-05-22 22:11:17 +00:00
}
}
//restoreto
2005-06-09 09:41:06 +00:00
$restore_restoreto = required_param('restore_restoreto');
2005-02-07 17:52:58 +00:00
//restore_metacourse
2005-06-09 09:41:06 +00:00
$restore_metacourse = required_param('restore_metacourse');
2003-05-22 22:11:17 +00:00
//restore_users
2005-06-09 09:41:06 +00:00
$restore_users = required_param('restore_users');
2003-05-22 22:11:17 +00:00
//restore_logs
2005-06-09 09:41:06 +00:00
$restore_logs = required_param('restore_logs');
2003-05-22 22:11:17 +00:00
//restore_user_files
2005-06-09 09:41:06 +00:00
$restore_user_files = required_param('restore_user_files');
2003-05-22 22:11:17 +00:00
//restore_course_files
2005-06-09 09:41:06 +00:00
$restore_course_files = required_param('restore_course_files');
2005-02-13 19:09:41 +00:00
//restore_messages
2005-06-09 09:41:06 +00:00
$restore_messages = required_param('restore_messages');
2003-05-22 22:11:17 +00:00
2006-04-10 19:02:39 +00:00
//Check we've selected a course to restore to
$course_id = optional_param('course_id', 0, PARAM_INT);
2003-05-22 22:11:17 +00:00
//We are here, having all we need !!
//Create the restore object and put it in the session
$restore->backup_unique_code = $backup_unique_code;
$restore->file = $file;
if ($allmods = get_records("modules")) {
foreach ($allmods as $mod) {
$modname = $mod->name;
$var = "restore_".$modname;
$restore->mods[$modname]->restore=$$var;
$var = "restore_user_info_".$modname;
$restore->mods[$modname]->userinfo=$$var;
2006-06-03 20:01:24 +00:00
$instances = !empty($info->mods[$mod->name]->instances) ? $info->mods[$mod->name]->instances : NULL;
2006-03-03 22:26:22 +00:00
if ($instances === NULL) {
continue;
}
2006-01-13 03:45:31 +00:00
foreach ($instances as $instance) {
$var = 'restore_'.$modname.'_instance_'.$instance->id;
$restore->mods[$modname]->instances[$instance->id]->restore = $$var;
$var = 'restore_user_info_'.$modname.'_instance_'.$instance->id;
$restore->mods[$modname]->instances[$instance->id]->userinfo = $$var;
}
2003-05-22 22:11:17 +00:00
}
}
$restore->restoreto=$restore_restoreto;
2005-02-07 17:52:58 +00:00
$restore->metacourse=$restore_metacourse;
2003-05-22 22:11:17 +00:00
$restore->users=$restore_users;
$restore->logs=$restore_logs;
$restore->user_files=$restore_user_files;
$restore->course_files=$restore_course_files;
2005-02-13 19:09:41 +00:00
$restore->messages=$restore_messages;
2003-05-22 22:11:17 +00:00
$restore->course_id=$course_id;
2006-09-13 13:19:39 +00:00
//add new vars to restore object
2006-09-13 09:40:55 +00:00
$restore->course_startdateoffset = $coursestartdatedateoffset;
$restore->course_shortname = $currentcourseshortname;
2006-12-15 05:34:35 +00:00
2006-10-02 08:49:07 +00:00
// create role mappings, not sure all should be here
if ($data2 = data_submitted()) {
foreach ($data2 as $tempname=>$tempdata) {
if (strstr($tempname, 'roles_')) {
$temprole = explode('_', $tempname);
$oldroleid = $temprole[1];
$newroleid = $tempdata;
$restore->rolesmapping[$oldroleid] = $newroleid;
}
}
2006-10-03 08:54:46 +00:00
}
// default role mapping for moodle < 1.7
if ($defaultteacheredit = optional_param('defaultteacheredit', 0, PARAM_INT)) {
$restore->rolesmapping['defaultteacheredit'] = $defaultteacheredit;
}
if ($defaultteacher = optional_param('defaultteacher', 0, PARAM_INT)) {
$restore->rolesmapping['defaultteacher'] = $defaultteacher;
}
if ($defaultstudent = optional_param('defaultstudent', 0, PARAM_INT)) {
$restore->rolesmapping['defaultstudent'] = $defaultstudent;
}
2006-10-02 08:49:07 +00:00
2003-05-22 22:11:17 +00:00
} else {
//We have the object, so check if we have a new course_id
//passed as parammeter
2006-04-10 19:02:39 +00:00
$course_id = optional_param('course_id', 0, PARAM_INT);
2003-05-22 22:11:17 +00:00
if ($course_id) {
$restore->course_id=$course_id;
2003-05-19 19:29:51 +00:00
}
2006-10-02 08:49:07 +00:00
}
2003-05-22 22:11:17 +00:00
//We have the object with data, put it in the session
$SESSION->restore = $restore;
2005-02-07 17:52:58 +00:00
//From here to the end of the script, only use the $restore object
2003-05-19 19:29:51 +00:00
//Check login
require_login();
//Check admin
2003-08-03 00:42:41 +00:00
if (!empty($id)) {
2006-09-07 08:57:56 +00:00
if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $id))) {
2003-08-03 00:42:41 +00:00
error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
}
} else {
2006-09-07 08:57:56 +00:00
if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
2003-08-03 00:42:41 +00:00
error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
}
2003-05-19 19:29:51 +00:00
}
//Check site
if (!$site = get_site()) {
error("Site not found!");
}
2003-05-22 22:11:17 +00:00
//Depending the selected restoreto:
2004-09-23 11:34:47 +00:00
// If user is a teacher (and not creator):
2003-08-03 00:42:41 +00:00
// 0-Current course, deleting: Put $restore->course_id and $restore->deleting (true), create the restore object
// 1-Current course, adding: Put $restore->course_id and $restore->deleting (false), create the restore object
2004-09-23 11:34:47 +00:00
// If the uses is a creator:
2003-08-03 00:42:41 +00:00
// 0-Existing course, deleting: Select the destination course and launch the check again, then
// put $restore->course_id and $restore->deleting (true), create the restore object.
// 1-Existing course, adding: Select the destination course and launch the check again, then
// put $restore->course_id and $restore->deleting (false), create the restore object.
// 2-New course: Create the restore object and launch the execute.
2004-09-23 11:34:47 +00:00
//If the user is a teacher and not a creator
2006-09-07 08:57:56 +00:00
if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
2003-08-03 00:42:41 +00:00
$restore->course_id = $id;
if ($restore->restoreto == 0) {
$restore->deleting = true;
} else {
$restore->deleting = false;
}
}
2004-09-23 11:34:47 +00:00
//If the user is a creator (or admin)
2006-09-07 08:57:56 +00:00
if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
2003-08-03 00:42:41 +00:00
//Set restore->deleting as needed
if ($restore->restoreto == 0) {
$restore->deleting = true;
} else {
$restore->deleting = false;
}
}
2003-05-22 22:11:17 +00:00
2003-08-03 00:42:41 +00:00
//Now, select the course if needed
2006-09-07 08:57:56 +00:00
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))) {
2005-10-06 17:22:52 +00:00
if ($courses = get_courses("all","c.fullname","c.id,c.fullname,c.shortname,c.visible")) {
2003-05-22 22:11:17 +00:00
print_heading(get_string("choosecourse"));
2004-09-12 01:34:24 +00:00
print_simple_box_start("center");
2003-05-22 22:11:17 +00:00
foreach ($courses as $course) {
2006-09-13 09:08:14 +00:00
if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $course->id))) {
2005-11-01 17:25:02 +00:00
continue;
}
2005-10-06 17:22:52 +00:00
if (empty($course->visible)) {
$optdimmed = ' class="dimmed" ';
} else {
$optdimmed = '';
}
2007-02-28 06:25:22 +00:00
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";
2003-05-19 19:29:51 +00:00
}
2003-05-22 22:11:17 +00:00
print_simple_box_end();
2003-05-19 19:29:51 +00:00
} else {
2003-05-22 22:11:17 +00:00
print_heading(get_string("nocoursesyet"));
2003-08-01 14:34:11 +00:00
print_continue("$CFG->wwwroot/$CFG->admin/index.php");
2003-05-19 19:29:51 +00:00
}
2003-05-22 22:11:17 +00:00
//Checks everything and execute restore
2003-08-03 00:42:41 +00:00
} else if ((($restore->restoreto == 0 or $restore->restoreto == 1) and ($restore->course_id != 0)) or ($restore->restoreto == 2)) {
2005-11-01 17:25:02 +00:00
//Final access control check
2006-09-07 08:57:56 +00:00
if ($restore->course_id == 0 and !has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
2005-11-01 17:25:02 +00:00
error("You need to be a creator or admin to restore into new course!");
2006-09-13 09:08:14 +00:00
} else if ($restore->course_id != 0 and !has_capability('moodle/site:backup', get_context_instance(CONTEXT_COURSE, $restore->course_id))) {
2005-11-01 17:25:02 +00:00
error("You need to be an edit teacher or admin to restore into selected course!");
}
2003-05-22 22:11:17 +00:00
$show_continue_button = true;
//Check if we've selected any mod's user info and restore->users
//is set to none. Change it to course and inform.
if ($restore->users == 2) {
$changed = false;
$mods = $restore->mods;
foreach ($mods as $mod) {
if ($mod->userinfo) {
$changed = true;
}
}
2005-02-13 19:09:41 +00:00
//If we have selected user files or messages, then users must be restored too
if ($restore->user_files || $restore->messages) {
$changed = 1;
}
2003-05-22 22:11:17 +00:00
if ($changed) {
echo get_string ("noteuserschangednonetocourse");
echo "< hr noshade size = \"1\" > ";
$restore->users = 1;
}
2003-05-19 19:29:51 +00:00
}
2003-05-22 22:11:17 +00:00
//Save the restore session object
$SESSION->restore = $restore;
if ($show_continue_button) {
//Print the continue button to execute the restore NOW !!!!
//All is prepared !!!
2007-03-01 02:46:04 +00:00
echo "< div style = 'text-align:center' > ";
2003-05-22 22:11:17 +00:00
$hidden["launch"] = "execute";
$hidden["file"] = $file;
2003-08-03 00:42:41 +00:00
$hidden["id"] = $id;
2006-12-15 05:34:35 +00:00
print_string('longtimewarning','admin');
2007-01-16 03:28:57 +00:00
if ($restore->users & & !empty($info->mnet_externalusers)
& & $info->mnet_externalusers === 'true') {
if ($info->original_wwwroot === $CFG->wwwroot) {
print '< p > '.get_string('mnetrestore_extusers','admin').'< / p > ';
} else {
print '< p > '. get_string('mnetrestore_extusers_mismatch','admin').'< / p > ';
}
}
2003-07-28 01:38:35 +00:00
print_single_button("restore.php", $hidden, get_string("restorecoursenow"),"post");
2007-03-01 02:46:04 +00:00
echo "< / div > ";
2003-05-19 19:29:51 +00:00
} else {
2003-05-22 22:11:17 +00:00
//Show error
error ("Something was wrong checking restore preferences");
}
//If we are here. Something must be wrong. Debug !!!
} else {
print_object($restore);
print_object($info);
print_object($course_header);
error ("Something must be wrong");
2003-05-19 19:29:51 +00:00
}
2003-05-22 22:11:17 +00:00
2003-05-19 19:29:51 +00:00
?>