Fixed quiz
This commit is contained in:
44
app/Http/Controllers/Admin/QuizSubjectController.php
Normal file
44
app/Http/Controllers/Admin/QuizSubjectController.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\QuizSubject;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class QuizSubjectController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
// Load subjects and count how many questions belong to each
|
||||
$subjects = QuizSubject::withCount('questions')->latest()->get();
|
||||
return view('admin.quiz-subjects.index', compact('subjects'));
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'title' => 'required|string|max:255',
|
||||
]);
|
||||
|
||||
QuizSubject::create([
|
||||
'title' => $request->title,
|
||||
// Generate a unique slug for potential API/URL usage
|
||||
'slug' => Str::slug($request->title) . '-' . time(),
|
||||
]);
|
||||
|
||||
return back()->with('success', 'درس با موفقیت اضافه شد.');
|
||||
}
|
||||
|
||||
public function destroy(QuizSubject $quiz_subject)
|
||||
{
|
||||
// Prevent deletion if questions are already assigned to this subject
|
||||
if ($quiz_subject->questions()->count() > 0) {
|
||||
return back()->with('error', 'این درس دارای سوال است و نمیتواند حذف شود. ابتدا سوالات مربوطه را ویرایش یا حذف کنید.');
|
||||
}
|
||||
|
||||
$quiz_subject->delete();
|
||||
return back()->with('success', 'درس با موفقیت حذف شد.');
|
||||
}
|
||||
}
|
||||
@@ -60,6 +60,13 @@
|
||||
</a>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<a href="{{ route('quiz-subjects.index') }}" class="waves-effect">
|
||||
<i class="mdi mdi-book-open-page-variant"></i>
|
||||
<span>مدیریت دروس</span>
|
||||
</a>
|
||||
</li>
|
||||
|
||||
|
||||
<li>
|
||||
<a href="javascript:%20void(0);" class="has-arrow waves-effect">
|
||||
|
||||
77
resources/views/admin/quiz-subjects/index.blade.php
Normal file
77
resources/views/admin/quiz-subjects/index.blade.php
Normal file
@@ -0,0 +1,77 @@
|
||||
@extends('admin.layouts.app')
|
||||
|
||||
@section('content')
|
||||
<div class="row">
|
||||
<!-- Form to Add Subject -->
|
||||
<div class="col-lg-4">
|
||||
<div class="card">
|
||||
<div class="card-header bg-primary text-white">
|
||||
<h6 class="mb-0 text-white">افزودن درس جدید</h6>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
|
||||
@if ($errors->any())
|
||||
<div class="alert alert-danger">
|
||||
<ul class="mb-0">
|
||||
@foreach ($errors->all() as $error)
|
||||
<li>{{ $error }}</li>
|
||||
@endforeach
|
||||
</ul>
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<form action="{{ route('quiz-subjects.store') }}" method="POST">
|
||||
@csrf
|
||||
<div class="mb-3">
|
||||
<label class="form-label">عنوان درس <span class="text-danger">*</span></label>
|
||||
<input type="text" name="title" class="form-control" value="{{ old('title') }}" placeholder="مثال: حقوق مدنی" required>
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn btn-success w-100">ثبت درس</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- List of Subjects -->
|
||||
<div class="col-lg-8">
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title mb-4">دروس ثبت شده</h5>
|
||||
<div class="table-responsive">
|
||||
<table class="table table-hover align-middle">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>عنوان درس</th>
|
||||
<th>تعداد سوالات مرتبط</th>
|
||||
<th>عملیات</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@forelse($subjects as $subject)
|
||||
<tr>
|
||||
<td><strong>{{ $subject->title }}</strong></td>
|
||||
<td>
|
||||
<span class="badge bg-soft-info text-info">{{ $subject->questions_count }} سوال</span>
|
||||
</td>
|
||||
<td>
|
||||
<form action="{{ route('quiz-subjects.destroy', $subject->id) }}" method="POST" class="d-inline" onsubmit="return confirm('آیا از حذف این درس اطمینان دارید؟')">
|
||||
@csrf
|
||||
@method('DELETE')
|
||||
<button class="btn btn-sm btn-outline-danger">حذف</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
@empty
|
||||
<tr>
|
||||
<td colspan="3" class="text-center py-3">هیچ درسی یافت نشد.</td>
|
||||
</tr>
|
||||
@endforelse
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
@@ -33,6 +33,7 @@ use App\Http\Controllers\Admin\TicketController;
|
||||
use App\Http\Controllers\Admin\DashboardController;
|
||||
use App\Http\Controllers\Admin\QuizController;
|
||||
use App\Http\Controllers\Admin\QuizCategoryController;
|
||||
use App\Http\Controllers\Admin\QuizSubjectController;
|
||||
|
||||
Route::get('payment/callback', [PayController::class, 'callback']);
|
||||
|
||||
@@ -78,5 +79,7 @@ Route::middleware(['auth', config('jetstream.auth_session')])->group(function ()
|
||||
Route::delete('questions/{question}', [QuizController::class, 'destroyQuestion'])->name('questions.destroy');
|
||||
Route::resource('quiz-categories', QuizCategoryController::class)->except(['create', 'show', 'edit', 'update']);
|
||||
|
||||
Route::resource('quiz-subjects', QuizSubjectController::class)->except(['create', 'show', 'edit', 'update']);
|
||||
|
||||
Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user