enablegroupings)) { $groupingid = 0; } if (!empty($userid)) { $userfrom = ", {$CFG->prefix}groups_members gm"; $userwhere = "AND g.id = gm.groupid AND gm.userid = '$userid'"; } else { $userfrom = ""; $userwhere = ""; } if (!empty($groupingid)) { $groupingfrom = ", {$CFG->prefix}groupings_groups gg"; $groupingwhere = "AND g.id = gg.groupid AND gg.groupingid = '$groupingid'"; } else { $groupingfrom = ""; $groupingwhere = ""; } return get_records_sql("SELECT g.* FROM {$CFG->prefix}groups g $userfrom $groupingfrom WHERE g.courseid = '$courseid' $userwhere $groupingwhere ORDER BY name ASC"); } /** * Determines if the user is a member of the given group. * * @uses $USER If $userid is null, use the global object. * @param int $groupid The group to check for membership. * @param int $userid The user to check against the group. * @return boolean True if the user is a member, false otherwise. */ function groups_is_member($groupid, $userid=null) { global $USER; if (!$userid) { $userid = $USER->id; } return record_exists('groups_members', 'groupid', $groupid, 'userid', $userid); } /** * Determines if current or specified is member of any active group in activity * @param object $cm coruse module object * @param int $userid id of user, null menas $USER->id * @return booelan true if user member of at least one group used in activity */ function groups_has_membership($cm, $userid=null) { global $CFG, $USER; // groupings are ignored when not enabled if (empty($CFG->enablegroupings)) { $cm->groupingid = 0; } if (empty($userid)) { $userid = $USER->id; } if ($cm->groupingid) { // find out if member of any group in selected activity grouping $sql = "SELECT 'x' FROM {$CFG->prefix}groups_members gm, {$CFG->prefix}groupings_groups gg WHERE gm.userid = $userid AND gm.groupid = gg.groupid AND gg.groupingid = {$cm->groupingid}"; } else { // no grouping used - check all groups in course $sql = "SELECT 'x' FROM {$CFG->prefix}groups_members gm, {$CFG->prefix}groups g WHERE gm.userid = $userid AND gm.groupid = g.id AND g.courseid = {$cm->course}"; } return record_exists_sql($sql); } /** * Returns the users in the specified group. * @param int $groupid The groupid to get the users for * @param int $sort optional sorting of returned users * @return array | false Returns an array of the users for the specified * group or false if no users or an error returned. */ function groups_get_members($groupid, $sort='lastname ASC') { global $CFG; return get_records_sql("SELECT u.* FROM {$CFG->prefix}user u, {$CFG->prefix}groups_members gm WHERE u.id = gm.userid AND gm.groupid = '$groupid' ORDER BY $sort"); } /** * Returns effective groupmode used in activity, course setting * overrides activity setting if groupmodeforce enabled. * @return integer group mode */ function groups_get_activity_groupmode($cm) { global $COURSE; // get course object (reuse COURSE if possible) if ($cm->course == $COURSE->id) { $course = $COURSE; } else { if (!$course = get_record('course', 'id', $cm->course)) { error('Incorrect course id in cm'); } } return empty($course->groupmodeforce) ? $cm->groupmode : $course->groupmode; } /** * Print group menu selector for activity. * @param object $cm course module object * @param string $urlroot return address * @param boolean $return return as string instead of printing * @return mixed void or string depending on $return param */ function groups_print_activity_menu($cm, $urlroot, $return=false) { global $CFG, $USER; // groupings are ignored when not enabled if (empty($CFG->enablegroupings)) { $cm->groupingid = 0; } if (!$groupmode = groups_get_activity_groupmode($cm)) { if ($return) { return ''; } else { return; } } $context = get_context_instance(CONTEXT_MODULE, $cm->id); if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) { $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid); // any group in grouping (all if groupings not used) } else { $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid); // only assigned groups } $activegroup = groups_get_activity_group($cm, true); $groupsmenu = array(); if (!$allowedgroups or $groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) { $groupsmenu[0] = get_string('allparticipants'); } if ($allowedgroups) { foreach ($allowedgroups as $group) { $groupsmenu[$group->id] = format_string($group->name); } } if ($groupmode == VISIBLEGROUPS) { $grouplabel = get_string('groupsvisible'); } else { $grouplabel = get_string('groupsseparate'); } if (count($groupsmenu) == 1) { $groupname = reset($groupsmenu); $output = $grouplabel.': '.$groupname; } else { $output = popup_form($urlroot.'&group=', $groupsmenu, 'selectgroup', $activegroup, '', '', '', true, 'self', $grouplabel); } $output = '