1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @author Vinzenz Rosenkranz <[email protected]> |
6
|
|
|
* @author René Gieling <[email protected]> |
7
|
|
|
* |
8
|
|
|
* @license GNU AGPL version 3 or any later version |
9
|
|
|
* |
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
13
|
|
|
* License, or (at your option) any later version. |
14
|
|
|
* |
15
|
|
|
* This program is distributed in the hope that it will be useful, |
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
* GNU Affero General Public License for more details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU Affero General Public License |
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace OCA\Polls\Db; |
26
|
|
|
|
27
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder; |
28
|
|
|
use OCP\IDBConnection; |
29
|
|
|
use OCP\AppFramework\Db\QBMapper; |
30
|
|
|
use Doctrine\DBAL\Exception\TableNotFoundException; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @template-extends QBMapper<Vote> |
34
|
|
|
*/ |
35
|
|
|
class VoteMapper extends QBMapper { |
36
|
|
|
public function __construct(IDBConnection $db) { |
37
|
1 |
|
parent::__construct($db, 'polls_votes', '\OCA\Polls\Db\Vote'); |
38
|
1 |
|
} |
39
|
1 |
|
|
40
|
|
|
/** |
41
|
|
|
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found |
42
|
|
|
* @return Vote[] |
43
|
|
|
*/ |
44
|
|
|
public function findByPoll(int $pollId) { |
45
|
|
|
$qb = $this->db->getQueryBuilder(); |
46
|
|
|
|
47
|
|
|
$qb->select('*') |
48
|
|
|
->from($this->getTableName()) |
49
|
|
|
->where($qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))); |
50
|
|
|
return $this->findEntities($qb); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found |
55
|
|
|
* @return Vote[] |
56
|
|
|
*/ |
57
|
|
|
public function findByPollAndUser(int $pollId, string $userId) { |
58
|
|
|
$qb = $this->db->getQueryBuilder(); |
59
|
|
|
|
60
|
|
|
$qb->select('*') |
61
|
|
|
->from($this->getTableName()) |
62
|
|
|
->where($qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))) |
63
|
|
|
->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))); |
64
|
|
|
return $this->findEntities($qb); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found |
69
|
|
|
* @return Vote |
70
|
|
|
*/ |
71
|
|
|
public function findSingleVote(int $pollId, string $optionText, string $userId) { |
72
|
|
|
$qb = $this->db->getQueryBuilder(); |
73
|
|
|
|
74
|
|
|
$qb->select('*') |
75
|
|
|
->from($this->getTableName()) |
76
|
|
|
->where($qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))) |
77
|
|
|
->andWhere($qb->expr()->eq('vote_option_text', $qb->createNamedParameter($optionText, IQueryBuilder::PARAM_STR))) |
78
|
|
|
->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))); |
79
|
|
|
return $this->findEntity($qb); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found |
84
|
|
|
* @return Vote[] |
85
|
|
|
*/ |
86
|
|
|
public function findParticipantsByPoll(int $pollId) { |
87
|
|
|
$qb = $this->db->getQueryBuilder(); |
88
|
|
|
|
89
|
|
|
$qb->selectDistinct('user_id') |
90
|
|
|
->from($this->getTableName()) |
91
|
|
|
->where( |
92
|
|
|
$qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT)) |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
return $this->findEntities($qb); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found |
100
|
|
|
* |
101
|
|
|
* @return Vote[] |
102
|
|
|
* |
103
|
|
|
* @psalm-return array<array-key, Vote> |
104
|
|
|
*/ |
105
|
|
|
public function findParticipantsVotes(int $pollId, $userId): array { |
106
|
|
|
$qb = $this->db->getQueryBuilder(); |
107
|
|
|
$qb->select('*') |
108
|
|
|
->from($this->getTableName()) |
109
|
|
|
->where($qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))) |
110
|
|
|
->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))); |
111
|
|
|
return $this->findEntities($qb); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function deleteByPollAndUser(int $pollId, string $userId): void { |
115
|
|
|
$qb = $this->db->getQueryBuilder(); |
116
|
|
|
$qb->delete($this->getTableName()) |
117
|
|
|
->where($qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))) |
118
|
|
|
->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))) |
119
|
|
|
->execute(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function deleteByPoll($pollId): void { |
123
|
|
|
$qb = $this->db->getQueryBuilder(); |
124
|
|
|
$qb->delete($this->getTableName()) |
125
|
|
|
->where($qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))) |
126
|
|
|
->execute(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function getYesVotesByParticipant(int $pollId, string $userId): array { |
130
|
|
|
$qb = $this->db->getQueryBuilder(); |
131
|
|
|
$qb->select('*') |
132
|
|
|
->from($this->getTableName()) |
133
|
|
|
->where($qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))) |
134
|
|
|
->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))) |
135
|
|
|
->andWhere($qb->expr()->eq('vote_answer', $qb->createNamedParameter('yes', IQueryBuilder::PARAM_STR))); |
136
|
|
|
return $this->findEntities($qb); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function getYesVotesByOption(int $pollId, string $pollOptionText): array { |
140
|
|
|
$qb = $this->db->getQueryBuilder(); |
141
|
|
|
|
142
|
|
|
$qb->select('*') |
143
|
|
|
->from($this->getTableName()) |
144
|
|
|
->where($qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))) |
145
|
|
|
->andWhere($qb->expr()->eq('vote_option_text', $qb->createNamedParameter($pollOptionText, IQueryBuilder::PARAM_STR))) |
146
|
|
|
->andWhere($qb->expr()->eq('vote_answer', $qb->createNamedParameter('yes', IQueryBuilder::PARAM_STR))); |
147
|
|
|
return $this->findEntities($qb); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return void |
153
|
|
|
*/ |
154
|
|
|
public function removeDuplicates() { |
155
|
|
|
try { |
156
|
|
|
$query = $this->db->getQueryBuilder(); |
157
|
|
|
$query->select('id', 'poll_id', 'user_id', 'vote_option_text') |
158
|
|
|
->from($this->getTableName()); |
159
|
|
|
$foundEntries = $query->execute(); |
160
|
|
|
|
161
|
|
|
$delete = $this->db->getQueryBuilder(); |
162
|
|
|
$delete->delete($this->getTableName()) |
163
|
|
|
->where('id = :id'); |
164
|
|
|
|
165
|
|
|
$entries2Keep = []; |
166
|
|
|
|
167
|
|
|
while ($row = $foundEntries->fetch()) { |
168
|
|
|
$currentRecord = [ |
169
|
|
|
$row['poll_id'], |
170
|
|
|
$row['user_id'], |
171
|
|
|
$row['vote_option_text'] |
172
|
|
|
]; |
173
|
|
|
if (in_array($currentRecord, $entries2Keep)) { |
174
|
|
|
$delete->setParameter('id', $row['id']); |
175
|
|
|
$delete->execute(); |
176
|
|
|
} else { |
177
|
|
|
$entries2Keep[] = $currentRecord; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} catch (TableNotFoundException $e) { |
181
|
|
|
// ignore |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return void |
187
|
|
|
*/ |
188
|
|
|
public function renameUserId(string $userId, string $replacementName): void { |
189
|
|
|
$query = $this->db->getQueryBuilder(); |
190
|
|
|
$query->update($this->getTableName()) |
191
|
|
|
->set('user_id', $query->createNamedParameter($replacementName)) |
192
|
|
|
->where($query->expr()->eq('user_id', $query->createNamedParameter($userId))) |
193
|
|
|
->execute(); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|