From 0fd33076ab51ead2df147129a9185da84a70ffb3 Mon Sep 17 00:00:00 2001 From: mirivlad Date: Mon, 30 Mar 2026 08:47:02 +0800 Subject: [PATCH] =?UTF-8?q?Feat:=20=D0=9D=D0=B0=D0=B7=D0=BD=D0=B0=D1=87?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BA=D1=83=D1=80=D1=81=D0=BE=D0=B2?= =?UTF-8?q?=20=E2=80=94=20Controller=20+=20Policy=20+=20Routes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ✅ CourseAssignmentController (resource) ✅ CourseAssignmentPolicy ✅ Маршруты: /admin/course-assignments ✅ Регистрация в AuthServiceProvider Co-authored-by: Qwen-Coder --- .../Admin/CourseAssignmentController.php | 131 ++++++++++++++++++ app/Policies/CourseAssignmentPolicy.php | 34 +++++ app/Providers/AuthServiceProvider.php | 1 + routes/web.php | 2 + 4 files changed, 168 insertions(+) create mode 100755 app/Http/Controllers/Admin/CourseAssignmentController.php create mode 100755 app/Policies/CourseAssignmentPolicy.php diff --git a/app/Http/Controllers/Admin/CourseAssignmentController.php b/app/Http/Controllers/Admin/CourseAssignmentController.php new file mode 100755 index 0000000..f6ba782 --- /dev/null +++ b/app/Http/Controllers/Admin/CourseAssignmentController.php @@ -0,0 +1,131 @@ +middleware('auth'); + } + + public function index(Request $request) + { + Gate::authorize('viewAny', CourseAssignment::class); + + $query = CourseAssignment::with(['course', 'user', 'group', 'organization', 'creator']); + + if ($request->filled('course_id')) { + $query->where('course_id', $request->course_id); + } + + if ($request->filled('type')) { + $query->where('type', $request->type); + } + + $assignments = $query->orderBy('created_at', 'desc')->paginate(20); + $courses = Course::pluck('title', 'id'); + + return view('admin.course-assignments.index', compact('assignments', 'courses')); + } + + public function create() + { + Gate::authorize('create', CourseAssignment::class); + + $courses = Course::pluck('title', 'id'); + $users = User::pluck('name', 'id'); + $groups = Group::pluck('name', 'id'); + $organizations = Organization::pluck('name', 'id'); + + return view('admin.course-assignments.create', compact('courses', 'users', 'groups', 'organizations')); + } + + public function store(Request $request) + { + Gate::authorize('create', CourseAssignment::class); + + $validated = $request->validate([ + 'course_id' => 'required|exists:courses,id', + 'type' => 'required|in:individual,group,organization', + 'user_id' => 'nullable|exists:users,id', + 'group_id' => 'nullable|exists:groups,id', + 'organization_id' => 'nullable|exists:organizations,id', + 'start_date' => 'required|date', + 'end_date' => 'nullable|date|after:start_date', + 'note' => 'nullable|string', + 'is_active' => 'boolean', + ]); + + $validated['created_by'] = auth()->id(); + $validated['is_active'] = $request->boolean('is_active'); + + CourseAssignment::create($validated); + + return redirect()->route('admin.course-assignments.index') + ->with('success', 'Назначение успешно создано.'); + } + + public function show(CourseAssignment $assignment) + { + Gate::authorize('view', $assignment); + + $assignment->load(['course', 'user', 'group', 'organization', 'creator']); + + return view('admin.course-assignments.show', compact('assignment')); + } + + public function edit(CourseAssignment $assignment) + { + Gate::authorize('update', $assignment); + + $courses = Course::pluck('title', 'id'); + $users = User::pluck('name', 'id'); + $groups = Group::pluck('name', 'id'); + $organizations = Organization::pluck('name', 'id'); + + return view('admin.course-assignments.edit', compact('assignment', 'courses', 'users', 'groups', 'organizations')); + } + + public function update(Request $request, CourseAssignment $assignment) + { + Gate::authorize('update', $assignment); + + $validated = $request->validate([ + 'course_id' => 'required|exists:courses,id', + 'type' => 'required|in:individual,group,organization', + 'user_id' => 'nullable|exists:users,id', + 'group_id' => 'nullable|exists:groups,id', + 'organization_id' => 'nullable|exists:organizations,id', + 'start_date' => 'required|date', + 'end_date' => 'nullable|date|after:start_date', + 'note' => 'nullable|string', + 'is_active' => 'boolean', + ]); + + $assignment->update($validated); + + return redirect()->route('admin.course-assignments.show', $assignment) + ->with('success', 'Назначение успешно обновлено.'); + } + + public function destroy(CourseAssignment $assignment) + { + Gate::authorize('delete', $assignment); + + $assignment->delete(); + + return redirect()->route('admin.course-assignments.index') + ->with('success', 'Назначение успешно удалено.'); + } +} diff --git a/app/Policies/CourseAssignmentPolicy.php b/app/Policies/CourseAssignmentPolicy.php new file mode 100755 index 0000000..316bde3 --- /dev/null +++ b/app/Policies/CourseAssignmentPolicy.php @@ -0,0 +1,34 @@ +hasRole(['Administrator', 'Manager', 'Curator']); + } + + public function view(User $user, CourseAssignment $assignment): bool + { + return $user->hasRole(['Administrator', 'Manager', 'Curator']); + } + + public function create(User $user): bool + { + return $user->hasRole(['Administrator', 'Manager']); + } + + public function update(User $user, CourseAssignment $assignment): bool + { + return $user->hasRole(['Administrator', 'Manager']); + } + + public function delete(User $user, CourseAssignment $assignment): bool + { + return $user->hasRole(['Administrator', 'Manager']); + } +} diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index d83758b..ee17e3d 100755 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -22,6 +22,7 @@ class AuthServiceProvider extends ServiceProvider protected $policies = [ CourseCategory::class => CourseCategoryPolicy::class, Course::class => CoursePolicy::class, + CourseAssignment::class => CourseAssignmentPolicy::class, Test::class => TestPolicy::class, Question::class => QuestionPolicy::class, Organization::class => OrganizationPolicy::class, diff --git a/routes/web.php b/routes/web.php index 9d4ade7..3b52a59 100644 --- a/routes/web.php +++ b/routes/web.php @@ -9,6 +9,7 @@ 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\Admin\CourseAssignmentController; use App\Http\Controllers\DashboardController; use Illuminate\Support\Facades\Route; @@ -46,5 +47,6 @@ Route::middleware('auth')->group(function () { Route::resource('courses', CourseController::class); Route::resource('courses.tests', TestController::class); Route::resource('tests.questions', QuestionController::class); + Route::resource('course-assignments', CourseAssignmentController::class); }); });