Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 34 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| TreeResourceLookup | |
0.00% |
0 / 34 |
|
0.00% |
0 / 3 |
110 | |
0.00% |
0 / 1 |
| getResources | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| getClasses | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| formatTreeData | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
20 | |||
| 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\tao\model\resources; |
| 23 | |
| 24 | use oat\oatbox\service\ConfigurableService; |
| 25 | use oat\tao\model\GenerisTreeFactory; |
| 26 | use oat\tao\helpers\TreeHelper; |
| 27 | |
| 28 | /** |
| 29 | * Look up resources and format them as a tree hierarchy |
| 30 | * |
| 31 | * @author Bertrand Chevrier <bertrand@taotesting.com> |
| 32 | */ |
| 33 | class TreeResourceLookup extends ConfigurableService implements ResourceLookup |
| 34 | { |
| 35 | public const SERVICE_ID = 'tao/TreeResourceLookup'; |
| 36 | |
| 37 | /** |
| 38 | * Retrieve Resources in their hierarchy, for the given parameters as format them as tree. |
| 39 | * |
| 40 | * @param \core_kernel_classes_Class $rootClass the resources class |
| 41 | * @param array $propertyFilters propUri/propValue to search resources |
| 42 | * @param string[] $selectedUris the resources to open |
| 43 | * @param int $offset for paging |
| 44 | * @param int $limit for paging |
| 45 | * @return array the resources |
| 46 | */ |
| 47 | public function getResources( |
| 48 | \core_kernel_classes_Class $rootClass, |
| 49 | array $selectedUris = [], |
| 50 | array $propertyFilters = [], |
| 51 | $offset = 0, |
| 52 | $limit = 30 |
| 53 | ) { |
| 54 | $openNodes = []; |
| 55 | if (count($selectedUris) > 0) { |
| 56 | $openNodes = TreeHelper::getNodesToOpen($selectedUris, $rootClass); |
| 57 | } |
| 58 | if (!in_array($rootClass->getUri(), $openNodes)) { |
| 59 | $openNodes[] = $rootClass->getUri(); |
| 60 | } |
| 61 | $factory = new GenerisTreeFactory(true, $openNodes, $limit, $offset, $selectedUris, $propertyFilters); |
| 62 | $treeData = $factory->buildTree($rootClass); |
| 63 | |
| 64 | return $this->formatTreeData([$treeData]); |
| 65 | } |
| 66 | |
| 67 | public function getClasses( |
| 68 | \core_kernel_classes_Class $rootClass, |
| 69 | array $selectedUris = [], |
| 70 | array $propertyFilters = [], |
| 71 | $offset = 0, |
| 72 | $limit = 30 |
| 73 | ) { |
| 74 | $openNodes = []; |
| 75 | if (count($selectedUris) > 0) { |
| 76 | $openNodes = TreeHelper::getNodesToOpen($selectedUris, $rootClass); |
| 77 | } |
| 78 | if (!in_array($rootClass->getUri(), $openNodes)) { |
| 79 | $openNodes[] = $rootClass->getUri(); |
| 80 | } |
| 81 | $factory = new GenerisTreeFactory(false, $openNodes, $limit, $offset, $selectedUris, $propertyFilters); |
| 82 | $treeData = $factory->buildTree($rootClass); |
| 83 | |
| 84 | return $this->formatTreeData([$treeData]); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Reformat the the tree : state and count |
| 89 | * Add the resource's categories |
| 90 | * @param array $treeData |
| 91 | * @return array the formated data |
| 92 | */ |
| 93 | private function formatTreeData(array $treeData) |
| 94 | { |
| 95 | return array_map(function ($data) { |
| 96 | |
| 97 | $formated = [ |
| 98 | 'label' => $data['data'], |
| 99 | 'type' => $data['type'], |
| 100 | 'uri' => $data['attributes']['data-uri'], |
| 101 | 'classUri' => $data['attributes']['data-classUri'], |
| 102 | 'signature' => $data['attributes']['data-signature'], |
| 103 | 'state' => isset($data['state']) ? $data['state'] : false, |
| 104 | 'count' => isset($data['count']) ? $data['count'] : 0 |
| 105 | ]; |
| 106 | if (isset($data['children'])) { |
| 107 | $formated['children'] = $this->formatTreeData($data['children']); |
| 108 | } |
| 109 | return $formated; |
| 110 | }, $treeData); |
| 111 | } |
| 112 | } |