You are here

function ad_report_admin_ad_table in Advertisement 5.2

Same name and namespace in other branches
  1. 6.3 report/ad_report.module \ad_report_admin_ad_table()
  2. 6.2 report/ad_report.module \ad_report_admin_ad_table()
  3. 7 report/ad_report.module \ad_report_admin_ad_table()
1 call to ad_report_admin_ad_table()
ad_report_admin_display in report/ad_report.module
Display the administrative report.
1 string reference to 'ad_report_admin_ad_table'
ad_report_menu in report/ad_report.module
Implementation of hook_menu().

File

report/ad_report.module, line 238
Provides comprehensive charts and reports about advertising statistics.

Code

function ad_report_admin_ad_table($start, $end, $group, $csv = FALSE) {

  // prepare dates
  $start = _ad_report_format_date_db($start);
  $end = _ad_report_format_date_db($end);

  // prepare groups
  $groups = ad_groups_list();
  $all = FALSE;
  $none = FALSE;
  if (is_array($group)) {
    if (in_array('all', $group)) {
      $all = TRUE;
    }
    if (!$all) {
      if (sizeof($group) == sizeof($groups)) {
        $all = TRUE;
      }
    }
    if (in_array('0', $group)) {
      unset($group[0]);
      $none = TRUE;
    }
  }
  if ($all) {
    $result = db_query('SELECT DISTINCT(aid) FROM {ad_statistics} WHERE date >= %d AND date <= %d', $start, $end);
  }
  else {
    if ($none) {
      if (sizeof($group)) {
        $result = db_query("SELECT DISTINCT(a.aid) FROM {ad_statistics} a LEFT JOIN {term_node} t ON a.aid = t.tid WHERE (t.tid IN (%s) OR ISNULL(t.tid)) AND action = 'view' AND a.date >= %d AND a.date <= %d", implode(',', $group), $start, $end);
      }
      else {
        $result = db_query("SELECT DISTINCT(a.aid) FROM {ad_statistics} a LEFT JOIN {term_node} t ON a.aid = t.tid WHERE ISNULL(t.tid) AND action = 'view' AND a.date >= %d AND a.date <= %d", $start, $end);
      }
    }
    else {
      $result = db_query("SELECT DISTINCT(a.aid) FROM {ad_statistics} a LEFT JOIN {term_node} t ON a.aid = t.tid WHERE t.tid IN (%s) AND action = 'view' AND a.date >= %d AND a.date <= %d", implode(',', $group), $start, $end);
    }
  }
  $ads = array();
  while ($ad = db_fetch_object($result)) {
    if ($ad->aid) {
      $ads[$ad->aid] = $ad->aid;
    }
  }
  if ($csv) {
    header('Content-type: application/octet-stream');
    header("Content-Disposition: attachment; filename=report-{$start}-{$end}.csv");
    echo "ad id, title, first view, last view, clicks, views, click-thru\n";
  }
  else {
    $output = '<div class="describe">' . t('There !count matching your parameters.', array(
      '!count' => format_plural(sizeof($ads), 'was 1 active ad', 'were @count active ads'),
    )) . '</div>';
    $headers = array(
      t('Advertisement'),
      t('Active dates'),
      t('Views'),
      t('Clicks'),
      t('Click-thru'),
    );

    // get counts for each ad
    $rows = array();
  }
  $total_views = $total_clicks = 0;
  foreach ($ads as $aid) {
    $ad = node_load($aid);
    if ($ad->aid) {
      $views = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d AND date <= %d", $aid, $start, $end));
      $first = _ad_report_get_date_from_path((int) db_result(db_query("SELECT MIN(date) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d AND date <= %d", $aid, $start, $end)));
      $first = format_date($first, 'small');
      $last = _ad_report_get_date_from_path((int) db_result(db_query("SELECT MAX(date) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d AND date <= %d", $aid, $start, $end)));
      $last = format_date($last, 'small');
      $clicks = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click' AND date >= %d AND date <= %d", $aid, $start, $end));
      if ($views) {
        $clickthru = number_format($clicks / $views, 2) . '%';
      }
      else {
        $clickthru = '0%';
      }
      if ($views || $clicks) {
        if ($csv) {
          echo "{$ad->nid}, {$ad->title}, {$first}, {$last}, {$views}, {$clicks}, {$clickthru}\n";
        }
        else {
          $row = array();
          $row[] = l($ad->title, "node/{$ad->nid}");
          $row[] = "first view: {$first}<br />last view: {$last}";
          $row[] = number_format($views);
          $row[] = number_format($clicks);
          $row[] = $clickthru;
          $rows[] = $row;
          $total_views += $views;
          $total_clicks += $clicks;
        }
      }
    }
  }
  if ($csv) {
    return 0;
  }
  if ($total_views || $total_clicks) {
    $row = array();
    $row[] = '<strong>' . t('Total') . '</strong>';
    $row[] = '';
    $row[] = '<strong>' . number_format($total_views) . '</strong>';
    $row[] = '<strong>' . number_format($total_clicks) . '</strong>';
    if ($total_views) {
      $row[] = '<strong>' . number_format($total_clicks / $total_views, 2) . '%' . '</strong>';
    }
    else {
      $row[] = '<strong>' . '0%' . '</strong>';
    }
    $rows[] = $row;
  }
  $output = theme('table', $headers, $rows);
  $output .= l(t('Download CSV'), 'admin/content/ad/report/csv');
  return $output;
}