You are here

function ad_admin_statistics in Advertisement 5

Same name and namespace in other branches
  1. 6 ad.admin.inc \ad_admin_statistics()
1 string reference to 'ad_admin_statistics'
ad_menu in ./ad.module
Implementation of hook_menu().

File

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

Code

function ad_admin_statistics() {
  $groups = ad_groups_list(TRUE);
  foreach ($groups as $tid => $group) {
    if ($tid) {
      $ads = db_result(db_query("SELECT count(aid) FROM {ads} a JOIN {term_node} t ON a.aid = t.nid WHERE t.tid = %d AND adstatus = 'active'", $tid));
      $filter = "= {$tid}";
    }
    else {
      $ads = db_result(db_query("SELECT count(aid) FROM {ads} a LEFT JOIN {term_node} t ON a.aid = t.nid WHERE t.tid IS NULL AND adstatus = 'active'"));
      $filter = "IS NULL";
    }
    if (!$ads) {
      continue;
    }
    $form[$group->name] = array(
      '#type' => 'fieldset',
      '#title' => $group->name,
      '#collapsible' => TRUE,
    );

    // Get overall global statistics.
    $statistics['global']['views'] = (int) db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'view' AND n.tid {$filter}"));
    $statistics['global']['clicks'] = (int) db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'click' AND n.tid {$filter}"));

    // Get overall 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(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'view' AND date >= %d AND date <= %d AND n.tid {$filter}", $last_year, $this_year));
    $statistics['last_year']['clicks'] = (int) db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'click' AND date >= %d AND date <= %d AND n.tid {$filter}", $last_year, $this_year));
    $statistics['this_year']['views'] = (int) db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'view' AND date >= %d AND n.tid {$filter}", $this_year));
    $statistics['this_year']['clicks'] = (int) db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'click' AND date >= %d AND n.tid {$filter}", $this_year));

    // 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(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'view' AND date >= %d AND date <= %d AND n.tid {$filter}", $last_month, $this_month));
    $statistics['last_month']['clicks'] = (int) db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'click' AND date >= %d AND date <= %d AND n.tid {$filter}", $last_month, $this_month));
    $statistics['this_month']['views'] = (int) db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'view' AND date >= %d AND n.tid {$filter}", $this_month));
    $statistics['this_month']['clicks'] = (int) db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'click' AND date >= %d AND n.tid {$filter}", $this_month));

    // 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(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'view' AND date >= %d AND n.tid {$filter}", $this_week_start));
    $statistics['this_week']['clicks'] = (int) db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'click' AND date >= %d AND n.tid {$filter}", $this_week_start));

    // 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(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'view' AND date >= %d AND date <= %d AND n.tid {$filter}", $yesterday_start, $yesterday_end));
    $statistics['yesterday']['clicks'] = (int) db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'click' AND date >= %d AND date <= %d AND n.tid {$filter}", $yesterday_start, $yesterday_end));
    $statistics['today']['views'] = (int) db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'view' AND date >= %d AND n.tid {$filter}", $today_start));
    $statistics['today']['clicks'] = (int) db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'click' AND date >= %d AND n.tid {$filter}", $today_start));

    // 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(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'view' AND date = %d AND n.tid {$filter}", $last_hour));
    $statistics['last_hour']['clicks'] = (int) db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'click' AND date = %d AND n.tid {$filter}", $last_hour));
    $statistics['this_hour']['views'] = (int) db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'view' AND date = %d AND n.tid {$filter}", $this_hour));
    $statistics['this_hour']['clicks'] = (int) db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'click' AND date = %d AND n.tid {$filter}", $this_hour));

    // TODO: Create this view and remove the && FALSE to enable this code.
    if (module_exists('views') && FALSE) {
      $form[$group->name]['statistics'] = array(
        '#type' => 'markup',
        '#value' => '<p>' . t('There %count in this group.', array(
          '%count' => format_plural($ads, 'is ' . l('1 active ad', "ad/{$group->gid}/group"), 'are ' . l('%count active ads', "ad/{$group->tid}/group")),
        )) . '</p>' . theme('ad_statistics_display', $statistics),
      );
    }
    else {
      $form[$group->name]['statistics'] = array(
        '#type' => 'markup',
        '#value' => '<p>' . t('There @count in this group.', array(
          '@count' => format_plural($ads, 'is 1 active ad', 'are @count active ads'),
        )) . '</p>' . theme('ad_statistics_display', $statistics),
      );
    }
  }
  if (count($form) == 0) {
    $form['header'] = array(
      '#type' => 'markup',
      '#value' => '<p>' . t('There are no active ads.') . '</p>',
    );
  }
  return $form;
}