Skip to content

Commit

Permalink
add requiredLibrariesCounter and contentsCounter (#156)
Browse files Browse the repository at this point in the history
* content counter

* add required libraries counter

Co-authored-by: Tomasz Smolarek <tomasz.smolarek@escolasoft.com>
  • Loading branch information
dyfero and dyfero authored Aug 19, 2022
1 parent 912cad2 commit 31767b1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/Http/Resources/LibraryResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public function toArray($request = null): array
'hasIcon' => $this->hasIcon,
'libraryId' => $this->libraryId,
'languages' => $this->languages,
'contentsCount' => $this->contentsCount,
'requiredLibrariesCount' => $this->requiredLibrariesCount,
];
}
}
21 changes: 19 additions & 2 deletions src/Models/H5PLibrary.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ class H5PLibrary extends Model
'hasIcon',
'libraryId',
'languages',
'addTo'
'addTo',
'contentsCount',
'requiredLibrariesCount',
];

protected $appends = [
Expand All @@ -187,7 +189,7 @@ class H5PLibrary extends Model
'tutorialUrl',
'hasIcon',
'libraryId',
'addTo'
'addTo',
];

protected $hidden = [
Expand Down Expand Up @@ -272,11 +274,26 @@ public function getHasIconAttribute():string
return isset($this->attributes['has_icon']) ? $this->attributes['has_icon'] : '';
}

public function getContentsCountAttribute(): int
{
return $this->contents()->count();
}

public function getRequiredLibrariesCountAttribute(): int
{
return $this->requiredLibraries()->count();
}

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

public function requiredLibraries(): HasMany
{
return $this->hasMany(H5PLibraryDependency::class, 'required_library_id', 'id');
}

public function languages(): HasMany
{
return $this->hasMany(H5PLibraryLanguage::class, 'library_id');
Expand Down
10 changes: 7 additions & 3 deletions src/Services/HeadlessH5PService.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,19 @@ public function getConfig(): array
public function getLibraries(string $machineName = null, string $major_version = null, string $minor_version = null)
{
$lang = config('hh5p.language');

$libraries_url = url(config('hh5p.h5p_library_url'));

if ($machineName) {
$defaultLang = $this->getEditor()->getLibraryLanguage($machineName, $major_version, $minor_version, $lang);

return $this->getEditor()->getLibraryData($machineName, $major_version, $minor_version, $lang, '', $libraries_url, $defaultLang);
}

return $this->getEditor()->getLibraries();
return collect($this->getEditor()->getLibraries())
->each(fn($item) => $item
->append('contentsCount')
->append('requiredLibrariesCount')
);
}

public function getEditorSettings($content = null): array
Expand Down Expand Up @@ -495,7 +499,7 @@ public function deleteLibrary($id): bool
$library = H5pLibrary::findOrFail($id);

$libraryUsage = $this->getRepository()->getLibraryUsage($library->getKey());
if ($libraryUsage['content'] > 0 ) {
if ($libraryUsage['content'] > 0) {
return false;
}

Expand Down
27 changes: 24 additions & 3 deletions tests/Api/LibraryApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ public function test_library_index(): void
'id',
'machineName',
'majorVersion',
'minorVersion'
'minorVersion',
'contentsCount',
'requiredLibrariesCount'
]]
]);
}
Expand Down Expand Up @@ -215,7 +217,26 @@ public function testHubContentHubMetadataCacheUnauthorized(): void
->assertUnauthorized();
}


public function testGetLibrariesAdmin(): void
{
$this->authenticateAsAdmin();

$lib1 = H5PLibrary::factory()->create();
$lib2 = H5PLibrary::factory()->create();
H5PLibraryDependency::factory()->count(3)->create(['required_library_id' => $lib1->getKey()]);
H5PLibraryDependency::factory()->count(7)->create(['required_library_id' => $lib2->getKey()]);
H5PContent::factory()->count(2)->create(['library_id' => $lib1->getKey()]);
H5PContent::factory()->count(5)->create(['library_id' => $lib2->getKey()]);


$response = $this
->actingAs($this->user, 'api')
->getJson('api/admin/hh5p/libraries')
->assertOk();

$data = $response->getData();
$this->assertEquals(2, current(array_filter($data, fn($item) => $item->id === $lib1->getKey()))->contentsCount);
$this->assertEquals(5, current(array_filter($data, fn($item) => $item->id === $lib2->getKey()))->contentsCount);
$this->assertEquals(3, current(array_filter($data, fn($item) => $item->id === $lib1->getKey()))->requiredLibrariesCount);
$this->assertEquals(7, current(array_filter($data, fn($item) => $item->id === $lib2->getKey()))->requiredLibrariesCount);
}
}

0 comments on commit 31767b1

Please sign in to comment.