You are here

function adserve_hook_select in Advertisement 5.2

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

Randomly select advertisements.

Parameters

array, valid ad ids.:

integer, how many advertisements to select:

string, the hostid:

File

./adcache.inc, line 320

Code

function adserve_hook_select($ids, $quantity = 1, $hostid = '') {
  $select = 0;
  if (is_array($ids)) {
    $ads = $ids;
    foreach ($ids as $key => $value) {
      $available = sizeof($ads);
      $select++;
      _debug_echo("Randomly selecting ad {$select} of {$quantity}.");
      $id = 0;
      if ($id == 0) {
        $id = $available > 1 ? $ads[mt_rand(0, $available - 1)] : $ads[0];
        _debug_echo("Randomly selected ID: {$id}.");
        $selected[] = $id;

        // strip away advertisments that have already been selected
        $ads = adserve_cache('validate', $ads, array(
          $id,
        ), $hostid);
      }
      if ($quantity == $select || !count($ads)) {

        // we have selected the right number of advertisements
        break;
      }
    }
  }
  if ($select < $quantity) {
    _debug_echo('No more advertisements available.');
  }
  return $selected;
}