AverageField

The Contact Form DB AverageField class.

Defined (1)

The class is defined in the following location(s).

/AverageField.php  
  1. class AverageField implements CFDBTransform { 
  2.  
  3. /** 
  4. * @var string field holding the value 
  5. */ 
  6. var $valueField; 
  7.  
  8. /** 
  9. * @var string field to group by 
  10. */ 
  11. var $groupByField; 
  12.  
  13. /** 
  14. * @var array of $groupByField => sum 
  15. */ 
  16. var $sums = array(); 
  17.  
  18. /** 
  19. * @var array of $groupByField => count 
  20. */ 
  21. var $counts = array(); 
  22.  
  23. function __construct($valueField, $groupByField = null) { 
  24. $this->valueField = $valueField; 
  25. $this->groupByField = $groupByField; 
  26.  
  27. public function addEntry(&$entry) { 
  28. if (array_key_exists($this->valueField, $entry) && is_numeric($entry[$this->valueField])) { 
  29. $value = $entry[$this->valueField]; 
  30. $groupByName = empty($this->groupByField) ? $this->valueField : $entry[$this->groupByField]; 
  31.  
  32. if ($value !== null && $value !== '') { 
  33. if (!array_key_exists($groupByName, $this->sums)) { 
  34. $this->sums[$groupByName] = $value; 
  35. $this->counts[$groupByName] = 1; 
  36. } else { 
  37. $this->sums[$groupByName] += $value; 
  38. $this->counts[$groupByName]++; 
  39.  
  40. public function getTransformedData() { 
  41. $data = array(); 
  42. foreach (array_keys($this->sums) as $name) { 
  43. $average = $this->sums[$name] / $this->counts[$name]; 
  44. if (empty($this->groupByField)) { 
  45. $data[] = array($this->valueField => $average); 
  46. } else { 
  47. $data[] = array($this->groupByField => $name, $this->valueField => $average); 
  48. return $data; 
  49.