Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 49 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| TreeHelper | |
0.00% |
0 / 49 |
|
0.00% |
0 / 2 |
182 | |
0.00% |
0 / 1 |
| getNodesToOpen | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
90 | |||
| buildResourceNode | |
0.00% |
0 / 19 |
|
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) 2015 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\helpers; |
| 23 | |
| 24 | use core_kernel_classes_Class; |
| 25 | use core_kernel_classes_Resource; |
| 26 | use oat\generis\model\OntologyRdfs; |
| 27 | use oat\oatbox\service\ServiceManager; |
| 28 | use oat\tao\model\security\SignatureGenerator; |
| 29 | use tao_helpers_Uri; |
| 30 | |
| 31 | /** |
| 32 | * Utility class to support building tree rendering component |
| 33 | */ |
| 34 | class TreeHelper |
| 35 | { |
| 36 | /** |
| 37 | * Returns the nodes to open in order to display |
| 38 | * all the listed resources to be visible |
| 39 | * |
| 40 | * @param array $uris list of resources to show |
| 41 | * @param core_kernel_classes_Class $rootNode root node of the tree |
| 42 | * @return array array of the uris of the nodes to open |
| 43 | */ |
| 44 | public static function getNodesToOpen($uris, core_kernel_classes_Class $rootNode) |
| 45 | { |
| 46 | // this array is in the form of |
| 47 | // URI to test => array of uris that depend on the URI |
| 48 | $toTest = []; |
| 49 | foreach ($uris as $uri) { |
| 50 | $resource = new core_kernel_classes_Resource($uri); |
| 51 | foreach ($resource->getTypes() as $type) { |
| 52 | $toTest[$type->getUri()] = []; |
| 53 | } |
| 54 | |
| 55 | $subClassProp = $resource->getOnePropertyValue( |
| 56 | $resource->getModel()->getProperty(OntologyRdfs::RDFS_SUBCLASSOF) |
| 57 | ); |
| 58 | if ($subClassProp !== null) { |
| 59 | $toTest[$subClassProp->getUri()] = []; |
| 60 | } |
| 61 | } |
| 62 | $toOpen = [$rootNode->getUri()]; |
| 63 | while (!empty($toTest)) { |
| 64 | reset($toTest); |
| 65 | $classUri = key($toTest); |
| 66 | $depends = current($toTest); |
| 67 | unset($toTest[$classUri]); |
| 68 | if (in_array($classUri, $toOpen)) { |
| 69 | $toOpen = array_merge($toOpen, $depends); |
| 70 | } else { |
| 71 | $class = new core_kernel_classes_Class($classUri); |
| 72 | /** @var core_kernel_classes_Class $parent */ |
| 73 | foreach ($class->getParentClasses(false) as $parent) { |
| 74 | if ($parent->getUri() === OntologyRdfs::RDFS_CLASS) { |
| 75 | continue; |
| 76 | } |
| 77 | if (!isset($toTest[$parent->getUri()])) { |
| 78 | $toTest[$parent->getUri()] = []; |
| 79 | } |
| 80 | $toTest[$parent->getUri()] = array_merge( |
| 81 | $toTest[$parent->getUri()], |
| 82 | [$classUri], |
| 83 | $depends |
| 84 | ); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | return $toOpen; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * generis tree representation of a resource node |
| 93 | * |
| 94 | * @param core_kernel_classes_Resource $resource |
| 95 | * @param core_kernel_classes_Class $class |
| 96 | * @param array $extraProperties |
| 97 | * @return array |
| 98 | */ |
| 99 | public static function buildResourceNode( |
| 100 | core_kernel_classes_Resource $resource, |
| 101 | core_kernel_classes_Class $class, |
| 102 | array $extraProperties = [] |
| 103 | ) { |
| 104 | $label = $resource->getLabel(); |
| 105 | $label = empty($label) ? __('no label') : $label; |
| 106 | |
| 107 | $extraValues = []; |
| 108 | if (!empty($extraProperties)) { |
| 109 | foreach ($extraProperties as $key => $value) { |
| 110 | $extraValues[$key] = $resource->getOnePropertyValue($class->getProperty($value)); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | $signatureGenerator = ServiceManager::getServiceManager()->get(SignatureGenerator::class); |
| 115 | |
| 116 | return [ |
| 117 | 'data' => _dh($label), |
| 118 | 'type' => 'instance', |
| 119 | 'attributes' => array_merge([ |
| 120 | 'id' => tao_helpers_Uri::encode($resource->getUri()), |
| 121 | 'class' => 'node-instance', |
| 122 | 'data-uri' => $resource->getUri(), |
| 123 | 'data-classUri' => $class->getUri(), |
| 124 | 'data-signature' => $signatureGenerator->generate($resource->getUri()), |
| 125 | ], $extraValues) |
| 126 | ]; |
| 127 | } |
| 128 | } |