/// If its a group discussion, make sure the user is in the group.
if ($discussion->groupid) {
if (!isset($groups[$discussion->groupid][$user->id])) {
- $groups[$discussion->groupid][$user->id] = ismember($discussion->groupid, $user->id);
+ $groups[$discussion->groupid][$user->id] = groups_is_member($discussion->groupid, $user->id);
}
}
if (!$discussion->groupid || !empty($groups[$discussion->groupid][$user->id])) {
switch($groupmode) {
case VISIBLEGROUPS:
// Display group-mode results
- $groups = get_groups($courseid);
+ $groups = groups_get_all_groups($courseid);
if(empty($groups)) {
// No groups exist, sorry
return $this->content;
}
- $mygroups = get_groups($courseid, $USER->id);
+ $mygroups = groups_get_all_groups($courseid, $USER->id);
if(empty($mygroups)) {
// Not member of a group, show nothing
return $this->content;
}
if (groupmode($course) == SEPARATEGROUPS
and !has_capability('moodle/site:accessallgroups', $coursecontext)) {
- if (!ismember($filterselect)) {
+ if (!groups_is_member($filterselect)) {
error ('You are not a member of this course group');
}
}
or groupmode($courseid) != SEPARATEGROUPS) {
return true;
} else {
- if ($usergroups = user_group($courseid, $targetuserid)) {
+ if ($usergroups = groups_get_all_groups($courseid, $targetuserid)) {
foreach ($usergroups as $usergroup) {
- if (ismember($usergroup->id)) {
+ if (groups_is_member($usergroup->id)) {
return true;
}
}
$allowed->courses = array($course->id => 1);
if($course->groupmode != NOGROUPS || !$course->groupmodeforce) {
- $allowed->groups = get_groups($SESSION->cal_course_referer);
+ $allowed->groups = (boolean)groups_get_all_groups($SESSION->cal_course_referer);
}
}
}
}
if ($showgroups) {
- if ($cgroups = get_groups($course->id)) {
+ if ($cgroups = groups_get_all_groups($course->id)) {
foreach ($cgroups as $cgroup) {
$groups[$cgroup->id] = $cgroup->name;
}
}
if ($showgroups) {
- if ($cgroups = get_groups($course->id)) {
+ if ($cgroups = groups_get_all_groups($course->id)) {
foreach ($cgroups as $cgroup) {
$groups[$cgroup->id] = $cgroup->name;
}
*/
function check_group_entry ($courseid, $password) {
$ingroup = false;
- if (($groups = get_groups($courseid)) !== false) {
+ if (($groups = groups_get_all_groups($courseid))) {
foreach ($groups as $group) {
if ( !empty($group->enrolmentkey) and ($password == $group->enrolmentkey) ) {
$ingroup = $group->id;
// now verify grading user has access to all groups or is member of the same group when separate groups used in course
if (groupmode($COURSE) == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
- if ($groups = user_group($COURSE->id, $userid)) {
+ if ($groups = groups_get_all_groups($COURSE->id, $userid)) {
$ok = false;
foreach ($groups as $group) {
if (groups_is_member($group->id, $USER->id)) {
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package groups
*/
-require_once($CFG->libdir.'/datalib.php');
-require_once($CFG->dirroot.'/group/lib.php');
-
/*******************************************************************************
* Utility functions
}
-/**
- * Given two users, determines if there exists a group to which they both belong
- * @param int $userid1 The id of the first user
- * @param int $userid2 The id of the second user
- * @return boolean True if the users are in a common group, false otherwise or
- * if an error occurred.
- */
-function groups_db_users_in_common_group($userid1, $userid2) {
- global $CFG;
- $havecommongroup = false;
- $sql = "SELECT gm1.groupid, 1 FROM {$CFG->prefix}groups_members gm1 " .
- "INNER JOIN {$CFG->prefix}groups_members gm2 " .
- "ON gm1.groupid = gm2.groupid" .
- "WHERE gm1.userid = '$userid1' AND gm2.userid = '$userid2'";
- $commongroups = get_record_sql($sql);
- if ($commongroups) {
- $havecommongroup = true;
- }
-
- return $havecommongroup;
-}
-
-
-
/*******************************************************************************
Membership functions
******************************************************************************/
return $exists;
}
-
-/**
- * Determine if a course ID, group name and description match a group in the database.
- * For backup/restorelib.php
- * @return mixed A group-like object with $group->id, or false.
- */
-function groups_db_group_matches($courseid, $grp_name, $grp_description) {
-//$gro_db->id; $gro_db = get_record("groups","courseid",$restore->course_id,"name",$gro->name,"description",$gro->description);
- global $CFG;
- $sql = "SELECT g.id, g.name, g.description
- FROM {$CFG->prefix}groups g
- WHERE g.name = '$grp_name'
- AND g.description = '$grp_description'
- AND g.courseid = '$courseid'";
- $records = get_records_sql($sql);
- $group = false;
- if ($records) {
- $group = array_shift($records);
- }
- return $group;
-}
-
-/**
- * Determine if a course ID, and group name match a group in the database.
- * @return mixed A group-like object with $group->id, or false.
- */
-function groups_db_group_name_exists($courseid, $grp_name) {
- global $CFG;
- $sql = "SELECT g.id, g.name
- FROM {$CFG->prefix}groups g
- WHERE g.name = '$grp_name'
- AND g.courseid = '$courseid'";
- $records = get_records_sql($sql);
- $group = false;
- if ($records) {
- $group = current($records);
- }
- return $group;
-}
-
-/**
- * Determines if a specified user is a member of a specified group
- * @param int $groupid The group about which the request has been made
- * @param int $userid The user about which the request has been made
- * @return boolean True if the user is a member of the group, false otherwise
- */
-function groups_db_is_member($groupid, $userid) {
- if (!$groupid or !$userid) {
- $ismember = false;
- } else {
- $ismember = record_exists($table = 'groups_members', 'groupid',
- $groupid, 'userid', $userid);
- }
-
- return $ismember;
-}
-
-
/**
* Determines if a specified group is a group for a specified course
* @param int $groupid The group about which the request has been made
}
-/**
- * Delete a specified group, first removing members and links with courses and groupings.
- * @param int $groupid The group to delete
- * @return boolean True if deletion was successful, false otherwise
- */
-function groups_db_delete_group($groupid) {
- if (!$groupid) {
- $success = false;
- } else {
- $success = true;
- // Get a list of users for the group and remove them all.
-
- $userids = groups_db_get_members($groupid);
- if ($userids != false) {
- foreach($userids as $userid) {
- $userdeleted = groups_db_remove_member($userid, $groupid);
- if (!$userdeleted) {
- $success = false;
- }
- }
- }
-
- // Remove any links with groupings to which the group belongs.
- //TODO: dbgroupinglib also seems to delete these links - duplication?
- $groupingids = groups_get_groupings_for_group($groupid);
- if ($groupingids != false) {
- foreach($groupingids as $groupingid) {
- $groupremoved = groups_remove_group_from_grouping($groupid,
- $groupingid);
- if(!$groupremoved) {
- $success = false;
- }
- }
- }
-
- // Delete the group itself
- $results = delete_records($table = 'groups', $field1 = 'id',
- $value1 = $groupid);
- // delete_records returns an array of the results from the sql call,
- // not a boolean, so we have to set our return variable
- if ($results == false) {
- $success = false;
- }
- }
-
- return $success;
-}
-
/**
* Internal function to set the time a group was modified.
*/
}
-/******************************************************************************
- * Groups SQL clauses for modules and core.
- */
-
-/**
- * Returns the table in which group members are stored, with a prefix 'gm'.
- * @return SQL string.
- */
-function groups_members_from_sql() {
- global $CFG;
- return " {$CFG->prefix}groups_members gm ";
-}
-
-/**
- * Returns a join testing user.id against member's user ID.
- * Relies on 'user' table being included as 'user u'.
- * Used in Quiz module reports.
- * @param group ID, optional to include a test for this in the SQL.
- * @return SQL string.
- */
-function groups_members_join_sql($groupid=false) {
- $sql = ' JOIN '.groups_members_from_sql().' ON u.id = gm.userid ';
- if ($groupid) {
- $sql = "AND gm.groupid = '$groupid' ";
- }
- return $sql;
- //return ' INNER JOIN '.$CFG->prefix.'role_assignments ra ON u.id=ra.userid'.
- // ' INNER JOIN '.$CFG->prefix.'context c ON ra.contextid=c.id AND c.contextlevel='.CONTEXT_GROUP.' AND c.instanceid='.$groupid;
-}
-
-/**
- * Returns SQL for a WHERE clause testing the group ID.
- * Optionally test the member's ID against another table's user ID column.
- * @param groupid
- * @param userid_sql Optional user ID column selector, example "mdl_user.id", or false.
- * @return SQL string.
- */
-function groups_members_where_sql($groupid, $userid_sql=false) {
- $sql = " gm.groupid = '$groupid' ";
- if ($userid_sql) {
- $sql .= "AND $userid_sql = gm.userid ";
- }
- return $sql;
-}
-
?>
echo '<select name="group" id="groups" size="15" class="select" onchange="membersCombo.refreshMembers(this.options[this.selectedIndex].value);"'."\n";
echo ' onclick="window.status=this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
- if ($groups_records = get_groups($courseid)) {
- $groupids = groups_groups_to_groupids($groups_records, $courseid);
+ if ($groups = groups_get_all_groups($courseid)) {
+ $groupids = array_keys($groups);
} else {
$groupids = false;
}
print_footer($course);
+/**
+ * Returns the first button action with the given prefix, taken from
+ * POST or GET, otherwise returns false.
+ * See /lib/moodlelib.php function optional_param.
+ * @param $prefix 'act_' as in 'action'.
+ * @return string The action without the prefix, or false if no action found.
+ */
+function groups_param_action($prefix = 'act_') {
+ $action = false;
+//($_SERVER['QUERY_STRING'] && preg_match("/$prefix(.+?)=(.+)/", $_SERVER['QUERY_STRING'], $matches)) { //b_(.*?)[&;]{0,1}/
+
+ if ($_POST) {
+ $form_vars = $_POST;
+ }
+ elseif ($_GET) {
+ $form_vars = $_GET;
+ }
+ if ($form_vars) {
+ foreach ($form_vars as $key => $value) {
+ if (preg_match("/$prefix(.+)/", $key, $matches)) {
+ $action = $matches[1];
+ break;
+ }
+ }
+ }
+ if ($action && !preg_match('/^\w+$/', $action)) {
+ $action = false;
+ error('Action had wrong type.');
+ }
+ ///if (debugging()) echo 'Debug: '.$action;
+ return $action;
+}
?>
\ No newline at end of file
List functions
*****************************/
-/**
- * Gets a list of the group IDs for a specified course.
- * @param int $courseid The id of the course.
- * @return array | false Returns an array of the group IDs or false if no records
- * or an error occurred.
- */
-function groups_get_groups($courseid) {
- $groupids = groups_db_get_groups($courseid);
- return $groupids;
-}
-
-
/**
* Returns the ids of the users in the specified group.
* @param int $groupid The groupid to get the users for
return false;
}
-/**
- * Gets the users for a course who are not in a specified group
- * @param int $groupid The id of the group
- * @param string searchtext similar to searchtext in role assign, search
- * @return array An array of the userids of the non-group members, or false if
- * an error occurred.
- * This function was changed to get_users_by_capability style
- * mostly because of the searchtext requirement
- */
-function groups_get_users_not_in_group($courseid, $groupid, $searchtext='') {
-
- global $CFG;
-
- $context = get_context_instance(CONTEXT_COURSE, $courseid);
-
- if ($searchtext !== '') { // Search for a subset of remaining users
- $LIKE = sql_ilike();
- $FULLNAME = sql_fullname();
- $wheresearch = " AND u.id IN (SELECT id FROM {$CFG->prefix}user WHERE $FULLNAME $LIKE '%$searchtext%' OR email $LIKE '%$searchtext%' )";
- } else {
- $wheresearch = '';
- }
-
- $capability = 'moodle/course:view';
- $doanything = false;
-
- // find all possible "student" roles
- if ($possibleroles = get_roles_with_capability($capability, CAP_ALLOW, $context)) {
- if (!$doanything) {
- if (!$sitecontext = get_context_instance(CONTEXT_SYSTEM)) {
- return false; // Something is seriously wrong
- }
- $doanythingroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, $sitecontext);
- }
-
- $validroleids = array();
- foreach ($possibleroles as $possiblerole) {
- if (!$doanything) {
- if (isset($doanythingroles[$possiblerole->id])) { // We don't want these included
- continue;
- }
- }
- if ($caps = role_context_capabilities($possiblerole->id, $context, $capability)) { // resolved list
- if (isset($caps[$capability]) && $caps[$capability] > 0) { // resolved capability > 0
- $validroleids[] = $possiblerole->id;
- }
- }
- }
- if (empty($validroleids)) {
- return false;
- }
- $roleids = '('.implode(',', $validroleids).')';
- } else {
- return false; // No need to continue, since no roles have this capability set
- }
-
-/// Construct the main SQL
- $select = " SELECT u.id, u.firstname, u.lastname";
- $from = " FROM {$CFG->prefix}user u
- INNER JOIN {$CFG->prefix}role_assignments ra ON ra.userid = u.id
- INNER JOIN {$CFG->prefix}role r ON r.id = ra.roleid";
- $where = " WHERE ra.contextid ".get_related_contexts_string($context)."
- AND u.deleted = 0
- AND ra.roleid in $roleids
- AND u.id NOT IN (SELECT userid
- FROM {$CFG->prefix}groups_members
- WHERE groupid = $groupid)
- $wheresearch";
-
- return get_records_sql($select.$from.$where);;
-}
-
-/**
- * Given two users, determines if there exists a group to which they both belong
- * @param int $userid1 The id of the first user
- * @param int $userid2 The id of the second user
- * @return boolean True if the users are in a common group, false otherwise or
- * if an error occurred.
- */
-function groups_users_in_common_group($userid1, $userid2) {
- return groups_db_users_in_common_group($userid1, $userid2);
-}
-
-
/*****************************
Membership functions
*****************************/
return groups_db_group_exists($groupid);
}
-
-/**
- * Determine if a course ID, group name and description match a group in the database.
- * For backup/restorelib.php
- * @return mixed A group-like object with $group->id, or false.
- */
-function groups_group_matches($courseid, $grp_name, $grp_description) {
- return groups_db_group_matches($courseid, $grp_name, $grp_description);
-}
-
-/**
- * Determine if a course ID, and group name match a group in the database.
- * @return mixed A group-like object with $group->id, or false.
- */
-function groups_group_name_exists($courseid, $grp_name) {
- return groups_db_group_name_exists($courseid, $grp_name);
-}
-
-/**
- * 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) {
- if (! $userid) {
- global $USER;
- $userid = $USER->id;
- }
- $ismember = groups_db_is_member($groupid, $userid);
-
- return $ismember;
-}
-
-
/**
* Determines if a specified group is a group for a specified course
* @param int $groupid The group about which the request has been made
*****************************/
-/**
- * Delete a group best effort, first removing members and links with courses and groupings.
- * @param int $groupid The group to delete
- * @return boolean True if deletion was successful, false otherwise
- * See comment above on web service autoupdating.
- */
-function groups_delete_group($groupid) {
- $groupdeleted = groups_db_delete_group($groupid);
-
- return $groupdeleted;
-}
-
-
/**
* Deletes the link between the specified user and group.
* @param int $groupid The group to delete the user from
-
-/**
- * Returns an array of group objects that the user is a member of
- * in the given course. If userid isn't specified, then return a
- * list of all groups in the course.
- *
- * @uses $CFG
- * @param int $courseid The id of the course in question.
- * @param int $userid The id of the user in question as found in the 'user'
- * table 'id' field.
- * @return object
- */
-function get_groups($courseid, $userid=0) {
- if ($userid) {
- $groupids = groups_get_groups_for_user($userid, $courseid);
- } else {
- $groupids = array_keys(groups_get_all_groups($courseid));
- }
-
- return groups_groupids_to_groups($groupids, $courseid, $alldata=true);
-}
-
-
-/**
- * Returns the user's group in a particular course
- *
- * @uses $CFG
- * @param int $courseid The course in question.
- * @param int $userid The id of the user as found in the 'user' table.
- * @param int $groupid The id of the group the user is in.
- * @return object
- */
-function user_group($courseid, $userid) {
- $groupids = groups_get_groups_for_user($userid, $courseid);
- return groups_groupids_to_groups($groupids);
-}
-
-
-/**
- * Determines if the user is a member of the given group.
- * TODO: replace all calls with 'groups_is_member'.
- *
- * @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 ismember($groupid, $userid = null) {
- return groups_is_member($groupid, $userid);
-}
-
/**
* Returns an array of user objects
*
return $cm->groupmode;
}
return $course->groupmode;
-
- /*if ($cm and !$course->groupingid) {
- //TODO: was $coursemodule
- return groups_has_groups_setup_for_instance($cm);
- } else {
- return groups_has_groups_setup($course->id);
- }*/
}
} elseif ($groupmode == VISIBLEGROUPS) {
// All groups are visible
- //if (ismember($group->id)){
+ //if (groups_is_member($group->id)){
$currentgroupid = set_current_group($course->id, $groupid); //set this since he might post
/*)}else {
$currentgroupid = $group->id;*/
} elseif ($groupmode == SEPARATEGROUPS) { // student in separate groups switching
- if (ismember($groupid)) { //check if is a member
+ if (groups_is_member($groupid)) { //check if is a member
$currentgroupid = set_current_group($course->id, $groupid); //might need to set_current_group?
}
else {
//this can mean that either, this guy has no group
//or, this guy just came from a visible all forum, and he left when he set his current group to 0 (show all)
- if ($usergroups = user_group($course->id, $USER->id)){
+ if ($usergroups = groups_get_all_groups($course->id, $USER->id)){
//for the second situation, we need to perform the trick and get him a group.
$first = reset($usergroups);
$currentgroup = get_and_set_current_group($course, $groupmode, $first->id);
if ($groupmode == VISIBLEGROUPS or ($groupmode and has_capability('moodle/site:accessallgroups', $context))) {
- if ($groups = get_groups($course->id)) {
+ if ($groups = groups_get_all_groups($course->id)) {
echo '<div class="groupselector">';
print_group_menu($groups, $groupmode, $currentgroup, $urlroot, 1);
} else if ($groupmode == SEPARATEGROUPS and has_capability('moodle/course:view', $context)) {
//get all the groups this guy is in in this course
- if ($usergroups = user_group($course->id, $USER->id)){
+ if ($usergroups = groups_get_all_groups($course->id, $USER->id)){
echo '<div class="groupselector">';
//print them in the menu
print_group_menu($usergroups, $groupmode, $currentgroup, $urlroot, 0);
// this may be slow, but this is the proper way of doing it
if (!has_capability('moodle/course:view', $context, $ra->userid)) {
// remove from groups
- if ($groups = get_groups($context->instanceid, $ra->userid)) {
+ if ($groups = groups_get_all_groups($context->instanceid)) {
foreach ($groups as $group) {
delete_records('groups_members', 'groupid', $group->id, 'userid', $ra->userid);
}
}
}
// Find all module instances for the course.
- if ($modules = get_records('course_modules', 'course', $context->instanceid)) {
+ if ($modules = get_records('course_modules', 'course', $context->instanceid, '', 'id')) {
foreach ($modules as $module) {
if ($child = get_context_instance(CONTEXT_MODULE, $module->id)) {
array_push($children, $child->id);
}
}
// Find all group instances for the course.
- if ($groupids = groups_get_groups($context->instanceid)) {
- foreach ($groupids as $groupid) {
- if ($child = get_context_instance(CONTEXT_GROUP, $groupid)) {
+ if ($groups = get_records('groups', 'courseid', $context->instanceid, '', 'id')) {
+ foreach ($groups as $group) {
+ if ($child = get_context_instance(CONTEXT_GROUP, $group->id)) {
array_push($children, $child->id);
}
}
foreach ($categories as $catid) {
// Find all courses for the category.
- if ($courses = get_records('course', 'category', $catid)) {
+ if ($courses = get_records('course', 'category', $catid, '', 'id')) {
foreach ($courses as $course) {
if ($courseci = get_context_instance(CONTEXT_COURSE, $course->id)) {
array_push($children, $courseci->id);
AND (lastname = '' OR firstname = '' OR email = '')");
}
-
-/** TODO: functions now in /group/lib/legacylib.php (3)
-get_groups
-get_group_users
-user_group
-
- * Returns an array of group objects that the user is a member of
- * in the given course. If userid isn't specified, then return a
- * list of all groups in the course.
- *
- * @uses $CFG
- * @param int $courseid The id of the course in question.
- * @param int $userid The id of the user in question as found in the 'user' table 'id' field.
- * @return object
- *
-function get_groups($courseid, $userid=0) {
- global $CFG;
-
- if ($userid) {
- $dbselect = ', '. $CFG->prefix .'groups_members m';
- $userselect = 'AND m.groupid = g.id AND m.userid = \''. $userid .'\'';
- } else {
- $dbselect = '';
- $userselect = '';
- }
-
- return get_records_sql("SELECT g.*
- FROM {$CFG->prefix}groups g $dbselect
- WHERE g.courseid = '$courseid' $userselect ");
-}
-
-
-/**
- * Returns an array of user objects that belong to a given group
- *
- * @uses $CFG
- * @param int $groupid The group in question.
- * @param string $sort ?
- * @param string $exceptions ?
- * @return object
- *
-function get_group_users($groupid, $sort='u.lastaccess DESC', $exceptions='', $fields='u.*') {
- global $CFG;
- if (!empty($exceptions)) {
- $except = ' AND u.id NOT IN ('. $exceptions .') ';
- } else {
- $except = '';
- }
- // in postgres, you can't have things in sort that aren't in the select, so...
- $extrafield = str_replace('ASC','',$sort);
- $extrafield = str_replace('DESC','',$extrafield);
- $extrafield = trim($extrafield);
- if (!empty($extrafield)) {
- $extrafield = ','.$extrafield;
- }
- return get_records_sql("SELECT $fields $extrafield
- FROM {$CFG->prefix}user u,
- {$CFG->prefix}groups_members m
- WHERE m.groupid = '$groupid'
- AND m.userid = u.id $except
- ORDER BY $sort");
-}
-
-/**
- * Returns the user's group in a particular course
- *
- * @uses $CFG
- * @param int $courseid The course in question.
- * @param int $userid The id of the user as found in the 'user' table.
- * @param int $groupid The id of the group the user is in.
- * @return object
- *
-function user_group($courseid, $userid) {
- global $CFG;
-
- return get_records_sql("SELECT g.*
- FROM {$CFG->prefix}groups g,
- {$CFG->prefix}groups_members m
- WHERE g.courseid = '$courseid'
- AND g.id = m.groupid
- AND m.userid = '$userid'
- ORDER BY name ASC");
-}
-*/
-
-
-
/// OTHER SITE AND COURSE FUNCTIONS /////////////////////////////////////////////
if ($teacher->editall) { // These can access anything
continue;
}
- if (($teacher->authority > 0) and ismember($groupid, $teacher->id)) { // Specific group teachers
+ if (($teacher->authority > 0) and groups_is_member($groupid, $teacher->id)) { // Specific group teachers
continue;
}
unset($teachers[$key]);
}
+/**
+ * Returns an array of group objects that the user is a member of
+ * in the given course. If userid isn't specified, then return a
+ * list of all groups in the course.
+ *
+ * @uses $CFG
+ * @param int $courseid The id of the course in question.
+ * @param int $userid The id of the user in question as found in the 'user' table 'id' field.
+ * @return object
+ */
+function get_groups($courseid, $userid=0) {
+ return groups_get_all_groups($courseid, $userid);
+}
+
+/**
+ * Returns the user's groups in a particular course
+ * note: this function originally returned only one group
+ *
+ * @uses $CFG
+ * @param int $courseid The course in question.
+ * @param int $userid The id of the user as found in the 'user' table.
+ * @param int $groupid The id of the group the user is in.
+ * @return aray of groups
+ */
+function user_group($courseid, $userid) {
+ return groups_get_all_groups($courseid, $userid);
+}
+
+
+/**
+ * Determines if the user is a member of the given group.
+ *
+ * @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 ismember($groupid, $userid = null) {
+ return groups_is_member($groupid, $userid);
+}
+
?>
--- /dev/null
+<?php //$Id$
+
+// folowing files will be removed soon
+require_once($CFG->dirroot.'/group/lib/basicgrouplib.php');
+require_once($CFG->dirroot.'/group/lib/groupinglib.php');
+require_once($CFG->dirroot.'/group/lib/utillib.php');
+require_once($CFG->dirroot.'/group/lib/legacylib.php');
+
+/**
+ * Returns the groupid of a group with the name specified for the course.
+ * Group names should be unique in course
+ * @param int $courseid The id of the course
+ * @param string $name name of group (without magic quotes)
+ * @return int $groupid
+ */
+function groups_get_group_by_name($courseid, $name) {
+ if (!$group = get_record('groups', 'courseid', $courseid, 'name', addslashes($name))) {
+ return false;
+ }
+
+ return $group->id;
+}
+
+/**
+ * Get the group object
+ * @param groupid ID of the group.
+ * @return group object
+ */
+function groups_get_group($groupid) {
+ return get_record('groups', 'id', $groupid);
+}
+
+/**
+ * Gets array of all groups in a specified course.
+ * @param int $courseid The id of the course.
+ * @param int $userid optional user id, returns only groups of the user.
+ * @return array | false Returns an array of the group IDs or false if no records
+ * or an error occurred.
+ */
+function groups_get_all_groups($courseid, $userid=0) {
+ global $CFG;
+
+ if (empty($userdi)) {
+ return get_records('groups', 'courseid', $courseid, 'name ASC');
+
+ } else {
+ return get_records_sql("SELECT g.*
+ FROM {$CFG->prefix}groups g,
+ {$CFG->prefix}groups_members m
+ WHERE g.courseid = '$courseid'
+ AND g.id = m.groupid
+ AND m.userid = '$userid'
+ 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);
+}
+
+
+?>
$graders = array();
if (groupmode($this->course, $this->cm) == SEPARATEGROUPS) { // Separate groups are being used
- if ($groups = user_group($this->course->id, $user->id)) { // Try to find all groups
+ if ($groups = groups_get_all_groups($this->course->id, $user->id)) { // Try to find all groups
foreach ($groups as $group) {
foreach ($potgraders as $t) {
if ($t->id == $user->id) {
if ($t->id == $user->id) {
continue; // do not send self
}
- if (!user_group($this->course->id, $t->id)) { //ugly hack
+ if (!groups_get_all_groups($this->course->id, $t->id)) { //ugly hack
$graders[$t->id] = $t;
}
}
return;
foreach ($assignments as $assignment) {
- if (empty($groupid) || ismember($groupid, $assignment->userid)) {
+ if (empty($groupid) || groups_is_member($groupid, $assignment->userid)) {
$tmpactivity = new Object;
$responsecount = 0;
foreach ($allanswers as $aa) {
if ($groupmode and $currentgroup) {
- if (ismember($currentgroup, $aa->userid)) {
+ if (groups_is_member($currentgroup, $aa->userid)) {
$responsecount++;
}
} else {
foreach ($allresponses as $aa) {
//TODO: rewrite with SQL
if ($groupmode and $currentgroup) {
- if (ismember($currentgroup, $aa->userid)) {
+ if (groups_is_member($currentgroup, $aa->userid)) {
$answers[$aa->userid] = $aa;
}
} else {
$studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
$myxls->write_string($row,2,$studentid);
$ug2 = '';
- if ($usergrps = user_group($course->id, $user->id)) {
+ if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
foreach ($usergrps as $ug) {
$ug2 = $ug2. $ug->name;
}
$studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
$myxls->write_string($row,2,$studentid);
$ug2 = '';
- if ($usergrps = user_group($course->id, $user->id)) {
+ if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
foreach ($usergrps as $ug) {
$ug2 = $ug2. $ug->name;
}
}
echo "\t". $studentid."\t";
$ug2 = '';
- if ($usergrps = user_group($course->id, $user->id)) {
+ if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
foreach ($usergrps as $ug) {
$ug2 = $ug2. $ug->name;
}
echo '</div></td></tr>';
echo '<tr><td class="left side">';
- if ($groups = user_group($data->course, $comment->userid)) {
+ if ($groups = groups_get_all_groups($data->course, $comment->userid)) {
print_group_picture($groups, $data->course, false, false, true);
} else {
echo ' ';
}
if ($currentgroup) {
- return ismember($currentgroup);
+ return groups_is_member($currentgroup);
} else {
//else it might be group 0 in visible mode
if ($groupmode == VISIBLEGROUPS){
// check group membership, if necessary
if ($groupid) {
// check user's group
- if (!ismember($groupid, $user->id)) {
+ if (!groups_is_member($groupid, $user->id)) {
continue; // skip this user
}
}
// check group membership, if necessary
if ($groupid) {
// check user's group
- if (!ismember($groupid, $user->id)) {
+ if (!groups_is_member($groupid, $user->id)) {
continue; // skip this user
}
}
// check group membership, if necessary
if ($groupid) {
// check user's group
- if (!ismember($groupid, $user->id)) {
+ if (!groups_is_member($groupid, $user->id)) {
continue; // skip this user
}
}
if ($groupmode && !has_capability('moodle/site:accessallgroups', $modcontext)) {
// Groups must be kept separate
- //change this to ismember
+ //change this to groups_is_member
$mygroupid = mygroupid($course->id); //only useful if 0, otherwise it's an array now
if ($groupmode == SEPARATEGROUPS) {
require_login();
- if ((empty($mygroupid) and $discussion->groupid == -1) || (ismember($discussion->groupid) || $mygroupid == $discussion->groupid)) {
+ if ((empty($mygroupid) and $discussion->groupid == -1) || (groups_is_member($discussion->groupid) || $mygroupid == $discussion->groupid)) {
// $canreply = true;
} elseif ($discussion->groupid == -1) {
$canreply = false;
} else if ($groupmode == VISIBLEGROUPS) {
$canreply = ( (empty($mygroupid) && $discussion->groupid == -1) ||
- (ismember($discussion->groupid) || $mygroupid == $discussion->groupid) );
+ (groups_is_member($discussion->groupid) || $mygroupid == $discussion->groupid) );
}
}
} else { // allow guests to see the link
continue; // Be safe and don't send it to anyone
}
- if (!ismember($discussion->groupid) and !has_capability('moodle/site:accessallgroups', $modcontext)) {
+ if (!groups_is_member($discussion->groupid) and !has_capability('moodle/site:accessallgroups', $modcontext)) {
// do not send posts from other groups when in SEPARATEGROUPS or VISIBLEGROUPS
continue;
}
if ($forums = get_records_sql($selectforums)) {
- $group = user_group($course->id, $userid);
+ $group = groups_get_all_groups($course->id, $userid);
foreach ($forums as $forum) {
$forumcontext = get_context_instance(CONTEXT_MODULE, $forum->cmid);
$output .= '</td></tr>';
$output .= '<tr><td class="left side" valign="top">';
- if ($group = user_group($course->id, $user->id)) {
+ if ($group = groups_get_all_groups($course->id, $user->id)) {
$output .= print_group_picture($group, $course->id, false, true, true);
} else {
$output .= ' ';
echo '</div></td></tr>';
echo '<tr><td class="left side">';
- if ($group = user_group($courseid, $post->userid)) {
+ if ($group = groups_get_all_groups($courseid, $post->userid)) {
print_group_picture($group, $courseid, false, false, true);
} else {
echo ' ';
}
if ($currentgroup) {
- return ismember($currentgroup);
+ return groups_is_member($currentgroup);
} else {
//else it might be group 0 in visible mode
if ($groupmode == VISIBLEGROUPS){
if ($discussion->groupid > 0) {
$groupmode = groupmode($course, $cm);
if ($groupmode == SEPARATEGROUPS) {
- return ismember($discussion->groupid) || has_capability('moodle/site:accessallgroups', $modcontext);
+ return groups_is_member($discussion->groupid) || has_capability('moodle/site:accessallgroups', $modcontext);
}
}
return true;
echo '</div>';
echo '</form>';
echo "</div>\n";
- } else if (!isguestuser() and isloggedin() and $forum->type != 'news' and $groupmode == SEPARATEGROUPS and !ismember($currentgroup)) {
+ } else if (!isguestuser() and isloggedin() and $forum->type != 'news' and $groupmode == SEPARATEGROUPS and !groups_is_member($currentgroup)) {
notify(get_string('cannotadddiscussion', 'forum'));
}
if (groupmode($course, $cm)) { // Make sure user can post here
$mygroupid = mygroupid($course->id);
if (!((empty($mygroupid) and $discussion->groupid == -1)
- || (ismember($discussion->groupid)/*$mygroupid == $discussion->groupid*/)
+ || (groups_is_member($discussion->groupid)/*$mygroupid == $discussion->groupid*/)
|| has_capability('moodle/site:accessallgroups', $modcontext, NULL, false) )) {
print_error('nopostdiscussion', 'forum');
}
if (!empty($records)) {
foreach ($records as $record) {
- if (empty($groupid) || ismember($groupid, $record->userid)) {
+ if (empty($groupid) || groups_is_member($groupid, $record->userid)) {
unset($activity);
$grades = make_grades_menu($journal->assessed);
$teachers = get_course_teachers($course->id);
- $allowedtograde = ($groupmode != VISIBLEGROUPS or isteacheredit($course->id) or ismember($currentgroup));
+ $allowedtograde = ($groupmode != VISIBLEGROUPS or isteacheredit($course->id) or groups_is_member($currentgroup));
if ($allowedtograde) {
echo '<form action="report.php" method="post">';
if ($usersdone = journal_get_users_done($journal)) {
foreach ($usersdone as $user) {
if ($currentgroup) {
- if (!ismember($currentgroup, $user->id)) { /// Yes, it's inefficient, but this module will die
+ if (!groups_is_member($currentgroup, $user->id)) { /// Yes, it's inefficient, but this module will die
continue;
}
}
if (!has_capability('mod/survey:readresponses', $context)) {
if ($type != "student.png" or $sid != $USER->id ) {
error("Sorry, you aren't allowed to see this.");
- } else if ($groupmode and !ismember($group)) {
+ } else if ($groupmode and !groups_is_member($group)) {
error("Sorry, you aren't allowed to see this.");
}
}
$groupid=$mygroupids[0];
} else {
// Whatever groups are in the course, pick one
- $coursegroups = get_groups($course->id);
+ $coursegroups = groups_get_all_groups($course->id);
if(!$coursegroups || count($coursegroups)==0) {
error("Can't access wiki in group mode when no groups are configured for the course");
}
$groupid=$mygroupids[0];
} else {
// Whatever groups are in the course, pick one
- $coursegroups = get_groups($course->id);
+ $coursegroups = groups_get_all_groups($course->id);
if(!$coursegroups || count($coursegroups)==0) {
error("Can't access wiki in group mode when no groups are configured for the course");
}
$wiki_entries = get_records_sql($sql);
$wiki_entries=is_array($wiki_entries)?$wiki_entries:array();
foreach ($wiki_entries as $wiki_entry) {
- if (($viewall === true) or ismember($viewall, $wiki_entry->userid)) {
+ if (($viewall === true) or groups_is_member($viewall, $wiki_entry->userid)) {
$key = 'view.php?id='.$id.'&userid='.$wiki_entry->userid.'&page='.$wiki_entry->pagename;
$wikis[$key] = fullname($wiki_entry).':'.$wiki_entry->pagename;
if ($currentid == $wiki_entry->id) {
$wiki_entries = wiki_get_entries($wiki, 'group');
if ($groupmode and ($isteacheredit or ($isteacher and !$mygroupid))) {
- if ($groups = get_groups($course->id)) {
+ if ($groups = groups_get_all_groups($course->id)) {
$defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
foreach ($groups as $group) {
}
//if a studnet with multiple groups in SPG
else if ($groupmode == SEPARATEGROUPS){
- if ($groups = get_groups($course->id, $user->id)){
+ if ($groups = groups_get_all_groups($course->id, $user->id)){
$defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
foreach ($groups as $group) {
/// If the user is an editing teacher, or a non-editing teacher not assigned to a group, show all
/// teacher wikis, regardless of creation.
if ($groupmode and ($isteacheredit or ($isteacher and !$mygroupid))) {
- if ($groups = get_groups($course->id)) {
+ if ($groups = groups_get_all_groups($course->id)) {
$defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
foreach ($groups as $group) {
/// create any student wiki in their group.
else {
return ((($userid == $user->id) and wiki_is_student($wiki, $user->id)) or wiki_is_teacheredit($wiki) or
- (wiki_is_teacher($wiki) and (!$groupmode or $mygroupid == 0 or (ismember($mygroupid, $userid)))));
+ (wiki_is_teacher($wiki) and (!$groupmode or $mygroupid == 0 or (groups_is_member($mygroupid, $userid)))));
}
break;
case 'student':
$can_edit = (($user->id == $wiki_entry->userid) or
($groupmode and wiki_is_teacher($wiki, $user->id) and
- ismember($mygroupid, $wiki_entry->userid)));
+ groups_is_member($mygroupid, $wiki_entry->userid)));
break;
case 'group':
/// If there is a groupmode, determine the user's group status.
if ($groupmode) {
/// If the user is a member of the wiki group, they can edit the wiki.
- $can_edit = ismember($wiki_entry->groupid, $user->id);
+ $can_edit = groups_is_member($wiki_entry->groupid, $user->id);
}
/// If mode is 'nogroups', then all participants can edit the wiki.
else {
/// If there is a groupmode, determine the user's group status.
if ($groupmode) {
/// If the user is a member of the wiki group, they can edit the wiki.
- $can_edit = (wiki_is_teacher($wiki, $user->id) and ismember($wiki_entry->groupid, $user->id));
+ $can_edit = (wiki_is_teacher($wiki, $user->id) and groups_is_member($wiki_entry->groupid, $user->id));
}
else {
$can_edit = wiki_is_teacher($wiki, $user->id);
/// - they are a non-editing teacher and group mode is NOGROUPS.
/// - they are an administrator (mostly for site-level wikis).
if (($userid and ($USER->id == $userid)) or ($groupmode == VISIBLEGROUPS) or
- (($groupmode == SEPARATEGROUPS) and ismember($usersgroup, $userid)) or
+ (($groupmode == SEPARATEGROUPS) and groups_is_member($usersgroup, $userid)) or
(wiki_is_teacheredit($wiki, $USER->id)) or
(wiki_is_teacher($wiki, $USER->id) and (!$usersgroup or $groupmode == NOGROUPS))) {
$can_access = true;
foreach ($posts as $post) {
- if (empty($groupid) || ismember($groupid, $post->userid)) {
+ if (empty($groupid) || groups_is_member($groupid, $post->userid)) {
$tmpactivity = new Object;
// check group membership, if necessary
if ($groupid) {
// check user's group
- if (!ismember($groupid, $submission->userid)) {
+ if (!groups_is_member($groupid, $submission->userid)) {
continue; // skip this user
}
}
// check group membership, if necessary
if ($groupid) {
// check user's group
- if (!ismember($groupid, $submission->userid)) {
+ if (!groups_is_member($groupid, $submission->userid)) {
continue; // skip this submission
}
}
// check group membership, if necessary
if ($groupid) {
// check user's group
- if (!ismember($groupid, $user->id)) {
+ if (!groups_is_member($groupid, $user->id)) {
continue; // skip this user
}
}
// check group membership, if necessary
if ($groupid) {
// check user's group
- if (!ismember($groupid, $user->id)) {
+ if (!groups_is_member($groupid, $user->id)) {
continue; // skip this user
}
}
// check group membership, if necessary
if ($groupid) {
// check user's group
- if (!ismember($groupid, $submission->userid)) {
+ if (!groups_is_member($groupid, $submission->userid)) {
continue; // skip this user
}
}
foreach ($submissions as $submission) {
if ($groupid) {
// check submission's group
- if (!ismember($groupid, $submission->userid)) {
+ if (!groups_is_member($groupid, $submission->userid)) {
continue; // skip this submission
}
}
foreach ($users as $user) {
// skip if student not in group
if ($currentgroup) {
- if (!ismember($currentgroup, $user->id)) {
+ if (!groups_is_member($currentgroup, $user->id)) {
continue;
}
}
//group consistency check : checks the following situations about groups
// trap if user is not same group and groups are separated
$current_group = get_current_group($course->id);
- if ((groupmode($course) == SEPARATEGROUPS) && !ismember($group_id) && !has_capability('moodle/site:accessallgroups', $module_context)) return false;
+ if ((groupmode($course) == SEPARATEGROUPS) && !groups_is_member($group_id) && !has_capability('moodle/site:accessallgroups', $module_context)) return false;
//ownership check : checks the following situations about user
// trap if user is not owner and has cannot see other's entries
//group consistency check : checks the following situations about groups
// trap if user is not same group and groups are separated
$current_group = get_current_group($course->id);
- if ((groupmode($course) == SEPARATEGROUPS) && !ismember($group_id) && !has_capability('moodle/site:accessallgroups', $module_context)) return false;
+ if ((groupmode($course) == SEPARATEGROUPS) && !groups_is_member($group_id) && !has_capability('moodle/site:accessallgroups', $module_context)) return false;
//ownership check : checks the following situations about user
// trap if user is not owner and has cannot see other's entries
/*TODO: $gtrue = false;
if ($mygroups = mygroupid($course->id)){
foreach ($mygroups as $group){
- if (ismember($group, $user->id)){
+ if (groups_is_member($group, $user->id)){
$gtrue = true;
}
}
$isseparategroups = ($course->groupmode == SEPARATEGROUPS and $course->groupmodeforce and
!has_capability('moodle/site:accessallgroups', $coursecontext));
if (!$isseparategroups){
- if ($usergroups = user_group($course->id, $user->id)){
+ if ($usergroups = groups_get_all_groups($course->id, $user->id)){
$groupstr = '';
foreach ($usergroups as $group){
$groupstr .= ' <a href="'.$CFG->wwwroot.'/user/index.php?id='.$course->id.'&group='.$group->id.'">'.$group->name.'</a>,';