Commit a95b6ef4 authored by Richard Mansfield's avatar Richard Mansfield

Change group membership link on find user page:

- Use template rather than js to generate groupbox html
- Combine invite & controlled memberships into single groupbox form
- Only show one user's membership form at any time, in the middle of the page
- Add a close button
Signed-off-by: default avatarRichard Mansfield <richardm@catalyst.net.nz>
parent ee0ca8a8
......@@ -33,13 +33,9 @@ define('JSON', 1);
require(dirname(dirname(__FILE__)) . '/init.php');
$userid = param_integer('userid');
$jointype = param_variable('jointype');
$groupdata = array();
$initialgroups = array('controlled' => array(), 'invite' => array());
if (!in_array($jointype, array('controlled', 'invite'))) {
json_reply('local', get_string('accessdenied', 'error'));
}
if ($jointype == 'controlled') {
/* Get (a) controlled membership groups,
(b) request membership groups where the displayed user has requested membership,
where the logged in user either:
......@@ -56,7 +52,7 @@ if ($jointype == 'controlled') {
* array(...),
* );
*/
$request = get_records_sql_array("SELECT g.*, gm.role,
$controlled = get_records_sql_array("SELECT g.*, gm.role,
(SELECT 1 FROM {group_member} gm1 WHERE gm1.member = ? AND gm1.group = g.id) AS member,
(SELECT gm1.role FROM {group_member} gm1 WHERE gm1.member = ? AND gm1.group = g.id) AS memberrole
FROM {group} g
......@@ -67,8 +63,20 @@ $request = get_records_sql_array("SELECT g.*, gm.role,
AND (g.jointype = 'controlled' OR (g.jointype = 'request' AND gmr.member = ?))
AND (gm.role = 'admin' OR gtr.see_submitted_views = 1)
AND g.deleted = 0", array($userid, $userid, $userid, $USER->get('id'), $userid));
if ($controlled) {
foreach ($controlled as &$g) {
if ($g->member) {
$g->checked = true;
$initialgroups['controlled'][] = $g->id;
if ($g->role != 'admin') {
$g->disabled = true;
}
}
}
$groupdata['controlled'] = $controlled;
}
elseif ($jointype == 'invite') {
/* Get 'Invite olny' groups where the logged in user is a group admin.
@return array A data structure containing results looking like ...
* $results = array(
......@@ -81,7 +89,7 @@ elseif ($jointype == 'invite') {
* array(...),
* );
*/
$request = get_records_sql_array("SELECT g.*, gm.role,
$invite = get_records_sql_array("SELECT g.*, gm.role,
(SELECT 1 FROM {group_member_invite} gi WHERE gi.member = ? AND gi.group = g.id) AS invited,
(SELECT 1 FROM {group_member} gm1 WHERE gm1.member = ? AND gm1.group = g.id) AS member
FROM {group} g
......@@ -90,9 +98,26 @@ $request = get_records_sql_array("SELECT g.*, gm.role,
AND g.jointype = 'invite'
AND gm.role = 'admin'
AND g.deleted = 0", array($userid, $userid, $USER->get('id')));
if ($invite) {
foreach ($invite as &$g) {
if ($g->member || $g->invited) {
$g->checked = true;
$g->disabled = true;
$initialgroups['invite'][] = $g->id;
}
}
$groupdata['invite'] = $invite;
}
$data['data'] = $request;
$smarty = smarty_core();
$smarty->assign('data', $groupdata);
$smarty->assign('userid', $userid);
$data['data'] = array(
'html' => $smarty->fetch('group/editgroupmembership.tpl'),
'initialgroups' => $initialgroups,
);
$data['error'] = false;
$data['message'] = null;
......
......@@ -20,9 +20,8 @@ Array.prototype.compare = function(testArr) {
var ul = null;
var initialgroups = new Array();
var reversetypes = {'invite':'controlled','controlled':'invite'};
function showGroupBox(event, user_id, type) {
function showGroupBox(event, user_id) {
replaceChildNodes('messages');
if (event.preventDefault) {
......@@ -31,17 +30,22 @@ function showGroupBox(event, user_id, type) {
event.returnValue = false;
}
ul = $(type + 'groupbox_' + user_id).getElementsByTagName('ul')[0];
if (!$('groupbox')) {
appendChildNodes(
getFirstElementByTagAndClassName('body'),
DIV({'id':'groupbox','class':'groupbox hidden'})
);
}
if (getStyle($(type + 'groupbox_' + user_id), 'display') == 'block'){
hideElement($(type + 'groupbox_' + user_id));
if (hasElementClass('groupbox', 'hidden')) {
getitems(user_id, function() {
removeElementClass('groupbox', 'hidden');
});
}
else {
hideElement($(reversetypes[type] + 'groupbox_' + user_id));
getitems(user_id, type, function() {
showElement($(type + 'groupbox_' + user_id));
});
addElementClass('groupbox', 'hidden');
}
return false;
}
function changemembership(event, user_id, type) {
......@@ -53,8 +57,6 @@ function changemembership(event, user_id, type) {
event.returnValue = false;
}
ul = $(type + 'groupbox_' + user_id).getElementsByTagName('ul')[0];
var groups = document.getElementsByName(type+'group_'+user_id);
var resultgroups = new Array();
......@@ -64,61 +66,43 @@ function changemembership(event, user_id, type) {
}
});
// apply changes only if something has been changed
if (!initialgroups[user_id].compare(resultgroups)){
if (!initialgroups[type].compare(resultgroups)){
sendjsonrequest('../group/changegroupsmembership.json.php',
{
'jointype':type,
'userid':user_id,
'resultgroups':resultgroups.join(','),
'initialgroups':initialgroups[user_id].join(',')
'initialgroups':initialgroups[type].join(',')
}, 'POST',
function() {
getitems(user_id, type, function() {});
addElementClass('groupbox', 'hidden');
});
}
}
function getitems(user_id, type, successfunction) {
function getitems(user_id, successfunction) {
sendjsonrequest('../group/controlledgroups.json.php', {
'userid':user_id,
'jointype':type
}, 'GET',
function(groups) {
var results = new Array();
initialgroups[user_id] = [];
if (groups.data == false) {
results.push(LI(get_string('nogroups')));
}
else {
forEach(groups.data, function(group) {
var li = LI('');
var input = INPUT({
'type':'checkbox',
'class':'checkbox',
'name':type+'group_'+user_id,
'value':group.id
});
if (group.member || group.invited) {
input.checked = true;
initialgroups[user_id].push(group.id);
}
appendChildNodes(li, input, '\u00A0\u00A0', group.name);
if (group.invited || (type == 'invite' && group.member) || (group.role == 'tutor' && ((group.memberrole == 'member' && input.checked) || (group.member && group.memberrole != 'member')))) {
li.setAttribute('class', 'disabled');
input.disabled = true;
}
results.push(li);
});
var a = A({
'href':'#'
}, '\u00A0\u00A0', get_string('applychanges'));
a.setAttribute('onclick', 'changemembership(event, '+user_id+', \''+type+'\');');
results.push(LI({}, a));
}
replaceChildNodes(ul, results);
if (ul.childNodes.length) ul.lastChild.className = 'last';
function(data) {
replaceChildNodes('groupbox');
$('groupbox').innerHTML = data.data.html;
var jt = getElementsByTagAndClassName('div', 'jointype', 'groupbox');
var jtwidth = 300;
forEach(jt, function(elem) { setStyle(elem, {'width': jtwidth + 'px'}); });
var gbwidth = jt.length == 2 ? 640 : 315;
var d = getElementDimensions('groupbox');
var vpdim = getViewportDimensions();
var newtop = getViewportPosition().y + Math.max((vpdim.h - d.h) / 2, 5);
setStyle('groupbox', {
'width': gbwidth + 'px',
'left': (vpdim.w - d.w) / 2 + 'px',
'top': newtop + 'px',
'position': 'absolute'
});
initialgroups = data.data.initialgroups;
successfunction();
});
}
......
......@@ -82,8 +82,8 @@ $string['hasbeeninvitedtojoin'] = 'has been invited to join this group';
$string['groupinvitesfrom'] = 'Invited to join:';
$string['requestedmembershipin'] = 'Requested membership in:';
$string['controlledmembership'] = 'Controlled Group membership';
$string['invitemembership'] = 'Invite only membership';
$string['editgroupmembership'] = 'Edit group membership';
$string['editmembershipforuser'] = 'Edit membership for %s';
$string['changedgroupmembership'] = 'Group membership updated sucessfully.';
$string['changedgroupmembershipsubject'] = 'Your group memberships have been changed';
$string['addedtogroupsmessage'] = "%s has added you to the group(s):\n\n%s\n\n";
......@@ -170,7 +170,6 @@ $string['allgroups'] = 'All groups';
$string['allgroupmembers'] = 'All group members';
$string['trysearchingforgroups'] = 'Try %ssearching for groups%s to join!';
$string['nogroupsfound'] = 'No groups found.';
$string['nogroups'] = 'No groups.';
$string['group'] = 'group';
$string['Group'] = 'Group';
$string['groups'] = 'groups';
......
......@@ -1266,20 +1266,33 @@ function group_get_associated_groups($userid, $filter='all', $limit=20, $offset=
function group_get_user_groups($userid=null, $roles=null) {
static $usergroups = array();
if (is_null($userid)) {
global $USER;
$userid = $USER->get('id');
}
if ($groups = get_records_sql_array(
"SELECT g.id, g.name, gm.role, g.jointype, g.grouptype
if (empty($roles) && isset($usergroups[$userid])) {
return $usergroups[$userid];
}
if (!$groups = get_records_sql_array(
"SELECT g.id, g.name, gm.role, g.jointype, g.grouptype, gtr.see_submitted_views
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)
WHERE gm.member = ?
AND g.deleted = 0 " . (is_array($roles) ? (' AND gm.role IN (' . join(',', array_map('db_quote', $roles)) . ')') : '') . "
ORDER BY gm.role = 'admin' DESC, gm.role, g.id", array($userid))) {
return $groups;
$groups = array();
}
return array();
if (empty($roles)) {
$usergroups[$userid] = $groups;
}
return $groups;
}
......
......@@ -419,7 +419,7 @@ input#files_filebrowser_edit_artefact {
color: #333;
}
/** link buttons **/
.rbuttons a.btn,
a.btn,
.upgradeicon a,
#loginas .btn-loginas {
font-size: 1em;
......@@ -442,7 +442,7 @@ input.button:hover,
input.buttondk:hover,
input.select:hover,
input#files_filebrowser_edit_artefact:hover,
.rbuttons a.btn:hover {
a.btn:hover {
background: #d1d1d1;
text-decoration: none;
cursor: default;
......@@ -455,7 +455,7 @@ input.button:active,
input.buttondk:active,
input.select:active,
input#files_filebrowser_edit_artefact:active,
.rbuttons a.btn:active {
a.btn:active {
border-color: #a3a3a3 #f0f0f0 #f0f0f0 #a3a3a3;
background: #d1d1d1;
}
......@@ -1460,12 +1460,28 @@ ul.groupuserstatus {
width: 180px;
font-size: .9em;
}
#friendslist ul.actionlist div.groupbox{
position: relative;
display: none;
div.groupbox {
width: 500px;
text-align: center;
background: white;
border: 1px solid black;
z-index: 4;
border: 10px solid #d1d1d1;
padding: 1.5em 10px .5em;
}
div.groupbox div.jointype {
border: 1px solid #d1d1d1;
margin: 0;
padding: .5em .5em 1em;
}
div.groupbox h3 {
margin-top: 0;
}
div.groupbox ul {
list-style: none;
margin: 0;
padding: 0;
}
div.groupbox li.last {
margin-top: .5em;
}
ul.groupuserstatus li,
#friendslist ul.actionlist li {
......
<h3>{str tag=editmembershipforuser section=group arg1=display_name($userid)}</h3>
{if !$data}
<p>{str tag=nogroups section=group}</p>
{else}
<div class="fullwidth center">
{foreach from=$data key=type item=groups}
{if $groups}
<div class="{cycle values='fl,fr'} jointype">
<div><strong>{str tag=membershiptype.$type section=group}</strong></div>
<ul>
{foreach from=$groups item=group}
<li>
<input type="checkbox" class="checkbox" name="{$type}group_{$userid}" value="{$group->id}"{if $group->checked} checked{/if}{if $group->disabled} disabled{/if}> {$group->name}
</li>
{/foreach}
<li class="last"><a class="btn" href="" onclick="changemembership(event, {$userid}, '{$type}');">{str tag=applychanges}</a></li>
{/if}
</ul>
</div>
{/foreach}
</div>
{/if}
<div class="cb"></div>
<p class="fullwidth center"><a class="btn" href="" onclick="addElementClass('groupbox', 'hidden');return false;">{str tag=Close}</a></p>
......@@ -39,17 +39,8 @@
{/if}
</li>
{/if}
{if $admingroups->controlled}
<li><a href="#" onclick="showGroupBox(event, {$user->id}, 'controlled')">{str tag='controlledmembership' section='group'}</a></li>
<div class="groupbox" id="controlledgroupbox_{$user->id}">
<ul></ul>
</div>
{/if}
{if $admingroups->invite}
<li><a href="#" onclick="showGroupBox(event, {$user->id}, 'invite')">{str tag='invitemembership' section='group'}</a></li>
<div class="groupbox" id="invitegroupbox_{$user->id}">
<ul></ul>
</div>
{if $admingroups}
<li><a href="" onclick="showGroupBox(event, {$user->id})">{str tag='editgroupmembership' section='group'}</a></li>
{/if}
</ul>
......
......@@ -43,26 +43,15 @@ $limit = 10;
$data = search_user($query, $limit, $offset, array('exclude' => $USER->get('id')));
$data['query'] = $query;
$controlledgroups = count_records_sql("SELECT COUNT(g.id)
FROM {group} g
JOIN {group_member} gm ON (gm.group = g.id)
JOIN {grouptype_roles} gtr ON (gtr.grouptype = g.grouptype AND gtr.role = gm.role)
WHERE gm.member = ?
AND g.jointype = 'controlled'
AND (gm.role = 'admin' OR gtr.see_submitted_views = 1)
AND g.deleted = 0", array($USER->get('id')));
$invite = count_records_sql("SELECT COUNT(g.id)
FROM {group} g
JOIN {group_member} gm ON (gm.group = g.id)
WHERE gm.member = ?
AND g.jointype = 'invite'
AND gm.role = 'admin'
AND g.deleted = 0", array($USER->get('id')));
$admingroups = new StdClass;
$admingroups->controlled = $controlledgroups;
$admingroups->invite = $invite;
require_once(get_config('libroot').'group.php');
$admingroups = false;
foreach (group_get_user_groups() as $g) {
if (($g->jointype == 'invite' && $g->role == 'admin')
|| ($g->jointype == 'controlled' && ($g->role == 'admin' || $g->see_submitted_views))) {
$admingroups = true;
break;
}
}
build_userlist_html($data, 'find', $admingroups);
......@@ -94,7 +83,7 @@ addLoadEvent(function () {
EOF;
$javascript = array('paginator');
if ($admingroups->invite || $admingroups->controlled) {
if ($admingroups) {
array_push($javascript, 'groupbox');
}
$smarty = smarty($javascript, array(), array('applychanges' => 'mahara', 'nogroups' => 'group'), array('sideblocks' => array(friends_control_sideblock('find'))));
......
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