diff --git a/sourcecode/apis/contentauthor/app/H5PLibrary.php b/sourcecode/apis/contentauthor/app/H5PLibrary.php index 62249f939..a6b316f18 100644 --- a/sourcecode/apis/contentauthor/app/H5PLibrary.php +++ b/sourcecode/apis/contentauthor/app/H5PLibrary.php @@ -370,12 +370,14 @@ public function getIconUrl(): string return $icon ?? url('/graphical/h5p_logo.svg'); } - public static function canBeDeleted(int $libraryId): bool + public static function canBeDeleted(int $libraryId, int|null $usageCount = null): bool { - $h5pFramework = app(H5PFrameworkInterface::class); - // Number of references by other content types/libraries. Only counts content using library as main content type, so we skip that - $usage = $h5pFramework->getLibraryUsage($libraryId, skipContent: true); + if ($usageCount === null) { + $h5pFramework = app(H5PFrameworkInterface::class); + // Number of references by other content types/libraries. Only counts content using library as main content type, so we skip that + $usageCount = $h5pFramework->getLibraryUsage($libraryId, skipContent: true)['libraries']; + } - return $usage['libraries'] === 0 && H5PContentLibrary::where('library_id', $libraryId)->doesntExist(); + return $usageCount === 0 && H5PContentLibrary::where('library_id', $libraryId)->doesntExist(); } } diff --git a/sourcecode/apis/contentauthor/app/Http/Controllers/Admin/LibraryUpgradeController.php b/sourcecode/apis/contentauthor/app/Http/Controllers/Admin/LibraryUpgradeController.php index 4dcce6edc..18a2fbd18 100644 --- a/sourcecode/apis/contentauthor/app/Http/Controllers/Admin/LibraryUpgradeController.php +++ b/sourcecode/apis/contentauthor/app/Http/Controllers/Admin/LibraryUpgradeController.php @@ -61,6 +61,7 @@ public function index(): View reset($versions); foreach ($versions as $library) { $usage = $this->h5pFramework->getLibraryUsage($library->id); + $item = [ 'machineName' => $library->name, 'majorVersion' => $library->major_version, @@ -71,7 +72,7 @@ public function index(): View 'hubUpgrade' => null, 'isLast' => $library->id === $lastVersion->id, 'libraryId' => $library->id, - 'canDelete' => H5PLibrary::canBeDeleted($library->id), + 'canDelete' => H5PLibrary::canBeDeleted($library->id, $usage['libraries']), ]; if ($library->runnable) { diff --git a/sourcecode/apis/contentauthor/database/migrations/2025_02_07_124500_create_h5p_contents_libraries_library_id_index.php b/sourcecode/apis/contentauthor/database/migrations/2025_02_07_124500_create_h5p_contents_libraries_library_id_index.php new file mode 100644 index 000000000..03a0f0a1b --- /dev/null +++ b/sourcecode/apis/contentauthor/database/migrations/2025_02_07_124500_create_h5p_contents_libraries_library_id_index.php @@ -0,0 +1,27 @@ +index('library_id'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('h5p_contents_libraries', function (Blueprint $table) { + $table->dropIndex(['library_id']); + }); + } +};