-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathVoteService.php
More file actions
221 lines (191 loc) · 6.97 KB
/
Copy pathVoteService.php
File metadata and controls
221 lines (191 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2017 Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Polls\Service;
use OCA\Polls\Db\Option;
use OCA\Polls\Db\OptionMapper;
use OCA\Polls\Db\Poll;
use OCA\Polls\Db\PollMapper;
use OCA\Polls\Db\Vote;
use OCA\Polls\Db\VoteMapper;
use OCA\Polls\Event\VoteDeletedOrphanedEvent;
use OCA\Polls\Event\VoteSetEvent;
use OCA\Polls\Exceptions\ForbiddenException;
use OCA\Polls\Exceptions\NotFoundException;
use OCA\Polls\Exceptions\VoteLimitExceededException;
use OCA\Polls\UserSession;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\EventDispatcher\IEventDispatcher;
class VoteService {
/** @psalm-suppress PossiblyUnusedMethod */
public function __construct(
private IEventDispatcher $eventDispatcher,
private OptionMapper $optionMapper,
private PollMapper $pollMapper,
private Vote $vote,
private VoteMapper $voteMapper,
private UserSession $userSession,
) {
}
/**
* Read all votes of a poll based on the poll id and return list as array
*
* @return Vote[]
*/
public function list(int $pollId): array {
$poll = $this->pollMapper->get($pollId, withRoles: true);
$poll->request(Poll::PERMISSION_POLL_VIEW);
if (!$poll->getIsAllowed(Poll::PERMISSION_POLL_RESULTS_VIEW)) {
// Just return the participants votes, no further anoymizing or obfuscating is nessecary
return $this->voteMapper->findByPollAndUser($pollId, ($this->userSession->getCurrentUserId()));
}
$votes = $this->voteMapper->findByPoll($pollId);
return $votes;
}
private function checkLimits(Option $option): void {
// check, if the optionlimit is reached or exceeded, if one is set
if ($option->getIsLockedByOptionLimit()) {
throw new VoteLimitExceededException;
}
if ($option->getIsLockedByVotesLimit()) {
throw new VoteLimitExceededException;
}
return;
}
private function checkVoteLimit(Option $option): void {
// check, if the optionlimit is reached or exceeded, if one is set
if ($option->getIsLockedByOptionLimit()) {
throw new VoteLimitExceededException();
}
if ($option->getIsLockedByVotesLimit()) {
throw new VoteLimitExceededException;
}
return;
}
/**
* Set vote
*/
public function set(Option|int $optionOrOptionIdoptionId, string $setTo): ?Vote {
if ($optionOrOptionIdoptionId instanceof Option) {
$option = $optionOrOptionIdoptionId;
} else {
$option = $this->optionMapper->find($optionOrOptionIdoptionId);
}
$poll = $this->pollMapper->get($option->getPollId(), withRoles: true);
$poll->request(Poll::PERMISSION_VOTE_EDIT);
if ($option->getIsLocked()) {
$this->checkVoteLimit($option);
throw new NotFoundException();
}
try {
$this->vote = $this->voteMapper->findSingleVote($poll->getId(), $option->getPollOptionText(), $this->userSession->getCurrentUserId());
if ($setTo === $this->vote->getVoteAnswer()) {
return $this->vote;
}
if ($setTo === Vote::VOTE_YES) {
$this->checkLimits($option);
}
// delete no votes, if poll setting is set to useNo === 0
$deleteVoteInsteadOfNoVote = in_array(trim($setTo), [Vote::VOTE_NO, '']) && !boolval($poll->getUseNo());
if ($deleteVoteInsteadOfNoVote) {
$this->vote->setVoteAnswer('');
$this->voteMapper->delete($this->vote);
} else {
$this->vote->setVoteAnswer($setTo);
$this->vote = $this->voteMapper->update($this->vote);
}
} catch (DoesNotExistException $e) {
// Vote does not exist, insert as new Vote
$this->vote = new Vote();
$this->vote->setPollId($poll->getId());
$this->vote->setUserId($this->userSession->getCurrentUserId());
$this->vote->setVoteOptionText($option->getPollOptionText());
$this->vote->setVoteOptionId($option->getId());
$this->vote->setVoteAnswer($setTo);
$this->vote = $this->voteMapper->insert($this->vote);
}
$this->eventDispatcher->dispatchTyped(new VoteSetEvent($this->vote));
return $this->vote;
}
/**
* Get all votes of a poll, which are not assigned to an option
*
* @param int $pollId poll id of the poll the votes get deleted from
* @return Vote[]
*/
public function getOprhanedVotes(int $pollId): array {
try {
$poll = $this->pollMapper->get($pollId, withRoles: true);
$poll->request(Poll::PERMISSION_POLL_EDIT);
return $this->voteMapper->findOrphanedByPoll($pollId);
} catch (ForbiddenException $e) {
return [];
}
}
/**
* Delete all votes of a poll, which are not assigned to an option
*
* @param int $pollId poll id of the poll the votes get deleted from
* @return Vote[]
*/
public function deleteOrphanedVotes(int $pollId): array {
$poll = $this->pollMapper->get($pollId, withRoles: true);
$poll->request(Poll::PERMISSION_VOTE_FOREIGN_CHANGE);
// delete all votes of the poll, which are not assigned to an option
$votes = $this->voteMapper->findOrphanedByPoll($pollId);
foreach ($votes as $vote) {
$this->voteMapper->delete($vote);
// TODO: rework notification methods
// keep this dispatch as reminder
// $this->eventDispatcher->dispatchTyped(new VoteDeletedOrphanedEvent($this->vote, false));
}
return $votes;
}
/**
* Remove user from poll
*
* @param int $pollId poll id of the poll the votes get deleted from
* @param string $userId user id of the user, the votes get deleted from
* @param bool $deleteOnlyOrphaned - false deletes all votes of the specified user, true only the orphaned votes aka votes without an option
*/
public function deleteUserFromPoll(int $pollId, string $userId = '', bool $deleteOnlyOrphaned = false): string {
if ($userId === '') {
// if no user set, use current user
$userId = $this->userSession->getCurrentUserId();
}
// Set default right to delete all votes of the user
$checkRight = Poll::PERMISSION_VOTE_FOREIGN_CHANGE;
if ($userId === $this->userSession->getCurrentUserId()) {
// allow current user to remove his votes
$checkRight = Poll::PERMISSION_VOTE_EDIT;
}
$this->pollMapper->get($pollId, withRoles: true)->request($checkRight);
return $this->delete($pollId, $userId, $deleteOnlyOrphaned);
}
/**
* Remove user from poll
* @param int $pollId poll id of the poll the votes get deleted from
* @param string $userId user id of the user, the votes get deleted from. No user affects the current user
* @param bool $deleteOnlyOrphaned - false deletes all votes of the specified user, true only the orphaned votes aka votes without an option
*/
private function delete(int $pollId, string $userId, bool $deleteOnlyOrphaned = false): string {
if ($deleteOnlyOrphaned) {
$votes = $this->voteMapper->findOrphanedByPollandUser($pollId, $userId);
foreach ($votes as $vote) {
$this->voteMapper->delete($vote);
}
return $userId;
}
// fake a vote so that the event can be triggered
// suppress logging of this action
$this->vote = new Vote();
$this->vote->setPollId($pollId);
$this->vote->setUserId($userId);
$this->voteMapper->deleteByPollAndUserId($pollId, $userId);
$this->eventDispatcher->dispatchTyped(new VoteSetEvent($this->vote, false));
return $userId;
}
}