Added quiz features
This commit is contained in:
47
app/Http/Controllers/Admin/QuizCategoryController.php
Normal file
47
app/Http/Controllers/Admin/QuizCategoryController.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\QuizCategory;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class QuizCategoryController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
// Load categories and count how many quizzes are in each
|
||||
$categories = QuizCategory::withCount('quizzes')->latest()->get();
|
||||
return view('admin.quiz-categories.index', compact('categories'));
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'title' => 'required|string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
]);
|
||||
|
||||
QuizCategory::create([
|
||||
'title' => $request->title,
|
||||
// Automatically generate a unique slug for URLs
|
||||
'slug' => Str::slug($request->title) . '-' . time(),
|
||||
'description' => $request->description,
|
||||
'is_active' => $request->has('is_active'),
|
||||
]);
|
||||
|
||||
return back()->with('success', 'دستهبندی با موفقیت ایجاد شد.');
|
||||
}
|
||||
|
||||
public function destroy(QuizCategory $quiz_category)
|
||||
{
|
||||
// Check if the category has quizzes before deleting
|
||||
if ($quiz_category->quizzes()->count() > 0) {
|
||||
return back()->with('error', 'این دستهبندی شامل آزمون است و نمیتواند حذف شود. ابتدا آزمونها را حذف کنید.');
|
||||
}
|
||||
|
||||
$quiz_category->delete();
|
||||
return back()->with('success', 'دستهبندی با موفقیت حذف شد.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user