course) || !groupmode($cm->course, $cm)) { return false; } elseif (GROUP_ANY_GROUPING == $groupingid) { return groups_get_groups_for_user($userid, $cm->course); } return groups_get_groups_for_user_in_grouping($userid, $groupingid); } /** * Get the ID of the first group for the global $USER in the course module. * Replaces legacylib function 'mygroupid'. * @uses $USER * @param $cm A course module object. * @return int A single group ID for this user. */ function groups_m_get_my_group($cm) { global $USER; $groupids = groups_m_get_groups_for_user($cm, $USER->id); if (!$groupids || count($groupids) == 0) { return 0; } return array_shift($groupids); } /** * Indicates if a specified user has a particular type of permission for a * particular group for this module instance. * @uses $USER * @param int $cmid The id of the module instance. This is necessary because the * same group can be used in different module instances with different * permission setups. * @param int $groupid The id of the group * @param int $permissiontype The permission type - see note on permission types * above * @userid int $userid The id of the user, defaults to the current user * @return boolean True if the user has the specified permission type, false * otherwise or if an error occurred. */ function groups_m_has_permission($cm, $groupid, $permissiontype, $userid = null) { if (!$userid) { global $USER; $userid = $USER->id; } $groupingid = groups_get_grouping_for_coursemodule($cm); if (!$groupingid || !is_object($cm) || !isset($cm->course)) { return false; } $courseid = $cm->course; $isstudent = isstudent($courseid, $userid); $isteacher = isteacher($courseid, $userid); $groupmember = groups_is_member($groupid, $userid); $memberofsomegroup = groups_is_member_of_some_group_in_grouping($userid, $groupingid); $groupingsettings = groups_get_grouping_settings($groupingid); $viewowngroup = $groupingsettings->viewowngroup; $viewallgroupsmembers = $groupingsettings->viewallgroupmembers; $viewallgroupsactivities = $groupingsettings->viewallgroupsactivities; $teachersgroupsmark = $groupingsettings->teachersgroupsmark; $teachersgroupsview = $groupingsettings->teachersgroupsview; $teachersgroupmark = $groupingsettings->teachersgroupmark; $teachersgroupview = $groupingsettings->teachersgroupview; $teachersoverride = $groupingsettings->teachersoverride; $permission = false; switch ($permissiontype) { case 'view': if (($isstudent and $groupmember) or ($isteacher and $groupmember) or ($isstudent and $viewallgroupsactivities) or ($isteacher and !$teachersgroupview) or ($isteacher and !$memberofsomegroup and $teachersoverride)) { $permission = true; } break; case 'studentcontribute': if (($isstudent and $groupmember) or ($isteacher and $groupmember) or ($isteacher and !$memberofsomegroup and $teachersoverride)) { $permission = true; } break; case 'teachermark': if (($isteacher and $groupmember) or ($isteacher and !$teachersgroupmark) or ($isteacher and !$memberofsomegroup and $teachersoverride)) { $permission = true; } break; case 'viewmembers': if (($isstudent and $groupmember and $viewowngroup) or ($isstudent and $viewallgroupsmembers) or ($isteacher and $groupmember) or ($isteacher and !$teachersgroupview) or ($isteacher and !$memberofsomegroup and $teachersoverride) or $isteacheredit) { $permission = true; } break; } return $permission; } /** * Gets an array of members of a group that have a particular permission type * for this instance of the module and that are enrolled on the course that * the module instance belongs to. * * @param int $cmid The id of the module instance. This is necessary because the * same group can be used in different module instances with different * permission setups. * @param int $groupid The id of the group * @param int $permissiontype The permission type - see note on permission types * above * @return array An array containing the ids of the users with the specified * permission. */ function groups_m_get_members_with_permission($cmid, $groupid, $permissiontype) { // Get all the users as $userid $validuserids = array(); foreach($validuserids as $userid) { $haspermission = groups_m_has_permission($cmid, $groupid, $permissiontype, $userid); if ($haspermission) { array_push($validuserids, $userid); } } return $validuserids; } /** * Gets the group object associated with a group id. This group object can be * used to get information such as the name of the group and the file for the * group icon if it exists. (Look at the groups table in the database to see * the fields). * @param int $groupid The id of the group * @return group The group object */ function groups_m_get_group($groupid) { return groups_db_m_get_group($groupid); } /** * Gets the groups for the module instance. In general, you should use * groups_m_get_groups_for_user, however this function is provided for * circumstances where this function isn't sufficient for some reason. * @param int $cmid The id of the module instance. * @return array An array of the ids of the groups for the module instance */ function groups_m_get_groups($cmid) { $groupingid = groups_db_get_groupingid($cmid); $groupids = groups_get_groups_in_grouping($groupingid); return $groupids; } /** * Gets the members of group that are enrolled on the course that the specified * module instance belongs to. * @param int $cmid The id of the module instance * @param int $groupid The id of the group * @return array An array of the userids of the members. */ function groups_m_get_members($cmid, $groupid) { $userids = groups_get_members($groupid, $membertype); if (!$userids) { $memberids = false; } else { // Check if each user is enrolled on the course @@@ TO DO } return $memberids; } /** * Stores a current group in the user's session, if not already present. * * Current group applies to all modules in the current course that share * a grouping (or use no grouping). * * This function allows the user to change group if they want, but it * checks they have permissions to access the new group and calls error() * otherwise. * @param object $cm Course-module object * @param int $groupmode Group mode * @param int $changegroup If specified, user wants to change to this group * @return Group ID */ function groups_m_get_and_set_current($cm, $groupmode, $changegroup=-1) { // Check group mode is turned on if (!$groupmode) { return false; } // Get current group and return it if no change requested $currentgroupid = groups_m_get_current($cm); if ($changegroup<0) { return $currentgroupid; } // Check 'all groups' access $context = get_context_instance(CONTEXT_COURSE, $cm->course); $allgroups = has_capability('moodle/site:accessallgroups', $context); // 0 is a special case for 'all groups'. if ($changegroup==0) { if ($groupmode!=VISIBLEGROUPS && !$allgroups) { error('You do not have access to view all groups'); } } else { // Normal group specified // Check group is in the course... if (!groups_group_belongs_to_course($changegroup, $cm->course)) { error('Requested group is not in this course.'); } // ...AND in the right grouping if required... if ($cm->groupingid && !groups_belongs_to_grouping($changegroup,$cm->groupingid)) { print_object($cm); print_object(groups_get_group($changegroup)); error('Requested group is not in this grouping.'); } // ...AND user has access to all groups, or it's in visible groups mode, or // user is a member. if (!$allgroups && $groupmode != VISIBLEGROUPS && !groups_is_member($changegroup)) { } } // OK, now remember this group in session global $SESSION; $SESSION->currentgroupinggroup[$cm->course][$cm->groupingid] = $changegroup; return $changegroup; } /** * Obtains the current group (see groups_m_get_and_set_current) either as an ID or object. * @param object $cm Course-module object * @param bool $full If true, returns group object rather than ID * @return mixed Group ID (default) or object */ function groups_m_get_current($cm, $full=false) { global $SESSION; if (isset($SESSION->currentgroupinggroup[$cm->course][$cm->groupingid])) { $currentgroup = $SESSION->currentgroupinggroup[$cm->course][$cm->groupingid]; } else { global $USER; if ($cm->groupingid) { $mygroupids = groups_get_groups_for_user_in_grouping($USER->id, $cm->groupingid); } else { $mygroupids = groups_get_groups_for_user($USER->id, $cm->course); } if (!$mygroupids) { return false; } $currentgroup = array_shift($mygroupids); $SESSION->currentgroupinggroup[$cm->course][$cm->groupingid] = $currentgroup; } if ($full) { return groups_get_group($currentgroup, false); } else { return $currentgroup; } } ?>