Skip to content

Commit

Permalink
Remove title column from h5p contents (#164)
Browse files Browse the repository at this point in the history
* Remove title column from h5p content

* fixes

* update phpunit

Co-authored-by: Tomasz Smolarek <tomasz.smolarek@escolasoft.com>
  • Loading branch information
dyfero and dyfero authored Aug 28, 2022
1 parent 2d3e102 commit 44055b4
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 55 deletions.
1 change: 0 additions & 1 deletion database/factories/H5PContentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public function definition()
$admin = User::role(UserRole::ADMIN)->inRandomOrder()->first();
return [
'user_id' => empty($admin) ? null : $admin->id,
'title' => 'The Title',
'parameters' => '{"params":{"taskDescription":"Documentation tool","pagesList":[{"params":{"elementList":[{"params":{},"library":"H5P.Text 1.1","metadata":{"contentType":"Text","license":"U","title":"Untitled Text","authors":[],"changes":[],"extraTitle":"Untitled Text"},"subContentId":"da3387da-355a-49fb-92bc-3a9a4e4646a9"}],"helpTextLabel":"More information","helpText":""},"library":"H5P.StandardPage 1.5","metadata":{"contentType":"Standard page","license":"U","title":"Untitled Standard page","authors":[],"changes":[],"extraTitle":"Untitled Standard page"},"subContentId":"ac6ffdac-be02-448c-861c-969e6a09dbd5"}],"i10n":{"previousLabel":"poprzedni","nextLabel":"Next","closeLabel":"Close"}},"metadata":{"license":"U","authors":[],"changes":[],"extraTitle":"fdsfds","title":"fdsfds"}}',
'nonce' => bin2hex(random_bytes(4)),
'filtered' => 'filtered',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ public function up()
$table->bigInteger('user_id')->nullable()->unsigned(); // ADD key
$table->string('title');
$table->bigInteger('library_id')->unsigned(); // ADD key
$table->mediumText('parameters'); // TODO this can be JSON type
$table->mediumText('parameters');
$table->string('nonce', 8)->unique(); // used for assiging temporary editor files to content


// TODO: do we need those ?
$table->text('filtered')->nullable();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

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

return new class extends Migration
{
public function up()
{
if (DB::connection() instanceof PostgresConnection) {
DB::statement('ALTER TABLE hh5p_contents ALTER COLUMN parameters TYPE JSON USING (parameters::json);');
} else {
Schema::table('hh5p_contents', function (Blueprint $table) {
$table->json('parameters')->change();
});
}

Schema::table('hh5p_contents', function (Blueprint $table) {
$table->dropColumn('title');
});
}

public function down()
{
Schema::table('hh5p_contents', function (Blueprint $table) {
$table->string('title');
$table->mediumText('parameters')->change();
});
}
};
2 changes: 1 addition & 1 deletion src/Dtos/ContentFilterCriteriaDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static function instantiateFromRequest(Request $request): self
$user = auth()->user();

if ($request->has('title')) {
$criteria->push(new LikeCriterion('hh5p_contents.title', $request->input('title')));
$criteria->push(new LikeCriterion('parameters->metadata->title', $request->get('title')));
}
if ($user->can(H5PPermissionsEnum::H5P_LIST) && $request->has('author_id')) {
$criteria->push(new EqualCriterion('hh5p_contents.user_id', $request->input('author_id')));
Expand Down
16 changes: 12 additions & 4 deletions src/Http/Controllers/ContentApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ public function index(ContentListRequest $request): JsonResponse
{
$contentFilterDto = ContentFilterCriteriaDto::instantiateFromRequest($request);
$columns = [
'hh5p_contents.title',
'hh5p_contents.id',
'hh5p_contents.uuid',
'hh5p_contents.library_id',
'hh5p_contents.user_id',
'hh5p_contents.author'
'hh5p_contents.author',
'hh5p_contents.parameters',
];
$list = $request->get('per_page') !== null && $request->get('per_page') == 0 ?
$this->contentRepository->unpaginatedList($contentFilterDto, $columns) :
Expand All @@ -52,7 +52,11 @@ public function index(ContentListRequest $request): JsonResponse
public function update(ContentUpdateRequest $request, int $id): JsonResponse
{
try {
$contentId = $this->contentRepository->edit($id, $request->get('title'), $request->get('library'), $request->get('params'), $request->get('nonce'));
$contentId = $this->contentRepository->edit(
$id,
$request->get('library'), $request->get('params'),
$request->get('nonce')
);
} catch (Exception $error) {
return $this->sendError($error->getMessage(), 422);
}
Expand All @@ -63,7 +67,11 @@ public function update(ContentUpdateRequest $request, int $id): JsonResponse
public function store(ContentCreateRequest $request): JsonResponse
{
try {
$contentId = $this->contentRepository->create($request->get('title'), $request->get('library'), $request->get('params'), $request->get('nonce'));
$contentId = $this->contentRepository->create(
$request->get('library'),
$request->get('params'),
$request->get('nonce')
);
} catch (Exception $error) {
return $this->sendError($error->getMessage(), 422);
}
Expand Down
5 changes: 0 additions & 5 deletions src/Http/Controllers/Swagger/ContentApiSwagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
* schema="H5PContentStore",
* type="object",
* @OA\Property(
* property="title",
* description="Title of new content",
* type="string"
* ),
* @OA\Property(
* property="library",
* description="ubername of library",
* type="string"
Expand Down
1 change: 0 additions & 1 deletion src/Http/Requests/ContentCreateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public function authorize(): bool
public function rules(): array
{
return [
'title' => ['required', 'string'],
'library' => ['required', 'string'],
'params' => ['required', 'string'],
'nonce' => ['required', 'string'],
Expand Down
1 change: 0 additions & 1 deletion src/Http/Requests/ContentUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public function authorize(): bool
public function rules(): array
{
return [
'title' => ['required', 'string'],
'library' => ['required', 'string'],
'params' => ['required', 'string'],
'nonce' => ['required', 'string'],
Expand Down
9 changes: 8 additions & 1 deletion src/Models/H5PContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ class H5PContent extends Model

protected $appends = [
'params',
'metadata'
'metadata',
'title'
];

protected $visible = [
Expand All @@ -122,6 +123,12 @@ class H5PContent extends Model
'nonce'
];

public function getTitleAttribute()
{
$parameters = json_decode($this->parameters);
return $parameters->metadata->title ?? null;
}

public function getParamsAttribute()
{
$parameters = json_decode($this->parameters);
Expand Down
4 changes: 2 additions & 2 deletions src/Repositories/Contracts/H5PContentRepositoryContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

interface H5PContentRepositoryContract
{
public function create(string $title, string $library, string $params, string $nonce): int;
public function create(string $library, string $params, string $nonce): int;

public function edit(int $id, string $title, string $library, string $params, string $nonce): int;
public function edit(int $id, string $library, string $params, string $nonce): int;

public function list(
ContentFilterCriteriaDto $contentFilterDto,
Expand Down
7 changes: 2 additions & 5 deletions src/Repositories/H5PContentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(HeadlessH5PServiceContract $hh5pService)
$this->hh5pService = $hh5pService;
}

public function create(string $title, string $library, string $params, string $nonce): int
public function create(string $library, string $params, string $nonce): int
{
$user = auth()->user();
$libNames = $this->hh5pService->getCore()->libraryFromString($library);
Expand All @@ -50,7 +50,6 @@ public function create(string $title, string $library, string $params, string $n

$content = $this->hh5pService->getCore()->saveContent([
'library_id' => $libDb->id,
'title' => $title,
'library' => $library,
'parameters' => $params,
'nonce' => $nonce,
Expand All @@ -63,7 +62,7 @@ public function create(string $title, string $library, string $params, string $n
return $content;
}

public function edit(int $id, string $title, string $library, string $params, string $nonce): int
public function edit(int $id, string $library, string $params, string $nonce): int
{
$libNames = $this->hh5pService->getCore()->libraryFromString($library);

Expand Down Expand Up @@ -92,7 +91,6 @@ public function edit(int $id, string $title, string $library, string $params, st
$id = $this->hh5pService->getCore()->saveContent([
'id' => $id,
'library_id' => $libDb->id,
'title' => $title,
'library' => $library,
'parameters' => $params,
//'nonce'=>$nonce
Expand Down Expand Up @@ -198,7 +196,6 @@ public function upload($file, $content = null, $only_upgrade = null, $disable_h5
$id = $this->hh5pService->getStorage()->contentId;

$content = $this->hh5pService->getCore()->loadContent($id);
$content['metadata']['title'] = $content['title'];

$safe_parameters = $this->hh5pService->getCore()->filterParameters($content);

Expand Down
27 changes: 15 additions & 12 deletions src/Repositories/H5PRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -536,35 +536,38 @@ private function fixContentParamsMetadataLibraryTitle($content)
// `parameters` is string, encode
if (isset($content['parameters']) && is_string($content['parameters'])) {
$parameters = json_decode($content['parameters']);
if (is_object($parameters) && isset($parameters->params)) {
$parameters = $parameters->params;

if (is_array($parameters) && isset($parameters['metadata'])) {
$metadata = $parameters['metadata'];
}
if (is_object($parameters) && isset($parameters->metadata)) {
$metadata = $parameters->metadata;
}
if (is_object($parameters) && isset($parameters->params)) {
$parameters = $parameters->params;
}
if (is_array($parameters) && isset($parameters['params'])) {
$parameters = $parameters['params'];
}
if (is_array($parameters) && isset($parameters['metadata'])) {
$metadata = $parameters['metadata'];
}
}

// `params` is string, encode
if (!isset($parameters) && is_string($content['params'])) {
$parameters = json_decode($content['params']);
if (is_object($parameters) && isset($parameters->params)) {
$parameters = $parameters->params;
}

if (is_object($parameters) && isset($parameters->metadata)) {
$metadata = $parameters->metadata;
}
if (is_array($parameters) && isset($parameters['params'])) {
$parameters = $parameters['params'];
}
if (is_array($parameters) && isset($parameters['metadata'])) {
$metadata = $parameters['metadata'];
}
if (is_object($parameters) && isset($parameters->params)) {
$parameters = $parameters->params;
}
if (is_array($parameters) && isset($parameters['params'])) {
$parameters = $parameters['params'];
}

}
// `params is array
if (!isset($parameters) && is_array($content['params'])) {
Expand Down Expand Up @@ -972,14 +975,14 @@ public function deleteLibrary($library)
public function loadContent($id)
{
$content = H5PContent::with('library')->where(['id' => $id])->firstOrFail();

if (is_null($content->library)) {
throw new H5PException(H5PException::LIBRARY_NOT_FOUND);
}
$content = $content->toArray();
$content['contentId'] = $content['id']; // : Identifier for the content
$content['params'] = json_encode($content['params']); // : json content as string
$content['embedType'] = \H5PCore::determineEmbedType($content['embed_type'] ?? 'div', $content['library']['embed_types']); // : csv of embed types
//$content ['title'] // : The contents title
//$content ['language'] // : Language code for the content
$content['libraryId'] = $content['library_id']; // : Id for the main library
$content['libraryName'] = $content['library']['machineName']; // The library machine name
Expand Down
Loading

0 comments on commit 44055b4

Please sign in to comment.