webservicelogs.php 8.2 KB
Newer Older
1
2
3
4
<?php
/**
 *
 * @package    mahara
5
 * @subpackage auth-webservice
6
 * @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
13
 *
 */

define('INTERNAL', 1);
define('ADMIN', 1);
14
define('MENUITEM', 'configextensions/webservices/logs');
15
16
17
define('SECTION_PAGE', 'webservicelogs');
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
require_once('webservicessearchlib.php');
Naomi Guyer's avatar
Naomi Guyer committed
18
define('TITLE', get_string('webservices_title', 'auth.webservice'));
19
define('SUBSECTIONHEADING', get_string('webservicelogs', 'auth.webservice'));
20

21
$userquery = param_variable('userquery', null);
22
23
24
if (is_array($userquery)) {
    $userquery = $userquery[0];
}
25
26
$username = (!empty($userquery)) ? get_field('usr', 'username', 'id', $userquery) : '';
$functionquery = param_variable('functionquery', null);
27
28
29
if (is_array($functionquery)) {
    $functionquery = $functionquery[0];
}
30
$functionname = (!empty($functionquery)) ? get_field('external_functions', 'name', 'id', $functionquery) : '';
31

32
$search = (object) array(
33
34
35
    'userquery'      => $username,
    'functionquery'  => $functionname,
    'protocol'       => trim(param_alphanumext('protocol', 'all')),
36
    'authtype'       => trim(param_alphanum('authtype', 'all')),
37
    'onlyerrors'     => (in_array(param_alphanum('onlyerrors', 0), array('on', '1'), true)),
38
39
40
41
    'sortby'         => param_alpha('sortby', 'timelogged'),
    'sortdir'        => param_alpha('sortdir', 'desc'),
    'offset'         => param_integer('offset', 0),
    'limit'          => param_integer('limit', 10),
42
43
44
45
46
);

if ($USER->get('admin')) {
    $institutions = get_records_array('institution', '', '', 'displayname');
    $search->institution = param_alphanum('institution', 'all');
47
48
}
else {
49
50
51
52
    $institutions = get_records_select_array('institution', "name IN ('" . join("','", array_keys($USER->get('admininstitutions'))) . "')", null, 'displayname');
    $search->institution_requested = param_alphanum('institution_requested', 'all');
}

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
list($html, $columns, $searchurl, $pagination) = build_webservice_log_search_results($search);

$institutionselect = '';
if (count($institutions) > 1) {
    $selecttype = $USER->get('admin') ? 'institution' : 'institution_requested';
    $options = array('all' => get_string('All'));
    foreach ($institutions as $institution) {
        $options[$institution->name] = $institution->displayname;
    }
    $institutionfield = array(
        $selecttype => array(
            'type' => 'select',
            'title' => get_string('Institution', 'admin'),
            'defaultvalue' => !empty($search->institution) ? $search->institution : 'all',
            'options' => $options,
        ),
    );
    $institutionselect = array_shift($institutionfield);
}

$protocoloptions = array('all' => get_string('All'));
$protocols = array('REST', 'XML-RPC', 'SOAP');
foreach ($protocols as $protocol) {
    $protocoloptions[$protocol] = $protocol;
}
$authtypes = array('TOKEN', 'USER', 'OAUTH');
$authtypeoptions = array('all' => get_string('All'));
foreach ($authtypes as $authtype) {
    $authtypeoptions[$authtype] = $authtype;
}

$form = array(
    'name' => 'logsearchform',
    'method' => 'post',
    'successcallback' => 'logsearchform_submit',
    'renderer' => 'div',
    'elements' => array(
        'userquery' => array(
            'type' => 'autocomplete',
            'title' => get_string('userauth', 'auth.webservice'),
            'defaultvalue' => !empty($userquery) ? $userquery : null,
            'ajaxurl' => get_config('wwwroot') . 'webservice/admin/users.json.php',
            'initfunction' => 'translate_ids_to_names',
            'multiple' => true,
            'ajaxextraparams' => array(),
            'extraparams' => array(
99
                'maximumSelectionLength' => 1
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
            ),
            'width' => '280px',
        ),
        $institutionselect,
        'protocol' => array(
            'type' => 'select',
            'title' => get_string('protocol', 'auth.webservice'),
            'defaultvalue' => !empty($search->protocol) ? $search->protocol : 'all',
            'options' => $protocoloptions,
        ),
        'authtype' => array(
            'type' => 'select',
            'title' => get_string('sauthtype', 'auth.webservice'),
            'defaultvalue' => !empty($search->authtype) ? $search->authtype : 'all',
            'options' => $authtypeoptions,
        ),
        'functionquery' => array(
            'type' => 'autocomplete',
            'title' => get_string('function', 'auth.webservice'),
            'defaultvalue' => !empty($functionquery) ? $functionquery : '',
            'ajaxurl' => get_config('wwwroot') . 'webservice/admin/functions.json.php',
            'initfunction' => 'translate_ids_to_functions',
            'multiple' => true,
            'ajaxextraparams' => array(),
            'extraparams' => array(
125
                'maximumSelectionLength' => 1
126
127
128
            ),
            'width' => '280px',
        ),
Naomi Guyer's avatar
Naomi Guyer committed
129
         'onlyerrors' => array(
130
            'type' => 'switchbox',
Naomi Guyer's avatar
Naomi Guyer committed
131
            'class' => 'last',
132
133
134
            'title' => get_string('errors', 'auth.webservice'),
            'defaultvalue' => $search->onlyerrors,
        ),
Naomi Guyer's avatar
Naomi Guyer committed
135
136
        'submit' => array(
            'type' => 'submit',
137
            'class' => 'btn-primary',
Naomi Guyer's avatar
Naomi Guyer committed
138
139
            'value' => get_string('go'),
        ),
140
141
142
143
    ),
);
unset($form['elements'][0]);
$form = pieform($form);
144
$smarty = smarty(array('paginator'));
145
setpageicon($smarty, 'icon-puzzle-piece');
146
safe_require('auth', 'webservice');
147

148
149
150
151
152
153
154
155
$smarty->assign('search', $search);
$smarty->assign('alphabet', explode(',', get_string('alphabet')));
$smarty->assign('cancel', get_string('cancel'));
$smarty->assign('institutions', $institutions);
$smarty->assign('protocols', array('REST', 'XML-RPC', 'SOAP'));
$smarty->assign('authtypes', array('TOKEN', 'USER', 'OAUTH'));
$smarty->assign('results', $html);
$smarty->assign('pagination', $pagination['html']);
156
$smarty->assign('pagination_js', $pagination['javascript']);
157
158
159
160
$smarty->assign('columns', $columns);
$smarty->assign('searchurl', $searchurl['url']);
$smarty->assign('sortby', $searchurl['sortby']);
$smarty->assign('sortdir', $searchurl['sortdir']);
161
$smarty->assign('form', $form);
Naomi Guyer's avatar
Naomi Guyer committed
162

163
164
$webservice_menu = PluginAuthWebservice::admin_menu_items();
$smarty->assign('SUBPAGENAV', $webservice_menu);
165
$smarty->display('auth:webservice:webservicelogs.tpl');
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224

function logsearchform_submit(Pieform $form, $values) {
    $query = array();
    $validoptions = array('userquery', 'protocol', 'authtype', 'functionquery', 'onlyerrors');
    foreach ($values as $key => $value) {
        if (in_array($key, $validoptions) === true && !empty($value)) {
            if ($key == 'userquery') {
                $query[$key] = $values['userquery'][0];
            }
            else if ($key == 'functionquery') {
                $query[$key] = $values['functionquery'][0];
            }
            else {
                $query[$key] = $value;
            }
        }
    }
    $goto = '/webservice/admin/webservicelogs.php?' . http_build_query($query);
    redirect($goto);
}

/**
 * Translate the supplied user id to it's display name
 *
 * @param array $ids  User id number
 * @return object $results containing id and text values
 */
function translate_ids_to_names(array $ids) {

    // for an empty list, the element '' is transmitted
    $ids = array_diff($ids, array(''));
    $results = array();
    foreach ($ids as $id) {
        $deleted = get_field('usr', 'deleted', 'id', $id);
        if (($deleted === '0') && is_numeric($id)) {
            $results[] = (object) array('id' => $id, 'text' => display_name($id));
        }
    }
    return $results;
}

/**
 * Translate the supplied id to the name from the external_functions table
 *
 * @param array $ids  external_functions table id number
 * @return object $results containing id and text values
 */
function translate_ids_to_functions(array $ids) {

    // for an empty list, the element '' is transmitted
    $ids = array_diff($ids, array(''));
    $results = array();
    foreach ($ids as $id) {
        if (is_numeric($id)) {
            $results[] = (object) array('id' => $id, 'text' => get_field('external_functions', 'name', 'id', $id));
        }
    }
    return $results;
}