Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 112 |
|
0.00% |
0 / 20 |
CRAP | |
0.00% |
0 / 1 |
| taoTests_models_classes_TestsService | |
0.00% |
0 / 112 |
|
0.00% |
0 / 20 |
1980 | |
0.00% |
0 / 1 |
| deleteTest | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| deleteResource | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getRootClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isTestClass | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
| deleteTestClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAllItems | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| onChangeTestLabel | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| cloneInstance | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
42 | |||
| cloneContent | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setDefaultModel | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| createInstance | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getTestItems | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| setTestModel | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
42 | |||
| getCompiler | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| getCompilerClass | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getTestModel | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getTestModelImplementation | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
30 | |||
| getPropertyByUri | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFileReferenceSerializer | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| hasItems | |
0.00% |
0 / 1 |
|
0.00% |
0 / 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) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg |
| 19 | * (under the project TAO & TAO2); |
| 20 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung |
| 21 | * (under the project TAO-TRANSFER); |
| 22 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 23 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 24 | * 2012-2021 (original work) Open Assessment Technologies SA; |
| 25 | */ |
| 26 | |
| 27 | use oat\generis\model\OntologyRdf; |
| 28 | use oat\tao\model\resources\Service\InstanceCopier; |
| 29 | use oat\tao\model\TaoOntology; |
| 30 | use oat\taoTests\models\event\TestCreatedEvent; |
| 31 | use oat\taoTests\models\event\TestDuplicatedEvent; |
| 32 | use oat\taoTests\models\event\TestRemovedEvent; |
| 33 | use oat\generis\model\fileReference\FileReferenceSerializer; |
| 34 | use oat\tao\model\service\ServiceFileStorage; |
| 35 | use oat\taoTests\models\TestModel; |
| 36 | use oat\taoTests\models\MissingTestmodelException; |
| 37 | use oat\tao\model\OntologyClassService; |
| 38 | |
| 39 | /** |
| 40 | * Service methods to manage the Tests business models using the RDF API. |
| 41 | * |
| 42 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 43 | * |
| 44 | * phpcs:disable Squiz.Classes.ValidClassName |
| 45 | */ |
| 46 | class taoTests_models_classes_TestsService extends OntologyClassService |
| 47 | { |
| 48 | public const CLASS_TEST_MODEL = 'http://www.tao.lu/Ontologies/TAOTest.rdf#TestModel'; |
| 49 | |
| 50 | // phpcs:ignore |
| 51 | public const PROPERTY_TEST_MODEL_IMPLEMENTATION = 'http://www.tao.lu/Ontologies/TAOTest.rdf#TestModelImplementation'; |
| 52 | |
| 53 | public const PROPERTY_TEST_TESTMODEL = 'http://www.tao.lu/Ontologies/TAOTest.rdf#TestTestModel'; |
| 54 | |
| 55 | /** @deprecated self::PROPERTY_TEST_CONTENT should be used */ |
| 56 | public const TEST_TESTCONTENT_PROP = 'http://www.tao.lu/Ontologies/TAOTest.rdf#TestContent'; |
| 57 | |
| 58 | public const PROPERTY_TEST_CONTENT = 'http://www.tao.lu/Ontologies/TAOTest.rdf#TestContent'; |
| 59 | |
| 60 | /** |
| 61 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 62 | */ |
| 63 | public function deleteTest(core_kernel_classes_Resource $test): bool |
| 64 | { |
| 65 | $returnValue = false; |
| 66 | if (!is_null($test)) { |
| 67 | try { |
| 68 | //delete the associated content |
| 69 | $model = $this->getTestModel($test); |
| 70 | $impl = $this->getTestModelImplementation($model); |
| 71 | $impl->deleteContent($test); |
| 72 | } catch (MissingTestmodelException $e) { |
| 73 | // no content present, skip |
| 74 | } |
| 75 | $returnValue = $test->delete(); |
| 76 | $this->getEventManager()->trigger(new TestRemovedEvent($test->getUri())); |
| 77 | } |
| 78 | return (bool) $returnValue; |
| 79 | } |
| 80 | |
| 81 | public function deleteResource(core_kernel_classes_Resource $resource): bool |
| 82 | { |
| 83 | return $this->deleteTest($resource); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @author Joel Bout, <joel@taotesting.com> |
| 88 | */ |
| 89 | public function getRootClass(): core_kernel_classes_Class |
| 90 | { |
| 91 | return $this->getClass(TaoOntology::CLASS_URI_TEST); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Check if the Class in parameter is a subclass of Test |
| 96 | * |
| 97 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 98 | */ |
| 99 | public function isTestClass(core_kernel_classes_Class $clazz): bool |
| 100 | { |
| 101 | if ($clazz->getUri() == $this->getClass(TaoOntology::CLASS_URI_TEST)->getUri()) { |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | foreach ($this->getClass(TaoOntology::CLASS_URI_TEST)->getSubClasses(true) as $subclass) { |
| 106 | if ($clazz->getUri() == $subclass->getUri()) { |
| 107 | return true; |
| 108 | } |
| 109 | } |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 115 | * @deprecated use $this->deleteClass instead |
| 116 | */ |
| 117 | public function deleteTestClass(core_kernel_classes_Class $clazz): bool |
| 118 | { |
| 119 | return $this->deleteClass($clazz); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 124 | */ |
| 125 | public function getAllItems(): array |
| 126 | { |
| 127 | $returnValue = []; |
| 128 | |
| 129 | $itemClazz = $this->getClass(TaoOntology::CLASS_URI_ITEM); |
| 130 | foreach ($itemClazz->getInstances(true) as $instance) { |
| 131 | $returnValue[$instance->getUri()] = $instance->getLabel(); |
| 132 | } |
| 133 | |
| 134 | return $returnValue; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Used to be called whenever the label of the Test changed |
| 139 | * Deprecated in favor of eventmanager |
| 140 | * |
| 141 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 142 | * @deprecated |
| 143 | */ |
| 144 | public function onChangeTestLabel(core_kernel_classes_Resource $test = null) |
| 145 | { |
| 146 | common_Logger::w('Call to deprecated ' . __FUNCTION__); |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 152 | * @deprecated Use service id 'oat\tao\model\resources\Service\InstanceCopier::TESTS' |
| 153 | */ |
| 154 | public function cloneInstance( |
| 155 | core_kernel_classes_Resource $instance, |
| 156 | core_kernel_classes_Class $clazz = null |
| 157 | ): ?core_kernel_classes_Resource { |
| 158 | $returnValue = null; |
| 159 | |
| 160 | //call the parent create instance to prevent useless process test to be created: |
| 161 | $label = $instance->getLabel(); |
| 162 | $cloneLabel = "$label bis"; |
| 163 | $clone = parent::createInstance($clazz, $cloneLabel); |
| 164 | |
| 165 | if (!is_null($clone)) { |
| 166 | $noCloningProperties = [ |
| 167 | self::PROPERTY_TEST_CONTENT, |
| 168 | OntologyRdf::RDF_TYPE |
| 169 | ]; |
| 170 | |
| 171 | foreach ($clazz->getProperties(true) as $property) { |
| 172 | if (!in_array($property->getUri(), $noCloningProperties)) { |
| 173 | //allow clone of every property value but the deliverycontent, which is a process: |
| 174 | foreach ($instance->getPropertyValues($property) as $propertyValue) { |
| 175 | $clone->setPropertyValue($property, $propertyValue); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | //Fix label |
| 180 | if (preg_match("/bis/", $label)) { |
| 181 | $cloneNumber = (int)preg_replace("/^(.?)*bis/", "", $label); |
| 182 | $cloneNumber++; |
| 183 | $cloneLabel = preg_replace("/bis(.?)*$/", "", $label) . "bis $cloneNumber" ; |
| 184 | } |
| 185 | $clone->setLabel($cloneLabel); |
| 186 | |
| 187 | $this->cloneContent($instance, $clone); |
| 188 | $this->getEventManager()->trigger(new TestDuplicatedEvent($instance->getUri(), $clone->getUri())); |
| 189 | |
| 190 | $returnValue = $clone; |
| 191 | } |
| 192 | |
| 193 | return $returnValue; |
| 194 | } |
| 195 | |
| 196 | public function cloneContent(core_kernel_classes_Resource $original, core_kernel_classes_Resource $clone): void |
| 197 | { |
| 198 | $impl = $this->getTestModelImplementation($this->getTestModel($original)); |
| 199 | $impl->cloneContent($original, $clone); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * @author Lionel Lecaque, lionel@taotesting.com |
| 204 | */ |
| 205 | protected function setDefaultModel(core_kernel_classes_Resource $test) |
| 206 | { |
| 207 | $testModelClass = $this->getClass(self::CLASS_TEST_MODEL); |
| 208 | $models = $testModelClass->getInstances(); |
| 209 | if (count($models) > 0) { |
| 210 | $this->setTestModel($test, current($models)); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 216 | */ |
| 217 | public function createInstance(core_kernel_classes_Class $clazz, $label = ''): core_kernel_classes_Resource |
| 218 | { |
| 219 | $test = parent::createInstance($clazz, $label); |
| 220 | $this->setDefaultModel($test); |
| 221 | |
| 222 | $this->getEventManager()->trigger(new TestCreatedEvent($test->getUri())); |
| 223 | |
| 224 | return $test; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 229 | */ |
| 230 | public function getTestItems(core_kernel_classes_Resource $test): array |
| 231 | { |
| 232 | try { |
| 233 | $model = $this->getTestModel($test); |
| 234 | $returnValue = $this->getTestModelImplementation($model)->getItems($test); |
| 235 | } catch (MissingTestmodelException $e) { |
| 236 | $returnValue = []; |
| 237 | } |
| 238 | return $returnValue; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Changes the model of the test, while trying |
| 243 | * to carry over the items of the test |
| 244 | */ |
| 245 | public function setTestModel(core_kernel_classes_Resource $test, core_kernel_classes_Resource $testModel): void |
| 246 | { |
| 247 | $current = $test->getOnePropertyValue($this->getProperty(self::PROPERTY_TEST_TESTMODEL)); |
| 248 | // did the model change? |
| 249 | if (is_null($current) || !$current->equals($testModel)) { |
| 250 | $items = []; |
| 251 | if (!is_null($current)) { |
| 252 | $former = $this->getTestModelImplementation($current); |
| 253 | if (!empty($former)) { |
| 254 | $items = $former->getItems($test); |
| 255 | $former->deleteContent($test); |
| 256 | } |
| 257 | } |
| 258 | $test->editPropertyValues($this->getProperty(self::PROPERTY_TEST_TESTMODEL), $testModel); |
| 259 | $newImpl = $this->getTestModelImplementation($testModel); |
| 260 | if (!empty($newImpl)) { |
| 261 | $newImpl->prepareContent($test, $items); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Returns a compiler instance for a given test |
| 268 | */ |
| 269 | public function getCompiler( |
| 270 | core_kernel_classes_Resource $test, |
| 271 | ServiceFileStorage $storage |
| 272 | ): tao_models_classes_Compiler { |
| 273 | $testModel = $this->getTestModelImplementation($this->getTestModel($test)); |
| 274 | if ($testModel instanceof TestModel) { |
| 275 | $compiler = $testModel->getCompiler($test, $storage); |
| 276 | } else { |
| 277 | $testCompilerClass = $testModel->getCompilerClass(); |
| 278 | $compiler = new $testCompilerClass($test, $storage); |
| 279 | $compiler->setServiceLocator($storage->getServiceLocator()); |
| 280 | } |
| 281 | return $compiler; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Returns the class of the compiler |
| 286 | * @deprecated $this->getCompiler should be used |
| 287 | */ |
| 288 | public function getCompilerClass(core_kernel_classes_Resource $test): string |
| 289 | { |
| 290 | $testModel = $this->getTestModel($test); |
| 291 | return $this->getTestModelImplementation($testModel)->getCompilerClass(); |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Returns the model of the current test |
| 296 | * |
| 297 | * @throws MissingTestmodelException |
| 298 | */ |
| 299 | public function getTestModel(core_kernel_classes_Resource $test): core_kernel_classes_Resource |
| 300 | { |
| 301 | $testModel = $test->getOnePropertyValue($this->getPropertyByUri(self::PROPERTY_TEST_TESTMODEL)); |
| 302 | |
| 303 | if (is_null($testModel)) { |
| 304 | throw new MissingTestmodelException('Undefined testmodel for test ' . $test->getUri()); |
| 305 | } |
| 306 | |
| 307 | return $testModel; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Returns the implementation of an items test model |
| 312 | */ |
| 313 | public function getTestModelImplementation( |
| 314 | core_kernel_classes_Resource $testModel |
| 315 | ): taoTests_models_classes_TestModel { |
| 316 | $serviceId = (string) $testModel->getOnePropertyValue( |
| 317 | $this->getPropertyByUri(self::PROPERTY_TEST_MODEL_IMPLEMENTATION) |
| 318 | ); |
| 319 | |
| 320 | if (empty($serviceId)) { |
| 321 | throw new common_exception_NoImplementation( |
| 322 | 'No implementation found for testmodel ' . $testModel->getUri() |
| 323 | ); |
| 324 | } |
| 325 | |
| 326 | try { |
| 327 | $testModelService = $this->getServiceManager()->get($serviceId); |
| 328 | } catch (\oat\oatbox\service\ServiceNotFoundException $e) { |
| 329 | if (!class_exists($serviceId)) { |
| 330 | throw new common_exception_Error('Test model service ' . $serviceId . ' not found'); |
| 331 | } |
| 332 | // for backward compatibility support classname instead of a serviceid |
| 333 | common_Logger::w('Outdated model definition "' . $serviceId . '", please use test model service'); |
| 334 | $testModelService = new $serviceId(); |
| 335 | } |
| 336 | |
| 337 | if (!$testModelService instanceof \taoTests_models_classes_TestModel) { |
| 338 | throw new common_exception_Error( |
| 339 | sprintf( |
| 340 | 'Test model service %s not compatible for test model %s', |
| 341 | get_class($testModelService), |
| 342 | $testModel->getUri() |
| 343 | ) |
| 344 | ); |
| 345 | } |
| 346 | |
| 347 | return $testModelService; |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * @deprecated $this->getProperty should be used |
| 352 | */ |
| 353 | public function getPropertyByUri(string $uri): core_kernel_classes_Property |
| 354 | { |
| 355 | return $this->getProperty($uri); |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Get serializer to persist filesystem object |
| 360 | */ |
| 361 | protected function getFileReferenceSerializer(): FileReferenceSerializer |
| 362 | { |
| 363 | return $this->getServiceManager()->get(FileReferenceSerializer::SERVICE_ID); |
| 364 | } |
| 365 | |
| 366 | public function hasItems(core_kernel_classes_Resource $test): bool |
| 367 | { |
| 368 | return !empty($this->getTestItems($test)); |
| 369 | } |
| 370 | } |