Commit a6348332 authored by Clare Lenihan's avatar Clare Lenihan Committed by Clare Lenihan
Browse files

modified search_group to order the groups (by group name) and allow to filter...

modified search_group to order the groups (by group name) and allow to filter search to groups the user isn't in
parent 0671237d
......@@ -46,7 +46,7 @@ $offset = param_integer('offset', 0);
switch($type) {
case 'group':
$data = search_group($query, $limit, $offset, true);
$data = search_group($query, $limit, $offset, 'all');
$data['type'] = 'group';
break;
default:
......
......@@ -231,11 +231,11 @@ function admin_user_search($queries, $constraints, $offset, $limit, $sortfield,
* ),
* );
*/
function search_group($query_string, $limit, $offset = 0, $all = false) {
function search_group($query_string, $limit, $offset = 0, $type = 'member') {
$plugin = get_config('searchplugin');
safe_require('search', $plugin);
return call_static_method(generate_class_name('search', $plugin), 'search_group', $query_string, $limit, $offset, $all);
return call_static_method(generate_class_name('search', $plugin), 'search_group', $query_string, $limit, $offset, $type);
}
function search_selfsearch($query_string, $limit, $offset, $type = 'all') {
......
......@@ -307,6 +307,7 @@ class PluginSearchInternal extends PluginSearch {
* @param string The query string
* @param integer How many results to return
* @param integer What result to start at (0 == first result)
* @param string Which groups to search (all, member, notmember)
* @return array A data structure containing results looking like ...
* $results = array(
* count => integer, // total number of results
......@@ -335,19 +336,19 @@ class PluginSearchInternal extends PluginSearch {
* ),
* );
*/
public static function search_group($query_string, $limit, $offset=0, $all=false) {
public static function search_group($query_string, $limit, $offset=0, $type='member') {
if (is_postgres()) {
return self::search_group_pg($query_string, $limit, $offset, $all);
return self::search_group_pg($query_string, $limit, $offset, $type);
}
else if (is_mysql()) {
return self::search_group_my($query_string, $limit, $offset, $all);
return self::search_group_my($query_string, $limit, $offset, $type);
}
else {
throw new SQLException('search_group() is not implemented for your database engine (' . get_config('dbtype') . ')');
}
}
public static function search_group_pg($query_string, $limit, $offset, $all) {
public static function search_group_pg($query_string, $limit, $offset, $type) {
global $USER;
$sql = "
SELECT
......@@ -359,8 +360,8 @@ class PluginSearchInternal extends PluginSearch {
OR description ILIKE '%' || ? || '%'
)";
$values = array($query_string, $query_string);
if (!$all) {
$sql .= 'AND (
if ($type == 'member') {
$sql .= 'AND (
owner = ? OR id IN (
SELECT "group" FROM {group_member} WHERE member = ?
)
......@@ -368,6 +369,16 @@ class PluginSearchInternal extends PluginSearch {
$values[] = $USER->get('id');
$values[] = $USER->get('id');
}
else if ($type == 'notmember') {
$sql .= 'AND (
owner != ? AND id NOT IN (
SELECT "group" FROM {group_member} WHERE member = ?
)
)';
$values[] = $USER->get('id');
$values[] = $USER->get('id');
}
$sql .= 'ORDER BY name';
$data = get_records_sql_array($sql, $values, $offset, $limit);
$sql = "
......@@ -379,14 +390,22 @@ class PluginSearchInternal extends PluginSearch {
name ILIKE '%' || ? || '%'
OR description ILIKE '%' || ? || '%'
)";
if (!$all) {
$sql .= 'AND (
if ($type == 'member') {
$sql .= 'AND (
owner = ? OR id IN (
SELECT "group" FROM {group_member} WHERE member = ?
)
)
';
}
else if ($type == 'notmember') {
$sql .= 'AND (
owner != ? AND id NOT IN (
SELECT "group" FROM {group_member} WHERE member = ?
)
)
';
}
$count = get_field_sql($sql, $values);
return array(
......@@ -397,7 +416,7 @@ class PluginSearchInternal extends PluginSearch {
);
}
public static function search_group_my($query_string, $limit, $offset, $all) {
public static function search_group_my($query_string, $limit, $offset, $type) {
global $USER;
$sql = "
SELECT
......@@ -409,7 +428,7 @@ class PluginSearchInternal extends PluginSearch {
OR description LIKE '%' || ? || '%'
)";
$values = array($query_string, $query_string);
if (!$all) {
if ($type == 'member') {
$sql .= "AND (
owner = ? OR id IN (
SELECT group FROM {group_member} WHERE member = ?
......@@ -418,6 +437,16 @@ class PluginSearchInternal extends PluginSearch {
$values[] = $USER->get('id');
$values[] = $USER->get('id');
}
else if ($type == 'notmember') {
$sql .= "AND (
owner != ? AND id NOT IN (
SELECT group FROM {group_member} WHERE member = ?
)
)";
$values[] = $USER->get('id');
$values[] = $USER->get('id');
}
$sql .= 'ORDER BY name';
$data = get_records_sql_array($sql, $values, $offset, $limit);
$sql = "
......@@ -429,14 +458,22 @@ class PluginSearchInternal extends PluginSearch {
name LIKE '%' || ? || '%'
OR description LIKE '%' || ? || '%'
)";
if (!$all) {
$sql .= "AND (
if ($type == 'member') {
$sql .= "AND (
owner = ? OR id IN (
SELECT group FROM {group_member} WHERE member = ?
)
)
";
}
else if ($type == 'notmember') {
$sql .= "AND (
owner != ? AND id NOT IN (
SELECT group FROM {group_member} WHERE member = ?
)
)
";
}
$count = get_field_sql($sql, $values);
return array(
......
......@@ -76,8 +76,7 @@ abstract class PluginSearch extends Plugin {
* @param string The query string
* @param integer How many results to return
* @param integer What result to start at (0 == first result)
* @param boolean Return all matching groups, or just ones this user
* is a member of?
* @param string Which groups to search (all, member, notmember)
* @return array A data structure containing results looking like ...
* $results = array(
* count => integer, // total number of results
......@@ -106,7 +105,7 @@ abstract class PluginSearch extends Plugin {
* ),
* );
*/
public static abstract function search_group($query_string, $limit, $offset=0, $all=false);
public static abstract function search_group($query_string, $limit, $offset=0, $type='member');
/**
* Given a query string and limits, return an array of matching objects
......
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