forked from nextcloud/forms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubmissionMapper.php
More file actions
183 lines (157 loc) · 5.53 KB
/
SubmissionMapper.php
File metadata and controls
183 lines (157 loc) · 5.53 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
<?php
/**
* @copyright Copyright (c) 2020 Jonas Rittershofer <jotoeri@users.noreply.github.com>
*
* @author affan98 <affan98@gmail.com>
* @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
* @author Jonas Rittershofer <jotoeri@users.noreply.github.com>
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Forms\Db;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
/**
* @extends QBMapper<Submission>
*/
class SubmissionMapper extends QBMapper {
private $answerMapper;
/**
* SubmissionMapper constructor.
* @param IDBConnection $db
* @param AnswerMapper $answerMapper
*/
public function __construct(IDBConnection $db, AnswerMapper $answerMapper) {
parent::__construct($db, 'forms_v2_submissions', Submission::class);
$this->answerMapper = $answerMapper;
}
/**
* @param int $formId
* @throws DoesNotExistException if not found
* @return Submission[]
*/
public function findByForm(int $formId): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('form_id', $qb->createNamedParameter($formId, IQueryBuilder::PARAM_INT))
)
//Newest submissions first
->orderBy('timestamp', 'DESC');
return $this->findEntities($qb);
}
/**
* @param int $id
* @return Submission
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
*/
public function findById(int $id): Submission {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
);
return $this->findEntity($qb);
}
/**
* Сhecks if there are multiple form submissions by user
* @param Form $form of the form to count submissions
* @param string $userId ID of the user to count submissions
*/
public function hasMultipleFormSubmissionsByUser(Form $form, string $userId): bool {
return $this->countSubmissionsWithFilters($form->getId(), $userId, 2) >= 2;
}
/**
* Сhecks if there are form submissions by user
* @param Form $form of the form to count submissions
* @param string $userId ID of the user to count submissions
*/
public function hasFormSubmissionsByUser(Form $form, string $userId): bool {
return (bool)$this->countSubmissionsWithFilters($form->getId(), $userId, 1);
}
/**
* Count submissions by form
* @param int $formId ID of the form to count submissions
* @throws \Exception
*/
public function countSubmissions(int $formId): int {
return $this->countSubmissionsWithFilters($formId, null, -1);
}
/**
* Count submissions by form with optional filters
* @param int $formId ID of the form to count submissions
* @param string|null $userId optionally limit submissions to the one of that user
* @param int $limit allows to limit the query selection. If -1, the restriction is ignored
* @throws \Exception
*/
protected function countSubmissionsWithFilters(int $formId, ?string $userId = null, int $limit = -1): int {
$qb = $this->db->getQueryBuilder();
$query = $qb->select($qb->func()->count('*', 'num_submissions'))
->from($this->getTableName())
->where($qb->expr()->eq('form_id', $qb->createNamedParameter($formId, IQueryBuilder::PARAM_INT)));
if (!is_null($userId)) {
$query->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)));
}
if ($limit !== -1) {
$query->setMaxResults($limit);
}
$result = $query->executeQuery();
$row = $result->fetch();
$result->closeCursor();
return (int) ($row['num_submissions'] ?? 0);
}
/**
* Delete the Submission, including answers.
* @param int $id of the submission to delete
*/
public function deleteById(int $id): void {
$qb = $this->db->getQueryBuilder();
// First delete corresponding answers.
$submissionEntity = $this->findById($id);
$this->answerMapper->deleteBySubmission($submissionEntity->getId());
//Delete Submission
$qb->delete($this->getTableName())
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
);
$qb->executeStatement();
}
/**
* Delete all Submissions corresponding to form, including answers.
* @param int $formId
*/
public function deleteByForm(int $formId): void {
$qb = $this->db->getQueryBuilder();
// First delete corresponding answers.
$submissionEntities = $this->findByForm($formId);
foreach ($submissionEntities as $submissionEntity) {
$this->answerMapper->deleteBySubmission($submissionEntity->id);
}
//Delete Submissions
$qb->delete($this->getTableName())
->where(
$qb->expr()->eq('form_id', $qb->createNamedParameter($formId, IQueryBuilder::PARAM_INT))
);
$qb->executeStatement();
}
}