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