Ordne Übungsabschnitte im Medienbestand ein und filtere Materialarten mehrfach
Deploy Test / test (push) Successful in 2m51s
CI Feature / test (push) Successful in 2m49s
Deploy Test / deploy (push) Successful in 35s

- Medium anlegen: Pflichtfeld Fachliche Materialart (serverseitig geprüft)
- Materialart Übungsabschnitt (automatisch) für Abschnitte im Übungseditor und aus Shinkan, mit Verweis auf die Übung
- Medienmanager: Checkboxen statt Dropdown, Start ohne Übungsabschnitte, persönlicher Standard je Konto
- Zuordnung des Bestands nur bei belegter Herkunft; Übungsabschnitte nie öffentlich

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Sonnet 5.5 committed 2026-11-24 07:40:30 +01:00
1 parent 99122ddfee
commit ad581cb545
29 files changed
+1321 -25

No files matched your search

+7
View File
@@ -32,6 +32,7 @@ class ContentElement extends Model
'editorial_content_kind_id',
'archived_at',
'source_ref',
'origin_composition_id',
];
protected function casts(): array
@@ -47,6 +48,12 @@ public function editorialContentKind(): BelongsTo
return $this->belongsTo(EditorialContentKind::class);
}
/** Die Übung, für die dieser Textabschnitt angelegt wurde (nur bei Übungsabschnitten). */
public function originComposition(): BelongsTo
{
return $this->belongsTo(Composition::class, 'origin_composition_id');
}
public function versions(): HasMany
{
return $this->hasMany(ContentElementVersion::class);
+13 -1
View File
@@ -27,7 +27,13 @@ class EditorialContentKind extends Model
* Modulen (programm, modul), Vertraulichkeit von Traineranleitungen (traineranleitung). Ihre Bezeichnung bleibt
* pflegbar; deaktivieren, Kontexte ändern und löschen sind gesperrt.
*/
public const SYSTEM_SLUGS = ['programm', 'modul', 'uebung', 'traineranleitung'];
public const SYSTEM_SLUGS = ['programm', 'modul', 'uebung', 'traineranleitung', self::EXERCISE_SECTION_SLUG];
/**
* Textabschnitte einer Übung. Die Materialart wird nur automatisch gesetzt (ExerciseSections) und hat deshalb
* keinen wählbaren Kontext.
*/
public const EXERCISE_SECTION_SLUG = 'uebungsabschnitt';
protected $fillable = [
'slug',
@@ -74,6 +80,12 @@ public function allows(string $context): bool
return in_array($context, (array) ($this->contexts ?? []), true);
}
/** Wird nie von Hand gewählt, sondern von der Übungsverwaltung gesetzt. */
public function isAutomatic(): bool
{
return $this->slug === self::EXERCISE_SECTION_SLUG;
}
public function isSystem(): bool
{
return in_array($this->slug, self::SYSTEM_SLUGS, true);
@@ -124,10 +124,52 @@ public function kindFilterOptions(string $context, string $selected = ''): array
public function kindLabel(EditorialContentKind $kind, string $context): string
{
// Die automatische Art hat bewusst keinen wählbaren Kontext; das ist kein Mangel der Zuordnung.
if ($kind->isAutomatic() && $kind->is_active) {
return (string) $kind->label;
}
return (string) $kind->label
.(! $kind->is_active ? self::INACTIVE_SUFFIX : (! $kind->allows($context) ? self::OUT_OF_CONTEXT_SUFFIX : ''));
}
/**
* Materialarten für die Mehrfachauswahl im Medienmanager: im Kontext zulässige Arten, Arten, die bei Medien
* tatsächlich vorkommen, und die automatische Art „Übungsabschnitt“. Die Reihenfolge folgt der Pflege, die
* automatische Art steht zuletzt. „Ohne Materialart“ ergänzt der Medienmanager.
*
* @param list<int> $selectedIds bereits gewählte Arten bleiben sichtbar
* @return list<array{id: int, label: string, automatic: bool}>
*/
public function mediumKindChoices(array $selectedIds = []): array
{
$used = $this->usedKindIds('medium');
$regular = [];
$automatic = [];
foreach (EditorialContentKind::query()->orderBy('sort_order')->orderBy('id')->get() as $kind) {
$id = (int) $kind->id;
$show = $kind->isAutomatic()
|| ($kind->allows('medium') && $kind->is_active)
|| in_array($id, $used, true)
|| in_array($id, $selectedIds, true);
if (! $show) {
continue;
}
$row = ['id' => $id, 'label' => $this->kindLabel($kind, 'medium'), 'automatic' => $kind->isAutomatic()];
if ($kind->isAutomatic()) {
$automatic[] = $row;
} else {
$regular[] = $row;
}
}
return [...$regular, ...$automatic];
}
/**
* @return list<int>
*/
+132
View File
@@ -0,0 +1,132 @@
<?php
namespace App\Domain\Catalog;
use Illuminate\Support\Facades\DB;
/**
* Übungsabschnitte: Textabschnitte, die für eine bestimmte Übung angelegt wurden, im Übungseditor oder bei der
* Übernahme aus Shinkan. Sie liegen im gemeinsamen Medienbestand und tragen dort die Materialart „Übungsabschnitt“
* und einen Verweis auf ihre Übung (`origin_composition_id`). Die Texte nutzen die gemeinsame Inhalts- und
* Fassungsverwaltung; bearbeitet werden sie im Übungseditor.
*/
final class ExerciseSections
{
public const KIND_SLUG = EditorialContentKind::EXERCISE_SECTION_SLUG;
public function kindId(): ?int
{
$id = EditorialContentKind::query()->where('slug', self::KIND_SLUG)->value('id');
return $id === null ? null : (int) $id;
}
/**
* Ordnet einen Abschnitt seiner Übung zu und gibt ihm die Materialart. Eine bereits gesetzte Materialart bleibt:
* Sie ist eine bewusste Einordnung der Redaktion.
*/
public function mark(ContentElement $element, Composition $exercise): void
{
$element->origin_composition_id = $exercise->getKey();
if ($element->editorial_content_kind_id === null) {
$element->editorial_content_kind_id = $this->kindId();
}
$element->save();
}
/**
* Ordnet vorhandene Texte nur dann zu, wenn ihre Herkunft belegt ist:
*
* - Ein Abschnitt aus der Übernahme aus Shinkan (`source_ref` = `shinkan:<Quell-ID>:<Feld>`) gehört zur lokalen
* Übung dieser Quelle.
* - Ein anderer Text gilt als Abschnitt, wenn er ohne Materialart ist, ausschließlich in genau einer Übung
* vorkommt und nirgends in einem Modul oder Programm direkt verwendet wird.
*
* Ein bereits gesetzter Materialtyp bleibt in jedem Fall erhalten, ebenso unklare Fälle.
*
* @return int Anzahl der Texte, die jetzt einer Übung zugeordnet sind
*/
public function backfill(): int
{
$kindId = $this->kindId();
if ($kindId === null) {
return 0;
}
$assigned = 0;
foreach ($this->fromSource() + $this->fromUse() as $elementId => $compositionId) {
DB::table('content_elements')->where('id', $elementId)->whereNull('origin_composition_id')->update(['origin_composition_id' => $compositionId]);
DB::table('content_elements')->where('id', $elementId)->whereNull('editorial_content_kind_id')->update(['editorial_content_kind_id' => $kindId]);
$assigned++;
}
return $assigned;
}
/**
* @return array<int, int> Element-ID => Übung
*/
private function fromSource(): array
{
$byRef = [];
foreach (DB::table('exercise_sources')->whereNotNull('composition_id')->get(['source_id', 'composition_id']) as $source) {
$byRef[(string) $source->source_id] = (int) $source->composition_id;
}
$found = [];
foreach (DB::table('content_elements')->where('output_class', 'text')->where('source_ref', 'like', 'shinkan:%')->get(['id', 'source_ref']) as $element) {
$parts = explode(':', (string) $element->source_ref);
if (count($parts) === 3 && isset($byRef[$parts[1]])) {
$found[(int) $element->id] = $byRef[$parts[1]];
}
}
return $found;
}
/**
* @return array<int, int> Element-ID => Übung
*/
private function fromUse(): array
{
$exerciseKind = CompositionKind::Exercise->value;
$uses = DB::table('composition_members as m')
->join('content_element_versions as v', 'v.id', '=', 'm.content_element_version_id')
->join('content_elements as e', 'e.id', '=', 'v.content_element_id')
->join('composition_versions as cv', 'cv.id', '=', 'm.composition_version_id')
->join('compositions as c', 'c.id', '=', 'cv.composition_id')
->where('e.output_class', 'text')
->whereNull('e.editorial_content_kind_id')
->whereNull('e.origin_composition_id')
->select('e.id as element_id', 'c.id as composition_id', 'c.kind as composition_kind')
->get();
$exercises = [];
$elsewhere = [];
foreach ($uses as $use) {
if ($use->composition_kind === $exerciseKind) {
$exercises[(int) $use->element_id][(int) $use->composition_id] = true;
} else {
$elsewhere[(int) $use->element_id] = true;
}
}
$found = [];
foreach ($exercises as $elementId => $compositions) {
if (count($compositions) === 1 && ! isset($elsewhere[$elementId])) {
$found[$elementId] = array_key_first($compositions);
}
}
return $found;
}
}
+5 -2
View File
@@ -473,7 +473,7 @@ public function adopt(ExerciseSourceSnapshot $snapshot, ?User $actor = null, arr
}
$keepLocal = $decision === 'keep_local' && $latest !== null;
$target = $keepLocal ? $latest : $this->sectionVersion($source, $item['field'], $item['title'], $item['text'], $latest);
$target = $keepLocal ? $latest : $this->sectionVersion($source, $composition, $item['field'], $item['title'], $item['text'], $latest);
$members[] = array_filter([
'role' => $item['role'],
'target' => $target,
@@ -750,7 +750,7 @@ private function localAdditions(?CompositionVersion $previous, array $sourceVari
/**
* Die Textfassung eines Abschnitts der Quelle. Unveränderter Text bleibt dieselbe Fassung.
*/
private function sectionVersion(ExerciseSource $source, string $field, string $title, string $text, ?ContentElementVersion $latest): ContentElementVersion
private function sectionVersion(ExerciseSource $source, Composition $composition, string $field, string $title, string $text, ?ContentElementVersion $latest): ContentElementVersion
{
$content = app(ContentService::class);
$ref = 'shinkan:'.$source->source_id.':'.$field;
@@ -762,6 +762,9 @@ private function sectionVersion(ExerciseSource $source, string $field, string $t
$element->save();
}
// Der Abschnitt gehört zu dieser Übung und liegt als „Übungsabschnitt“ im gemeinsamen Medienbestand.
app(ExerciseSections::class)->mark($element, $composition);
return $latest !== null && FormattedText::same($latest->body, $text)
? $latest
: $content->publishElementVersion($element, $text, [], self::IMPORT_NOTE);