Commit bb792caf authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Add more group controls to view group page and view user page; search & add for pending members

parent 3e5b7503
......@@ -47,6 +47,8 @@ if (!group_user_can_leave($group)) {
throw new AccessDeniedException(get_string('cantleavegroup', 'group'));
}
$goto = get_config('wwwroot') . 'group/' . $returnto . '.php' . ($returnto == 'view' ? ('?id=' . $groupid) : '');
$views = count_records_sql(
'SELECT COUNT(*)
FROM {view} v
......@@ -66,7 +68,7 @@ $form = pieform(array(
'submit' => array(
'type' => 'submitcancel',
'value' => array(get_string('yes'), get_string('no')),
'goto' => get_config('wwwroot') . ($returnto == 'find' ? 'group/find.php' : 'group/mygroups.php')
'goto' => $goto
),
'returnto' => array(
'type' => 'hidden',
......@@ -83,9 +85,9 @@ $smarty->assign('group', $group);
$smarty->display('group/leave.tpl');
function leavegroup_submit(Pieform $form, $values) {
global $USER, $SESSION, $groupid;
global $USER, $SESSION, $groupid, $goto;
group_remove_user($groupid, $USER->get('id'));
$SESSION->add_ok_msg(get_string('leftgroup', 'group'));
redirect($values['returnto'] == 'find' ? '/group/find.php' : '/group/mygroups.php');
redirect($goto);
}
?>
......@@ -32,6 +32,7 @@ require_once('searchlib.php');
require_once(get_config('docroot') . 'interaction/lib.php');
$id = param_integer('id');
$membershiptype = param_alpha('membershiptype', null);
if (!$group = get_record('group', 'id', $id, 'deleted', 0)) {
throw new GroupNotFoundException("Couldn't find group with id $id");
......@@ -40,11 +41,15 @@ define('TITLE', $group->name . ' - ' . get_string('Members', 'group'));
$role = group_user_access($id);
if (!empty($membershiptype) && $role != 'admin') {
throw new AccessDeniedException();
}
// Search related stuff for member pager
$query = trim(param_variable('query', ''));
$offset = param_integer('offset', 0);
$limit = param_integer('limit', 10);
list($html, $pagination, $count, $offset) = group_get_membersearch_data($id, $query, $offset, $limit);
list($html, $pagination, $count, $offset, $membershiptype) = group_get_membersearch_data($id, $query, $offset, $limit, $membershiptype);
$smarty = smarty(array('paginator', 'groupmembersearch'), array(), array(), array('sideblocks' => array(interaction_sideblock($id, $role))));
$smarty->assign('group', $group);
......@@ -54,6 +59,7 @@ $smarty->assign('query', $query);
$smarty->assign('results', $html);
$smarty->assign('pagination', $pagination['html']);
$smarty->assign('pagination_js', $pagination['javascript']);
$smarty->assign('membershiptype', $membershiptype);
$smarty->display('group/members.tpl');
?>
......@@ -35,7 +35,15 @@ $query = trim(param_variable('query', ''));
$offset = param_integer('offset', 0);
$limit = param_integer('limit', 10);
list($html, $pagination, $count, $offset) = group_get_membersearch_data($id, $query, $offset, $limit);
$membershiptype = param_alpha('membershiptype', null);
if (!empty($membershiptype)) {
if (group_user_access($id) != 'admin') {
json_reply('local', get_string('accessdenied', 'error'));
}
}
list($html, $pagination, $count, $offset, $membershiptype) = group_get_membersearch_data($id, $query, $offset, $limit, $membershiptype);
log_debug($USER);
json_reply(false, array(
'message' => null,
......@@ -46,6 +54,7 @@ json_reply(false, array(
'count' => $count,
'results' => $count . ' ' . ($count == 1 ? get_string('result') : get_string('results')),
'offset' => $offset,
'membershiptype' => $membershiptype,
)
));
......
......@@ -45,6 +45,8 @@ if ($group->jointype != 'request'
define('TITLE', get_string('requestjoinspecifiedgroup', 'group', $group->name));
$goto = get_config('wwwroot') . 'group/' . $returnto . '.php' . ($returnto == 'view' ? ('?id=' . $groupid) : '');
$form = pieform(array(
'name' => 'requestjoingroup',
'autofocus' => false,
......@@ -59,7 +61,7 @@ $form = pieform(array(
'submit' => array(
'type' => 'submitcancel',
'value' => array(get_string('request', 'group'), get_string('cancel')),
'goto' => get_config('wwwroot') . ($returnto == 'find' ? 'group/find.php' : 'group/mygroups.php')
'goto' => $goto
),
'returnto' => array(
'type' => 'hidden',
......@@ -75,7 +77,7 @@ $smarty->assign('group', $group);
$smarty->display('group/requestjoin.tpl');
function requestjoingroup_submit(Pieform $form, $values) {
global $SESSION, $USER, $group;
global $SESSION, $USER, $group, $goto;
insert_record(
'group_member_request',
(object)array(
......@@ -103,6 +105,6 @@ function requestjoingroup_submit(Pieform $form, $values) {
'url' => get_config('wwwroot') . 'group/view.php?id=' . $group->id));
}
$SESSION->add_ok_msg(get_string('grouprequestsent', 'group'));
redirect($values['returnto'] == 'find' ? '/group/find.php' : '/group/mygroups.php');
redirect($goto);
}
?>
......@@ -46,6 +46,31 @@ $group->admins = get_column_sql("SELECT member
AND role = 'admin'", array($id));
$role = group_user_access($id);
if ($role) {
if ($role == 'admin') {
$group->membershiptype = 'admin';
$group->requests = count_records('group_member_request', 'group', $group->id);
if ($group->requests > 1) {
$group->requests = array($group->requests);
}
}
else {
$group->membershiptype = 'member';
}
$group->canleave = group_user_can_leave($group->id);
}
else if ($group->jointype == 'invite'
and $invite = get_record('group_member_invite', 'group', $group->id, 'member', $USER->get('id'))) {
$group->membershiptype = 'invite';
$group->invite = group_get_accept_form('invite', $group->id, 'view');
}
else if ($group->jointype == 'request'
and $request = get_record('group_member_request', 'group', $group->id, 'member', $USER->get('id'))) {
$group->membershiptype = 'request';
}
else if ($group->jointype == 'open') {
$group->groupjoin = group_get_join_form('joingroup', $group->id);
}
$filecounts = ArtefactTypeFileBase::count_user_files(null, null, $group->id);
......
......@@ -59,6 +59,11 @@ $string['Created'] = 'Created';
$string['groupadmins'] = 'Group admins';
$string['grouptype'] = 'Group Type';
$string['hasrequestedmembership'] = 'has requested membership of this group';
$string['hasbeeninvitedtojoin'] = 'has been invited to join this group';
$string['groupinvitesfrom'] = 'Invited to join:';
$string['requestedmembershipin'] = 'Requested membership in:';
// Used to refer to all the members of a group - NOT a "member" group role!
$string['member'] = 'member';
$string['members'] = 'members';
......
......@@ -200,6 +200,7 @@ function group_add_member($groupid, $userid, $role=null) {
}
$cm->role = $role;
insert_record('group_member', $cm);
delete_records('group_member_request', 'group', $groupid, 'member', $userid);
$user = optional_userobj($userid);
}
......@@ -229,6 +230,72 @@ function group_get_admin_ids($group) {
AND role = 'admin'", $group);
}
function group_get_join_form($name, $groupid) {
return pieform(array(
'name' => $name,
'successcallback' => 'joingroup_submit',
'elements' => array(
'join' => array(
'type' => 'submit',
'value' => get_string('joingroup', 'group')
),
'group' => array(
'type' => 'hidden',
'value' => $groupid
)
)
));
}
function group_get_accept_form($name, $groupid, $returnto) {
return pieform(array(
'name' => $name,
'renderer' => 'oneline',
'successcallback' => 'group_invite_submit',
'elements' => array(
'accept' => array(
'type' => 'submit',
'value' => get_string('acceptinvitegroup', 'group')
),
'decline' => array(
'type' => 'submit',
'value' => get_string('declineinvitegroup', 'group')
),
'group' => array(
'type' => 'hidden',
'value' => $groupid
),
'returnto' => array(
'type' => 'hidden',
'value' => $returnto
)
)
));
}
function group_get_addmember_form($userid, $groupid) {
return pieform(array(
'name' => 'addmember',
'successcallback' => 'group_addmember_submit',
'renderer' => 'div',
'elements' => array(
'group' => array(
'type' => 'hidden',
'value' => $groupid,
),
'member' => array(
'type' => 'hidden',
'value' => $userid,
),
'submit' => array(
'type' => 'submit',
'value' => get_string('add'),
),
),
));
}
/**
* Sets up groups for display in mygroups.php and find.php
*
......@@ -265,45 +332,10 @@ function group_prepare_usergroups_for_display($groups, $returnto='mygroups') {
$group->canleave = group_user_can_leave($group->id);
}
else if ($group->jointype == 'open') {
$group->groupjoin = pieform(array(
'name' => 'joingroup' . $i++,
'successcallback' => 'joingroup_submit',
'elements' => array(
'join' => array(
'type' => 'submit',
'value' => get_string('joingroup', 'group')
),
'group' => array(
'type' => 'hidden',
'value' => $group->id
)
)
));
$group->groupjoin = group_get_join_form('joingroup' . $i++, $group->id);
}
else if ($group->membershiptype == 'invite') {
$group->invite = pieform(array(
'name' => 'invite' . $i++,
'renderer' => 'oneline',
'successcallback' => 'group_invite_submit',
'elements' => array(
'accept' => array(
'type' => 'submit',
'value' => get_string('acceptinvitegroup', 'group')
),
'decline' => array(
'type' => 'submit',
'value' => get_string('declineinvitegroup', 'group')
),
'group' => array(
'type' => 'hidden',
'value' => $group->id
),
'returnto' => array(
'type' => 'hidden',
'value' => $returnto
)
)
));
$group->invite = group_get_accept_form('invite' . $i++, $group->id, $returnto);
}
else if ($group->membershiptype == 'admin' && $group->requests > 1) {
$group->requests = array($group->requests);
......@@ -335,6 +367,21 @@ function group_invite_submit(Pieform $form, $values) {
}
}
function group_addmember_submit(Pieform $form, $values) {
global $SESSION;
$group = (int)$values['group'];
if (group_user_access($group) != 'admin') {
$SESSION->add_error_msg(get_string('accessdenied', 'error'));
redirect('/group/members.php?id=' . $group . '&membershiptype=request');
}
group_add_member($group, $values['member']);
$SESSION->add_ok_msg(get_string('useradded', 'group'));
if (count_records('group_member_request', 'group', $group)) {
redirect('/group/members.php?id=' . $group . '&membershiptype=request');
}
redirect('/group/members.php?id=' . $group);
}
function group_get_role_info($groupid) {
$roles = get_records_sql_assoc('SELECT role, edit_views, see_submitted_views, gr.grouptype FROM {grouptype_roles} gr
INNER JOIN {group} g ON g.grouptype = gr.grouptype
......@@ -346,18 +393,32 @@ function group_get_role_info($groupid) {
return $roles;
}
function group_get_membersearch_data($group, $query, $offset, $limit) {
$results = get_group_user_search_results($group, $query, $offset, $limit);
function group_get_membersearch_data($group, $query, $offset, $limit, $membershiptype) {
$results = get_group_user_search_results($group, $query, $offset, $limit, $membershiptype);
$params = array();
if (!empty($query)) {
$params[] = 'query=' . $query;
}
$params[] = 'limit=' . $limit;
$searchurl = get_config('wwwroot') . 'group/view.php?' . join('&', $params);
if (!empty($membershiptype)) {
$params[] = 'membershiptype=' . $membershiptype;
}
$searchurl = get_config('wwwroot') . 'group/members.php?id=' . $group . '&' . join('&', $params);
$smarty = smarty_core();
if (!empty($membershiptype)) {
if ($membershiptype == 'request') {
foreach ($results['data'] as &$r) {
$r['addform'] = group_get_addmember_form($r['id'], $group);
}
}
$smarty->assign('membershiptype', $membershiptype);
}
$results['cdata'] = array_chunk($results['data'], 2);
$results['roles'] = group_get_role_info($group);
$smarty = smarty_core();
$smarty->assign_by_ref('results', $results);
$smarty->assign('searchurl', $searchurl);
$smarty->assign('pagebaseurl', $searchurl);
......@@ -368,7 +429,7 @@ function group_get_membersearch_data($group, $query, $offset, $limit) {
$pagination = build_pagination(array(
'id' => 'member_pagination',
'class' => 'center',
'url' => get_config('wwwroot') . 'group/view.php?id=' . $group,
'url' => $searchurl,
'count' => $results['count'],
'limit' => $limit,
'offset' => $offset,
......@@ -383,7 +444,7 @@ function group_get_membersearch_data($group, $query, $offset, $limit) {
'resultcounttextplural' => get_string('members', 'group'),
));
return array($html, $pagination, $results['count'], $offset);
return array($html, $pagination, $results['count'], $offset, $membershiptype);
}
......@@ -463,7 +524,7 @@ function group_get_menu_tabs($group) {
return $menu;
}
safe_require('grouptype', $group->grouptype);
$artefactplugins = call_static_method('GroupType' . $grouptype, 'get_group_artefact_plugins');
$artefactplugins = call_static_method('GroupType' . $group->grouptype, 'get_group_artefact_plugins');
if ($plugins = get_records_array('artefact_installed', 'active', 1)) {
foreach ($plugins as &$plugin) {
if (!in_array($plugin->name, $artefactplugins)) {
......
......@@ -340,8 +340,10 @@ function admin_user_search($queries, $constraints, $offset, $limit, $sortfield,
* @param string $query A search string to filter by
* @param int $offset What result to start showing paginated results from
* @param int $limit How many results to show
* @param array $extra User membershiptype
*
*/
function get_group_user_search_results($group, $query, $offset, $limit) {
function get_group_user_search_results($group, $query, $offset, $limit, $membershiptype) {
$queries = array();
$constraints = array();
if (!empty($query)) {
......@@ -364,7 +366,7 @@ function get_group_user_search_results($group, $query, $offset, $limit) {
}
}
$results = group_user_search($group, $queries, $constraints, $offset, $limit);
$results = group_user_search($group, $queries, $constraints, $offset, $limit, $membershiptype);
if ($results['count']) {
$userids = array_map(create_function('$a', 'return $a["id"];'), $results['data']);
$introductions = get_records_sql_assoc("SELECT owner, title
......@@ -382,11 +384,11 @@ function get_group_user_search_results($group, $query, $offset, $limit) {
}
function group_user_search($group, $queries, $constraints, $offset, $limit) {
function group_user_search($group, $queries, $constraints, $offset, $limit, $membershiptype) {
$plugin = get_config('searchplugin');
safe_require('search', $plugin);
return call_static_method(generate_class_name('search', $plugin), 'group_search_user',
$group, $queries, $constraints, $offset, $limit);
$group, $queries, $constraints, $offset, $limit, $membershiptype);
}
/**
......
......@@ -342,9 +342,10 @@ function email_user($userto, $userfrom, $subject, $messagetext, $messagehtml='')
* Both parameters should be objects containing id, preferredname, firstname,
* lastname, admin
*
* @param object the user that you're trying to format to a string
* @param object the user that is looking at the string representation (if left
* @param object $user the user that you're trying to format to a string
* @param object $userto the user that is looking at the string representation (if left
* blank, will default to the currently logged in user).
* @param boolean $nameonly do not append the user's username even if $userto can see it.
*
* @returns string name to display
*/
......
......@@ -400,7 +400,7 @@ class PluginSearchInternal extends PluginSearch {
}
public static function group_search_user($group, $queries, $constraints, $offset, $limit) {
public static function group_search_user($group, $queries, $constraints, $offset, $limit, $membershiptype) {
$where = 'WHERE gm.group = ?';
$values = array($group);
......@@ -419,17 +419,27 @@ class PluginSearchInternal extends PluginSearch {
$where .= join(' OR ', $str) . ') ';
}
$count = get_field_sql('SELECT COUNT(*) FROM {usr} u INNER JOIN {group_member} gm ON (gm.member = u.id) ' . $where, $values);
$group_member = 'group_member';
if (!empty($membershiptype) && in_array($membershiptype, array('request', 'invite'))) {
$group_member .= '_' . $membershiptype;
$gm_role = '';
$gm_role_order = '';
}
else {
$gm_role = ', gm.role';
$gm_role_order = "gm.role = 'admin' DESC, ";
}
$count = get_field_sql('SELECT COUNT(*) FROM {usr} u INNER JOIN {' . $group_member . '} gm ON (gm.member = u.id) ' . $where, $values);
if ($count > 0) {
$data = get_records_sql_assoc('
SELECT
u.id, u.firstname, u.lastname, u.username, u.email, u.staff, ' . db_format_tsfield('gm.ctime', 'jointime') . ',
gm.role
u.id, u.firstname, u.lastname, u.username, u.email, u.staff, ' . db_format_tsfield('gm.ctime', 'jointime') . $gm_role . '
FROM
{usr} u
INNER JOIN {group_member} gm ON (gm.member = u.id) ' . $where . '
ORDER BY gm.role = \'admin\' DESC, gm.ctime, u.firstname, u.lastname, u.id',
INNER JOIN {' . $group_member . '} gm ON (gm.member = u.id) ' . $where . '
ORDER BY ' . $gm_role_order . 'gm.ctime, u.firstname, u.lastname, u.id',
$values,
$offset,
$limit);
......@@ -442,7 +452,7 @@ class PluginSearchInternal extends PluginSearch {
}
}
else {
$data = false;
$data = array();
}
return array(
......
......@@ -9,45 +9,6 @@
{if $group->membercount > 3}<a href="{$WWWROOT}group/view.php?id={$group->id|escape}&amp;#members" class="links-members">...</a>{/if}
</li>
{if $group->membershiptype == 'member'}
<li>{str tag="youaregroupmember" section="group"}</li>
{if $group->canleave}
<li><a href = "{$WWWROOT}group/leave.php?id={$group->id|escape}&amp;returnto={$returnto}" id="btn-leavegroup">{str tag="leavegroup" section="group"}</a></li>
{/if}
{include file="group/groupuserstatus.tpl" group=$group returnto='find'}
{elseif $group->membershiptype == 'admin'}
<li><a href="{$WWWROOT}group/edit.php?id={$group->id|escape}" id="btn-edit">{str tag="edit"}</a></li>
<li><a href="{$WWWROOT}group/delete.php?id={$group->id|escape}" id="btn-delete">{str tag="delete"}</a></li>
{if $group->jointype == 'request' && $group->requests}
<li>
{if $group->requests == 1}
<a href="{$WWWROOT}group/view.php?id={$group->id|escape}&amp;pending={$group->requests|escape}#members" id="btn-requests">{str tag="thereispendingrequest" section="group"}</a>
{else}
<a href="{$WWWROOT}group/view.php?id={$group->id|escape}&amp;pending={$group->requests[0]|escape}#members" id="btn-requests">{str tag="therearependingrequests" args=$group->requests}</a>
{/if}
</li>
{/if}
{elseif $group->membershiptype == 'invite'}
<li>
{if $group->role}
{assign var=grouptype value=$group->grouptype}
{assign var=grouprole value=$group->role}
{str tag="grouphaveinvitewithrole" section="group"}: {str tag="$grouprole" section="grouptype.$grouptype"}
{else}
{str tag="grouphaveinvite" section="group"}
{/if}
{$group->invite}
</li>
{elseif $group->membershiptype == 'request'}
<li>{str tag="requestedtojoin" section="group"}</li>
{elseif $group->jointype == 'open'}
{$group->groupjoin}
{elseif $group->jointype == 'request'}
<li><a href="{$WWWROOT}group/requestjoin.php?id={$group->id|escape}&amp;returnto={$returnto}" id="btn-requests">{str tag="requestjoingroup" section="group"}</a></li>
{/if}
</ul>
{if $group->membershiptype == 'member'}
<li>{str tag="youaregroupmember" section="group"}</li>
{if $group->canleave}
<li><a href = "{$WWWROOT}group/leave.php?id={$group->id|escape}&amp;returnto={$returnto}" id="btn-leavegroup">{str tag="leavegroup" section="group"}</a></li>
{/if}
{elseif $group->membershiptype == 'admin'}
<li><a href="{$WWWROOT}group/edit.php?id={$group->id|escape}" id="btn-edit">{str tag="edit"}</a></li>
<li><a href="{$WWWROOT}group/delete.php?id={$group->id|escape}" id="btn-delete">{str tag="delete"}</a></li>
{if $group->jointype == 'request' && $group->requests}
<li>
{if $group->requests == 1}
<a href="{$WWWROOT}group/members.php?id={$group->id|escape}&amp;membershiptype=request" id="btn-requests">{str tag="thereispendingrequest" section="group"}</a>
{else}
<a href="{$WWWROOT}group/members.php?id={$group->id|escape}&amp;membershiptype=request" id="btn-requests">{str tag="therearependingrequests" section="group" args=$group->requests}</a>
{/if}
</li>
{/if}
{elseif $group->membershiptype == 'invite'}
<li>
{if $group->role}
{assign var=grouptype value=$group->grouptype}
{assign var=grouprole value=$group->role}
{str tag="grouphaveinvitewithrole" section="group"}: {str tag="$grouprole" section="grouptype.$grouptype"}
{else}
{str tag="grouphaveinvite" section="group"}
{/if}
{$group->invite}
</li>
{elseif $group->membershiptype == 'request'}
<li>{str tag="requestedtojoin" section="group"}</li>
{elseif $group->jointype == 'open'}
{$group->groupjoin}
{elseif $group->jointype == 'request'}
<li><a href="{$WWWROOT}group/requestjoin.php?id={$group->id|escape}&amp;returnto={$returnto}" id="btn-requests">{str tag="requestjoingroup" section="group"}</a></li>
{/if}
......@@ -13,7 +13,10 @@
<input type="text" name="query" id="query" value="{$query|escape}">
</label>
<button id="query-button" type="submit">{str tag="go"}</button>
{if $membershiptype}<input type="hidden" name="membershiptype" value="{$membershiptype|escape}" />{/if}
</div>
</form>
{if $membershiptype}<h3>{str tag=pendingmembers section=group}</h3>{/if}
<div id="results">
<table id="membersearchresults" class="tablerenderer">
<tbody>
......@@ -23,7 +26,6 @@
</div>
{$pagination}
<script type="text/javascript">{$pagination_js}</script>
</form>
{include file="group/tabend.tpl"}
......
......@@ -5,9 +5,16 @@
<td>
<div class="fl"><img src="{$WWWROOT}thumb.php?type=profileicon&amp;maxsize=40&amp;id={$r.id|escape}" alt=""></div>
<h4><a href="{$WWWROOT}user/view.php?id={$r.id|escape}">{$r.name|escape}</a></h4>
{if $r.role}
<p>{$results.roles[$r.role]->display}{if $caneditroles} (<a href="{$WWWROOT}group/changerole.php?group={$group}&amp;user={$r.id}">{str tag=changerole section=group}</a>){/if}</p>
<p><strong>Joined:</strong> {$r.jointime}</p>