Commit 1f4cee41 authored by Robert Lyon's avatar Robert Lyon Committed by Gerrit Code Review
Browse files

Simplifying the webservice log search functions (Bug #1405282)



Allowing the searching for user and / or function name via autocomplete

Also updating the autocomplete on the webservices/admin/index.php page
to be the same type

Change-Id: I1f15f38d4491205ed0a90ad431345b4b08069d34
Signed-off-by: Robert Lyon's avatarRobert Lyon <robertl@catalyst.net.nz>
parent 875b8c74
......@@ -103,3 +103,19 @@ span.response {
font-weight: bold;
border-bottom: 1px solid #CCCCCC;
}
#logsearchformcontainer div {
display: inline-block;
}
#logsearchformcontainer div .select2-display-none {
display: none;
}
#logsearchformcontainer div input,
#logsearchformcontainer div select {
margin-left: 5px;
}
#logsearchform_onlyerrors_container {
display: block !important;
}
\ No newline at end of file
{include file="header.tpl"}
<form id="logsearchform" action="{$WWWROOT}webservice/admin/webservicelogs.php" method="post">
<div class="searchform">
<label>{str tag='userauth' section='auth.webservice'}:</label>
<input type="text" name="userquery" id="query"{if $search->userquery} value="{$search->userquery}"{/if}>
{if count($institutions) > 1}
<span class="institutions">
<label>{str tag='Institution' section='admin'}:</label>
{if $USER->get('admin')}
<select name="institution" id="institution">
{else}
<select name="institution_requested" id="institution_requested">
{/if}
<option value="all"{if !$.request.institution} selected="selected"{/if}>{str tag=All}</option>
{foreach from=$institutions item=i}
<option value="{$i->name}"{if $i->name == $.request.institution}" selected="selected"{/if}>{$i->displayname}</option>
{/foreach}
</select>
</span>
{/if}
<span class="institutions">
<label>{str tag='protocol' section='auth.webservice'}:</label>
<select name="protocol" id="protocol">
<option value="all"{if !$.request.protocol} selected="selected"{/if}>{str tag=All}</option>
{foreach from=$protocols item=i}
<option value="{$i}"{if $i == $.request.protocol}" selected="selected"{/if}>{$i}</option>
{/foreach}
</select>
</span>
<span class="institutions">
<label>{str tag='sauthtype' section='auth.webservice'}:</label>
<select name="authtype" id="authtype">
<option value="all"{if !$.request.authtype} selected="selected"{/if}>{str tag=All}</option>
{foreach from=$authtypes item=i}
<option value="{$i}"{if $i == $.request.authtype}" selected="selected"{/if}>{$i}</option>
{/foreach}
</select>
</span>
<label>{str tag='function' section='auth.webservice'}:</label>
<input type="text" name="functionquery" id="query"{if $search->functionquery} value="{$search->functionquery}"{/if}>
<button id="query-button" class="btn-search" type="submit">{str tag="go"}</button>
<br/>
<label>{str tag='errors' section='auth.webservice'}:</label>
<input type="checkbox" name="onlyerrors" id="query"{if $search->onlyerrors} CHECKED{/if}>
</div>
<div id="results" class="section">
<div id="logsearchformcontainer">
{$form|safe}
</div>
<div id="results" class="section">
<h2 id="resultsheading">{str tag="Results"}</h2>
{if $results}
<table id="searchresults" class="tablerenderer fullwidth listing">
......@@ -77,8 +36,7 @@
<div>{str tag="noresultsfound"}</div>
{/if}
</div>
</div>
</form>
<script type="application/javascript">
// to clear any offset when submitting form again
jQuery(function() {
......
<?php
/**
*
* @package mahara
* @subpackage webservices
* @author Catalyst IT Ltd
* @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.
*
*/
define('INTERNAL', 1);
define('JSON', 1);
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
require_once(get_config('docroot') . '/lib/searchlib.php');
global $USER;
$request = param_variable('q');
$page = param_integer('page');
if ($page < 1) {
$page = 1;
}
$itemsperpage = 5;
$more = true;
$tmpitem = array();
while ($more && count($tmpitem) < $itemsperpage) {
$items = search_functions($request, $itemsperpage, $itemsperpage * ($page - 1));
$more = $items['count'] > $itemsperpage * $page;
if (!$items['data']) {
$items['data'] = array();
}
foreach ($items['data'] as $item) {
if (count($tmpitem) >= $itemsperpage) {
$more = true;
continue;
}
$tmpitem[] = (object) array('id' => $item->id,
'text' => $item->name);
}
$page++;
}
json_reply(array(
'more' => $more,
'results' => $tmpitem,
));
function search_functions($request, $limit, $offset) {
$data = array('count' => 0, 'data' => false);
$sql = "SELECT * FROM {external_functions} WHERE name LIKE ?";
$values = array('%' . $request . '%');
if ($results = get_records_sql_array($sql, $values, $offset, $limit)) {
$data['count'] = sizeof(get_records_sql_array($sql, $values));
$data['data'] = (array)$results;
}
return $data;
}
\ No newline at end of file
......@@ -161,8 +161,8 @@ function webservices_token_submit(Pieform $form, $values) {
global $SESSION, $USER;
if ($values['action'] == 'generate') {
if (isset($values['userid'])) {
$dbuser = get_record('usr', 'id', $values['userid']);
if (isset($values['userid'][0])) {
$dbuser = get_record('usr', 'id', $values['userid'][0]);
if (!empty($dbuser)) {
$services = get_records_array('external_services', 'restrictedusers', 0);
if (empty($services)) {
......@@ -208,8 +208,8 @@ function webservices_user_submit(Pieform $form, $values) {
global $SESSION, $USER;
if ($values['action'] == 'add') {
if (isset($values['userid'])) {
$dbuser = get_record('usr', 'id', $values['userid']);
if (isset($values['userid'][0])) {
$dbuser = get_record('usr', 'id', $values['userid'][0]);
if ($auth_instance = webservice_validate_user($dbuser)) {
// make sure that this account is not already in use
$existing = get_record('external_services_users', 'userid', $dbuser->id);
......@@ -685,10 +685,13 @@ function service_tokens_edit_form() {
'type' => 'autocomplete',
'title' => get_string('username') . ': ',
'ajaxurl' => get_config('wwwroot') . 'webservice/admin/users.json.php',
'multiple' => false,
'multiple' => true,
'allowclear' => true,
'ajaxextraparams' => array(),
'width' => '400px',
'extraparams' => array(
'maximumSelectionSize' => 1
),
'width' => '280px',
),
'action' => array('type' => 'hidden', 'value' => 'generate'),
'submit' => array(
......@@ -874,10 +877,13 @@ function service_users_edit_form() {
'type' => 'autocomplete',
'title' => get_string('username') . ': ',
'ajaxurl' => get_config('wwwroot') . 'webservice/admin/users.json.php',
'multiple' => false,
'multiple' => true,
'allowclear' => true,
'ajaxextraparams' => array(),
'width' => '400px',
'extraparams' => array(
'maximumSelectionSize' => 1
),
'width' => '280px',
),
'action' => array('type' => 'hidden', 'value' => 'add'),
'submit' => array(
......
......@@ -25,22 +25,17 @@ if ($action == 'search') {
$params->protocol = param_alphanum('protocol', 'all');
$params->authtype = param_alphanum('authtype', 'all');
$params->institution_requested = param_alphanum('institution_requested', null);
$offset = param_integer('offset', 0);
$limit = param_integer('limit', 10);
$sortby = param_alpha('sortby', 'timelogged');
$sortdir = param_alpha('sortdir', 'desc');
$params->sortby = $sortby;
$params->sortdir = $sortdir;
$params->offset = $offset;
$params->limit = $limit;
$params->offset = param_integer('offset', 0);
$params->limit = param_integer('limit', 10);
$params->sortby = param_alpha('sortby', 'timelogged');
$params->sortdir = param_alpha('sortdir', 'desc');
json_headers();
if (param_boolean('raw', false)) {
$data = get_log_search_results($params, $offset, $limit, $sortby, $sortdir);
$data = get_log_search_results($params);
}
else {
$data['data'] = build_webservice_log_search_results($params, $offset, $limit, $sortby, $sortdir);
$data['data'] = build_webservice_log_search_results($params);
}
$data['error'] = false;
$data['message'] = null;
......
......@@ -18,22 +18,20 @@ require_once('webservicessearchlib.php');
define('TITLE', get_string('webservicelogs', 'auth.webservice'));
require_once('pieforms/pieform.php');
$sortby = param_alpha('sortby', 'timelogged');
$sortdir = param_alpha('sortdir', 'desc');
$offset = param_integer('offset', 0);
$limit = param_integer('limit', 10);
$userquery = param_variable('userquery', null);
$username = (!empty($userquery)) ? get_field('usr', 'username', 'id', $userquery) : '';
$functionquery = param_variable('functionquery', null);
$functionname = (!empty($functionquery)) ? get_field('external_functions', 'name', 'id', $functionquery) : '';
$search = (object) array(
'userquery' => trim(param_variable('userquery', '')),
'functionquery' => trim(param_variable('functionquery', '')),
'protocol' => trim(param_alphanum('protocol', 'all')),
'userquery' => $username,
'functionquery' => $functionname,
'protocol' => trim(param_alphanumext('protocol', 'all')),
'authtype' => trim(param_alphanum('authtype', 'all')),
'onlyerrors' => ('on' == param_alphanum('onlyerrors', 'off') ? 1 : 0),
'sortby' => $sortby,
'sortdir' => $sortdir,
'offset' => $offset,
'limit' => $limit,
'sortby' => param_alpha('sortby', 'timelogged'),
'sortdir' => param_alpha('sortdir', 'desc'),
'offset' => param_integer('offset', 0),
'limit' => param_integer('limit', 10),
);
if ($USER->get('admin')) {
......@@ -45,6 +43,95 @@ else {
$search->institution_requested = param_alphanum('institution_requested', 'all');
}
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(
'maximumSelectionSize' => 1
),
'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(
'maximumSelectionSize' => 1
),
'width' => '280px',
),
'submit' => array(
'type' => 'submit',
'value' => get_string('go'),
),
'onlyerrors' => array(
'type' => 'checkbox',
'title' => get_string('errors', 'auth.webservice'),
'defaultvalue' => $search->onlyerrors,
),
),
);
unset($form['elements'][0]);
$form = pieform($form);
$smarty = smarty(array(), array('<link rel="stylesheet" type="text/css" href="' . $THEME->get_url('style/webservice.css', false, 'auth/webservice') . '">',));
safe_require('auth', 'webservice');
......@@ -54,12 +141,71 @@ $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'));
list($html, $columns, $searchurl, $pagination) = build_webservice_log_search_results($search, $offset, $limit, $sortby, $sortdir);
$smarty->assign('results', $html);
$smarty->assign('pagination', $pagination['html']);
$smarty->assign('columns', $columns);
$smarty->assign('searchurl', $searchurl['url']);
$smarty->assign('sortby', $searchurl['sortby']);
$smarty->assign('sortdir', $searchurl['sortdir']);
$smarty->assign('form', $form);
$smarty->assign('PAGEHEADING', TITLE);
$smarty->display('auth:webservice:webservicelogs.tpl');
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;
}
......@@ -14,11 +14,29 @@ defined('INTERNAL') || die();
require_once('searchlib.php');
require_once('user.php');
function build_webservice_log_search_results($search, $offset, $limit, $sortby, $sortdir) {
/**
* Get results for log search with results containing markup/pagination
*
* @param object Contains:
* - userquery string
* - functionquery string
* - protocol string
* - authtype string
* - sortby string
* - sortdir string
* - offset int
* - limit int
* - onlyerrors string optional
* - institution string optional
* - institution_requested string optional
*
* @return array Contains search results markup/pagination
*/
function build_webservice_log_search_results($search) {
global $THEME;
$THEME->templatedirs[]= get_config('docroot') . 'auth/webservice/theme/raw/';
$results = get_log_search_results($search, $offset, $limit, $sortby, $sortdir);
$results = get_log_search_results($search);
$params = array();
foreach ($search as $k => $v) {
......@@ -27,18 +45,18 @@ function build_webservice_log_search_results($search, $offset, $limit, $sortby,
}
}
$searchurl = get_config('wwwroot') . 'webservice/admin/webservicelogs.php?' . join('&', $params) . '&limit=' . $limit;
$searchurl = get_config('wwwroot') . 'webservice/admin/webservicelogs.php?' . join('&', $params);
$pagination = $results['pagination'] = build_pagination(array(
'id' => 'admin_usersearch_pagination',
'class' => 'center',
'url' => $searchurl,
'count' => $results['count'],
'limit' => $limit,
'limit' => $search->limit,
'setlimit' => true,
'jumplinks' => 8,
'numbersincludeprevnext' => 2,
'offset' => $offset,
'offset' => $search->offset,
'datatable' => 'searchresults',
'jsonscript' => 'webservice/admin/logsearch.json.php',
));
......@@ -61,14 +79,14 @@ function build_webservice_log_search_results($search, $offset, $limit, $sortby,
$smarty->assign_by_ref('results', $results);
$smarty->assign_by_ref('institutions', $institutions);
$smarty->assign('searchurl', $searchurl);
$smarty->assign('sortby', $sortby);
$smarty->assign('sortdir', $sortdir);
$smarty->assign('sortby', $search->sortby);
$smarty->assign('sortdir', $search->sortdir);
$smarty->assign('limitoptions', array(10, 50, 100, 200, 500));
$smarty->assign('pagebaseurl', $searchurl . '&sortby=' . $sortby . '&sortdir=' . $sortdir);
$smarty->assign('pagebaseurl', $searchurl . '&sortby=' . $search->sortby . '&sortdir=' . $search->sortdir);
$smarty->assign('cols', $cols);
return array($smarty->fetch('searchresulttable.tpl'), $cols, array(
'url' => $searchurl . '&sortby=' . $sortby . '&sortdir=' . $sortdir,
'url' => $searchurl . '&sortby=' . $search->sortby . '&sortdir=' . $search->sortdir,
'sortby' => $search->sortby,
'sortdir' => $search->sortdir
), $pagination);
......@@ -113,7 +131,13 @@ function split_query_string($query) {
return $terms;
}
function get_log_search_results($search, $offset, $limit) {
/**
* Get raw results for webservices log search
*
* @param object $search - see build_webservice_log_search_results() for
* list of variables
*/
function get_log_search_results($search) {
$sort = 'TRUE';
if (preg_match('/^[a-zA-Z_0-9"]+$/', $search->sortby)) {
$sort = $search->sortby;
......@@ -185,12 +209,12 @@ function get_log_search_results($search, $offset, $limit) {
FROM {external_services_logs} el
JOIN {usr} u
ON el.userid = u.id
' . $where . ' ORDER BY ' . $sort, $params, $offset);
' . $where . ' ORDER BY ' . $search->sortby, $params, $search->offset);
$results = array(
'count' => $count,
'limit' => $limit,
'offset' => $offset,
'limit' => $search->limit,
'offset' => $search->offset,
'data' => array(),
);
if (!empty($data)) {
......
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