Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
TreeService
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 5
210
0.00% covered (danger)
0.00%
0 / 1
 getRootClass
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFlatStructure
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
20
 getNestedStructure
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
20
 getTrees
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 createTree
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
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) 2014-2023 (original work) Open Assessment Technologies SA.
19 */
20
21namespace oat\taoBackOffice\model\tree;
22
23use tao_helpers_Uri;
24use tao_models_classes_ClassService;
25use core_kernel_classes_Class;
26use core_kernel_classes_Property;
27
28/**
29 * Class TreeService
30 */
31class TreeService extends tao_models_classes_ClassService
32{
33    public const CLASS_URI = 'http://www.tao.lu/Ontologies/TAO.rdf#Tree';
34
35    public const PROPERTY_CHILD_OF = 'http://www.tao.lu/Ontologies/TAO.rdf#isChildOf';
36
37    public function getRootClass()
38    {
39        return new core_kernel_classes_Class(self::CLASS_URI);
40    }
41
42    /**
43     * used to build visjs visualization
44     *
45     * @param core_kernel_classes_Class $tree
46     * @param callable|null $labelProcessor
47     *
48     * @return array
49     */
50    public function getFlatStructure(core_kernel_classes_Class $tree, $labelProcessor = null)
51    {
52        $returnValue = [
53            'nodes' => [],
54            'edges' => []
55        ];
56
57        $childOf = new \core_kernel_classes_Property(self::PROPERTY_CHILD_OF);
58        foreach ($tree->getInstances() as $node) {
59            $returnValue['nodes'][] = [
60                'id'    => $node->getUri(),
61                'label' => is_callable($labelProcessor) ? $labelProcessor($node->getLabel()) : $node->getLabel(),
62            ];
63            foreach ($node->getPropertyValues($childOf) as $childUri) {
64                $returnValue['edges'][] = [
65                    'from' => $childUri,
66                    'to'   => $node->getUri()
67                ];
68            }
69        }
70
71        return $returnValue;
72    }
73
74    /**
75     * Used to build jsTree visualization widget
76     *
77     * @param array $nodes
78     *
79     * @return array
80     */
81    public function getNestedStructure(array $nodes)
82    {
83        $childOf = new \core_kernel_classes_Property(self::PROPERTY_CHILD_OF);
84        $tmpTree = [];
85
86        foreach ($nodes as $node) {
87            $nodeData = [
88                'data'       => $node->getLabel(),
89                'parent'     => 0,
90                'attributes' => [
91                    'id'    => tao_helpers_Uri::encode($node->getUri()),
92                    'class' => 'node-instance',
93                ]
94            ];
95
96            foreach ($node->getPropertyValues($childOf) as $childUri) {
97                $nodeData['parent'] = tao_helpers_Uri::encode($childUri);
98            }
99
100            if (isset($nodeData['parent'])) {
101                $tmpTree[$nodeData['parent']][] = $nodeData;
102            }
103        }
104
105        $tree = self::createTree($tmpTree, array_shift($tmpTree));
106
107        return $tree;
108    }
109
110    /**
111     * get all the tree classes
112     *
113     * @return array
114     */
115    public function getTrees()
116    {
117        $returnValue = [];
118
119        foreach ($this->getRootClass()->getSubClasses(false) as $tree) {
120            $returnValue[] = $tree;
121        }
122
123        return $returnValue;
124    }
125
126    /**
127     * @param $list
128     * @param $parent
129     *
130     * @return array
131     */
132    protected static function createTree($list, $parent)
133    {
134        $tree = [];
135        foreach ($parent as $k => $node) {
136            if (isset($list[$node['attributes']['id']])) {
137                $node['children'] = self::createTree($list, $list[$node['attributes']['id']]);
138                $node['state']    = 'open';
139            }
140            $tree[] = $node;
141        }
142
143        return $tree;
144    }
145}