Ein Baustein (FormattedText) bereinigt, bringt Werte in die kanonische Form und wandelt Klartext des Altbestands; RichField ist der eine Editor, ManagesRichFields verbindet ihn mit den Arbeitsplätzen. Formatiert sind Texte der Übungen und Textbausteine, Kurzbeschreibung und Beschreibung, Lernziel und Ausstattung. Mitgliederansicht und PDF zeigen fett, kursiv, Listen, Überschrift, Zitat und Link (PDF: schräge Schnitte, nur wenn gebraucht). Das Livewire-Limit für verschachtelte Pfade steigt auf 30, weil Aufzählungen sonst abgelehnt werden. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
186 lines
7.9 KiB
PHP
186 lines
7.9 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Catalog;
|
|
|
|
use App\Domain\Entitlement\DecideAccess;
|
|
use App\Domain\Entitlement\Grant;
|
|
use App\Domain\Entitlement\GrantStatus;
|
|
use App\Http\LibraryCatalog;
|
|
use App\Models\User;
|
|
|
|
/**
|
|
* Die Anleitung einer Übung, wie ein Stand sie festschreibt: die im Stand gebundene Übungsfassung, die angebotenen
|
|
* Varianten und je Abschnitt oder Medium die Entscheidung der zentralen Rechteprüfung (`DecideAccess`). Ansicht und
|
|
* PDF lesen dieses Ergebnis und zeigen damit dieselbe Fassung.
|
|
*
|
|
* Eine Sperre oder fehlende Berechtigung betrifft nur den betroffenen Abschnitt oder das Medium. Anleitung und übrige
|
|
* Inhalte bleiben im selben Stand erreichbar. Fehlt in einer Variante ein erforderlicher Bestandteil, gilt die Variante
|
|
* als derzeit nicht vollständig durchführbar; ein ergänzender Bestandteil entfällt mit einem Hinweis.
|
|
*/
|
|
class ExerciseGuide
|
|
{
|
|
public function __construct(private readonly DecideAccess $access) {}
|
|
|
|
/**
|
|
* Darf dieses Konto den Stand überhaupt sehen? Aktive, nutzbare Berechtigung auf den Artikel und ein auslieferbarer
|
|
* Stand. Über die einzelnen Inhalte entscheidet danach `DecideAccess`.
|
|
*/
|
|
public function mayOpen(User $user, LicenseArticle $article, ArticleRelease $release): bool
|
|
{
|
|
if ($release->license_article_id !== $article->id
|
|
|| ! in_array($release->status, [ReleaseStatus::Veroeffentlicht, ReleaseStatus::AbgeloestVerfuegbar], true)) {
|
|
return false;
|
|
}
|
|
|
|
return Grant::query()
|
|
->where('license_article_id', $article->id)
|
|
->where('status', GrantStatus::Aktiv)
|
|
->usableBy($user)
|
|
->exists();
|
|
}
|
|
|
|
/**
|
|
* Die Übungsfassung an einer Stelle des Stands. Der Pfad nennt die Positionen der Mitglieder von der Wurzel des
|
|
* Stands abwärts („2“ oder „1-3“). So ist auch dieselbe Übung an zwei Stellen eines Produkts unterscheidbar.
|
|
*
|
|
* @return array{version: CompositionVersion, selection: list<string>|null}|null
|
|
*/
|
|
public function locate(ArticleRelease $release, string $path): ?array
|
|
{
|
|
if (preg_match('/^\d+(-\d+)*$/', $path) !== 1) {
|
|
return null;
|
|
}
|
|
|
|
$version = $release->rootCompositionVersion;
|
|
$selection = null;
|
|
|
|
foreach (array_map('intval', explode('-', $path)) as $position) {
|
|
if ($version === null) {
|
|
return null;
|
|
}
|
|
|
|
$member = $version->members()->where('position', $position)->first();
|
|
|
|
if ($member === null || $member->nested_composition_version_id === null) {
|
|
return null;
|
|
}
|
|
|
|
$selection = is_array($member->variant_selection) ? array_values(array_map('strval', $member->variant_selection)) : null;
|
|
$version = $member->nestedCompositionVersion;
|
|
}
|
|
|
|
$version?->loadMissing('composition');
|
|
|
|
if ($version === null || $version->composition === null || $version->composition->kind !== CompositionKind::Exercise) {
|
|
return null;
|
|
}
|
|
|
|
return ['version' => $version, 'selection' => $selection];
|
|
}
|
|
|
|
/**
|
|
* Pfad zur ersten Stelle, an der die Wurzel die Übungsfassung einbindet.
|
|
*/
|
|
public function pathTo(CompositionVersion $root, int $versionId, array $stack = []): ?string
|
|
{
|
|
if (in_array($root->id, $stack, true)) {
|
|
return null;
|
|
}
|
|
|
|
foreach ($root->members()->orderBy('position')->get() as $member) {
|
|
if ($member->nested_composition_version_id === null) {
|
|
continue;
|
|
}
|
|
|
|
if ($member->nested_composition_version_id === $versionId) {
|
|
return (string) $member->position;
|
|
}
|
|
|
|
$below = $this->pathTo($member->nestedCompositionVersion, $versionId, [...$stack, $root->id]);
|
|
|
|
if ($below !== null) {
|
|
return $member->position.'-'.$below;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* @param list<string>|null $selection angebotene Variantenschlüssel; null = alle
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function build(User $user, LicenseArticle $article, ArticleRelease $release, CompositionVersion $exercise, ?array $selection, ?string $variantKey = null): array
|
|
{
|
|
$exercise->loadMissing(['composition', 'variants', 'members.variant', 'members.contentElementVersion.element']);
|
|
$library = app(LibraryCatalog::class);
|
|
$offered = $exercise->variants->filter(fn (CompositionVersionVariant $variant): bool => $selection === null || in_array($variant->key, $selection, true))->values();
|
|
$chosen = $offered->firstWhere('key', $variantKey) ?? $offered->first();
|
|
$entries = [];
|
|
|
|
foreach ($exercise->members as $member) {
|
|
$version = $member->contentElementVersion;
|
|
|
|
if ($version === null) {
|
|
continue;
|
|
}
|
|
|
|
$key = $member->variant?->key;
|
|
$class = (string) $version->element->output_class;
|
|
$action = in_array($class, ['film', 'audio'], true) ? 'streamen' : 'ansehen';
|
|
$decision = ($this->access)($user, $article, $release, $version, $action);
|
|
$download = $action === 'ansehen' ? ($this->access)($user, $article, $release, $version, 'laden') : null;
|
|
|
|
$entries[] = [
|
|
'variant' => $key,
|
|
'role' => $member->role,
|
|
'note' => $member->placement_note,
|
|
'title' => $library->text($version->element)['title'] ?: $version->element->working_title,
|
|
'class' => $class,
|
|
'requirement' => $member->requirement ?: CompositionMember::REQUIRED,
|
|
'action' => $action,
|
|
'version' => $version,
|
|
'allowed' => $decision->allowed,
|
|
'may_download' => $download?->allowed ?? false,
|
|
'grant' => $decision->grant,
|
|
'download_grant' => $download?->grant,
|
|
'state' => $decision->allowed ? 'ok' : ($version->status === ContentVersionStatus::Gesperrt ? 'gesperrt' : 'nicht_lizenziert'),
|
|
'blocks' => $decision->allowed && $class === 'text' ? FormattedText::blocks($version->body) : null,
|
|
'html' => $decision->allowed && $class === 'text' ? FormattedText::html($version->body) : null,
|
|
];
|
|
}
|
|
|
|
$variants = $offered->map(function (CompositionVersionVariant $variant) use ($entries): array {
|
|
$missing = array_values(array_filter($entries, fn (array $entry): bool => ($entry['variant'] === null || $entry['variant'] === $variant->key)
|
|
&& $entry['requirement'] === CompositionMember::REQUIRED
|
|
&& ! $entry['allowed']));
|
|
|
|
return [
|
|
'key' => $variant->key,
|
|
'name' => $variant->name,
|
|
'description' => $variant->description,
|
|
'complete' => $missing === [],
|
|
'missing' => array_map(fn (array $entry): string => $entry['title'], $missing),
|
|
];
|
|
})->all();
|
|
|
|
$shown = array_values(array_filter($entries, fn (array $entry): bool => $entry['variant'] === null || $entry['variant'] === $chosen?->key));
|
|
|
|
return [
|
|
'exercise' => $exercise,
|
|
'title' => $library->text($exercise->composition)['title'],
|
|
'summary' => $library->text($exercise->composition)['summary'],
|
|
'summary_html' => $library->text($exercise->composition)['summary_html'],
|
|
'learning_goal' => $exercise->learning_goal,
|
|
'duration_minutes' => $exercise->duration_minutes,
|
|
'duration_max' => $exercise->duration_max,
|
|
'prerequisites' => $exercise->prerequisites,
|
|
'material_note' => $exercise->material_note,
|
|
'variants' => $variants,
|
|
'chosen' => $chosen?->key,
|
|
'entries' => $shown,
|
|
'entry_total' => count($entries),
|
|
];
|
|
}
|
|
}
|