]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-12922 'moodle/restore:rolldates' capability - Implement logic on restore. Merged...
authorstronk7 <stronk7>
Tue, 6 Oct 2009 01:27:37 +0000 (01:27 +0000)
committerstronk7 <stronk7>
Tue, 6 Oct 2009 01:27:37 +0000 (01:27 +0000)
backup/restore_check.html
backup/restore_form.html
backup/restorelib.php

index f2ac378543261c7eda65b2625b66fddca8b3fe1f..528f3988cb027caf6d9d2aba5c28965b521cc0f2 100644 (file)
@@ -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;
             }
             }
         }
 
+        // 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) {
index 982ee1b29a5f9fc9894b910e959db0fb47e184d7..3e4c90354b879f974baa5b506ff459c0a61d3d6c 100644 (file)
@@ -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"</td></tr>";
+    } 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 "<tr valign=\"top\"> ";
         echo "<td align=\"right\"> ";
         print_string("startdate");
@@ -298,7 +307,7 @@ function selectItemInCheckboxByName(formId, checkName, checked ) {
         }
         echo "</td></tr>";
     } 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 '<input type="hidden" name="startyear" value="0" />';
         echo '<input type="hidden" name="startmonth" value="0" />';
         echo '<input type="hidden" name="startday" value="0" />';
index 275d74a336e4bfb33fcc68a93004e35e5f340bb7..96c084bc27971e52fc2ad700b25716b099416695 100644 (file)
@@ -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') {