Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 23 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| ResultsPayload | |
0.00% |
0 / 23 |
|
0.00% |
0 / 6 |
42 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getPayload | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| doPostProcessing | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
| limitRows | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFilters | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| jsonSerialize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 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 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoOutcomeUi\model\table; |
| 23 | |
| 24 | use oat\tao\model\datatable\DatatablePayload as DataTablePayloadInterface; |
| 25 | use oat\tao\model\datatable\implementation\DatatableRequest; |
| 26 | use oat\taoOutcomeUi\model\export\ResultsExporterInterface; |
| 27 | use oat\taoOutcomeUi\model\ResultsService; |
| 28 | |
| 29 | /** |
| 30 | * ResultsPayload |
| 31 | * |
| 32 | * @author Gyula Szucs <gyula@taotesting.com> |
| 33 | */ |
| 34 | class ResultsPayload implements DataTablePayloadInterface |
| 35 | { |
| 36 | private $request; |
| 37 | |
| 38 | /** |
| 39 | * @var ResultsExporterInterface |
| 40 | */ |
| 41 | private $exporter; |
| 42 | |
| 43 | public function __construct(ResultsExporterInterface $exporter) |
| 44 | { |
| 45 | $this->request = DatatableRequest::fromGlobals(); |
| 46 | $this->exporter = $exporter; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @return array |
| 51 | */ |
| 52 | public function getPayload() |
| 53 | { |
| 54 | $data = $this->exporter->getData(); |
| 55 | return $this->doPostProcessing($data); |
| 56 | } |
| 57 | |
| 58 | private function doPostProcessing($data) |
| 59 | { |
| 60 | $countTotal = count($data); |
| 61 | $filters = $this->getFilters(); |
| 62 | $this->limitRows($data, $filters); |
| 63 | |
| 64 | $payload = [ |
| 65 | 'data' => $data, |
| 66 | 'page' => $this->request->getPage(), |
| 67 | 'amount' => count($data), |
| 68 | 'total' => ceil($countTotal / $filters['limit']) |
| 69 | ]; |
| 70 | |
| 71 | return $payload; |
| 72 | } |
| 73 | |
| 74 | private function limitRows(&$data, $filters) |
| 75 | { |
| 76 | $data = array_slice($data, $filters['offset'], $filters['limit']); |
| 77 | } |
| 78 | |
| 79 | protected function getFilters() |
| 80 | { |
| 81 | $page = $this->request->getPage(); |
| 82 | $limit = $this->request->getRows(); |
| 83 | |
| 84 | $filters = [ |
| 85 | 'offset' => $limit * ($page - 1), |
| 86 | 'limit' => $limit |
| 87 | ]; |
| 88 | |
| 89 | return $filters; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @return array |
| 94 | */ |
| 95 | public function jsonSerialize() |
| 96 | { |
| 97 | return $this->getPayload(); |
| 98 | } |
| 99 | } |