215 lines
7.2 KiB
PHP
215 lines
7.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http;
|
|
|
|
use App\Domain\Catalog\ArticleRelease;
|
|
use App\Domain\Catalog\Composition;
|
|
use App\Domain\Catalog\CompositionDisplayRelease;
|
|
use App\Domain\Catalog\CompositionStructureReader;
|
|
use App\Domain\Catalog\CompositionVersion;
|
|
use App\Domain\Catalog\ContentElementVersion;
|
|
use App\Domain\Catalog\ReleaseStatus;
|
|
use App\Domain\Catalog\StructureNode;
|
|
use App\Domain\Entitlement\DecideAccess;
|
|
use App\Domain\Entitlement\Grant;
|
|
use App\Domain\Entitlement\GrantStatus;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class LibraryCatalog
|
|
{
|
|
public function __construct(
|
|
private readonly CompositionDisplayRelease $display,
|
|
private readonly CompositionStructureReader $structure,
|
|
private readonly DecideAccess $access,
|
|
) {}
|
|
|
|
/**
|
|
* @return array{programs: list<array<string, mixed>>, materials: list<array<string, mixed>>}
|
|
*/
|
|
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' => filled($composition->display_title) ? $composition->display_title : $composition->designation,
|
|
'summary' => $composition->summary,
|
|
'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<array<string, mixed>>}
|
|
*/
|
|
public function program(User $user, Composition $composition): array
|
|
{
|
|
$version = $this->display->highestReleasedVersion($composition);
|
|
abort_if($version === null, 404);
|
|
|
|
$nodes = array_map(
|
|
fn (StructureNode $node): array => $this->present($user, $node),
|
|
$this->structure->tree($version),
|
|
);
|
|
$leaves = $this->leaves($nodes);
|
|
$open = count(array_filter($leaves, fn (array $leaf): bool => $leaf['view_url'] !== null));
|
|
|
|
return [
|
|
'composition' => $composition,
|
|
'title' => filled($composition->display_title) ? $composition->display_title : $composition->designation,
|
|
'summary' => $composition->summary,
|
|
'kind' => $composition->editorialContentKind?->label
|
|
?? ($composition->kind?->label()),
|
|
'learning_goal' => $version->learning_goal,
|
|
'duration_minutes' => $version->duration_minutes,
|
|
'material_note' => $version->material_note,
|
|
'coverage' => $leaves === [] ? 'nicht nutzbar' : ($open === count($leaves) ? 'vollständig' : ($open === 0 ? 'nicht nutzbar' : 'teilweise')),
|
|
'nodes' => $nodes,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
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,
|
|
'program_url' => $this->programUrl($node),
|
|
'children' => array_map(
|
|
fn (StructureNode $child): array => $this->present($user, $child),
|
|
$node->children,
|
|
),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param list<array<string, mixed>> $nodes
|
|
* @return list<array<string, mixed>>
|
|
*/
|
|
private function leaves(array $nodes): array
|
|
{
|
|
$leaves = [];
|
|
|
|
foreach ($nodes as $node) {
|
|
if ($node['children'] === []) {
|
|
$leaves[] = $node;
|
|
|
|
continue;
|
|
}
|
|
|
|
$leaves = [...$leaves, ...$this->leaves($node['children'])];
|
|
}
|
|
|
|
return $leaves;
|
|
}
|
|
|
|
private function programUrl(StructureNode $node): ?string
|
|
{
|
|
if ($node->compositionId === null || $node->compositionVersionId === null) {
|
|
return null;
|
|
}
|
|
|
|
$version = CompositionVersion::query()->find($node->compositionVersionId);
|
|
|
|
if ($version === null || ! $this->display->isReleased($version)) {
|
|
return null;
|
|
}
|
|
|
|
return route('member.program', $node->compositionId);
|
|
}
|
|
|
|
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<int, Grant>
|
|
*/
|
|
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();
|
|
}
|
|
}
|