From 64acc5a55d45319f6e84f878852750ac2a51a597 Mon Sep 17 00:00:00 2001 From: stronk7 Date: Fri, 22 May 2009 15:02:44 +0000 Subject: [PATCH] MDL-19246 restore - cleanup of "restoreto" options. Prev. site restrictions out. Merged from 19_STABLE --- backup/lib.php | 13 +++++++++---- backup/restore_check.html | 41 ++++++++------------------------------- backup/restore_form.html | 40 +++++++++++++------------------------- backup/restorelib.php | 29 +++++++++++++++------------ 4 files changed, 48 insertions(+), 75 deletions(-) diff --git a/backup/lib.php b/backup/lib.php index e8f8e11b47..c442f73f93 100644 --- a/backup/lib.php +++ b/backup/lib.php @@ -1,6 +1,13 @@ 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); diff --git a/backup/restore_check.html b/backup/restore_check.html index 63b19179a1..f5decdd211 100644 --- a/backup/restore_check.html +++ b/backup/restore_check.html @@ -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'); } } diff --git a/backup/restore_form.html b/backup/restore_form.html index d501cb20dd..6bd34e4161 100644 --- a/backup/restore_form.html +++ b/backup/restore_form.html @@ -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 ""; echo ""; - // 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 diff --git a/backup/restorelib.php b/backup/restorelib.php index f5d1d46e74..de34e0245f 100644 --- a/backup/restorelib.php +++ b/backup/restorelib.php @@ -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 '
  • '.get_string('creatingnewcourse') . '
  • '; } @@ -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 "
  • ".get_string("creatingsections"); } @@ -8363,7 +8363,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3); echo '
  • '; } //Into existing course - } else if ($restore->restoreto == 0 or $restore->restoreto == 1) { + } else if ($restore->restoreto != RESTORETO_NEW_COURSE) { if (!defined('RESTORE_SILENTLY')) { echo "
  • ".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 "
  • ".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 '
  • '.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 '
  • '.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 "
  • ".get_string("creatingblocksroles").'
  • '; } @@ -9203,6 +9207,7 @@ WHERE } } } + /************************************************ * Restoring assignments from userid level * * role assignments/overrides * -- 2.39.5