You are here

function ad_statistics in Advertisement 5.2

Same name and namespace in other branches
  1. 5 ad.module \ad_statistics()
  2. 6.3 ad.pages.inc \ad_statistics()
  3. 6 ad.pages.inc \ad_statistics()
  4. 6.2 ad.pages.inc \ad_statistics()
  5. 7 ad.pages.inc \ad_statistics()

Calculate statistics for the given advertisements. TODO: Introduce caching to make this more efficient.

3 calls to ad_statistics()
ad_form in ./ad.module
Drupal _form hook.
ad_notify_send_mail in notify/ad_notify.module
Send email notifications using PHP mail() function.
theme_node_ad in ./ad.module
3 string references to 'ad_statistics'
ad_update_10 in ./ad.install
Introduce "adgroup" for ad statistics and clicks, providing per-group granularity.
ad_update_5 in ./ad.install
Convert to utf8 character set for all tables to allow for proper internationalization.
ad_update_5112 in ./ad.install
Introduce "extra" for ad statistics and clicks, optionally allowing add-on modules to provide additional granularity.

File

./ad.module, line 352
An advertising system for Drupal powered websites.

Code

function ad_statistics($aid) {

  // Get global statistics.
  $statistics['global']['views'] = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view'", $aid));
  $statistics['global']['clicks'] = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click'", $aid));

  // No sense in making further queries if the ad has no global statistics.
  if (!$statistics['global']['views'] && !$statistics['global']['clicks']) {
    return $statistics;
  }

  // Get statistics for this year and last year.
  $this_year = date('Y000000');
  $last_year = date('Y') - 1 . '000000';
  $statistics['last_year']['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, $last_year, $this_year));
  $statistics['last_year']['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, $last_year, $this_year));
  $statistics['this_year']['views'] = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d", $aid, $this_year));
  $statistics['this_year']['clicks'] = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click' AND date >= %d", $aid, $this_year));

  // No sense in making further queries if the ad has no statistics this year.
  if (!$statistics['this_year']['views'] && !$statistics['this_year']['clicks']) {
    return $statistics;
  }

  // Get statistics for this month and last month.
  $this_month = date('Ym0000');
  $last_month = date('m') - 1;
  if ($last_month == 0) {
    $last_month = date('Y') - 1 . '120000';
  }
  else {
    $last_month = date('Y') . ($last_month < 10 ? '0' : '') . $last_month . '0000';
  }
  $statistics['last_month']['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, $last_month, $this_month));
  $statistics['last_month']['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, $last_month, $this_month));
  $statistics['this_month']['views'] = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d", $aid, $this_month));
  $statistics['this_month']['clicks'] = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click' AND date >= %d", $aid, $this_month));

  // No sense in making further queries if the ad has no statistics this month.
  if (!$statistics['this_month']['views'] && !$statistics['this_month']['clicks']) {
    return $statistics;
  }

  // Get statistics for this week.
  $this_week_start = date('Ymd00', time() - 60 * 60 * 24 * 6);
  $statistics['this_week']['views'] = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date > %d", $aid, $this_week_start));
  $statistics['this_week']['clicks'] = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click' AND date > %d", $aid, $this_week_start));

  // No sense in making further queries if the ad has no statistics this week.
  if (!$statistics['this_week']['views'] && !$statistics['this_week']['clicks']) {
    return $statistics;
  }

  // Get statistics for yesterday and today.
  $yesterday_start = date('Ymd00', time() - 60 * 60 * 24);
  $yesterday_end = date('Ymd24', time() - 60 * 60 * 24);
  $today_start = date('Ymd00', time());
  $statistics['yesterday']['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, $yesterday_start, $yesterday_end));
  $statistics['yesterday']['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, $yesterday_start, $yesterday_end));
  $statistics['today']['views'] = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d", $aid, $today_start));
  $statistics['today']['clicks'] = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click' AND date >= %d", $aid, $today_start));

  // No sense in making further queries if the ad has no statistics today.
  if (!$statistics['today']['views'] && !$statistics['today']['clicks']) {
    return $statistics;
  }

  // Get statistics for this hour and the last hour.
  $last_hour = date('YmdH', time() - 60 * 60);
  $this_hour = date('YmdH', time());
  $statistics['last_hour']['views'] = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date = %d", $aid, $last_hour));
  $statistics['last_hour']['clicks'] = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click' AND date = %d", $aid, $last_hour));
  $statistics['this_hour']['views'] = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date = %d", $aid, $this_hour));
  $statistics['this_hour']['clicks'] = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click' AND date = %d", $aid, $this_hour));
  return $statistics;
}