Feat: CRUD тестов (начало)
✅ TestController (resource controller) ✅ Маршруты: /admin/courses/{course}/tests ✅ Blade-шаблоны: index, create ✅ Интеграция в show курса Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
parent
68439932a8
commit
32c9df5453
|
|
@ -0,0 +1,111 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Course;
|
||||
use App\Models\Test;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
|
||||
class TestController extends Controller
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
}
|
||||
|
||||
public function index(Course $course)
|
||||
{
|
||||
Gate::authorize('viewAny', Test::class);
|
||||
|
||||
$tests = $course->tests()->withCount('questions')->orderBy('created_at', 'desc')->get();
|
||||
|
||||
return view('admin.tests.index', compact('course', 'tests'));
|
||||
}
|
||||
|
||||
public function create(Course $course)
|
||||
{
|
||||
Gate::authorize('create', Test::class);
|
||||
|
||||
return view('admin.tests.create', compact('course'));
|
||||
}
|
||||
|
||||
public function store(Request $request, Course $course)
|
||||
{
|
||||
Gate::authorize('create', Test::class);
|
||||
|
||||
$validated = $request->validate([
|
||||
'title' => 'required|string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
'type' => 'required|in:probationary,final,intermediate',
|
||||
'time_limit_minutes' => 'nullable|integer|min:1',
|
||||
'passing_score' => 'nullable|integer|min:0|max:100',
|
||||
'max_attempts' => 'nullable|integer|min:1',
|
||||
'shuffle_questions' => 'boolean',
|
||||
'show_correct_answers' => 'boolean',
|
||||
'is_active' => 'boolean',
|
||||
]);
|
||||
|
||||
$validated['is_active'] = $request->boolean('is_active');
|
||||
$validated['shuffle_questions'] = $request->boolean('shuffle_questions');
|
||||
$validated['show_correct_answers'] = $request->boolean('show_correct_answers');
|
||||
|
||||
$course->tests()->create($validated);
|
||||
|
||||
return redirect()->route('admin.courses.tests.index', $course)
|
||||
->with('success', 'Тест успешно создан.');
|
||||
}
|
||||
|
||||
public function show(Course $course, Test $test)
|
||||
{
|
||||
Gate::authorize('view', $test);
|
||||
|
||||
$test->load(['questions.answers', 'questions.matchingPairs']);
|
||||
|
||||
return view('admin.tests.show', compact('course', 'test'));
|
||||
}
|
||||
|
||||
public function edit(Course $course, Test $test)
|
||||
{
|
||||
Gate::authorize('update', $test);
|
||||
|
||||
return view('admin.tests.edit', compact('course', 'test'));
|
||||
}
|
||||
|
||||
public function update(Request $request, Course $course, Test $test)
|
||||
{
|
||||
Gate::authorize('update', $test);
|
||||
|
||||
$validated = $request->validate([
|
||||
'title' => 'required|string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
'type' => 'required|in:probationary,final,intermediate',
|
||||
'time_limit_minutes' => 'nullable|integer|min:1',
|
||||
'passing_score' => 'nullable|integer|min:0|max:100',
|
||||
'max_attempts' => 'nullable|integer|min:1',
|
||||
'shuffle_questions' => 'boolean',
|
||||
'show_correct_answers' => 'boolean',
|
||||
'is_active' => 'boolean',
|
||||
]);
|
||||
|
||||
$validated['is_active'] = $request->boolean('is_active');
|
||||
$validated['shuffle_questions'] = $request->boolean('shuffle_questions');
|
||||
$validated['show_correct_answers'] = $request->boolean('show_correct_answers');
|
||||
|
||||
$test->update($validated);
|
||||
|
||||
return redirect()->route('admin.courses.tests.show', [$course, $test])
|
||||
->with('success', 'Тест успешно обновлён.');
|
||||
}
|
||||
|
||||
public function destroy(Course $course, Test $test)
|
||||
{
|
||||
Gate::authorize('delete', $test);
|
||||
|
||||
$test->delete();
|
||||
|
||||
return redirect()->route('admin.courses.tests.index', $course)
|
||||
->with('success', 'Тест успешно удалён.');
|
||||
}
|
||||
}
|
||||
|
|
@ -62,7 +62,10 @@
|
|||
</div>
|
||||
<div class="col-md-6 mb-4">
|
||||
<div class="card shadow-sm">
|
||||
<div class="card-header d-flex justify-content-between"><h5 class="mb-0">Тесты</h5><a href="#" class="btn btn-sm btn-primary"><i class="bi bi-plus"></i></a></div>
|
||||
<div class="card-header d-flex justify-content-between">
|
||||
<h5 class="mb-0">Тесты</h5>
|
||||
<a href="{{ route('admin.tests.create', $course) }}" class="btn btn-sm btn-primary"><i class="bi bi-plus"></i></a>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
@if($course->tests->count() > 0)<ul class="list-group list-group-flush">@foreach($course->tests as $test)<li class="list-group-item d-flex justify-content-between">{{ $test->title }}<span class="badge bg-secondary">{{ $test->type }}</span></li>@endforeach</ul>
|
||||
@else<p class="text-muted mb-0">Нет тестов</p>@endif
|
||||
|
|
|
|||
|
|
@ -0,0 +1,76 @@
|
|||
@extends('layouts.app')
|
||||
@section('title', 'Добавить тест')
|
||||
@section('content')
|
||||
<div class="container-fluid">
|
||||
<div class="row">
|
||||
<nav class="col-md-3 col-lg-2 d-md-block sidebar"><div class="position-sticky pt-3">@include('partials._sidebar')</div></nav>
|
||||
<main class="col-md-9 ms-sm-auto col-lg-10 px-md-4 main-content">
|
||||
<div class="d-flex justify-content-between align-items-center pt-3 pb-2 mb-3 border-bottom">
|
||||
<h1 class="h2">Добавить тест</h1>
|
||||
<a href="{{ route('admin.tests.index', $course) }}" class="btn btn-secondary btn-sm">Назад</a>
|
||||
</div>
|
||||
<form action="{{ route('admin.tests.store', $course) }}" method="POST">
|
||||
@csrf
|
||||
<div class="row">
|
||||
<div class="col-md-8">
|
||||
<div class="card shadow-sm mb-3">
|
||||
<div class="card-body">
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Название *</label>
|
||||
<input type="text" name="title" class="form-control @error('title') is-invalid @enderror" value="{{ old('title') }}" required>
|
||||
@error('title')<div class="invalid-feedback">{{ $message }}</div>@enderror
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Описание</label>
|
||||
<textarea name="description" class="form-control" rows="3">{{ old('description') }}</textarea>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<div class="card shadow-sm mb-3">
|
||||
<div class="card-body">
|
||||
<h5>Настройки</h5>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Тип *</label>
|
||||
<select name="type" class="form-select">
|
||||
<option value="intermediate" {{ old('type') == 'intermediate' ? 'selected' : '' }}>Промежуточный</option>
|
||||
<option value="probationary" {{ old('type') == 'probationary' ? 'selected' : '' }}>Пробный</option>
|
||||
<option value="final" {{ old('type') == 'final' ? 'selected' : '' }}>Итоговый</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Время (мин)</label>
|
||||
<input type="number" name="time_limit_minutes" class="form-control" value="{{ old('time_limit_minutes') }}">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Проходной балл (%)</label>
|
||||
<input type="number" name="passing_score" class="form-control" value="{{ old('passing_score', 70) }}" min="0" max="100">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Попыток</label>
|
||||
<input type="number" name="max_attempts" class="form-control" value="{{ old('max_attempts', 3) }}" min="1">
|
||||
</div>
|
||||
<div class="form-check mb-3">
|
||||
<input type="checkbox" name="shuffle_questions" value="1" class="form-check-input" {{ old('shuffle_questions') ? 'checked' : '' }}>
|
||||
<label class="form-check-label">Перемешать вопросы</label>
|
||||
</div>
|
||||
<div class="form-check mb-3">
|
||||
<input type="checkbox" name="show_correct_answers" value="1" class="form-check-input" {{ old('show_correct_answers', true) ? 'checked' : '' }}>
|
||||
<label class="form-check-label">Показывать правильные ответы</label>
|
||||
</div>
|
||||
<div class="form-check mb-3">
|
||||
<input type="checkbox" name="is_active" value="1" class="form-check-input" {{ old('is_active', true) ? 'checked' : '' }}>
|
||||
<label class="form-check-label">Активен</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Создать тест</button>
|
||||
<a href="{{ route('admin.tests.index', $course) }}" class="btn btn-secondary">Отмена</a>
|
||||
</form>
|
||||
</main>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
@extends('layouts.app')
|
||||
@section('title', 'Тесты курса')
|
||||
@section('content')
|
||||
<div class="container-fluid">
|
||||
<div class="row">
|
||||
<nav class="col-md-3 col-lg-2 d-md-block sidebar"><div class="position-sticky pt-3">@include('partials._sidebar')</div></nav>
|
||||
<main class="col-md-9 ms-sm-auto col-lg-10 px-md-4 main-content">
|
||||
<div class="d-flex justify-content-between align-items-center pt-3 pb-2 mb-3 border-bottom">
|
||||
<h1 class="h2">Тесты: {{ $course->title }}</h1>
|
||||
<a href="{{ route('admin.tests.create', $course) }}" class="btn btn-primary btn-sm"><i class="bi bi-plus-lg"></i> Добавить тест</a>
|
||||
</div>
|
||||
@if(session('success'))<div class="alert alert-success">{{ session('success') }}</div>@endif
|
||||
|
||||
<div class="mb-3"><a href="{{ route('admin.courses.show', $course) }}" class="btn btn-secondary btn-sm"><i class="bi bi-arrow-left"></i> Назад к курсу</a></div>
|
||||
|
||||
<div class="card shadow-sm">
|
||||
<div class="card-body">
|
||||
<div class="row">
|
||||
@forelse($tests as $test)
|
||||
<div class="col-md-6 mb-4">
|
||||
<div class="card h-100 shadow-sm">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title">{{ $test->title }}</h5>
|
||||
<p class="card-text text-muted small">{{ Str::limit($test->description, 80) }}</p>
|
||||
<div class="mb-2">
|
||||
<span class="badge bg-info">{{ $test->type }}</span>
|
||||
<span class="badge bg-secondary">{{ $test->questions_count }} вопросов</span>
|
||||
@if($test->time_limit_minutes)<span class="badge bg-warning">{{ $test->time_limit_minutes }} мин</span>@endif
|
||||
</div>
|
||||
<small class="text-muted">Проходной балл: {{ $test->passing_score }}%</small>
|
||||
</div>
|
||||
<div class="card-footer bg-white">
|
||||
<div class="btn-group btn-group-sm w-100">
|
||||
<a href="{{ route('admin.tests.show', $test) }}" class="btn btn-outline-primary"><i class="bi bi-eye"></i></a>
|
||||
<a href="{{ route('admin.tests.edit', $test) }}" class="btn btn-outline-warning"><i class="bi bi-pencil"></i></a>
|
||||
<form action="{{ route('admin.tests.destroy', $test) }}" method="POST" class="d-inline" onsubmit="return confirm('Удалить?')">@csrf @method('DELETE')<button class="btn btn-outline-danger"><i class="bi bi-trash"></i></button></form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@empty
|
||||
<div class="col-12 text-center text-muted py-5"><i class="bi bi-inbox" style="font-size:3rem;"></i><p class="mt-3">Тестов пока нет</p></div>
|
||||
@endforelse
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
|
|
@ -7,6 +7,8 @@ use App\Http\Controllers\Admin\GroupController;
|
|||
use App\Http\Controllers\Admin\UserController;
|
||||
use App\Http\Controllers\Admin\CourseCategoryController;
|
||||
use App\Http\Controllers\Admin\CourseController;
|
||||
use App\Http\Controllers\Admin\TestController;
|
||||
use App\Http\Controllers\Admin\QuestionController;
|
||||
use App\Http\Controllers\DashboardController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
|
|
@ -42,5 +44,6 @@ Route::middleware('auth')->group(function () {
|
|||
Route::resource('users', UserController::class);
|
||||
Route::resource('course-categories', CourseCategoryController::class);
|
||||
Route::resource('courses', CourseController::class);
|
||||
Route::resource('courses.tests', TestController::class)->shallow();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue