@@ -86,44 +86,48 @@ public function findById(int $id): Submission {
8686 }
8787
8888 /**
89- * @param int $formId
90- * @throws DoesNotExistException if not found
91- * @return array
89+ * Checks if the specified user has submissions for the specified form
90+ * @param int $formId ID of the form
91+ * @param string $userId UID of the user
92+ * @param bool $checkMultipleFormSubmissions if true, we check if there is more than one submissions, if false, at least one submissions
93+ * @return bool
9294 */
93- public function findParticipantsByForm (int $ formId ): array {
95+ public function hasFormSubmissionsByUser (
96+ int $ formId ,
97+ string $ userId ,
98+ bool $ checkMultipleFormSubmissions
99+ ): bool {
100+ $ requireCountFormSubmissions = $ checkMultipleFormSubmissions ? 2 : 1 ;
101+
94102 $ qb = $ this ->db ->getQueryBuilder ();
95103
96- $ qb ->select ('user_id ' )
104+ $ query = $ qb ->select ('id ' )
97105 ->from ($ this ->getTableName ())
98106 ->where (
99107 $ qb ->expr ()->eq ('form_id ' , $ qb ->createNamedParameter ($ formId , IQueryBuilder::PARAM_INT ))
100- );
101-
102- $ submissionEntities = $ this -> findEntities ( $ qb );
103-
104- // From array of submissionEntities produce array of userIds.
105- $ userIds = array_map ( function ( $ submissionEntity ) {
106- return $ submissionEntity -> getUserId ();
107- }, $ submissionEntities );
108+ )
109+ -> andWhere (
110+ $ qb -> expr ()-> eq ( ' user_id ' , $ qb -> createNamedParameter ( $ userId , IQueryBuilder:: PARAM_STR ))
111+ )
112+ -> setMaxResults ( $ requireCountFormSubmissions );
113+ $ result = $ query -> executeQuery ();
114+ $ rows = $ result -> fetchAll ();
115+ $ result -> closeCursor ( );
108116
109- return $ userIds ;
117+ return count ( $ rows ) === $ requireCountFormSubmissions ;
110118 }
111119
112120 /**
113- * Count submissions by form and optionally also by userId
114- * @param int $formId ID of the form to count submissions for
115- * @param string|null $userId optionally limit submissions to the one of that user
121+ * Count submissions by form
122+ * @param int $formId ID of the form to count submissions
116123 * @throws \Exception
117124 */
118- public function countSubmissions (int $ formId, ? string $ userId = null ): int {
125+ public function countSubmissions (int $ formId ): int {
119126 $ qb = $ this ->db ->getQueryBuilder ();
120127
121128 $ query = $ qb ->select ($ qb ->func ()->count ('* ' , 'num_submissions ' ))
122129 ->from ($ this ->getTableName ())
123130 ->where ($ qb ->expr ()->eq ('form_id ' , $ qb ->createNamedParameter ($ formId , IQueryBuilder::PARAM_INT )));
124- if (!is_null ($ userId )) {
125- $ query ->andWhere ($ qb ->expr ()->eq ('user_id ' , $ qb ->createNamedParameter ($ userId , IQueryBuilder::PARAM_STR )));
126- }
127131
128132 $ result = $ query ->executeQuery ();
129133 $ row = $ result ->fetch ();
0 commit comments