You are here

function ad_report_admin_ad_table in Advertisement 6.3

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

Generate a table reporting on the selected advertisements. Returns an array of ad NIDs in $ads.

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 302
Provides comprehensive charts and reports about advertising statistics.

Code

function ad_report_admin_ad_table($start = 0, $end = 0, $group = array(), $csv = FALSE, &$ads) {
  if (!$start) {
    $start = isset($_SESSION['ad_report_start']) ? strtotime($_SESSION['ad_report_start']) : 0;
  }
  if (!$end) {
    $end = isset($_SESSION['ad_report_end']) ? strtotime($_SESSION['ad_report_end']) : 0;
  }
  if (!$csv) {
    drupal_set_message(t('Report dates: !start to !end', array(
      '!start' => isset($_SESSION['ad_report_start']) ? format_date($start, 'small') : 'now',
      '!end' => isset($_SESSION['ad_report_end']) ? format_date($end, 'small') : 'now',
    )));
  }
  if (empty($group)) {
    $group = isset($_SESSION['ad_report_group']) ? $_SESSION['ad_report_group'] : array();
  }

  // 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 (!$csv) {
    $list = array();
    if ($all) {
      $list[] = t('all');
    }
    else {
      if ($none) {
        $list[] = t('none');
      }
      else {
        foreach ($group as $gid) {
          if (isset($groups[$gid])) {
            $list[] = $groups[$gid];
          }
        }
      }
    }
    drupal_set_message(t('Ad groups: !groups', array(
      '!groups' => implode(', ', $list),
    )));
  }
  $select = 'SELECT DISTINCT(aid) as nid FROM {ad_statistics} a';
  if ($all) {
    $where = array(
      "a.action = 'view'",
      'a.date >= %d',
      'a.date <= %d',
      'a.aid > 0',
    );
    $join = array();
    $args = array(
      $start,
      $end,
    );
  }
  else {
    if ($none) {
      if (sizeof($group)) {
        $where = array(
          '(t.tid IN (%s) OR ISNULL(t.tid))',
          "a.action = 'view'",
          'a.date >= %d',
          'a.date <= %d',
        );
        $join = array(
          'LEFT JOIN {term_node} t ON a.aid = t.nid',
        );
        $args = array(
          implode(',', $group),
          $start,
          $end,
        );
      }
      else {
        $where = array(
          'ISNULL(t.tid)',
          "a.action = 'view'",
          'a.date >= %d',
          'a.date <= %d',
        );
        $join = array(
          'LEFT JOIN {term_node} t ON a.aid = t.nid',
        );
        $args = array(
          $start,
          $end,
        );
      }
    }
    else {
      $where = array(
        't.tid IN (%s)',
        "a.action = 'view'",
        'a.date >= %d',
        'a.date <= %d',
      );
      $join = array(
        'LEFT JOIN {term_node} t ON a.aid = t.nid',
      );
      $args = array(
        implode(',', $group),
        $start,
        $end,
      );
    }
  }
  $return = module_invoke_all('adreport', $join, $where, $args, $select);
  foreach ($return as $type => $value) {
    switch ($type) {
      case 'join':
        if (is_array($value)) {
          foreach ($value as $option) {
            $join[] = $option;
          }
        }
        break;
      case 'where':
        if (is_array($value)) {
          foreach ($value as $option) {
            $where[] = $option;
          }
        }
        break;
      case 'args':
        if (is_array($value)) {
          foreach ($value as $option) {
            $args[] = $option;
          }
        }
        break;
    }
  }

  // Build the query.
  $query = $select . ' ' . implode(' ', $join) . ' WHERE ' . implode(' AND ', $where);
  $result = db_query($query, $args);
  $ads = array();
  while ($ad = db_fetch_object($result)) {
    if ($ad->nid) {
      $ads[$ad->nid] = $ad->nid;
    }
  }
  if ($csv) {
    header('Content-type: application/octet-stream');
    header("Content-Disposition: attachment; filename=report-{$start}-{$end}.csv");
    echo "ad id, title, first impression, latest impression, impressions, clicks, click-thru\n";
  }
  else {
    $output = '<div class="describe">' . t('There !count found.', array(
      '!count' => format_plural(sizeof($ads), 'was <strong>1</strong> ad', 'were <strong>@count</strong> ads'),
    )) . '</div>';
    $headers = array(
      t('Advertisement'),
      t('Active dates'),
      t('Impressions'),
      t('Clicks'),
      t('Click-thru'),
    );

    // get counts for each ad
    $rows = array();
  }
  $total_views = $total_clicks = 0;
  foreach ($ads as $nid) {
    $ad = node_load($nid);
    if ($ad->nid) {
      $views = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d AND date <= %d", $nid, $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", $nid, $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", $nid, $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", $nid, $start, $end));
      if ($views) {
        $clickthru = number_format(100 * $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 impression: {$first}<br />latest impression: {$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(100 * $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;
}