From 1ac09e6889aa430c8ef323e8332c3cba4260ecd3 Mon Sep 17 00:00:00 2001 From: mirivlad Date: Thu, 26 Mar 2026 16:47:31 +0800 Subject: [PATCH] =?UTF-8?q?Fix:=20QuestionController=20=D0=BD=D0=B5=20?= =?UTF-8?q?=D0=B1=D1=8B=D0=BB=20=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D1=91?= =?UTF-8?q?=D0=BD=20-=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B7=D0=B0=D0=BF=D0=B8?= =?UTF-8?q?=D1=81=D0=B0=D0=BD=20=D0=BF=D0=BE=D0=BB=D0=BD=D1=8B=D0=B9=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BD=D1=82=D1=80=D0=BE=D0=BB=D0=BB=D0=B5=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Qwen-Coder --- .../Controllers/Admin/QuestionController.php | 174 ++++++++++++++---- 1 file changed, 139 insertions(+), 35 deletions(-) diff --git a/app/Http/Controllers/Admin/QuestionController.php b/app/Http/Controllers/Admin/QuestionController.php index 9ea3a2c..4747af9 100755 --- a/app/Http/Controllers/Admin/QuestionController.php +++ b/app/Http/Controllers/Admin/QuestionController.php @@ -3,63 +3,167 @@ namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; +use App\Models\Question; +use App\Models\Test; +use App\Models\Answer; +use App\Models\QuestionMatchingPair; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Gate; +use Illuminate\Support\Facades\DB; class QuestionController extends Controller { - /** - * Display a listing of the resource. - */ - public function index() + public function __construct() { - // + $this->middleware('auth'); } - /** - * Show the form for creating a new resource. - */ - public function create() + public function index(Test $test) { - // + Gate::authorize('viewAny', Question::class); + + $questions = $test->questions()->with('answers')->orderBy('sort_order')->get(); + + return view('admin.questions.index', compact('test', 'questions')); } - /** - * Store a newly created resource in storage. - */ - public function store(Request $request) + public function create(Test $test) { - // + Gate::authorize('create', Question::class); + + return view('admin.questions.create', compact('test')); } - /** - * Display the specified resource. - */ - public function show(string $id) + public function store(Request $request, Test $test) { - // + Gate::authorize('create', Question::class); + + $validated = $request->validate([ + 'type' => 'required|in:single_choice,multiple_choice,input,matching', + 'question_text' => 'required|string', + 'explanation' => 'nullable|string', + 'score' => 'nullable|integer|min:1', + 'sort_order' => 'nullable|integer', + 'is_required' => 'boolean', + 'answers' => 'nullable|array', + 'matching_pairs' => 'nullable|array', + ]); + + DB::transaction(function () use ($test, $validated) { + $question = $test->questions()->create([ + 'type' => $validated['type'], + 'question_text' => $validated['question_text'], + 'explanation' => $validated['explanation'] ?? null, + 'score' => $validated['score'] ?? 1, + 'sort_order' => $validated['sort_order'] ?? $test->questions()->count(), + 'is_required' => $validated['is_required'] ?? true, + ]); + + if (in_array($validated['type'], ['single_choice', 'multiple_choice']) && !empty($validated['answers'])) { + foreach ($validated['answers'] as $answer) { + if (!empty($answer['text'])) { + $question->answers()->create([ + 'answer_text' => $answer['text'], + 'is_correct' => $answer['is_correct'] ?? false, + 'sort_order' => $answer['sort_order'] ?? 0, + ]); + } + } + } + + if ($validated['type'] === 'matching' && !empty($validated['matching_pairs'])) { + foreach ($validated['matching_pairs'] as $pair) { + if (!empty($pair['left_text']) && !empty($pair['right_text'])) { + $question->matchingPairs()->create([ + 'left_text' => $pair['left_text'], + 'right_text' => $pair['right_text'], + 'match_score' => $pair['match_score'] ?? 1, + 'sort_order' => $pair['sort_order'] ?? 0, + ]); + } + } + } + }); + + return redirect()->route('admin.tests.questions.index', $test) + ->with('success', 'Вопрос успешно создан.'); } - /** - * Show the form for editing the specified resource. - */ - public function edit(string $id) + public function show(Test $test, Question $question) { - // + Gate::authorize('view', $question); + return view('admin.questions.show', compact('test', 'question')); } - /** - * Update the specified resource in storage. - */ - public function update(Request $request, string $id) + public function edit(Test $test, Question $question) { - // + Gate::authorize('update', $question); + $question->load(['answers', 'matchingPairs']); + return view('admin.questions.edit', compact('test', 'question')); } - /** - * Remove the specified resource from storage. - */ - public function destroy(string $id) + public function update(Request $request, Test $test, Question $question) { - // + Gate::authorize('update', $question); + + $validated = $request->validate([ + 'type' => 'required|in:single_choice,multiple_choice,input,matching', + 'question_text' => 'required|string', + 'explanation' => 'nullable|string', + 'score' => 'nullable|integer|min:1', + 'sort_order' => 'nullable|integer', + 'is_required' => 'boolean', + 'answers' => 'nullable|array', + 'matching_pairs' => 'nullable|array', + ]); + + DB::transaction(function () use ($question, $validated) { + $question->update([ + 'type' => $validated['type'], + 'question_text' => $validated['question_text'], + 'explanation' => $validated['explanation'] ?? null, + 'score' => $validated['score'] ?? 1, + 'sort_order' => $validated['sort_order'] ?? $question->sort_order, + 'is_required' => $validated['is_required'] ?? true, + ]); + + if (in_array($validated['type'], ['single_choice', 'multiple_choice']) && !empty($validated['answers'])) { + $question->answers()->delete(); + foreach ($validated['answers'] as $answer) { + if (!empty($answer['text'])) { + $question->answers()->create([ + 'answer_text' => $answer['text'], + 'is_correct' => $answer['is_correct'] ?? false, + 'sort_order' => $answer['sort_order'] ?? 0, + ]); + } + } + } + + if ($validated['type'] === 'matching' && !empty($validated['matching_pairs'])) { + $question->matchingPairs()->delete(); + foreach ($validated['matching_pairs'] as $pair) { + if (!empty($pair['left_text']) && !empty($pair['right_text'])) { + $question->matchingPairs()->create([ + 'left_text' => $pair['left_text'], + 'right_text' => $pair['right_text'], + 'match_score' => $pair['match_score'] ?? 1, + 'sort_order' => $pair['sort_order'] ?? 0, + ]); + } + } + } + }); + + return redirect()->route('admin.tests.questions.index', $test) + ->with('success', 'Вопрос успешно обновлён.'); + } + + public function destroy(Test $test, Question $question) + { + Gate::authorize('delete', $question); + $question->delete(); + return redirect()->route('admin.tests.questions.index', $test) + ->with('success', 'Вопрос успешно удалён.'); } }