You are here

public function PollVoteStorage::getVotes in Poll 8

Get all votes for a poll.

Parameters

PollInterface $poll:

Return value

mixed

Overrides PollVoteStorageInterface::getVotes

File

src/PollVoteStorage.php, line 115

Class

PollVoteStorage
Controller class for poll vote storage.

Namespace

Drupal\poll

Code

public function getVotes(PollInterface $poll) {
  $votes = array();

  // Set votes for all options to 0
  $options = $poll
    ->getOptions();
  foreach ($options as $id => $label) {
    $votes[$id] = 0;
  }
  $result = $this->connection
    ->query("SELECT chid, COUNT(chid) AS votes FROM {poll_vote} WHERE pid = :pid GROUP BY chid", array(
    ':pid' => $poll
      ->id(),
  ));

  // Replace the count for options that have recorded votes in the database.
  foreach ($result as $row) {
    $votes[$row->chid] = $row->votes;
  }
  return $votes;
}