77
88namespace OCA \Forms \Db ;
99
10+ use OC \DB \QueryBuilder \QueryFunction ;
1011use OCP \AppFramework \Db \DoesNotExistException ;
1112use OCP \AppFramework \Db \QBMapper ;
1213use OCP \DB \QueryBuilder \IQueryBuilder ;
@@ -30,19 +31,34 @@ public function __construct(
3031
3132 /**
3233 * @param int $formId
34+ * @param ?string $query
35+ * @param int $limit
36+ * @param int $offset
3337 * @throws DoesNotExistException if not found
3438 * @return Submission[]
3539 */
36- public function findByForm (int $ formId ): array {
40+ public function findByForm (int $ formId, ? string $ query = null , int $ limit = 20 , int $ offset = 0 ): array {
3741 $ qb = $ this ->db ->getQueryBuilder ();
3842
39- $ qb ->select ('* ' )
40- ->from ($ this ->getTableName ())
41- ->where (
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 (
4254 $ qb ->expr ()->eq ('form_id ' , $ qb ->createNamedParameter ($ formId , IQueryBuilder::PARAM_INT ))
4355 )
4456 //Newest submissions first
45- ->orderBy ('timestamp ' , 'DESC ' );
57+ ->orderBy ('timestamp ' , 'DESC ' )
58+ ->setMaxResults ($ limit )
59+ ->setFirstResult ($ offset );
60+
61+ //var_dump($qb->getSQL());exit;
4662
4763 return $ this ->findEntities ($ qb );
4864 }
@@ -88,8 +104,8 @@ public function hasFormSubmissionsByUser(Form $form, string $userId): bool {
88104 * @param int $formId ID of the form to count submissions
89105 * @throws \Exception
90106 */
91- public function countSubmissions (int $ formId ): int {
92- return $ this ->countSubmissionsWithFilters ($ formId , null , -1 );
107+ public function countSubmissions (int $ formId, ? string $ searchString = null ): int {
108+ return $ this ->countSubmissionsWithFilters ($ formId , null , -1 , $ searchString );
93109 }
94110
95111 /**
@@ -99,15 +115,25 @@ public function countSubmissions(int $formId): int {
99115 * @param int $limit allows to limit the query selection. If -1, the restriction is ignored
100116 * @throws \Exception
101117 */
102- protected function countSubmissionsWithFilters (int $ formId , ?string $ userId = null , int $ limit = -1 ): int {
118+ protected function countSubmissionsWithFilters (int $ formId , ?string $ userId = null , int $ limit = -1 , ? string $ searchString = null ): int {
103119 $ qb = $ this ->db ->getQueryBuilder ();
104120
105- $ query = $ qb ->select ($ qb ->func ()->count ('* ' , 'num_submissions ' ))
106- ->from ($ this ->getTableName ())
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
130+ ->from ($ this ->getTableName (), 'submissions ' )
107131 ->where ($ qb ->expr ()->eq ('form_id ' , $ qb ->createNamedParameter ($ formId , IQueryBuilder::PARAM_INT )));
132+
108133 if (!is_null ($ userId )) {
109134 $ query ->andWhere ($ qb ->expr ()->eq ('user_id ' , $ qb ->createNamedParameter ($ userId , IQueryBuilder::PARAM_STR )));
110135 }
136+
111137 if ($ limit !== -1 ) {
112138 $ query ->setMaxResults ($ limit );
113139 }
0 commit comments