Skip to content

Commit

Permalink
Merge pull request #517 from oat-sa/release-4.1.0
Browse files Browse the repository at this point in the history
Release 4.1.0
  • Loading branch information
llecaque authored Jul 15, 2016
2 parents 3d555d4 + 6ff321d commit 87755a9
Show file tree
Hide file tree
Showing 25 changed files with 382 additions and 208 deletions.
43 changes: 24 additions & 19 deletions actions/class.RestQtiTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,36 @@ public function __construct()
*/
public function import()
{
$fileUploadName = "qtiPackage";
if ($this->getRequestMethod() != Request::HTTP_POST) {
throw new \common_exception_NotImplemented('Only post method is accepted to import Qti package.');
}
$file = tao_helpers_Http::getUploadedFile("qtiPackage");
$mimeType = tao_helpers_File::getMimeType($file['tmp_name']);
if (!in_array($mimeType, self::$accepted_types)) {
$this->returnFailure(new common_exception_BadRequest());
} else {
$report = $this->service->importQtiTest($file['tmp_name']);
if ($report->getType() === common_report_Report::TYPE_SUCCESS) {
$data = array();
foreach ($report as $r) {
$values = $r->getData();
$testid = $values->rdfsResource->getUri();
foreach ($values->items as $item) {
$itemsid[] = $item->getUri();
if(tao_helpers_Http::hasUploadedFile($fileUploadName)) {
$file = tao_helpers_Http::getUploadedFile($fileUploadName);
$mimeType = tao_helpers_File::getMimeType($file['tmp_name']);
if (!in_array($mimeType, self::$accepted_types)) {
$this->returnFailure(new common_exception_BadRequest());
} else {
$report = $this->service->importQtiTest($file['tmp_name']);
if ($report->getType() === common_report_Report::TYPE_SUCCESS) {
$data = array();
foreach ($report as $r) {
$values = $r->getData();
$testid = $values->rdfsResource->getUri();
foreach ($values->items as $item) {
$itemsid[] = $item->getUri();
}
$data[] = array(
'testId' => $testid,
'testItems' => $itemsid);
}
$data[] = array(
'testId' => $testid,
'testItems' => $itemsid);
return $this->returnSuccess($data);
} else {
return $this->returnFailure(new common_exception_InconsistentData($report->getMessage()));
}
return $this->returnSuccess($data);
} else {
return $this->returnFailure(new common_exception_InvalidArgumentType($report->getMessage()));
}
} else {
return $this->returnFailure(new common_exception_BadRequest());
}
}
}
4 changes: 2 additions & 2 deletions doc/rest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"info": {
"version": "1.0.0",
"title": "QTI Test REST API",
"description": "#### The QTI Test REST API\nAn API for the QTI Test specific actions. For generic CRUD actions on tests please see the taoTest Rest API\n"
"description": "#### The QTI Test REST API. An API for the QTI Test specific actions. For generic CRUD actions on tests please see the taoTest Rest API"
},
"schemes": [
"https",
Expand All @@ -22,7 +22,7 @@
"paths": {
"/taoQtiTest/RestQtiTests/import": {
"post": {
"description": "Import a QTI test package into the TAO platform.\n\nAvailable since Tao 3.1.\n",
"description": "Import a QTI test package into the TAO platform. Available since Tao 3.1.",
"tags": [
"test"
],
Expand Down
7 changes: 4 additions & 3 deletions manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@
'label' => 'QTI test model',
'description' => 'TAO QTI test implementation',
'license' => 'GPL-2.0',
'version' => '3.1.0',
'version' => '4.1.0',
'author' => 'Open Assessment Technologies',
'requires' => array(
'taoTests' => '>=2.19.0',
'taoQtiItem' => '>=3.0.0'
'taoTests' => '>=2.21.0',
'taoQtiItem' => '>=3.1',
'tao' => '>=5.3.0'
),
'models' => array(
'http://www.tao.lu/Ontologies/TAOTest.rdf'
Expand Down
15 changes: 9 additions & 6 deletions models/classes/ExtendedStateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ExtendedStateService
const VAR_SECURITY_TOKEN = 'security_token';
const VAR_SESSION_TOKEN = 'session_token';

private $cache = null;
private static $cache = null;

/**
* Retrieves extended state information
Expand All @@ -42,7 +42,7 @@ class ExtendedStateService
*/
protected function getExtra($testSessionId)
{
if (!isset($this->cache[$testSessionId])) {
if (!isset(self::$cache[$testSessionId])) {
$storageService = \tao_models_classes_service_StateStorage::singleton();
$userUri = \common_session_SessionManager::getSession()->getUserUri();

Expand All @@ -54,12 +54,12 @@ protected function getExtra($testSessionId)
}
} else {
$data = array(
self::VAR_REVIEW => array()
self::VAR_REVIEW => array()
);
}
$this->cache[$testSessionId] = $data;
self::$cache[$testSessionId] = $data;
}
return $this->cache[$testSessionId];
return self::$cache[$testSessionId];
}

/**
Expand All @@ -69,7 +69,9 @@ protected function getExtra($testSessionId)
*/
protected function saveExtra($testSessionId, $extra)
{
$this->cache[$testSessionId] = $extra;

self::$cache[$testSessionId] = $extra;

$storageService = \tao_models_classes_service_StateStorage::singleton();
$userUri = \common_session_SessionManager::getSession()->getUserUri();

Expand Down Expand Up @@ -102,6 +104,7 @@ public function setItemFlag($testSessionId, $itemRef, $flag)
{
$extra = $this->getExtra($testSessionId);
$extra[self::VAR_REVIEW][$itemRef] = $flag;

$this->saveExtra($testSessionId, $extra);
}

Expand Down
31 changes: 15 additions & 16 deletions models/classes/class.QtiTestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

use oat\taoQtiItem\model\qti\Resource;
use oat\taoQtiItem\model\qti\ImportService;
use oat\taoTests\models\event\TestUpdatedEvent;
use qtism\data\storage\StorageException;
use qtism\data\storage\xml\XmlDocument;
use qtism\data\storage\xml\marshalling\UnmarshallingException;
Expand All @@ -43,7 +44,7 @@
*/
class taoQtiTest_models_classes_QtiTestService extends taoTests_models_classes_TestsService {

const CONFIG_QTITEST_FOLDER = 'qtiTestFolder';
const CONFIG_QTITEST_FILESYSTEM = 'qtiTestFolder';

const CONFIG_QTITEST_ACCEPTABLE_LATENCY = 'qtiAcceptableLatency';

Expand Down Expand Up @@ -88,6 +89,9 @@ public function saveJsonTest(core_kernel_classes_Resource $test, $json) {
$converter->fromJson($json);

$saved = $this->saveDoc($test, $doc);

$this->getEventManager()->trigger(new TestUpdatedEvent($test->getUri()));

}
return $saved;
}
Expand Down Expand Up @@ -913,13 +917,8 @@ private function saveDoc( core_kernel_classes_Resource $test, XmlDocument $doc){
*/
public function createContent( core_kernel_classes_Resource $test, $createTestFile = true) {

$props = self::getQtiTestDirectory()->getPropertiesValues(array(
PROPERTY_FILE_FILESYSTEM,
PROPERTY_FILE_FILEPATH
));

$repository = new core_kernel_versioning_Repository(current($props[PROPERTY_FILE_FILESYSTEM]));
$path = ((string)current($props[PROPERTY_FILE_FILEPATH])).DIRECTORY_SEPARATOR. md5($test->getUri());
$repository = self::getQtiTestFileSystem();
$path = md5($test->getUri());

// $directory is the directory where test related resources will be stored.
$directory = $repository->createFile('', $path );
Expand Down Expand Up @@ -989,28 +988,28 @@ public function deleteContent(core_kernel_classes_Resource $test)

/**
* Set the directory where the tests' contents are stored.
* @param core_kernel_file_File $folder
* @param core_kernel_versioning_Repository $folder
*/
public function setQtiTestDirectory(core_kernel_file_File $folder)
public function setQtiTestFileSystem(core_kernel_versioning_Repository $folder)
{
$ext = common_ext_ExtensionsManager::singleton()->getExtensionById('taoQtiTest');
$ext->setConfig(self::CONFIG_QTITEST_FOLDER, $folder->getUri());
$ext->setConfig(self::CONFIG_QTITEST_FILESYSTEM, $folder->getUri());
}

/**
* Get the directory where the tests' contents are stored.
*
* @return core_kernel_file_File
* @return core_kernel_versioning_Repository
* @throws common_Exception
*/
public function getQtiTestDirectory()
public function getQtiTestFileSystem()
{
$ext = common_ext_ExtensionsManager::singleton()->getExtensionById('taoQtiTest');
$uri = $ext->getConfig(self::CONFIG_QTITEST_FOLDER);
$uri = $ext->getConfig(self::CONFIG_QTITEST_FILESYSTEM);
if (empty($uri)) {
throw new common_Exception('No default repository defined for uploaded files storage.');
throw new common_Exception('No default file system defined for QTI test files storage.');
}
return new core_kernel_file_File($uri);
return new core_kernel_versioning_Repository($uri);
}

/**
Expand Down
98 changes: 98 additions & 0 deletions scripts/cli/importMultipleTestsFromDir.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace oat\taoQtiTest\scripts\cli;

use oat\generis\model\OntologyAwareTrait;
use oat\oatbox\action\Action;
use oat\oatbox\service\ServiceManager;
use oat\taoQtiItem\model\qti\exception\ExtractException;
use oat\taoQtiItem\model\qti\exception\ParsingException;

class importMultipleTestsFromDir implements Action
{
use OntologyAwareTrait;

protected $uploadDirectoryPath;
protected $uploadDirectoryUri;

protected function init()
{
$this->uploadDirectoryPath = FILES_PATH . 'tao/upload/';
if (!file_exists($this->uploadDirectoryPath)) {
throw new \Exception('Unable to find ' . $this->uploadDirectoryPath);
}

\common_ext_ExtensionsManager::singleton()->getExtensionById('taoQtiTest');

$this->uploadDirectoryUri = \common_ext_ExtensionsManager::singleton()
->getExtensionById('tao')
->getConfig('defaultUploadFileSource');

}

public function __invoke($params = [])
{
try {
$this->init();

$uploadFileSystem = new \core_kernel_fileSystem_FileSystem($this->uploadDirectoryUri);

$dir = new \tao_models_classes_service_StorageDirectory(
$this->uploadDirectoryUri,
$uploadFileSystem,
'/import', null
);
$dir->setServiceLocator(ServiceManager::getServiceManager());

/** @var \ArrayIterator $iterator */
$tests = 0;
$iterator = $dir->getIterator();
while ($iterator->valid()) {
if (substr($iterator->current(), 0, 1) !== '.') {
$this->importTest($this->uploadDirectoryPath . $iterator->current());
$tests++;
echo $iterator->current() . ' imported.' . PHP_EOL;
} else {
echo $iterator->current() . ' skipped.' . PHP_EOL;
}
\tao_helpers_File::remove($this->uploadDirectoryPath . $iterator->current());
$iterator->next();
}
return $this->returnSuccess($tests);
} catch (ExtractException $e) {
return $this->returnFailure('The ZIP archive containing the IMS QTI Item cannot be extracted.');
} catch (ParsingException $e) {
return $this->returnFailure('The ZIP archive does not contain an imsmanifest.xml file or is an invalid ZIP archive.');
} catch (\Exception $e) {
return $this->returnFailure($e->getMessage());
}
}

protected function importTest($package)
{
// Call service to import package
\helpers_TimeOutHelper::setTimeOutLimit(\helpers_TimeOutHelper::LONG);
$report = \taoQtiTest_models_classes_QtiTestService::singleton()
->importMultipleTests($this->getDestinationClass(), $package);

if ($report->getType() !== \common_report_Report::TYPE_SUCCESS) {
throw new \Exception("ERROR: " . $report->getMessage());
}
return true;
}

protected function returnFailure($msg)
{
return new \common_report_Report(\common_report_Report::TYPE_ERROR, $msg);
}

protected function returnSuccess($testsCount)
{
return new \common_report_Report(\common_report_Report::TYPE_SUCCESS, $testsCount . ' tests imported');
}

protected function getDestinationClass()
{
return new \core_kernel_classes_Class(TAO_TEST_CLASS);
}
}
11 changes: 6 additions & 5 deletions scripts/install/addQtiTestFolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@
/*
* This post-installation script creates a fodler for the QTI Tests
*/
$dataPath = FILES_PATH . 'taoQtiTest' . DIRECTORY_SEPARATOR;
$dataPath = FILES_PATH . 'taoQtiTest' . DIRECTORY_SEPARATOR. 'testData' . DIRECTORY_SEPARATOR;
if (file_exists($dataPath)) {
helpers_File::emptyDirectory($dataPath);
}

$source = tao_models_classes_FileSourceService::singleton()->addLocalSource('QTI test datasource', $dataPath);
mkdir($dataPath.'testData');
$directory = new core_kernel_file_File($source->createFile('', 'testData'));
$serviceManager = oat\oatbox\service\ServiceManager::getServiceManager();
$fsService = $serviceManager->get(oat\oatbox\filesystem\FileSystemService::SERVICE_ID);
$fsService->registerLocalFileSystem('http://taoQtiTest#fs', FILES_PATH . 'taoQtiTest');
$serviceManager->register(oat\oatbox\filesystem\FileSystemService::SERVICE_ID, $fsService);

taoQtiTest_models_classes_QtiTestService::singleton()->setQtiTestDirectory($directory);
taoQtiTest_models_classes_QtiTestService::singleton()->setQtiTestFileSystem(new core_kernel_fileSystem_FileSystem('http://taoQtiTest#fs'));
16 changes: 16 additions & 0 deletions scripts/update/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ public function update($initialVersion) {

$this->setVersion('2.31.1');
}

$this->skip('2.31.1', '3.0.0');

if ($this->isVersion('3.0.0')) {
Expand All @@ -440,5 +441,20 @@ public function update($initialVersion) {

$this->setVersion('3.1.0');
}

$this->skip('3.1.0', '3.4.0');

if ($this->isVersion('3.4.0')) {
$ext = \common_ext_ExtensionsManager::singleton()->getExtensionById('taoQtiTest');
$uri = $ext->getConfig(\taoQtiTest_models_classes_QtiTestService::CONFIG_QTITEST_FILESYSTEM);
$dir = new \core_kernel_file_File($uri);

$fs = $dir->getFileSystem();
\taoQtiTest_models_classes_QtiTestService::singleton()->setQtiTestFileSystem($fs);

$this->setVersion('4.0.0');
}

$this->skip('4.0.0', '4.1.0');
}
}
4 changes: 1 addition & 3 deletions test/QtiTestServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,7 @@ public function testCreateContent()
{
$attrValue = '"A & B < C"';

$qtiTest = $this->testService->createInstance($this->testService->getRootclass(), 'UnitTestQtiItem');
$qtiTest->setLabel($attrValue);
$this->testService->createContent($qtiTest);
$qtiTest = $this->testService->createInstance($this->testService->getRootclass(), $attrValue);
$xmlFilePath = $this->testService->getDocPath($qtiTest);
$this->assertTrue(file_exists($xmlFilePath));

Expand Down
Loading

0 comments on commit 87755a9

Please sign in to comment.