]> git.mjollnir.org Git - moodle.git/commitdiff
role assignment and unassignment now propagates to metacourses
authorskodak <skodak>
Sun, 17 Sep 2006 08:42:42 +0000 (08:42 +0000)
committerskodak <skodak>
Sun, 17 Sep 2006 08:42:42 +0000 (08:42 +0000)
lib/accesslib.php
lib/deprecatedlib.php

index 3f424fa502974ab2511e0d2a08a5323be11b634b..39d04aff8a2c315f7976bdfcb39353fd4bd0b089 100755 (executable)
@@ -1416,6 +1416,18 @@ function role_assign($roleid, $userid, $groupid, $contextid, $timestart=0, $time
     /// Make sure they have an entry in user_lastaccess for courses they can access
     //    role_add_lastaccess_entries($userid, $context);
     }
+    
+    /// now handle metacourse role assignments if in course context
+    if ($success and $context->aggregatelevel == CONTEXT_COURSE) {
+        if ($parents = get_records('course_meta', 'child_course', $context->instanceid)) {
+            foreach ($parents as $parent) {
+                if ($metacontext = get_context_instance(CONTEXT_COURSE, $parent->parent_course)) {
+                    // try it even when something failed
+                    $success = $success and role_assign($roleid, $userid, $groupid, $metacontext->id, $timestart, $timeend, $hidden, $enrol);
+                }
+            }
+        }
+    }
 
     return $success;
 }
@@ -1432,6 +1444,8 @@ function role_assign($roleid, $userid, $groupid, $contextid, $timestart=0, $time
 function role_unassign($roleid=0, $userid=0, $groupid=0, $contextid=0) {
 
     global $USER, $CFG;
+    
+    $success = true;
 
     $args = array('roleid', 'userid', 'groupid', 'contextid');
     $select = array();
@@ -1442,37 +1456,51 @@ function role_unassign($roleid=0, $userid=0, $groupid=0, $contextid=0) {
     }
 
     if ($select) {
-        if (delete_records_select('role_assignments', implode(' AND ', $select))) {
-
-        /// If the user is the current user, then reload the capabilities too.
-            if (!empty($USER->id) && $USER->id == $userid) {
-                load_user_capability();
-            }
-    
-            if ($contextid) {
-                if ($context = get_record('context', 'id', $contextid)) {
+        if ($ras = get_records_select('role_assignments', implode(' AND ', $select))) {
+            $mods = get_list_of_plugins('mod');
+            foreach($ras as $ra) {
+                $success = delete_records('role_assignments', 'id', $ra->id) and $success;
+                /// If the user is the current user, then reload the capabilities too.
+                if (!empty($USER->id) && $USER->id == $ra->userid) {
+                    load_user_capability();
+                }
+                $context = get_record('context', 'id', $ra->contextid);
 
                 /// Ask all the modules if anything needs to be done for this user
-                    if ($mods = get_list_of_plugins('mod')) {
-                        foreach ($mods as $mod) {
-                            include_once($CFG->dirroot.'/mod/'.$mod.'/lib.php');
-                            $functionname = $mod.'_role_unassign';
-                            if (function_exists($functionname)) {
-                                $functionname($userid, $context);
+                foreach ($mods as $mod) {
+                    include_once($CFG->dirroot.'/mod/'.$mod.'/lib.php');
+                    $functionname = $mod.'_role_unassign';
+                    if (function_exists($functionname)) {
+                        $functionname($ra->userid, $context); // watch out, $context might be NULL if something goes wrong
+                    }
+                }
+
+                /// now handle metacourse role unassigment and removing from goups if in course context
+                if (!empty($context) and $context->aggregatelevel == CONTEXT_COURSE) {
+                    //remove from groups when user has no role
+                    $roles = get_user_roles($context, $ra->userid, true);
+                    if (empty($roles)) {
+                        if ($groups = get_groups($context->instanceid, $ra->userid)) {
+                            foreach ($groups as $group) {
+                                delete_records('groups_members', 'groupid', $group->id, 'userid', $ra->userid);
+                            }
+                        }
+                    }
+                    //unassign roles in metacourses too
+                    if ($parents = get_records('course_meta', 'child_course', $context->instanceid)) {
+                        foreach ($parents as $parent) {
+                            if ($metacontext = get_context_instance(CONTEXT_COURSE, $parent->parent_course)) {
+                                // ignore errors in metacourses in case they are not properly synchronized
+                                role_unassign($roleid, $userid, $groupid, $metacontext->id);
                             }
                         }
                     }
-        
-                /// Remove entries from user_lastaccess for courses they can no longer access
-                    //role_add_lastaccess_entries($userid, $context);
                 }
             }
-
-            return true;
         }
-        return false;
     }
-    return true;
+
+    return $success;
 }
 
 /**
@@ -2445,4 +2473,4 @@ function get_role_users($roleid, $context, $parent=false) {
     return get_records_sql($SQL);
 }
 
-?>
+?>
\ No newline at end of file
index 741830579fcd6d1e0c696df25c20a852f1f9ac1e..e1a43f802c7825a66553c5dc69f7fa231ed48c72 100644 (file)
@@ -316,15 +316,6 @@ function enrol_student($userid, $courseid, $timestart=0, $timeend=0, $enrol='man
         return false;
     }
 
-/// Enrol the student in any parent meta courses...
-    if ($parents = get_records('course_meta', 'child_course', $courseid)) {
-        foreach ($parents as $parent) {
-            if ($metacontext = get_context_instance(CONTEXT_COURSE, $parent->parent_course)) {
-                role_assign($role->id, $user->id, 0, $metacontext->id, $timestart, $timeend, 0, 'metacourse');
-            }
-        }
-    }
-
     return role_assign($role->id, $user->id, 0, $context->id, $timestart, $timeend, 0, $enrol);
 }
 
@@ -348,18 +339,7 @@ function unenrol_student($userid, $courseid=0) {
                 delete_records('forum_subscriptions', 'forum', $forum->id, 'userid', $userid);
             }
         }
-        if ($groups = get_groups($courseid, $userid)) {
-            foreach ($groups as $group) {
-                delete_records('groups_members', 'groupid', $group->id, 'userid', $userid);
-            }
-        }
-        /// unenroll from all parent metacourses
-        if ($parents = get_records('course_meta','child_course',$courseid)) {
-            foreach ($parents as $parent) {
-                $status = $status and unenrol_student($userid, $parent->parent_course);
-            }
-        }
-        /// remove from all student roles
+        /// remove from all legacy student roles
         if ($courseid == SITEID) {
             $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
         } else if (!$context = get_context_instance(CONTEXT_COURSE, $courseid)) {
@@ -369,13 +349,13 @@ function unenrol_student($userid, $courseid=0) {
             return false;
         }
         foreach($roles as $role) {
-            $status = $status and role_unassign($role->id, $userid, 0, $context);
+            $status = role_unassign($role->id, $userid, 0, $context) and $status;
         }
     } else {
-        // recursivelly unenroll student from all course
+        // recursivelly unenroll student from all courses
         if ($courses = get_records('course')) {
             foreach($courses as $course) {
-                $status = $status and unenrol_student($userid, $course->id);
+                $status = unenrol_student($userid, $course->id) and $status;
             }
         }
     }