add optional countUsersInGroup method to group backends

This commit is contained in:
Arthur Schiwon 2014-04-04 18:56:14 +02:00
parent 411a810ebe
commit 698297feb3
4 changed files with 47 additions and 0 deletions

View File

@ -34,6 +34,7 @@ define('OC_GROUP_BACKEND_DELETE_GROUP', 0x00000010);
define('OC_GROUP_BACKEND_ADD_TO_GROUP', 0x00000100);
define('OC_GROUP_BACKEND_REMOVE_FROM_GOUP', 0x00001000);
define('OC_GROUP_BACKEND_GET_DISPLAYNAME', 0x00010000);
define('OC_GROUP_BACKEND_COUNT_USERS', 0x00100000);
/**
* Abstract base class for user management
@ -45,6 +46,7 @@ abstract class OC_Group_Backend implements OC_Group_Interface {
OC_GROUP_BACKEND_ADD_TO_GROUP => 'addToGroup',
OC_GROUP_BACKEND_REMOVE_FROM_GOUP => 'removeFromGroup',
OC_GROUP_BACKEND_GET_DISPLAYNAME => 'displayNamesInGroup',
OC_GROUP_BACKEND_COUNT_USERS => 'countUsersInGroup',
);
/**

View File

@ -211,6 +211,20 @@ class OC_Group_Database extends OC_Group_Backend {
return $users;
}
/**
* @brief get the number of all users matching the search string in a group
* @param string $gid
* @param string $search
* @param int $limit
* @param int $offset
* @return int | false
*/
public function countUsersInGroup($gid, $search = '') {
$stmt = OC_DB::prepare('SELECT COUNT(`uid`) AS `count` FROM `*PREFIX*group_user` WHERE `gid` = ? AND `uid` LIKE ?');
$result = $stmt->execute(array($gid, $search.'%'));
return $result->fetchOne();
}
/**
* @brief get a list of all display names in a group
* @param string $gid

View File

@ -157,4 +157,14 @@ class OC_Group_Dummy extends OC_Group_Backend {
}
}
/**
* @brief get the number of all users in a group
* @returns int | bool
*/
public function countUsersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
if(isset($this->groups[$gid])) {
return count($this->groups[$gid]);
}
}
}

View File

@ -186,6 +186,27 @@ class Group {
return array_values($users);
}
/**
* returns the number of users matching the search string
*
* @param string $search
* @return int | bool
*/
public function count($search) {
$users = false;
foreach ($this->backends as $backend) {
if(method_exists($backend, 'countUsersInGroup')) {
if($users === false) {
//we could directly add to a bool variable, but this would
//be ugly
$users = 0;
}
$users += $backend->countUsersInGroup($this->gid, $search);
}
}
return $users;
}
/**
* search for users in the group by displayname
*