From 81b06e9b171cd437bf0c6e49d004eed37c6bdb36 Mon Sep 17 00:00:00 2001 From: stronk7 Date: Tue, 6 Oct 2009 01:27:37 +0000 Subject: [PATCH] MDL-12922 'moodle/restore:rolldates' capability - Implement logic on restore. Merged from 19_STABLE --- backup/restore_check.html | 22 +++++++++++++++++++++- backup/restore_form.html | 11 ++++++++++- backup/restorelib.php | 22 ++++++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/backup/restore_check.html b/backup/restore_check.html index f2ac378543..528f3988cb 100644 --- a/backup/restore_check.html +++ b/backup/restore_check.html @@ -35,7 +35,7 @@ // Only apply rolling of dates if differences are bigger than one day // that should solve current problems with daylight changes between // backup and restore - if ($restore->course_startdateoffset < 24 * 60 * 60) { + if (abs($restore->course_startdateoffset) < 24 * 60 * 60) { $coursestartdatedateoffset = 0; $restore->course_startdateoffset = 0; } @@ -282,6 +282,26 @@ } } + // If we have selected to roll dates on restore and the user is missing + // such capability (moodle/restore:rolldates) in the target course/category, + // disable roll of dates and warn + if ($restore->course_startdateoffset != 0) { + $canrolldates = false; // init to false + + // if we know the target course, check the capability on it + if (!empty($restore->course_id)) { + $canrolldates = has_capability('moodle/restore:rolldates', get_context_instance(CONTEXT_COURSE, $restore->course_id)); + // if we know the target category, check capability on it + } else if (!empty($restore->restore_restorecatto)) { + $canrolldates = has_capability('moodle/restore:rolldates', get_context_instance(CONTEXT_COURSECAT, $restore->restore_restorecatto)); + } + + if (!$canrolldates) { + $messages[] = get_string ("noteusercannotrolldatesoncontext"); + $restore->course_startdateoffset = 0; + } + } + //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) { diff --git a/backup/restore_form.html b/backup/restore_form.html index 982ee1b29a..3e4c90354b 100644 --- a/backup/restore_form.html +++ b/backup/restore_form.html @@ -276,6 +276,15 @@ function selectItemInCheckboxByName(formId, checkName, checked ) { echo $OUTPUT->help_icon(moodle_help_icon::make("coursefullname", get_string("fullname"))) ; if (isset($err["fullname"])) echo $OUTPUT->error_text($err["fullname"]); echo""; + } else { + // nothing to show here if the user cannot create users + } + + // If user can roll dates (in any course is enough for now) show the roll dates option. + // Important: The final restore check step will validate, once we know the final category/course + // where the restore is happening, if the user is really capable of rolling dates there, noticing + // and disabling roll if not allowed. + if (restore_user_can_roll_dates()) { echo " "; echo " "; print_string("startdate"); @@ -298,7 +307,7 @@ function selectItemInCheckboxByName(formId, checkName, checked ) { } echo ""; } else { - /// If user cannot create course, prevent any change in startyear/month/day (so restore wont calculate any roll. MDL-12922 + /// If user lacks the moodle/restore:rolldates completely, prevent any change in startyear/month/day (so restore won't perform any roll) echo ''; echo ''; echo ''; diff --git a/backup/restorelib.php b/backup/restorelib.php index 275d74a336..96c084bc27 100644 --- a/backup/restorelib.php +++ b/backup/restorelib.php @@ -9344,6 +9344,28 @@ WHERE } } + /** + * true or false function to see if user can roll dates on restore (any course is enough) + * @return bool + */ + function restore_user_can_roll_dates() { + global $USER; + // if user has moodle/restore:rolldates capability at system or any course cat return true + + if (has_capability('moodle/restore:rolldates', get_context_instance(CONTEXT_SYSTEM))) { + return true; + } + + // Non-cached - get accessinfo + if (isset($USER->access)) { + $accessinfo = $USER->access; + } else { + $accessinfo = get_user_access_sitewide($USER->id); + } + $courses = get_user_courses_bycap($USER->id, 'moodle/restore:rolldates', $accessinfo, true); + return !empty($courses); + } + //write activity date changes to the html log file, and update date values in the the xml array function restore_log_date_changes($recordtype, &$restore, &$xml, $TAGS, $NAMETAG='NAME') { -- 2.39.5