fix: Import ersetzt bestehende Fragen der betroffenen Fächer
This commit is contained in:
@@ -129,9 +129,21 @@ class QuestionController extends Controller {
|
|||||||
return back()->with('error', 'Ungültiges JSON-Format.');
|
return back()->with('error', 'Ungültiges JSON-Format.');
|
||||||
}
|
}
|
||||||
$subjects = Subject::pluck('id','slug');
|
$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;
|
$imported = 0;
|
||||||
$skipped = 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) {
|
foreach ($raw as $item) {
|
||||||
if (empty($item['subject']) || !isset($subjects[$item['subject']])) { $skipped++; continue; }
|
if (empty($item['subject']) || !isset($subjects[$item['subject']])) { $skipped++; continue; }
|
||||||
if (empty($item['question_text'])) { $skipped++; continue; }
|
if (empty($item['question_text'])) { $skipped++; continue; }
|
||||||
@@ -154,7 +166,7 @@ class QuestionController extends Controller {
|
|||||||
$imported++;
|
$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).";
|
if ($skipped) $msg .= " {$skipped} übersprungen (unbekanntes Fach oder fehlender Text).";
|
||||||
return back()->with('success', $msg);
|
return back()->with('success', $msg);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user