Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 40 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| DataTablePayload | |
0.00% |
0 / 40 |
|
0.00% |
0 / 7 |
182 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| customiseRowBy | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getPayload | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
6 | |||
| getCustomisedData | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| count | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| applyDataTableFilters | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| 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\taoTaskQueue\model\TaskLog; |
| 23 | |
| 24 | use oat\tao\model\datatable\DatatablePayload as DataTablePayloadInterface; |
| 25 | use oat\tao\model\datatable\implementation\DatatableRequest; |
| 26 | use oat\taoTaskQueue\model\TaskLogBroker\TaskLogBrokerInterface; |
| 27 | |
| 28 | /** |
| 29 | * Helper class for handling js datatable request. |
| 30 | * |
| 31 | * @deprecated Use \oat\tao\model\taskQueue\TaskLog\DataTablePayload |
| 32 | * |
| 33 | * @author Gyula Szucs <gyula@taotesting.com> |
| 34 | */ |
| 35 | class DataTablePayload implements DataTablePayloadInterface, \Countable |
| 36 | { |
| 37 | private $taskLogFilter; |
| 38 | private $broker; |
| 39 | private $request; |
| 40 | |
| 41 | /** |
| 42 | * @var \Closure |
| 43 | */ |
| 44 | private $rowCustomiser; |
| 45 | |
| 46 | /** |
| 47 | * DataTablePayload constructor. |
| 48 | * |
| 49 | * @param TaskLogFilter $filter |
| 50 | * @param TaskLogBrokerInterface $broker |
| 51 | */ |
| 52 | public function __construct(TaskLogFilter $filter, TaskLogBrokerInterface $broker) |
| 53 | { |
| 54 | $this->taskLogFilter = $filter; |
| 55 | $this->broker = $broker; |
| 56 | $this->request = DatatableRequest::fromGlobals(); |
| 57 | |
| 58 | $this->applyDataTableFilters(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * You can pass an anonymous function to customise the final payload: either to change the value of a field or to |
| 63 | * add extra field(s); |
| 64 | * |
| 65 | * The function will be bind to the task log entity (TaskLogEntity) so $this can be used inside of the closure. |
| 66 | * The return value needs to be an array. |
| 67 | * |
| 68 | * For example: |
| 69 | * <code> |
| 70 | * $payload->customiseRowBy(function (){ |
| 71 | * $row['extraField'] = 'value'; |
| 72 | * $row['extraField2'] = $this->getParameters()['some_parameter_key']; |
| 73 | * $row['createdAt'] = \tao_helpers_Date::displayeDate($this->getCreatedAt()); |
| 74 | * |
| 75 | * return $row; |
| 76 | * }); |
| 77 | * </code> |
| 78 | * |
| 79 | * @param \Closure $func |
| 80 | * @return DataTablePayload |
| 81 | */ |
| 82 | public function customiseRowBy(\Closure $func) |
| 83 | { |
| 84 | $this->rowCustomiser = $func; |
| 85 | |
| 86 | return $this; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @return array |
| 91 | */ |
| 92 | public function getPayload() |
| 93 | { |
| 94 | $countTotal = $this->count(); |
| 95 | |
| 96 | $page = $this->request->getPage(); |
| 97 | $limit = $this->request->getRows(); |
| 98 | |
| 99 | // we don't want to "pollute" the original filter |
| 100 | $cloneFilter = clone $this->taskLogFilter; |
| 101 | |
| 102 | $cloneFilter->setLimit($limit) |
| 103 | ->setOffset($limit * ($page - 1)) |
| 104 | ->setSortBy($this->request->getSortBy()) |
| 105 | ->setSortOrder($this->request->getSortOrder()); |
| 106 | |
| 107 | // get task log entities by filters |
| 108 | $collection = $this->broker->search($cloneFilter); |
| 109 | |
| 110 | // get customised data |
| 111 | $customisedData = $this->getCustomisedData($collection); |
| 112 | |
| 113 | $data = [ |
| 114 | 'rows' => $limit, |
| 115 | 'page' => $page, |
| 116 | 'amount' => count($collection), |
| 117 | 'total' => ceil($countTotal / $limit), |
| 118 | 'data' => $customisedData ?: $collection->toArray(), |
| 119 | ]; |
| 120 | |
| 121 | return $data; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Get customised data if we have a customiser set |
| 126 | * |
| 127 | * @param TaskLogCollection $collection |
| 128 | * @return array |
| 129 | */ |
| 130 | private function getCustomisedData(TaskLogCollection $collection) |
| 131 | { |
| 132 | $data = []; |
| 133 | |
| 134 | if (!is_null($this->rowCustomiser)) { |
| 135 | foreach ($collection as $taskLogEntity) { |
| 136 | $newCustomiser = $this->rowCustomiser->bindTo($taskLogEntity, $taskLogEntity); |
| 137 | |
| 138 | $data[] = array_merge($taskLogEntity->toArray(), (array) $newCustomiser()); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | return $data; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @return int |
| 147 | */ |
| 148 | public function count() |
| 149 | { |
| 150 | return $this->broker->count($this->taskLogFilter); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Add filter values from request to the taskLogFilter. |
| 155 | */ |
| 156 | private function applyDataTableFilters() |
| 157 | { |
| 158 | $filters = $this->request->getFilters(); |
| 159 | |
| 160 | foreach ($filters as $fieldName => $filterValue) { |
| 161 | if (empty($filterValue)) { |
| 162 | continue; |
| 163 | } |
| 164 | |
| 165 | if (is_array($filterValue)) { |
| 166 | $this->taskLogFilter->in($fieldName, $filterValue); |
| 167 | continue; |
| 168 | } |
| 169 | |
| 170 | $this->taskLogFilter->eq($fieldName, (string) $filterValue); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * @return array |
| 176 | */ |
| 177 | public function jsonSerialize() |
| 178 | { |
| 179 | return $this->getPayload(); |
| 180 | } |
| 181 | } |