Quellübung mit stabiler Kennung und Inhaltswert, genau ein Stand je Prüfsumme, bewusste Übernahme als neue lokale Fassung, lokale Ergänzungen bleiben erhalten, Ausblenden an der Quelle setzt nur die Verfügbarkeit. Seite Übernahme (Shinkan), Feldzuordnung, ADR-49 (inklusive Abweichung bei der Mediensperre), Abnahmestand und Rückfallverfahren. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
529 lines
20 KiB
PHP
529 lines
20 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Catalog;
|
|
|
|
use App\Jobs\StoreContentFile;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ContentService
|
|
{
|
|
public function __construct(private readonly ContentResolver $resolver) {}
|
|
|
|
public function createElement(string $kind, string $outputClass, string $workingTitle): ContentElement
|
|
{
|
|
return ContentElement::query()->create([
|
|
'kind' => $this->required($kind, 'Die Art des Inhaltselements fehlt.'),
|
|
'output_class' => $this->required($outputClass, 'Die Ausgabeklasse fehlt.'),
|
|
'working_title' => $this->required($workingTitle, 'Der Arbeitstitel fehlt.'),
|
|
]);
|
|
}
|
|
|
|
public function block(ContentElementVersion|CompositionVersion $version, string $reason): void
|
|
{
|
|
if (trim($reason) === '') {
|
|
throw new DomainRuleException('Eine Sperre braucht eine Begründung.');
|
|
}
|
|
|
|
if ($version->status !== ContentVersionStatus::Bereit) {
|
|
throw new DomainRuleException('Nur eine bereitgestellte Fassung kann gesperrt werden.');
|
|
}
|
|
|
|
$version->status = ContentVersionStatus::Gesperrt;
|
|
|
|
if ($version instanceof ContentElementVersion) {
|
|
$version->block_reason = trim($reason);
|
|
}
|
|
|
|
$version->save();
|
|
}
|
|
|
|
public function describe(ContentElement $element, string $workingTitle, ?string $description): void
|
|
{
|
|
$element->working_title = $this->required($workingTitle, 'Der Arbeitstitel fehlt.');
|
|
$description = trim((string) $description);
|
|
$element->description = $description === '' ? null : $description;
|
|
$element->save();
|
|
}
|
|
|
|
public function publishElementVersion(
|
|
ContentElement $element,
|
|
?string $body = null,
|
|
array $metadata = [],
|
|
?string $changeNote = null,
|
|
): ContentElementVersion {
|
|
if (($body === null || $body === '') && $metadata === []) {
|
|
throw new DomainRuleException('Eine Inhaltsfassung braucht einen Inhalt.');
|
|
}
|
|
|
|
$number = ((int) $element->versions()->max('number')) + 1;
|
|
$incoming = $metadata['incoming'] ?? null;
|
|
$version = $element->versions()->create([
|
|
'number' => $number,
|
|
'status' => is_string($incoming) && $incoming !== '' ? ContentVersionStatus::Entwurf : ContentVersionStatus::Bereit,
|
|
'content_hash' => hash('sha256', json_encode([
|
|
'body' => $body,
|
|
'metadata' => $metadata,
|
|
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE)),
|
|
'body' => $body,
|
|
'metadata' => $metadata === [] ? null : $metadata,
|
|
'change_note' => $changeNote,
|
|
]);
|
|
|
|
if (is_string($incoming) && $incoming !== '') {
|
|
StoreContentFile::dispatch($version->id, $incoming);
|
|
$version->refresh();
|
|
}
|
|
|
|
return $version;
|
|
}
|
|
|
|
public function draftElementVersion(
|
|
ContentElement $element,
|
|
?string $body = null,
|
|
array $metadata = [],
|
|
): ContentElementVersion {
|
|
if (($body === null || $body === '') && $metadata === []) {
|
|
throw new DomainRuleException('Eine Inhaltsfassung braucht einen Inhalt.');
|
|
}
|
|
|
|
$number = ((int) $element->versions()->max('number')) + 1;
|
|
$version = $element->versions()->create([
|
|
'number' => $number,
|
|
'status' => ContentVersionStatus::Entwurf,
|
|
'content_hash' => hash('sha256', json_encode([
|
|
'body' => $body,
|
|
'metadata' => $metadata,
|
|
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE)),
|
|
'body' => $body,
|
|
'metadata' => $metadata === [] ? null : $metadata,
|
|
'change_note' => null,
|
|
]);
|
|
$incoming = $metadata['incoming'] ?? null;
|
|
|
|
if (is_string($incoming) && $incoming !== '') {
|
|
StoreContentFile::dispatch($version->id, $incoming);
|
|
$version->refresh();
|
|
}
|
|
|
|
return $version;
|
|
}
|
|
|
|
public function isCommitted(ContentElementVersion $version): bool
|
|
{
|
|
if ($version->status !== ContentVersionStatus::Bereit) {
|
|
return false;
|
|
}
|
|
|
|
$usedByComposition = CompositionMember::query()
|
|
->where('content_element_version_id', $version->id)
|
|
->whereHas('compositionVersion', function ($query): void {
|
|
$query->where('status', ContentVersionStatus::Bereit);
|
|
})
|
|
->exists();
|
|
|
|
if ($usedByComposition) {
|
|
return true;
|
|
}
|
|
|
|
return ReleaseContent::query()
|
|
->where('content_element_version_id', $version->id)
|
|
->exists();
|
|
}
|
|
|
|
public function reviseElementContent(ContentElementVersion $version, string $body): ContentElementVersion
|
|
{
|
|
$body = trim($body);
|
|
|
|
if ($body === '') {
|
|
throw new DomainRuleException('Der Text braucht einen Inhalt.');
|
|
}
|
|
|
|
$version->loadMissing('element');
|
|
|
|
if ($version->status === ContentVersionStatus::Gesperrt) {
|
|
throw new DomainRuleException('Eine deaktivierte Fassung lässt sich nicht ändern.');
|
|
}
|
|
|
|
if (trim((string) $version->body) === $body) {
|
|
return $version;
|
|
}
|
|
|
|
if ($this->isCommitted($version)) {
|
|
return $this->publishElementVersion($version->element, $body);
|
|
}
|
|
|
|
$metadata = $version->metadata ?? [];
|
|
$version->body = $body;
|
|
$version->content_hash = hash('sha256', json_encode([
|
|
'body' => $body,
|
|
'metadata' => $metadata,
|
|
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));
|
|
$version->save();
|
|
|
|
return $version->refresh();
|
|
}
|
|
|
|
public function elementReleaseProblem(ContentElementVersion $version): ?string
|
|
{
|
|
$version->loadMissing('element');
|
|
|
|
if ($version->status === ContentVersionStatus::Gesperrt) {
|
|
return 'Diese Fassung ist deaktiviert und kann nicht freigegeben werden.';
|
|
}
|
|
|
|
if ($version->status === ContentVersionStatus::Bereit) {
|
|
return null;
|
|
}
|
|
|
|
$meta = is_array($version->metadata) ? $version->metadata : [];
|
|
|
|
if (isset($meta['incoming']) && ! isset($meta['file'])) {
|
|
return 'Die Datei ist noch nicht übernommen. Die Freigabe wartet, bis die Datei bereitsteht.';
|
|
}
|
|
|
|
if (isset($meta['error']) && is_string($meta['error']) && trim($meta['error']) !== '') {
|
|
return trim($meta['error']);
|
|
}
|
|
|
|
if ($version->element?->output_class === 'text' && trim((string) $version->body) === '') {
|
|
return 'Der Text braucht einen Inhalt.';
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function releaseElementVersion(ContentElementVersion $version): ContentElementVersion
|
|
{
|
|
$version->loadMissing('element');
|
|
|
|
if ($version->status === ContentVersionStatus::Bereit) {
|
|
return $version;
|
|
}
|
|
|
|
if ($version->status !== ContentVersionStatus::Entwurf) {
|
|
throw new DomainRuleException('Eine deaktivierte Fassung lässt sich nicht freigeben.');
|
|
}
|
|
|
|
$problem = $this->elementReleaseProblem($version);
|
|
|
|
if ($problem !== null) {
|
|
throw new DomainRuleException($problem);
|
|
}
|
|
|
|
$meta = is_array($version->metadata) ? $version->metadata : [];
|
|
unset($meta['held']);
|
|
$version->metadata = $meta === [] ? null : $meta;
|
|
$version->content_hash = hash('sha256', json_encode([
|
|
'body' => $version->body,
|
|
'metadata' => $meta,
|
|
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));
|
|
$version->status = ContentVersionStatus::Bereit;
|
|
$version->save();
|
|
|
|
return $version->refresh();
|
|
}
|
|
|
|
/**
|
|
* @param list<array{role: string, target: ContentElementVersion|CompositionVersion, note?: string|null, variant?: string|null, requirement?: string|null, selection?: list<string>|null}> $members
|
|
* @param array{learning_goal?: mixed, duration_minutes?: mixed, duration_max?: mixed, material_note?: mixed, prerequisites?: mixed} $didactic
|
|
* @param list<array{key: string, name: string, description?: string|null, origin?: string}> $variants Durchführungsvarianten einer Übung
|
|
*/
|
|
public function publishComposition(
|
|
Composition $composition,
|
|
array $members,
|
|
?User $confirmedBy = null,
|
|
?string $note = null,
|
|
array $didactic = [],
|
|
array $variants = [],
|
|
): CompositionVersion {
|
|
if ($members === []) {
|
|
throw new DomainRuleException('Eine Komposition braucht mindestens ein Mitglied.');
|
|
}
|
|
|
|
$variants = $this->checkedVariants($variants);
|
|
$this->assertAcyclic($composition->id, $members);
|
|
|
|
foreach ($members as $member) {
|
|
$this->assertReadyMember($member['target']);
|
|
$this->assertMemberPlacement($member, $variants);
|
|
}
|
|
|
|
return DB::transaction(function () use ($composition, $members, $confirmedBy, $note, $didactic, $variants): CompositionVersion {
|
|
$number = ((int) $composition->versions()->max('number')) + 1;
|
|
$version = $composition->versions()->create([
|
|
'number' => $number,
|
|
'status' => ContentVersionStatus::Entwurf,
|
|
'members_hash' => $this->membersHash($members),
|
|
'resolved_hash' => 'pending',
|
|
'learning_goal' => $this->optionalText($didactic['learning_goal'] ?? null),
|
|
'duration_minutes' => isset($didactic['duration_minutes']) ? (int) $didactic['duration_minutes'] : null,
|
|
'duration_max' => isset($didactic['duration_max']) ? (int) $didactic['duration_max'] : null,
|
|
'material_note' => $this->optionalText($didactic['material_note'] ?? null),
|
|
'prerequisites' => $this->optionalText($didactic['prerequisites'] ?? null),
|
|
'source_snapshot_id' => isset($didactic['source_snapshot_id']) ? (int) $didactic['source_snapshot_id'] : null,
|
|
]);
|
|
|
|
$variantIds = [];
|
|
|
|
foreach ($variants as $index => $variant) {
|
|
$variantIds[$variant['key']] = $version->variants()->create([
|
|
'key' => $variant['key'],
|
|
'position' => $index + 1,
|
|
'name' => $variant['name'],
|
|
'description' => $variant['description'],
|
|
'origin' => $variant['origin'],
|
|
])->id;
|
|
}
|
|
|
|
foreach (array_values($members) as $index => $member) {
|
|
$target = $member['target'];
|
|
$selection = $target instanceof CompositionVersion ? ($member['selection'] ?? null) : null;
|
|
$version->members()->create([
|
|
'position' => $index + 1,
|
|
'role' => $this->required($member['role'], 'Die Rolle eines Mitglieds fehlt.'),
|
|
'placement_note' => $this->optionalText($member['note'] ?? null),
|
|
'content_element_version_id' => $target instanceof ContentElementVersion ? $target->id : null,
|
|
'nested_composition_version_id' => $target instanceof CompositionVersion ? $target->id : null,
|
|
'variant_id' => isset($member['variant']) ? $variantIds[$member['variant']] : null,
|
|
'requirement' => $member['requirement'] ?? CompositionMember::REQUIRED,
|
|
'variant_selection' => $selection === null ? null : array_values($selection),
|
|
'origin' => ($member['origin'] ?? 'lokal') === 'quelle' ? 'quelle' : 'lokal',
|
|
]);
|
|
}
|
|
|
|
$version->load('members.variant', 'variants');
|
|
$resolvedHash = $this->resolver->resolvedHash($version);
|
|
$structureHash = $this->structureHash($version);
|
|
$version->attestations()->create([
|
|
'resolved_hash' => $resolvedHash,
|
|
'structure_hash' => $structureHash,
|
|
'confirmed_by' => $confirmedBy?->id,
|
|
'confirmed_at' => now(),
|
|
'note' => $note,
|
|
]);
|
|
$version->resolved_hash = $resolvedHash;
|
|
$version->structure_hash = $structureHash;
|
|
$version->status = ContentVersionStatus::Bereit;
|
|
$version->save();
|
|
|
|
return $version->load('members');
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Änderungsnachweis einer Fassung: Felder, Varianten und die Verwendung jedes Mitglieds. Anders als der
|
|
* `resolved_hash` (nur die Menge der enthaltenen Medienfassungen) ändert er sich auch dann, wenn Variante,
|
|
* Verwendung, Reihenfolge oder Variantenauswahl wechseln und die Medien gleich bleiben.
|
|
*/
|
|
public function structureHash(CompositionVersion $version): string
|
|
{
|
|
$version->loadMissing('members.variant', 'variants');
|
|
|
|
return hash('sha256', json_encode([
|
|
'fields' => [
|
|
$version->learning_goal,
|
|
$version->duration_minutes,
|
|
$version->duration_max,
|
|
$version->material_note,
|
|
$version->prerequisites,
|
|
],
|
|
'variants' => $version->variants->map(fn (CompositionVersionVariant $variant): array => [
|
|
$variant->key, $variant->position, $variant->name, $variant->description, $variant->origin,
|
|
])->all(),
|
|
'members' => $version->members->map(fn (CompositionMember $member): array => [
|
|
$member->position,
|
|
$member->role,
|
|
$member->placement_note,
|
|
$member->content_element_version_id,
|
|
$member->nested_composition_version_id,
|
|
$member->variant?->key,
|
|
$member->requirement,
|
|
$member->variant_selection,
|
|
$member->origin,
|
|
])->all(),
|
|
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));
|
|
}
|
|
|
|
/**
|
|
* @param list<array{key?: string, name?: string, description?: string|null}> $variants
|
|
* @return list<array{key: string, name: string, description: string|null}>
|
|
*/
|
|
private function checkedVariants(array $variants): array
|
|
{
|
|
$checked = [];
|
|
$keys = [];
|
|
|
|
foreach (array_values($variants) as $variant) {
|
|
$key = trim((string) ($variant['key'] ?? ''));
|
|
|
|
if ($key === '' || in_array($key, $keys, true)) {
|
|
throw new DomainRuleException('Jede Variante braucht einen eigenen Schlüssel.');
|
|
}
|
|
|
|
$keys[] = $key;
|
|
$checked[] = [
|
|
'key' => $key,
|
|
'name' => $this->required((string) ($variant['name'] ?? ''), 'Jede Variante braucht einen Namen.'),
|
|
'description' => $this->optionalText($variant['description'] ?? null),
|
|
'origin' => ($variant['origin'] ?? 'lokal') === 'quelle' ? 'quelle' : 'lokal',
|
|
];
|
|
}
|
|
|
|
return $checked;
|
|
}
|
|
|
|
/**
|
|
* @param array{target: ContentElementVersion|CompositionVersion, variant?: string|null, requirement?: string|null, selection?: list<string>|null} $member
|
|
* @param list<array{key: string, name: string, description: string|null}> $variants
|
|
*/
|
|
private function assertMemberPlacement(array $member, array $variants): void
|
|
{
|
|
$variant = $member['variant'] ?? null;
|
|
|
|
if ($variant !== null && ! in_array($variant, array_column($variants, 'key'), true)) {
|
|
throw new DomainRuleException('Ein Mitglied verweist auf eine Variante, die es in dieser Fassung nicht gibt.');
|
|
}
|
|
|
|
$requirement = $member['requirement'] ?? CompositionMember::REQUIRED;
|
|
|
|
if (! in_array($requirement, [CompositionMember::REQUIRED, CompositionMember::SUPPLEMENTARY], true)) {
|
|
throw new DomainRuleException('Die Verwendung eines Mitglieds ist erforderlich oder ergänzend.');
|
|
}
|
|
|
|
$selection = $member['selection'] ?? null;
|
|
|
|
if ($selection === null) {
|
|
return;
|
|
}
|
|
|
|
if (! $member['target'] instanceof CompositionVersion) {
|
|
throw new DomainRuleException('Eine Variantenauswahl gibt es nur an einer eingebundenen Übungsfassung.');
|
|
}
|
|
|
|
$offered = $member['target']->variants()->pluck('key')->all();
|
|
|
|
if ($selection === [] || array_diff($selection, $offered) !== []) {
|
|
throw new DomainRuleException('Die Variantenauswahl nennt eine Variante, die die eingebundene Fassung nicht anbietet, oder keine.');
|
|
}
|
|
}
|
|
|
|
public function createComposition(string $designation, CompositionKind $kind = CompositionKind::Uebung): Composition
|
|
{
|
|
return Composition::query()->create([
|
|
'designation' => $this->required($designation, 'Die Bezeichnung fehlt.'),
|
|
'kind' => $kind,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @return list<int>
|
|
*/
|
|
public function containingCompositionIds(ContentElementVersion $version): array
|
|
{
|
|
$ids = [];
|
|
|
|
foreach (CompositionVersion::query()->where('status', ContentVersionStatus::Bereit)->get() as $compositionVersion) {
|
|
$contained = array_column($this->resolver->expand($compositionVersion), 'version_id');
|
|
|
|
if (in_array($version->id, $contained, true)) {
|
|
$ids[] = $compositionVersion->id;
|
|
}
|
|
}
|
|
|
|
return $ids;
|
|
}
|
|
|
|
/**
|
|
* @param list<array{role: string, target: ContentElementVersion|CompositionVersion}> $members
|
|
* @param list<int> $stack
|
|
*/
|
|
private function assertAcyclic(int $compositionId, array $members, array $stack = []): void
|
|
{
|
|
if (in_array($compositionId, $stack, true)) {
|
|
throw new DomainRuleException('Kompositionen dürfen keinen Zyklus bilden.');
|
|
}
|
|
|
|
$stack[] = $compositionId;
|
|
|
|
foreach ($members as $member) {
|
|
$target = $member['target'];
|
|
|
|
if (! $target instanceof CompositionVersion) {
|
|
continue;
|
|
}
|
|
|
|
$nested = [];
|
|
|
|
foreach ($target->members()->orderBy('position')->get() as $row) {
|
|
$child = $row->contentElementVersion ?? $row->nestedCompositionVersion;
|
|
|
|
if ($child === null) {
|
|
throw new DomainRuleException('Ein Kompositionsmitglied braucht genau ein Ziel.');
|
|
}
|
|
|
|
$nested[] = ['role' => $row->role, 'target' => $child];
|
|
}
|
|
|
|
$this->assertAcyclic($target->composition_id, $nested, $stack);
|
|
}
|
|
}
|
|
|
|
private function assertReadyMember(ContentElementVersion|CompositionVersion $target): void
|
|
{
|
|
if (app(ForeignCompositions::class)->reaches($target)) {
|
|
throw new DomainRuleException('Dieser Bestandteil stammt aus einer neueren Programmversion und wird hier nicht in eine Fassung aufgenommen.');
|
|
}
|
|
|
|
if ($target->status !== ContentVersionStatus::Bereit) {
|
|
throw new DomainRuleException('Eine Komposition darf nur bereitgestellte Fassungen enthalten.');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param list<array{role: string, target: ContentElementVersion|CompositionVersion}> $members
|
|
*/
|
|
private function membersHash(array $members): string
|
|
{
|
|
$list = [];
|
|
|
|
foreach (array_values($members) as $index => $member) {
|
|
$target = $member['target'];
|
|
$list[] = [
|
|
'position' => $index + 1,
|
|
'role' => $member['role'],
|
|
'note' => $this->optionalText($member['note'] ?? null),
|
|
'element_version_id' => $target instanceof ContentElementVersion ? $target->id : null,
|
|
'composition_version_id' => $target instanceof CompositionVersion ? $target->id : null,
|
|
'variant' => $member['variant'] ?? null,
|
|
'requirement' => $member['requirement'] ?? CompositionMember::REQUIRED,
|
|
'selection' => $member['selection'] ?? null,
|
|
];
|
|
}
|
|
|
|
return hash('sha256', json_encode($list, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));
|
|
}
|
|
|
|
private function optionalText(mixed $value): ?string
|
|
{
|
|
if (! is_string($value)) {
|
|
return null;
|
|
}
|
|
|
|
$value = trim($value);
|
|
|
|
return $value === '' ? null : $value;
|
|
}
|
|
|
|
private function required(string $value, string $message): string
|
|
{
|
|
$value = trim($value);
|
|
|
|
if ($value === '') {
|
|
throw new DomainRuleException($message);
|
|
}
|
|
|
|
return $value;
|
|
}
|
|
}
|