110 lines
3.9 KiB
PHP
110 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Quiz;
|
|
use App\Models\QuizCategory;
|
|
use App\Models\QuizSubject;
|
|
use App\Models\Question;
|
|
use App\Models\QuestionOption;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Str;
|
|
|
|
class QuizController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$quizzes = Quiz::with('category')->withCount('questions')->latest()->paginate(15);
|
|
return view('admin.quizzes.index', compact('quizzes'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$categories = QuizCategory::where('is_active', true)->get();
|
|
return view('admin.quizzes.create', compact('categories'));
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
// 1. Validate only the text and number fields
|
|
$validated = $request->validate([
|
|
'title' => 'required|string|max:255',
|
|
'category_id' => 'required|exists:quiz_categories,id',
|
|
'year' => 'nullable|integer',
|
|
'type' => 'required|in:multiple_choice,descriptive,mixed',
|
|
'duration_minutes' => 'required|integer|min:1',
|
|
'description' => 'nullable|string',
|
|
]);
|
|
|
|
// 2. Safely handle checkboxes and generate slug
|
|
$validated['slug'] = \Illuminate\Support\Str::slug($validated['title']) . '-' . time();
|
|
$validated['has_negative_score'] = $request->has('has_negative_score');
|
|
$validated['is_free'] = $request->has('is_free');
|
|
$validated['is_published'] = $request->has('is_published');
|
|
|
|
// 3. Create the quiz
|
|
$quiz = Quiz::create($validated);
|
|
|
|
return redirect()->route('quizzes.questions.index', $quiz->id)
|
|
->with('success', 'آزمون با موفقیت ایجاد شد. اکنون میتوانید سوالات را اضافه کنید.');
|
|
}
|
|
|
|
public function destroy(Quiz $quiz)
|
|
{
|
|
$quiz->delete();
|
|
return redirect()->route('quizzes.index')->with('success', 'آزمون حذف شد.');
|
|
}
|
|
|
|
// --- Questions Management ---
|
|
|
|
public function questions(Quiz $quiz)
|
|
{
|
|
$quiz->load(['questions.options', 'questions.subject']);
|
|
$subjects = QuizSubject::all();
|
|
return view('admin.quizzes.questions', compact('quiz', 'subjects'));
|
|
}
|
|
|
|
public function storeQuestion(Request $request, Quiz $quiz)
|
|
{
|
|
$request->validate([
|
|
'question_text' => 'required|string',
|
|
'type' => 'required|in:multiple_choice,descriptive',
|
|
'subject_id' => 'nullable|exists:quiz_subjects,id',
|
|
'score' => 'required|numeric|min:0.5',
|
|
'solution_explanation' => 'nullable|string',
|
|
// Options validation for multiple-choice questions
|
|
'options' => 'required_if:type,multiple_choice|array|min:4|max:4',
|
|
'options.*' => 'required_if:type,multiple_choice|string',
|
|
'correct_option' => 'required_if:type,multiple_choice|integer|between:0,3',
|
|
]);
|
|
|
|
$question = $quiz->questions()->create([
|
|
'subject_id' => $request->subject_id,
|
|
'question_text' => $request->question_text,
|
|
'type' => $request->type,
|
|
'score' => $request->score,
|
|
'solution_explanation' => $request->solution_explanation,
|
|
'order' => $quiz->questions()->count() + 1,
|
|
]);
|
|
|
|
// Save options if question is multiple choice
|
|
if ($request->type === 'multiple_choice' && $request->has('options')) {
|
|
foreach ($request->options as $index => $optionText) {
|
|
$question->options()->create([
|
|
'option_text' => $optionText,
|
|
'is_correct' => ($index == $request->correct_option),
|
|
]);
|
|
}
|
|
}
|
|
|
|
return back()->with('success', 'سوال با موفقیت ثبت شد.');
|
|
}
|
|
|
|
public function destroyQuestion(Question $question)
|
|
{
|
|
$question->delete();
|
|
return back()->with('success', 'سوال حذف شد.');
|
|
}
|
|
}
|