Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
88.46% |
23 / 26 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ListItemLookup | |
88.46% |
23 / 26 |
|
66.67% |
2 / 3 |
6.06 | |
0.00% |
0 / 1 |
| getItems | |
87.50% |
21 / 24 |
|
0.00% |
0 / 1 |
4.03 | |||
| getListResourceLookupService | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getCategoryService | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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\taoQtiTest\models\creator; |
| 23 | |
| 24 | use common_exception_Error; |
| 25 | use core_kernel_classes_Class; |
| 26 | use oat\generis\model\OntologyAwareTrait; |
| 27 | use oat\oatbox\service\ConfigurableService; |
| 28 | use oat\tao\model\resources\ListResourceLookup; |
| 29 | use oat\taoItems\model\CategoryService; |
| 30 | |
| 31 | /** |
| 32 | * Look up items and format them as a flat list |
| 33 | * |
| 34 | * @author Bertrand Chevrier <bertrand@taotesting.com> |
| 35 | */ |
| 36 | class ListItemLookup extends ConfigurableService implements ItemLookup |
| 37 | { |
| 38 | use OntologyAwareTrait; |
| 39 | use PermissionLookupTrait; |
| 40 | |
| 41 | public const SERVICE_ID = 'taoQtiTest/CreatorItems/list'; |
| 42 | |
| 43 | /** |
| 44 | * Retrieve QTI Items for the given parameters. |
| 45 | * |
| 46 | * @param core_kernel_classes_Class $itemClass the item class |
| 47 | * @param array $propertyFilters the lookup format |
| 48 | * @param int $offset for paging |
| 49 | * @param int $limit for paging |
| 50 | * |
| 51 | * @return array the items |
| 52 | * @throws common_exception_Error |
| 53 | */ |
| 54 | public function getItems( |
| 55 | core_kernel_classes_Class $itemClass, |
| 56 | array $propertyFilters = [], |
| 57 | $offset = 0, |
| 58 | $limit = 30 |
| 59 | ): array { |
| 60 | $result = $this->getListResourceLookupService()->getResources( |
| 61 | $itemClass, |
| 62 | [], |
| 63 | $propertyFilters, |
| 64 | $offset, |
| 65 | $limit |
| 66 | ); |
| 67 | |
| 68 | if (empty($result['nodes'])) { |
| 69 | return $result; |
| 70 | } |
| 71 | |
| 72 | $nodeIds = array_map( |
| 73 | static function (array $node): string { |
| 74 | return $node['uri']; |
| 75 | }, |
| 76 | $result['nodes'] |
| 77 | ); |
| 78 | |
| 79 | foreach ($result['nodes'] as $i => &$node) { |
| 80 | if (!in_array($node['uri'], $nodeIds, true)) { |
| 81 | unset($result['nodes'][$i]); |
| 82 | $result['total']--; |
| 83 | |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | $node['categories'] = $this->getCategoryService()->getItemCategories($this->getResource($node['uri'])); |
| 88 | } |
| 89 | unset($node); |
| 90 | |
| 91 | $result['nodes'] = $this->fillPermissions($result['nodes']); |
| 92 | return $result; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get the ListResourceLookup |
| 97 | */ |
| 98 | protected function getListResourceLookupService() |
| 99 | { |
| 100 | return $this->getServiceLocator()->get(ListResourceLookup::SERVICE_ID); |
| 101 | } |
| 102 | |
| 103 | private function getCategoryService(): CategoryService |
| 104 | { |
| 105 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
| 106 | return $this->getServiceLocator()->get(CategoryService::SERVICE_ID); |
| 107 | } |
| 108 | } |