checkMethod(); $this->validateParams([self::DELIVERY, self::RESULT]); $deliveryId = $this->getRequestParameter(self::DELIVERY); $resultId = $this->getRequestParameter(self::RESULT); $fetchOnlyLastAttemptResult = filter_var($this->getRequestParameter(self::LAST_RESULT), FILTER_VALIDATE_BOOLEAN); $this->returnValidXml($this->getQtiResultService()->getQtiResultXml($deliveryId, $resultId, $fetchOnlyLastAttemptResult)); } catch (Exception $e) { $this->returnFailure($e); } } /** * Return the service for Result * * @return ResultService */ protected function getQtiResultService() { if (!$this->service) { $this->service = $this->getServiceManager()->get(ResultService::SERVICE_ID); } return $this->service; } /** * It's based on TestTaker and Delivery. Parameters are in URI format. * It fetches the latest Delivery Execution. * * There are two endpoints because of swagger. In this way, it's possible to describe the parameters properly. * * @author Gyula Szucs, */ public function getLatest() { try { $this->checkMethod(); $this->validateParams([self::TESTTAKER, self::DELIVERY]); $deliveryExecution = $this->getQtiResultService()->getDeliveryExecutionByTestTakerAndDelivery( $this->getRequestParameter(self::DELIVERY), $this->getRequestParameter(self::TESTTAKER) ); $this->returnValidXml($this->getQtiResultService()->getDeliveryExecutionXml($deliveryExecution)); } catch (Exception $e) { $this->returnFailure($e); } } /** * It requires only Delivery Execution in URI format. * * @author Gyula Szucs, */ public function byDeliveryExecution() { try { $this->checkMethod(); $this->validateParams([self::DELIVERY_EXECUTION]); $deliveryExecution = $this->getQtiResultService()->getDeliveryExecutionById( $this->getRequestParameter(self::DELIVERY_EXECUTION) ); $this->returnValidXml($this->getQtiResultService()->getDeliveryExecutionXml($deliveryExecution)); } catch (Exception $e) { $this->returnFailure($e); } } /** * Checks the required request method. * * @author Gyula Szucs, * @throws common_exception_MethodNotAllowed */ protected function checkMethod() { if ($this->getRequestMethod() != 'GET') { throw new common_exception_MethodNotAllowed($this->getRequestURI()); } } /** * Validates the given parameters. * * @author Gyula Szucs, * @param array $params * @throws common_exception_MissingParameter * @throws common_exception_ValidationFailed */ protected function validateParams(array $params) { foreach ($params as $param) { if (!$this->hasRequestParameter($param)) { throw new common_exception_MissingParameter($param . ' is missing from the request.', $this->getRequestURI()); } if (empty($this->getRequestParameter($param))) { throw new common_exception_ValidationFailed($param, $param . ' cannot be empty'); } } } /** * Return XML response if it is valid. * * @param string $data * @throws Exception * @throws common_exception_NotFound */ protected function returnValidXml($data) { if (empty($data)) { throw new common_exception_NotFound('Delivery execution not found.'); } // delete control characters $data = preg_replace('/[\x00-\x1F\x7F]/', '', $data); $doc = @simplexml_load_string($data); if (!$doc) { common_Logger::i('invalid xml result'); throw new Exception('Xml output is malformed.'); } // force XML content type header header('Content-Type: application/xml'); echo $data; exit(0); } }