Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ElementPropertyListValuesFactory
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
5
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_Resource;
26use oat\tao\model\Context\ContextInterface;
27use oat\tao\model\Specification\ClassSpecificationInterface;
28use tao_helpers_form_elements_xhtml_Combobox;
29use tao_helpers_Uri;
30use tao_models_classes_ListService;
31
32class ElementPropertyListValuesFactory extends AbstractElementPropertyListValuesFactory
33{
34    public const OPTION_REMOTE_LIST_ATTRIBUTE = 'data-remote-list';
35
36    /** @var tao_models_classes_ListService|null */
37    private $listService;
38
39    /** @var ClassSpecificationInterface */
40    private $remoteListClassSpecification;
41
42    public function __construct(
43        ClassSpecificationInterface $remoteListClassSpecification,
44        tao_models_classes_ListService $listService = null
45    ) {
46        $this->remoteListClassSpecification = $remoteListClassSpecification;
47        $this->listService = $listService ?? tao_models_classes_ListService::singleton();
48    }
49
50    public function create(ContextInterface $context): tao_helpers_form_elements_xhtml_Combobox
51    {
52        /** @var int $index */
53        $index = $context->getParameter(ElementFactoryContext::PARAM_INDEX);
54
55        /** @var mixed|core_kernel_classes_Resource|null $range */
56        $range = $context->getParameter(ElementFactoryContext::PARAM_RANGE);
57
58        $element = $this->createElement($index, 'range_list');
59        $element->setDescription(__('List values'));
60        $element->addAttribute('class', 'property-template list-template');
61        $element->setEmptyOption(' --- ' . __('select') . ' --- ');
62        $element->addAttribute(self::PROPERTY_LIST_ATTRIBUTE, true);
63        $element->disable();
64
65        $listOptions = [];
66
67        foreach ($this->listService->getLists() as $list) {
68            $encodedListUri = tao_helpers_Uri::encode($list->getUri());
69            $listOptions[$encodedListUri] = $list->getLabel();
70
71            if ($range instanceof core_kernel_classes_Resource && $range->getUri() === $list->getUri()) {
72                $element->setValue($list->getUri());
73            }
74
75            if ($this->remoteListClassSpecification->isSatisfiedBy($list)) {
76                $element->addOptionAttribute(
77                    $encodedListUri,
78                    self::OPTION_REMOTE_LIST_ATTRIBUTE,
79                    'true'
80                );
81            }
82        }
83
84        $element->setOptions($listOptions);
85
86        return $element;
87    }
88}