You are here

class PollVoteStorage in Poll 8

Controller class for poll vote storage.

Hierarchy

Expanded class hierarchy of PollVoteStorage

1 string reference to 'PollVoteStorage'
poll.services.yml in ./poll.services.yml
poll.services.yml
1 service uses PollVoteStorage
poll_vote.storage in ./poll.services.yml
Drupal\poll\PollVoteStorage

File

src/PollVoteStorage.php, line 12

Namespace

Drupal\poll
View source
class PollVoteStorage implements PollVoteStorageInterface {

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * The cache tags invalidator.
   *
   * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
   */
  protected $cacheTagsInvalidator;

  /**
   * The poll vote for the current user keyed by Poll ID and User ID.
   *
   * @var array[]
   */
  protected $currentUserVote = [];

  /**
   * Constructs a new PollVoteStorage.
   *
   * @param \Drupal\Core\Database\Connection $connection
   *   A Database connection to use for reading and writing database data.
   * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_invalidator
   *   The cache tags invalidator.
   */
  public function __construct(Connection $connection, CacheTagsInvalidatorInterface $cache_tags_invalidator) {
    $this->connection = $connection;
    $this->cacheTagsInvalidator = $cache_tags_invalidator;
  }

  /**
   * {@inheritdoc}
   */
  public function deleteChoicesVotes(array $choices) {
    $this->connection
      ->delete('poll_vote')
      ->condition('chid', $choices, 'IN')
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function deleteVotes(PollInterface $poll) {
    $this->connection
      ->delete('poll_vote')
      ->condition('pid', $poll
      ->id())
      ->execute();

    // Deleting a vote means that any cached vote might not be updated in the
    // UI, so we need to invalidate them all.
    $this->cacheTagsInvalidator
      ->invalidateTags([
      'poll-votes:' . $poll
        ->id(),
    ]);

    // Invalidate the static cache of votes.
    $this->currentUserVote = [];
  }

  /**
   * {@inheritdoc}
   */
  public function cancelVote(PollInterface $poll, AccountInterface $account = NULL) {
    if ($account
      ->id()) {
      $this->connection
        ->delete('poll_vote')
        ->condition('pid', $poll
        ->id())
        ->condition('uid', $account
        ->id())
        ->execute();
    }
    else {
      $this->connection
        ->delete('poll_vote')
        ->condition('pid', $poll
        ->id())
        ->condition('uid', \Drupal::currentUser()
        ->id())
        ->condition('hostname', \Drupal::request()
        ->getClientIp())
        ->execute();
    }

    // Deleting a vote means that any cached vote might not be updated in the
    // UI, so we need to invalidate them all.
    $this->cacheTagsInvalidator
      ->invalidateTags([
      'poll-votes:' . $poll
        ->id(),
    ]);

    // Invalidate the static cache of votes.
    $this->currentUserVote = [];
  }

  /**
   * {@inheritdoc}
   */
  public function saveVote(array $options) {
    if (!is_array($options)) {
      return;
    }
    $this->connection
      ->insert('poll_vote')
      ->fields($options)
      ->execute();

    // Deleting a vote means that any cached vote might not be updated in the
    // UI, so we need to invalidate them all.
    $this->cacheTagsInvalidator
      ->invalidateTags([
      'poll-votes:' . $options['pid'],
    ]);

    // Invalidate the static cache of votes.
    $this->currentUserVote = [];
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function getUserVote(PollInterface $poll) {
    $uid = \Drupal::currentUser()
      ->id();
    $key = $poll
      ->id() . ':' . $uid;
    if (isset($this->currentUserVote[$key])) {
      return $this->currentUserVote[$key];
    }
    $this->currentUserVote[$key] = FALSE;
    if ($uid || $poll
      ->getAnonymousVoteAllow()) {
      if ($uid) {
        $query = $this->connection
          ->query("SELECT * FROM {poll_vote} WHERE pid = :pid AND uid = :uid", array(
          ':pid' => $poll
            ->id(),
          ':uid' => $uid,
        ));
      }
      else {
        $query = $this->connection
          ->query("SELECT * FROM {poll_vote} WHERE pid = :pid AND hostname = :hostname AND uid = 0", array(
          ':pid' => $poll
            ->id(),
          ':hostname' => \Drupal::request()
            ->getClientIp(),
        ));
      }
      $this->currentUserVote[$key] = $query
        ->fetchAssoc();
    }
    return $this->currentUserVote[$key];
  }

  /**
   * {@inheritdoc}
   */
  public function getTotalVotes(PollInterface $poll) {
    $query = $this->connection
      ->query("SELECT COUNT(chid) FROM {poll_vote} WHERE pid = :pid", array(
      ':pid' => $poll
        ->id(),
    ));
    return $query
      ->fetchField();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PollVoteStorage::$cacheTagsInvalidator protected property The cache tags invalidator.
PollVoteStorage::$connection protected property The database connection.
PollVoteStorage::$currentUserVote protected property The poll vote for the current user keyed by Poll ID and User ID.
PollVoteStorage::cancelVote public function Cancel a user's vote. Overrides PollVoteStorageInterface::cancelVote
PollVoteStorage::deleteChoicesVotes public function Delete a user's votes for a poll choice. Overrides PollVoteStorageInterface::deleteChoicesVotes
PollVoteStorage::deleteVotes public function Delete a user's votes for a poll. Overrides PollVoteStorageInterface::deleteVotes
PollVoteStorage::getTotalVotes public function Get total votes for a poll. Overrides PollVoteStorageInterface::getTotalVotes
PollVoteStorage::getUserVote public function Get a user's votes for a poll. Overrides PollVoteStorageInterface::getUserVote
PollVoteStorage::getVotes public function Get all votes for a poll. Overrides PollVoteStorageInterface::getVotes
PollVoteStorage::saveVote public function Save a user's vote. Overrides PollVoteStorageInterface::saveVote
PollVoteStorage::__construct public function Constructs a new PollVoteStorage.