You are here

public function RateWidget::__construct in Rate 7.2

Create a new Rate widget.

Parameters

array $attributes: Associative array with the following keys:

  • type: Machine name for this widget.
  • entity_type: Entity type
  • entity_id: Entity id for the entity we are rating on.
  • mode: Display mode.
  • account: Voters account.

File

classes/widget.inc, line 139

Class

RateWidget

Code

public function __construct($attributes) {
  global $user;
  $this->type = $attributes['type'];
  $this->entity_type = $attributes['entity_type'];
  $this->entity_id = $attributes['entity_id'];
  $this->mode = $attributes['mode'];
  $this->field_name = $attributes['field_name'];
  $this->account = empty($attributes['account']) ? $user : $attributes['account'];
  $this->displayed = empty($attributes['displayed']) ? RATE_AVERAGE : $attributes['displayed'];
  $this->displayed_just_voted = empty($attributes['displayed_just_voted']) ? RATE_USER_OR_AVERAGE : $attributes['displayed_just_voted'];
  $this->elements = array();
  $this->just_voted = FALSE;
  $this->tag = isset($attributes['tag']) ? $attributes['tag'] : 'vote';
  $this
    ->load();
  $this
    ->processVote();
  if ($this->just_voted) {
    $this->displayed = $this->displayed_just_voted;
  }
  $this->results = $this
    ->getAverageResults();
  $user_vote = $this
    ->getUserResults();
  if ($user_vote) {
    $this->voted = TRUE;
    $this->results[] = array(
      'function' => 'user',
      'value' => $user_vote,
    );
  }
  else {
    $this->voted = FALSE;
  }
  $average_rating = NULL;
  $user_rating = NULL;
  $this->rating = 0;
  $this->count = 0;
  foreach ($this->results as $result) {
    switch ($result['function']) {
      case 'average':
        $average_rating = $result['value'];
      case 'user':
        $user_rating = $result['value'];
        break;
      case 'count':
        $this->count = $result['value'];
        break;
    }
  }
  switch ($this->displayed) {
    case RATE_USER_OR_AVERAGE:
    case RATE_USER:
      if (!is_null($user_rating)) {
        $this->rating = $user_rating;
        $this->displayed = RATE_USER;
        break;
      }
    case RATE_AVERAGE:
      $this->rating = $average_rating;
      break;
  }
  $this
    ->calculateResults();
  $this
    ->init();
}