Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
TreeItemLookup
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
4 / 4
7
100.00% covered (success)
100.00%
1 / 1
 getCategoryService
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTreeResourceLookupService
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getItems
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 formatTreeData
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
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
22namespace oat\taoQtiTest\models\creator;
23
24use common_exception_Error;
25use core_kernel_classes_Class;
26use core_kernel_classes_Resource;
27use oat\oatbox\service\ConfigurableService;
28use oat\tao\model\resources\ResourceLookup;
29use oat\tao\model\resources\TreeResourceLookup;
30use oat\taoItems\model\CategoryService;
31
32/**
33 * Look up items and format them as a tree hierarchy
34 *
35 * @author Bertrand Chevrier <bertrand@taotesting.com>
36 */
37class TreeItemLookup extends ConfigurableService implements ItemLookup
38{
39    use PermissionLookupTrait;
40
41    public const SERVICE_ID = 'taoQtiTest/CreatorItems/tree';
42
43    public function getCategoryService(): CategoryService
44    {
45        /** @noinspection PhpIncompatibleReturnTypeInspection */
46        return $this->getServiceLocator()->get(CategoryService::SERVICE_ID);
47    }
48
49    public function getTreeResourceLookupService(): ResourceLookup
50    {
51        /** @noinspection PhpIncompatibleReturnTypeInspection */
52        return $this->getServiceLocator()->get(TreeResourceLookup::SERVICE_ID);
53    }
54
55    /**
56     * Retrieve QTI Items in their hierarchy, for the given parameters as format them as tree.
57     *
58     * @param core_kernel_classes_Class $itemClass       the item class
59     * @param array                     $propertyFilters propUri/propValue to search items
60     * @param int                       $offset          for paging
61     * @param int                       $limit           for paging
62     *
63     * @return array the items
64     *
65     * @throws common_exception_Error
66     */
67    public function getItems(
68        core_kernel_classes_Class $itemClass,
69        array $propertyFilters = [],
70        $offset = 0,
71        $limit = 30
72    ): array {
73        $data = $this->getTreeResourceLookupService()->getResources($itemClass, [], $propertyFilters, $offset, $limit);
74        $nodes =  $this->formatTreeData($data);
75        $nodes = $this->fillPermissions($nodes);
76        return $nodes;
77    }
78
79    /**
80     * Reformat the the tree : state and count
81     * Add the item's categories
82     *
83     * @param array $treeData
84     *
85     * @return array the formatted data
86     *
87     * @throws common_exception_Error
88     */
89    private function formatTreeData(array $treeData): array
90    {
91        foreach ($treeData as &$item) {
92            if ($item['type'] === 'instance') {
93                $item['categories'] = $this->getCategoryService()->getItemCategories(
94                    new core_kernel_classes_Resource($item['uri'])
95                );
96            } elseif (isset($item['children'])) {
97                $item['children'] = $this->formatTreeData($item['children']);
98            }
99        }
100        return $treeData;
101    }
102}