diff options
author | dartcafe <github@dartcafe.de> | 2021-05-19 22:31:33 +0300 |
---|---|---|
committer | dartcafe <github@dartcafe.de> | 2021-05-19 22:31:33 +0300 |
commit | aee209e115cf8e2a9aff20bd620f749d322d4ecd (patch) | |
tree | 11d37c730700cbfcd59bf44d64a55f2c8c0d7e67 /lib | |
parent | 23e365f39dde884866d810f86c058bc00e4d6223 (diff) |
Option to hadle no as unvoted
Signed-off-by: dartcafe <github@dartcafe.de>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Db/Poll.php | 8 | ||||
-rw-r--r-- | lib/Db/VoteMapper.php | 7 | ||||
-rw-r--r-- | lib/Migration/Version0200Date20210323120002.php | 7 | ||||
-rw-r--r-- | lib/Service/VoteService.php | 15 |
4 files changed, 36 insertions, 1 deletions
diff --git a/lib/Db/Poll.php b/lib/Db/Poll.php index 39a10ff1..5e2c3021 100644 --- a/lib/Db/Poll.php +++ b/lib/Db/Poll.php @@ -76,6 +76,8 @@ use OCA\Polls\Model\User; * @method void setImportant(integer $value) * @method int getHideBookedUp() * @method void setHideBookedUp(integer $value) + * @method int getUseNo() + * @method void setUseNo(integer $value) */ class Poll extends Entity implements JsonSerializable { public const TYPE_DATE = 'datePoll'; @@ -155,6 +157,9 @@ class Poll extends Entity implements JsonSerializable { /** @var int $hideBookedUp*/ protected $hideBookedUp; + /** @var int $useNo*/ + protected $useNo; + public function __construct() { $this->addType('created', 'int'); $this->addType('expire', 'int'); @@ -168,6 +173,7 @@ class Poll extends Entity implements JsonSerializable { $this->addType('adminAccess', 'int'); $this->addType('important', 'int'); $this->addType('hideBookedUp', 'int'); + $this->addType('useNo', 'int'); } public function jsonSerialize() { @@ -195,6 +201,7 @@ class Poll extends Entity implements JsonSerializable { 'ownerDisplayName' => $this->getDisplayName(), 'important' => $this->getImportant(), 'hideBookedUp' => $this->getHideBookedUp(), + 'useNo' => $this->getUseNo(), ]; } @@ -218,6 +225,7 @@ class Poll extends Entity implements JsonSerializable { $this->setAdminAccess($array['adminAccess'] ?? $this->getAdminAccess()); $this->setImportant($array['important'] ?? $this->getImportant()); $this->setHideBookedUp($array['hideBookedUp'] ?? $this->getHideBookedUp()); + $this->setUseNo($array['useNo'] ?? $this->getUseNo()); return $this; } diff --git a/lib/Db/VoteMapper.php b/lib/Db/VoteMapper.php index 8d286f19..bc836fa8 100644 --- a/lib/Db/VoteMapper.php +++ b/lib/Db/VoteMapper.php @@ -111,6 +111,13 @@ class VoteMapper extends QBMapper { return $this->findEntities($qb); } + public function deleteById(int $voteId): void { + $qb = $this->db->getQueryBuilder(); + $qb->delete($this->getTableName()) + ->where($qb->expr()->eq('id', $qb->createNamedParameter($voteId, IQueryBuilder::PARAM_INT))) + ->execute(); + } + public function deleteByPollAndUserId(int $pollId, string $userId): void { $qb = $this->db->getQueryBuilder(); $qb->delete($this->getTableName()) diff --git a/lib/Migration/Version0200Date20210323120002.php b/lib/Migration/Version0200Date20210323120002.php index 149d0cc8..b4a83e10 100644 --- a/lib/Migration/Version0200Date20210323120002.php +++ b/lib/Migration/Version0200Date20210323120002.php @@ -56,6 +56,13 @@ class Version0200Date20210323120002 extends SimpleMigrationStep { 'default' => 'disallow' ]); } + if (!$table->hasColumn('use_no')) { + $table->addColumn('use_no', 'integer', [ + 'length' => 11, + 'notnull' => true, + 'default' => 1 + ]); + } if (!$table->hasColumn('proposals_expire')) { $table->addColumn('proposals_expire', 'integer', [ 'length' => 11, diff --git a/lib/Service/VoteService.php b/lib/Service/VoteService.php index 36245cd7..46dd0e38 100644 --- a/lib/Service/VoteService.php +++ b/lib/Service/VoteService.php @@ -138,7 +138,7 @@ class VoteService { /** * Set vote */ - public function set(int $optionId, string $setTo, string $token = ''): Vote { + public function set(int $optionId, string $setTo, string $token = ''): ?Vote { $option = $this->optionMapper->find($optionId); if ($token) { @@ -153,8 +153,21 @@ class VoteService { try { $this->vote = $this->voteMapper->findSingleVote($this->acl->getPollId(), $option->getPollOptionText(), $this->acl->getUserId()); + + if (in_array(trim($setTo), ['no', '']) && !$this->acl->getPoll()->getUseNo()) { + try { + $this->voteMapper->delete($this->vote); + } catch (DoesNotExistException $e) { + // catch silently + } + $this->vote->setVoteAnswer(''); + return $this->vote; + + } + $this->vote->setVoteAnswer($setTo); $this->voteMapper->update($this->vote); + } catch (DoesNotExistException $e) { // Vote does not exist, insert as new Vote $this->vote = new Vote(); |