Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
DataTableHelper
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
56
0.00% covered (danger)
0.00%
0 / 1
 paginate
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
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) 2015 (original work) Open Assessment Technologies SA ;
19 *
20 */
21
22namespace oat\taoProctoring\helpers;
23
24use oat\taoProctoring\model\PaginatedStorage;
25
26/**
27 * Provides common data helper for datatable component.
28 * @todo Move this class in taoCore, make an abstract/interface and its implementation
29 */
30class DataTableHelper
31{
32    /**
33     * The default number of rows displayed in a page
34     */
35    public const DEFAULT_ROWS = 25;
36
37    /**
38     * The index of the default page
39     */
40    public const DEFAULT_PAGE = 1;
41
42    /**
43     * The index of the option providing the number of rows per page
44     */
45    public const OPTION_ROWS = 'rows';
46
47    /**
48     * The index of the option providing the page number
49     */
50    public const OPTION_PAGE = 'page';
51
52    /**
53     * The index of the option providing the page filter
54     */
55    public const OPTION_FILTER = 'filter';
56
57    /**
58     * The keyword for an ascending sort
59     */
60    public const SORT_ASC = 'asc';
61
62    /**
63     * The keyword for a descending sort
64     */
65    public const SORT_DESC = 'desc';
66
67    /**
68     * The default sort order to use when none is provided
69     */
70    public const DEFAULT_SORT_ORDER = self::SORT_ASC;
71
72    /**
73     * Paginates a collection to render a subset in a table
74     * @param array|PaginatedStorage $collection - The full amount of lines to paginate
75     * @param array [$options] - Allow to setup the page. These options are supported:
76     * - self::OPTION_ROWS : The number of rows per page
77     * - self::OPTION_PAGE : The index of the page to get
78     * @param function [$dataRenderer] - An optional callback function provided to format the paginated data
79     * @return array
80     */
81    public static function paginate($collection, $options = array(), $dataRenderer = null)
82    {
83        $optRows = abs(intval(isset($options[self::OPTION_ROWS]) ? $options[self::OPTION_ROWS] : self::DEFAULT_ROWS));
84        $optPage = abs(intval(isset($options[self::OPTION_PAGE]) ? $options[self::OPTION_PAGE] : self::DEFAULT_PAGE));
85        $optFilter = isset($options[self::OPTION_FILTER]) ? $options[self::OPTION_FILTER] : null;
86
87        if ($collection instanceof PaginatedStorage) {
88            $amount = $collection->count($optFilter);
89            $paginatedStorage = true;
90        } else {
91            $amount = count($collection);
92            $paginatedStorage = false;
93        }
94
95        $rows = max(1, $optRows);
96        $total = ceil($amount / $rows);
97        $page = max(1, min($optPage, $total));
98        $offset = ($page - 1) * $rows;
99
100        $result = array(
101            'success' => true,
102            'offset' => $offset,
103            'amount' => $amount,
104            'total' => $total,
105            'page' => $page,
106            'rows' => $rows
107        );
108
109        if ($paginatedStorage) {
110            $result['data'] = $collection->findPage($page, $rows, $optFilter);
111        } else {
112            $result['data'] = array_slice($collection, $offset, $rows);
113        }
114
115        if (is_callable($dataRenderer)) {
116            $result['data'] = $dataRenderer($result['data']);
117        }
118        $result['length'] = count($result['data']);
119
120        return $result;
121    }
122}