membersearchresults.json.php 2.78 KB
Newer Older
1
2
3
4
5
6
<?php
/**
 *
 * @package    mahara
 * @subpackage core
 * @author     Catalyst IT Ltd
7
8
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
9
10
11
 *
 */

12
define('PUBLIC', 1);
13
14
15
define('INTERNAL', 1);
define('JSON', 1);
require(dirname(dirname(__FILE__)) . '/init.php');
16
17
require_once('group.php');
require_once('searchlib.php');
18
19
20

$id     = param_integer('id');
$query  = trim(param_variable('query', ''));
21
$setlimit = param_boolean('setlimit', false);
22
$offset = param_integer('offset', 0);
23
$limit  = param_integer('limit', 10);
24
$sortoptionidx = param_alpha('sortoption', 'adminfirst');
25

26
27
28
define('GROUP', $id);
$group = group_current_group();
if (!is_logged_in() && !$group->public) {
29
    throw new AccessDeniedException(get_string('accessdenied', 'error'));
30
31
}

32
33
$role = group_user_access($group->id);

34
35
36
37
38
39
40
if (!$USER->get('admin') && !$USER->get('staff')) {
    if (!$role && ($group->hidemembers || $group->hidemembersfrommembers)) {
        json_reply('local', get_string('accessdenied', 'error'));
    }
    if ($role != 'admin' && $group->hidemembersfrommembers) {
        json_reply('local', get_string('accessdenied', 'error'));
    }
41
42
}

43
$membershiptype = param_variable('membershiptype', '');
44
$friends = param_integer('friends', 0);
45
if (!empty($membershiptype)) {
46
    if ($role != 'admin') {
47
48
49
50
51
        // Non-admins are allowed to find the 'notinvited' users, but only if 'invitefriends'
        // or 'suggestfriends' is enabled, and they're filtering by their friends list
        if ($membershiptype != 'notinvited' || !$role || !($group->invitefriends || $group->suggestfriends) || !$friends) {
            json_reply('local', get_string('accessdenied', 'error'));
        }
52
53
54
    }
}

55
56
$results = get_group_user_search_results(
    $group->id, $query, $offset, $limit, $membershiptype, null,
57
58
    $friends ? $USER->get('id') : null,
    $sortoptionidx
59
);
60
if (!param_integer('html', 1)) {
61
    foreach ($results['data'] as &$result) {
62
        $result = array('id' => $result['id'], 'name' => display_name($result['id'], $USER->get('id')));
63
    }
64
65
66
    json_reply(false, $results);
}

67
list($html, $pagination, $count, $offset, $membershiptype) = group_get_membersearch_data($results, $id, $query, $membershiptype, $setlimit, $sortoptionidx);
68

69
70
71
72
73
json_reply(false, array(
    'message' => null,
    'data' => array(
        'tablerows' => $html,
        'pagination' => $pagination['html'],
74
        'pagination_js' => $pagination['javascript'],
75
76
77
        'count' => $count,
        'results' => $count . ' ' . ($count == 1 ? get_string('result') : get_string('results')),
        'offset' => $offset,
78
        'setlimit' => $setlimit,
79
        'membershiptype' => $membershiptype,
80
        'sortoption' => $sortoptionidx,
81
82
    )
));