You are here

function ad_channel_enforce_inventory_level in Advertisement 6.2

Same name and namespace in other branches
  1. 6.3 channel/ad_channel.inc \ad_channel_enforce_inventory_level()
  2. 7 channel/ad_channel.inc \ad_channel_enforce_inventory_level()
1 call to ad_channel_enforce_inventory_level()
ad_channel_cache_filter in channel/ad_channel.inc
Filter advertisements not in an appropriate channel, from cache.

File

channel/ad_channel.inc, line 216
Ad Channel include file.

Code

function ad_channel_enforce_inventory_level($chid, $ads) {
  if ($chid > 0) {
    $channels = adserve_cache('get_cache', 'channel');
    $channel = $channels['channels'][$chid];
    $level = $channel->inventory;
    $num_ads = count($ads);
    if ($num_ads < $level) {
      _debug_echo("ad_channel_enforce_inventory_level: channel({$chid}) has {$num_ads} and needs {$level}");
      $remnants = array_values(adserve_cache('get_cache', 'remnant'));
      $available = count($remnants);
      if ($available > 0) {
        _debug_echo("ad_channel_enforce_inventory_level: randomly selecting from {$available} remnants.");
        while (count($ads) < $level) {
          shuffle($remnants);
          $selected = array_pop($remnants);
          _debug_echo("ad_channel_enforce_inventory_level: selected {$selected}.");
          $ads[] = $selected;
        }
      }
      else {
        _debug_echo("ad_channel_enforce_inventory_level: no remnants to choose from.");
      }
    }
    else {
      _debug_echo("ad_channel_enforce_inventory_level: channel({$chid}) no inventory level assigned");
    }
  }
  else {
    _debug_echo("ad_channel_enforce_inventory_level: not needed for channel({$chid})");
  }
  return $ads;
}