Mercurial > defr > drupal > ad
annotate weight/probability/ad_weight_probability.inc @ 1:948362c2a207 ad
update advertisement
author | pierre |
---|---|
date | Thu, 02 Apr 2009 15:28:21 +0000 |
parents | |
children |
rev | line source |
---|---|
pierre@1 | 1 <?php |
pierre@1 | 2 // $Id: ad_weight_probability.inc,v 1.1.4.3 2009/02/28 05:48:42 jeremy Exp $ |
pierre@1 | 3 |
pierre@1 | 4 /** |
pierre@1 | 5 * @file |
pierre@1 | 6 * A plug in for the ad.module, allowing an admin to set the probability that |
pierre@1 | 7 * a given advertisement will be displayed. |
pierre@1 | 8 * |
pierre@1 | 9 * Copyright (c) 2008-2009. |
pierre@1 | 10 * Jeremy Andrews <jeremy@tag1consulting.com>. |
pierre@1 | 11 */ |
pierre@1 | 12 |
pierre@1 | 13 function ad_weight_probability_cache_filter($ads) { |
pierre@1 | 14 $display = array(); |
pierre@1 | 15 if (is_array($ads)) { |
pierre@1 | 16 $probability = array(); |
pierre@1 | 17 $cache = adserve_cache('get_cache', 'weight'); |
pierre@1 | 18 foreach ($ads as $aid) { |
pierre@1 | 19 $probability[] = isset($cache['probability'][$aid]) ? $cache['probability'][$aid] : 0; |
pierre@1 | 20 } |
pierre@1 | 21 $gcd = ad_weight_probability_gcd($probability); |
pierre@1 | 22 _debug_echo("ad_weight_probability cache_filter gcd($gcd)"); |
pierre@1 | 23 foreach ($ads as $aid) { |
pierre@1 | 24 $weight = (isset($cache['probability'][$aid]) && $gcd) ? $cache['probability'][$aid] / $gcd : 0; |
pierre@1 | 25 _debug_echo("ad_weight_probability cache_filter aid($aid) weight($weight)"); |
pierre@1 | 26 for ($i = 1; $i <= $weight; $i++) { |
pierre@1 | 27 $display[] = $aid; |
pierre@1 | 28 } |
pierre@1 | 29 } |
pierre@1 | 30 } |
pierre@1 | 31 return $display; |
pierre@1 | 32 } |
pierre@1 | 33 |
pierre@1 | 34 /** |
pierre@1 | 35 * Returns the greatest common divisor of an array of integers. |
pierre@1 | 36 */ |
pierre@1 | 37 function ad_weight_probability_gcd($integers) { |
pierre@1 | 38 $gcd = array_shift($integers); |
pierre@1 | 39 |
pierre@1 | 40 while (!empty($integers)) { |
pierre@1 | 41 $gcd = _ad_weight_probability_gcd($gcd, array_shift($integers)); |
pierre@1 | 42 } |
pierre@1 | 43 return $gcd; |
pierre@1 | 44 } |
pierre@1 | 45 |
pierre@1 | 46 /** |
pierre@1 | 47 * Helper function to calculate the greatest common divisor using the Euclidean |
pierre@1 | 48 * algorithm (http://en.wikipedia.org/wiki/Euclidean_algorithm). |
pierre@1 | 49 */ |
pierre@1 | 50 function _ad_weight_probability_gcd($a, $b) { |
pierre@1 | 51 if ($b == 0) { |
pierre@1 | 52 return $a; |
pierre@1 | 53 } |
pierre@1 | 54 else { |
pierre@1 | 55 return _ad_weight_probability_gcd($b, $a % $b); |
pierre@1 | 56 } |
pierre@1 | 57 } |