Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.97% covered (success)
96.97%
32 / 33
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ElementPropertyEmptyListValuesFactory
96.97% covered (success)
96.97%
32 / 33
50.00% covered (danger)
50.00%
1 / 2
5
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 create
96.67% covered (success)
96.67%
29 / 30
0.00% covered (danger)
0.00%
0 / 1
4
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) 2021 (original work) Open Assessment Technologies SA;
19 */
20
21declare(strict_types=1);
22
23namespace oat\tao\helpers\form\Factory;
24
25use core_kernel_classes_Property;
26use oat\tao\model\Context\ContextInterface;
27use oat\tao\model\featureFlag\FeatureFlagCheckerInterface;
28use oat\tao\model\Lists\Business\Specification\PropertySpecificationContext;
29use oat\tao\model\Lists\Business\Specification\SecondaryPropertySpecification;
30use oat\tao\model\Specification\PropertySpecificationInterface;
31use tao_helpers_form_elements_xhtml_Combobox;
32
33class ElementPropertyEmptyListValuesFactory extends AbstractElementPropertyListValuesFactory
34{
35    private const ATTRIBUTE_FORCE_DISABLED = 'data-force-disabled';
36    private const ATTRIBUTE_DISABLED_MESSAGE = 'data-disabled-message';
37
38    /** @var PropertySpecificationInterface */
39    private $primaryPropertySpecification;
40
41    /** @var SecondaryPropertySpecification */
42    private $secondaryPropertySpecification;
43
44    /** @var FeatureFlagCheckerInterface */
45    private $featureFlagChecker;
46
47    public function __construct(
48        PropertySpecificationInterface $primaryPropertySpecification,
49        SecondaryPropertySpecification $secondaryPropertySpecification,
50        FeatureFlagCheckerInterface $featureFlagChecker
51    ) {
52        $this->primaryPropertySpecification = $primaryPropertySpecification;
53        $this->secondaryPropertySpecification = $secondaryPropertySpecification;
54        $this->featureFlagChecker = $featureFlagChecker;
55    }
56
57    public function create(ContextInterface $context): tao_helpers_form_elements_xhtml_Combobox
58    {
59        /** @var int $index */
60        $index = $context->getParameter(ElementFactoryContext::PARAM_INDEX);
61
62        /** @var core_kernel_classes_Property $property */
63        $property = $context->getParameter(ElementFactoryContext::PARAM_PROPERTY);
64
65        /** @var array $newData */
66        $newData = $context->getParameter(ElementFactoryContext::PARAM_DATA);
67
68        $element = $this->createElement($index, 'range');
69        $element->setDescription(__('List values'));
70        $element->addAttribute('class', 'property-listvalues property');
71        $element->setEmptyOption(' --- ' . __('select') . ' --- ');
72        $element->addAttribute(self::PROPERTY_LIST_ATTRIBUTE, true);
73
74        if (
75            !$this->featureFlagChecker->isEnabled(FeatureFlagCheckerInterface::FEATURE_FLAG_LISTS_DEPENDENCY_ENABLED)
76        ) {
77            return $element;
78        }
79
80        $isSecondaryProperty = $this->secondaryPropertySpecification->isSatisfiedBy(
81            new PropertySpecificationContext(
82                [
83                    PropertySpecificationContext::PARAM_PROPERTY => $property,
84                    PropertySpecificationContext::PARAM_FORM_INDEX => $index,
85                    PropertySpecificationContext::PARAM_FORM_DATA => $newData
86                ]
87            )
88        );
89
90        if ($isSecondaryProperty || $this->primaryPropertySpecification->isSatisfiedBy($property)) {
91            $element->disable();
92            $element->addAttribute(
93                self::ATTRIBUTE_FORCE_DISABLED,
94                'true'
95            );
96            $element->addAttribute(
97                self::ATTRIBUTE_DISABLED_MESSAGE,
98                __('The field "List" is disabled because the property is part of a dependency')
99            );
100        }
101
102        return $element;
103    }
104}