Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
83.64% |
46 / 55 |
|
46.15% |
6 / 13 |
CRAP | |
0.00% |
0 / 1 |
| GetClassMetadataValuesService | |
83.64% |
46 / 55 |
|
46.15% |
6 / 13 |
31.44 | |
0.00% |
0 / 1 |
| ignoreWidgets | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getByClassRecursive | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getByClassExplicitly | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getClassMetadata | |
88.24% |
15 / 17 |
|
0.00% |
0 / 1 |
6.06 | |||
| getPropertyValues | |
93.33% |
14 / 15 |
|
0.00% |
0 / 1 |
4.00 | |||
| getWidgetType | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
3.07 | |||
| isTextWidget | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isListWidget | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isCalendar | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isWidgetType | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| getListItemsUri | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
3.33 | |||
| isWidget | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
3.07 | |||
| getValueCollectionService | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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-2022 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\Lists\Business\Service; |
| 24 | |
| 25 | use core_kernel_classes_Class; |
| 26 | use core_kernel_classes_Property; |
| 27 | use core_kernel_classes_Resource; |
| 28 | use oat\generis\model\OntologyAwareTrait; |
| 29 | use oat\oatbox\service\ConfigurableService; |
| 30 | use oat\tao\helpers\form\elements\xhtml\SearchDropdown; |
| 31 | use oat\tao\model\Lists\Business\Domain\Metadata; |
| 32 | use oat\tao\model\Lists\Business\Domain\MetadataCollection; |
| 33 | use oat\tao\model\Lists\Business\Domain\ValueCollectionSearchRequest; |
| 34 | use oat\tao\model\Lists\Business\Input\ValueCollectionSearchInput; |
| 35 | use tao_helpers_form_elements_Calendar; |
| 36 | use tao_helpers_form_elements_Checkbox as CheckBox; |
| 37 | use tao_helpers_form_elements_Combobox as ComboBox; |
| 38 | use tao_helpers_form_elements_Htmlarea as HtmlArea; |
| 39 | use tao_helpers_form_elements_Radiobox as RadioBox; |
| 40 | use tao_helpers_form_elements_Readonly; |
| 41 | use tao_helpers_form_elements_Textarea as TextArea; |
| 42 | use tao_helpers_form_elements_Textbox as TextBox; |
| 43 | use tao_helpers_form_elements_xhtml_Searchtextbox as SearchTextBox; |
| 44 | |
| 45 | class GetClassMetadataValuesService extends ConfigurableService |
| 46 | { |
| 47 | use OntologyAwareTrait; |
| 48 | |
| 49 | public const TEXT_WIDGETS = [ |
| 50 | TextBox::WIDGET_ID, |
| 51 | TextArea::WIDGET_ID, |
| 52 | HtmlArea::WIDGET_ID, |
| 53 | tao_helpers_form_elements_Readonly::WIDGET_ID, |
| 54 | ]; |
| 55 | public const LIST_WIDGETS = [ |
| 56 | RadioBox::WIDGET_ID, |
| 57 | CheckBox::WIDGET_ID, |
| 58 | ComboBox::WIDGET_ID, |
| 59 | SearchTextBox::WIDGET_ID, |
| 60 | SearchDropdown::WIDGET_ID, |
| 61 | ]; |
| 62 | public const DATA_TYPE_LIST = 'list'; |
| 63 | public const DATA_TYPE_CALENDAR = 'calendar'; |
| 64 | public const DATA_TYPE_TEXT = 'text'; |
| 65 | |
| 66 | private const BASE_LIST_ITEMS_URI = '/tao/PropertyValues/get?propertyUri=%s'; |
| 67 | |
| 68 | /** @var array */ |
| 69 | private $ignoredWidgets = []; |
| 70 | |
| 71 | public function ignoreWidgets(array $ignoreWidgets): self |
| 72 | { |
| 73 | $this->ignoredWidgets = $ignoreWidgets; |
| 74 | |
| 75 | return $this; |
| 76 | } |
| 77 | |
| 78 | public function getByClassRecursive( |
| 79 | core_kernel_classes_Class $class, |
| 80 | int $maxListSize = 100 |
| 81 | ): MetadataCollection { |
| 82 | return $this->getClassMetadata($class, $maxListSize, true); |
| 83 | } |
| 84 | |
| 85 | public function getByClassExplicitly( |
| 86 | core_kernel_classes_Class $class, |
| 87 | int $maxListSize = 100 |
| 88 | ): MetadataCollection { |
| 89 | return $this->getClassMetadata($class, $maxListSize, false); |
| 90 | } |
| 91 | |
| 92 | private function getClassMetadata( |
| 93 | core_kernel_classes_Class $class, |
| 94 | int $maxListSize, |
| 95 | bool $recursively = true |
| 96 | ): MetadataCollection { |
| 97 | $collection = new MetadataCollection(); |
| 98 | |
| 99 | foreach ($class->getProperties($recursively) as $property) { |
| 100 | if (!$this->isWidget($property)) { |
| 101 | continue; |
| 102 | } |
| 103 | |
| 104 | if (!$this->isCalendar($property) && !$this->isTextWidget($property) && !$this->isListWidget($property)) { |
| 105 | continue; |
| 106 | } |
| 107 | |
| 108 | $values = $this->getPropertyValues($property, $maxListSize); |
| 109 | $uri = $this->getListItemsUri($property, $values); |
| 110 | |
| 111 | $metadata = (new Metadata()) |
| 112 | ->setLabel($property->getLabel()) |
| 113 | ->setAlias($property->getAlias()) |
| 114 | ->setType($this->getWidgetType($property)) |
| 115 | ->setValues($values) |
| 116 | ->setUri($uri) |
| 117 | ->setPropertyUri($property->getUri()); |
| 118 | |
| 119 | $collection->addMetadata($metadata); |
| 120 | } |
| 121 | |
| 122 | return $collection; |
| 123 | } |
| 124 | |
| 125 | private function getPropertyValues(core_kernel_classes_Property $property, int $maxListSize): ?array |
| 126 | { |
| 127 | if (!$this->isListWidget($property)) { |
| 128 | return null; |
| 129 | } |
| 130 | |
| 131 | $values = []; |
| 132 | $range = $property->getRange(); |
| 133 | |
| 134 | $search = new ValueCollectionSearchInput( |
| 135 | (new ValueCollectionSearchRequest()) |
| 136 | ->setValueCollectionUri($range->getUri()) |
| 137 | ); |
| 138 | $propertyValuesCount = $this->getValueCollectionService()->count($search); |
| 139 | |
| 140 | if ($propertyValuesCount > $maxListSize) { |
| 141 | return null; |
| 142 | } |
| 143 | |
| 144 | $valueCollection = $this->getValueCollectionService()->findAll($search); |
| 145 | |
| 146 | foreach ($valueCollection as $value) { |
| 147 | array_push($values, $value->getLabel()); |
| 148 | } |
| 149 | |
| 150 | return $values; |
| 151 | } |
| 152 | |
| 153 | private function getWidgetType(core_kernel_classes_Property $property): string |
| 154 | { |
| 155 | if ($this->isListWidget($property)) { |
| 156 | return self::DATA_TYPE_LIST; |
| 157 | } |
| 158 | |
| 159 | if ($this->isCalendar($property)) { |
| 160 | return self::DATA_TYPE_CALENDAR; |
| 161 | } |
| 162 | |
| 163 | return self::DATA_TYPE_TEXT; |
| 164 | } |
| 165 | |
| 166 | private function isTextWidget(core_kernel_classes_Property $property): bool |
| 167 | { |
| 168 | return $this->isWidgetType($property, self::TEXT_WIDGETS); |
| 169 | } |
| 170 | |
| 171 | private function isListWidget(core_kernel_classes_Property $property): bool |
| 172 | { |
| 173 | return $this->isWidgetType($property, self::LIST_WIDGETS); |
| 174 | } |
| 175 | |
| 176 | private function isCalendar(core_kernel_classes_Property $property): bool |
| 177 | { |
| 178 | return $this->isWidgetType($property, [tao_helpers_form_elements_Calendar::WIDGET_ID]); |
| 179 | } |
| 180 | |
| 181 | private function isWidgetType(core_kernel_classes_Property $property, array $types): bool |
| 182 | { |
| 183 | $widgetUri = $property->getWidget()->getUri(); |
| 184 | |
| 185 | return ($widgetUri) ? in_array($widgetUri, $types, true) : false; |
| 186 | } |
| 187 | |
| 188 | private function getListItemsUri(core_kernel_classes_Property $property, ?array $values): ?string |
| 189 | { |
| 190 | if (!$this->isListWidget($property) || $values) { |
| 191 | return null; |
| 192 | } |
| 193 | |
| 194 | return sprintf(self::BASE_LIST_ITEMS_URI, urlencode($property->getUri())); |
| 195 | } |
| 196 | |
| 197 | private function isWidget(core_kernel_classes_Property $property): bool |
| 198 | { |
| 199 | $widget = $property->getWidget(); |
| 200 | |
| 201 | if (!$widget instanceof core_kernel_classes_Resource) { |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | $widgetUri = $widget->getUri(); |
| 206 | |
| 207 | return $widgetUri && !in_array($widgetUri, $this->ignoredWidgets, true); |
| 208 | } |
| 209 | |
| 210 | private function getValueCollectionService(): ValueCollectionService |
| 211 | { |
| 212 | return $this->getServiceLocator()->get(ValueCollectionService::SERVICE_ID); |
| 213 | } |
| 214 | } |