Added quiz features
This commit is contained in:
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