fix: Import ersetzt bestehende Fragen der betroffenen Fächer

This commit is contained in:
root
2026-05-05 15:12:24 +00:00
parent 085ea7c3ca
commit 03b59f3e77
@@ -129,9 +129,21 @@ class QuestionController extends Controller {
return back()->with('error', 'Ungültiges JSON-Format.');
}
$subjects = Subject::pluck('id','slug');
// Collect subject IDs that appear in the file
$affectedIds = collect($raw)
->pluck('subject')
->unique()
->filter(fn($s) => isset($subjects[$s]))
->map(fn($s) => $subjects[$s])
->values();
$imported = 0;
$skipped = 0;
DB::transaction(function() use ($raw, $subjects, &$imported, &$skipped) {
DB::transaction(function() use ($raw, $subjects, $affectedIds, &$imported, &$skipped) {
// Delete existing questions for those subjects
Question::whereIn('subject_id', $affectedIds)->delete();
foreach ($raw as $item) {
if (empty($item['subject']) || !isset($subjects[$item['subject']])) { $skipped++; continue; }
if (empty($item['question_text'])) { $skipped++; continue; }
@@ -154,7 +166,7 @@ class QuestionController extends Controller {
$imported++;
}
});
$msg = "{$imported} Fragen importiert.";
$msg = "{$imported} Fragen importiert (bestehende Fragen der betroffenen Fächer wurden ersetzt).";
if ($skipped) $msg .= " {$skipped} übersprungen (unbekanntes Fach oder fehlender Text).";
return back()->with('success', $msg);
}