diff --git a/app/Http/Controllers/Member/LibraryController.php b/app/Http/Controllers/Member/LibraryController.php index 713eaf5..89040d8 100644 --- a/app/Http/Controllers/Member/LibraryController.php +++ b/app/Http/Controllers/Member/LibraryController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Member; use App\Domain\Catalog\ArticleRelease; +use App\Domain\Catalog\Composition; use App\Domain\Catalog\ContentElementVersion; use App\Domain\Catalog\LicenseArticle; use App\Domain\Entitlement\DecideAccess; @@ -10,6 +11,7 @@ use App\Domain\Entitlement\GrantStatus; use App\Http\Controllers\Controller; use App\Http\CourseOutline; +use App\Http\LibraryCatalog; use App\Http\SignedLink; use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage; @@ -20,12 +22,12 @@ class LibraryController extends Controller { public function index(Request $request): View { - return view('member.library', [ - 'grants' => $this->grantQuery($request)->with([ - 'article.currentRelease.rootCompositionVersion.composition', - 'article.currentRelease.rootElementVersion.element', - ])->get(), - ]); + return view('member.library', app(LibraryCatalog::class)->index($request->user())); + } + + public function program(Request $request, Composition $composition): View + { + return view('member.program', app(LibraryCatalog::class)->program($request->user(), $composition)); } public function materials(Request $request): View diff --git a/app/Http/LibraryCatalog.php b/app/Http/LibraryCatalog.php new file mode 100644 index 0000000..59472b7 --- /dev/null +++ b/app/Http/LibraryCatalog.php @@ -0,0 +1,166 @@ +>, materials: list>} + */ + public function index(User $user): array + { + $programs = []; + $materials = []; + + foreach ($this->grants($user) as $grant) { + $article = $grant->article; + $release = $article->currentRelease; + $composition = $release?->rootCompositionVersion?->composition; + $version = $release?->rootCompositionVersion; + + if ($composition !== null && $version !== null && $this->display->isReleased($version)) { + $programs[$composition->id] = [ + 'composition' => $composition, + 'version' => $version, + 'title' => $composition->designation, + 'kind' => $composition->editorialContentKind?->label + ?? ($composition->kind?->label()), + ]; + + continue; + } + + $materials[$article->id] = [ + 'article' => $article, + 'title' => app(CourseOutline::class)->title($article), + 'kind' => app(CourseOutline::class)->kindLabel($article), + ]; + } + + return [ + 'programs' => array_values($programs), + 'materials' => array_values($materials), + ]; + } + + /** + * @return array{composition: Composition, title: string, kind: ?string, nodes: list>} + */ + public function program(User $user, Composition $composition): array + { + $version = $this->display->highestReleasedVersion($composition); + abort_if($version === null, 404); + + return [ + 'composition' => $composition, + 'title' => $composition->designation, + 'kind' => $composition->editorialContentKind?->label + ?? ($composition->kind?->label()), + 'nodes' => array_map( + fn (StructureNode $node): array => $this->present($user, $node), + $this->structure->tree($version), + ), + ]; + } + + /** + * @return array + */ + private function present(User $user, StructureNode $node): array + { + $view = null; + $download = null; + + if ($node->contentElementVersionId !== null) { + $view = $this->open($user, $node->contentElementVersionId, $this->viewAction($node->outputClass)); + $download = $this->open($user, $node->contentElementVersionId, 'laden'); + } + + return [ + 'role' => $node->role, + 'title' => $node->title, + 'kind' => $node->editorialKindLabel ?? $node->technicalKindLabel, + 'output_class' => $node->outputClass, + 'view_url' => $view, + 'download_url' => $download, + 'children' => array_map( + fn (StructureNode $child): array => $this->present($user, $child), + $node->children, + ), + ]; + } + + private function viewAction(?string $outputClass): string + { + return in_array($outputClass, ['film', 'audio'], true) ? 'streamen' : 'ansehen'; + } + + private function open(User $user, int $versionId, string $action): ?string + { + $version = ContentElementVersion::query()->with('element')->find($versionId); + + if ($version === null) { + return null; + } + + $releases = ArticleRelease::query() + ->whereIn('status', [ReleaseStatus::Veroeffentlicht, ReleaseStatus::AbgeloestVerfuegbar]) + ->whereHas('contents', fn ($query) => $query->where('content_element_version_id', $versionId)) + ->with('article') + ->orderByDesc('id') + ->get(); + + foreach ($releases as $release) { + $decision = ($this->access)($user, $release->article, $release, $version, $action); + + if (! $decision->allowed) { + continue; + } + + return SignedLink::temporary('member.media', now()->addMinutes(10), [ + 'article' => $release->article->id, + 'release' => $release->id, + 'version' => $version->id, + 'action' => $action, + 'reader' => $user->id, + ]); + } + + return null; + } + + /** + * @return Collection + */ + private function grants(User $user): Collection + { + return Grant::query() + ->where('status', GrantStatus::Aktiv) + ->whereHas('acquisition', fn ($query) => $query->where('party_id', $user->party_id)) + ->with([ + 'article.currentRelease.rootCompositionVersion.composition.editorialContentKind', + 'article.currentRelease.rootElementVersion.element.editorialContentKind', + ]) + ->orderBy('id') + ->get(); + } +} diff --git a/resources/views/member/library.blade.php b/resources/views/member/library.blade.php index d72dbef..4cb09cc 100644 --- a/resources/views/member/library.blade.php +++ b/resources/views/member/library.blade.php @@ -4,27 +4,33 @@ @section('content') @include('member.partials.nav') - @inject('courses', App\Http\CourseOutline::class)

Bibliothek

-

Jede Berechtigung öffnet ein Programm, ein Modul oder ein einzelnes Material.

- @if ($grants->isEmpty()) +

Programme zeigen den Aufbau. Einzelne Materialien stehen darunter, solange sie zu keinem freigegebenen Programm gehören.

+ @if ($programs === [] && $materials === [])

Noch nichts in der Bibliothek. Unter Angebote können Sie ein kostenloses Material abrufen oder ein Angebot im Testmodus kaufen.

Zu den Angeboten

@else - + @if ($programs !== []) +

Programme und Module

+ + @endif + @if ($materials !== []) +

Materialien

+ + @endif @endif @endsection diff --git a/resources/views/member/partials/structure.blade.php b/resources/views/member/partials/structure.blade.php new file mode 100644 index 0000000..456c5e2 --- /dev/null +++ b/resources/views/member/partials/structure.blade.php @@ -0,0 +1,27 @@ +
    + @foreach ($nodes as $node) +
  1. +

    {{ $node['role'] }}: {{ $node['title'] }}

    + @if ($node['kind']) +

    {{ $node['kind'] }}

    + @endif + @if ($node['output_class'] === 'film' || $node['output_class'] === 'audio') + @if ($node['view_url']) + + @else +

    Für diesen Film liegt keine Freigabe vor.

    + @endif + @elseif ($node['view_url']) +

    Ansehen

    + @elseif ($node['children'] === []) +

    Für diesen Teil liegt keine Freigabe vor.

    + @endif + @if ($node['download_url']) +

    Laden

    + @endif + @if ($node['children'] !== []) + @include('member.partials.structure', ['nodes' => $node['children']]) + @endif +
  2. + @endforeach +
diff --git a/resources/views/member/program.blade.php b/resources/views/member/program.blade.php new file mode 100644 index 0000000..b92cead --- /dev/null +++ b/resources/views/member/program.blade.php @@ -0,0 +1,13 @@ +@extends('layouts.site') + +@section('title', $title) + +@section('content') + @include('member.partials.nav') +

Bibliothek

+

{{ $title }}

+ @if ($kind) +

{{ $kind }}

+ @endif + @include('member.partials.structure', ['nodes' => $nodes]) +@endsection diff --git a/routes/web.php b/routes/web.php index b0f08ba..a864466 100644 --- a/routes/web.php +++ b/routes/web.php @@ -53,6 +53,7 @@ Route::middleware(['auth', 'verified'])->prefix('bereich')->name('member.')->group(function () { Route::get('/', [MemberController::class, 'dashboard'])->name('dashboard'); Route::get('/bibliothek', [LibraryController::class, 'index'])->name('library'); + Route::get('/bibliothek/programm/{composition}', [LibraryController::class, 'program'])->name('program'); Route::get('/bibliothek/{article}', [LibraryController::class, 'show'])->name('article'); Route::get('/ausgabe/{article}/{release}/{version}/{action}', [LibraryController::class, 'media']) ->middleware('signed:relative') diff --git a/tests/Feature/LibraryProgramTest.php b/tests/Feature/LibraryProgramTest.php new file mode 100644 index 0000000..74f2de8 --- /dev/null +++ b/tests/Feature/LibraryProgramTest.php @@ -0,0 +1,70 @@ +publishElementVersion( + $content->createElement('film', 'film', 'Hofszene'), + null, + ['file' => 'inhalte/hof.mp4'], + ); + $programm = EditorialContentKind::query()->where('slug', 'programm')->firstOrFail(); + $released = $content->publishComposition( + tap($content->createComposition('Sicher auf dem Schulweg'), function ($item) use ($programm): void { + $item->editorial_content_kind_id = $programm->id; + $item->save(); + }), + [['role' => 'Film', 'target' => $film]], + ); + $hidden = $content->publishComposition( + $content->createComposition('Noch nicht zeigen'), + [['role' => 'Film', 'target' => $film]], + ); + + $articles = app(ArticleService::class); + $visibleArticle = $articles->create('sichtbar'); + $articles->publishRelease($visibleArticle, $released, 'Programmartikel.'); + $hiddenArticle = $articles->create('verborgen'); + $articles->publishRelease($hiddenArticle, $hidden, 'Nicht freigegeben.'); + + $offers = app(OfferService::class); + $rights = $offers->createRightsProfile('Film', 'ehrenamtlich', ['film' => ['streamen', 'laden']]); + $offer = $offers->create('Programmzugang'); + $offers->activate($offer, [ + ['target' => $visibleArticle->fresh(), 'rights' => $rights], + ], ['grant_kind' => 'auf_abruf']); + + $member = User::factory()->create(); + app(GrantIssuer::class)->claim($member, $offer); + app(CompositionDisplayRelease::class)->release($released, $member); + + $this->actingAs($member) + ->get(route('member.library')) + ->assertOk() + ->assertSee('Sicher auf dem Schulweg') + ->assertDontSee('Noch nicht zeigen'); + + $this->actingAs($member) + ->get(route('member.program', $released->composition)) + ->assertOk() + ->assertSee('Film: Hofszene') + ->assertSee('Laden') + ->assertSee('