From 81828de9a3c7fb1a5f20dd4884e2bbc9e011979d Mon Sep 17 00:00:00 2001 From: mirivlad Date: Mon, 30 Mar 2026 16:06: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=D1=81=20tags-input?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ✅ CourseAssignmentController (index, create, store) ✅ create.blade.php с тремя tags-input (пользователи/группы/организации) ✅ Разные цвета бейджей (зелёный/голубой/синий) ✅ UserSearchController API ✅ index.blade.php список назначений Co-authored-by: Qwen-Coder --- .../Admin/CourseAssignmentController.php | 87 ++++++++++++++++--- .../Controllers/Api/UserSearchController.php | 31 +++++++ .../admin/course-assignments/create.blade.php | 85 +++++++----------- .../admin/course-assignments/index.blade.php | 14 +-- routes/web.php | 2 + 5 files changed, 145 insertions(+), 74 deletions(-) create mode 100755 app/Http/Controllers/Api/UserSearchController.php diff --git a/app/Http/Controllers/Admin/CourseAssignmentController.php b/app/Http/Controllers/Admin/CourseAssignmentController.php index f6ba782..0ac55f7 100755 --- a/app/Http/Controllers/Admin/CourseAssignmentController.php +++ b/app/Http/Controllers/Admin/CourseAssignmentController.php @@ -57,10 +57,10 @@ class CourseAssignmentController extends Controller $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', + 'type' => 'nullable|in:individual,group,organization', + 'user_ids' => 'nullable|string', + 'group_ids' => 'nullable|string', + 'organization_ids' => 'nullable|string', 'start_date' => 'required|date', 'end_date' => 'nullable|date|after:start_date', 'note' => 'nullable|string', @@ -70,10 +70,76 @@ class CourseAssignmentController extends Controller $validated['created_by'] = auth()->id(); $validated['is_active'] = $request->boolean('is_active'); - CourseAssignment::create($validated); + // Определяем тип назначения по выбранным элементам + if (empty($validated['type'])) { + if (!empty($validated['user_ids'])) { + $validated['type'] = 'individual'; + } elseif (!empty($validated['group_ids'])) { + $validated['type'] = 'group'; + } elseif (!empty($validated['organization_ids'])) { + $validated['type'] = 'organization'; + } + } + + // Создаём назначения для каждого выбранного элемента + $created = 0; + + // Назначения пользователям + if (!empty($validated['user_ids'])) { + $userIds = array_map('intval', array_filter(explode(',', $validated['user_ids']))); + foreach ($userIds as $userId) { + CourseAssignment::create([ + 'course_id' => $validated['course_id'], + 'user_id' => $userId, + 'type' => 'individual', + 'start_date' => $validated['start_date'], + 'end_date' => $validated['end_date'] ?? null, + 'note' => $validated['note'] ?? null, + 'created_by' => $validated['created_by'], + 'is_active' => $validated['is_active'], + ]); + $created++; + } + } + + // Назначения группам + if (!empty($validated['group_ids'])) { + $groupIds = array_map('intval', array_filter(explode(',', $validated['group_ids']))); + foreach ($groupIds as $groupId) { + CourseAssignment::create([ + 'course_id' => $validated['course_id'], + 'group_id' => $groupId, + 'type' => 'group', + 'start_date' => $validated['start_date'], + 'end_date' => $validated['end_date'] ?? null, + 'note' => $validated['note'] ?? null, + 'created_by' => $validated['created_by'], + 'is_active' => $validated['is_active'], + ]); + $created++; + } + } + + // Назначения организациям + if (!empty($validated['organization_ids'])) { + $organizationIds = array_map('intval', array_filter(explode(',', $validated['organization_ids']))); + foreach ($organizationIds as $organizationId) { + CourseAssignment::create([ + 'course_id' => $validated['course_id'], + 'organization_id' => $organizationId, + 'type' => 'organization', + 'start_date' => $validated['start_date'], + 'end_date' => $validated['end_date'] ?? null, + 'note' => $validated['note'] ?? null, + 'created_by' => $validated['created_by'], + 'is_active' => $validated['is_active'], + ]); + $created++; + } + } return redirect()->route('admin.course-assignments.index') - ->with('success', 'Назначение успешно создано.'); + ->with('success', "Создано назначений: {$created}"); } public function show(CourseAssignment $assignment) @@ -90,11 +156,8 @@ class CourseAssignmentController extends Controller 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')); + return view('admin.course-assignments.edit', compact('assignment', 'courses')); } public function update(Request $request, CourseAssignment $assignment) @@ -103,10 +166,6 @@ class CourseAssignmentController extends Controller $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', diff --git a/app/Http/Controllers/Api/UserSearchController.php b/app/Http/Controllers/Api/UserSearchController.php new file mode 100755 index 0000000..1788355 --- /dev/null +++ b/app/Http/Controllers/Api/UserSearchController.php @@ -0,0 +1,31 @@ +get('q', ''); + + $users = User::query() + ->with('organization') + ->where('name', 'like', "%{$query}%") + ->orWhere('email', 'like', "%{$query}%") + ->orderBy('name') + ->limit(50) + ->get() + ->map(function($user) { + return [ + 'id' => $user->id, + 'text' => $user->name . ($user->organization ? " ({$user->organization->name})" : ''), + ]; + }); + + return response()->json($users); + } +} diff --git a/resources/views/admin/course-assignments/create.blade.php b/resources/views/admin/course-assignments/create.blade.php index 2709379..e344dbd 100644 --- a/resources/views/admin/course-assignments/create.blade.php +++ b/resources/views/admin/course-assignments/create.blade.php @@ -13,8 +13,9 @@
@csrf
-
-
+
+
+
Основная информация
@@ -28,44 +29,36 @@
- - - @error('type')
{{ $message }}
@enderror + + + Зелёные бейджи — индивидуальные назначения
-