diff --git a/user/view.php b/user/view.php index 2f9b8e9ba30ae..ab82bf22b3347 100644 --- a/user/view.php +++ b/user/view.php @@ -246,12 +246,23 @@ // Show groups this user is in if (!isset($hiddenfields['groups'])) { - if ($course->groupmode != SEPARATEGROUPS or has_capability('moodle/site:accessallgroups', $coursecontext)) { - if ($usergroups = groups_get_all_groups($course->id, $user->id)) { - $groupstr = ''; - foreach ($usergroups as $group){ + $accessallgroups = has_capability('moodle/site:accessallgroups', $coursecontext); + if ($usergroups = groups_get_all_groups($course->id, $user->id)) { + $groupstr = ''; + foreach ($usergroups as $group){ + if ($course->groupmode == SEPARATEGROUPS and !$accessallgroups and $user->id != $USER->id) { + if (!groups_is_member($group->id, $user->id)) { + continue; + } + } + + if ($course->groupmode != NOGROUPS) { $groupstr .= ' '.format_string($group->name).','; + } else { + $groupstr .= ' '.format_string($group->name); // the user/index.php shows groups only when course in group mode } + } + if ($groupstr !== '') { print_row(get_string("group").":", rtrim($groupstr, ', ')); } }