info)) {
error( 'info object missing from session' );
}
if (!($course_header = $SESSION->course_header)) {
error( 'course_header object missing from session' );
}
$restore_gradebook_history = optional_param('restore_gradebook_history', 0, PARAM_INT);
//Check that we have all we need
//backup_unique_code
$backup_unique_code = required_param( 'backup_unique_code' );
//file
$file = required_param( 'file' );
//Check login
require_login();
//Init restoreuserinfo
$restoreuserinfo = false;
//Check admin
if (!empty($id)) {
if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $id))) {
error("You need to be a teacher or admin user to use this page.", "$CFG->wwwroot/login/index.php");
}
$restoreuserinfo = has_capability('moodle/restore:userinfo', get_context_instance(CONTEXT_COURSE, $id));
} else {
if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_SYSTEM))) {
error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
}
$restoreuserinfo = has_capability('moodle/restore:userinfo', get_context_instance(CONTEXT_SYSTEM));
}
//Check site
if (!$site = get_site()) {
error("Site not found!");
}
//Checks for the required files/functions to restore every mod
$count = 0;
if ($allmods = get_records("modules") ) {
foreach ($allmods as $mod) {
$modname = $mod->name;
$modfile = "$CFG->dirroot/mod/$modname/restorelib.php";
$modrestore = $modname."_restore_mods";
if (file_exists($modfile)) {
include_once($modfile);
if (function_exists($modrestore)) {
$var = "exists_".$modname;
$$var = true;
$count++;
}
}
//Check data
//Check module info
$var = "restore_".$modname;
if (!isset($$var)) {
$$var = 1;
}
//Check include user info
$var = "restore_user_info_".$modname;
if (!isset($$var) && $restoreuserinfo) {
$$var = 1;
} else {
$$var = 0;
}
}
}
//Check other parameters
if (!isset($restore_metacourse)) {
$restore_metacourse = 1;
}
if (!isset($restore_users)) {
$restore_users = 1;
}
if (!isset($restore_groups)) {
if (empty($CFG->enablegroupings)) {
$restore_groups = RESTORE_GROUPS_ONLY;
} else {
$restore_groups = RESTORE_GROUPS_GROUPINGS;
}
}
if (!isset($restore_logs)) {
$restore_logs = 1;
}
if (!isset($restore_user_files)) {
$restore_user_files = 1;
}
if (!isset($restore_course_files)) {
$restore_course_files = 1;
}
if (!isset($restore_site_files)) {
$restore_site_files = 1;
}
if (!isset($restore_messages)) {
$restore_messages = 1;
}
if (!isset($restore_blogs)) {
$restore_blogs = 1;
}
$cancreatecourses = user_can_create_courses();
if (!isset($restore_restoreto)) {
if (!$cancreatecourses) {
$restore_restoreto = RESTORETO_CURRENT_ADDING;
} else {
$restore_restoreto = RESTORETO_NEW_COURSE;
}
}
if (!isset($course_header->category->id)) {
$course_header->category->id = 0;
}
if(!isset($form1->startdate)) {
$form1->startdate = $course_header->course_startdate; //$course_header->course_startdate;
}
if (empty($form1->shortname)) {
$form1->shortname = $course_header->course_shortname; //'_shortname'; //$course_header->course_shortname;
}
if (empty($form1->fullname)) {
$form1->fullname = $course_header->course_fullname; // '_fullname'; //$course_header->course_fullname;
}
if ($count == 0) {
notice("No restorable modules are installed!");
}
?>
shortname);
if ($testroleid && restore_is_samerole($testroleid, $rolefromxml)) {
return get_record('role', 'id', $testroleid);
}
// Finally, search all other roles. In orter to speed things up, we exclude the ones we have
// already tested, and we only search roles with the same number of capabilities set in their
// definition.
$extracondition = '';
if ($testroleid) {
$extracondition = "AND roleid <> $testroleid";
}
$candidateroleids = get_records_sql("SELECT roleid
FROM {$CFG->prefix}role_capabilities
WHERE roleid <> $roleid $extracondition
GROUP BY roleid
HAVING COUNT(capability) = ".count($rolefromxml->capabilities));
if (!empty($candidateroleids)) {
foreach ($candidateroleids as $testroleid => $notused) {
if (restore_is_samerole($testroleid, $rolefromxml)) {
return get_record('role', 'id', $testroleid);
}
}
}
return false;
}
/**
* Compare a role in the database with one loaded from the backup file, and determine whether
* they have identical permissions for each capability.
* @param integer $testroleid the id of the role from the database to test against.
* @param object $rolefromxml the role definition loaded from the backup file.
* @return boolean true if the two roles are identical.
*/
function restore_is_samerole($testroleid, $rolefromxml) {
// Load the role definition from the databse.
$rolefromdb = get_records('role_capabilities', 'roleid', $testroleid, '', 'capability,permission');
if (!$rolefromdb) {
return false;
}
// Quick check, do they have the permissions on the same number of capabilities?
if (count($rolefromdb) != count($rolefromxml->capabilities)) {
return false;
}
// If they do, check each one.
foreach ($rolefromdb as $capability => $permissions) {
if (!isset($rolefromxml->capabilities[$capability]) ||
$permissions->permission != $rolefromxml->capabilities[$capability]->permission) {
return false;
}
}
return true;
}
?>