Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 94
0.00% covered (danger)
0.00%
0 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
OntologyFormDataProvider
0.00% covered (danger)
0.00%
0 / 94
0.00% covered (danger)
0.00%
0 / 12
1056
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 preloadFormData
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getClassProperties
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDataToFeedProperty
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 getDescriptionFromTranslatedPropertyLabel
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 getPropertyListElementOptions
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 getPropertyNotListElementOptions
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
20
 getPropertyValidators
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPropertyInstanceValues
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
56
 isPropertyList
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 getPropertyGUIOrder
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getListValues
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
42
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) 2023-2024 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
19 *
20 */
21
22namespace oat\tao\model\form\DataProvider;
23
24use core_kernel_classes_Class;
25use core_kernel_classes_Literal;
26use core_kernel_classes_Property;
27use core_kernel_classes_Resource;
28use oat\generis\model\OntologyAwareTrait;
29use oat\generis\model\OntologyRdfs;
30use oat\tao\helpers\form\ValidationRuleRegistry;
31use oat\tao\model\Language\Business\Specification\LanguageClassSpecification;
32use oat\tao\model\Language\Filter\LanguageAllowedFilter;
33use oat\tao\model\Language\Service\LanguageListElementSortService;
34use oat\tao\model\Lists\Business\Domain\ValueCollection;
35use oat\tao\model\Lists\Business\Domain\ValueCollectionSearchRequest;
36use oat\tao\model\Lists\Business\Input\ValueCollectionSearchInput;
37use oat\tao\model\Lists\Business\Service\ValueCollectionService;
38use oat\tao\model\TaoOntology;
39use tao_helpers_form_elements_Readonly;
40use tao_helpers_form_GenerisFormFactory;
41use tao_helpers_Uri;
42
43class OntologyFormDataProvider implements FormDataProviderInterface
44{
45    use OntologyAwareTrait;
46
47    private LanguageClassSpecification $languageClassSpecification;
48    private LanguageListElementSortService $languageListElementSortService;
49    private ValueCollectionService $valueCollectionService;
50    private LanguageAllowedFilter $languageAllowedFilter;
51
52    public function __construct(
53        LanguageClassSpecification $languageClassSpecification,
54        LanguageListElementSortService $languageListElementSortService,
55        ValueCollectionService $valueCollectionService,
56        LanguageAllowedFilter $languageAllowedFilter
57    ) {
58        $this->languageClassSpecification = $languageClassSpecification;
59        $this->languageListElementSortService = $languageListElementSortService;
60        $this->valueCollectionService = $valueCollectionService;
61        $this->languageAllowedFilter = $languageAllowedFilter;
62    }
63
64    public function preloadFormData(string $classUri, string $topClassUri, string $elementUri, string $language): void
65    {
66        // Not implemented for Ontology Data Provider
67    }
68
69    public function getClassProperties(\core_kernel_classes_Class $class, \core_kernel_classes_Class $topClass): array
70    {
71        return tao_helpers_form_GenerisFormFactory::getClassProperties($class, $topClass);
72    }
73
74    public function getDataToFeedProperty(core_kernel_classes_Property $property): array
75    {
76        return [
77            $property->getWidget(),
78            $property->getRange(),
79            $property->getDomain(),
80        ];
81    }
82
83    public function getDescriptionFromTranslatedPropertyLabel(
84        core_kernel_classes_Property $property,
85        string $language
86    ): ?string {
87        $propertyLabel = current(
88            $property->getPropertyValues(
89                $this->getProperty(OntologyRdfs::RDFS_LABEL),
90                ['lg' => $language, 'one' => true]
91            )
92        );
93
94        if (empty($propertyLabel)) {
95            $propertyLabel = $property->getLabel();
96        }
97
98        if (empty(trim($propertyLabel))) {
99            return str_replace(LOCAL_NAMESPACE, '', $property->getUri());
100        }
101
102        return $propertyLabel;
103    }
104
105    public function getPropertyListElementOptions(
106        core_kernel_classes_Property $property,
107        ?core_kernel_classes_Property $parentProperty,
108        $instance
109    ): array {
110        $options = [];
111        $values = $this->getListValues($instance, $property, $parentProperty);
112
113        if ($this->languageClassSpecification->isSatisfiedBy($property->getRange())) {
114            $values = $this->languageListElementSortService->getSortedListCollectionValues(
115                $this->languageAllowedFilter->filterByValueCollection($values)
116            );
117        }
118
119        foreach ($values as $value) {
120            $encodedUri = tao_helpers_Uri::encode($value->getUri());
121            $options[$encodedUri] = [$encodedUri, $value->getLabel()];
122        }
123
124        return $options;
125    }
126
127    public function getPropertyNotListElementOptions(core_kernel_classes_Property $property): array
128    {
129        $options = [];
130
131        foreach ($property->getRange()->getInstances(true) as $rangeInstance) {
132            $level = $rangeInstance->getOnePropertyValue(
133                new core_kernel_classes_Property(TaoOntology::PROPERTY_LIST_LEVEL)
134            );
135            if (null === $level) {
136                $encodedUri = tao_helpers_Uri::encode($rangeInstance->getUri());
137                $options[$encodedUri] = [$encodedUri, $rangeInstance->getLabel()];
138            } else {
139                $level = ($level instanceof core_kernel_classes_Resource)
140                    ? $level->getUri()
141                    : (string)$level;
142                $options[$level] = [
143                    tao_helpers_Uri::encode($rangeInstance->getUri()),
144                    $rangeInstance->getLabel()
145                ];
146            }
147        }
148
149        ksort($options);
150
151        return $options;
152    }
153
154    public function getPropertyValidators(core_kernel_classes_Property $property): array
155    {
156        return ValidationRuleRegistry::getRegistry()->getValidators($property);
157    }
158
159    public function getPropertyInstanceValues(core_kernel_classes_Property $property, $instance, $element): array
160    {
161        $output = [];
162        $values = $instance->getPropertyValuesCollection($property);
163
164        foreach ($values as $value) {
165            if ($value instanceof core_kernel_classes_Resource) {
166                $elementValue = $element instanceof tao_helpers_form_elements_Readonly
167                    ? $value->getLabel()
168                    : $value->getUri();
169                $elementValueUri = $value->getUri();
170            } elseif ($value instanceof core_kernel_classes_Literal) {
171                $elementValue = (string)$value;
172                $elementValueUri = $elementValue;
173            } else {
174                continue;
175            }
176
177            if ($this->isPropertyList($property)) {
178                $searchRequest = new ValueCollectionSearchRequest();
179                $searchRequest->setValueCollectionUri($property->getRange()->getUri());
180                $searchRequest->setUris($elementValueUri);
181                $valueCollection = $this->valueCollectionService->findAll(
182                    new ValueCollectionSearchInput($searchRequest)
183                );
184
185                foreach ($valueCollection as $v) {
186                    $output[] = [$v->getUri(), $v->getLabel()];
187                }
188            } else {
189                $output[] = [$elementValue];
190            }
191        }
192
193        return $output;
194    }
195
196    public function isPropertyList($property): bool
197    {
198        if ($property->getRange() === null) {
199            return false;
200        }
201
202        if (!$property->getRange()->isClass()) {
203            return false;
204        }
205
206        return $property->getRange()->isSubClassOf(
207            new core_kernel_classes_Class(TaoOntology::CLASS_URI_LIST)
208        );
209    }
210
211    public function getPropertyGUIOrder(core_kernel_classes_Property $property): array
212    {
213        $guiOrderProperty = new core_kernel_classes_Property(TaoOntology::PROPERTY_GUI_ORDER);
214
215        return $property->getPropertyValues($guiOrderProperty);
216    }
217
218    //--------------END OF PUBLIC INTERFACE-------------
219
220    private function getListValues(
221        $instance,
222        core_kernel_classes_Property $property,
223        core_kernel_classes_Property $parentProperty = null
224    ): ValueCollection {
225        $searchRequest = (new ValueCollectionSearchRequest())->setValueCollectionUri($property->getRange()->getUri());
226
227        if ($instance instanceof core_kernel_classes_Resource) {
228            $selectedValue = $instance->getOnePropertyValue($property);
229
230            if ($selectedValue instanceof core_kernel_classes_Literal && !empty($selectedValue->literal)) {
231                $searchRequest->setSelectedValues($selectedValue->literal);
232            }
233
234            if ($parentProperty) {
235                $parentPropertyValues = [];
236
237                foreach ($instance->getPropertyValuesCollection($parentProperty) as $parentPropertyValue) {
238                    $parentPropertyValues[] = (string)$parentPropertyValue;
239                }
240                $searchRequest->setPropertyUri($property->getUri());
241                $searchRequest->setParentListValues(...$parentPropertyValues);
242            }
243        }
244
245        return $this->valueCollectionService->findAll(new ValueCollectionSearchInput($searchRequest));
246    }
247}