Feat: Учебная часть для студентов

 Login Modal в навигации
 Навигация: Админка (для админов), Мои курсы, Тесты
 StudentCourseController - список и просмотр курсов
 StudentTestController - список и просмотр тестов
 Маршруты /student/courses, /student/tests
 Views для курсов и тестов

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
mirivlad
2026-04-02 11:47:46 +08:00
co-authored by Qwen-Coder
parent b56d8b2b3d
commit dcb89380aa
8 changed files with 470 additions and 4 deletions
+84
View File
@@ -0,0 +1,84 @@
<?php
namespace App\Http\Controllers\Student;
use App\Http\Controllers\Controller;
use App\Models\Course;
use App\Models\CourseAssignment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class CourseController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$user = Auth::user();
// Получаем назначения для пользователя
$query = CourseAssignment::with(['course', 'group', 'organization'])
->where('is_active', true)
->where(function($q) use ($user) {
// Индивидуальные назначения
$q->where('type', 'individual')
->where('user_id', $user->id);
// Или назначения группе пользователя
if ($user->groups->count() > 0) {
$q->orWhere(function($sub) use ($user) {
$sub->where('type', 'group')
->whereIn('group_id', $user->groups->pluck('id'));
});
}
// Или назначения организации пользователя
if ($user->organization_id) {
$q->orWhere(function($sub) use ($user) {
$sub->where('type', 'organization')
->where('organization_id', $user->organization_id);
});
}
});
$assignments = $query->get();
// Группируем по курсам
$courses = $assignments->unique('course_id')->map(function($assignment) {
return $assignment->course;
});
return view('student.courses.index', compact('courses', 'assignments'));
}
public function show(Course $course)
{
$user = Auth::user();
// Проверяем доступ к курсу
$hasAccess = CourseAssignment::where('course_id', $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, 'У вас нет доступа к этому курсу');
}
$course->load(['tests', 'category']);
return view('student.courses.show', compact('course'));
}
}
+71
View File
@@ -0,0 +1,71 @@
<?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'));
}
}