PollVoteStorage.php in Poll 8
File
src/PollVoteStorage.php
View source
<?php
namespace Drupal\poll;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Session\AccountInterface;
class PollVoteStorage implements PollVoteStorageInterface {
protected $connection;
protected $cacheTagsInvalidator;
protected $currentUserVote = [];
public function __construct(Connection $connection, CacheTagsInvalidatorInterface $cache_tags_invalidator) {
$this->connection = $connection;
$this->cacheTagsInvalidator = $cache_tags_invalidator;
}
public function deleteChoicesVotes(array $choices) {
$this->connection
->delete('poll_vote')
->condition('chid', $choices, 'IN')
->execute();
}
public function deleteVotes(PollInterface $poll) {
$this->connection
->delete('poll_vote')
->condition('pid', $poll
->id())
->execute();
$this->cacheTagsInvalidator
->invalidateTags([
'poll-votes:' . $poll
->id(),
]);
$this->currentUserVote = [];
}
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();
}
$this->cacheTagsInvalidator
->invalidateTags([
'poll-votes:' . $poll
->id(),
]);
$this->currentUserVote = [];
}
public function saveVote(array $options) {
if (!is_array($options)) {
return;
}
$this->connection
->insert('poll_vote')
->fields($options)
->execute();
$this->cacheTagsInvalidator
->invalidateTags([
'poll-votes:' . $options['pid'],
]);
$this->currentUserVote = [];
}
public function getVotes(PollInterface $poll) {
$votes = array();
$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(),
));
foreach ($result as $row) {
$votes[$row->chid] = $row->votes;
}
return $votes;
}
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];
}
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();
}
}