Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make properties accessible and extendible #1341

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions config/default/assetParserFactoryService.conf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* 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) 2019 (original work) Open Assessment Technologies SA;
*
* @author Oleksandr Zagovorychev <[email protected]>
*/

use oat\taoQtiItem\model\qti\AssetParser;
use oat\taoQtiItem\model\qti\AssetParserFactoryService;

return new AssetParserFactoryService([
AssetParserFactoryService::OPTION_ASSET_PARSER => AssetParser::class
]);
26 changes: 26 additions & 0 deletions config/default/parserFactoryService.conf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* 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) 2019 (original work) Open Assessment Technologies SA;
*
* @author Oleksandr Zagovorychev <[email protected]>
*/
use oat\taoQtiItem\model\qti\ParserFactory;
use oat\taoQtiItem\model\qti\ParserFactoryService;

return new ParserFactoryService([
ParserFactoryService::OPTION_PARSER_FACTORY_CLASS => ParserFactory::class
]);
2 changes: 1 addition & 1 deletion manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
'name' => 'taoQtiItem',
'label' => 'QTI item model',
'license' => 'GPL-2.0',
'version' => '22.0.2',
'version' => '22.1.0',
'author' => 'Open Assessment Technologies',
'requires' => array(
'taoItems' => '>=9.1.0',
Expand Down
16 changes: 14 additions & 2 deletions model/Export/AbstractQTIItemExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
use oat\taoQtiItem\model\portableElement\exception\PortableElementException;
use oat\taoQtiItem\model\portableElement\exception\PortableElementInvalidAssetException;
use oat\taoQtiItem\model\portableElement\PortableElementService;
use oat\taoQtiItem\model\qti\AssetParserFactoryService;
use oat\taoQtiItem\model\qti\Element;
use oat\taoQtiItem\model\qti\exception\ExportException;
use oat\taoQtiItem\model\qti\Item;
use oat\taoQtiItem\model\qti\metadata\exporter\MetadataExporter;
use oat\taoQtiItem\model\qti\metadata\MetadataService;
use Psr\Http\Message\StreamInterface;
Expand Down Expand Up @@ -246,7 +248,7 @@ protected function getAssets(\core_kernel_classes_Resource $item, $lang)
if (is_null($qtiItem)) {
return [];
}
$assetParser = new AssetParser($qtiItem, $this->getStorageDirectory($item, $lang));
$assetParser = $this->getAssetParser($qtiItem, $this->getStorageDirectory($item, $lang));
$assetParser->setGetSharedLibraries(false);
$returnValue = array();
foreach ($assetParser->extract() as $type => $assets) {
Expand All @@ -269,7 +271,7 @@ protected function getPortableElementAssets(\core_kernel_classes_Resource $item,
return [];
}
$directory = $this->getStorageDirectory($item, $lang);
$assetParser = new AssetParser($qtiItem, $directory);
$assetParser = $this->getAssetParser($qtiItem, $directory);
$assetParser->setGetCustomElementDefinition(true);
return $assetParser->extractPortableAssetElements();
}
Expand Down Expand Up @@ -309,4 +311,14 @@ protected function getMetadataExporter()
}
return $this->metadataExporter;
}

/**
* @param Item $qtiItem
* @param Directory $directory
* @return AssetParser
*/
protected function getAssetParser(Item $qtiItem, Directory $directory)
{
return $this->getServiceManager()->get(AssetParserFactoryService::SERVICE_ID)->create($qtiItem, $directory);
}
}
15 changes: 15 additions & 0 deletions model/ItemModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
namespace oat\taoQtiItem\model;

use oat\oatbox\service\ConfigurableService;
use oat\tao\model\service\ServiceFileStorage;
use oat\taoItems\model\search\IndexableItemModel;
use oat\taoQtiItem\model\qti\Service;
use oat\taoQtiItem\model\search\QtiItemContentTokenizer;
Expand All @@ -31,6 +32,7 @@
use \core_kernel_classes_Resource;
use \common_Logger;
use taoItems_models_classes_itemModel;
use taoItems_models_classes_ItemCompiler;

/**
* Short description of class oat\taoQtiItem\model\ItemModel
Expand Down Expand Up @@ -154,4 +156,17 @@ public function getItemContentTokenizer()
{
return new QtiItemContentTokenizer();
}

/**
* @param core_kernel_classes_Resource $resource
* @param ServiceFileStorage $storage
* @return taoItems_models_classes_ItemCompiler
*/
public function getItemCompiler(core_kernel_classes_Resource $resource, ServiceFileStorage $storage)
{
$class = $this->getCompilerClass();
$compiler = new $class($resource, $storage);
$compiler->setServiceLocator($this->getServiceLocator());
return $compiler;
}
}
46 changes: 40 additions & 6 deletions model/QtiItemCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@

namespace oat\taoQtiItem\model;

use common_Exception;
use common_exception_Error;
use common_report_Report;
use core_kernel_classes_Resource;
use oat\oatbox\filesystem\Directory;
use oat\taoItems\model\ItemCompilerIndex;
use oat\taoQtiItem\model\compile\QtiItemCompilerAssetBlacklist;
use oat\taoQtiItem\model\qti\AssetParserFactoryService;
use oat\taoQtiItem\model\qti\exception\XIncludeException;
use oat\taoQtiItem\model\qti\Item;
use oat\taoQtiItem\model\qti\Service;
use tao_models_classes_FileNotFoundException;
use tao_models_classes_service_ConstantParameter;
use tao_models_classes_service_ServiceCall;
use tao_models_classes_service_StorageDirectory;
Expand Down Expand Up @@ -150,7 +154,7 @@ protected function deployQtiItem(
)
{
$itemService = taoItems_models_classes_ItemsService::singleton();
$qtiService = Service::singleton();
$qtiService = $this->getQtiService();

//copy item.xml file to private directory
$itemDir = $itemService->getItemDirectory($item, $language);
Expand Down Expand Up @@ -215,22 +219,42 @@ protected function deployQtiItem(
}
}

protected function getQtiService()
{
return Service::singleton();
}

/**
* @param $qtiItem
* @param $publicDirectory
* @return AssetParser
*/
protected function createAssetParser($qtiItem, $publicDirectory)
{
return $this->getServiceLocator()->get(AssetParserFactoryService::SERVICE_ID)->create($qtiItem, $publicDirectory);
}

/**
* @param core_kernel_classes_Resource $item
* @param string $lang
* @param $lang
* @param Directory $publicDirectory
* @return qti\Item
* @return Item
* @throws XIncludeException
* @throws tao_models_classes_FileNotFoundException
* @throws common_Exception
* @throws common_exception_Error
* @throws qti\exception\QtiModelException
* @throws taoItems_models_classes_CompilationFailedException
*/
protected function retrieveAssets(core_kernel_classes_Resource $item, $lang, Directory $publicDirectory)
{
$qtiItem = Service::singleton()->getDataItemByRdfItem($item, $lang);
$qtiItem = $this->getQtiService()->getDataItemByRdfItem($item, $lang);

if(is_null($qtiItem)){
throw new taoItems_models_classes_CompilationFailedException(__('Unable to retrieve item : ' . $item->getLabel()));
}

$assetParser = new AssetParser($qtiItem, $publicDirectory);
$assetParser = $this->createAssetParser($qtiItem, $publicDirectory);
$assetParser->setGetSharedLibraries(false);
$assetParser->setGetXinclude(false);
$resolver = new ItemMediaResolver($item, $lang);
Expand Down Expand Up @@ -294,7 +318,7 @@ protected function retrieveAssets(core_kernel_classes_Resource $item, $lang, Dir
throw new taoItems_models_classes_CompilationFailedException('Unable to load XML');
}

$qtiParser = new Parser($dom->saveXML());
$qtiParser = $this->getParser($dom->saveXML());
$assetRetrievedQtiItem = $qtiParser->load();

//loadxinclude
Expand All @@ -304,6 +328,16 @@ protected function retrieveAssets(core_kernel_classes_Resource $item, $lang, Dir
return $assetRetrievedQtiItem;
}

/**
* @param string $source
* @return Parser
* @throws common_Exception
* @throws common_exception_Error
*/
protected function getParser($source) {
return new Parser($source);
}

/**
* @param string $uri
* @param Item $qtiItem
Expand Down
3 changes: 2 additions & 1 deletion model/pack/QtiItemPacker.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use oat\oatbox\filesystem\Directory;
use oat\taoItems\model\pack\ItemPack;
use oat\taoItems\model\pack\ItemPacker;
use oat\taoQtiItem\model\qti\AssetParserFactoryService;
use oat\taoQtiItem\model\qti\Item;
use oat\taoQtiItem\model\qti\Parser as QtiParser;
use oat\taoQtiItem\model\qti\AssetParser;
Expand Down Expand Up @@ -106,7 +107,7 @@ public function packQtiItem($item, $lang, $qtiItem, Directory $directory)

$itemPack->setAssetEncoders($this->getAssetEncoders());

$assetParser = new AssetParser($qtiItem, $directory);
$assetParser = $directory->getServiceLocator()->get(AssetParserFactoryService::SERVICE_ID)->create($qtiItem, $directory);
$assetParser->setDeepParsing($this->isNestedResourcesInclusion());
$assetParser->setGetXinclude(!$this->replaceXinclude);

Expand Down
7 changes: 6 additions & 1 deletion model/qti/AssetParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,16 @@ public function extract(){
return $this->assets;
}

protected function getItem()
{
return $this->item;
}

/**
* Lookup and extract assets from IMG elements
* @param Element $element container of the target element
*/
private function extractImg(Element $element){
protected function extractImg(Element $element){
if($element instanceof Container){
foreach($element->getElements('oat\taoQtiItem\model\qti\Img') as $img){
$this->addAsset('img', $img->attr('src'));
Expand Down
49 changes: 49 additions & 0 deletions model/qti/AssetParserFactoryService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* 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) 2019 (original work) Open Assessment Technologies SA;
*
* @author Oleksandr Zagovorychev <[email protected]>
*/

namespace oat\taoQtiItem\model\qti;


use common_exception_Error;
use oat\oatbox\filesystem\Directory;
use oat\oatbox\service\ConfigurableService;

class AssetParserFactoryService extends ConfigurableService
{
const SERVICE_ID = 'taoQtiItem/assetParserFactoryService';

const OPTION_ASSET_PARSER = 'asset_parser';

/**
* @param Item $item
* @param Directory $directory
* @return AssetParser
* @throws common_exception_Error
*/
public function create(Item $item, Directory $directory)
{
$class = $this->getOption(self::OPTION_ASSET_PARSER);
if ($class !== AssetParser::class && !is_subclass_of($class, AssetParser::class)) {
throw new common_exception_Error(sprintf('Asset parser implement %s', AssetParser::class));
}
return new $class($item, $directory);
}
}
12 changes: 10 additions & 2 deletions model/qti/ImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,18 @@ public function importQTIPACKFile(
return $report;
}

/**
* @return AssetManager
*/
protected function getAssetManager()
{
return new AssetManager();
}


/**
* @param $folder
* @param \oat\taoQtiItem\model\qti\Resource $qtiItemResource
* @param Resource $qtiItemResource
* @param $itemClass
* @param array $dependencies
* @param array $metadataValues
Expand Down Expand Up @@ -491,7 +499,7 @@ public function importQtiItem(
$qtiIdentifierProperty = new \core_kernel_classes_Property(self::PROPERTY_QTI_ITEM_IDENTIFIER);
$rdfItem->editPropertyValues($qtiIdentifierProperty, $resourceIdentifier);

$itemAssetManager = new AssetManager();
$itemAssetManager = $this->getAssetManager();
$itemAssetManager->setItemContent($qtiModel->toXML());
$itemAssetManager->setSource($folder);

Expand Down
12 changes: 10 additions & 2 deletions model/qti/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
namespace oat\taoQtiItem\model\qti;

use oat\oatbox\service\ServiceManager;
use oat\taoQtiItem\model\qti\ParserFactory;
use oat\taoQtiItem\model\qti\exception\UnsupportedQtiElement;
use oat\taoQtiItem\model\ValidationService;
use \tao_models_classes_Parser;
Expand Down Expand Up @@ -149,7 +148,7 @@ public function load($resolveXInclude = false){
$basePath = dirname($this->source).'/';
}
//build the item from the xml
$parserFactory = new ParserFactory($xml, $basePath);
$parserFactory = $this->createParserFactory($xml);
try{
$returnValue = $parserFactory->load();
}catch(UnsupportedQtiElement $e){
Expand All @@ -168,6 +167,15 @@ public function load($resolveXInclude = false){
return $returnValue;
}

/**
* @param $xml
* @return ParserFactory
*/
protected function createParserFactory($xml)
{
return $this->getServiceManager()->get(ParserFactoryService::SERVICE_ID)->create($xml);
}

protected function addError($error){

$this->valid = false;
Expand Down
Loading