Skip to content

Commit

Permalink
Fixed issue #QE-769: REST API survey list pagination (#3722)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-foster-uk committed Feb 5, 2024
1 parent 16225b5 commit 05c0ce6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
6 changes: 5 additions & 1 deletion application/libraries/Api/Command/V1/SurveyList.php
Expand Up @@ -62,7 +62,11 @@ public function run(Request $request)

$dataProvider = $this->survey
->with('defaultlanguage')
->search();
->search([
'pageSize' => $request->getData('pageSize'),
// Yii pagination is zero based - so we must add 1
'currentPage' => $request->getData('page') + 1,
]);

$data = $this->transformerOutputSurvey
->transformAll($dataProvider->getData());
Expand Down
16 changes: 14 additions & 2 deletions application/models/Survey.php
Expand Up @@ -1508,15 +1508,26 @@ public function getButtons(): string
}

/**
* Search
*
* $options = [
* 'pageSize' => 10,
* 'currentPage' => 1
* ];
*
* @param array $options
* @return CActiveDataProvider
*/
public function search()
public function search($options = [])
{
$options = $options ?? [];
// Flush cache to get proper counts for partial/complete/total responses
if (method_exists(Yii::app()->cache, 'flush')) {
Yii::app()->cache->flush();
}
$pageSize = Yii::app()->user->getState('pageSize', Yii::app()->params['defaultPageSize']);
$pageSizeUser = Yii::app()->user->getState('pageSize', Yii::app()->params['defaultPageSize']);
$pageSize = $options['pageSize'] ?? $pageSizeUser;
$currentPage = $options['currentPage'] ?? null;

$sort = new CSort();
$sort->attributes = array(
Expand Down Expand Up @@ -1644,6 +1655,7 @@ public function search()
'criteria' => $criteria,
'pagination' => array(
'pageSize' => $pageSize,
'currentPage' => $currentPage
),
));

Expand Down

0 comments on commit 05c0ce6

Please sign in to comment.