Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
64.00% |
64 / 100 |
|
63.64% |
7 / 11 |
CRAP | |
0.00% |
0 / 1 |
| TestCategoryPresetProvider | |
64.00% |
64 / 100 |
|
63.64% |
7 / 11 |
100.87 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| register | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
30 | |||
| getPresets | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
3.14 | |||
| findPresetGroupOrFail | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getAvailablePresets | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
4 | |||
| getPresetGroups | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
2 | |||
| isPresetAvailable | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
3 | |||
| loadPresetFromProviders | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| filterInactivePresets | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
9 | |||
| sortPresets | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
3 | |||
| groomPresets | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| 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) 2017 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | /** |
| 24 | * @author Christophe Noël <christophe@taotesting.com> |
| 25 | */ |
| 26 | |
| 27 | namespace oat\taoQtiTest\models; |
| 28 | |
| 29 | use oat\oatbox\service\ConfigurableService; |
| 30 | use oat\taoTests\models\runner\plugins\TestPluginService; |
| 31 | use RuntimeException; |
| 32 | |
| 33 | class TestCategoryPresetProvider extends ConfigurableService |
| 34 | { |
| 35 | public const SERVICE_ID = 'taoQtiTest/CategoryPresetProvider'; |
| 36 | |
| 37 | public const GROUP_NAVIGATION = 'navigation'; |
| 38 | public const GROUP_WARNING = 'warning'; |
| 39 | public const GROUP_TOOLS = 'tools'; |
| 40 | |
| 41 | private $allPresets; |
| 42 | |
| 43 | private $isGroomed = false; |
| 44 | |
| 45 | /** |
| 46 | * TestCategoryPresetProvider constructor. |
| 47 | * |
| 48 | * @param array $options |
| 49 | * @param array $allPresets - allow override of preset list |
| 50 | */ |
| 51 | public function __construct(array $options = [], array $allPresets = []) |
| 52 | { |
| 53 | $this->allPresets = $allPresets; |
| 54 | |
| 55 | parent::__construct($options); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @param string $presetGroup |
| 60 | * @param TestCategoryPreset[]|TestCategoryPreset $presets |
| 61 | */ |
| 62 | public function register(string $presetGroup, $presets): void |
| 63 | { |
| 64 | if (!array_key_exists($presetGroup, $this->allPresets)) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | if (!is_array($presets)) { |
| 69 | $presets = [$presets]; |
| 70 | } |
| 71 | |
| 72 | foreach ($presets as $preset) { |
| 73 | /** @noinspection TypeUnsafeArraySearchInspection */ |
| 74 | if (!in_array($preset, $this->allPresets[$presetGroup]['presets'])) { |
| 75 | $this->allPresets[$presetGroup]['presets'][] = $preset; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get all active presets |
| 82 | * |
| 83 | * @param bool $keepGroupKeys if `true` returns groups mapped to their group IDs |
| 84 | * |
| 85 | * @return array - the sorted preset list |
| 86 | */ |
| 87 | public function getPresets(bool $keepGroupKeys = false): array |
| 88 | { |
| 89 | if (empty($this->allPresets)) { |
| 90 | $this->loadPresetFromProviders(); |
| 91 | } |
| 92 | |
| 93 | $this->groomPresets(); |
| 94 | |
| 95 | return $keepGroupKeys ? $this->allPresets : array_values($this->allPresets); |
| 96 | } |
| 97 | |
| 98 | public function findPresetGroupOrFail(string $groupId): array |
| 99 | { |
| 100 | $presets = $this->getPresets(true); |
| 101 | |
| 102 | if (!isset($presets[$groupId])) { |
| 103 | throw new RuntimeException("Failed to fetch #$groupId preset group."); |
| 104 | } |
| 105 | |
| 106 | return $presets[$groupId]; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Get all active presets matching the given config. |
| 111 | * |
| 112 | * If a preset is linked to a feature flag, |
| 113 | * we add it only if the config value matching the flag is true. |
| 114 | * |
| 115 | * For example, if a $aPreset->featureFlag = 'foo'; |
| 116 | * The preset will be included only if $config['foo'] = true. |
| 117 | * |
| 118 | * If the config doesn't have a flag, we keep the preset. |
| 119 | * |
| 120 | * @param array $config a config flag list as { key : string => value : boolean } |
| 121 | * |
| 122 | * @return array the sorted preset list |
| 123 | */ |
| 124 | public function getAvailablePresets(array $config = []): array |
| 125 | { |
| 126 | //work on a clone |
| 127 | $presets = array_merge([], $this->getPresets()); |
| 128 | |
| 129 | foreach ($presets as $groupId => &$presetGroup) { |
| 130 | if (isset($presetGroup['presets'])) { |
| 131 | //filter presets based on the config value |
| 132 | //if the config has the flag, we check it's value |
| 133 | //if the config doesn't have the flag, we keep the preset |
| 134 | $presetGroup['presets'] = array_filter( |
| 135 | $presetGroup['presets'], |
| 136 | function ($preset) use ($config) { |
| 137 | return $this->isPresetAvailable($preset, $config); |
| 138 | } |
| 139 | ); |
| 140 | |
| 141 | //remove empty groups |
| 142 | if (count($presetGroup['presets']) === 0) { |
| 143 | unset($presets[$groupId]); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | return $presets; |
| 148 | } |
| 149 | |
| 150 | protected function getPresetGroups(): array |
| 151 | { |
| 152 | return [ |
| 153 | self::GROUP_NAVIGATION => [ |
| 154 | 'groupId' => self::GROUP_NAVIGATION, |
| 155 | 'groupLabel' => __('Test Navigation'), |
| 156 | 'groupOrder' => 100, |
| 157 | 'presets' => [], |
| 158 | ], |
| 159 | |
| 160 | self::GROUP_WARNING => [ |
| 161 | 'groupId' => self::GROUP_WARNING, |
| 162 | 'groupLabel' => __('Navigation Warnings'), |
| 163 | 'groupOrder' => 200, |
| 164 | 'presets' => [], |
| 165 | ], |
| 166 | |
| 167 | self::GROUP_TOOLS => [ |
| 168 | 'groupId' => self::GROUP_TOOLS, |
| 169 | 'groupLabel' => __('Test-Taker Tools'), |
| 170 | 'groupOrder' => 300, |
| 171 | 'presets' => [], |
| 172 | ], |
| 173 | ]; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Is a preset available according to a configuration (ie. based on it's featureFlag) |
| 178 | * |
| 179 | * @param TestCategoryPreset $preset the preset to test |
| 180 | * @param array $config the configuration |
| 181 | * |
| 182 | * @return boolean true if available |
| 183 | */ |
| 184 | private function isPresetAvailable(TestCategoryPreset $preset, array $config = []): bool |
| 185 | { |
| 186 | $flag = $preset->getFeatureFlag(); |
| 187 | |
| 188 | return !$flag || !isset($config[$flag]) || $config[$flag]; |
| 189 | } |
| 190 | |
| 191 | private function loadPresetFromProviders(): void |
| 192 | { |
| 193 | $this->allPresets = $this->getPresetGroups(); |
| 194 | |
| 195 | $providersRegistry = TestCategoryPresetRegistry::getRegistry(); |
| 196 | |
| 197 | $allProviders = $providersRegistry->getMap(); |
| 198 | |
| 199 | if (!empty($allProviders)) { |
| 200 | foreach ($allProviders as $providerClass) { |
| 201 | if (class_exists($providerClass)) { |
| 202 | $providerInstance = new $providerClass(); |
| 203 | $providerInstance->registerPresets($this); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | private function filterInactivePresets(): void |
| 210 | { |
| 211 | $serviceLocator = $this->getServiceLocator(); |
| 212 | $pluginService = $serviceLocator->get(TestPluginService::SERVICE_ID); |
| 213 | |
| 214 | $allEmptyGroups = []; |
| 215 | |
| 216 | if (!empty($this->allPresets)) { |
| 217 | foreach ($this->allPresets as $groupId => &$presetGroup) { |
| 218 | if (!empty($presetGroup['presets'])) { |
| 219 | $presetGroup['presets'] = array_filter( |
| 220 | $presetGroup['presets'], |
| 221 | static function (TestCategoryPreset $preset) use ($pluginService): bool { |
| 222 | $presetPluginId = $preset->getPluginId(); |
| 223 | |
| 224 | if (!empty($presetPluginId)) { |
| 225 | $presetPlugin = $pluginService->getPlugin($presetPluginId); |
| 226 | return ($presetPlugin !== null) ? $presetPlugin->isActive() : false; |
| 227 | } |
| 228 | return true; |
| 229 | } |
| 230 | ); |
| 231 | } |
| 232 | |
| 233 | if (empty($presetGroup['presets'])) { |
| 234 | $allEmptyGroups[] = $groupId; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | unset($presetGroup); |
| 239 | } |
| 240 | |
| 241 | // finally, remove empty groups, if any |
| 242 | if (!empty($allEmptyGroups)) { |
| 243 | foreach ($allEmptyGroups as $emptyGroupId) { |
| 244 | unset($this->allPresets[$emptyGroupId]); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | private function sortPresets(): void |
| 250 | { |
| 251 | // sort presets groups |
| 252 | uasort( |
| 253 | $this->allPresets, |
| 254 | static function (array $a, array $b): int { |
| 255 | return $a['groupOrder'] <=> $b['groupOrder']; |
| 256 | } |
| 257 | ); |
| 258 | |
| 259 | // sort presets |
| 260 | foreach ($this->allPresets as &$presetGroup) { |
| 261 | if (!empty($presetGroup)) { |
| 262 | usort( |
| 263 | $presetGroup['presets'], |
| 264 | static function (TestCategoryPreset $a, TestCategoryPreset $b): int { |
| 265 | return $a->getOrder() <=> $b->getOrder(); |
| 266 | } |
| 267 | ); |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | private function groomPresets(): void |
| 273 | { |
| 274 | if ($this->isGroomed) { |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | $this->filterInactivePresets(); |
| 279 | $this->sortPresets(); |
| 280 | |
| 281 | $this->isGroomed = true; |
| 282 | } |
| 283 | } |