Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
75.00% |
24 / 32 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ItemCreationListener | |
75.00% |
24 / 32 |
|
33.33% |
1 / 3 |
15.64 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| populateUniqueId | |
94.74% |
18 / 19 |
|
0.00% |
0 / 1 |
7.01 | |||
| getEventItem | |
22.22% |
2 / 9 |
|
0.00% |
0 / 1 |
16.76 | |||
| 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\taoQtiItem\model\UniqueId\Listener; |
| 24 | |
| 25 | use core_kernel_classes_Resource; |
| 26 | use InvalidArgumentException; |
| 27 | use oat\generis\model\data\Ontology; |
| 28 | use oat\oatbox\event\Event; |
| 29 | use oat\tao\model\featureFlag\FeatureFlagCheckerInterface; |
| 30 | use oat\tao\model\IdentifierGenerator\Generator\IdentifierGeneratorInterface; |
| 31 | use oat\tao\model\resources\Event\InstanceCopiedEvent; |
| 32 | use oat\tao\model\TaoOntology; |
| 33 | use oat\tao\model\Translation\Service\AbstractQtiIdentifierSetter; |
| 34 | use oat\taoItems\model\event\ItemCreatedEvent; |
| 35 | use oat\taoItems\model\event\ItemDuplicatedEvent; |
| 36 | use oat\taoQtiItem\model\event\ItemImported; |
| 37 | use oat\taoQtiItem\model\qti\Identifier\Service\QtiIdentifierSetter; |
| 38 | |
| 39 | class ItemCreationListener |
| 40 | { |
| 41 | private FeatureFlagCheckerInterface $featureFlagChecker; |
| 42 | private Ontology $ontology; |
| 43 | private IdentifierGeneratorInterface $identifierGenerator; |
| 44 | private QtiIdentifierSetter $qtiIdentifierSetter; |
| 45 | |
| 46 | public function __construct( |
| 47 | FeatureFlagCheckerInterface $featureFlagChecker, |
| 48 | Ontology $ontology, |
| 49 | IdentifierGeneratorInterface $identifierGenerator, |
| 50 | QtiIdentifierSetter $qtiIdentifierSetter |
| 51 | ) { |
| 52 | $this->featureFlagChecker = $featureFlagChecker; |
| 53 | $this->ontology = $ontology; |
| 54 | $this->identifierGenerator = $identifierGenerator; |
| 55 | $this->qtiIdentifierSetter = $qtiIdentifierSetter; |
| 56 | } |
| 57 | |
| 58 | public function populateUniqueId(Event $event): void |
| 59 | { |
| 60 | if ( |
| 61 | !$event instanceof ItemCreatedEvent |
| 62 | && !$event instanceof ItemImported |
| 63 | && !$event instanceof ItemDuplicatedEvent |
| 64 | && !$event instanceof InstanceCopiedEvent |
| 65 | ) { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | if (!$this->featureFlagChecker->isEnabled('FEATURE_FLAG_UNIQUE_NUMERIC_QTI_IDENTIFIER')) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | $item = $this->getEventItem($event); |
| 74 | |
| 75 | // We are not going to populate Unique ID for translations |
| 76 | if ($item->getRootId() !== TaoOntology::CLASS_URI_ITEM) { |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | $identifier = $this->identifierGenerator->generate([IdentifierGeneratorInterface::OPTION_RESOURCE => $item]); |
| 81 | $item->editPropertyValues( |
| 82 | $this->ontology->getProperty(TaoOntology::PROPERTY_UNIQUE_IDENTIFIER), |
| 83 | $identifier |
| 84 | ); |
| 85 | |
| 86 | $this->qtiIdentifierSetter->set([ |
| 87 | AbstractQtiIdentifierSetter::OPTION_RESOURCE => $item, |
| 88 | AbstractQtiIdentifierSetter::OPTION_IDENTIFIER => $identifier, |
| 89 | ]); |
| 90 | } |
| 91 | |
| 92 | private function getEventItem(Event $event): core_kernel_classes_Resource |
| 93 | { |
| 94 | if ($event instanceof ItemCreatedEvent) { |
| 95 | return $this->ontology->getResource($event->getItemUri()); |
| 96 | } |
| 97 | |
| 98 | if ($event instanceof ItemImported) { |
| 99 | return $event->getRdfItem(); |
| 100 | } |
| 101 | |
| 102 | if ($event instanceof ItemDuplicatedEvent) { |
| 103 | return $this->ontology->getResource($event->getCloneUri()); |
| 104 | } |
| 105 | |
| 106 | if ($event instanceof InstanceCopiedEvent) { |
| 107 | return $this->ontology->getResource($event->getInstanceUri()); |
| 108 | } |
| 109 | |
| 110 | throw new InvalidArgumentException('Cannot retrieve event item: event %s is not supported', get_class($event)); |
| 111 | } |
| 112 | } |