Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
2.53% |
4 / 158 |
|
0.00% |
0 / 17 |
CRAP | |
0.00% |
0 / 1 |
| QtiResultsService | |
2.53% |
4 / 158 |
|
0.00% |
0 / 17 |
1920.06 | |
0.00% |
0 / 1 |
| getDeliveryExecutionService | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getDeliveryExecutionByTestTakerAndDelivery | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getDeliveryExecutionById | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getDeliveryExecutionXml | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getQtiResultXml | |
0.00% |
0 / 69 |
|
0.00% |
0 / 1 |
156 | |||
| injectXmlResultToDeliveryExecution | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| storeTestVariables | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| storeItemVariables | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
| createCDATANode | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| createItemResultNode | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| createItemVariableNode | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
72 | |||
| getDisplayDate | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| prepareItemVariableValue | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| renderBinaryContentAsVariableValue | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
| getResultStorage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getMapper | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAssessmentResultFileResponseResolver | |
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) 2016-2020 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | declare(strict_types=1); |
| 23 | |
| 24 | namespace oat\taoResultServer\models\classes; |
| 25 | |
| 26 | use common_Exception; |
| 27 | use common_exception_InvalidArgumentType; |
| 28 | use common_exception_NotFound; |
| 29 | use common_exception_NotImplemented; |
| 30 | use common_exception_ResourceNotFound; |
| 31 | use core_kernel_classes_Resource; |
| 32 | use DOMDocument; |
| 33 | use DOMElement; |
| 34 | use finfo; |
| 35 | use InvalidArgumentException; |
| 36 | use oat\oatbox\service\ConfigurableService; |
| 37 | use oat\oatbox\service\exception\InvalidServiceManagerException; |
| 38 | use oat\taoDelivery\model\execution\DeliveryExecution as DeliveryExecutionInterface; |
| 39 | use oat\taoDelivery\model\execution\ServiceProxy; |
| 40 | use oat\taoResultServer\models\AssessmentResultResolver\AssessmentResultFileResponseResolver; |
| 41 | use oat\taoResultServer\models\Exceptions\DuplicateVariableException; |
| 42 | use oat\taoResultServer\models\Mapper\ResultMapper; |
| 43 | use oat\taoResultServer\models\Parser\QtiResultParser; |
| 44 | use qtism\common\enums\Cardinality; |
| 45 | use qtism\data\results\AssessmentResult; |
| 46 | use qtism\data\storage\xml\XmlResultDocument; |
| 47 | use qtism\data\storage\xml\XmlStorageException; |
| 48 | use tao_helpers_Date; |
| 49 | use taoResultServer_models_classes_WritableResultStorage as WritableResultStorage; |
| 50 | |
| 51 | class QtiResultsService extends ConfigurableService implements ResultService |
| 52 | { |
| 53 | protected $deliveryExecutionService; |
| 54 | |
| 55 | private const QTI_NS = 'http://www.imsglobal.org/xsd/imsqti_result_v2p1'; |
| 56 | |
| 57 | public const CLASS_RESPONSE_VARIABLE = 'http://www.tao.lu/Ontologies/TAOResult.rdf#ResponseVariable'; |
| 58 | public const CLASS_OUTCOME_VARIABLE = 'http://www.tao.lu/Ontologies/TAOResult.rdf#OutcomeVariable'; |
| 59 | |
| 60 | /** |
| 61 | * Get the implementation of delivery execution service |
| 62 | * |
| 63 | * @return ServiceProxy |
| 64 | * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
| 65 | */ |
| 66 | protected function getDeliveryExecutionService() |
| 67 | { |
| 68 | if (!$this->deliveryExecutionService) { |
| 69 | $this->deliveryExecutionService = $this->getServiceLocator()->get(ServiceProxy::SERVICE_ID); |
| 70 | } |
| 71 | return $this->deliveryExecutionService; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get last delivery execution from $delivery & $testtaker uri |
| 76 | * |
| 77 | * @param string $delivery uri |
| 78 | * @param string $testtaker uri |
| 79 | * @return \oat\taoDelivery\model\execution\DeliveryExecutionInterface |
| 80 | * @throws |
| 81 | */ |
| 82 | public function getDeliveryExecutionByTestTakerAndDelivery($delivery, $testtaker) |
| 83 | { |
| 84 | $delivery = new core_kernel_classes_Resource($delivery); |
| 85 | $deliveryExecutions = $this->getDeliveryExecutionService()->getUserExecutions($delivery, $testtaker); |
| 86 | if (empty($deliveryExecutions)) { |
| 87 | throw new common_exception_NotFound('Provided parameters don\'t match with any delivery execution.'); |
| 88 | } |
| 89 | return array_pop($deliveryExecutions); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get Delivery execution from resource |
| 94 | * |
| 95 | * @param $deliveryExecutionId |
| 96 | * @return DeliveryExecutionInterface |
| 97 | * @throws common_exception_NotFound |
| 98 | */ |
| 99 | public function getDeliveryExecutionById($deliveryExecutionId) |
| 100 | { |
| 101 | $deliveryExecution = $this->getDeliveryExecutionService()->getDeliveryExecution($deliveryExecutionId); |
| 102 | try { |
| 103 | $deliveryExecution->getDelivery(); |
| 104 | } catch (common_exception_NotFound $e) { |
| 105 | throw new common_exception_NotFound('Provided parameters don\'t match with any delivery execution.'); |
| 106 | } |
| 107 | return $deliveryExecution; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Return delivery execution as xml of testtaker based on delivery |
| 112 | * |
| 113 | * @param DeliveryExecutionInterface $deliveryExecution |
| 114 | * @return string |
| 115 | */ |
| 116 | public function getDeliveryExecutionXml(DeliveryExecutionInterface $deliveryExecution) |
| 117 | { |
| 118 | return $this->getQtiResultXml($deliveryExecution->getDelivery()->getUri(), $deliveryExecution->getIdentifier()); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @param $deliveryId |
| 123 | * @param $resultId |
| 124 | * @param bool $fetchOnlyLastAttemptResult |
| 125 | * @return string |
| 126 | * @throws common_Exception |
| 127 | * @throws InvalidServiceManagerException |
| 128 | */ |
| 129 | public function getQtiResultXml($deliveryId, $resultId, $fetchOnlyLastAttemptResult = false) |
| 130 | { |
| 131 | $deId = $this |
| 132 | ->getServiceManager() |
| 133 | ->get(ResultAliasServiceInterface::SERVICE_ID) |
| 134 | ->getDeliveryExecutionId($resultId); |
| 135 | |
| 136 | if ($deId === null) { |
| 137 | $deId = $resultId; |
| 138 | } |
| 139 | |
| 140 | $resultService = $this->getServiceLocator()->get(ResultServerService::SERVICE_ID); |
| 141 | $resultServer = $resultService->getResultStorage(); |
| 142 | |
| 143 | $crudService = new CrudResultsService(); |
| 144 | |
| 145 | $dom = new DOMDocument('1.0', 'UTF-8'); |
| 146 | $dom->formatOutput = true; |
| 147 | |
| 148 | $itemResultsByAttempt = $crudService->format( |
| 149 | $resultServer, |
| 150 | $deId, |
| 151 | CrudResultsService::GROUP_BY_ITEM, |
| 152 | $fetchOnlyLastAttemptResult, |
| 153 | true |
| 154 | ); |
| 155 | $testResults = $crudService->format($resultServer, $deId, CrudResultsService::GROUP_BY_TEST); |
| 156 | |
| 157 | $assessmentResultElt = $dom->createElementNS(self::QTI_NS, 'assessmentResult'); |
| 158 | $dom->appendChild($assessmentResultElt); |
| 159 | |
| 160 | /** Context */ |
| 161 | $contextElt = $dom->createElementNS(self::QTI_NS, 'context'); |
| 162 | $userId = $resultServer->getTestTaker($deId); |
| 163 | |
| 164 | if ($userId === false) { |
| 165 | throw new common_exception_ResourceNotFound( |
| 166 | 'Provided parameters don\'t match with any delivery execution.' |
| 167 | ); |
| 168 | } |
| 169 | |
| 170 | if (\common_Utils::isUri($userId)) { |
| 171 | $userId = \tao_helpers_Uri::getUniqueId($userId); |
| 172 | } |
| 173 | |
| 174 | $contextElt->setAttribute('sourcedId', $userId); |
| 175 | $assessmentResultElt->appendChild($contextElt); |
| 176 | |
| 177 | /** Test Result */ |
| 178 | foreach ($testResults as $testResultIdentifier => $testResult) { |
| 179 | $identifierParts = explode('.', $testResultIdentifier); |
| 180 | $testIdentifier = array_pop($identifierParts); |
| 181 | |
| 182 | $testResultElement = $dom->createElementNS(self::QTI_NS, 'testResult'); |
| 183 | $testResultElement->setAttribute('identifier', $testIdentifier); |
| 184 | $testResultElement->setAttribute('datestamp', $this->getDisplayDate($testResult[0]['epoch'])); |
| 185 | |
| 186 | /** Item Variable */ |
| 187 | foreach ($testResult as $itemVariable) { |
| 188 | $isResponseVariable = $itemVariable['type']->getUri() === self::CLASS_RESPONSE_VARIABLE; |
| 189 | $testVariableElement = $dom->createElementNS( |
| 190 | self::QTI_NS, |
| 191 | ($isResponseVariable) ? 'responseVariable' : 'outcomeVariable' |
| 192 | ); |
| 193 | $testVariableElement->setAttribute('identifier', $itemVariable['identifier']); |
| 194 | $testVariableElement->setAttribute('cardinality', $itemVariable['cardinality']); |
| 195 | $testVariableElement->setAttribute('baseType', $itemVariable['basetype']); |
| 196 | |
| 197 | $valueElement = $this->createCDATANode($dom, 'value', trim($itemVariable['value'])); |
| 198 | |
| 199 | if ($isResponseVariable) { |
| 200 | $candidateResponseElement = $dom->createElementNS(self::QTI_NS, 'candidateResponse'); |
| 201 | $candidateResponseElement->appendChild($valueElement); |
| 202 | $testVariableElement->appendChild($candidateResponseElement); |
| 203 | } else { |
| 204 | $testVariableElement->appendChild($valueElement); |
| 205 | } |
| 206 | |
| 207 | $testResultElement->appendChild($testVariableElement); |
| 208 | } |
| 209 | |
| 210 | $assessmentResultElt->appendChild($testResultElement); |
| 211 | } |
| 212 | |
| 213 | /** Item Result */ |
| 214 | foreach ($itemResultsByAttempt as $itemResultIdentifier => $itemResults) { |
| 215 | /** Iterates variables */ |
| 216 | foreach ($itemResults as $itemResult) { |
| 217 | $itemElement = $this->createItemResultNode($dom, $itemResultIdentifier, $itemResult); |
| 218 | /** Item variables */ |
| 219 | foreach ($itemResult as $key => $itemVariable) { |
| 220 | $isResponseVariable = $itemVariable['type']->getUri() === self::CLASS_RESPONSE_VARIABLE; |
| 221 | |
| 222 | if ($itemVariable['identifier'] == 'comment') { |
| 223 | /** Comment */ |
| 224 | $itemVariableElement = $dom->createElementNS( |
| 225 | self::QTI_NS, |
| 226 | 'candidateComment', |
| 227 | $itemVariable['value'] |
| 228 | ); |
| 229 | } else { |
| 230 | $itemVariableElement = $this->createItemVariableNode($dom, $isResponseVariable, $itemVariable); |
| 231 | } |
| 232 | $itemElement->appendChild($itemVariableElement); |
| 233 | } |
| 234 | $assessmentResultElt->appendChild($itemElement); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | return $dom->saveXML(); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Parse the xml to save including variables into given deliveryExecution |
| 243 | * |
| 244 | * @param string $deliveryExecutionId |
| 245 | * @param string $xml |
| 246 | * @throws common_exception_InvalidArgumentType |
| 247 | * @throws common_exception_NotFound |
| 248 | * @throws common_exception_NotImplemented |
| 249 | * @throws XmlStorageException |
| 250 | * @throws DuplicateVariableException |
| 251 | */ |
| 252 | public function injectXmlResultToDeliveryExecution($deliveryExecutionId, $xml) |
| 253 | { |
| 254 | $this->getDeliveryExecutionById($deliveryExecutionId); |
| 255 | |
| 256 | $xmlResultDocument = new XmlResultDocument(); |
| 257 | $xmlResultDocument->loadFromString($xml); |
| 258 | $assessmentResult = $xmlResultDocument->getDocumentComponent(); |
| 259 | if (!$assessmentResult instanceof AssessmentResult) { |
| 260 | throw new InvalidArgumentException('Unsupported xml provided'); |
| 261 | } |
| 262 | |
| 263 | $assessmentResult = $this->getAssessmentResultFileResponseResolver()->resolve($assessmentResult); |
| 264 | $map = $this->getMapper()->loadSource($assessmentResult); |
| 265 | |
| 266 | $resultStorage = $this->getResultStorage(); |
| 267 | $this->storeTestVariables($resultStorage, $deliveryExecutionId, $map->getTestVariables()); |
| 268 | $this->storeItemVariables($resultStorage, $deliveryExecutionId, $map->getItemVariables()); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Store test variables associated to a delivery execution |
| 273 | * |
| 274 | * @param WritableResultStorage $resultStorage |
| 275 | * @param string $deliveryExecutionId |
| 276 | * @param array $itemVariablesByTestResult |
| 277 | * @throws DuplicateVariableException |
| 278 | */ |
| 279 | protected function storeTestVariables( |
| 280 | WritableResultStorage $resultStorage, |
| 281 | $deliveryExecutionId, |
| 282 | array $itemVariablesByTestResult |
| 283 | ) { |
| 284 | $test = ' '; |
| 285 | foreach ($itemVariablesByTestResult as $test => $testVariables) { |
| 286 | $resultStorage->storeTestVariables($deliveryExecutionId, $test, $testVariables, $test); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Store item variables associated to a delivery execution |
| 292 | * |
| 293 | * @param WritableResultStorage $resultStorage |
| 294 | * @param string $deliveryExecutionId |
| 295 | * @param array $itemVariablesByItemResult |
| 296 | * @throws DuplicateVariableException |
| 297 | */ |
| 298 | protected function storeItemVariables( |
| 299 | WritableResultStorage $resultStorage, |
| 300 | $deliveryExecutionId, |
| 301 | array $itemVariablesByItemResult |
| 302 | ) { |
| 303 | $test = null; |
| 304 | foreach ($itemVariablesByItemResult as $itemResultIdentifier => $itemVariables) { |
| 305 | $callIdItem = $deliveryExecutionId . '.' . $itemResultIdentifier; |
| 306 | foreach ($itemVariables as $variable) { |
| 307 | if ($variable->getIdentifier() == 'numAttempts') { |
| 308 | $callIdItem .= '.' . (int)$variable->getValue(); |
| 309 | } |
| 310 | } |
| 311 | $resultStorage->storeItemVariables( |
| 312 | $deliveryExecutionId, |
| 313 | $test, |
| 314 | $itemResultIdentifier, |
| 315 | $itemVariables, |
| 316 | $callIdItem |
| 317 | ); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * @param DOMDocument $dom |
| 323 | * @param string $tag Xml tag to create |
| 324 | * @param string $data Data to escape |
| 325 | * @return DOMElement |
| 326 | */ |
| 327 | protected function createCDATANode($dom, $tag, $data) |
| 328 | { |
| 329 | $node = $dom->createCDATASection($data); |
| 330 | $returnValue = $dom->createElementNS(self::QTI_NS, $tag); |
| 331 | $returnValue->appendChild($node); |
| 332 | return $returnValue; |
| 333 | } |
| 334 | |
| 335 | private function createItemResultNode(DOMDocument $dom, string $itemResultIdentifier, array $itemResult): DOMElement |
| 336 | { |
| 337 | $identifierParts = explode('.', $itemResultIdentifier); |
| 338 | $occurrenceNumber = array_pop($identifierParts); |
| 339 | $refIdentifier = array_pop($identifierParts); |
| 340 | |
| 341 | $itemElement = $dom->createElementNS(self::QTI_NS, 'itemResult'); |
| 342 | $itemElement->setAttribute('identifier', $refIdentifier); |
| 343 | $itemElement->setAttribute('datestamp', $this->getDisplayDate($itemResult[0]['epoch'])); |
| 344 | $itemElement->setAttribute('sessionStatus', 'final'); |
| 345 | |
| 346 | return $itemElement; |
| 347 | } |
| 348 | |
| 349 | private function createItemVariableNode(DOMDocument $dom, bool $isResponseVariable, $itemVariable): DOMElement |
| 350 | { |
| 351 | /** Item variable */ |
| 352 | $itemVariableElement = $dom->createElementNS( |
| 353 | self::QTI_NS, |
| 354 | ($isResponseVariable) ? 'responseVariable' : 'outcomeVariable' |
| 355 | ); |
| 356 | $itemVariableElement->setAttribute('identifier', $itemVariable['identifier']); |
| 357 | $itemVariableElement->setAttribute('cardinality', $itemVariable['cardinality']); |
| 358 | $itemVariableElement->setAttribute('baseType', $itemVariable['basetype']); |
| 359 | |
| 360 | /** Split multiple response */ |
| 361 | $itemVariable['value'] = $this->prepareItemVariableValue($itemVariable['value'], $itemVariable['basetype']); |
| 362 | if ($itemVariable['cardinality'] !== Cardinality::getNameByConstant(Cardinality::SINGLE)) { |
| 363 | $values = explode(';', $itemVariable['value']); |
| 364 | $returnValue = []; |
| 365 | foreach ($values as $value) { |
| 366 | $returnValue[] = $this->createCDATANode($dom, 'value', $value); |
| 367 | } |
| 368 | } else { |
| 369 | $returnValue = $this->createCDATANode($dom, 'value', $itemVariable['value']); |
| 370 | } |
| 371 | |
| 372 | /** Get response parent element */ |
| 373 | if ($isResponseVariable) { |
| 374 | /** Response variable */ |
| 375 | $responseElement = $dom->createElementNS(self::QTI_NS, 'candidateResponse'); |
| 376 | } else { |
| 377 | /** Outcome variable */ |
| 378 | $responseElement = $itemVariableElement; |
| 379 | } |
| 380 | |
| 381 | /** Write a response node foreach answer */ |
| 382 | if (is_array($returnValue)) { |
| 383 | foreach ($returnValue as $valueElement) { |
| 384 | $responseElement->appendChild($valueElement); |
| 385 | } |
| 386 | } else { |
| 387 | $responseElement->appendChild($returnValue); |
| 388 | } |
| 389 | |
| 390 | if ($isResponseVariable) { |
| 391 | $itemVariableElement->appendChild($responseElement); |
| 392 | } |
| 393 | |
| 394 | return $itemVariableElement; |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * @throws common_Exception |
| 399 | */ |
| 400 | private function getDisplayDate(string $epoch): string |
| 401 | { |
| 402 | return tao_helpers_Date::displayeDate($epoch, tao_helpers_Date::FORMAT_ISO8601); |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * Prepares a variable value depending on it's baseType |
| 407 | */ |
| 408 | private function prepareItemVariableValue($value, $basetype): string |
| 409 | { |
| 410 | if ($basetype === 'file') { |
| 411 | return self::renderBinaryContentAsVariableValue($value); |
| 412 | } |
| 413 | |
| 414 | return trim($value, '[]'); |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * Tries to guess a MIME type from passed binary content and builds a properly formatted string |
| 419 | * @param string $binaryContent |
| 420 | * @return string |
| 421 | */ |
| 422 | public static function renderBinaryContentAsVariableValue(string $binaryContent): string |
| 423 | { |
| 424 | if (extension_loaded('fileinfo')) { |
| 425 | $info = new finfo(FILEINFO_MIME_TYPE); |
| 426 | $mimeType = $info->buffer($binaryContent); |
| 427 | } else { |
| 428 | $mimeType = 'application/octet-stream'; |
| 429 | } |
| 430 | |
| 431 | return sprintf('%s,base64,%s', $mimeType, base64_encode($binaryContent)); |
| 432 | } |
| 433 | |
| 434 | private function getResultStorage(): WritableResultStorage |
| 435 | { |
| 436 | return $this->getServiceLocator()->get(ResultServerService::SERVICE_ID)->getResultStorage(); |
| 437 | } |
| 438 | |
| 439 | protected function getMapper(): ResultMapper |
| 440 | { |
| 441 | return $this->getServiceLocator()->get(ResultMapper::class); |
| 442 | } |
| 443 | |
| 444 | protected function getAssessmentResultFileResponseResolver(): AssessmentResultFileResponseResolver |
| 445 | { |
| 446 | return $this->getServiceLocator()->getContainer()->get(AssessmentResultFileResponseResolver::class); |
| 447 | } |
| 448 | } |