Skip to content

Commit

Permalink
MDL-10383 - deprecated get_groups(), user_group() and ismember()
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Aug 15, 2007
1 parent 1d68419 commit 2c386f8
Show file tree
Hide file tree
Showing 35 changed files with 224 additions and 544 deletions.
2 changes: 1 addition & 1 deletion admin/upgradeforumread.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
/// 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])) {
Expand Down
4 changes: 2 additions & 2 deletions blocks/quiz_results/block_quiz_results.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function get_content() {
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
Expand Down Expand Up @@ -262,7 +262,7 @@ function get_content() {
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;
Expand Down
2 changes: 1 addition & 1 deletion blog/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
}
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');
}
}
Expand Down
4 changes: 2 additions & 2 deletions blog/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,9 @@ function blog_user_can_view_user_post($targetuserid, $blogEntry=null) {
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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion calendar/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1557,7 +1557,7 @@ function calendar_get_allowed_types(&$allowed) {
$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);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions course/report/log/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ function print_mnet_log_selector_form($hostid, $course, $selecteduser=0, $select
}

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;
}
Expand Down Expand Up @@ -448,7 +448,7 @@ function print_log_selector_form($course, $selecteduser=0, $selecteddate='today'
}

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;
}
Expand Down
2 changes: 1 addition & 1 deletion enrol/manual/enrol.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ function check_entry($form, $course) {
*/
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;
Expand Down
2 changes: 1 addition & 1 deletion grade/edit/tree/grade.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

// 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)) {
Expand Down
178 changes: 0 additions & 178 deletions group/db/dbbasicgrouplib.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
* @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
Expand Down Expand Up @@ -140,30 +137,6 @@ function groups_db_get_group_settings($groupid, $courseid=false, $alldata=false)

}

/**
* 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
******************************************************************************/
Expand All @@ -185,64 +158,6 @@ function groups_db_group_exists($groupid) {
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
Expand Down Expand Up @@ -399,54 +314,6 @@ function groups_db_remove_member($groupid, $userid) {
}


/**
* 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.
*/
Expand All @@ -455,49 +322,4 @@ function groups_db_set_group_modified($groupid) {
}


/******************************************************************************
* 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;
}

?>
36 changes: 34 additions & 2 deletions group/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@
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;
}
Expand Down Expand Up @@ -216,5 +216,37 @@

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;
}

?>
Loading

0 comments on commit 2c386f8

Please sign in to comment.