You are here

function _search_log_summary_chart in Search Log 7

Internal function for summary chart.

1 call to _search_log_summary_chart()
search_log_report in ./search_log.admin.inc
Display search log report.

File

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

Code

function _search_log_summary_chart($total = 0, $unique = 0, $failed = 0, $time = array(), $filter = array()) {
  $output = '';

  // Build daily searches.
  if ($time['from'] && $time['to']) {
    $chart = array(
      '#chart_id' => 'daily',
      '#title' => t('Total searches (@total)', array(
        '@total' => number_format($total),
      )),
      '#type' => CHART_TYPE_LINE,
      '#adjust_resolution' => TRUE,
      '#grid_lines' => chart_grid_lines(20, 20),
      '#size' => chart_size(280, 130),
    );

    // Line graph cannot be a single point.
    if ($time['from'] == $time['to']) {
      $time['to'] += SEARCH_LOG_DAY;
    }

    // Calculate maximum days per label.
    $days = ($time['to'] - $time['from']) / SEARCH_LOG_DAY;
    if ($days < 8) {
      $days_per_label = 1;
    }
    elseif ($days < 15) {
      $days_per_label = 2;
    }
    elseif ($days < 32) {
      $days_per_label = 7;
    }
    elseif ($days < 93) {
      $days_per_label = 30;
    }
    else {
      $days_per_label = 90;
    }

    // Add empty data set for days in range.
    $days = 1;
    for ($day = $time['from']; $day <= $time['to']; $day += SEARCH_LOG_DAY) {
      $chart['#data'][$day] = 0;
      if (--$days == 0) {
        $chart['#labels'][$day] = date('j M', $day);
        $days = $days_per_label;
      }
    }

    // Build query.
    $query = db_select('search_log')
      ->fields('search_log', array(
      'day',
    ))
      ->condition('day', $time['from'], '>=')
      ->condition('day', $time['to'], '<=')
      ->groupBy('day');
    $query
      ->addExpression('SUM(counter)', 'total');
    _search_log_get_query_filter($query, $time, $filter);

    // Build results.
    $label_max = 0;
    $result = $query
      ->execute();
    while ($data = $result
      ->fetchObject()) {
      $chart['#data'][$data->day] = $data->total;
      if ($data->total > $label_max) {
        $label_max = $data->total;
      }
    }
    ksort($chart['#data']);
    $chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][0][] = chart_mixed_axis_range_label(0, $label_max);
    $output .= theme('chart', array(
      'chart' => $chart,
    ));
  }

  // Build Unique terms.
  $chart = array(
    '#chart_id' => 'unique',
    '#type' => CHART_TYPE_PIE_3D,
    '#size' => chart_size(200, 130),
  );
  $chart['#title'] = t('Unique terms') . ' (' . number_format($unique) . ')';
  $chart['#data']['unique'] = $unique;
  $chart['#data']['remainder'] = $total - $unique;
  $output .= theme('chart', array(
    'chart' => $chart,
  ));

  // Build Failed searches.
  $chart = array(
    '#chart_id' => 'failed',
    '#type' => CHART_TYPE_PIE_3D,
    '#size' => chart_size(200, 130),
  );
  $chart['#title'] = t('Failed searches') . ' (' . number_format($failed) . ')';
  $chart['#data']['failed'] = $failed;
  $chart['#data']['remainder'] = $total - $failed;
  $chart['#data_colors'][] = 'ff6666';
  $output .= theme('chart', array(
    'chart' => $chart,
  ));
  return $output;
}