Files
LMS/app/Http/Controllers/Student/TestController.php
T
mirivladandQwen-Coder dcb89380aa Feat: Учебная часть для студентов
✅ Login Modal в навигации
✅ Навигация: Админка (для админов), Мои курсы, Тесты
✅ StudentCourseController - список и просмотр курсов
✅ StudentTestController - список и просмотр тестов
✅ Маршруты /student/courses, /student/tests
✅ Views для курсов и тестов

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
2026-04-02 11:47:46 +08:00

72 lines
2.4 KiB
PHP
Executable File

<?php
namespace App\Http\Controllers\Student;
use App\Http\Controllers\Controller;
use App\Models\Test;
use App\Models\CourseAssignment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class TestController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$user = Auth::user();
// Получаем тесты из доступных курсов
$courseIds = CourseAssignment::where('is_active', true)
->where(function($q) use ($user) {
$q->where('type', 'individual')->where('user_id', $user->id)
->orWhere(function($sub) use ($user) {
$sub->where('type', 'group')
->whereIn('group_id', $user->groups->pluck('id'));
})
->orWhere(function($sub) use ($user) {
$sub->where('type', 'organization')
->where('organization_id', $user->organization_id);
});
})->pluck('course_id');
$tests = Test::with(['course'])
->whereIn('course_id', $courseIds)
->where('is_active', true)
->get();
return view('student.tests.index', compact('tests'));
}
public function show(Test $test)
{
$user = Auth::user();
// Проверяем доступ к тесту через курс
$hasAccess = CourseAssignment::where('course_id', $test->course_id)
->where('is_active', true)
->where(function($q) use ($user) {
$q->where('type', 'individual')->where('user_id', $user->id)
->orWhere(function($sub) use ($user) {
$sub->where('type', 'group')
->whereIn('group_id', $user->groups->pluck('id'));
})
->orWhere(function($sub) use ($user) {
$sub->where('type', 'organization')
->where('organization_id', $user->organization_id);
});
})->exists();
if (!$hasAccess) {
abort(403, 'У вас нет доступа к этому тесту');
}
$test->load(['questions.answers', 'course']);
return view('student.tests.show', compact('test'));
}
}