<?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');
// 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);
$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);
// 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"));
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;
//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');
}
}
$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;
}
}
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;
$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
$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 {
//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>';
}
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.");
//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");
}
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");
}
//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");
}
}
//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');
}
}
}
- 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');
}
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);
}
}
* 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>';
}
}
}
}
+
/************************************************
* Restoring assignments from userid level *
* role assignments/overrides *