Commit 1a9a4081 authored by Son Nguyen's avatar Son Nguyen

Refactor the function group_get_user_groups(). Bug 1304053

 - Add function comment,
 - Make it compatible to old calls
 - Sort the list out of the queries to reduce the MYSQL queries
 - Set the sort default value when displaying the my group side bar

Change-Id: I50decadcebe8a5cb38706809d70a784e6694c46d
Signed-off-by: default avatarSon Nguyen <son.nguyen@catalyst.net.nz>
parent 6696c633
......@@ -81,7 +81,13 @@ class PluginBlocktypeMyGroups extends SystemBlocktype {
$smarty = smarty_core();
require_once('group.php');
// Group stuff
list($usergroups, $count) = group_get_user_groups($userid, null, $sort, $limit);
if (!empty($limit)) {
list($usergroups, $count) = group_get_user_groups($userid, null, $sort, $limit, 0);
}
else {
$usergroups = group_get_user_groups($userid, null, $sort);
$count = count($usergroups);
}
foreach ($usergroups as $group) {
$group->roledisplay = get_string($group->role, 'grouptype.'.$group->grouptype);
}
......
......@@ -46,10 +46,16 @@ if (!$userid) {
$smarty = smarty_core();
// Group stuff
list($usergroups, $count) = group_get_user_groups($userid, null, $sort, $limit);
if (!empty($limit)) {
list($usergroups, $count) = group_get_user_groups($userid, null, $sort, $limit, $offset);
}
else {
$usergroups = group_get_user_groups($userid, null, $sort);
$count = count($usergroups);
}
foreach ($usergroups as $group) {
$group->roledisplay = get_string($group->role, 'grouptype.'.$group->grouptype);
$group->roledisplay = get_string($group->role, 'grouptype.' . $group->grouptype);
}
$groups = array('data' => $usergroups,
'count' => $count,
......
......@@ -336,7 +336,7 @@ function pieform_element_filebrowser_configure_tabs($viewowner, $prefix) {
if ($groups = $USER->get('grouproles')) {
$tabs['group'] = get_string('groupfiles', 'artefact.file');
require_once(get_config('libroot') . 'group.php');
list($groups) = group_get_user_groups($USER->get('id'));
$groups = group_get_user_groups($USER->get('id'));
if ($selectedtab == 'group') {
if (!$selectedsubtab = (int) param_variable($prefix . '_ownerid', 0)) {
$selectedsubtab = $groups[0]->id;
......
......@@ -109,8 +109,7 @@ function pieform_element_viewacl(Pieform $form, $element) {
'preset' => true
);
$mygroups = array();
list($usergroups) = group_get_user_groups($USER->get('id'));
foreach ($usergroups as $g) {
foreach (group_get_user_groups($USER->get('id')) as $g) {
$group = array(
'type' => 'group',
'id' => $g->id,
......
......@@ -1990,8 +1990,23 @@ function group_get_associated_groups($userid, $filter='all', $limit=20, $offset=
}
function group_get_user_groups($userid=null, $roles=null, $sort=null, $limit=null) {
/**
* returns a list of groups of a user by $userid or the current logged in user, given $roles from cache or database
* where $roles is the list of the user's role in a group
* if the user id is null, the logged in user will take into account
* if $roles is empty, all groups of the user will be returned
*
* @param int $userid
* @param array $roles
* @param string $sort is 'earliest', 'latest', or 'alphabetical'(default),
* sorts the list of groups based on the date the user joined the group or group name
* if empty, the list will be sorted by group name, admin role first
* @param int $limit The number of groups to display per page (page size)
* @param int $offset The first group index in the current page to display
* @param boolean $fromcache if yes, try to return the list from the cache first
* or no, force to query database and update the cache
*/
function group_get_user_groups($userid=null, $roles=null, $sort=null, $limit=null, $offset=0, $fromcache=true) {
global $USER;
static $usergroups = array();
......@@ -2002,14 +2017,7 @@ function group_get_user_groups($userid=null, $roles=null, $sort=null, $limit=nul
$userid = $loggedinid;
}
if (!isset($usergroups[$userid]) || !empty($sort)) {
$order = '';
if ($sort == 'earliest') {
$order = 'gm.ctime ASC, ';
}
if ($sort == 'latest') {
$order = 'gm.ctime DESC, ';
}
if (!$fromcache || !isset($usergroups[$userid])) {
$groups = get_records_sql_array("
SELECT g.id, g.name, gm.role, g.jointype, g.request, g.grouptype, gtr.see_submitted_views, g.category,
......@@ -2020,18 +2028,49 @@ function group_get_user_groups($userid=null, $roles=null, $sort=null, $limit=nul
LEFT OUTER JOIN {group_member} gm1 ON gm1.group = gm.group AND gm1.member = ?
WHERE gm.member = ?
AND g.deleted = 0
ORDER BY " . $order . "g.name, gm.role = 'admin' DESC, gm.role, g.id",
ORDER BY g.name, gm.role = 'admin' DESC, gm.role, g.id",
array($loggedinid, $userid)
);
$usergroups[$userid] = $groups ? $groups : array();
}
if (!empty($sort)) {
// Sort the list of groups based on the date the user joined the group
if ($sort == 'earliest') {
usort($usergroups[$userid],
function ($g1, $g2) {
if ($g1->ctime == $g2->ctime) {
return ($g1->name < $g2->name) ? -1 : 1;
}
return ($g1->ctime < $g2->ctime) ? -1 : 1;
}
);
}
else if ($sort == 'latest') {
usort($usergroups[$userid],
function ($g1, $g2) {
if ($g1->ctime == $g2->ctime) {
return ($g1->name < $g2->name) ? -1 : 1;
}
return ($g1->ctime > $g2->ctime) ? -1 : 1;
}
);
}
else if ($sort == 'alphabetical') {
// Do nothing, the list sorted by the SQL query
}
else {
throw new SystemException('Unknown sort flag: "' . $sort . '"');
}
}
if (empty($roles) && $userid == $loggedinid) {
$count = count($usergroups[$userid]);
if (!empty($limit)) {
$usergroups[$userid] = array_slice($usergroups[$userid], 0, $limit);
$truncatedusergroups = array_slice($usergroups[$userid], $offset, $limit);
return array($truncatedusergroups, $count);
}
return array($usergroups[$userid], $count);
return $usergroups[$userid];
}
$filtered = array();
......@@ -2045,16 +2084,16 @@ function group_get_user_groups($userid=null, $roles=null, $sort=null, $limit=nul
}
$count = count($filtered);
if (!empty($limit)) {
array_slice($filtered, 0, $limit);
$filtered = array_slice($filtered, $offset, $limit);
return array($filtered, $count);
}
return array($filtered, $count);
return $filtered;
}
function group_get_user_admintutor_groups() {
$groups = array();
list($usergroups) = group_get_user_groups();
foreach ($usergroups as $g) {
foreach (group_get_user_groups() as $g) {
if ($g->role == 'admin' || $g->see_submitted_views) {
$groups[] = $g;
}
......
......@@ -2928,22 +2928,28 @@ function profile_sideblock() {
$data['pendingfriends'] = count_records('usr_friend_request', 'owner', $USER->get('id'));
$data['pendingfriendsmessage'] = $data['pendingfriends'] == 1 ? get_string('pendingfriend') : get_string('pendingfriends');
// Check if we want to limit the displayed groups by the account setting
$sort = null;
$limitto = null;
if ($limit = $USER->get_account_preference('groupsideblockmaxgroups')) {
if ($sortorder = $USER->get_account_preference('groupsideblocksortby')) {
$sort = $sortorder;
}
if (!empty($limit)) {
$limitto = $limit;
$limitto = intval($limit);
}
}
list($data['groups'], $total) = group_get_user_groups($USER->get('id'), null, $sort, $limitto);
if (isset($limit) && $limit == '0') {
$sort = null;
if ($sortorder = $USER->get_account_preference('groupsideblocksortby')) {
$sort = $sortorder;
}
if ($limitto === null) {
$data['groups'] = group_get_user_groups($USER->get('id'), null, $sort);
$total = count($data['groups']);
}
else if ($limitto === 0) {
$data['groups'] = null;
}
else {
list($data['groups'], $total) = group_get_user_groups($USER->get('id'), null, $sort, $limitto);
}
$limitstr = '';
if (!empty($limitto) && intval($limitto) < $total) {
if (!empty($limitto) && $limitto < $total) {
switch ($sort) {
case 'earliest':
$limitstr = get_string('numberofmygroupsshowingearliest', 'blocktype.mygroups', $limitto, $total);
......@@ -2951,7 +2957,7 @@ function profile_sideblock() {
case 'latest':
$limitstr = get_string('numberofmygroupsshowinglatest', 'blocktype.mygroups', $limitto, $total);
break;
case 'alphabetical':
default:
$limitstr = get_string('numberofmygroupsshowing', 'blocktype.mygroups', $limitto, $total);
break;
}
......
......@@ -912,8 +912,7 @@ class ElasticsearchFilterAcl extends \Elastica\Filter\BoolOr
private function getGroupsList(){
$list = array();
list($groups) = group_get_user_groups($this->user->get('id'));
foreach ($groups as $group) {
foreach (group_get_user_groups($this->user->get('id')) as $group) {
$list[$group->role][] = $group->id;
$list['member'][] = $group->id;
}
......
......@@ -144,8 +144,7 @@ if (!empty($loggedinid) && $loggedinid != $userid) {
// - the logged in user has a role which is allowed to assess submitted views, or
// - the logged in user is a member & is allowed to invite friends (when the displayed user is a friend)
$groups = array();
list($usergroups) = group_get_user_groups();
foreach ($usergroups as $g) {
foreach (group_get_user_groups() as $g) {
if ($g->role == 'admin' || $g->see_submitted_views || ($is_friend && $g->invitefriends)) {
$groups[] = $g;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment