Added quiz features
This commit is contained in:
109
app/Http/Controllers/Admin/QuizController.php
Normal file
109
app/Http/Controllers/Admin/QuizController.php
Normal file
@@ -0,0 +1,109 @@
|
||||
<?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)
|
||||
{
|
||||
$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',
|
||||
'has_negative_score' => 'boolean',
|
||||
'is_free' => 'boolean',
|
||||
'is_published' => 'boolean',
|
||||
'description' => 'nullable|string',
|
||||
]);
|
||||
|
||||
$validated['slug'] = 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');
|
||||
|
||||
$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', 'سوال حذف شد.');
|
||||
}
|
||||
}
|
||||
46
app/Models/Question.php
Normal file
46
app/Models/Question.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Question extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'quiz_id',
|
||||
'subject_id',
|
||||
'question_text',
|
||||
'type',
|
||||
'score',
|
||||
'solution_explanation',
|
||||
'order',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'score' => 'float',
|
||||
'order' => 'integer',
|
||||
];
|
||||
|
||||
public function quiz()
|
||||
{
|
||||
return $this->belongsTo(Quiz::class, 'quiz_id');
|
||||
}
|
||||
|
||||
public function subject()
|
||||
{
|
||||
return $this->belongsTo(QuizSubject::class, 'subject_id');
|
||||
}
|
||||
|
||||
public function options()
|
||||
{
|
||||
return $this->hasMany(QuestionOption::class, 'question_id');
|
||||
}
|
||||
|
||||
public function userAnswers()
|
||||
{
|
||||
return $this->hasMany(UserQuizAnswer::class, 'question_id');
|
||||
}
|
||||
}
|
||||
26
app/Models/QuestionOption.php
Normal file
26
app/Models/QuestionOption.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class QuestionOption extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'question_id',
|
||||
'option_text',
|
||||
'is_correct',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'is_correct' => 'boolean',
|
||||
];
|
||||
|
||||
public function question()
|
||||
{
|
||||
return $this->belongsTo(Question::class, 'question_id');
|
||||
}
|
||||
}
|
||||
47
app/Models/Quiz.php
Normal file
47
app/Models/Quiz.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Quiz extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'category_id',
|
||||
'title',
|
||||
'slug',
|
||||
'year',
|
||||
'type',
|
||||
'duration_minutes',
|
||||
'has_negative_score',
|
||||
'is_free',
|
||||
'is_published',
|
||||
'description',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'year' => 'integer',
|
||||
'duration_minutes' => 'integer',
|
||||
'has_negative_score' => 'boolean',
|
||||
'is_free' => 'boolean',
|
||||
'is_published' => 'boolean',
|
||||
];
|
||||
|
||||
public function category()
|
||||
{
|
||||
return $this->belongsTo(QuizCategory::class, 'category_id');
|
||||
}
|
||||
|
||||
public function questions()
|
||||
{
|
||||
return $this->hasMany(Question::class, 'quiz_id')->orderBy('order');
|
||||
}
|
||||
|
||||
public function attempts()
|
||||
{
|
||||
return $this->hasMany(UserQuizAttempt::class, 'quiz_id');
|
||||
}
|
||||
}
|
||||
27
app/Models/QuizCategory.php
Normal file
27
app/Models/QuizCategory.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class QuizCategory extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'title',
|
||||
'slug',
|
||||
'description',
|
||||
'is_active',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'is_active' => 'boolean',
|
||||
];
|
||||
|
||||
public function quizzes()
|
||||
{
|
||||
return $this->hasMany(Quiz::class, 'category_id');
|
||||
}
|
||||
}
|
||||
21
app/Models/QuizSubject.php
Normal file
21
app/Models/QuizSubject.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class QuizSubject extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'title',
|
||||
'slug',
|
||||
];
|
||||
|
||||
public function questions()
|
||||
{
|
||||
return $this->hasMany(Question::class, 'subject_id');
|
||||
}
|
||||
}
|
||||
41
app/Models/UserQuizAnswer.php
Normal file
41
app/Models/UserQuizAnswer.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class UserQuizAnswer extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'attempt_id',
|
||||
'question_id',
|
||||
'selected_option_id',
|
||||
'text_answer',
|
||||
'is_correct',
|
||||
'earned_score',
|
||||
'reviewer_feedback',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'is_correct' => 'boolean',
|
||||
'earned_score' => 'float',
|
||||
];
|
||||
|
||||
public function attempt()
|
||||
{
|
||||
return $this->belongsTo(UserQuizAttempt::class, 'attempt_id');
|
||||
}
|
||||
|
||||
public function question()
|
||||
{
|
||||
return $this->belongsTo(Question::class, 'question_id');
|
||||
}
|
||||
|
||||
public function selectedOption()
|
||||
{
|
||||
return $this->belongsTo(QuestionOption::class, 'selected_option_id');
|
||||
}
|
||||
}
|
||||
47
app/Models/UserQuizAttempt.php
Normal file
47
app/Models/UserQuizAttempt.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class UserQuizAttempt extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'quiz_id',
|
||||
'started_at',
|
||||
'completed_at',
|
||||
'total_score',
|
||||
'correct_answers',
|
||||
'wrong_answers',
|
||||
'unanswered',
|
||||
'status',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'started_at' => 'datetime',
|
||||
'completed_at' => 'datetime',
|
||||
'total_score' => 'float',
|
||||
'correct_answers' => 'integer',
|
||||
'wrong_answers' => 'integer',
|
||||
'unanswered' => 'integer',
|
||||
];
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'user_id');
|
||||
}
|
||||
|
||||
public function quiz()
|
||||
{
|
||||
return $this->belongsTo(Quiz::class, 'quiz_id');
|
||||
}
|
||||
|
||||
public function answers()
|
||||
{
|
||||
return $this->hasMany(UserQuizAnswer::class, 'attempt_id');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user