@@ -35,13 +35,27 @@ public function __construct(
3535 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
3636 * @return Poll
3737 */
38- public function get (int $ id , bool $ getDeleted = false ): Poll {
38+ public function get (int $ id , bool $ getDeleted = false , bool $ withRoles = false ): Poll {
3939 $ qb = $ this ->db ->getQueryBuilder ();
40- $ qb ->select ('* ' )
41- ->from ($ this ->getTableName ())
42- ->where ($ qb ->expr ()->eq ('id ' , $ qb ->createNamedParameter ($ id , IQueryBuilder::PARAM_INT )));
40+ $ qb ->select (self ::TABLE . '.* ' )
41+ ->from ($ this ->getTableName (), self ::TABLE )
42+ ->where ($ qb ->expr ()->eq (self ::TABLE . '.id ' , $ qb ->createNamedParameter ($ id , IQueryBuilder::PARAM_INT )))
43+ ->groupBy (self ::TABLE . '.id ' );
44+
4345 if (!$ getDeleted ) {
44- $ qb ->andWhere ($ qb ->expr ()->eq ('deleted ' , $ qb ->expr ()->literal (0 , IQueryBuilder::PARAM_INT )));
46+ $ qb ->andWhere ($ qb ->expr ()->eq (self ::TABLE . '.deleted ' , $ qb ->expr ()->literal (0 , IQueryBuilder::PARAM_INT )));
47+ }
48+
49+ if ($ withRoles ) {
50+ $ pollGroupsAlias = 'poll_groups ' ;
51+ $ currentUserId = $ this ->userSession ->getCurrentUserId ();
52+ // $this->joinOptions($qb, self::TABLE);
53+ $ this ->joinUserRole ($ qb , self ::TABLE , $ currentUserId );
54+ $ this ->joinGroupShares ($ qb , self ::TABLE );
55+ $ this ->joinPollGroups ($ qb , self ::TABLE , $ pollGroupsAlias );
56+ $ this ->joinPollGroupShares ($ qb , $ pollGroupsAlias , $ currentUserId , $ pollGroupsAlias );
57+ // $this->joinVotesCount($qb, self::TABLE, $currentUserId);
58+ // $this->joinParticipantsCount($qb, self::TABLE);
4559 }
4660 return $ this ->findEntity ($ qb );
4761 }
@@ -184,19 +198,19 @@ public function deleteByUserId(string $userId): void {
184198 * Build the enhanced query with joined tables
185199 */
186200 protected function buildQuery (): IQueryBuilder {
187- $ currentUserId = $ this ->userSession ->getCurrentUserId ();
188201 $ qb = $ this ->db ->getQueryBuilder ();
189202
190203 $ qb ->select (self ::TABLE . '.* ' )
191204 ->from ($ this ->getTableName (), self ::TABLE )
192205 ->groupBy (self ::TABLE . '.id ' );
193206
207+ $ currentUserId = $ this ->userSession ->getCurrentUserId ();
194208 $ pollGroupsAlias = 'poll_groups ' ;
195209 $ this ->joinOptions ($ qb , self ::TABLE );
196210 $ this ->joinUserRole ($ qb , self ::TABLE , $ currentUserId );
197211 $ this ->joinGroupShares ($ qb , self ::TABLE );
198212 $ this ->joinPollGroups ($ qb , self ::TABLE , $ pollGroupsAlias );
199- $ this ->joinUserSharesfromPollGroups ($ qb , $ pollGroupsAlias , $ currentUserId , $ pollGroupsAlias );
213+ $ this ->joinPollGroupShares ($ qb , $ pollGroupsAlias , $ currentUserId , $ pollGroupsAlias );
200214 $ this ->joinVotesCount ($ qb , self ::TABLE , $ currentUserId );
201215 $ this ->joinParticipantsCount ($ qb , self ::TABLE );
202216 return $ qb ;
@@ -299,7 +313,7 @@ protected function joinPollGroups(
299313 * Supported share types are User and Admin
300314 * Groups, Teams will not work atm.
301315 */
302- protected function joinUserSharesfromPollGroups (
316+ protected function joinPollGroupShares (
303317 IQueryBuilder $ qb ,
304318 string $ fromAlias ,
305319 string $ currentUserId ,
@@ -338,13 +352,16 @@ protected function joinOptions(
338352 string $ fromAlias ,
339353 string $ joinAlias = 'options ' ,
340354 ): void {
355+ // add highest option date
356+ $ qb ->addSelect ($ qb ->createFunction ('MAX( ' . $ joinAlias . '.timestamp) AS max_date ' ));
357+
358+ // add lowest option date
359+ $ qb ->addSelect ($ qb ->createFunction ('MIN( ' . $ joinAlias . '.timestamp) AS min_date ' ));
341360
342- $ zero = $ qb -> expr ()-> literal ( 0 , IQueryBuilder:: PARAM_INT );
343- $ saveMin = $ qb ->createNamedParameter ( time (), IQueryBuilder:: PARAM_INT );
361+ // add number of options with an owner (results in number of proposals)
362+ $ qb -> addSelect ( $ qb ->createFunction ( ' COUNT(DISTINCT(CASE WHEN ' . $ joinAlias . ' .owner != \'\' THEN 1 END)) AS proposals_count ' ) );
344363
345- $ qb ->addSelect ($ qb ->createFunction ('coalesce(MAX( ' . $ joinAlias . '.timestamp), ' . $ zero . ') AS max_date ' ))
346- ->addSelect ($ qb ->createFunction ('coalesce(MIN( ' . $ joinAlias . '.timestamp), ' . $ saveMin . ') AS min_date ' ))
347- ->addSelect ($ qb ->createFunction ('COUNT(DISTINCT(CASE WHEN ' . $ joinAlias . '.owner != \'\' THEN 1 END)) AS proposals_count ' ));
364+ // count number of options by counting unique ids
348365 $ qb ->selectAlias ($ qb ->func ()->count ($ joinAlias . '.id ' ), 'optionsCount ' );
349366
350367 $ qb ->leftJoin (
0 commit comments