Skip to content

Commit

Permalink
Merge branch 'release-3.6.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed May 10, 2022
2 parents 23d4750 + b84c1ab commit 0c6a7a2
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 28 deletions.
81 changes: 81 additions & 0 deletions model/LegacyPciHelper/ImageToPropertiesHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?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) 2022 (original work) Open Assessment Technologies SA;
*/

declare(strict_types=1);

namespace oat\pciSamples\model\LegacyPciHelper;

use oat\oatbox\filesystem\Directory;
use oat\taoMediaManager\model\fileManagement\FileManagement;
use oat\taoMediaManager\model\MediaSource;

class ImageToPropertiesHelper
{
/** @var MediaSource */
private $mediaSource;

/** @var FileManagement */
private $fileManagement;

public function __construct(MediaSource $mediaSource, FileManagement $fileManagement)
{
$this->mediaSource = $mediaSource;
$this->fileManagement = $fileManagement;
}

public function addImagesToProperties(array $images, array $properties, Directory $itemDirectory): array
{
foreach ($images as $image) {
if ($this->isImageMediaManager($image['fileName'])) {
$fileInfo = $this->mediaSource->getFileInfo($image['fileName']);
$properties = $this->addBase64Image(
$properties,
$image['fileName'],
$this->fileManagement->getFileStream(
$fileInfo['link']
)->read(1024 * 8)
);
continue;
}
$properties = $this->addBase64Image(
$properties,
$image['fileName'],
$itemDirectory->getFile($image['fileName'])->read()
);
}

return $properties;
}

private function addBase64Image(array $properties, string $fileName, string $image): array
{
$properties['content-' . $fileName] = sprintf(
"data:image/png;base64,%s",
base64_encode($image)
);

return $properties;
}

private function isImageMediaManager(string $fileName): bool
{
return false !== strpos($fileName, 'taomedia://');
}
}
58 changes: 30 additions & 28 deletions model/LegacyPciHelper/Task/UpgradeTextReaderInteractionTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
use oat\oatbox\filesystem\Directory;
use oat\oatbox\reporting\Report;
use oat\oatbox\service\ServiceManagerAwareTrait;
use oat\pciSamples\model\LegacyPciHelper\ImageToPropertiesHelper;
use oat\pciSamples\model\LegacyPciHelper\TextReaderLegacyDetection;
use oat\taoQtiItem\helpers\QtiFile;
use oat\taoQtiItem\model\qti\interaction\CustomInteraction;
use oat\taoQtiItem\model\qti\interaction\PortableCustomInteraction;
use oat\taoQtiItem\model\qti\Item;
use oat\taoQtiItem\model\qti\Parser;
Expand All @@ -55,32 +57,24 @@ public function __invoke($params)
}

$this->itemDirectory = $this->getItemService()->getItemDirectory($itemResource);
$itemXmlFile = $this->getItemService()->getItemDirectory($itemResource)->getFile(QtiFile::FILE);
$itemXmlFile = $this->itemDirectory->getFile(QtiFile::FILE);
$parser = new Parser($itemXmlFile->read());
$xmlItem = $parser->load();

if (!$this->isLegacyTextReader($xmlItem)) {
return Report::createSuccess("Item does not contain Legacy PCI Text Reader");
}

/** @var PortableCustomInteraction $pciInteraction */
foreach ($xmlItem->getBody()->getElements(PortableCustomInteraction::class) as $pciInteraction) {
if ($this->getTextReaderLegacyDetection()->isTextReaderWithImage($pciInteraction)) {
$properties = $pciInteraction->getProperties();
foreach ($properties['pages'] as $page) {
$images = $this->extractImages($page['content']);
$properties = $this->addImagesToProperties($images, $properties);
}

$pciInteraction->setProperties($properties);
}
}

try {
$this->addImagesToProperties($xmlItem);
$this->getQtiService()->saveDataItemToRdfItem($xmlItem, $itemResource);
} catch (Exception $e) {
return Report::createError(
sprintf('Task failed with item %s', $itemResource->getUri())
sprintf(
'Task failed with item %s with error: %s',
$itemResource->getUri(),
$e->getMessage()
)
);
}
return Report::createSuccess(
Expand Down Expand Up @@ -108,21 +102,9 @@ private function extractImages(array $content): array
return $images;
}

private function addImagesToProperties(array $images, array $properties): array
{
foreach ($images as $image) {
$properties['content-' . $image['fileName']] = sprintf(
"data:image/png;base64,%s",
base64_encode($this->itemDirectory->getFile($image['fileName'])->read())
);
}

return $properties;
}

private function getPciInteractions(Item $xmlItem): array
{
return $xmlItem->getBody()->getElements(PortableCustomInteraction::class);
return $xmlItem->getBody()->getElements(CustomInteraction::class);
}

private function isLegacyTextReader(Item $item): bool
Expand Down Expand Up @@ -160,4 +142,24 @@ private function getQtiService(): QtiService
{
return $this->getServiceManager()->getContainer()->get(QtiService::class);
}

private function getImageToPropertyHelper(): ImageToPropertiesHelper
{
return $this->getServiceManager()->getContainer()->get(ImageToPropertiesHelper::class);
}

private function addImagesToProperties(Item $xmlItem): void
{
foreach ($xmlItem->getBody()->getElements(PortableCustomInteraction::class) as $pciInteraction) {
if ($this->getTextReaderLegacyDetection()->isTextReaderWithImage($pciInteraction)) {
$properties = $pciInteraction->getProperties();
foreach ($properties['pages'] as $page) {
$images = $this->extractImages($page['content']);
$properties = $this->getImageToPropertyHelper()->addImagesToProperties($images, $properties, $this->itemDirectory);
}

$pciInteraction->setProperties($properties);
}
}
}
}
13 changes: 13 additions & 0 deletions model/ServiceProvider/UpgradeProcessServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
namespace oat\pciSamples\model\ServiceProvider;

use oat\generis\model\DependencyInjection\ContainerServiceProviderInterface;
use oat\pciSamples\model\LegacyPciHelper\ImageToPropertiesHelper;
use oat\pciSamples\model\LegacyPciHelper\LegacyTextReaderItemUpdate;
use oat\pciSamples\model\LegacyPciHelper\TextReaderLegacyDetection;
use oat\tao\model\taskQueue\QueueDispatcherInterface;
use oat\taoMediaManager\model\fileManagement\FileManagement;
use oat\taoMediaManager\model\MediaSource;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use taoItems_models_classes_ItemsService;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
Expand All @@ -37,6 +40,16 @@ public function __invoke(ContainerConfigurator $configurator): void
{
$services = $configurator->services();

$services->set(MediaSource::class, MediaSource::class)
->private();

$services->set(ImageToPropertiesHelper::class, ImageToPropertiesHelper::class)
->public()
->args([
service(MediaSource::class),
service(FileManagement::SERVICE_ID)
]);

$services
->set(LegacyTextReaderItemUpdate::class, LegacyTextReaderItemUpdate::class)
->public()
Expand Down

0 comments on commit 0c6a7a2

Please sign in to comment.