Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| Paginator | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
56 | |
0.00% |
0 / 1 |
| paginate | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
56 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2017-2023 (original work) Open Assessment Technologies SA. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoClientDiagnostic\model\diagnostic; |
| 23 | |
| 24 | use oat\taoClientDiagnostic\model\storage\PaginatedStorage; |
| 25 | |
| 26 | /** |
| 27 | * Provides common data helper for datatable component. |
| 28 | */ |
| 29 | class Paginator |
| 30 | { |
| 31 | /** |
| 32 | * The default number of rows displayed in a page |
| 33 | */ |
| 34 | public const DEFAULT_ROWS = 25; |
| 35 | |
| 36 | /** |
| 37 | * The index of the default page |
| 38 | */ |
| 39 | public const DEFAULT_PAGE = 1; |
| 40 | |
| 41 | /** |
| 42 | * The index of the option providing the number of rows per page |
| 43 | */ |
| 44 | public const OPTION_ROWS = 'rows'; |
| 45 | |
| 46 | /** |
| 47 | * The index of the option providing the page number |
| 48 | */ |
| 49 | public const OPTION_PAGE = 'page'; |
| 50 | |
| 51 | /** |
| 52 | * The index of the option providing the page filter |
| 53 | */ |
| 54 | public const OPTION_FILTER = 'filter'; |
| 55 | |
| 56 | /** |
| 57 | * Paginates a collection to render a subset in a table |
| 58 | * |
| 59 | * @param array|PaginatedStorage $collection The full amount of lines to paginate |
| 60 | * @param ?array $options Allow to setup the page. These options are supported: |
| 61 | * - self::OPTION_ROWS : The number of rows per page |
| 62 | * - self::OPTION_PAGE : The index of the page to get |
| 63 | * @param ?function $dataRenderer An optional callback function provided to format the paginated data |
| 64 | * @return array |
| 65 | */ |
| 66 | public function paginate($collection, $options = array(), $dataRenderer = null) |
| 67 | { |
| 68 | $optRows = abs(intval(isset($options[self::OPTION_ROWS]) ? $options[self::OPTION_ROWS] : self::DEFAULT_ROWS)); |
| 69 | $optPage = abs(intval(isset($options[self::OPTION_PAGE]) ? $options[self::OPTION_PAGE] : self::DEFAULT_PAGE)); |
| 70 | $optFilter = isset($options[self::OPTION_FILTER]) ? $options[self::OPTION_FILTER] : null; |
| 71 | |
| 72 | if ($collection instanceof PaginatedStorage) { |
| 73 | $amount = $collection->count($optFilter); |
| 74 | $paginatedStorage = true; |
| 75 | } else { |
| 76 | $amount = count($collection); |
| 77 | $paginatedStorage = false; |
| 78 | } |
| 79 | |
| 80 | $rows = max(1, $optRows); |
| 81 | $total = ceil($amount / $rows); |
| 82 | $page = max(1, min($optPage, $total)); |
| 83 | $offset = ($page - 1) * $rows; |
| 84 | |
| 85 | $result = array( |
| 86 | 'offset' => $offset, |
| 87 | 'amount' => $amount, |
| 88 | 'total' => $total, |
| 89 | 'page' => $page, |
| 90 | 'rows' => $rows |
| 91 | ); |
| 92 | |
| 93 | if ($paginatedStorage) { |
| 94 | $result['data'] = $collection->findPage($page, $rows, $optFilter); |
| 95 | } else { |
| 96 | $result['data'] = array_slice($collection, $offset, $rows); |
| 97 | } |
| 98 | |
| 99 | if (is_callable($dataRenderer)) { |
| 100 | $result['data'] = $dataRenderer($result['data']); |
| 101 | } |
| 102 | $result['length'] = count($result['data']); |
| 103 | |
| 104 | return $result; |
| 105 | } |
| 106 | } |