Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.30% |
26 / 27 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| TranslationFormModifier | |
96.30% |
26 / 27 |
|
66.67% |
2 / 3 |
11 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| modify | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| getTranslationElementsToRemove | |
95.45% |
21 / 22 |
|
0.00% |
0 / 1 |
8 | |||
| 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) 2024 (original work) Open Assessment Technologies SA. |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\Translation\Form\Modifier; |
| 24 | |
| 25 | use core_kernel_classes_Literal; |
| 26 | use core_kernel_classes_Resource; |
| 27 | use oat\generis\model\data\Ontology; |
| 28 | use oat\tao\model\featureFlag\FeatureFlagCheckerInterface; |
| 29 | use oat\tao\model\featureFlag\Service\FeatureFlagPropertiesMapping; |
| 30 | use oat\tao\model\form\Modifier\AbstractFormModifier; |
| 31 | use oat\tao\model\TaoOntology; |
| 32 | use tao_helpers_form_Form; |
| 33 | use tao_helpers_Uri; |
| 34 | |
| 35 | class TranslationFormModifier extends AbstractFormModifier |
| 36 | { |
| 37 | private FeatureFlagCheckerInterface $featureFlagChecker; |
| 38 | private FeatureFlagPropertiesMapping $featureFlagPropertiesMapping; |
| 39 | private Ontology $ontology; |
| 40 | |
| 41 | public function __construct( |
| 42 | FeatureFlagCheckerInterface $featureFlagChecker, |
| 43 | FeatureFlagPropertiesMapping $featureFlagPropertiesMapping, |
| 44 | Ontology $ontology |
| 45 | ) { |
| 46 | $this->featureFlagChecker = $featureFlagChecker; |
| 47 | $this->featureFlagPropertiesMapping = $featureFlagPropertiesMapping; |
| 48 | $this->ontology = $ontology; |
| 49 | } |
| 50 | |
| 51 | public function modify(tao_helpers_form_Form $form, array $options = []): void |
| 52 | { |
| 53 | foreach ($this->getTranslationElementsToRemove($form) as $elementUri) { |
| 54 | $form->removeElement(tao_helpers_Uri::encode($elementUri)); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | private function getTranslationElementsToRemove(tao_helpers_form_Form $form): array |
| 59 | { |
| 60 | if (!$this->featureFlagChecker->isEnabled('FEATURE_FLAG_TRANSLATION_ENABLED')) { |
| 61 | return $this->featureFlagPropertiesMapping->getFeatureProperties('FEATURE_FLAG_TRANSLATION_ENABLED'); |
| 62 | } |
| 63 | |
| 64 | $elementsToRemove = [ |
| 65 | TaoOntology::PROPERTY_TRANSLATION_TYPE, |
| 66 | TaoOntology::PROPERTY_TRANSLATED_INTO_LANGUAGES, |
| 67 | ]; |
| 68 | |
| 69 | $instance = $this->ontology->getResource($form->getValue('uri')); |
| 70 | $translationType = $instance->getOnePropertyValue( |
| 71 | $this->ontology->getProperty(TaoOntology::PROPERTY_TRANSLATION_TYPE) |
| 72 | ); |
| 73 | $isTranslationTypeEmpty = empty($translationType); |
| 74 | $translationTypeUri = $translationType instanceof core_kernel_classes_Literal |
| 75 | ? (string) $translationType |
| 76 | : ($translationType instanceof core_kernel_classes_Resource ? $translationType->getUri() : null); |
| 77 | |
| 78 | if ( |
| 79 | $isTranslationTypeEmpty |
| 80 | || $translationTypeUri === TaoOntology::PROPERTY_VALUE_TRANSLATION_TYPE_ORIGINAL |
| 81 | ) { |
| 82 | $elementsToRemove[] = TaoOntology::PROPERTY_TRANSLATION_PROGRESS; |
| 83 | $elementsToRemove[] = TaoOntology::PROPERTY_TRANSLATION_ORIGINAL_RESOURCE_URI; |
| 84 | } |
| 85 | |
| 86 | if ( |
| 87 | $isTranslationTypeEmpty |
| 88 | || $translationTypeUri === TaoOntology::PROPERTY_VALUE_TRANSLATION_TYPE_TRANSLATION |
| 89 | ) { |
| 90 | $elementsToRemove[] = TaoOntology::PROPERTY_TRANSLATION_STATUS; |
| 91 | } |
| 92 | |
| 93 | return $elementsToRemove; |
| 94 | } |
| 95 | } |