Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 52
0.00% covered (danger)
0.00%
0 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
BulkFormDataProvider
0.00% covered (danger)
0.00%
0 / 52
0.00% covered (danger)
0.00%
0 / 12
462
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
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 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 getDataToFeedProperty
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getDescriptionFromTranslatedPropertyLabel
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPropertyListElementOptions
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 getPropertyNotListElementOptions
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 getPropertyValidators
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getPropertyInstanceValues
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 isPropertyList
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPropertyGUIOrder
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 getFormData
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
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 (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_Property;
26use oat\generis\model\data\Ontology;
27use oat\generis\model\kernel\persistence\DataProvider\form\DTO\FormDTO;
28use oat\generis\model\kernel\persistence\DataProvider\form\FormDTOProviderInterface;
29use oat\tao\helpers\form\ValidationRuleRegistry;
30use tao_helpers_Uri;
31
32class BulkFormDataProvider implements FormDataProviderInterface
33{
34    private ?FormDTO $formData = null;
35
36    private FormDTOProviderInterface $formDTOProvider;
37    private Ontology $ontology;
38
39    public function __construct(Ontology $ontology, FormDTOProviderInterface $formDTOProvider)
40    {
41        $this->formDTOProvider = $formDTOProvider;
42        $this->ontology = $ontology;
43    }
44
45    public function preloadFormData(string $classUri, string $topClassUri, string $elementUri, string $language): void
46    {
47        $this->formData = $this->formDTOProvider->get($classUri, $topClassUri, $elementUri, $language);
48    }
49
50    public function getClassProperties(core_kernel_classes_Class $class, core_kernel_classes_Class $topClass): array
51    {
52        $classProperties = [];
53        foreach ($this->getFormData()->getProperties() as $formProperty) {
54            $property = $this->ontology->getProperty($formProperty->getPropertyUri());
55            $classProperties[$formProperty->getPropertyUri()] = $property;
56        }
57
58        return $classProperties;
59    }
60
61    public function getDataToFeedProperty(core_kernel_classes_Property $property): array
62    {
63        $property = $this->getFormData()->getProperty($property->getUri());
64
65        return [
66            $property->getWidgetUri(),
67            $property->getRangeUri(),
68            $property->getClassUri(),
69        ];
70    }
71
72    public function getDescriptionFromTranslatedPropertyLabel(
73        core_kernel_classes_Property $property,
74        string $language
75    ): ?string {
76        return $this->getFormData()->getProperty($property->getUri())->getLabel();
77    }
78
79    public function getPropertyListElementOptions(
80        core_kernel_classes_Property $property,
81        ?core_kernel_classes_Property $parentProperty,
82        $instance
83    ): array {
84        $options = [];
85        foreach ($this->getFormData()->getProperty($property->getUri())->getOptions() as $option) {
86            $encodedUri = tao_helpers_Uri::encode($option->getUri());
87            $options[$encodedUri] = [$encodedUri, $option->getLabel()];
88        }
89
90        return $options;
91    }
92
93    public function getPropertyNotListElementOptions(core_kernel_classes_Property $property): array
94    {
95        $options = [];
96        foreach ($this->getFormData()->getProperty($property->getUri())->getOptions() as $option) {
97            if ($option->getLevel() === null) {
98                $encodedUri = tao_helpers_Uri::encode($option->getUri());
99                $options[$encodedUri] = [$encodedUri, $option->getLabel()];
100            } else {
101                $options[$option->getLevel()] = [
102                    tao_helpers_Uri::encode($option->getUri()),
103                    $option->getLabel()
104                ];
105            }
106        }
107
108        ksort($options);
109
110        return $options;
111    }
112
113    public function getPropertyValidators(core_kernel_classes_Property $property): array
114    {
115        $validators = [];
116        foreach ($this->getFormData()->getProperty($property->getUri())->getValidationRule() as $validatorId) {
117            $validators[] = ValidationRuleRegistry::getRegistry()->get($validatorId);
118        }
119
120        return $validators;
121    }
122
123    public function getPropertyInstanceValues(core_kernel_classes_Property $property, $instance, $element): array
124    {
125        $values = $this->getFormData()->getProperty($property->getUri())->getValue();
126        $output = [];
127        foreach ($values as $value) {
128            if ($this->isPropertyList($property)) {
129                $output[] = [
130                    $value,
131                    $this->getFormData()->getProperty($property->getUri())->getOption($value)->getLabel()
132                ];
133            } else {
134                $output[] = [$value];
135                $element->setValue($value);
136            }
137        }
138
139        return $output;
140    }
141
142    public function isPropertyList(core_kernel_classes_Property $property): bool
143    {
144        return $this->getFormData()->getProperty($property->getUri())->isList();
145    }
146
147    public function getPropertyGUIOrder(core_kernel_classes_Property $property): array
148    {
149        $order = $this->getFormData()->getProperty($property->getUri())->getGuiOrder();
150
151        return $order !== null ? [$order] : [];
152    }
153
154    //---------------END OF PUBLIC INTERFACE----------------
155
156    private function getFormData(): FormDTO
157    {
158        if ($this->formData === null) {
159            throw new DataProviderException('Form data was not loaded');
160        }
161
162        return $this->formData;
163    }
164}