77
88namespace OCA \Forms \Db ;
99
10- use OC \DB \QueryBuilder \QueryFunction ;
1110use OCP \AppFramework \Db \DoesNotExistException ;
1211use OCP \AppFramework \Db \QBMapper ;
1312use OCP \DB \QueryBuilder \IQueryBuilder ;
@@ -30,35 +29,43 @@ public function __construct(
3029 }
3130
3231 /**
33- * @param int $formId
34- * @param ?string $query
35- * @param int $limit
36- * @param int $offset
37- * @throws DoesNotExistException if not found
38- * @return Submission[]
32+ * Retrieves a list of submissions for a specific form.
33+ *
34+ * @param int $formId The ID of the form whose submissions are being retrieved.
35+ * @param ?string $query An optional search query to filter the submissions.
36+ * @param ?int $limit The maximum number of submissions to retrieve, default: all submissions
37+ * @param int $offset The number of submissions to skip before starting to retrieve, default: 0
38+ *
39+ * @throws DoesNotExistException If no submissions are found for the given form ID.
40+ *
41+ * @return Submission[] An array of Submission objects.
3942 */
40- public function findByForm (int $ formId , ?string $ query = null , int $ limit = 20 , int $ offset = 0 ): array {
43+ public function findByForm (int $ formId , ?string $ query = null , ? int $ limit = null , int $ offset = 0 ): array {
4144 $ qb = $ this ->db ->getQueryBuilder ();
4245
43- if ($ query ) {
44- $ qb ->select ('MAX(submissions.id) AS id, MAX(submissions.form_id) AS form_id, MAX(submissions.user_id) AS user_id, MAX(submissions.timestamp) AS timestamp ' )
45- ->join ('submissions ' , $ this ->answerMapper ->getTableName (), 'answers ' , $ qb ->expr ()->eq ('submissions.id ' , 'answers.submission_id ' ))
46- ->where ($ qb ->expr ()->like ('answers.text ' , $ qb ->createNamedParameter ('% ' . $ query . '% ' )))
47- ->groupBy ('submissions.id ' );
48- } else {
49- $ qb ->select ('* ' );
50- }
51-
52- $ qb ->from ($ this ->getTableName (), 'submissions ' )
53- ->andWhere (
54- $ qb ->expr ()->eq ('form_id ' , $ qb ->createNamedParameter ($ formId , IQueryBuilder::PARAM_INT ))
46+ // Select all columns from the submissions table
47+ $ qb ->select ('submissions.* ' )
48+ ->from ($ this ->getTableName (), 'submissions ' )
49+ ->where (
50+ $ qb ->expr ()->eq ('submissions.form_id ' , $ qb ->createNamedParameter ($ formId , IQueryBuilder::PARAM_INT ))
5551 )
56- //Newest submissions first
57- ->orderBy ('timestamp ' , 'DESC ' )
58- ->setMaxResults ($ limit )
59- ->setFirstResult ($ offset );
60-
61- //var_dump($qb->getSQL());exit;
52+ // Newest submissions first
53+ ->orderBy ('submissions.timestamp ' , 'DESC ' )
54+ ->setFirstResult ($ offset )
55+ ->setMaxResults ($ limit );
56+
57+ // If a query is provided, join the answers table and filter by the query text
58+ if ($ query !== null ) {
59+ $ qb ->join (
60+ 'submissions ' ,
61+ $ this ->answerMapper ->getTableName (),
62+ 'answers ' ,
63+ $ qb ->expr ()->eq ('submissions.id ' , 'answers.submission_id ' )
64+ )
65+ ->andWhere (
66+ $ qb ->expr ()->like ('answers.text ' , $ qb ->createNamedParameter ('% ' . $ query . '% ' ))
67+ );
68+ }
6269
6370 return $ this ->findEntities ($ qb );
6471 }
@@ -100,38 +107,50 @@ public function hasFormSubmissionsByUser(Form $form, string $userId): bool {
100107 }
101108
102109 /**
103- * Count submissions by form
104- * @param int $formId ID of the form to count submissions
105- * @throws \Exception
110+ * Counts the number of submissions associated with a specific form.
111+ *
112+ * @param int $formId The ID of the form for which submissions are to be counted.
113+ * @param ?string $searchString An optional search string to filter submissions by their answers.
114+ * @return int The total number of submissions for the specified form.
115+ * @throws \Exception If an error occurs during the count operation.
106116 */
107117 public function countSubmissions (int $ formId , ?string $ searchString = null ): int {
108118 return $ this ->countSubmissionsWithFilters ($ formId , null , -1 , $ searchString );
109119 }
110120
111121 /**
112- * Count submissions by form with optional filters
113- * @param int $formId ID of the form to count submissions
114- * @param string|null $userId optionally limit submissions to the one of that user
115- * @param int $limit allows to limit the query selection. If -1, the restriction is ignored
116- * @throws \Exception
122+ * Count submissions by form with optional filters.
123+ *
124+ * @param int $formId The ID of the form for which submissions are to be counted.
125+ * @param string|null $userId Optionally limit submissions to those made by the specified user.
126+ * @param int $limit The maximum number of submissions to count. If -1, no limit is applied.
127+ * @param string|null $searchString An optional search string to filter submissions by their answers.
128+ *
129+ * @return int The total number of submissions matching the specified filters.
130+ *
131+ * @throws \Exception If an error occurs during the count operation.
117132 */
118133 protected function countSubmissionsWithFilters (int $ formId , ?string $ userId = null , int $ limit = -1 , ?string $ searchString = null ): int {
119134 $ qb = $ this ->db ->getQueryBuilder ();
120135
121- if ($ searchString ) {
122- $ query = $ qb ->select (new QueryFunction ('COUNT(DISTINCT submissions.id) AS num_submissions ' ))
123- ->join ('submissions ' , $ this ->answerMapper ->getTableName (), 'answers ' , $ qb ->expr ()->eq ('submissions.id ' , 'answers.submission_id ' ))
124- ->andWhere ($ qb ->expr ()->like ('answers.text ' , $ qb ->createNamedParameter ('% ' . $ searchString . '% ' )));
125- } else {
126- $ query = $ qb ->select ($ qb ->func ()->count ('* ' , 'num_submissions ' ));
127- }
128-
129- $ query
136+ $ query = $ qb ->select ($ qb ->func ()->count ('submissions.id ' , 'num_submissions ' ))
130137 ->from ($ this ->getTableName (), 'submissions ' )
131- ->where ($ qb ->expr ()->eq ('form_id ' , $ qb ->createNamedParameter ($ formId , IQueryBuilder::PARAM_INT )));
138+ ->where ($ qb ->expr ()->eq ('submissions. form_id ' , $ qb ->createNamedParameter ($ formId , IQueryBuilder::PARAM_INT )));
132139
133140 if (!is_null ($ userId )) {
134- $ query ->andWhere ($ qb ->expr ()->eq ('user_id ' , $ qb ->createNamedParameter ($ userId , IQueryBuilder::PARAM_STR )));
141+ $ query ->andWhere ($ qb ->expr ()->eq ('submissions.user_id ' , $ qb ->createNamedParameter ($ userId , IQueryBuilder::PARAM_STR )));
142+ }
143+
144+ if (!is_null ($ searchString )) {
145+ $ query ->join (
146+ 'submissions ' ,
147+ $ this ->answerMapper ->getTableName (),
148+ 'answers ' ,
149+ $ qb ->expr ()->eq ('submissions.id ' , 'answers.submission_id ' )
150+ )
151+ ->andWhere (
152+ $ qb ->expr ()->like ('answers.text ' , $ qb ->createNamedParameter ('% ' . $ searchString . '% ' ))
153+ );
135154 }
136155
137156 if ($ limit !== -1 ) {
0 commit comments