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)) { ...@@ -47,6 +47,8 @@ if (!group_user_can_leave($group)) {
throw new AccessDeniedException(get_string('cantleavegroup', 'group')); throw new AccessDeniedException(get_string('cantleavegroup', 'group'));
} }
$goto = get_config('wwwroot') . 'group/' . $returnto . '.php' . ($returnto == 'view' ? ('?id=' . $groupid) : '');
$views = count_records_sql( $views = count_records_sql(
'SELECT COUNT(*) 'SELECT COUNT(*)
FROM {view} v FROM {view} v
...@@ -66,7 +68,7 @@ $form = pieform(array( ...@@ -66,7 +68,7 @@ $form = pieform(array(
'submit' => array( 'submit' => array(
'type' => 'submitcancel', 'type' => 'submitcancel',
'value' => array(get_string('yes'), get_string('no')), 'value' => array(get_string('yes'), get_string('no')),
'goto' => get_config('wwwroot') . ($returnto == 'find' ? 'group/find.php' : 'group/mygroups.php') 'goto' => $goto
), ),
'returnto' => array( 'returnto' => array(
'type' => 'hidden', 'type' => 'hidden',
...@@ -83,9 +85,9 @@ $smarty->assign('group', $group); ...@@ -83,9 +85,9 @@ $smarty->assign('group', $group);
$smarty->display('group/leave.tpl'); $smarty->display('group/leave.tpl');
function leavegroup_submit(Pieform $form, $values) { function leavegroup_submit(Pieform $form, $values) {
global $USER, $SESSION, $groupid; global $USER, $SESSION, $groupid, $goto;
group_remove_user($groupid, $USER->get('id')); group_remove_user($groupid, $USER->get('id'));
$SESSION->add_ok_msg(get_string('leftgroup', 'group')); $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'); ...@@ -32,6 +32,7 @@ require_once('searchlib.php');
require_once(get_config('docroot') . 'interaction/lib.php'); require_once(get_config('docroot') . 'interaction/lib.php');
$id = param_integer('id'); $id = param_integer('id');
$membershiptype = param_alpha('membershiptype', null);
if (!$group = get_record('group', 'id', $id, 'deleted', 0)) { if (!$group = get_record('group', 'id', $id, 'deleted', 0)) {
throw new GroupNotFoundException("Couldn't find group with id $id"); throw new GroupNotFoundException("Couldn't find group with id $id");
...@@ -40,11 +41,15 @@ define('TITLE', $group->name . ' - ' . get_string('Members', 'group')); ...@@ -40,11 +41,15 @@ define('TITLE', $group->name . ' - ' . get_string('Members', 'group'));
$role = group_user_access($id); $role = group_user_access($id);
if (!empty($membershiptype) && $role != 'admin') {
throw new AccessDeniedException();
}
// Search related stuff for member pager // Search related stuff for member pager
$query = trim(param_variable('query', '')); $query = trim(param_variable('query', ''));
$offset = param_integer('offset', 0); $offset = param_integer('offset', 0);
$limit = param_integer('limit', 10); $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 = smarty(array('paginator', 'groupmembersearch'), array(), array(), array('sideblocks' => array(interaction_sideblock($id, $role))));
$smarty->assign('group', $group); $smarty->assign('group', $group);
...@@ -54,6 +59,7 @@ $smarty->assign('query', $query); ...@@ -54,6 +59,7 @@ $smarty->assign('query', $query);
$smarty->assign('results', $html); $smarty->assign('results', $html);
$smarty->assign('pagination', $pagination['html']); $smarty->assign('pagination', $pagination['html']);
$smarty->assign('pagination_js', $pagination['javascript']); $smarty->assign('pagination_js', $pagination['javascript']);
$smarty->assign('membershiptype', $membershiptype);
$smarty->display('group/members.tpl'); $smarty->display('group/members.tpl');
?> ?>
...@@ -35,7 +35,15 @@ $query = trim(param_variable('query', '')); ...@@ -35,7 +35,15 @@ $query = trim(param_variable('query', ''));
$offset = param_integer('offset', 0); $offset = param_integer('offset', 0);
$limit = param_integer('limit', 10); $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); log_debug($USER);
json_reply(false, array( json_reply(false, array(
'message' => null, 'message' => null,
...@@ -46,6 +54,7 @@ json_reply(false, array( ...@@ -46,6 +54,7 @@ json_reply(false, array(
'count' => $count, 'count' => $count,
'results' => $count . ' ' . ($count == 1 ? get_string('result') : get_string('results')), 'results' => $count . ' ' . ($count == 1 ? get_string('result') : get_string('results')),
'offset' => $offset, 'offset' => $offset,
'membershiptype' => $membershiptype,
) )
)); ));
......
...@@ -45,6 +45,8 @@ if ($group->jointype != 'request' ...@@ -45,6 +45,8 @@ if ($group->jointype != 'request'
define('TITLE', get_string('requestjoinspecifiedgroup', 'group', $group->name)); define('TITLE', get_string('requestjoinspecifiedgroup', 'group', $group->name));
$goto = get_config('wwwroot') . 'group/' . $returnto . '.php' . ($returnto == 'view' ? ('?id=' . $groupid) : '');
$form = pieform(array( $form = pieform(array(
'name' => 'requestjoingroup', 'name' => 'requestjoingroup',
'autofocus' => false, 'autofocus' => false,
...@@ -59,7 +61,7 @@ $form = pieform(array( ...@@ -59,7 +61,7 @@ $form = pieform(array(
'submit' => array( 'submit' => array(
'type' => 'submitcancel', 'type' => 'submitcancel',
'value' => array(get_string('request', 'group'), get_string('cancel')), '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( 'returnto' => array(
'type' => 'hidden', 'type' => 'hidden',
...@@ -75,7 +77,7 @@ $smarty->assign('group', $group); ...@@ -75,7 +77,7 @@ $smarty->assign('group', $group);
$smarty->display('group/requestjoin.tpl'); $smarty->display('group/requestjoin.tpl');
function requestjoingroup_submit(Pieform $form, $values) { function requestjoingroup_submit(Pieform $form, $values) {
global $SESSION, $USER, $group; global $SESSION, $USER, $group, $goto;
insert_record( insert_record(
'group_member_request', 'group_member_request',
(object)array( (object)array(
...@@ -103,6 +105,6 @@ function requestjoingroup_submit(Pieform $form, $values) { ...@@ -103,6 +105,6 @@ function requestjoingroup_submit(Pieform $form, $values) {
'url' => get_config('wwwroot') . 'group/view.php?id=' . $group->id)); 'url' => get_config('wwwroot') . 'group/view.php?id=' . $group->id));
} }
$SESSION->add_ok_msg(get_string('grouprequestsent', 'group')); $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 ...@@ -46,6 +46,31 @@ $group->admins = get_column_sql("SELECT member
AND role = 'admin'", array($id)); AND role = 'admin'", array($id));
$role = group_user_access($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); $filecounts = ArtefactTypeFileBase::count_user_files(null, null, $group->id);
......
...@@ -59,6 +59,11 @@ $string['Created'] = 'Created'; ...@@ -59,6 +59,11 @@ $string['Created'] = 'Created';
$string['groupadmins'] = 'Group admins'; $string['groupadmins'] = 'Group admins';
$string['grouptype'] = 'Group Type'; $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! // Used to refer to all the members of a group - NOT a "member" group role!
$string['member'] = 'member'; $string['member'] = 'member';
$string['members'] = 'members'; $string['members'] = 'members';
......
...@@ -200,6 +200,7 @@ function group_add_member($groupid, $userid, $role=null) { ...@@ -200,6 +200,7 @@ function group_add_member($groupid, $userid, $role=null) {
} }
$cm->role = $role; $cm->role = $role;
insert_record('group_member', $cm); insert_record('group_member', $cm);
delete_records('group_member_request', 'group', $groupid, 'member', $userid);
$user = optional_userobj($userid); $user = optional_userobj($userid);
} }
...@@ -229,6 +230,72 @@ function group_get_admin_ids($group) { ...@@ -229,6 +230,72 @@ function group_get_admin_ids($group) {
AND role = 'admin'", $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 * Sets up groups for display in mygroups.php and find.php
* *
...@@ -265,45 +332,10 @@ function group_prepare_usergroups_for_display($groups, $returnto='mygroups') { ...@@ -265,45 +332,10 @@ function group_prepare_usergroups_for_display($groups, $returnto='mygroups') {
$group->canleave = group_user_can_leave($group->id); $group->canleave = group_user_can_leave($group->id);
} }
else if ($group->jointype == 'open') { else if ($group->jointype == 'open') {
$group->groupjoin = pieform(array( $group->groupjoin = group_get_join_form('joingroup' . $i++, $group->id);
'name' => 'joingroup' . $i++,
'successcallback' => 'joingroup_submit',
'elements' => array(
'join' => array(
'type' => 'submit',
'value' => get_string('joingroup', 'group')
),
'group' => array(
'type' => 'hidden',
'value' => $group->id
)
)
));
} }
else if ($group->membershiptype == 'invite') { else if ($group->membershiptype == 'invite') {
$group->invite = pieform(array( $group->invite = group_get_accept_form('invite' . $i++, $group->id, $returnto);
'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
)
)
));
} }
else if ($group->membershiptype == 'admin' && $group->requests > 1) { else if ($group->membershiptype == 'admin' && $group->requests > 1) {
$group->requests = array($group->requests); $group->requests = array($group->requests);
...@@ -335,6 +367,21 @@ function group_invite_submit(Pieform $form, $values) { ...@@ -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) { function group_get_role_info($groupid) {
$roles = get_records_sql_assoc('SELECT role, edit_views, see_submitted_views, gr.grouptype FROM {grouptype_roles} gr $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 INNER JOIN {group} g ON g.grouptype = gr.grouptype
...@@ -346,18 +393,32 @@ function group_get_role_info($groupid) { ...@@ -346,18 +393,32 @@ function group_get_role_info($groupid) {
return $roles; return $roles;
} }
function group_get_membersearch_data($group, $query, $offset, $limit) { function group_get_membersearch_data($group, $query, $offset, $limit, $membershiptype) {
$results = get_group_user_search_results($group, $query, $offset, $limit); $results = get_group_user_search_results($group, $query, $offset, $limit, $membershiptype);
$params = array(); $params = array();
if (!empty($query)) { if (!empty($query)) {
$params[] = 'query=' . $query; $params[] = 'query=' . $query;
} }
$params[] = 'limit=' . $limit; $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['cdata'] = array_chunk($results['data'], 2);
$results['roles'] = group_get_role_info($group); $results['roles'] = group_get_role_info($group);
$smarty = smarty_core();
$smarty->assign_by_ref('results', $results); $smarty->assign_by_ref('results', $results);
$smarty->assign('searchurl', $searchurl); $smarty->assign('searchurl', $searchurl);
$smarty->assign('pagebaseurl', $searchurl); $smarty->assign('pagebaseurl', $searchurl);
...@@ -368,7 +429,7 @@ function group_get_membersearch_data($group, $query, $offset, $limit) { ...@@ -368,7 +429,7 @@ function group_get_membersearch_data($group, $query, $offset, $limit) {
$pagination = build_pagination(array( $pagination = build_pagination(array(
'id' => 'member_pagination', 'id' => 'member_pagination',
'class' => 'center', 'class' => 'center',
'url' => get_config('wwwroot') . 'group/view.php?id=' . $group, 'url' => $searchurl,
'count' => $results['count'], 'count' => $results['count'],
'limit' => $limit, 'limit' => $limit,
'offset' => $offset, 'offset' => $offset,
...@@ -383,7 +444,7 @@ function group_get_membersearch_data($group, $query, $offset, $limit) { ...@@ -383,7 +444,7 @@ function group_get_membersearch_data($group, $query, $offset, $limit) {
'resultcounttextplural' => get_string('members', 'group'), '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) { ...@@ -463,7 +524,7 @@ function group_get_menu_tabs($group) {
return $menu; return $menu;
} }
safe_require('grouptype', $group->grouptype); 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)) { if ($plugins = get_records_array('artefact_installed', 'active', 1)) {
foreach ($plugins as &$plugin) { foreach ($plugins as &$plugin) {
if (!in_array($plugin->name, $artefactplugins)) { if (!in_array($plugin->name, $artefactplugins)) {
......
...@@ -340,8 +340,10 @@ function admin_user_search($queries, $constraints, $offset, $limit, $sortfield, ...@@ -340,8 +340,10 @@ function admin_user_search($queries, $constraints, $offset, $limit, $sortfield,
* @param string $query A search string to filter by * @param string $query A search string to filter by
* @param int $offset What result to start showing paginated results from * @param int $offset What result to start showing paginated results from
* @param int $limit How many results to show * @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(); $queries = array();
$constraints = array(); $constraints = array();
if (!empty($query)) { if (!empty($query)) {
...@@ -364,7 +366,7 @@ function get_group_user_search_results($group, $query, $offset, $limit) { ...@@ -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']) { if ($results['count']) {
$userids = array_map(create_function('$a', 'return $a["id"];'), $results['data']); $userids = array_map(create_function('$a', 'return $a["id"];'), $results['data']);
$introductions = get_records_sql_assoc("SELECT owner, title $introductions = get_records_sql_assoc("SELECT owner, title
...@@ -382,11 +384,11 @@ function get_group_user_search_results($group, $query, $offset, $limit) { ...@@ -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'); $plugin = get_config('searchplugin');
safe_require('search', $plugin); safe_require('search', $plugin);
return call_static_method(generate_class_name('search', $plugin), 'group_search_user', 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='') ...@@ -342,9 +342,10 @@ function email_user($userto, $userfrom, $subject, $messagetext, $messagehtml='')
* Both parameters should be objects containing id, preferredname, firstname, * Both parameters should be objects containing id, preferredname, firstname,
* lastname, admin * lastname, admin
* *
* @param object the user that you're trying to format to a string * @param object $user 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 $userto the user that is looking at the string representation (if left
* blank, will default to the currently logged in user). * 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 * @returns string name to display
*/ */
......
...@@ -400,7 +400,7 @@ class PluginSearchInternal extends PluginSearch { ...@@ -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 = ?'; $where = 'WHERE gm.group = ?';
$values = array($group); $values = array($group);
...@@ -419,17 +419,27 @@ class PluginSearchInternal extends PluginSearch { ...@@ -419,17 +419,27 @@ class PluginSearchInternal extends PluginSearch {
$where .= join(' OR ', $str) . ') '; $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) { if ($count > 0) {
$data = get_records_sql_assoc(' $data = get_records_sql_assoc('
SELECT SELECT