From a75a381f0469098f95ef53c0d8f62b755c5c17d1 Mon Sep 17 00:00:00 2001 From: Jason Platts Date: Mon, 19 Jan 2015 10:23:18 +0000 Subject: [PATCH] MDL-48437 Make Visible/All groups selector show own groups first --- lang/en/group.php | 2 ++ lib/grouplib.php | 71 ++++++++++++++++++++++++++++++------- lib/tests/grouplib_test.php | 69 +++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+), 13 deletions(-) diff --git a/lang/en/group.php b/lang/en/group.php index 14060bb8b7c..5536a7a9bee 100644 --- a/lang/en/group.php +++ b/lang/en/group.php @@ -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'; diff --git a/lib/grouplib.php b/lib/grouplib.php index 98ac4be3479..3f3a233c9fd 100644 --- a/lib/grouplib.php +++ b/lib/grouplib.php @@ -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'); diff --git a/lib/tests/grouplib_test.php b/lib/tests/grouplib_test.php index 5c0d0f3c2bf..b200fb91ffa 100644 --- a/lib/tests/grouplib_test.php +++ b/lib/tests/grouplib_test.php @@ -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))); + } }