Commit b8c8b505 authored by Robert Lyon's avatar Robert Lyon

Sorting / limiting the Mygroups sideblock (Bug #1304053)

Using the settings that the user has in their account settings.

Also fixing up group_get_user_groups() so that it can return count
before any limiting is applied.

Change-Id: I66318b9d739530b121e5a14928326ef4bdf2f880
Signed-off-by: Robert Lyon's avatarRobert Lyon <robertl@catalyst.net.nz>
parent 07551cc0
......@@ -204,6 +204,7 @@ function accountprefs_submit(Pieform $form, $values) {
// Remember the user's language & theme prefs, so we can reload the page if they change them
$oldlang = $USER->get_account_preference('lang');
$oldtheme = $USER->get_account_preference('theme');
$oldgroupsideblockmaxgroups = $USER->get_account_preference('groupsideblockmaxgroups');
if (get_config('allowmobileuploads')) {
// Make sure the mobile token is formatted / saved correctly
......@@ -250,6 +251,9 @@ function accountprefs_submit(Pieform $form, $values) {
$returndata['message'] = get_string_from_language($values['lang'], 'prefssaved', 'account');
$reload = true;
}
if (isset($values['groupsideblockmaxgroups']) && $values['groupsideblockmaxgroups'] != $oldgroupsideblockmaxgroups) {
$reload = true;
}
if (!empty($reload)) {
// Use PIEFORM_CANCEL here to force a page reload and show the new language.
......
......@@ -20,4 +20,8 @@ $string['earliest'] = 'Earliest joined';
$string['latest'] = 'Latest joined';
$string['alphabetical'] = 'A to Z';
$string['limitto'] = 'Maximum groups displayed';
$string['limittodesc'] = 'Maximum number of groups to display in the block. Leave empty to show all your groups.';
\ No newline at end of file
$string['limittodesc'] = 'Maximum number of groups to display in the block. Leave empty to show all your groups.';
$string['limittodescsideblock'] = 'Maximum number of groups to display in the your "My groups" list. Leave blank to show all your groups.';
$string['numberofmygroupsshowing'] = 'Showing %s of %s my groups';
$string['numberofmygroupsshowingearliest'] = 'Showing earlist %s of %s groups';
$string['numberofmygroupsshowinglatest'] = 'Showing latest %s of %s groups';
\ No newline at end of file
......@@ -81,9 +81,7 @@ class PluginBlocktypeMyGroups extends SystemBlocktype {
$smarty = smarty_core();
require_once('group.php');
// Group stuff
$usergroups = group_get_user_groups($userid, null, $sort, $limit);
$count = count(group_get_user_groups($userid, null, $sort, null));
list($usergroups, $count) = group_get_user_groups($userid, null, $sort, $limit);
foreach ($usergroups as $group) {
$group->roledisplay = get_string($group->role, 'grouptype.'.$group->grouptype);
}
......
......@@ -46,13 +46,13 @@ if (!$userid) {
$smarty = smarty_core();
// Group stuff
$usergroups = group_get_user_groups($userid, null, $sort, null);
list($usergroups, $count) = group_get_user_groups($userid, null, $sort, $limit);
foreach ($usergroups as $group) {
$group->roledisplay = get_string($group->role, 'grouptype.'.$group->grouptype);
}
$groups = array('data' => array_slice($usergroups, $offset, $limit),
'count' => count($usergroups),
$groups = array('data' => $usergroups,
'count' => $count,
'limit' => $limit,
'offset' => $offset,
);
......
......@@ -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');
$groups = group_get_user_groups($USER->get('id'));
list($groups) = group_get_user_groups($USER->get('id'));
if ($selectedtab == 'group') {
if (!$selectedsubtab = (int) param_variable($prefix . '_ownerid', 0)) {
$selectedsubtab = $groups[0]->id;
......
......@@ -109,7 +109,8 @@ function pieform_element_viewacl(Pieform $form, $element) {
'preset' => true
);
$mygroups = array();
foreach (group_get_user_groups($USER->get('id')) as $g) {
list($usergroups) = group_get_user_groups($USER->get('id'));
foreach ($usergroups as $g) {
$group = array(
'type' => 'group',
'id' => $g->id,
......
......@@ -2010,7 +2010,6 @@ function group_get_user_groups($userid=null, $roles=null, $sort=null, $limit=nul
if ($sort == 'latest') {
$order = 'gm.ctime DESC, ';
}
$limitby = (!empty($limit)) ? " LIMIT " . $limit : '';
$groups = get_records_sql_array("
SELECT g.id, g.name, gm.role, g.jointype, g.request, g.grouptype, gtr.see_submitted_views, g.category,
......@@ -2021,14 +2020,18 @@ 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" . $limitby,
ORDER BY " . $order . "g.name, gm.role = 'admin' DESC, gm.role, g.id",
array($loggedinid, $userid)
);
$usergroups[$userid] = $groups ? $groups : array();
}
if (empty($roles) && $userid == $loggedinid) {
return $usergroups[$userid];
$count = count($usergroups[$userid]);
if (!empty($limit)) {
$usergroups[$userid] = array_slice($usergroups[$userid], 0, $limit);
}
return array($usergroups[$userid], $count);
}
$filtered = array();
......@@ -2040,14 +2043,18 @@ function group_get_user_groups($userid=null, $roles=null, $sort=null, $limit=nul
$filtered[] = $g;
}
}
return $filtered;
$count = count($filtered);
if (!empty($limit)) {
array_slice($filtered, 0, $limit);
}
return array($filtered, $count);
}
function group_get_user_admintutor_groups() {
$groups = array();
foreach (group_get_user_groups() as $g) {
list($usergroups) = group_get_user_groups();
foreach ($usergroups as $g) {
if ($g->role == 'admin' || $g->see_submitted_views) {
$groups[] = $g;
}
......
......@@ -2776,7 +2776,37 @@ function profile_sideblock() {
$data['invitedgroupsmessage'] = $data['invitedgroups'] == 1 ? get_string('invitedgroup') : get_string('invitedgroups');
$data['pendingfriends'] = count_records('usr_friend_request', 'owner', $USER->get('id'));
$data['pendingfriendsmessage'] = $data['pendingfriends'] == 1 ? get_string('pendingfriend') : get_string('pendingfriends');
$data['groups'] = group_get_user_groups($USER->get('id'));
// 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;
}
}
list($data['groups'], $total) = group_get_user_groups($USER->get('id'), null, $sort, $limitto);
if (isset($limit) && $limit == '0') {
$data['groups'] = null;
}
$limitstr = '';
if (!empty($limitto) && intval($limitto) < $total) {
switch ($sort) {
case 'earliest':
$limitstr = get_string('numberofmygroupsshowingearliest', 'blocktype.mygroups', $limitto, $total);
break;
case 'latest':
$limitstr = get_string('numberofmygroupsshowinglatest', 'blocktype.mygroups', $limitto, $total);
break;
case 'alphabetical':
$limitstr = get_string('numberofmygroupsshowing', 'blocktype.mygroups', $limitto, $total);
break;
}
}
$data['grouplimitstr'] = $limitstr;
$data['views'] = get_records_sql_array(
'SELECT v.id, v.title, v.urlid, v.owner
FROM {view} v
......
......@@ -201,6 +201,8 @@ function expected_account_preferences() {
'addremovecolumns' => 0,
'maildisabled' => 0,
'tagssideblockmaxtags' => get_config('tagssideblockmaxtags'),
'groupsideblockmaxgroups' => 0,
'groupsideblocksortby' => 'alphabetical',
'hiderealname' => 0,
'multipleblogs' => 0,
'showhomeinfo' => 1,
......@@ -356,6 +358,24 @@ function general_account_prefs_form_elements($prefs) {
'rules' => array('integer' => true, 'minvalue' => 0, 'maxvalue' => 1000),
);
}
$elements['groupsideblockmaxgroups'] = array(
'type' => 'text',
'size' => 4,
'title' => get_string('limitto', 'blocktype.mygroups'),
'description' => get_string('limittodescsideblock', 'blocktype.mygroups'),
'defaultvalue' => isset($prefs->groupsideblockmaxgroups) ? $prefs->groupsideblockmaxgroups : 0,
'rules' => array('regex' => '/^[0-9]*$/', 'minvalue' => 0, 'maxvalue' => 1000),
);
$elements['groupsideblocksortby'] = array(
'type' => 'select',
'defaultvalue' => isset($prefs->groupsideblocksortby) ? $prefs->groupsideblocksortby : 'alphabetical',
'title' => get_string('sortby', 'blocktype.mygroups'),
'options' => array(
'latest' => get_string('latest', 'blocktype.mygroups'),
'earliest' => get_string('earliest', 'blocktype.mygroups'),
'alphabetical' => get_string('alphabetical', 'blocktype.mygroups'),
),
);
if (get_config('userscanhiderealnames')) {
$elements['hiderealname'] = array(
'type' => 'checkbox',
......
......@@ -912,7 +912,7 @@ class ElasticsearchFilterAcl extends \Elastica\Filter\BoolOr
private function getGroupsList(){
$list = array();
$groups = group_get_user_groups($this->user->get('id'));
list($groups) = group_get_user_groups($this->user->get('id'));
foreach ($groups as $group) {
$list[$group->role][] = $group->id;
$list['member'][] = $group->id;
......
......@@ -26,7 +26,9 @@
{foreach from=$sbdata.groups item=group}
<li><a href="{group_homepage_url($group)}">{$group->name}</a>{if $group->role == 'admin'} ({str tag=Admin section=group}){/if}</li>
{/foreach}
</ul></li>
</ul>
<span class="tiny">{$sbdata.grouplimitstr}</span>
</li>
{/if}
{if $sbdata.views}
<li id="views"><strong><a href="{$WWWROOT}view/">{str tag="views"}:</a></strong>
......
......@@ -144,7 +144,8 @@ 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();
foreach (group_get_user_groups() as $g) {
list($usergroups) = group_get_user_groups();
foreach ($usergroups 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