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

feat: advanced search support #4117

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
30 changes: 30 additions & 0 deletions migrations/Version202411111300522237_tao.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace oat\tao\migrations;

use Doctrine\DBAL\Schema\Schema;
use oat\tao\scripts\tools\migrations\AbstractMigration;
use oat\tao\scripts\update\OntologyUpdater;

/**
* phpcs:disable Squiz.Classes.ValidClassName
*/
final class Version202411111300522237_tao extends AbstractMigration
{
public function getDescription(): string
{
return 'Sync models';
}

public function up(Schema $schema): void
{
OntologyUpdater::syncModels();
}

public function down(Schema $schema): void
{
OntologyUpdater::syncModels();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use tao_helpers_form_elements_Combobox as ComboBox;
use tao_helpers_form_elements_Htmlarea as HtmlArea;
use tao_helpers_form_elements_Radiobox as RadioBox;
use tao_helpers_form_elements_Readonly;
use tao_helpers_form_elements_Textarea as TextArea;
use tao_helpers_form_elements_Textbox as TextBox;
use tao_helpers_form_elements_xhtml_Searchtextbox as SearchTextBox;
Expand All @@ -49,6 +50,7 @@ class GetClassMetadataValuesService extends ConfigurableService
TextBox::WIDGET_ID,
TextArea::WIDGET_ID,
HtmlArea::WIDGET_ID,
tao_helpers_form_elements_Readonly::WIDGET_ID,
];
public const LIST_WIDGETS = [
RadioBox::WIDGET_ID,
Expand Down
1 change: 1 addition & 0 deletions models/classes/TaoOntology.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,6 @@ interface TaoOntology
public const PROPERTY_UNIQUE_IDENTIFIER = 'http://www.tao.lu/Ontologies/TAO.rdf#UniqueIdentifier';
public const PROPERTY_TRANSLATION_ORIGINAL_RESOURCE_URI = 'http://www.tao.lu/Ontologies/TAO.rdf#TranslationOriginalResourceUri';
public const PROPERTY_LANGUAGE = 'http://www.tao.lu/Ontologies/TAO.rdf#Language';
public const PROPERTY_TRANSLATED_INTO_LANGUAGES = 'http://www.tao.lu/Ontologies/TAO.rdf#TranslatedIntoLanguages';
public const LANGUAGE_PREFIX = 'http://www.tao.lu/Ontologies/TAO.rdf#Lang';
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use core_kernel_classes_Resource;
use oat\generis\model\data\Ontology;
use oat\tao\model\featureFlag\FeatureFlagCheckerInterface;
use oat\tao\model\featureFlag\Service\FeatureFlagPropertiesMapping;
use oat\tao\model\form\Modifier\AbstractFormModifier;
use oat\tao\model\TaoOntology;
use tao_helpers_form_Form;
Expand All @@ -34,11 +35,16 @@
class TranslationFormModifier extends AbstractFormModifier
{
private FeatureFlagCheckerInterface $featureFlagChecker;
private FeatureFlagPropertiesMapping $featureFlagPropertiesMapping;
private Ontology $ontology;

public function __construct(FeatureFlagCheckerInterface $featureFlagChecker, Ontology $ontology)
{
public function __construct(
FeatureFlagCheckerInterface $featureFlagChecker,
FeatureFlagPropertiesMapping $featureFlagPropertiesMapping,
Ontology $ontology
) {
$this->featureFlagChecker = $featureFlagChecker;
$this->featureFlagPropertiesMapping = $featureFlagPropertiesMapping;
$this->ontology = $ontology;
}

Expand All @@ -52,17 +58,12 @@ public function modify(tao_helpers_form_Form $form, array $options = []): void
private function getTranslationElementsToRemove(tao_helpers_form_Form $form): array
{
if (!$this->featureFlagChecker->isEnabled('FEATURE_FLAG_TRANSLATION_ENABLED')) {
return [
TaoOntology::PROPERTY_TRANSLATION_ORIGINAL_RESOURCE_URI,
TaoOntology::PROPERTY_LANGUAGE,
TaoOntology::PROPERTY_TRANSLATION_STATUS,
TaoOntology::PROPERTY_TRANSLATION_PROGRESS,
TaoOntology::PROPERTY_TRANSLATION_TYPE,
];
return $this->featureFlagPropertiesMapping->getFeatureProperties('FEATURE_FLAG_TRANSLATION_ENABLED');
}

$elementsToRemove = [
TaoOntology::PROPERTY_TRANSLATION_TYPE
TaoOntology::PROPERTY_TRANSLATION_TYPE,
shpran marked this conversation as resolved.
Show resolved Hide resolved
TaoOntology::PROPERTY_TRANSLATED_INTO_LANGUAGES,
];

$instance = $this->ontology->getResource($form->getValue('uri'));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?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) 2024 (original work) Open Assessment Technologies SA.
*/

declare(strict_types=1);

namespace oat\tao\model\Translation\Service;

use core_kernel_classes_Resource;
use oat\generis\model\data\Ontology;
use oat\tao\model\TaoOntology;
use oat\tao\model\Translation\Entity\AbstractResource;
use oat\tao\model\Translation\Query\ResourceTranslationQuery;
use oat\tao\model\Translation\Repository\ResourceTranslationRepository;

class TranslatedIntoLanguagesSynchronizer
{
private Ontology $ontology;
private ResourceTranslationRepository $resourceTranslationRepository;

public function __construct(Ontology $ontology, ResourceTranslationRepository $resourceTranslationRepository)
{
$this->ontology = $ontology;
$this->resourceTranslationRepository = $resourceTranslationRepository;
}

public function sync(core_kernel_classes_Resource $resource): void
{
$originalResource = $this->getOriginalResource($resource);

$property = $this->ontology->getProperty(TaoOntology::PROPERTY_TRANSLATED_INTO_LANGUAGES);
$originalResource->removePropertyValues($property);

/** @var AbstractResource[] $translations */
$translations = $this->resourceTranslationRepository->find(
new ResourceTranslationQuery([$originalResource->getUri()])
);

foreach ($translations as $translation) {
$originalResource->setPropertyValue(
$property,
TaoOntology::LANGUAGE_PREFIX . $translation->getLanguageCode()
);
}
}

private function getOriginalResource(core_kernel_classes_Resource $resource): core_kernel_classes_Resource
{
$originalResourceUriProperty = $this->ontology->getProperty(
TaoOntology::PROPERTY_TRANSLATION_ORIGINAL_RESOURCE_URI
);
$originalResourceUri = $resource->getOnePropertyValue($originalResourceUriProperty);

return !empty($originalResourceUri)
? $this->ontology->getResource($originalResourceUri)
: $resource;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class TranslationCreationService
private ResourceTranslationRepository $resourceTranslationRepository;
private LanguageRepositoryInterface $languageRepository;
private LoggerInterface $logger;
private TranslatedIntoLanguagesSynchronizer $translatedIntoLanguagesSynchronizer;

private array $resourceTransferServices;
private array $callables;

Expand All @@ -55,13 +57,15 @@ public function __construct(
ResourceTranslatableRepository $resourceTranslatableRepository,
ResourceTranslationRepository $resourceTranslationRepository,
LanguageRepositoryInterface $languageRepository,
LoggerInterface $logger
LoggerInterface $logger,
TranslatedIntoLanguagesSynchronizer $translatedIntoLanguagesSynchronizer
) {
$this->ontology = $ontology;
$this->resourceTranslatableRepository = $resourceTranslatableRepository;
$this->resourceTranslationRepository = $resourceTranslationRepository;
$this->languageRepository = $languageRepository;
$this->logger = $logger;
$this->translatedIntoLanguagesSynchronizer = $translatedIntoLanguagesSynchronizer;
}

public function setResourceTransfer(string $resourceType, ResourceTransferInterface $resourceTransfer): void
Expand Down Expand Up @@ -188,6 +192,8 @@ public function create(CreateTranslationCommand $command): core_kernel_classes_R
$callable($clonedInstance);
}

$this->translatedIntoLanguagesSynchronizer->sync($instance);

return $clonedInstance;
} catch (Throwable $exception) {
$this->logger->error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,20 @@ class TranslationDeletionService
private ResourceDeleterInterface $resourceDeleter;
private ResourceTranslationRepository $resourceTranslationRepository;
private LoggerInterface $logger;
private TranslatedIntoLanguagesSynchronizer $translatedIntoLanguagesSynchronizer;

public function __construct(
Ontology $ontology,
ResourceDeleterInterface $resourceDeleter,
ResourceTranslationRepository $resourceTranslationRepository,
LoggerInterface $logger
LoggerInterface $logger,
TranslatedIntoLanguagesSynchronizer $translatedIntoLanguagesSynchronizer
) {
$this->ontology = $ontology;
$this->resourceDeleter = $resourceDeleter;
$this->resourceTranslationRepository = $resourceTranslationRepository;
$this->logger = $logger;
$this->translatedIntoLanguagesSynchronizer = $translatedIntoLanguagesSynchronizer;
}

public function deleteByRequest(ServerRequestInterface $request): core_kernel_classes_Resource
Expand Down Expand Up @@ -87,6 +90,8 @@ public function deleteByRequest(ServerRequestInterface $request): core_kernel_cl
$this->resourceDeleter->delete($resource);
}

$this->translatedIntoLanguagesSynchronizer->sync($this->ontology->getResource($resourceUri));

return $resource;
} catch (Throwable $exception) {
$this->logger->error(
Expand Down
29 changes: 10 additions & 19 deletions models/classes/Translation/Service/TranslationSyncService.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,19 @@ class TranslationSyncService
private Ontology $ontology;
private ResourceTranslationRepository $resourceTranslationRepository;
private LoggerInterface $logger;
private TranslatedIntoLanguagesSynchronizer $translatedIntoLanguagesSynchronizer;
private array $synchronizers;

public function __construct(
Ontology $ontology,
ResourceTranslationRepository $resourceTranslationRepository,
LoggerInterface $logger
LoggerInterface $logger,
TranslatedIntoLanguagesSynchronizer $translatedIntoLanguagesSynchronizer
) {
$this->ontology = $ontology;
$this->resourceTranslationRepository = $resourceTranslationRepository;
$this->logger = $logger;
$this->translatedIntoLanguagesSynchronizer = $translatedIntoLanguagesSynchronizer;
}

public function addSynchronizer(string $resourceType, callable $synchronizer): void
Expand All @@ -71,12 +74,14 @@ public function syncByRequest(ServerRequestInterface $request): core_kernel_clas

$translations = $this->getTranslations($resource, $requestParams['languageUri'] ?? null);

foreach ($this->synchronizers[$this->getResourceType($resource)] as $callable) {
foreach ($this->synchronizers[$resource->getRootId()] as $callable) {
foreach ($translations as $translation) {
$callable($translation);
}
}

$this->translatedIntoLanguagesSynchronizer->sync($resource);

return $resource;
}

Expand Down Expand Up @@ -114,15 +119,15 @@ private function getTranslations(core_kernel_classes_Resource $resource, ?string

/** @var ResourceTranslation $translation */
foreach ($translations as $translation) {
$resource = $this->ontology->getResource($translation->getResourceUri());
$translationResource = $this->ontology->getResource($translation->getResourceUri());

if (!$resource->exists()) {
if (!$translationResource->exists()) {
$this->logger->error('Resource %s does not exist', $translation->getResourceUri());

continue;
}

$resources[] = $resource;
$resources[] = $translationResource;
}

if (empty($resources)) {
Expand All @@ -137,18 +142,4 @@ private function getTranslations(core_kernel_classes_Resource $resource, ?string

return $resources;
}

private function getResourceType(core_kernel_classes_Resource $resource): string
{
$parentClassIds = $resource->getParentClassesIds();
$resourceType = array_pop($parentClassIds);

if (empty($resourceType)) {
throw new ResourceTranslationException(
sprintf('Resource %s must have a resource type', $resource->getUri())
);
}

return $resourceType;
}
}
11 changes: 9 additions & 2 deletions models/classes/Translation/Service/TranslationUpdateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ class TranslationUpdateService
{
private Ontology $ontology;
private LoggerInterface $logger;
private TranslatedIntoLanguagesSynchronizer $translatedIntoLanguagesSynchronizer;

public function __construct(Ontology $ontology, LoggerInterface $logger)
{
public function __construct(
Ontology $ontology,
LoggerInterface $logger,
TranslatedIntoLanguagesSynchronizer $translatedIntoLanguagesSynchronizer
) {
$this->ontology = $ontology;
$this->logger = $logger;
$this->translatedIntoLanguagesSynchronizer = $translatedIntoLanguagesSynchronizer;
}

public function update(UpdateTranslationCommand $command): core_kernel_classes_Resource
Expand Down Expand Up @@ -73,6 +78,8 @@ public function update(UpdateTranslationCommand $command): core_kernel_classes_R
$command->getProgressUri()
);

$this->translatedIntoLanguagesSynchronizer->sync($instance);

return $instance;
} catch (Throwable $exception) {
$this->logger->error(
Expand Down
Loading
Loading