MDL-19246 restore - cleanup of "restoreto" options. Prev. site restrictions out. Merged from 19_STABLE

This commit is contained in:
stronk7 2009-05-22 15:02:44 +00:00
parent 1d85bec383
commit 64acc5a55d
4 changed files with 48 additions and 75 deletions

View File

@ -1,6 +1,13 @@
<?php //$Id$
//This file contains all the general function needed (file manipulation...)
//not directly part of the backup/restore utility
//not directly part of the backup/restore utility plus some constants
// Define "restoreto" options
define('RESTORETO_CURRENT_DELETING', 0);
define('RESTORETO_CURRENT_ADDING', 1);
define('RESTORETO_NEW_COURSE', 2);
define('RESTORETO_EXISTING_DELETING', 3);
define('RESTORETO_EXISTING_ADDING', 4);
require_once($CFG->libdir.'/uploadlib.php');
require_once($CFG->libdir . '/completionlib.php');
@ -566,7 +573,7 @@
// add on some extra stuff we need...
$SESSION->restore->metacourse = $restore->metacourse = (isset($preferences['restore_metacourse']) ? $preferences['restore_metacourse'] : 0);
$SESSION->restore->restoreto = $restore->restoreto = 1;
$SESSION->restore->restoreto = $restore->restoreto = RESTORETO_CURRENT_ADDING;
$SESSION->restore->users = $restore->users = $userdata;
$SESSION->restore->groups = $restore->groups = (isset($preferences['restore_groups']) ? $preferences['restore_groups'] : RESTORE_GROUPS_NONE);
$SESSION->restore->logs = $restore->logs = (isset($preferences['restore_logs']) ? $preferences['restore_logs'] : 0);
@ -574,8 +581,6 @@
$SESSION->restore->messages = $restore->messages = (isset($preferences['restore_messages']) ? $preferences['restore_messages'] : 0);
$SESSION->restore->blogs = $restore->blogs = (isset($preferences['restore_blogs']) ? $preferences['restore_blogs'] : 0);
$SESSION->restore->course_id = $restore->course_id = $destinationcourse;
$SESSION->restore->restoreto = 1;
$SESSION->restore->course_id = $destinationcourse;
$SESSION->restore->deleting = $emptyfirst;
$SESSION->restore->restore_course_files = $restore->course_files = (isset($preferences['restore_course_files']) ? $preferences['restore_course_files'] : 0);
$SESSION->restore->restore_site_files = $restore->site_files = (isset($preferences['restore_site_files']) ? $preferences['restore_site_files'] : 0);

View File

@ -222,44 +222,19 @@
// Calculate if the user can create courses
$cancreatecourses = user_can_create_courses();
//Depending the selected restoreto:
// A) If user has 'moodle/site:restore' in more than one course and cannot create courses and he isn't restoring from SITEID:
// 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.
// B) Else, if user has 'moodle/site:restore' in only 1 course or cannot create courses or he is restoring from within SITEID:
// 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
// C) If the user is a creator:
// 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.
// NOTE: SITEID is prevented as a source for restoring because it's public area and we must encourage admins
// to avoid using it as a "repository" for courses storage.
// A) If user has 'moodle/site:restore' in more than one course and cannot create courses and he isn't restoring from SITEID
if (count($mycourses) > 1 && !$cancreatecourses && $id != SITEID) {
/// Nothing to do, process continues and C) will show the list of courses
// B) Else, if user has 'moodle/site:restore' is only 1 course or cannot create courses or he is restoring from within SITEID
} else if (count($mycourses) == 1 || !$cancreatecourses || $id == SITEID) {
if (empty($restore->course_id) && ($restore->restoreto == RESTORETO_CURRENT_DELETING || $restore->restoreto == RESTORETO_CURRENT_ADDING)) {
$restore->course_id = $id; /// Force restore to current course, disabling pick course from list
}
// C) Arrived here, we have preprocessed all the changes needed by A) and B). Continue with the process
//Set restore->deleting as needed
if ($restore->restoreto == 0) {
if ($restore->restoreto == RESTORETO_CURRENT_DELETING || $restore->restoreto == RESTORETO_EXISTING_DELETING) {
$restore->deleting = true;
} else {
$restore->deleting = false;
}
//Now, select the course if needed
if (($restore->restoreto == 0 or $restore->restoreto == 1) and ($restore->course_id == 0) && count($mycourses) > 1) {
if (empty($restore->course_id) && ($restore->restoreto == RESTORETO_EXISTING_DELETING || $restore->restoreto == RESTORETO_EXISTING_ADDING)) {
if ($courses = $mycourses) {
print_heading(get_string("choosecourse"));
@ -281,11 +256,11 @@
print_continue("$CFG->wwwroot/$CFG->admin/index.php");
}
//Checks everything and execute restore
} else if ((($restore->restoreto == 0 or $restore->restoreto == 1) and ($restore->course_id != 0)) or ($restore->restoreto == 2)) {
} else if (($restore->restoreto != RESTORETO_NEW_COURSE and !empty($restore->course_id)) or ($restore->restoreto == RESTORETO_NEW_COURSE)) {
//Final access control check
if ($restore->course_id == 0 and !$cancreatecourses) {
if (empty($restore->course_id) and !$cancreatecourses) {
print_error("cannotrestoreadminorcreator");
} else if ($restore->course_id != 0 and !has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $restore->course_id))) {
} else if (!empty($restore->course_id) and !has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $restore->course_id))) {
print_error("cannotrestoreadminoredit");
}
$show_continue_button = true;
@ -293,9 +268,9 @@
//Check and warn if we are restoring over frontpage (SITEID) course. MDL-19163
if ($restore->course_id == SITEID) {
if ($restore->restoreto == 0) {
if ($restore->restoreto == RESTORETO_CURRENT_DELETING) {
$messages[] = get_string ('restoretositedeleting');
} else if ($restore->restoreto == 1) {
} else if ($restore->restoreto == RESTORETO_CURRENT_ADDING) {
$messages[] = get_string ('restoretositeadding');
}
}

View File

@ -116,9 +116,9 @@
$cancreatecourses = user_can_create_courses();
if (!isset($restore_restoreto)) {
if (!$cancreatecourses) {
$restore_restoreto = 1;
$restore_restoreto = RESTORETO_CURRENT_ADDING;
} else {
$restore_restoreto = 2;
$restore_restoreto = RESTORETO_NEW_COURSE;
}
}
@ -198,20 +198,8 @@ function selectItemInCheckboxByName(formId, checkName, checked ) {
echo "</b>";
echo "</td><td colspan=\"3\">";
// permission should have been checked already
/**
* if user has manageactivities in any course and we are not restoring from within SITEID
* existingcoursedeleting
* existingcourseadding
* else we show
* currentcoursedeleting
* currentcourse
* if user has course:create in any category, we show
* newcourse
*/
// Non-cached - get accessinfo
if (isset($USER->access)) {
$accessinfo = $USER->access;
@ -221,21 +209,21 @@ function selectItemInCheckboxByName(formId, checkName, checked ) {
$mycourses = get_user_courses_bycap($USER->id, 'moodle/site:restore', $accessinfo, true);
// if the user can manage 2 or more courses and we are not restoring from within SITEID,
// we show options for existing courses
if (count($mycourses) > 1 && $id != SITEID) {
$restore_restoreto_options[0] = get_string("existingcoursedeleting");
$restore_restoreto_options[1] = get_string("existingcourseadding");
// else if the user can write to current course
} else if (has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $id))){
$restore_restoreto_options[0] = get_string("currentcoursedeleting");
$restore_restoreto_options[1] = get_string("currentcourseadding");
// if the user can restore to current course, grant the "current" options
if (has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $id))){
$restore_restoreto_options[RESTORETO_CURRENT_DELETING] = get_string("currentcoursedeleting");
$restore_restoreto_options[RESTORETO_CURRENT_ADDING] = get_string("currentcourseadding");
}
// if user can create any course at all, give the option
// if user can create any course, grant the "new course" option
if ($cancreatecourses) {
$restore_restoreto_options[2] = get_string("newcourse");
$restore_restoreto_options[RESTORETO_NEW_COURSE] = get_string("newcourse");
}
// if the user can restore to 2 or more courses (or to just 1 but different from current), grant the "existing" options
if (count($mycourses) > 1 || (count($mycourses) == 1 && $mycourses[0]->id != $id)) {
$restore_restoreto_options[RESTORETO_EXISTING_DELETING] = get_string("existingcoursedeleting");
$restore_restoreto_options[RESTORETO_EXISTING_ADDING] = get_string("existingcourseadding");
}
/// Acummulator for hidden options and proper XHTML output

View File

@ -1072,7 +1072,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
$section->sequence = "";
//Now calculate the section's newid
$newid = 0;
if ($restore->restoreto == 2) {
if ($restore->restoreto == RESTORETO_NEW_COURSE) {
//Save it to db (only if restoring to new course)
$newid = $DB->insert_record("course_sections",$section);
} else {
@ -8133,7 +8133,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
//If we've selected to restore into new course
//create it (course)
//Saving conversion id variables into backup_tables
if ($restore->restoreto == 2) {
if ($restore->restoreto == RESTORETO_NEW_COURSE) {
if (!defined('RESTORE_SILENTLY')) {
echo '<li>'.get_string('creatingnewcourse') . '</li>';
}
@ -8193,7 +8193,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
if ($status) {
//Now , this situation is equivalent to the "restore to new course" one (we
//have a course record and nothing more), so define it as "to new course"
$restore->restoreto = 2;
$restore->restoreto = RESTORETO_NEW_COURSE;
} else {
if (!defined('RESTORE_SILENTLY')) {
notify("An error occurred while deleting some of the course contents.");
@ -8347,7 +8347,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
//we have to do this after groups and groupings are restored, because we need the new groupings id
if ($status) {
//Into new course
if ($restore->restoreto == 2) {
if ($restore->restoreto == RESTORETO_NEW_COURSE) {
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("creatingsections");
}
@ -8363,7 +8363,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
echo '</li>';
}
//Into existing course
} else if ($restore->restoreto == 0 or $restore->restoreto == 1) {
} else if ($restore->restoreto != RESTORETO_NEW_COURSE) {
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("checkingsections");
}
@ -8393,7 +8393,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
//Now create metacourse info
if ($status and $restore->metacourse) {
//Only to new courses!
if ($restore->restoreto == 2) {
if ($restore->restoreto == RESTORETO_NEW_COURSE) {
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("creatingmetacoursedata");
}
@ -8605,9 +8605,11 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
}
//Bring back the course blocks -- do it AFTER the modules!!!
if($status) {
if ($status) {
//If we are deleting and bringing into a course or making a new course, same situation
if($restore->restoreto == 0 || $restore->restoreto == 2) {
if ($restore->restoreto == RESTORETO_CURRENT_DELETING ||
$restore->restoreto == RESTORETO_EXISTING_DELETING ||
$restore->restoreto == RESTORETO_NEW_COURSE) {
if (!defined('RESTORE_SILENTLY')) {
echo '<li>'.get_string('creatingblocks');
}
@ -8626,9 +8628,11 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
}
}
if($status) {
if ($status) {
//If we are deleting and bringing into a course or making a new course, same situation
if($restore->restoreto == 0 || $restore->restoreto == 2) {
if ($restore->restoreto == RESTORETO_CURRENT_DELETING ||
$restore->restoreto == RESTORETO_EXISTING_DELETING ||
$restore->restoreto == RESTORETO_NEW_COURSE) {
if (!defined('RESTORE_SILENTLY')) {
echo '<li>'.get_string('courseformatdata');
}
@ -9134,7 +9138,7 @@ WHERE
foreach ($courseoverrides as $oldroleid => $courseoverride) {
// if not importing into exiting course, or creating new role, we are ok
// local course overrides to be respected (i.e. restored course overrides ignored)
if ($restore->restoreto != 1 || empty($restore->rolesmapping[$oldroleid])) {
if (($restore->restoreto != RESTORETO_CURRENT_ADDING && $restore->restoreto != RESTORETO_EXISTING_ADDING) || empty($restore->rolesmapping[$oldroleid])) {
restore_write_roleoverrides($restore, $courseoverride->overrides, $newcoursecontext, $oldroleid);
}
}
@ -9180,7 +9184,7 @@ WHERE
* role assignments/overrides *
*************************************************/
if ($restore->restoreto != 1) { // skip altogether if restoring to exisitng course by adding
if ($restore->restoreto != RESTORETO_CURRENT_ADDING && $restore->restoreto != RESTORETO_EXISTING_ADDING) { // skip altogether if restoring to exisitng course by adding
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("creatingblocksroles").'</li>';
}
@ -9203,6 +9207,7 @@ WHERE
}
}
}
/************************************************
* Restoring assignments from userid level *
* role assignments/overrides *