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) { ...@@ -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 // Remember the user's language & theme prefs, so we can reload the page if they change them
$oldlang = $USER->get_account_preference('lang'); $oldlang = $USER->get_account_preference('lang');
$oldtheme = $USER->get_account_preference('theme'); $oldtheme = $USER->get_account_preference('theme');
$oldgroupsideblockmaxgroups = $USER->get_account_preference('groupsideblockmaxgroups');
if (get_config('allowmobileuploads')) { if (get_config('allowmobileuploads')) {
// Make sure the mobile token is formatted / saved correctly // Make sure the mobile token is formatted / saved correctly
...@@ -250,6 +251,9 @@ function accountprefs_submit(Pieform $form, $values) { ...@@ -250,6 +251,9 @@ function accountprefs_submit(Pieform $form, $values) {
$returndata['message'] = get_string_from_language($values['lang'], 'prefssaved', 'account'); $returndata['message'] = get_string_from_language($values['lang'], 'prefssaved', 'account');
$reload = true; $reload = true;
} }
if (isset($values['groupsideblockmaxgroups']) && $values['groupsideblockmaxgroups'] != $oldgroupsideblockmaxgroups) {
$reload = true;
}
if (!empty($reload)) { if (!empty($reload)) {
// Use PIEFORM_CANCEL here to force a page reload and show the new language. // Use PIEFORM_CANCEL here to force a page reload and show the new language.
......
...@@ -20,4 +20,8 @@ $string['earliest'] = 'Earliest joined'; ...@@ -20,4 +20,8 @@ $string['earliest'] = 'Earliest joined';
$string['latest'] = 'Latest joined'; $string['latest'] = 'Latest joined';
$string['alphabetical'] = 'A to Z'; $string['alphabetical'] = 'A to Z';
$string['limitto'] = 'Maximum groups displayed'; $string['limitto'] = 'Maximum groups displayed';
$string['limittodesc'] = 'Maximum number of groups to display in the block. Leave empty to show all your groups.'; $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['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 { ...@@ -81,9 +81,7 @@ class PluginBlocktypeMyGroups extends SystemBlocktype {
$smarty = smarty_core(); $smarty = smarty_core();
require_once('group.php'); require_once('group.php');
// Group stuff // Group stuff
$usergroups = group_get_user_groups($userid, null, $sort, $limit); list($usergroups, $count) = group_get_user_groups($userid, null, $sort, $limit);
$count = count(group_get_user_groups($userid, null, $sort, null));
foreach ($usergroups as $group) { foreach ($usergroups as $group) {
$group->roledisplay = get_string($group->role, 'grouptype.'.$group->grouptype); $group->roledisplay = get_string($group->role, 'grouptype.'.$group->grouptype);
} }
......
...@@ -46,13 +46,13 @@ if (!$userid) { ...@@ -46,13 +46,13 @@ if (!$userid) {
$smarty = smarty_core(); $smarty = smarty_core();
// Group stuff // 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) { 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' => array_slice($usergroups, $offset, $limit), $groups = array('data' => $usergroups,
'count' => count($usergroups), 'count' => $count,
'limit' => $limit, 'limit' => $limit,
'offset' => $offset, 'offset' => $offset,
); );
......
...@@ -336,7 +336,7 @@ function pieform_element_filebrowser_configure_tabs($viewowner, $prefix) { ...@@ -336,7 +336,7 @@ function pieform_element_filebrowser_configure_tabs($viewowner, $prefix) {
if ($groups = $USER->get('grouproles')) { if ($groups = $USER->get('grouproles')) {
$tabs['group'] = get_string('groupfiles', 'artefact.file'); $tabs['group'] = get_string('groupfiles', 'artefact.file');
require_once(get_config('libroot') . 'group.php'); 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 ($selectedtab == 'group') {
if (!$selectedsubtab = (int) param_variable($prefix . '_ownerid', 0)) { if (!$selectedsubtab = (int) param_variable($prefix . '_ownerid', 0)) {
$selectedsubtab = $groups[0]->id; $selectedsubtab = $groups[0]->id;
......
...@@ -109,7 +109,8 @@ function pieform_element_viewacl(Pieform $form, $element) { ...@@ -109,7 +109,8 @@ function pieform_element_viewacl(Pieform $form, $element) {
'preset' => true 'preset' => true
); );
$mygroups = array(); $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( $group = array(
'type' => 'group', 'type' => 'group',
'id' => $g->id, 'id' => $g->id,
......
...@@ -2010,7 +2010,6 @@ function group_get_user_groups($userid=null, $roles=null, $sort=null, $limit=nul ...@@ -2010,7 +2010,6 @@ function group_get_user_groups($userid=null, $roles=null, $sort=null, $limit=nul
if ($sort == 'latest') { if ($sort == 'latest') {
$order = 'gm.ctime DESC, '; $order = 'gm.ctime DESC, ';
} }
$limitby = (!empty($limit)) ? " LIMIT " . $limit : '';
$groups = get_records_sql_array(" $groups = get_records_sql_array("
SELECT g.id, g.name, gm.role, g.jointype, g.request, g.grouptype, gtr.see_submitted_views, g.category, 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 ...@@ -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 = ? LEFT OUTER JOIN {group_member} gm1 ON gm1.group = gm.group AND gm1.member = ?
WHERE gm.member = ? WHERE gm.member = ?
AND g.deleted = 0 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) array($loggedinid, $userid)
); );
$usergroups[$userid] = $groups ? $groups : array(); $usergroups[$userid] = $groups ? $groups : array();
} }
if (empty($roles) && $userid == $loggedinid) { 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(); $filtered = array();
...@@ -2040,14 +2043,18 @@ function group_get_user_groups($userid=null, $roles=null, $sort=null, $limit=nul ...@@ -2040,14 +2043,18 @@ function group_get_user_groups($userid=null, $roles=null, $sort=null, $limit=nul
$filtered[] = $g; $filtered[] = $g;
} }
} }
$count = count($filtered);
return $filtered; if (!empty($limit)) {
array_slice($filtered, 0, $limit);
}
return array($filtered, $count);
} }
function group_get_user_admintutor_groups() { function group_get_user_admintutor_groups() {
$groups = array(); $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) { if ($g->role == 'admin' || $g->see_submitted_views) {
$groups[] = $g; $groups[] = $g;
} }
......
...@@ -2776,7 +2776,37 @@ function profile_sideblock() { ...@@ -2776,7 +2776,37 @@ function profile_sideblock() {
$data['invitedgroupsmessage'] = $data['invitedgroups'] == 1 ? get_string('invitedgroup') : get_string('invitedgroups'); $data['invitedgroupsmessage'] = $data['invitedgroups'] == 1 ? get_string('invitedgroup') : get_string('invitedgroups');
$data['pendingfriends'] = count_records('usr_friend_request', 'owner', $USER->get('id')); $data['pendingfriends'] = count_records('usr_friend_request', 'owner', $USER->get('id'));
$data['pendingfriendsmessage'] = $data['pendingfriends'] == 1 ? get_string('pendingfriend') : get_string('pendingfriends'); $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( $data['views'] = get_records_sql_array(
'SELECT v.id, v.title, v.urlid, v.owner 'SELECT v.id, v.title, v.urlid, v.owner
FROM {view} v FROM {view} v
......
...@@ -201,6 +201,8 @@ function expected_account_preferences() { ...@@ -201,6 +201,8 @@ function expected_account_preferences() {
'addremovecolumns' => 0, 'addremovecolumns' => 0,
'maildisabled' => 0, 'maildisabled' => 0,
'tagssideblockmaxtags' => get_config('tagssideblockmaxtags'), 'tagssideblockmaxtags' => get_config('tagssideblockmaxtags'),
'groupsideblockmaxgroups' => 0,
'groupsideblocksortby' => 'alphabetical',
'hiderealname' => 0, 'hiderealname' => 0,
'multipleblogs' => 0, 'multipleblogs' => 0,
'showhomeinfo' => 1, 'showhomeinfo' => 1,
...@@ -356,6 +358,24 @@ function general_account_prefs_form_elements($prefs) { ...@@ -356,6 +358,24 @@ function general_account_prefs_form_elements($prefs) {
'rules' => array('integer' => true, 'minvalue' => 0, 'maxvalue' => 1000), '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')) { if (get_config('userscanhiderealnames')) {
$elements['hiderealname'] = array( $elements['hiderealname'] = array(
'type' => 'checkbox', 'type' => 'checkbox',
......
...@@ -912,7 +912,7 @@ class ElasticsearchFilterAcl extends \Elastica\Filter\BoolOr ...@@ -912,7 +912,7 @@ class ElasticsearchFilterAcl extends \Elastica\Filter\BoolOr
private function getGroupsList(){ private function getGroupsList(){
$list = array(); $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) { foreach ($groups as $group) {
$list[$group->role][] = $group->id; $list[$group->role][] = $group->id;
$list['member'][] = $group->id; $list['member'][] = $group->id;
......
...@@ -26,7 +26,9 @@ ...@@ -26,7 +26,9 @@
{foreach from=$sbdata.groups item=group} {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> <li><a href="{group_homepage_url($group)}">{$group->name}</a>{if $group->role == 'admin'} ({str tag=Admin section=group}){/if}</li>
{/foreach} {/foreach}
</ul></li> </ul>
<span class="tiny">{$sbdata.grouplimitstr}</span>
</li>
{/if} {/if}
{if $sbdata.views} {if $sbdata.views}
<li id="views"><strong><a href="{$WWWROOT}view/">{str tag="views"}:</a></strong> <li id="views"><strong><a href="{$WWWROOT}view/">{str tag="views"}:</a></strong>
......
...@@ -144,7 +144,8 @@ if (!empty($loggedinid) && $loggedinid != $userid) { ...@@ -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 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) // - the logged in user is a member & is allowed to invite friends (when the displayed user is a friend)
$groups = array(); $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)) { if ($g->role == 'admin' || $g->see_submitted_views || ($is_friend && $g->invitefriends)) {
$groups[] = $g; $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