> */ public function overview(User $user): Collection { return $this->grants($user)->map(fn (Grant $grant): array => $this->entry($user, $grant))->values(); } /** * @return array */ public function detail(User $user, Grant $grant): array { $grant->loadMissing('article.currentRelease.contents.elementVersion.element', 'article.currentRelease.rootCompositionVersion.composition'); $entry = $this->entry($user, $grant); $release = $grant->article->currentRelease; $classes = []; $contents = []; foreach ($release?->contents ?? [] as $content) { $element = $content->elementVersion?->element; if ($element === null) { continue; } $classes[$element->output_class] = ($classes[$element->output_class] ?? 0) + 1; $contents[$element->id] = [ 'title' => $this->catalog->text($element)['title'], 'kind' => $this->catalog->outputLabel($element->output_class), 'role' => $content->role, ]; } $moduleCount = 0; $rootVersion = $release?->rootCompositionVersion; if ($rootVersion !== null) { $moduleCount = $this->countCompositions($this->structure->tree($rootVersion)); } $scope = []; if ($moduleCount > 0) { $scope[] = $moduleCount.' '.($moduleCount === 1 ? 'Modul' : 'Module'); } foreach ($classes as $class => $count) { $scope[] = $count.' '.$this->plural((string) $class, $count); } $granted = $this->granted($grant); $restrictions = []; foreach (array_keys($classes) as $class) { foreach (CatalogLabels::FAEHIGKEITEN as $key => $label) { if (str_starts_with($key, $class.':') && ! in_array($key, $granted, true)) { $restrictions[] = $label.' ist in diesem Erwerb nicht enthalten.'; } } } if ($entry['state'] !== 'aktiv') { array_unshift($restrictions, $entry['state_note']); } if ($entry['holder_kind'] === 'organisation') { $restrictions[] = 'Die Nutzung gilt, solange deine Mitgliedschaft in dieser Organisation mit Bestellberechtigung besteht.'; } return $entry + [ 'scope' => $scope, 'contents' => array_values($contents), 'restrictions' => $restrictions, 'composition' => $rootVersion?->composition, 'open_url' => $rootVersion?->composition !== null ? route('member.program', $rootVersion->composition) : route('member.article', $grant->article), ]; } /** * @return array */ private function entry(User $user, Grant $grant): array { $grant->loadMissing('article.currentRelease.rootCompositionVersion.composition.editorialContentKind', 'article.currentRelease.rootElementVersion.element', 'acquisition.party'); $article = $grant->article; $release = $article->currentRelease; $state = $this->state($grant); $snapshot = is_array($grant->rights_snapshot) ? $grant->rights_snapshot : []; $conditions = is_array($grant->acquisition?->conditions) ? $grant->acquisition->conditions : []; $holderKind = $grant->acquisition?->party?->type === PartyType::Organisation ? 'organisation' : 'person'; $composition = $release?->rootCompositionVersion?->composition; $element = $release?->rootElementVersion?->element; $imageUrl = null; if ($grant->status === GrantStatus::Aktiv) { $imageUrl = $composition !== null ? ThumbnailRef::url('member.thumbnail', 'komposition', $composition->id, $composition->thumbnail_path) : ($element === null ? null : ThumbnailRef::url('member.thumbnail', 'element', $element->id, $element->thumbnail_path)); } $title = $composition !== null ? $this->catalog->text($composition)['title'] : ($element !== null ? $this->catalog->text($element)['title'] : app(CourseOutline::class)->title($article)); return [ 'grant' => $grant, 'id' => $grant->id, 'title' => $title !== '' ? $title : $article->code, 'kind' => app(CourseOutline::class)->kindLabel($article), 'code' => $article->code, 'image_url' => $imageUrl, 'state' => $state['key'], 'state_label' => $state['label'], 'state_note' => $state['note'], 'active' => $state['key'] === 'aktiv', 'purpose' => filled($snapshot['purpose'] ?? null) ? CatalogLabels::zweck((string) $snapshot['purpose']) : null, 'rights' => CatalogLabels::faehigkeiten(is_array($snapshot['capabilities'] ?? null) ? $snapshot['capabilities'] : []), 'valid_from' => $grant->valid_from, 'valid_until' => $grant->valid_until, 'validity' => $this->validity($grant, $conditions), 'term' => filled($conditions['term_rule'] ?? null) ? ProductOptions::termRuleLabel((string) $conditions['term_rule']) : null, 'updates' => ($conditions['update_promise'] ?? null) === 'alle_freigegebenen_fassungen' ? 'Spätere freigegebene Fassungen sind eingeschlossen.' : null, 'source' => $this->source($user, $grant), 'licence_scope' => $conditions['licence_scope'] ?? null, 'trainer_count' => isset($conditions['trainer_count']) ? (int) $conditions['trainer_count'] : null, 'holder_kind' => $holderKind, 'holder' => $holderKind === 'organisation' ? (string) ($grant->acquisition?->party?->display_name ?? 'Organisation') : 'Persönlich', 'url' => route('member.grant', $grant), ]; } /** * Zustand wie in DecideAccess: Status aktiv, Beginn erreicht, Ende nicht überschritten. * * @return array{key: string, label: string, note: string} */ private function state(Grant $grant): array { $now = now(); if ($grant->status === GrantStatus::Widerrufen) { return ['key' => 'widerrufen', 'label' => 'Widerrufen', 'note' => 'Diese Berechtigung wurde widerrufen.'.$this->reason($grant)]; } if ($grant->status === GrantStatus::Ausgesetzt) { return ['key' => 'ausgesetzt', 'label' => 'Ausgesetzt', 'note' => 'Diese Berechtigung ist derzeit ausgesetzt.'.$this->reason($grant)]; } if ($grant->status === GrantStatus::Abgelaufen || ($grant->valid_until !== null && ! $grant->valid_until->greaterThan($now))) { return ['key' => 'abgelaufen', 'label' => 'Abgelaufen', 'note' => 'Der Zeitraum dieser Berechtigung ist abgelaufen.']; } if ($grant->valid_from !== null && $grant->valid_from->greaterThan($now)) { return ['key' => 'geplant', 'label' => 'Noch nicht gültig', 'note' => 'Diese Berechtigung beginnt am '.$this->date($grant->valid_from).'.']; } return ['key' => 'aktiv', 'label' => 'Aktiv', 'note' => '']; } private function reason(Grant $grant): string { return filled($grant->status_reason) ? ' Grund: '.$grant->status_reason : ''; } /** * @param array $conditions */ private function validity(Grant $grant, array $conditions): string { $parts = []; if ($grant->valid_from !== null) { $parts[] = 'seit '.$this->date($grant->valid_from); } if ($grant->valid_until !== null) { $parts[] = 'bis '.$this->date($grant->valid_until); } elseif (($conditions['term_rule'] ?? 'dauerhaft') === 'dauerhaft') { $parts[] = 'ohne Ablaufdatum'; } else { $parts[] = 'Ende nicht hinterlegt'; } return implode(' · ', $parts); } /** * Herkunft des Rechts: Art des Erwerbs und, soweit vorhanden, die Bezeichnung des Angebots. * * @return array{label: string, detail: ?string, url: ?string} */ private function source(User $user, Grant $grant): array { $acquisition = $grant->acquisition; $type = $acquisition?->type; $label = match ($type) { AcquisitionType::Bestellposition => 'Kauf', AcquisitionType::FreierAbruf => 'Kostenloser Abruf', AcquisitionType::ManuelleVergabe => 'Vergabe durch das Team', AcquisitionType::Abonnement => 'Abonnement', default => 'Erwerb', }; $detail = null; $url = null; if ($type === AcquisitionType::Bestellposition && $acquisition?->reference_type === 'order_line') { $line = OrderLine::query()->with('order')->find($acquisition->reference_id); if ($line !== null) { $detail = $line->offer_name; if ($line->order?->user_id === $user->id) { $url = route('member.order', $line->order); } } } return ['label' => $label, 'detail' => $detail, 'url' => $url]; } /** * @return list Schlüssel „Klasse:Aktion“ der Berechtigung */ private function granted(Grant $grant): array { $keys = []; $capabilities = is_array($grant->rights_snapshot['capabilities'] ?? null) ? $grant->rights_snapshot['capabilities'] : []; foreach ($capabilities as $class => $actions) { foreach ((array) $actions as $action) { $keys[] = $class.':'.$action; } } return $keys; } /** * @param list $nodes */ private function countCompositions(array $nodes): int { $count = 0; foreach ($nodes as $node) { if ($node->compositionId !== null) { $count++; } $count += $this->countCompositions($node->children); } return $count; } private function plural(string $class, int $count): string { $forms = match ($class) { 'film' => ['Film', 'Filme'], 'audio' => ['Hörstück', 'Hörstücke'], 'document', 'pdf' => ['Dokument', 'Dokumente'], 'text' => ['Text', 'Texte'], 'bild' => ['Bild', 'Bilder'], default => [$class, $class], }; return $count === 1 ? $forms[0] : $forms[1]; } private function date(CarbonInterface $date): string { return $date->timezone(config('app.timezone'))->format('d.m.Y'); } /** * Alle Berechtigungen des Kontos, auch nicht mehr aktive: Inaktive sollen nachvollziehbar bleiben. * * @return Collection */ private function grants(User $user): Collection { return Grant::query() ->usableBy($user) ->with('article.currentRelease', 'acquisition.party') ->orderBy('id') ->get(); } }