MDL-48437 Make Visible/All groups selector show own groups first

This commit is contained in:
Jason Platts 2015-01-19 10:23:18 +00:00
parent 7357af2b0c
commit a75a381f04
3 changed files with 129 additions and 13 deletions

View File

@ -159,6 +159,8 @@ $string['nousersinrole'] = 'There are no suitable users in the selected role';
$string['number'] = 'Group/member count';
$string['numgroups'] = 'Number of groups';
$string['nummembers'] = 'Members per group';
$string['mygroups'] = 'My groups';
$string['othergroups'] = 'Other groups';
$string['overview'] = 'Overview';
$string['potentialmembers'] = 'Potential members: {$a}';
$string['potentialmembs'] = 'Potential members';

View File

@ -513,8 +513,11 @@ function groups_print_course_menu($course, $urlroot, $return=false) {
$context = context_course::instance($course->id);
$aag = has_capability('moodle/site:accessallgroups', $context);
$usergroups = array();
if ($groupmode == VISIBLEGROUPS or $aag) {
$allowedgroups = groups_get_all_groups($course->id, 0, $course->defaultgroupingid);
// Get user's own groups and put to the top.
$usergroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
} else {
$allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
}
@ -526,11 +529,7 @@ function groups_print_course_menu($course, $urlroot, $return=false) {
$groupsmenu[0] = get_string('allparticipants');
}
if ($allowedgroups) {
foreach ($allowedgroups as $group) {
$groupsmenu[$group->id] = format_string($group->name);
}
}
$groupsmenu += groups_sort_menu_options($allowedgroups, $usergroups);
if ($groupmode == VISIBLEGROUPS) {
$grouplabel = get_string('groupsvisible');
@ -562,6 +561,55 @@ function groups_print_course_menu($course, $urlroot, $return=false) {
}
}
/**
* Turn an array of groups into an array of menu options.
* @param array $groups of group objects.
* @return array groupid => formatted group name.
*/
function groups_list_to_menu($groups) {
$groupsmenu = array();
foreach ($groups as $group) {
$groupsmenu[$group->id] = format_string($group->name);
}
return $groupsmenu;
}
/**
* Takes user's allowed groups and own groups and formats for use in group selector menu
* If user has allowed groups + own groups will add to an optgroup
* Own groups are removed from allowed groups
* @param array $allowedgroups All groups user is allowed to see
* @param array $usergroups Groups user belongs to
* @return array
*/
function groups_sort_menu_options($allowedgroups, $usergroups) {
$useroptions = array();
if ($usergroups) {
$useroptions = groups_list_to_menu($usergroups);
// Remove user groups from other groups list.
foreach ($usergroups as $group) {
unset($allowedgroups[$group->id]);
}
}
$allowedoptions = array();
if ($allowedgroups) {
$allowedoptions = groups_list_to_menu($allowedgroups);
}
if ($useroptions && $allowedoptions) {
return array(
1 => array(get_string('mygroups', 'group') => $useroptions),
2 => array(get_string('othergroups', 'group') => $allowedoptions)
);
} else if ($useroptions) {
return $useroptions;
} else {
return $allowedoptions;
}
}
/**
* Generates html to print menu selector for course level, listing all groups.
* Note: This api does not do any group mode check use groups_print_course_menu() instead if you want proper checks.
@ -587,9 +635,7 @@ function groups_allgroups_course_menu($course, $urlroot, $update = false, $activ
$allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
}
foreach ($allowedgroups as $group) {
$groupsmenu[$group->id] = format_string($group->name);
}
$groupsmenu += groups_list_to_menu($allowedgroups);
if ($update) {
// Init activegroup array if necessary.
@ -665,8 +711,11 @@ function groups_print_activity_menu($cm, $urlroot, $return=false, $hideallpartic
$context = context_module::instance($cm->id);
$aag = has_capability('moodle/site:accessallgroups', $context);
$usergroups = array();
if ($groupmode == VISIBLEGROUPS or $aag) {
$allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid); // any group in grouping
// Get user's own groups and put to the top.
$usergroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid);
} else {
$allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid); // only assigned groups
}
@ -678,11 +727,7 @@ function groups_print_activity_menu($cm, $urlroot, $return=false, $hideallpartic
$groupsmenu[0] = get_string('allparticipants');
}
if ($allowedgroups) {
foreach ($allowedgroups as $group) {
$groupsmenu[$group->id] = format_string($group->name);
}
}
$groupsmenu += groups_sort_menu_options($allowedgroups, $usergroups);
if ($groupmode == VISIBLEGROUPS) {
$grouplabel = get_string('groupsvisible');

View File

@ -838,4 +838,73 @@ class core_grouplib_testcase extends advanced_testcase {
$this->assertCount(0, $usergroups1[0]);
$this->assertCount(0, $usergroups2[0]);
}
/**
* Create dummy groups array for use in menu tests
* @param int $number
* @return array
*/
protected function make_group_list($number) {
$testgroups = array();
for ($a = 0; $a < $number; $a++) {
$grp = new stdClass();
$grp->id = 100 + $a;
$grp->name = 'test group ' . $grp->id;
$testgroups[$grp->id] = $grp;
}
return $testgroups;
}
public function test_groups_sort_menu_options_empty() {
$this->assertEquals(array(), groups_sort_menu_options(array(), array()));
}
public function test_groups_sort_menu_options_allowed_goups_only() {
$this->assertEquals(array(
100 => 'test group 100',
101 => 'test group 101',
), groups_sort_menu_options($this->make_group_list(2), array()));
}
public function test_groups_sort_menu_options_user_goups_only() {
$this->assertEquals(array(
100 => 'test group 100',
101 => 'test group 101',
), groups_sort_menu_options(array(), $this->make_group_list(2)));
}
public function test_groups_sort_menu_options_user_both() {
$this->assertEquals(array(
1 => array(get_string('mygroups', 'group') => array(
100 => 'test group 100',
101 => 'test group 101',
)),
2 => array(get_string('othergroups', 'group') => array(
102 => 'test group 102',
103 => 'test group 103',
)),
), groups_sort_menu_options($this->make_group_list(4), $this->make_group_list(2)));
}
public function test_groups_sort_menu_options_user_both_many_groups() {
$this->assertEquals(array(
1 => array(get_string('mygroups', 'group') => array(
100 => 'test group 100',
101 => 'test group 101',
)),
2 => array (get_string('othergroups', 'group') => array(
102 => 'test group 102',
103 => 'test group 103',
104 => 'test group 104',
105 => 'test group 105',
106 => 'test group 106',
107 => 'test group 107',
108 => 'test group 108',
109 => 'test group 109',
110 => 'test group 110',
111 => 'test group 111',
112 => 'test group 112',
)),
), groups_sort_menu_options($this->make_group_list(13), $this->make_group_list(2)));
}
}