Skip to content

Commit

Permalink
Merge pull request #4094 from oat-sa/feat/ADF-1781/translations-feature
Browse files Browse the repository at this point in the history
feat: add translation feature
  • Loading branch information
gabrielfs7 authored Sep 13, 2024
2 parents 0ecf0fe + 314ef31 commit f4709bf
Show file tree
Hide file tree
Showing 51 changed files with 4,165 additions and 28 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ Here you can find the environment variables including feature flags
| DATA_STORE_STATISTIC_PUB_SUB_TOPIC | Topic name for statistic metadata Pub/Sub | - |
| REDIRECT_AFTER_LOGOUT_URL | Allows to configure the redirect after logout via environment variable. The fallback is the configured redirect on urlroute.conf.php | - |
| PORTAL_URL | The Portal url used on the back button of Portal theme | - |
| FEATURE_TRANSLATION_ENABLED | Enable access to items/tests translations feature | - |


# Routing
Expand Down
19 changes: 17 additions & 2 deletions actions/class.Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,11 +521,13 @@ private function getSections($shownExtension, $shownStructure)
$sections = [];
$user = $this->getSession()->getUser();
$structure = MenuService::getPerspective($shownExtension, $shownStructure);
$sectionVisibilityFilter = $this->getSectionVisibilityFilter();

if (!is_null($structure)) {
foreach ($structure->getChildren() as $section) {
$resolver = new ActionResolver($section->getUrl());

if (!$this->getSectionVisibilityFilter()->isVisible($section->getId())) {
if (!$sectionVisibilityFilter->isVisible($section->getId())) {
continue;
}

Expand All @@ -540,6 +542,19 @@ private function getSections($shownExtension, $shownStructure)

if (FuncProxy::accessPossible($user, $resolver->getController(), $resolver->getAction())) {
foreach ($section->getActions() as $action) {
$sectionPath = $sectionVisibilityFilter->createSectionPath(
[
$section->getId(),
$action->getId()
]
);

if (!$sectionVisibilityFilter->isVisible($sectionPath)) {
$section->removeAction($action);

continue;
}

$this->propagate($action);
$resolver = new ActionResolver($action->getUrl());
if (
Expand Down Expand Up @@ -580,7 +595,7 @@ protected function getUserService()
return $this->getServiceLocator()->get(tao_models_classes_UserService::SERVICE_ID);
}

private function getSectionVisibilityFilter(): SectionVisibilityFilterInterface
private function getSectionVisibilityFilter(): SectionVisibilityFilter
{
if (empty($this->sectionVisibilityFilter)) {
$this->sectionVisibilityFilter = $this->getServiceLocator()->get(SectionVisibilityFilter::SERVICE_ID);
Expand Down
122 changes: 122 additions & 0 deletions actions/class.Translation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?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);

use oat\tao\model\http\HttpJsonResponseTrait;
use oat\tao\model\Translation\Command\UpdateTranslationCommand;
use oat\tao\model\Translation\Service\ResourceTranslationRetriever;
use oat\tao\model\Translation\Service\ResourceTranslatableRetriever;
use oat\tao\model\Translation\Service\TranslationCreationService;
use oat\tao\model\Translation\Service\TranslationUpdateService;

class tao_actions_Translation extends tao_actions_CommonModule
{
use HttpJsonResponseTrait;

/**
* @requiresRight id WRITE
*/
public function update(): void
{
try {
$resource = $this->getTranslationUpdateService()->update(
new UpdateTranslationCommand(
$this->getRequestParameter('id'),
$this->getRequestParameter('progress'),
)
);

$this->setSuccessJsonResponse(
[
'resourceUri' => $resource->getUri()
]
);
} catch (Throwable $exception) {
$this->setErrorJsonResponse($exception->getMessage());
}
}

/**
* @requiresRight id WRITE
*/
public function translate(): void
{
try {
$newResource = $this->getTranslationCreationService()->createByRequest($this->getPsrRequest());

$this->setSuccessJsonResponse(
[
'resourceUri' => $newResource->getUri()
]
);
} catch (Throwable $exception) {
$this->setErrorJsonResponse($exception->getMessage());
}
}

/**
* @requiresRight id READ
*/
public function translations(): void
{
try {
$this->setSuccessJsonResponse(
$this->getResourceTranslationRetriever()->getByRequest($this->getPsrRequest())
);
} catch (Throwable $exception) {
$this->setErrorJsonResponse($exception->getMessage());
}
}

/**
* @requiresRight id READ
*/
public function translatable(): void
{
try {
$this->setSuccessJsonResponse(
$this->getResourceTranslatableRetriever()->getByRequest($this->getPsrRequest())
);
} catch (Throwable $exception) {
$this->setErrorJsonResponse($exception->getMessage());
}
}

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

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

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

private function getTranslationUpdateService(): TranslationUpdateService
{
return $this->getServiceManager()->getContainer()->get(TranslationUpdateService::class);
}
}
165 changes: 165 additions & 0 deletions doc/taoApi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,89 @@ info:
version: v1

paths:
/tao/Translation/update:
post:
summary: Return a list of translations for a given translatable resource
requestBody:
required: true
content:
multipart/form-data:
schema:
type: object
properties:
id:
type: string
description: The resource URI
progress:
type: string
description: The progress URI
responses:
200:
$ref: '#/components/responses/TranslateResponse'
400:
$ref: '#/components/responses/BadRequestResponse'
500:
$ref: '#/components/responses/InternalServerErrorResponse'
/tao/Translation/translations:
get:
summary: Return a list of translations for a given translatable resource
parameters:
- in: query
name: id
required: true
schema:
type: string
description: The RDF resource id
- in: query
name: languageUri
required: false
schema:
type: string
description: The RDF language URI
responses:
200:
$ref: '#/components/responses/TranslationsResponse'
400:
$ref: '#/components/responses/BadRequestResponse'
500:
$ref: '#/components/responses/InternalServerErrorResponse'
/tao/Translation/translatable:
get:
summary: Return translatable resources
parameters:
- in: query
name: id
required: true
schema:
type: string
description: The RDF resource id
responses:
200:
$ref: '#/components/responses/TranslatableResponse'
400:
$ref: '#/components/responses/BadRequestResponse'
500:
$ref: '#/components/responses/InternalServerErrorResponse'
/tao/Translation/translate:
post:
summary: translate a resources
requestBody:
required: true
content:
multipart/form-data:
schema:
type: object
properties:
id:
type: string
description: The resource URI
responses:
200:
$ref: '#/components/responses/TranslateResponse'
400:
$ref: '#/components/responses/BadRequestResponse'
500:
$ref: '#/components/responses/InternalServerErrorResponse'
/tao/Languages/index:
get:
summary: Get a list of available languages in the TAO platform
Expand Down Expand Up @@ -46,6 +129,70 @@ paths:
$ref: '#/components/responses/InternalServerErrorResponse'
components:
schemas:
Translations:
properties:
success:
type: boolean
example: true
data:
type: object
properties:
resources:
type: array
items:
properties:
originResourceUri:
type: string
resourceUri:
type: string
resourceLabel:
type: string
metadata:
type: object
properties:
key:
type: object
properties:
value:
type: string
literal:
type: string
Translatable:
properties:
success:
type: boolean
example: true
data:
type: object
properties:
resources:
type: array
items:
properties:
resourceUri:
type: string
resourceLabel:
type: string
metadata:
type: object
properties:
key:
type: object
properties:
value:
type: string
literal:
type: string
Translated:
properties:
success:
type: boolean
example: true
data:
type: object
properties:
resourceUri:
type: string
ResourceRelationResource:
description: 'A resource related to another resources'
type: object
Expand Down Expand Up @@ -104,6 +251,24 @@ components:
message:
type: string
responses:
TranslationsResponse:
description: The list of translations
content:
application/json:
schema:
$ref: '#/components/schemas/Translations'
TranslatableResponse:
description: The list of translatable
content:
application/json:
schema:
$ref: '#/components/schemas/Translatable'
TranslateResponse:
description: The resource is translated
content:
application/json:
schema:
$ref: '#/components/schemas/Translated'
ResourceRelationsResponse:
description: Bad request
content:
Expand Down
Loading

0 comments on commit f4709bf

Please sign in to comment.