Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.12% |
39 / 41 |
|
88.89% |
8 / 9 |
CRAP | |
0.00% |
0 / 1 |
| AssessmentTestXmlFactory | |
95.12% |
39 / 41 |
|
88.89% |
8 / 9 |
14 | |
0.00% |
0 / 1 |
| create | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| createTest | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
1 | |||
| extendTest | |
80.00% |
8 / 10 |
|
0.00% |
0 / 1 |
6.29 | |||
| getAssessmentSectionId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getAssessmentSectionTitle | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getQtiVersion | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getTestPartId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getApplicationService | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getConfigurationRegistry | |
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) 2020-2025 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | declare(strict_types=1); |
| 23 | |
| 24 | namespace oat\taoQtiTest\models\test; |
| 25 | |
| 26 | use common_exception_Error; |
| 27 | use common_ext_ExtensionException; |
| 28 | use DOMDocument; |
| 29 | use oat\oatbox\service\ConfigurableService; |
| 30 | use oat\oatbox\service\exception\InvalidService; |
| 31 | use oat\oatbox\service\exception\InvalidServiceManagerException; |
| 32 | use oat\oatbox\service\ServiceNotFoundException; |
| 33 | use oat\tao\model\service\ApplicationService; |
| 34 | use oat\taoQtiTest\models\test\Template\DefaultConfigurationRegistry; |
| 35 | use qtism\data\AssessmentSection; |
| 36 | use qtism\data\AssessmentSectionCollection; |
| 37 | use qtism\data\AssessmentTest; |
| 38 | use qtism\data\ItemSessionControl; |
| 39 | use qtism\data\storage\xml\XmlDocument; |
| 40 | use qtism\data\storage\xml\XmlStorageException; |
| 41 | use qtism\data\TestPart; |
| 42 | use qtism\data\TestPartCollection; |
| 43 | use RuntimeException; |
| 44 | |
| 45 | class AssessmentTestXmlFactory extends ConfigurableService implements AssessmentTestXmlFactoryInterface |
| 46 | { |
| 47 | public const OPTION_QTI_VERSION = 'qti_version'; |
| 48 | public const OPTION_EXTENSIONS = 'extensions'; |
| 49 | public const OPTION_CONFIGURATION_REGISTRY = 'configurationRegistry'; |
| 50 | |
| 51 | private const DEFAULT_QTI_VERSION = '2.2'; |
| 52 | |
| 53 | /** |
| 54 | * @param string $testIdentifier |
| 55 | * @param string $testTitle |
| 56 | * |
| 57 | * @return DOMDocument |
| 58 | * @throws InvalidService |
| 59 | * @throws InvalidServiceManagerException |
| 60 | * @throws XmlStorageException |
| 61 | * @throws common_exception_Error |
| 62 | * @throws common_ext_ExtensionException |
| 63 | */ |
| 64 | public function create(string $testIdentifier, string $testTitle): string |
| 65 | { |
| 66 | $test = $this->createTest($testIdentifier, $testTitle); |
| 67 | |
| 68 | $this->extendTest($test); |
| 69 | |
| 70 | $xmlDoc = new XmlDocument($this->getQtiVersion(), $test); |
| 71 | |
| 72 | return $xmlDoc->saveToString(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @param string $testIdentifier |
| 77 | * @param string $testTitle |
| 78 | * |
| 79 | * @return AssessmentTest |
| 80 | * @throws InvalidService |
| 81 | * @throws InvalidServiceManagerException |
| 82 | * @throws common_exception_Error |
| 83 | * @throws common_ext_ExtensionException |
| 84 | */ |
| 85 | protected function createTest(string $testIdentifier, string $testTitle): AssessmentTest |
| 86 | { |
| 87 | $itemSectionControl = new ItemSessionControl(); |
| 88 | $itemSectionControl->setMaxAttempts($this->getConfigurationRegistry()->getMaxAttempts()); |
| 89 | |
| 90 | $assessmentSection = new AssessmentSection( |
| 91 | $this->getAssessmentSectionId(), |
| 92 | $this->getAssessmentSectionTitle(), |
| 93 | true |
| 94 | ); |
| 95 | $assessmentSection->setRequired(true); |
| 96 | |
| 97 | $assessmentSections = new AssessmentSectionCollection([$assessmentSection]); |
| 98 | |
| 99 | $testPart = new TestPart( |
| 100 | $this->getTestPartId(), |
| 101 | $assessmentSections, |
| 102 | $this->getConfigurationRegistry()->getNavigationMode(), |
| 103 | $this->getConfigurationRegistry()->getSubmissionMode() |
| 104 | ); |
| 105 | |
| 106 | $testPart->setItemSessionControl($itemSectionControl); |
| 107 | $testPartCollection = new TestPartCollection([$testPart]); |
| 108 | |
| 109 | $test = new AssessmentTest($testIdentifier, $testTitle, $testPartCollection); |
| 110 | $test->setToolName('tao'); |
| 111 | $test->setToolVersion($this->getApplicationService()->getPlatformVersion()); |
| 112 | |
| 113 | return $test; |
| 114 | } |
| 115 | |
| 116 | private function extendTest(AssessmentTest $test): void |
| 117 | { |
| 118 | $testExtensionsClassNames = $this->getOption(self::OPTION_EXTENSIONS); |
| 119 | |
| 120 | if (!$testExtensionsClassNames || !is_array($testExtensionsClassNames)) { |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | foreach ($testExtensionsClassNames as $testExtensionsClassName) { |
| 125 | try { |
| 126 | $testExtension = $this->getServiceLocator()->get($testExtensionsClassName); |
| 127 | } catch (ServiceNotFoundException $e) { |
| 128 | $testExtension = new $testExtensionsClassName(); |
| 129 | } |
| 130 | |
| 131 | if (!$testExtension instanceof TestExtensionInterface) { |
| 132 | throw new RuntimeException('A test extension should inherit ' . TestExtensionInterface::class); |
| 133 | } |
| 134 | |
| 135 | $testExtension->extend($test); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @return string |
| 141 | * |
| 142 | * @throws InvalidService |
| 143 | * @throws InvalidServiceManagerException |
| 144 | */ |
| 145 | private function getAssessmentSectionId(): string |
| 146 | { |
| 147 | return "{$this->getConfigurationRegistry()->getSectionIdPrefix()}-1"; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @return string |
| 152 | * |
| 153 | * @throws InvalidService |
| 154 | * @throws InvalidServiceManagerException |
| 155 | */ |
| 156 | private function getAssessmentSectionTitle(): string |
| 157 | { |
| 158 | return $this->getConfigurationRegistry()->getSectionTitlePrefix(); |
| 159 | } |
| 160 | |
| 161 | private function getQtiVersion(): string |
| 162 | { |
| 163 | return $this->getOption(self::OPTION_QTI_VERSION) ?? self::DEFAULT_QTI_VERSION; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * @return string |
| 168 | * |
| 169 | * @throws InvalidService |
| 170 | * @throws InvalidServiceManagerException |
| 171 | */ |
| 172 | private function getTestPartId(): string |
| 173 | { |
| 174 | return "{$this->getConfigurationRegistry()->getPartIdPrefix()}-1"; |
| 175 | } |
| 176 | |
| 177 | private function getApplicationService(): ApplicationService |
| 178 | { |
| 179 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
| 180 | return $this->getServiceLocator()->get(ApplicationService::SERVICE_ID); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * @return DefaultConfigurationRegistry |
| 185 | * |
| 186 | * @throws InvalidService |
| 187 | * @throws InvalidServiceManagerException |
| 188 | */ |
| 189 | private function getConfigurationRegistry(): DefaultConfigurationRegistry |
| 190 | { |
| 191 | return $this->getSubService(self::OPTION_CONFIGURATION_REGISTRY, DefaultConfigurationRegistry::class); |
| 192 | } |
| 193 | } |