Fix: QuestionController не был обновлён - перезаписан полный контроллер
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
parent
d73fbf3050
commit
1ac09e6889
|
|
@ -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', 'Вопрос успешно удалён.');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue