You are here

protected function RateWidget::calculateResults in Rate 7.2

Add elements with results.

1 call to RateWidget::calculateResults()
RateWidget::__construct in classes/widget.inc
Create a new Rate widget.

File

classes/widget.inc, line 388

Class

RateWidget

Code

protected function calculateResults() {

  // Add rating.
  $this->elements['rating'] = round($this->rating);

  // Add vote count.
  $this->elements['count'] = $this->count;

  // Add thumbs up / down values.
  if ($this->value_type == 'points') {
    $up = NULL;
    $down = NULL;
    foreach ($this->elements as $name => $element) {
      if ($element instanceof RateButton) {
        if ($element
          ->getValue() == 1) {
          $up = $name;
        }
        if ($element
          ->getValue() == -1) {
          $down = $name;
        }
      }
    }
    if ($up && $down) {
      $down_count = (int) ($this->count - $this->rating) / 2;
      $up_count = $this->count - $down_count;
      if ($up_count == $down_count) {
        $up_percent = 50;
        $down_percent = 50;
      }
      else {
        $up_percent = $up_count / $this->count * 100;
        $down_percent = 100 - $up_percent;
      }
      $this->elements['up_count'] = $up_count;
      $this->elements['up_percent'] = $up_percent;
      $this->elements['down_count'] = $down_count;
      $this->elements['down_percent'] = $down_percent;
    }
  }

  // Add button counts for option widgets.
  foreach ($this->elements as $name => $element) {
    if ($element instanceof RateButton) {
      $value = $element
        ->getValue();
      foreach ($this->results as $result) {
        if ($result['function'] == 'option-' . $value) {
          $this->elements["{$name}_count"] = $result['value'];
        }
      }
    }
  }
}