You are here

function adserve_cache_increment in Advertisement 6.3

Same name and namespace in other branches
  1. 5.2 adcache.inc \adserve_cache_increment()
  2. 6.2 adcache.inc \adserve_cache_increment()
  3. 7 adcache.inc \adserve_cache_increment()

Increment action directly in the database.

File

./adcache.inc, line 294

Code

function adserve_cache_increment($action, $aid) {
  $hostid = adserve_variable('hostid');
  _debug_echo("adserve_cache_increment action({$action}) aid({$aid}) hostid({$hostid})");

  // be sure that drupal is bootstrapped
  adserve_bootstrap();

  // allow add-on modules to implement their own statistics granularity
  $extra = adserve_invoke_hook('increment_extra', 'merge', $action, $aid);
  if (is_array($extra)) {
    $extra = implode('|,|', $extra);
  }
  adserve_variable('extra', $extra);
  _debug_echo("adserve_cache_increment extra({$extra})");

  // update statistics
  db_query("UPDATE {ad_statistics} SET count = count + 1 WHERE aid = %d AND action = '%s' AND date = %d AND adgroup = '%s' AND extra = '%s' AND hostid = '%s'", $aid, $action, date('YmdH'), adserve_variable('group'), $extra, $hostid);

  // if column doesn't already exist, add it
  if (!db_affected_rows()) {
    db_query("INSERT INTO {ad_statistics} (aid, date, action, adgroup, extra, hostid, count) VALUES(%d, %d, '%s', '%s', '%s', '%s', 1)", $aid, date('YmdH'), $action, adserve_variable('group'), $extra, $hostid);
    if (!db_affected_rows()) {

      // we lost a race to add it, increment it
      db_query("UPDATE {ad_statistics} SET count = count + 1 WHERE aid = %d AND action = '%s' AND date = %d AND adgroup = '%s' AND extra = '%s' AND hostid = '%s'", $aid, $action, date('YmdH'), adserve_variable('group'), $extra, $hostid);
    }
  }
  if ($action == 'view') {
    $ad = db_fetch_object(db_query('SELECT maxviews, activated FROM {ads} WHERE aid = %d', $aid));

    // See if we need to perform additional queries.
    if ($ad->maxviews) {
      $views = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d", $aid, date('YmdH', $ad->activated)));
      if ($views >= $ad->maxviews) {
        db_query("UPDATE {ads} SET adstatus = 'expired', autoexpire = 0, autoexpired = %d, expired = %d WHERE aid = %d", time(), time(), $aid);
        ad_statistics_increment($aid, 'autoexpired');
        ad_statistics_increment($aid, 'expired');
      }
    }
  }
}