Skip to content

Commit

Permalink
Merge branch 'release-2.18.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
tikhanovichA committed Aug 14, 2020
2 parents 2a25a01 + 33a50f6 commit d6a098e
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 173 deletions.
202 changes: 105 additions & 97 deletions controller/Previewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,127 +15,73 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2018-2019 (original work) Open Assessment Technologies SA ;
* Copyright (c) 2018-2020 (original work) Open Assessment Technologies SA ;
*/

declare(strict_types=1);

namespace oat\taoQtiTestPreviewer\controller;

use Exception;
use common_exception_Error;
use oat\tao\helpers\Base64;
use tao_helpers_Http as HttpHelper;
use oat\taoItems\model\pack\Packer;
use common_Exception as CommonException;
use common_exception_BadRequest as BadRequestException;
use common_exception_MissingParameter as MissingParameterException;
use common_exception_NoImplementation as NoImplementationException;
use common_exception_NotImplemented as NotImplementedException;
use common_exception_Unauthorized as UnauthorizedException;
use common_exception_UserReadableException as UserReadableException;
use Exception;
use taoItems_models_classes_ItemsService;
use oat\generis\model\OntologyAwareTrait;
use oat\tao\model\media\sourceStrategy\HttpSource;
use oat\tao\model\routing\AnnotationReader\security;
use tao_actions_ServiceModule as ServiceModule;
use oat\taoItems\model\media\ItemMediaResolver;
use oat\taoQtiTestPreviewer\models\ItemPreviewer;
use oat\tao\model\media\sourceStrategy\HttpSource;
use oat\tao\model\routing\AnnotationReader\security;
use common_exception_BadRequest as BadRequestException;
use taoQtiTest_helpers_TestRunnerUtils as TestRunnerUtils;
use oat\taoQtiTestPreviewer\models\PreviewLanguageService;
use tao_actions_ServiceModule as ServiceModule;
use tao_helpers_Http as HttpHelper;
use common_exception_Unauthorized as UnauthorizedException;
use common_exception_NotImplemented as NotImplementedException;
use common_exception_MissingParameter as MissingParameterException;
use common_exception_NoImplementation as NoImplementationException;
use common_exception_UserReadableException as UserReadableException;
use tao_models_classes_FileNotFoundException as FileNotFoundException;
use taoItems_models_classes_ItemsService;
use taoQtiTest_helpers_TestRunnerUtils as TestRunnerUtils;
use oat\taoItems\model\pack\Packer;

/**
* Class taoQtiTest_actions_Runner
* Class Previewer
*
* Serves QTI implementation of the test runner
* @package oat\taoQtiTestPreviewer\controller
*/
class Previewer extends ServiceModule
{
use OntologyAwareTrait;

/**
* taoQtiTest_actions_Runner constructor.
* Previewer constructor.
*
* @security("hide")
*/
public function __construct()
{
parent::__construct();

// Prevent anything to be cached by the client.
TestRunnerUtils::noHttpClientCache();
}

/**
* Gets an error response array
* @param Exception $e
* @return array
*/
protected function getErrorResponse($e)
{
$response = [
'success' => false,
'type' => 'error',
];

if ($e instanceof FileNotFoundException) {
$response['type'] = 'FileNotFound';
$response['message'] = __('File not found');
} elseif ($e instanceof UnauthorizedException) {
$response['code'] = 403;
$response['message'] = $e->getUserMessage();
} elseif ($e instanceof UserReadableException) {
$response['message'] = $e->getUserMessage();
} elseif ($e instanceof Exception) {
$response['type'] = 'exception';
$response['code'] = $e->getCode();
$response['message'] = $e->getMessage();
} else {
$response['message'] = __('An error occurred!');
}

return $response;
}

/**
* Gets an HTTP response code
* @param Exception $e
* @return int
*/
protected function getErrorCode($e)
{
switch (true) {
case $e instanceof NotImplementedException:
case $e instanceof NoImplementationException:
case $e instanceof UnauthorizedException:
$code = 403;
break;

case $e instanceof FileNotFoundException:
$code = 404;
break;

default:
$code = 500;
break;
}

return $code;
}

/**
* Initializes the delivery session
*/
public function init()
public function init(): void
{
$code = 200;

try {
$this->validateCsrf();

$requestParams = $this->getPsrRequest()->getQueryParams();
$serviceCallId = $requestParams['serviceCallId'];

$response = [
'success' => $serviceCallId === 'previewer',
'itemIdentifier' => null,
'itemData' => null
'itemData' => null,
];
} catch (Exception $e) {
$response = $this->getErrorResponse($e);
Expand All @@ -149,16 +95,12 @@ public function init()
* Provides the definition data and the state for a particular item
*
* @param taoItems_models_classes_ItemsService $itemsService
*
* @return void
*/
public function getItem(taoItems_models_classes_ItemsService $itemsService)
public function getItem(taoItems_models_classes_ItemsService $itemsService): void
{
$code = 200;

try {
$this->validateCsrf();

$requestParams = $this->getPsrRequest()->getQueryParams();

$itemUri = $requestParams['itemUri'] ?? '';
Expand Down Expand Up @@ -202,15 +144,19 @@ public function getItem(taoItems_models_classes_ItemsService $itemsService)
$lang = $this->getSession()->getDataLanguage();

if (!$itemsService->hasItemContent($item, $lang)) {
return $this->returnJson($response, $code);
$this->returnJson($response, $code);
return;
}

$packer = new Packer($item, $lang);
$packer->setServiceLocator($this->getServiceLocator());

$itemPack = $packer->pack();
$response['content'] = $itemPack->JsonSerialize();
$response['baseUrl'] = _url('asset', null, null, ['uri' => $itemUri, 'path' => '']);
$response['baseUrl'] = _url('asset', null, null, [
'uri' => $itemUri,
'path' => '',
]);
} else {
throw new BadRequestException('Either itemUri or resultId needs to be provided.');
}
Expand All @@ -231,17 +177,15 @@ public function getItem(taoItems_models_classes_ItemsService $itemsService)
* @throws FileNotFoundException
* @throws common_exception_Error
*/
public function asset()
public function asset(): void
{
$requestParams = $this->getPsrRequest()->getQueryParams();
$itemUri = $requestParams['uri'];
$path = $requestParams['path'];

$item = $this->getResource($itemUri);
$item = $this->getResource($requestParams['uri']);
$lang = $this->getSession()->getDataLanguage();
$resolver = new ItemMediaResolver($item, $lang);

$asset = $resolver->resolve($path);
$asset = $resolver->resolve($requestParams['path']);
$mediaSource = $asset->getMediaSource();
$mediaIdentifier = $asset->getMediaIdentifier();

Expand All @@ -251,18 +195,18 @@ public function asset()

$info = $mediaSource->getFileInfo($mediaIdentifier);
$stream = $mediaSource->getFileStream($mediaIdentifier);

HttpHelper::returnStream($stream, $info['mime']);
}

/**
* Stores the state object and the response set of a particular item
*/
public function submitItem()
public function submitItem(): void
{
$code = 200;

try {
$this->validateCsrf();
$requestParams = $this->getPsrRequest()->getQueryParams();
$itemUri = $requestParams['itemUri'];
$jsonPayload = $this->getPayload();
Expand All @@ -275,23 +219,87 @@ public function submitItem()
$this->returnJson($response, $code);
}

/**
* Gets an error response array
*
* @param Exception $e
*
* @return array
*/
protected function getErrorResponse(Exception $e): array
{
$response = [
'success' => false,
'type' => 'error',
];

if ($e instanceof FileNotFoundException) {
$response['type'] = 'FileNotFound';
$response['message'] = __('File not found');
} elseif ($e instanceof UnauthorizedException) {
$response['code'] = 403;
$response['message'] = $e->getUserMessage();
} elseif ($e instanceof UserReadableException) {
$response['message'] = $e->getUserMessage();
} elseif ($e instanceof Exception) {
$response['type'] = 'exception';
$response['code'] = $e->getCode();
$response['message'] = $e->getMessage();
} else {
$response['message'] = __('An error occurred!');
}

return $response;
}

/**
* Gets an HTTP response code
*
* @param Exception $e
*
* @return int
*/
protected function getErrorCode(Exception $e): int
{
switch (true) {
case $e instanceof NotImplementedException:
case $e instanceof NoImplementationException:
case $e instanceof UnauthorizedException:
$code = 403;
break;

case $e instanceof FileNotFoundException:
$code = 404;
break;

default:
$code = 500;
break;
}

return $code;
}

/**
* @return ItemPreviewer
*/
private function getItemPreviewer()
private function getItemPreviewer(): ItemPreviewer
{
return $this->getServiceLocator()->get(ItemPreviewer::class);
/** @var ItemPreviewer $itemPreviewer */
$itemPreviewer = $this->getServiceLocator()->get(ItemPreviewer::class);

return $itemPreviewer;
}

/**
* Gets payload from the request
*
* @return array|mixed|object|null
*/
private function getPayload()
{
$jsonPayload = $this->getPsrRequest()->getParsedBody();
$jsonPayload = json_decode($jsonPayload['itemResponse'], true);

return $jsonPayload;
return json_decode($jsonPayload['itemResponse'], true);
}
}
2 changes: 1 addition & 1 deletion manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
'label' => 'extension-tao-testqti-previewer',
'description' => 'extension that provides QTI test previewer',
'license' => 'GPL-2.0',
'version' => '2.17.0',
'version' => '2.18.0',
'author' => 'Open Assessment Technologies SA',
'requires' => [
'generis' => '>=12.15.0',
Expand Down
2 changes: 1 addition & 1 deletion views/js/loader/qtiPreviewer.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion views/js/loader/qtiPreviewer.min.js.map

Large diffs are not rendered by default.

21 changes: 18 additions & 3 deletions views/js/previewer/proxy/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,12 @@ define([
this.configStorage = configFactory(config || {});

// request for initialization
return this.request(this.configStorage.getTestActionUrl('init'), params);
return this.request(
this.configStorage.getTestActionUrl('init'),
params,
void 0,
true
);
},

/**
Expand Down Expand Up @@ -174,7 +179,12 @@ define([
* Any error will be provided if rejected.
*/
getItem: function getItem(itemIdentifier, params) {
return this.request(this.configStorage.getItemActionUrl(itemIdentifier, 'getItem'), params);
return this.request(
this.configStorage.getItemActionUrl(itemIdentifier, 'getItem'),
params,
void 0,
true
);
},

/**
Expand All @@ -192,7 +202,12 @@ define([
itemResponse: response
}, params || {});

return this.request(this.configStorage.getItemActionUrl(itemIdentifier, 'submitItem'), body);
return this.request(
this.configStorage.getItemActionUrl(itemIdentifier, 'submitItem'),
body,
void 0,
true
);
}
};
});
Loading

0 comments on commit d6a098e

Please sign in to comment.