function search_log_report in Search Log 7
Same name and namespace in other branches
- 6 search_log.admin.inc \search_log_report()
Display search log report.
1 string reference to 'search_log_report'
- search_log_menu in ./
search_log.module - Implements hook_menu().
File
- ./
search_log.admin.inc, line 124 - Admin page callbacks file for the search_log module.
Code
function search_log_report($period = NULL) {
// Set defaults.
$today = getdate(_search_log_get_time());
$date['from'] = $date['to'] = date('Y-m-d', $today[0]);
$filter['modules'] = isset($_SESSION['search_log']['modules']) ? $_SESSION['search_log']['modules'] : array();
$filter['languages'] = isset($_SESSION['search_log']['languages']) ? $_SESSION['search_log']['languages'] : array();
$filter['result'] = isset($_SESSION['search_log']['result']) ? $_SESSION['search_log']['result'] : SEARCH_LOG_RESULT_UNKNOWN;
$rows = array();
// Handle $period arg and set $_SESSION.
switch ($period) {
// Today begins at 00:00.
case 'today':
$_SESSION['search_log']['from'] = $_SESSION['search_log']['to'] = $date['from'];
break;
// Week begins on Sunday.
case 'week':
$date['from'] = $_SESSION['search_log']['from'] = date('Y-m-d', $today[0] - $today['wday'] * SEARCH_LOG_DAY);
$date['to'] = $_SESSION['search_log']['to'] = date('Y-m-d', $today[0] + (6 - $today['wday']) * SEARCH_LOG_DAY);
break;
case 'month':
$date['from'] = $_SESSION['search_log']['from'] = date('Y-m-d', mktime(0, 0, 0, $today['mon'], 1, $today['year']));
$date['to'] = $_SESSION['search_log']['to'] = date('Y-m-d', mktime(0, 0, 0, $today['mon'] + 1, 1, $today['year']) - SEARCH_LOG_DAY);
break;
case 'year':
$date['from'] = $_SESSION['search_log']['from'] = date('Y-m-d', mktime(0, 0, 0, 1, 1, $today['year']));
$date['to'] = $_SESSION['search_log']['to'] = date('Y-m-d', mktime(0, 0, 0, 12, 31, $today['year']));
break;
default:
$date['from'] = isset($_SESSION['search_log']['from']) ? $_SESSION['search_log']['from'] : $date['from'];
$date['to'] = isset($_SESSION['search_log']['to']) ? $_SESSION['search_log']['to'] : $date['from'];
}
// Convert all human-readable dates back to time.
$time['from'] = strtotime($date['from']);
$time['to'] = strtotime($date['to']);
if ($time['from'] && $time['to']) {
// Result filter is enabled if there are any failed searches.
$enabled['result'] = db_query_range('SELECT qid FROM {search_log} WHERE result = :result', 0, 1, array(
':result' => SEARCH_LOG_RESULT_FAILED,
))
->fetchField();
// Build header.
$header = array(
array(
'data' => t('Search term'),
'field' => 'q',
),
array(
'data' => t('Module'),
'field' => 'module',
),
array(
'data' => t('Language'),
'field' => 'language',
),
);
if ($enabled['result']) {
array_push($header, array(
'data' => t('Result'),
'field' => 'result',
));
}
array_push($header, array(
'data' => t('Total'),
'field' => 'total',
'sort' => 'desc',
));
// Build query.
$query = db_select('search_log')
->fields('search_log', array(
'q',
'module',
'language',
'result',
))
->condition('day', $time['from'], '>=')
->condition('day', $time['to'], '<=')
->groupBy('q, module')
->extend('PagerDefault')
->limit(SEARCH_LOG_ADMIN_ROWS)
->extend('TableSort')
->orderByHeader($header);
$query
->addExpression('SUM(counter)', 'total');
_search_log_get_query_filter($query, $time, $filter);
// Build results.
$result = $query
->execute();
while ($data = $result
->fetchObject()) {
$row = array();
$row[] = l(truncate_utf8($data->q, SEARCH_LOG_ADMIN_TERM_LENGTH, TRUE, TRUE), "search/{$data->module}/{$data->q}");
$row[] = $data->module;
$row[] = $data->language;
if ($enabled['result']) {
$row[] = $data->result == SEARCH_LOG_RESULT_FAILED ? '<span class="error">' . t('Failed') . '</span>' : '';
}
$row[] = $data->total;
$rows[] = $row;
}
if ($rows) {
// Calculate total searches.
$query = db_select('search_log')
->condition('day', $time['from'], '>=')
->condition('day', $time['to'], '<=');
$query
->addExpression('SUM(counter)', 'total');
_search_log_get_query_filter($query, $time, $filter);
$total = $query
->execute()
->fetchField();
// Calculate unique terms.
$query = db_select('search_log')
->condition('day', $time['from'], '>=')
->condition('day', $time['to'], '<=');
$query
->addExpression('COUNT(DISTINCT q, module)', 'total');
_search_log_get_query_filter($query, $time, $filter);
$unique = $query
->execute()
->fetchField();
// Calculate failed searches.
switch ($filter['result']) {
case SEARCH_LOG_RESULT_UNKNOWN:
$query = db_select('search_log')
->condition('day', $time['from'], '>=')
->condition('day', $time['to'], '<=')
->condition('result', SEARCH_LOG_RESULT_FAILED, '=');
$query
->addExpression('SUM(counter)', 'total');
_search_log_get_query_filter($query, $time, $filter, FALSE);
$failed = (int) $query
->execute()
->fetchField();
break;
case SEARCH_LOG_RESULT_SUCCESS:
$failed = 0;
break;
case SEARCH_LOG_RESULT_FAILED:
$failed = $total;
break;
}
return theme('search_log_report', array(
'summary' => module_exists('chart') ? _search_log_summary_chart($total, $unique, $failed, $time, $filter) : theme('search_log_summary', array(
'total' => $total,
'unique' => $unique,
'failed' => $failed,
)),
'filters' => drupal_get_form('search_log_report_form', $date, $filter),
'table' => theme('table', array(
'header' => $header,
'rows' => $rows,
'attributes' => array(
'id' => 'search-log',
),
)),
'pager' => theme('pager', array(
'tags' => array(),
)),
));
}
}
return theme('search_log_report', array(
'summary' => t('No searches found for period.'),
'filters' => drupal_get_form('search_log_report_form', $date, $filter),
'table' => NULL,
'pager' => NULL,
));
}