/** * Print group menu selector for activity. * * @category group * @param stdClass|cm_info $cm course module object * @param string|moodle_url $urlroot return address that users get to if they choose an option; * should include any parameters needed, e.g. "$CFG->wwwroot/mod/forum/view.php?id=34" * @param bool $return return as string instead of printing * @param bool $hideallparticipants If true, this prevents the 'All participants' * option from appearing in cases where it normally would. This is intended for * use only by activities that cannot display all groups together. (Note that * selecting this option does not prevent groups_get_activity_group from * returning 0; it will still do that if the user has chosen 'all participants' * in another activity, or not chosen anything.) * @return mixed void or string depending on $return param */ function groups_print_activity_menu($cm, $urlroot, $return = false, $hideallparticipants = false) { global $USER, $OUTPUT; if ($urlroot instanceof moodle_url) { // no changes necessary } else { if (strpos($urlroot, 'http') !== 0) { // Will also work for https // Display error if urlroot is not absolute (this causes the non-JS version to break) debugging('groups_print_activity_menu requires absolute URL for ' . '$urlroot, not <tt>' . s($urlroot) . '</tt>. Example: ' . 'groups_print_activity_menu($cm, $CFG->wwwroot . \'/mod/mymodule/view.php?id=13\');', DEBUG_DEVELOPER); } $urlroot = new moodle_url($urlroot); } if (!($groupmode = groups_get_activity_groupmode($cm))) { if ($return) { return ''; } else { return; } } $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 } $activegroup = groups_get_activity_group($cm, true, $allowedgroups); $groupsmenu = array(); if ((!$allowedgroups or $groupmode == VISIBLEGROUPS or $aag) and !$hideallparticipants) { $groupsmenu[0] = get_string('allparticipants'); } $groupsmenu += groups_sort_menu_options($allowedgroups, $usergroups); if ($groupmode == VISIBLEGROUPS) { $grouplabel = get_string('groupsvisible'); } else { $grouplabel = get_string('groupsseparate'); } if ($aag and $cm->groupingid) { if ($grouping = groups_get_grouping($cm->groupingid)) { $grouplabel = $grouplabel . ' (' . format_string($grouping->name) . ')'; } } if (count($groupsmenu) == 1) { $groupname = reset($groupsmenu); $output = $grouplabel . ': ' . $groupname; } else { $select = new single_select($urlroot, 'group', $groupsmenu, $activegroup, null, 'selectgroup'); $select->label = $grouplabel; $output = $OUTPUT->render($select); } $output = '<div class="groupselector">' . $output . '</div>'; if ($return) { return $output; } else { echo $output; } }
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))); }