Commit 07551cc0 authored by Yuliya Bozhko's avatar Yuliya Bozhko Committed by Gerrit Code Review

Merge "Adding config to allow the sorting/limiting of groups displayed (Bug #1304053)"

parents 694ba1cb 4fcb89bc
......@@ -846,12 +846,6 @@ class BlockInstance {
$blocktypeclass = generate_class_name('blocktype', $this->get('blocktype'));
$elements = call_static_method($blocktypeclass, 'instance_config_form', $this, $this->get_view()->get('template'));
// @todo: If we know the title is going to be overridden in override_instance_title(),
// hide or disable the title field in the form. Currently not a problem, because the
// blocktypes that override the instance title don't have configurable instances.
// Maybe just remove or simplify title in the elements list below and make all the
// blocktype classes pass the title element in their instance_config_form().
// Block types may specify a method to generate a default title for a block
$hasdefault = method_exists($blocktypeclass, 'get_instance_title');
......@@ -860,17 +854,26 @@ class BlockInstance {
$retractable = (isset($configdata['retractable']) ? $configdata['retractable'] : false);
$retractedonload = (isset($configdata['retractedonload']) ? $configdata['retractedonload'] : $retractable);
if (call_static_method($blocktypeclass, 'override_instance_title', $this)) {
$titleelement = array(
'type' => 'hidden',
'value' => $title,
);
}
else {
$titleelement = array(
'type' => 'text',
'title' => get_string('blocktitle', 'view'),
'description' => $hasdefault ? get_string('defaulttitledescription', 'blocktype.' . blocktype_name_to_namespaced($this->get('blocktype'))) : null,
'defaultvalue' => $title,
'rules' => array('maxlength' => 255),
'hidewhenempty' => $hasdefault,
'expandtext' => get_string('setblocktitle'),
);
}
$elements = array_merge(
array(
'title' => array(
'type' => 'text',
'title' => get_string('blocktitle', 'view'),
'description' => $hasdefault ? get_string('defaulttitledescription', 'blocktype.' . blocktype_name_to_namespaced($this->get('blocktype'))) : null,
'defaultvalue' => $title,
'rules' => array('maxlength' => 255),
'hidewhenempty' => $hasdefault,
'expandtext' => get_string('setblocktitle'),
),
'title' => $titleelement,
'blockconfig' => array(
'type' => 'hidden',
'value' => $this->get('id'),
......
......@@ -14,3 +14,10 @@ defined('INTERNAL') || die();
$string['title'] = 'My groups';
$string['otherusertitle'] = "%s's groups";
$string['description'] = 'Display a list of the groups you belong to';
$string['sortby'] = 'Sort groups by';
$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
......@@ -33,8 +33,47 @@ class PluginBlocktypeMyGroups extends SystemBlocktype {
return array('profile', 'dashboard');
}
/**
* This function renders a list of items as html
*
* @param array items
* @param string template
* @param array options
* @param array pagination
*/
public function render_items(&$items, $template, $options, $pagination) {
$smarty = smarty_core();
$smarty->assign('options', $options);
$smarty->assign('items', $items['data']);
$items['tablerows'] = $smarty->fetch($template);
if ($items['limit'] && $pagination) {
$pagination = build_pagination(array(
'id' => $pagination['id'],
'class' => 'center',
'datatable' => $pagination['datatable'],
'url' => $pagination['baseurl'],
'jsonscript' => $pagination['jsonscript'],
'count' => $items['count'],
'limit' => $items['limit'],
'offset' => $items['offset'],
'numbersincludefirstlast' => false,
'resultcounttextsingular' => $pagination['resultcounttextsingular'] ? $pagination['resultcounttextsingular'] : get_string('result'),
'resultcounttextplural' => $pagination['resultcounttextplural'] ? $pagination['resultcounttextplural'] :get_string('results'),
));
$items['pagination'] = $pagination['html'];
$items['pagination_js'] = $pagination['javascript'];
}
}
public static function render_instance(BlockInstance $instance, $editing=false) {
$userid = $instance->get_view()->get('owner');
$configdata = $instance->get('configdata');
$sort = !empty($configdata['sort']) ? $configdata['sort'] : null;
$limit = !empty($configdata['limitto']) ? $configdata['limitto'] : null;
$view = $instance->get_view();
$baseurl = ($view->get('type') == 'dashboard') ? $view->get_url() . '?id=' . $view->get('id') : $view->get_url();
$baseurl .= (strpos($baseurl, '?') === false ? '?' : '&') . 'block=' . $instance->get('id');
$userid = $view->get('owner');
if (!$userid) {
return '';
}
......@@ -42,17 +81,64 @@ class PluginBlocktypeMyGroups extends SystemBlocktype {
$smarty = smarty_core();
require_once('group.php');
// Group stuff
$usergroups = group_get_user_groups($userid);
$usergroups = group_get_user_groups($userid, null, $sort, $limit);
$count = count(group_get_user_groups($userid, null, $sort, null));
foreach ($usergroups as $group) {
$group->roledisplay = get_string($group->role, 'grouptype.'.$group->grouptype);
}
$smarty->assign('USERGROUPS', $usergroups);
$groups = array('data' => $usergroups,
'count' => $count,
'limit' => $limit,
'offset' => 0,
);
$pagination = array(
'baseurl' => $baseurl,
'id' => 'mygroups_pagination',
'datatable' => 'usergroupstable',
'jsonscript' => 'blocktype/mygroups/mygroups.json.php',
'resultcounttextsingular' => get_string('group', 'group'),
'resultcounttextplural' => get_string('groups', 'group'),
);
self::render_items($groups, 'blocktype:mygroups:mygroupslist.tpl', $configdata, $pagination);
$smarty->assign('USERGROUPS', $groups);
return $smarty->fetch('blocktype:mygroups:mygroups.tpl');
}
public static function has_instance_config() {
return false;
return true;
}
public static function instance_config_form($instance) {
$configdata = $instance->get('configdata');
return array(
'sort' => array(
'type' => 'select',
'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'),
),
'defaultvalue' => isset($configdata['sort']) ? $configdata['sort'] : 'alphabetical',
),
'limitto' => array(
'type' => 'text',
'title' => get_string('limitto', 'blocktype.mygroups'),
'description' => get_string('limittodesc', 'blocktype.mygroups'),
'width' => '30%',
'defaultvalue' => isset($configdata['limitto']) ? $configdata['limitto'] : 20,
'rules' => array(
'maxlength' => 4,
),
),
);
}
public static function instance_config_save($values) {
$values['limitto'] = !empty($values['limitto']) ? (int)$values['limitto'] : '';
return $values;
}
public static function default_copy_type() {
......
<?php
/**
*
* @package mahara
* @subpackage blocktype/mygroups
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
* @copyright For copyright information on Mahara, please see the README file distributed with this software.
*
*/
/**
* returns all user's groups for the mygroups block
*/
define('INTERNAL', 1);
define('JSON', 1);
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
safe_require('blocktype', 'mygroups');
require_once(get_config('libroot') . 'view.php');
require_once(get_config('libroot') . 'group.php');
$offset = param_integer('offset', 0);
$id = param_integer('id');
$blockid = param_integer('block');
$view = new View($id);
if (!can_view_view($view)) {
json_reply(true, get_string('accessdenied', 'error'));
}
$bi = new Blockinstance($blockid);
$configdata = $bi->get('configdata');
$sort = !empty($configdata['sort']) ? $configdata['sort'] : null;
$limit = !empty($configdata['limitto']) ? $configdata['limitto'] : null;
$baseurl = $bi->get_view()->get_url();
$baseurl .= (strpos($baseurl, '?') === false ? '?' : '&') . 'block=' . $bi->get('id');
$userid = $bi->get_view()->get('owner');
if (!$userid) {
json_reply(true, get_string('accessdenied', 'error'));
}
$smarty = smarty_core();
// Group stuff
$usergroups = group_get_user_groups($userid, null, $sort, null);
foreach ($usergroups as $group) {
$group->roledisplay = get_string($group->role, 'grouptype.'.$group->grouptype);
}
$groups = array('data' => array_slice($usergroups, $offset, $limit),
'count' => count($usergroups),
'limit' => $limit,
'offset' => $offset,
);
$pagination = array(
'baseurl' => $baseurl,
'id' => 'mygroups_pagination',
'datatable' => 'usergroupstable',
'jsonscript' => 'blocktype/mygroups/mygroups.json.php',
'resultcounttextsingular' => get_string('group', 'group'),
'resultcounttextplural' => get_string('groups', 'group'),
);
PluginBlocktypeMygroups::render_items($groups, 'blocktype:mygroups:mygroupslist.tpl', $configdata, $pagination);
json_reply(false, array('data' => $groups));
{if $USERGROUPS}
<div id="usergroupstable" class="fullwidth listing">
{foreach from=$USERGROUPS item=item}
<div class="{cycle values='r0,r1'} listrow">
<h3 class="title"><a href="{group_homepage_url($item)}">{$item->name}</a><span class="roledisplay"> - {$item->roledisplay}</span></h3>
{if $item->description}
<div class="details">{$item->description|str_shorten_html:100:true|safe}</div>
{/if}
</div>
{/foreach}
{$USERGROUPS.tablerows|safe}
</div>
{if $USERGROUPS.pagination}
<div id="usergroups_page_container" class="hidden center">{$USERGROUPS.pagination|safe}</div>
{/if}
{if $USERGROUPS.pagination_js}
<script>
addLoadEvent(function() {literal}{{/literal}
{$USERGROUPS.pagination_js|safe}
removeElementClass('usergroups_page_container', 'hidden');
{literal}}{/literal});
</script>
{/if}
{else}
{str tag='notinanygroups' section='group'}
{/if}
{foreach from=$items item=item}
<div class="{cycle values='r0,r1'} listrow">
<h3 class="title"><a href="{group_homepage_url($item)}">{$item->name}</a><span class="roledisplay"> - {$item->roledisplay}</span></h3>
{if $item->description}
<div class="details">{$item->description|str_shorten_html:100:true|safe}</div>
{/if}
</div>
{/foreach}
\ No newline at end of file
......@@ -1991,7 +1991,7 @@ function group_get_associated_groups($userid, $filter='all', $limit=20, $offset=
}
function group_get_user_groups($userid=null, $roles=null) {
function group_get_user_groups($userid=null, $roles=null, $sort=null, $limit=null) {
global $USER;
static $usergroups = array();
......@@ -2002,17 +2002,26 @@ function group_get_user_groups($userid=null, $roles=null) {
$userid = $loggedinid;
}
if (!isset($usergroups[$userid])) {
if (!isset($usergroups[$userid]) || !empty($sort)) {
$order = '';
if ($sort == 'earliest') {
$order = 'gm.ctime ASC, ';
}
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,
g.hidemembers, g.invitefriends, g.urlid, gm1.role AS loggedinrole
g.hidemembers, g.invitefriends, g.urlid, gm.ctime, gm1.role AS loggedinrole
FROM {group} g
JOIN {group_member} gm ON gm.group = g.id
JOIN {grouptype_roles} gtr ON g.grouptype = gtr.grouptype AND gm.role = gtr.role
LEFT OUTER JOIN {group_member} gm1 ON gm1.group = gm.group AND gm1.member = ?
WHERE gm.member = ?
AND g.deleted = 0
ORDER BY g.name, gm.role = 'admin' DESC, gm.role, g.id",
ORDER BY " . $order . "g.name, gm.role = 'admin' DESC, gm.role, g.id" . $limitby,
array($loggedinid, $userid)
);
$usergroups[$userid] = $groups ? $groups : array();
......
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