You are here

function search_log_report in Search Log 6

Same name and namespace in other branches
  1. 7 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
Implementation of hook_menu().

File

./search_log.admin.inc, line 115
Admin page callbacks file for the search_log module.

Code

function search_log_report($period = NULL) {

  // Get from and to parameters.
  $today = _search_log_get_time();
  $date = getdate($today);
  switch ($period) {

    // Today begins at 00:00
    case 'today':
      $from_date = $to_date = date('Y-m-d', $today);
      break;

    // Week begins on Sunday
    case 'week':
      $from_date = date('Y-m-d', $today - $date['wday'] * 86400);
      $to_date = date('Y-m-d', $today + (6 - $date['wday']) * 86400);
      break;
    case 'month':
      $from_date = date('Y-m-d', mktime(0, 0, 0, $date['mon'], 1, $date['year']));
      $to_date = date('Y-m-d', mktime(0, 0, 0, $date['mon'] + 1, 1, $date['year']) - 86400);
      break;
    case 'year':
      $from_date = date('Y-m-d', mktime(0, 0, 0, 1, 1, $date['year']));
      $to_date = date('Y-m-d', mktime(0, 0, 0, 12, 31, $date['year']));
      break;
    default:
      $from_date = $_SESSION['search_log']['from'];
      $to_date = $_SESSION['search_log']['to'];
      break;
  }
  $from_time = strtotime($from_date);
  $to_time = strtotime($to_date);
  if ($from_time && $to_time) {

    // Setup session vars.
    $modules = $_SESSION['search_log']['modules'];
    $status = $_SESSION['search_log']['status'];
    $rows_n = $_SESSION['search_log']['rows'] ? $_SESSION['search_log']['rows'] : 50;

    // Build header.
    $header = array(
      array(
        'data' => t('Search term'),
        'field' => 'q',
      ),
      array(
        'data' => t('Module'),
        'field' => 'module',
      ),
    );
    if ($failed_enabled = db_result(db_query_range('SELECT qid FROM {search_log} WHERE result < 0', 0, 1))) {
      array_push($header, array(
        'data' => t('Result'),
        'field' => 'result',
      ));
    }
    array_push($header, array(
      'data' => t('Total'),
      'field' => 'total',
      'sort' => 'desc',
    ));

    // Build query.
    $query_where_status = '';
    if ($status == SEARCH_LOG_STATUS_SUCCESS) {
      $query_where_status = ' AND result >= 0';
    }
    elseif ($status == SEARCH_LOG_STATUS_FAILED) {
      $query_where_status = ' AND result < 0';
    }
    $query_where_modules = count($modules) ? ' AND module IN ("' . implode('","', $modules) . '")' : '';
    $query = 'SELECT q, module, SUM(counter) as total, result FROM {search_log} WHERE day >= %d AND day <= %d' . $query_where_modules . $query_where_status . ' GROUP BY q, module';
    $query_count = 'SELECT COUNT(DISTINCT q, module) FROM {search_log} WHERE day >= %d AND day <= %d' . $query_where_modules . $query_where_status;
    $result = pager_query($query . tablesort_sql($header), $rows_n, 0, $query_count, $from_time, $to_time);

    // Build results.
    $rows = array();
    while ($data = db_fetch_object($result)) {
      $row = array();
      $row[] = l($data->q, "search/{$data->module}/{$data->q}");
      $row[] = $data->module;
      if ($failed_enabled) {
        $row[] = $data->result < 0 ? '<span class="error">' . t('Failed') . '</span>' : '';
      }
      $row[] = $data->total;
      $rows[] = $row;
    }
    if ($rows) {
      $total = db_result(db_query('SELECT SUM(counter) FROM {search_log} WHERE day >= %d AND day <= %d' . $query_where_modules . $query_where_status, $from_time, $to_time));
      $unique = db_result(db_query($query_count, $from_time, $to_time));
      switch ($status) {
        case SEARCH_LOG_STATUS_ALL:
          $failed = (int) db_result(db_query('SELECT SUM(counter) FROM {search_log} WHERE result < 0 AND day >= %d AND day <= %d' . $query_where_modules, $from_time, $to_time));
          break;
        case SEARCH_LOG_STATUS_SUCCESS:
          $failed = 0;
          break;
        case SEARCH_LOG_STATUS_FAILED:
          $failed = $total;
          break;
      }
      return theme('search_log_report', theme('table', $header, $rows, array(
        'id' => 'search-log',
      )), theme('pager', array(), $rows_n, 0), theme('search_log_summary', $total, $unique, $failed), drupal_get_form('search_log_report_form', $from_date, $to_date, $modules, $status, $rows_n));
    }
  }
  return theme('search_log_report', NULL, NULL, t('No searches found for period.'), drupal_get_form('search_log_report_form', $from_date, $to_date, $modules, $status, $rows_n));
}