Commit 570a9189 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Pages for group admins to add/invite members in bulk


Signed-off-by: default avatarRichard Mansfield <richardm@catalyst.net.nz>
parent 4b8fce89
<?php
/**
* Mahara: Electronic portfolio, weblog, resume builder and social networking
* Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
* http://wiki.mahara.org/Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package mahara
* @subpackage core
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
*
*/
define('PUBLIC', 1);
define('INTERNAL', 1);
define('MENUITEM', 'groups/members');
require(dirname(dirname(__FILE__)) . '/init.php');
require_once('group.php');
require_once('pieforms/pieform.php');
define('GROUP', param_integer('id'));
$group = group_current_group();
if (!is_logged_in() && !$group->public) {
throw new AccessDeniedException();
}
$role = group_user_access($group->id);
if ($role != 'admin') {
throw new AccessDeniedException();
}
if ($group->jointype != 'controlled') {
redirect(get_config('wwwroot') . 'group/members.php?id=' . GROUP);
}
define('TITLE', $group->name . ' - ' . get_string('addmembers', 'group'));
$form = pieform(array(
'name' => 'addmembers',
'elements' => array(
'users' => array(
'type' => 'userlist',
'lefttitle' => get_string('potentialmembers', 'group'),
'righttitle' => get_string('userstobeadded', 'group'),
'searchscript' => 'group/membersearchresults.json.php',
'defaultvalue' => array(),
'filter' => false,
'searchparams' => array(
'id' => GROUP,
'limit' => 100,
'html' => false,
'membershiptype' => 'nonmember',
),
),
'submit' => array(
'type' => 'submit',
'value' => get_string('submit'),
)
)
));
$smarty = smarty();
$smarty->assign('subheading', get_string('addmembers', 'group'));
$smarty->assign('form', $form);
$smarty->display('group/form.tpl');
exit;
function addmembers_submit(Pieform $form, $values) {
global $SESSION;
if (empty($values['users'])) {
return;
}
db_begin();
foreach ($values['users'] as $userid) {
group_add_user(GROUP, $userid);
}
db_commit();
$SESSION->add_ok_msg(get_string('newmembersadded', 'group', count($values['users'])));
redirect(get_config('wwwroot') . 'group/members.php?id=' . GROUP);
}
<?php
/**
* Mahara: Electronic portfolio, weblog, resume builder and social networking
* Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
* http://wiki.mahara.org/Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package mahara
* @subpackage core
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
*
*/
define('PUBLIC', 1);
define('INTERNAL', 1);
define('MENUITEM', 'groups/members');
require(dirname(dirname(__FILE__)) . '/init.php');
require_once('group.php');
require_once('pieforms/pieform.php');
define('GROUP', param_integer('id'));
$group = group_current_group();
if (!is_logged_in() && !$group->public) {
throw new AccessDeniedException();
}
$role = group_user_access($group->id);
if ($role != 'admin') {
throw new AccessDeniedException();
}
if ($group->jointype != 'invite') {
redirect(get_config('wwwroot') . 'group/members.php?id=' . GROUP);
}
define('TITLE', $group->name . ' - ' . get_string('sendinvitations', 'group'));
$form = pieform(array(
'name' => 'addmembers',
'elements' => array(
'users' => array(
'type' => 'userlist',
'lefttitle' => get_string('potentialmembers', 'group'),
'righttitle' => get_string('userstobeinvited', 'group'),
'searchscript' => 'group/membersearchresults.json.php',
'defaultvalue' => array(),
'filter' => false,
'searchparams' => array(
'id' => GROUP,
'limit' => 100,
'html' => false,
'membershiptype' => 'notinvited',
),
),
'submit' => array(
'type' => 'submit',
'value' => get_string('submit'),
)
)
));
$smarty = smarty();
$smarty->assign('subheading', get_string('sendinvitations', 'group'));
$smarty->assign('form', $form);
$smarty->display('group/form.tpl');
exit;
function addmembers_submit(Pieform $form, $values) {
global $SESSION, $group, $USER;
if (empty($values['users'])) {
return;
}
foreach ($values['users'] as $userid) {
group_invite_user($group, $userid, $USER->get('id'));
}
$SESSION->add_ok_msg(get_string('invitationssent', 'group', count($values['users'])));
redirect(get_config('wwwroot') . 'group/members.php?id=' . GROUP);
}
...@@ -68,15 +68,20 @@ if ($remove && $member) { ...@@ -68,15 +68,20 @@ if ($remove && $member) {
$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, $membershiptype) = group_get_membersearch_data($group->id, $query, $offset, $limit, $membershiptype);
$results = get_group_user_search_results($group->id, $query, $offset, $limit, $membershiptype);
list($html, $pagination, $count, $offset, $membershiptype) = group_get_membersearch_data($results, $group->id, $query, $membershiptype);
// Type-specific instructions // Type-specific instructions
$instructions = ''; $instructions = '';
if ('admin' == $role) { if ('admin' == $role) {
if ('invite' == $group->jointype) { if ('invite' == $group->jointype) {
$instructions = get_string('instructions:invite', 'group'); $url = get_config('wwwroot') . 'group/inviteusers.php?id=' . GROUP;
} elseif ('controlled' == $group->jointype) { $instructions = get_string('membersdescription:invite', 'group', $url);
$instructions = get_string('instructions:controlled', 'group'); }
else if ('controlled' == $group->jointype) {
$url = get_config('wwwroot') . 'group/addmembers.php?id=' . GROUP;
$instructions = get_string('membersdescription:controlled', 'group', $url);
} }
} }
......
...@@ -51,7 +51,12 @@ if (!empty($membershiptype)) { ...@@ -51,7 +51,12 @@ if (!empty($membershiptype)) {
} }
} }
list($html, $pagination, $count, $offset, $membershiptype) = group_get_membersearch_data($id, $query, $offset, $limit, $membershiptype); $results = get_group_user_search_results($group->id, $query, $offset, $limit, $membershiptype);
if (!param_boolean('html', true)) {
json_reply(false, $results);
}
list($html, $pagination, $count, $offset, $membershiptype) = group_get_membersearch_data($results, $id, $query, $membershiptype);
json_reply(false, array( json_reply(false, array(
'message' => null, 'message' => null,
......
...@@ -173,6 +173,15 @@ $string['notamember'] = 'You are not a member of this group'; ...@@ -173,6 +173,15 @@ $string['notamember'] = 'You are not a member of this group';
$string['notmembermayjoin'] = 'You must join the group \'%s\' to see this page.'; $string['notmembermayjoin'] = 'You must join the group \'%s\' to see this page.';
$string['declinerequestsuccess'] = 'Group membership request has been declined sucessfully.'; $string['declinerequestsuccess'] = 'Group membership request has been declined sucessfully.';
// Bulk add, invite
$string['addmembers'] = 'Add members';
$string['invitationssent'] = '%d invitations sent';
$string['newmembersadded'] = 'Added %d new members';
$string['potentialmembers'] = 'Potential members';
$string['sendinvitations'] = 'Send invitations';
$string['userstobeadded'] = 'Users to be added';
$string['userstobeinvited'] = 'Users to be invited';
// friendslist // friendslist
$string['reasonoptional'] = 'Reason (optional)'; $string['reasonoptional'] = 'Reason (optional)';
$string['request'] = 'Request'; $string['request'] = 'Request';
...@@ -300,8 +309,8 @@ $string['About'] = 'About'; ...@@ -300,8 +309,8 @@ $string['About'] = 'About';
$string['Joined'] = 'Joined'; $string['Joined'] = 'Joined';
$string['instructions:invite'] = 'This is an invite-only group. Invite users through their profile pages.'; $string['membersdescription:invite'] = 'This is an invite-only group. You can invite users through their profile pages or <a href="%s">send multiple invitations at once</a>.';
$string['instructions:controlled'] = 'This is a controlled membership group. Add users through their profile pages.'; $string['membersdescription:controlled'] = 'This is a controlled membership group. You can add users through their profile pages or <a href="%s">add many users at once</a>.';
// View submission // View submission
$string['submit'] = 'Submit'; $string['submit'] = 'Submit';
......
...@@ -828,15 +828,14 @@ function group_prepare_usergroups_for_display($groups, $returnto='mygroups') { ...@@ -828,15 +828,14 @@ function group_prepare_usergroups_for_display($groups, $returnto='mygroups') {
} }
function group_get_membersearch_data($group, $query, $offset, $limit, $membershiptype) { function group_get_membersearch_data($results, $group, $query, $membershiptype) {
global $USER; global $USER;
$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=' . $results['limit'];
if (!empty($membershiptype)) { if (!empty($membershiptype)) {
$params[] = 'membershiptype=' . $membershiptype; $params[] = 'membershiptype=' . $membershiptype;
} }
...@@ -887,8 +886,8 @@ function group_get_membersearch_data($group, $query, $offset, $limit, $membershi ...@@ -887,8 +886,8 @@ function group_get_membersearch_data($group, $query, $offset, $limit, $membershi
'class' => 'center', 'class' => 'center',
'url' => $searchurl, 'url' => $searchurl,
'count' => $results['count'], 'count' => $results['count'],
'limit' => $limit, 'limit' => $results['limit'],
'offset' => $offset, 'offset' => $results['offset'],
'datatable' => 'membersearchresults', 'datatable' => 'membersearchresults',
'jsonscript' => 'group/membersearchresults.json.php', 'jsonscript' => 'group/membersearchresults.json.php',
'firsttext' => '', 'firsttext' => '',
...@@ -900,7 +899,7 @@ function group_get_membersearch_data($group, $query, $offset, $limit, $membershi ...@@ -900,7 +899,7 @@ function group_get_membersearch_data($group, $query, $offset, $limit, $membershi
'resultcounttextplural' => get_string('members', 'group'), 'resultcounttextplural' => get_string('members', 'group'),
)); ));
return array($html, $pagination, $results['count'], $offset, $membershiptype); return array($html, $pagination, $results['count'], $results['offset'], $membershiptype);
} }
......
...@@ -374,7 +374,9 @@ function get_group_user_search_results($group, $query, $offset, $limit, $members ...@@ -374,7 +374,9 @@ function get_group_user_search_results($group, $query, $offset, $limit, $members
foreach ($results['data'] as &$result) { foreach ($results['data'] as &$result) {
$result['name'] = display_name($result); $result['name'] = display_name($result);
$result['introduction'] = isset($introductions[$result['id']]) ? $introductions[$result['id']]->title : ''; $result['introduction'] = isset($introductions[$result['id']]) ? $introductions[$result['id']]->title : '';
$result['jointime'] = strftime(get_string('strftimedate'), $result['jointime']); if (isset($result['jointime'])) {
$result['jointime'] = strftime(get_string('strftimedate'), $result['jointime']);
}
} }
} }
return $results; return $results;
......
...@@ -353,16 +353,12 @@ class PluginSearchInternal extends PluginSearch { ...@@ -353,16 +353,12 @@ class PluginSearchInternal extends PluginSearch {
public static function group_search_user($group, $queries, $constraints, $offset, $limit, $membershiptype) { public static function group_search_user($group, $queries, $constraints, $offset, $limit, $membershiptype) {
$where = 'WHERE gm.group = ?';
$values = array($group);
// Get the correct keyword for case insensitive LIKE
$ilike = db_ilike();
// Only handle OR/AND expressions at the top level. Eventually we may need subexpressions. // Only handle OR/AND expressions at the top level. Eventually we may need subexpressions.
$searchsql = '';
$values = array();
if (!empty($queries)) { if (!empty($queries)) {
$where .= ' AND ( '; $ilike = db_ilike();
$searchsql .= ' AND ( ';
$str = array(); $str = array();
foreach ($queries as $f) { foreach ($queries as $f) {
if (!preg_match('/^[a-zA-Z_0-9"]+$/', $f['field'])) { if (!preg_match('/^[a-zA-Z_0-9"]+$/', $f['field'])) {
...@@ -371,30 +367,73 @@ class PluginSearchInternal extends PluginSearch { ...@@ -371,30 +367,73 @@ class PluginSearchInternal extends PluginSearch {
$str[] = 'u.' . $f['field'] $str[] = 'u.' . $f['field']
. PluginSearchInternal::match_expression($f['type'], $f['string'], $values, $ilike); . PluginSearchInternal::match_expression($f['type'], $f['string'], $values, $ilike);
} }
$where .= join(' OR ', $str) . ') '; $searchsql .= join(' OR ', $str) . ') ';
} }
$group_member = 'group_member'; if ($membershiptype == 'nonmember') {
if (!empty($membershiptype) && in_array($membershiptype, array('request', 'invite'))) { $select = '
$group_member .= '_' . $membershiptype; u.id, u.firstname, u.lastname, u.username, u.email, u.profileicon, u.staff';
$gm_role = ''; $from = '
$gm_role_order = ''; FROM {usr} u
WHERE u.id > 0 AND u.deleted = 0 ' . $searchsql . '
AND NOT u.id IN (SELECT member FROM {group_member} gm WHERE gm.group = ?)';
$values[] = $group;
$orderby = 'u.firstname, u.lastname, u.id';
} }
else { else if ($membershiptype == 'notinvited') {
$gm_role = ', gm.role'; $select = '
$gm_role_order = "gm.role = 'admin' DESC, "; u.id, u.firstname, u.lastname, u.username, u.email, u.profileicon, u.staff';
$from = '
FROM {usr} u
WHERE u.id > 0 AND u.deleted = 0 ' . $searchsql . '
AND NOT u.id IN (SELECT member FROM {group_member} gm WHERE gm.group = ?)
AND NOT u.id IN (SELECT member FROM {group_member_invite} gmi WHERE gmi.group = ?)';
$values[] = $group;
$values[] = $group;
$orderby = 'u.firstname, u.lastname, u.id';
}
else if ($membershiptype == 'request') {
$select = '
u.id, u.firstname, u.lastname, u.username, u.email, u.profileicon,
u.staff, ' . db_format_tsfield('gmr.ctime', 'jointime');
$from = '
FROM {usr} u
INNER JOIN {group_member_request} gmr ON (gmr.member = u.id)
WHERE u.id > 0 AND u.deleted = 0 ' . $searchsql . '
AND gmr.group = ?';
$values[] = $group;
$orderby = 'gmr.ctime, u.firstname, u.lastname, u.id';
}
else if ($membershiptype == 'invite') {
$select = '
u.id, u.firstname, u.lastname, u.username, u.email, u.profileicon,
u.staff, ' . db_format_tsfield('gmi.ctime', 'jointime');
$from = '
FROM {usr} u
INNER JOIN {group_member_invite} gmi ON (gmi.member = u.id)
WHERE u.id > 0 AND u.deleted = 0 ' . $searchsql . '
AND gmi.group = ?';
$values[] = $group;
$orderby = 'gmi.ctime, u.firstname, u.lastname, u.id';
}
else { // All group members
$select = '
u.id, u.firstname, u.lastname, u.username, u.email, u.profileicon,
u.staff, ' . db_format_tsfield('gm.ctime', 'jointime') . ', gm.role';
$from = '
FROM {usr} u
INNER JOIN {group_member} gm ON (gm.member = u.id)
WHERE u.id > 0 AND u.deleted = 0 ' . $searchsql . '
AND gm.group = ?';
$values[] = $group;
$orderby = "gm.role = 'admin' DESC, gm.ctime, u.firstname, u.lastname, u.id";
} }
$count = get_field_sql('SELECT COUNT(*) FROM {usr} u INNER JOIN {' . $group_member . '} gm ON (gm.member = u.id) ' . $where, $values); $count = get_field_sql('SELECT COUNT(*)' . $from, $values);
if ($count > 0) { if ($count > 0) {
$data = get_records_sql_assoc(' $data = get_records_sql_assoc('
SELECT SELECT ' . $select . $from . ' ORDER BY ' . $orderby,
u.id, u.firstname, u.lastname, u.username, u.email, u.profileicon, 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_order . 'gm.ctime, u.firstname, u.lastname, u.id',
$values, $values,
$offset, $offset,
$limit); $limit);
......
{include file="header.tpl"}
{if $subheading}
<h2>{$subheading}</h2>
{/if}
{if $pagedescription}
<p>{$pagedescription}</p>
{elseif $pagedescriptionhtml}
{$pagedescriptionhtml|safe}
{/if}
{$form|safe}
{include file="footer.tpl"}
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