Skip to content

Commit

Permalink
GH #2617: Update translation strings in H5P content
Browse files Browse the repository at this point in the history
  • Loading branch information
chrieinv committed Jan 11, 2024
1 parent d07820f commit f2ba3fb
Show file tree
Hide file tree
Showing 12 changed files with 461 additions and 12 deletions.
9 changes: 9 additions & 0 deletions sourcecode/apis/contentauthor/app/H5PContentLibrary.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class H5PContentLibrary extends Model
{
Expand All @@ -14,4 +15,12 @@ class H5PContentLibrary extends Model
protected $guarded = [];

public $timestamps = false;

/**
* @return BelongsTo<H5PLibrary, self>
*/
public function library(): BelongsTo
{
return $this->belongsTo(H5PLibrary::class, 'library_id');
}
}
11 changes: 11 additions & 0 deletions sourcecode/apis/contentauthor/app/H5PContentsMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,19 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Carbon;

/**
* @property int $id
* @property int $content_id
* @property string $authors
* @property string $license
* @property string $license_version
* @property string $license_extras
* @property Carbon $created_at
* @property Carbon $updated_at
* @property string $default_language
*
* @method static self make(array $attributes = [])
*/
class H5PContentsMetadata extends Model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
use App\Http\Controllers\Controller;
use App\Http\Requests\AdminTranslationUpdateRequest;
use App\Libraries\ContentAuthorStorage;
use App\Libraries\H5P\AdminConfig;
use Cerpus\VersionClient\VersionData;
use Exception;
use H5PCore;
use H5PValidator;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Carbon;
Expand Down Expand Up @@ -204,6 +206,10 @@ public function libraryTranslation(H5PLibrary $library, string $locale): View
'translationFile' => Storage::disk()->get(
sprintf('libraries/%s/language/%s.json', $library->getFolderName(), $locale)
),
'contentCount' => H5PContent::where('library_id', $library->id)
->whereHas('metadata', function (Builder $query) use ($locale) {
$query->where('default_language', $locale);
})->count(),
]);
}

Expand Down Expand Up @@ -252,6 +258,33 @@ public function libraryTranslationUpdate(AdminTranslationUpdateRequest $request,
sprintf('libraries/%s/language/%s.json', $library->getFolderName(), $locale)
),
'messages' => $messages,
'contentCount' => H5PContent::where('library_id', $library->id)
->whereHas('metadata', function (Builder $query) use ($locale) {
$query->where('default_language', $locale);
})->count(),
]);
}

public function contentTranslationUpdate(H5PLibrary $library, string $locale): View
{
$config = app(AdminConfig::class);
$config->getConfig();
$config->addContentLanguageScripts();

$content = H5PContent::where('library_id', $library->id)
->whereHas('metadata', function (Builder $query) use ($locale) {
$query->where('default_language', $locale);
})->get();

return view('admin.content-language-update', [
'library' => $library,
'languageCode' => $locale,
'contentCount' => $content->count(),
//'h5pAdminIntegration' => $configuration,
'h5pIntegration' => $config->config,
'scripts' => $config->getScriptAssets(),
'styles' => $config->getStyleAssets(),
'contents' => $content->random(),
]);
}

Expand Down
28 changes: 28 additions & 0 deletions sourcecode/apis/contentauthor/app/Libraries/H5P/AdminConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,34 @@ public function addPresaveScripts()
$this->addAsset('scripts', asset('/js/h5p/h5peditor-pre-save.js'));
}

public function addContentLanguageScripts(): void
{
$this->addCoreAssets();
$this->addAsset('scripts', $this->getAssetUrl('editor', 'scripts/h5peditor.js'));
$this->addAsset('scripts', $this->getAssetUrl('editor', 'scripts/h5peditor-editor.js'));
//$this->addAsset('scripts', $this->getAssetUrl('editor', 'scripts/h5peditor-form.js'));
$this->addAsset('scripts', $this->getAssetUrl('editor', 'language/en.js'));

// Widgets
//$this->addAsset('scripts', $this->getAssetUrl('editor', 'scripts/h5peditor-file-uploader.js'));
//$this->addAsset('scripts', $this->getAssetUrl('editor', 'scripts/h5peditor-av.js'));
//$this->addAsset('scripts', $this->getAssetUrl('editor', 'scripts/h5peditor-semantic-structure.js'));
//$this->addAsset('scripts', $this->getAssetUrl('editor', 'scripts/h5peditor-boolean.js'));
//$this->addAsset('scripts', $this->getAssetUrl('editor', 'scripts/h5peditor-coordinates.js'));
//$this->addAsset('scripts', $this->getAssetUrl('editor', 'scripts/h5peditor-dimensions.js'));
//$this->addAsset('scripts', $this->getAssetUrl('editor', 'scripts/h5peditor-file.js'));
//$this->addAsset('scripts', $this->getAssetUrl('editor', 'scripts/h5peditor-group.js'));
//$this->addAsset('scripts', $this->getAssetUrl('editor', 'scripts/h5peditor-html.js'));
//$this->addAsset('scripts', $this->getAssetUrl('editor', 'scripts/h5peditor-image.js'));
//$this->addAsset('scripts', $this->getAssetUrl('editor', 'scripts/h5peditor-library.js'));
//$this->addAsset('scripts', $this->getAssetUrl('editor', 'scripts/h5peditor-list.js'));
//$this->addAsset('scripts', $this->getAssetUrl('editor', 'scripts/h5peditor-none.js'));
//$this->addAsset('scripts', $this->getAssetUrl('editor', 'scripts/h5peditor-number.js'));
//$this->addAsset('scripts', $this->getAssetUrl('editor', 'scripts/h5peditor-select.js'));
//$this->addAsset('scripts', $this->getAssetUrl('editor', 'scripts/h5peditor-text.js'));
//$this->addAsset('scripts', $this->getAssetUrl('editor', 'scripts/h5peditor-textarea.js'));
}

public function getSettings(H5PLibrary $library)
{
$upgrades = $library->getUpgrades(false)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '../js/ndla-content-language-update';

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
@extends('layouts.admin')
@push('js')
<script>H5PIntegration ={!! json_encode($h5pIntegration) !!}</script>
@foreach($scripts as $script)
<script src="{{$script}}"></script>
@endforeach

<script src="{{ mix('js/ndla-content-language.js') }}"></script>
@endpush
@section ('content')
<div class="container" style="width:80vw;">
<a href="{{ route('admin.update-libraries') }}">Library list</a>
<br>
<a href="{{ route('admin.check-library', [$library->id]) }}">Library details</a>
<br>
<a href="{{ route('admin.library-translation', [$library->id, $languageCode]) }}">Library "{{$languageCode}}" translation</a>
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<h3>
Refresh translations saved with content for {{ $library->getLibraryString(true) }} and "{{$languageCode}}" language
</h3>
</div>
<div class="panel-body row">
{{$contentCount}} content will be updated
<button type="button" onclick="goGoGo()">Go</button>
</div>
</div>
</div>
</div>
</div>
<script>
function goGoGo() {
new H5PEditor.ContentLanguageUpdateProcess(
JSON.parse(@json($contents->parameters)),
'',
'{{ $library->getLibraryString(false) }}',
'{{ $languageCode }}'
);
}
</script>
@dump($contents);
@endsection
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@
<td>{{ $latestVersion ? 'Yes' : 'No' }}</td>
</tr>
<tr>
<th>Language</th>
<td>{{ $content->language_iso_639_3 }}</td>
<th>Edlib language</th>
<td>{{ $content->language_iso_639_3 }} ({{{Iso639p3::englishName($content->language_iso_639_3)}}})</td>
</tr>
<tr>
<th>H5P language</th>
<td>{{ $content->metadata->default_language }} ({{{Iso639p3::englishName($content->metadata->default_language)}}})</td>
</tr>
<tr>
<th>License</th>
Expand All @@ -75,7 +79,7 @@
<th>Library</th>
<td>
<a href="{{ route('admin.check-library', [$content->library->id]) }}">
{{ sprintf('%s %d.%d.%d', $content->library->name, $content->library->major_version, $content->library->minor_version, $content->library->patch_version) }}
{{ $content->library->getLibraryString(true) }}
</a>
</td>
</tr>
Expand Down Expand Up @@ -198,6 +202,43 @@
</div>
</div>
@endempty
<div class="panel panel-default">
<div class="panel-heading">
<h4>Libraries</h4>
</div>
<div class="panel-body">
<table class="table table-striped">
<thead>
<tr>
<th>Library</th>
<th>Library type</th>
<th>Dependency type</th>
</tr>
</thead>
<tbody>
@foreach($content->contentLibraries()->orderBy('dependency_type')->orderBy('weight')->get() as $contentLib)
<tr>
<td>
<a href="{{ route('admin.check-library', [$contentLib->library_id]) }}">
{{ $contentLib->library->getLibraryString(true) }}
</a>
</td>
<td>
@if($contentLib->library->runnable)
Content type
@else
Library
@endif
</td>
<td>
{{ $contentLib->dependency_type }}
</td>
</td>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,16 @@ class="btn btn-default"
</td>
<td></td>
</tr>
<tr>
<th>Number of contents</th>
<td colspan="2">
<a href="{{ route('admin.content-library', [$library->id]) }}">
{{ $library->contents()->count() }}
</a>
</td>
</tr>
@if($library->runnable)
<tr>
<th>Number of contents</th>
<td colspan="2">
<a href="{{ route('admin.content-library', [$library->id]) }}">
{{ $library->contents()->count() }}
</a>
</td>
</tr>
@endif
</table>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,17 @@
<div class="panel-body row">
<div class="panel panel-default">
<div class="panel-heading">
<h5>Upload new translation</h5>
<h4>Content</h4>
</div>
<div class="panel-body row">
This language has <a href="{{ route('admin.library-transation-content', [$library->id, $languageCode]) }}">{{ $contentCount }} content</a>
</div>
</div>
</div>
<div class="panel-body row">
<div class="panel panel-default">
<div class="panel-heading">
<h4>Upload new translation</h4>
</div>
<div class="panel-body row">
@if($haveTranslation)
Expand Down
2 changes: 2 additions & 0 deletions sourcecode/apis/contentauthor/routes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ function () {
Route::get('libraries/{library}/translation/{locale}', [AdminH5PDetailsController::class, 'libraryTranslation'])
->name('admin.library-translation');
Route::post('libraries/{library}/translation/{locale}', [AdminH5PDetailsController::class, 'libraryTranslationUpdate']);
Route::get('libraries/{library}/translation/{locale}/content', [AdminH5PDetailsController::class, 'contentTranslationUpdate'])
->name('admin.library-transation-content');

Route::get('libraries/{library}', [ContentUpgradeController::class, 'upgrade'])->name('admin.library');

Expand Down
2 changes: 2 additions & 0 deletions sourcecode/apis/contentauthor/webpack.mix.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ mix
.js('resources/assets/entrypoints/react-contentbrowser.js', 'js/react-contentbrowser.js')
.js('resources/assets/entrypoints/react-h5p.js', 'js/react-h5p.js')
.js('resources/assets/entrypoints/react-questionset.js', 'js/react-questionset.js')
.js('resources/assets/entrypoints/ndla-content-language.js', 'js/ndla-content-language.js')
.webpackConfig({
resolve: {
fallback: {
Expand All @@ -49,4 +50,5 @@ mix
}
}
})
.sourceMaps(false, 'inline-source-map')
;

0 comments on commit f2ba3fb

Please sign in to comment.