>, 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' => 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>} */ 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 */ 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> $nodes * @return list> */ 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 */ 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(); } }