Skip to content

Commit

Permalink
Feature/h5 repo (#144)
Browse files Browse the repository at this point in the history
* H5PRepository WIP

* fix

* fix

* possible fix

* transaction fix

Co-authored-by: Tomasz Smolarek <tomasz.smolarek@escolasoft.com>
  • Loading branch information
dyfero and dyfero authored Aug 15, 2022
1 parent 6c1c2eb commit 8d64965
Show file tree
Hide file tree
Showing 9 changed files with 344 additions and 31 deletions.
21 changes: 21 additions & 0 deletions database/factories/H5PLibraryDependencyFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace EscolaLms\HeadlessH5P\Database\Factories;

use EscolaLms\HeadlessH5P\Models\H5PLibrary;
use EscolaLms\HeadlessH5P\Models\H5PLibraryDependency;
use Illuminate\Database\Eloquent\Factories\Factory;

class H5PLibraryDependencyFactory extends Factory
{
protected $model = H5PLibraryDependency::class;

public function definition()
{
return [
'library_id' => H5PLibrary::factory(),
'required_library_id' => H5PLibrary::factory(),
'dependency_type' => $this->faker->randomElement(['editor', 'preloaded'])
];
}
}
1 change: 1 addition & 0 deletions database/factories/H5PLibraryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function definition()
'semantics' => '',
'tutorial_url' => '',
'has_icon' => 0,
'add_to' => ''
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up()
{
Schema::table('hh5p_libraries', function (Blueprint $table) {
$table->text('add_to')->nullable();
});
}

public function down()
{
Schema::table('hh5p_libraries', function (Blueprint $table) {
$table->dropColumn('add_to');
});
}
};
14 changes: 11 additions & 3 deletions src/Models/H5PLibrary.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ class H5PLibrary extends Model
'hasIcon',
'libraryId',
'children',
'languages'
'languages',
'addTo'
];

protected $appends = [
Expand All @@ -184,7 +185,8 @@ class H5PLibrary extends Model
'dropLibraryCss',
'tutorialUrl',
'hasIcon',
'libraryId'
'libraryId',
'addTo'
];

protected $hidden = [
Expand All @@ -196,6 +198,7 @@ class H5PLibrary extends Model
'drop_library_css',
'tutorial_url',
'has_icon',
'add_to'
];

public function getSemanticsAttribute($value)
Expand Down Expand Up @@ -248,6 +251,11 @@ public function getPreloadedCssAttribute():string
return isset($this->attributes['preloaded_css']) ? $this->attributes['preloaded_css'] : '';
}

public function getAddToAttribute():string
{
return isset($this->attributes['add_to']) ? $this->attributes['add_to'] : '';
}

public function getDropLibraryCssAttribute():string
{
return isset($this->attributes['drop_library_css']) ? $this->attributes['drop_library_css'] : '';
Expand All @@ -265,7 +273,7 @@ public function getHasIconAttribute():string

public function dependencies()
{
return $this->hasMany(H5PLibraryDependency::class, 'library_id');
return $this->hasMany(H5PLibraryDependency::class, 'library_id', 'id');
}

public function children()
Expand Down
13 changes: 11 additions & 2 deletions src/Models/H5PLibraryDependency.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

namespace EscolaLms\HeadlessH5P\Models;

use EscolaLms\HeadlessH5P\Database\Factories\H5PLibraryDependencyFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use EscolaLms\HeadlessH5P\Models\H5PLibrary;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class H5PLibraryDependency extends Model
{
use HasFactory;

public $incrementing = false;

public $timestamps = false;

protected $table = 'hh5p_libraries_dependencies';
Expand Down Expand Up @@ -41,4 +45,9 @@ public function requiredLibrary():BelongsTo
{
return $this->belongsTo(H5PLibrary::class, 'required_library_id');
}

protected static function newFactory(): H5PLibraryDependencyFactory
{
return H5PLibraryDependencyFactory::new();
}
}
8 changes: 4 additions & 4 deletions src/Repositories/H5PEditorStorageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ class H5PEditorStorageRepository implements H5peditorStorage
* @param string $lang Language code
* @return string Translation in JSON format
*/
public function getLanguage($machineName, $majorVersion, $minorVersion, $language): string
public function getLanguage($machineName, $majorVersion, $minorVersion, $language): ?string
{
if (!isset($language)) {
return '';
return null;
}

$library = H5PLibrary::select(['id'])->where([
['major_version', $majorVersion],
['minor_version', $minorVersion],
['name', $machineName],
])->first();


if ($library) {
$libraryLanguage = H5PLibraryLanguage::where([
['library_id', $library->id],
Expand All @@ -50,7 +50,7 @@ public function getLanguage($machineName, $majorVersion, $minorVersion, $languag
}
}

return '';
return null;
}

/**
Expand Down
64 changes: 42 additions & 22 deletions src/Repositories/H5PRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Illuminate\Support\Facades\DB;
use DateTime;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;

class H5PRepository implements H5PFrameworkInterface
{
Expand Down Expand Up @@ -181,6 +182,8 @@ public function t($message, $replacements = []): string
*/
public function getLibraryFileUrl($libraryFolderName, $fileName)
{
$path = 'h5p/libraries/' . $libraryFolderName . '/' . $fileName;
return Storage::disk('local')->exists($path) ? Storage::disk('local')->url($path) : null;
}

/**
Expand Down Expand Up @@ -222,10 +225,25 @@ public function getUploadedH5pPath()
*
* @return array
*/
public function loadAddons()
public function loadAddons(): array
{
// TODO this should return something
return [];
return H5PLibrary::query()
->select(['l1.id', 'l1.name', 'l1.major_version', 'l1.minor_version', 'l1.patch_version', 'l1.preloaded_js', 'l1.preloaded_css', 'l1.add_to'])
->from('hh5p_libraries as l1')
->leftJoin('hh5p_libraries as l2', fn($join) => $join
->on('l1.name', '=', 'l2.name')
->on(fn($query) => $query
->on('l1.major_version', '<', 'l2.major_version')
->orOn(fn ($query) => $query
->orOn('l1.major_version', '=', 'l2.major_version')
->on('l1.minor_version', '<', 'l2.minor_version')
)
)
)
->whereNotNull('l1.add_to')
->whereNull('l2.name')
->get()
->toArray();
}

/**
Expand Down Expand Up @@ -262,6 +280,7 @@ public function loadLibraries()
->orderBy('major_version', 'ASC')
->orderBy('minor_version', 'ASC')
->get();

$libraries = [];
foreach ($results as $library) {
$libraries[$library->name][] = $library;
Expand Down Expand Up @@ -376,8 +395,6 @@ public function isInDevMode()
*/
public function mayUpdateLibraries()
{
// TODO check if current user is allowed to do this action
// best option use middleware on router
return true;
}

Expand Down Expand Up @@ -453,8 +470,9 @@ public function saveLibraryData(&$libraryData, $new = true)
'preloaded_css' => $this->pathsToCsv($libraryData, 'preloadedCss'),
'drop_library_css' => '', // TODO, what is this ?
'semantics' => isset($libraryData['semantics']) ? $libraryData['semantics'] : '',
'tutorial_url' => isset($libraryData['tutorial_url']) ? isset($libraryData['tutorial_url']) : '',
'tutorial_url' => isset($libraryData['tutorial_url']) ?: '',
'has_icon' => isset($libraryData['hasIcon']) ? 1 : 0,
'add_to' => isset($library['addTo']) ? json_encode($library['addTo']) : null
];

$libObj = H5PLibrary::firstOrCreate($library);
Expand Down Expand Up @@ -496,7 +514,7 @@ public function saveLibraryData(&$libraryData, $new = true)
*/
public function insertContent($content, $contentMainId = null)
{
return $this->updateContent($content, $contentMainId = null);
return $this->updateContent($content, $contentMainId);
}

private function fixContentParamsMetadataLibraryTitle($content)
Expand Down Expand Up @@ -659,6 +677,7 @@ public function saveLibraryDependencies($libraryId, $dependencies, $dependency_t
*/
public function copyLibraryUsage($contentId, $copyFromId, $contentMainId = null)
{

}

/**
Expand All @@ -679,6 +698,7 @@ public function deleteContentData($contentId)
*/
public function deleteLibraryUsage($contentId)
{
H5PContent::findOrFail($contentId)->libraries()->delete();
}

/**
Expand Down Expand Up @@ -868,7 +888,7 @@ public function alterLibrarySemantics(&$semantics, $machineName, $majorVersion,
*/
public function deleteLibraryDependencies($libraryId)
{
// TODO this must be implemented
H5PLibrary::findOrFail($libraryId)->dependencies()->delete();
}

/**
Expand Down Expand Up @@ -1053,6 +1073,7 @@ public function setOption($name, $value)
*/
public function updateContentFields($id, $fields)
{
H5PContent::findOrFail($id)->update($fields);
}

/**
Expand All @@ -1064,6 +1085,7 @@ public function updateContentFields($id, $fields)
*/
public function clearFilteredParameters($library_ids)
{
H5PContent::query()->whereIn('library_id', $library_ids)->update(['filtered' => null]);
}

/**
Expand Down Expand Up @@ -1119,6 +1141,7 @@ public function getLibraryStats($type)
*/
public function getNumAuthors()
{
return H5PContent::query()->select(['user_id'])->distinct()->count('user_id');
}

/**
Expand Down Expand Up @@ -1220,16 +1243,8 @@ public function hasPermission($permission, $id = null)
*/
public function replaceContentTypeCache($contentTypeCache)
{

// TODO refactor this ugly code
// Replace existing content type cache
DB::table('hh5p_libraries_hub_cache')->truncate();

// TODO wrap this in a transaction

foreach ($contentTypeCache->contentTypes as $ct) {
// Insert into db
H5pLibrariesHubCache::create([
$data[] = [
'machine_name' => $ct->id,
'major_version' => $ct->version->major,
'minor_version' => $ct->version->minor,
Expand All @@ -1245,14 +1260,19 @@ public function replaceContentTypeCache($contentTypeCache)
'is_recommended' => $ct->isRecommended === true ? 1 : 0,
'popularity' => $ct->popularity,
'screenshots' => json_encode($ct->screenshots),
'license' => json_encode(isset($ct->license) ? $ct->license : []),
'license' => json_encode($ct->license ?? []),
'example' => $ct->example,
'tutorial' => isset($ct->tutorial) ? $ct->tutorial : '',
'keywords' => json_encode(isset($ct->keywords) ? $ct->keywords : []),
'categories' => json_encode(isset($ct->categories) ? $ct->categories : []),
'tutorial' => $ct->tutorial ?? '',
'keywords' => json_encode($ct->keywords ?? []),
'categories' => json_encode($ct->categories ?? []),
'owner' => $ct->owner,
]);
];
}

DB::transaction(function () use($data) {
H5pLibrariesHubCache::query()->delete();
H5pLibrariesHubCache::insert($data);
});
}

/**
Expand Down
Loading

0 comments on commit 8d64965

Please sign in to comment.