Files
laraundfelix/app/Http/Controllers/Member/ExerciseController.php
T
LarsandClaude Sonnet 5.5 81037b9868
Deploy Test / test (push) Successful in 3m0s
Deploy Test / deploy (push) Successful in 25s
Lasse Bilder im Text den Rechten des Textes folgen, gib Bild- und PDF-Adressen ohne Ablauf aus und belege die Abnahme
- Bilder im Text folgen dem Recht am Text; eine eigene Fähigkeit für Bilder ist nicht nötig (ADR-51, fachliche Entscheidung zur Bestätigung).
- Bildadresse und PDF-Adresse des Dokuments laufen nicht ab; jeder Abruf entscheidet neu.
- Die Fähigkeit text:laden ist im Rechteprofil wählbar.
- Dokumentkapitel in der Medienansicht verweisen auf ihr Dokument; die Pille nennt jetzt auch Dokumentkapitel (MedienFilterDialogTest bewusst angepasst).
- Abnahmeprotokoll mit Bildern unter docs/ux/verwaltung/abnahme/dokumente.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-24 12:55:21 +01:00

134 lines
6.0 KiB
PHP

<?php
namespace App\Http\Controllers\Member;
use App\Domain\Catalog\ArticleRelease;
use App\Domain\Catalog\CompositionVersion;
use App\Domain\Catalog\ContentElementVersion;
use App\Domain\Catalog\EmbeddedImages;
use App\Domain\Catalog\ExerciseGuide;
use App\Domain\Catalog\ExercisePdf;
use App\Domain\Catalog\LicenseArticle;
use App\Domain\Media\DeliveryLog;
use App\Http\Controllers\Controller;
use App\Http\SignedLink;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
/**
* Die Übung als formatierte Anleitung innerhalb des erworbenen Moduls oder Programms und ihre PDF-Ausgabe. Beide zeigen
* die im Artikel-Stand gebundene Übungsfassung und die dort angebotenen Varianten. Über jeden Abschnitt und jedes
* Medium entscheidet `DecideAccess`; auch die direkte PDF-Adresse wird bei jedem Abruf neu entschieden.
*/
class ExerciseController extends Controller
{
public function show(Request $request, LicenseArticle $article, ArticleRelease $release, string $path): View
{
[$guide, $located] = $this->load($request, $article, $release, $path);
$user = $request->user();
$copy = DeliveryLog::newCopyId();
$shown = false;
foreach ($guide['entries'] as $index => $entry) {
$link = fn (string $action) => SignedLink::temporary('member.media', now()->addMinutes(10), [
'article' => $article->id,
'release' => $release->id,
'version' => $entry['version']->id,
'action' => $action,
'reader' => $user->id,
]);
$guide['entries'][$index]['url'] = $entry['allowed'] && $entry['class'] !== 'text' ? $link($entry['action']) : null;
$guide['entries'][$index]['download_url'] = $entry['allowed'] && $entry['may_download'] && $entry['class'] !== 'text' ? $link('laden') : null;
if ($entry['allowed'] && $entry['class'] === 'text' && $entry['grant'] !== null) {
app(DeliveryLog::class)->write($entry['grant'], $article, $release, $entry['version'], 'ansehen', 'text', hash('sha256', (string) $entry['version']->body), 'k2', $copy, $located['version']->id);
$shown = true;
}
}
$pdfAllowed = app(ExercisePdf::class)->sections($guide) !== null;
return view('member.exercise', [
'guide' => $guide,
'article' => $article,
'release' => $release,
'path' => $path,
'reader' => $user->name,
'copy' => $shown ? $copy : null,
'pdfUrl' => $pdfAllowed ? SignedLink::temporary('member.exercise.pdf', now()->addMinutes(10), [
'article' => $article->id,
'release' => $release->id,
'path' => $path,
'variante' => $guide['chosen'],
'reader' => $user->id,
]) : null,
'variantUrl' => fn (string $key): string => route('member.exercise', ['article' => $article, 'release' => $release, 'path' => $path, 'variante' => $key]),
]);
}
public function pdf(Request $request, LicenseArticle $article, ArticleRelease $release, string $path): Response
{
abort_unless((int) $request->query('reader') === $request->user()->id, 403);
[$guide, $located] = $this->load($request, $article, $release, $path);
$pdf = app(ExercisePdf::class);
$sections = $pdf->sections($guide);
abort_if($sections === null, 403, 'Für dieses PDF fehlt die Berechtigung oder ein erforderlicher Bestandteil.');
$copy = DeliveryLog::newCopyId();
$variant = collect($guide['variants'])->firstWhere('key', $guide['chosen']);
$images = app(EmbeddedImages::class)->forPdf($request->user(), $article, $release, array_values(array_map(fn (array $section): ContentElementVersion => $section['version'], array_filter($sections, fn (array $section): bool => $section['class'] === 'text'))));
$content = $pdf->render(
$guide,
$sections,
$request->user()->name,
$copy,
(string) ($variant['name'] ?? ''),
route('member.exercise', ['article' => $article, 'release' => $release, 'path' => $path, 'variante' => $guide['chosen']]),
$images,
);
$checksum = hash('sha256', $content);
foreach ($sections as $section) {
$grant = $section['download_grant'] ?? $section['grant'];
if ($grant !== null) {
app(DeliveryLog::class)->write($grant, $article, $release, $section['version'], 'laden', 'pdf', $checksum, 'k2', $copy, $located['version']->id);
}
}
// Bilder im Text sind eigene Fassungen: Auch sie stehen im Ausgabenachweis dieses PDF.
foreach ($images->used() as $image) {
app(DeliveryLog::class)->write($image['grant'], $article, $release, $image['version'], 'laden', 'pdf', $checksum, 'k2', $copy, $located['version']->id);
}
$name = str($guide['title'])->slug()->value() ?: 'uebung';
return response($content, 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="'.$name.'.pdf"',
'X-License-Copy' => $copy,
'Cache-Control' => 'private, no-store',
]);
}
/**
* @return array{0: array<string, mixed>, 1: array{version: CompositionVersion, selection: list<string>|null}}
*/
private function load(Request $request, LicenseArticle $article, ArticleRelease $release, string $path): array
{
$guides = app(ExerciseGuide::class);
abort_unless($guides->mayOpen($request->user(), $article, $release), 403);
$located = $guides->locate($release, $path);
abort_if($located === null, 404);
$variant = $request->query('variante');
return [
$guides->build($request->user(), $article, $release, $located['version'], $located['selection'], is_string($variant) ? $variant : null),
$located,
];
}
}