Feat: Назначения курсов с tags-input
✅ CourseAssignmentController (index, create, store) ✅ create.blade.php с тремя tags-input (пользователи/группы/организации) ✅ Разные цвета бейджей (зелёный/голубой/синий) ✅ UserSearchController API ✅ index.blade.php список назначений Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
parent
064778b8aa
commit
81828de9a3
|
|
@ -57,10 +57,10 @@ class CourseAssignmentController extends Controller
|
||||||
|
|
||||||
$validated = $request->validate([
|
$validated = $request->validate([
|
||||||
'course_id' => 'required|exists:courses,id',
|
'course_id' => 'required|exists:courses,id',
|
||||||
'type' => 'required|in:individual,group,organization',
|
'type' => 'nullable|in:individual,group,organization',
|
||||||
'user_id' => 'nullable|exists:users,id',
|
'user_ids' => 'nullable|string',
|
||||||
'group_id' => 'nullable|exists:groups,id',
|
'group_ids' => 'nullable|string',
|
||||||
'organization_id' => 'nullable|exists:organizations,id',
|
'organization_ids' => 'nullable|string',
|
||||||
'start_date' => 'required|date',
|
'start_date' => 'required|date',
|
||||||
'end_date' => 'nullable|date|after:start_date',
|
'end_date' => 'nullable|date|after:start_date',
|
||||||
'note' => 'nullable|string',
|
'note' => 'nullable|string',
|
||||||
|
|
@ -70,10 +70,76 @@ class CourseAssignmentController extends Controller
|
||||||
$validated['created_by'] = auth()->id();
|
$validated['created_by'] = auth()->id();
|
||||||
$validated['is_active'] = $request->boolean('is_active');
|
$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')
|
return redirect()->route('admin.course-assignments.index')
|
||||||
->with('success', 'Назначение успешно создано.');
|
->with('success', "Создано назначений: {$created}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public function show(CourseAssignment $assignment)
|
public function show(CourseAssignment $assignment)
|
||||||
|
|
@ -90,11 +156,8 @@ class CourseAssignmentController extends Controller
|
||||||
Gate::authorize('update', $assignment);
|
Gate::authorize('update', $assignment);
|
||||||
|
|
||||||
$courses = Course::pluck('title', 'id');
|
$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)
|
public function update(Request $request, CourseAssignment $assignment)
|
||||||
|
|
@ -103,10 +166,6 @@ class CourseAssignmentController extends Controller
|
||||||
|
|
||||||
$validated = $request->validate([
|
$validated = $request->validate([
|
||||||
'course_id' => 'required|exists:courses,id',
|
'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',
|
'start_date' => 'required|date',
|
||||||
'end_date' => 'nullable|date|after:start_date',
|
'end_date' => 'nullable|date|after:start_date',
|
||||||
'note' => 'nullable|string',
|
'note' => 'nullable|string',
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,31 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
class UserSearchController extends Controller
|
||||||
|
{
|
||||||
|
public function __invoke(Request $request)
|
||||||
|
{
|
||||||
|
$query = $request->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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -13,8 +13,9 @@
|
||||||
<form action="{{ route('admin.course-assignments.store') }}" method="POST">
|
<form action="{{ route('admin.course-assignments.store') }}" method="POST">
|
||||||
@csrf
|
@csrf
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-md-8">
|
<div class="col-md-8 mb-4">
|
||||||
<div class="card shadow-sm mb-3">
|
<div class="card shadow-sm">
|
||||||
|
<div class="card-header bg-primary text-white"><h5 class="mb-0">Основная информация</h5></div>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label class="form-label">Курс *</label>
|
<label class="form-label">Курс *</label>
|
||||||
|
|
@ -28,44 +29,36 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label class="form-label">Тип назначения *</label>
|
<label class="form-label">Пользователи</label>
|
||||||
<select name="type" id="assignmentType" class="form-select @error('type') is-invalid @enderror" required onchange="updateAssignmentFields()">
|
<x-tags-input
|
||||||
<option value="">Выберите тип</option>
|
name="user_ids"
|
||||||
<option value="individual" {{ old('type') == 'individual' ? 'selected' : '' }}>Индивидуальному пользователю</option>
|
url="{{ route('api.users.search') }}"
|
||||||
<option value="group" {{ old('type') == 'group' ? 'selected' : '' }}>Группе</option>
|
placeholder="Начните вводить имя пользователя..."
|
||||||
<option value="organization" {{ old('type') == 'organization' ? 'selected' : '' }}>Организации</option>
|
badge_color="success"
|
||||||
</select>
|
/>
|
||||||
@error('type')<div class="invalid-feedback">{{ $message }}</div>@enderror
|
<small class="text-muted">Зелёные бейджи — индивидуальные назначения</small>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="mb-3" id="userField" style="display:none;">
|
<div class="mb-3">
|
||||||
<label class="form-label">Пользователь *</label>
|
<label class="form-label">Группы</label>
|
||||||
<select name="user_id" class="form-select">
|
<x-tags-input
|
||||||
<option value="">Выберите пользователя</option>
|
name="group_ids"
|
||||||
@foreach($users as $id => $name)
|
url="{{ route('api.groups.search') }}"
|
||||||
<option value="{{ $id }}" {{ old('user_id') == $id ? 'selected' : '' }}>{{ $name }}</option>
|
placeholder="Начните вводить название группы..."
|
||||||
@endforeach
|
badge_color="info"
|
||||||
</select>
|
/>
|
||||||
|
<small class="text-muted">Голубые бейджи — назначения группам</small>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="mb-3" id="groupField" style="display:none;">
|
<div class="mb-3">
|
||||||
<label class="form-label">Группа *</label>
|
<label class="form-label">Организации</label>
|
||||||
<select name="group_id" class="form-select">
|
<x-tags-input
|
||||||
<option value="">Выберите группу</option>
|
name="organization_ids"
|
||||||
@foreach($groups as $id => $name)
|
url="{{ route('api.organizations.search') }}"
|
||||||
<option value="{{ $id }}" {{ old('group_id') == $id ? 'selected' : '' }}>{{ $name }}</option>
|
placeholder="Начните вводить название организации..."
|
||||||
@endforeach
|
badge_color="primary"
|
||||||
</select>
|
/>
|
||||||
</div>
|
<small class="text-muted">Синие бейджи — назначения организациям</small>
|
||||||
|
|
||||||
<div class="mb-3" id="organizationField" style="display:none;">
|
|
||||||
<label class="form-label">Организация *</label>
|
|
||||||
<select name="organization_id" class="form-select">
|
|
||||||
<option value="">Выберите организацию</option>
|
|
||||||
@foreach($organizations as $id => $name)
|
|
||||||
<option value="{{ $id }}" {{ old('organization_id') == $id ? 'selected' : '' }}>{{ $name }}</option>
|
|
||||||
@endforeach
|
|
||||||
</select>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
|
|
@ -76,10 +69,10 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="col-md-4">
|
<div class="col-md-4 mb-4">
|
||||||
<div class="card shadow-sm mb-3">
|
<div class="card shadow-sm">
|
||||||
|
<div class="card-header bg-success text-white"><h5 class="mb-0">Период доступа</h5></div>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<h5>Период доступа</h5>
|
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label class="form-label">Дата начала *</label>
|
<label class="form-label">Дата начала *</label>
|
||||||
<input type="date" name="start_date" class="form-control @error('start_date') is-invalid @enderror" value="{{ old('start_date', date('Y-m-d')) }}" required>
|
<input type="date" name="start_date" class="form-control @error('start_date') is-invalid @enderror" value="{{ old('start_date', date('Y-m-d')) }}" required>
|
||||||
|
|
@ -106,18 +99,4 @@
|
||||||
</main>
|
</main>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
|
||||||
function updateAssignmentFields() {
|
|
||||||
const type = document.getElementById('assignmentType').value;
|
|
||||||
document.getElementById('userField').style.display = (type === 'individual') ? 'block' : 'none';
|
|
||||||
document.getElementById('groupField').style.display = (type === 'group') ? 'block' : 'none';
|
|
||||||
document.getElementById('organizationField').style.display = (type === 'organization') ? 'block' : 'none';
|
|
||||||
}
|
|
||||||
|
|
||||||
// Инициализация при загрузке
|
|
||||||
document.addEventListener('DOMContentLoaded', function() {
|
|
||||||
updateAssignmentFields();
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
@endsection
|
@endsection
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@
|
||||||
<div class="card shadow-sm mb-4">
|
<div class="card shadow-sm mb-4">
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<form action="{{ route('admin.course-assignments.index') }}" method="GET" class="row g-3">
|
<form action="{{ route('admin.course-assignments.index') }}" method="GET" class="row g-3">
|
||||||
<div class="col-md-4">
|
<div class="col-md-6">
|
||||||
<select name="course_id" class="form-select">
|
<select name="course_id" class="form-select">
|
||||||
<option value="">Все курсы</option>
|
<option value="">Все курсы</option>
|
||||||
@foreach($courses as $id => $title)
|
@foreach($courses as $id => $title)
|
||||||
|
|
@ -22,12 +22,12 @@
|
||||||
@endforeach
|
@endforeach
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-3">
|
<div class="col-md-4">
|
||||||
<select name="type" class="form-select">
|
<select name="type" class="form-select">
|
||||||
<option value="">Все типы</option>
|
<option value="">Все типы</option>
|
||||||
<option value="individual" {{ request('type') == 'individual' ? 'selected' : '' }}>Индивидуальное</option>
|
<option value="individual">Индивидуальное</option>
|
||||||
<option value="group" {{ request('type') == 'group' ? 'selected' : '' }}>Группе</option>
|
<option value="group">Группе</option>
|
||||||
<option value="organization" {{ request('type') == 'organization' ? 'selected' : '' }}>Организации</option>
|
<option value="organization">Организации</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-2">
|
<div class="col-md-2">
|
||||||
|
|
@ -57,9 +57,9 @@
|
||||||
<td><strong>{{ $assignment->course->title }}</strong></td>
|
<td><strong>{{ $assignment->course->title }}</strong></td>
|
||||||
<td>
|
<td>
|
||||||
@if($assignment->type === 'individual')
|
@if($assignment->type === 'individual')
|
||||||
<span class="badge bg-info"><i class="bi bi-person"></i> Индивидуально</span>
|
<span class="badge bg-success"><i class="bi bi-person"></i> Индивидуально</span>
|
||||||
@elseif($assignment->type === 'group')
|
@elseif($assignment->type === 'group')
|
||||||
<span class="badge bg-success"><i class="bi bi-people"></i> Группе</span>
|
<span class="badge bg-info"><i class="bi bi-people"></i> Группе</span>
|
||||||
@else
|
@else
|
||||||
<span class="badge bg-primary"><i class="bi bi-building"></i> Организации</span>
|
<span class="badge bg-primary"><i class="bi bi-building"></i> Организации</span>
|
||||||
@endif
|
@endif
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ use App\Http\Controllers\Admin\CourseAssignmentController;
|
||||||
use App\Http\Controllers\Admin\GroupUserController;
|
use App\Http\Controllers\Admin\GroupUserController;
|
||||||
use App\Http\Controllers\Api\OrganizationSearchController;
|
use App\Http\Controllers\Api\OrganizationSearchController;
|
||||||
use App\Http\Controllers\Api\GroupSearchController;
|
use App\Http\Controllers\Api\GroupSearchController;
|
||||||
|
use App\Http\Controllers\Api\UserSearchController;
|
||||||
use App\Http\Controllers\DashboardController;
|
use App\Http\Controllers\DashboardController;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
|
|
||||||
|
|
@ -60,5 +61,6 @@ Route::middleware('auth')->group(function () {
|
||||||
Route::middleware('auth')->group(function() {
|
Route::middleware('auth')->group(function() {
|
||||||
Route::get('/api/organizations/search', OrganizationSearchController::class)->name('api.organizations.search');
|
Route::get('/api/organizations/search', OrganizationSearchController::class)->name('api.organizations.search');
|
||||||
Route::get('/api/groups/search', GroupSearchController::class)->name('api.groups.search');
|
Route::get('/api/groups/search', GroupSearchController::class)->name('api.groups.search');
|
||||||
|
Route::get('/api/users/search', UserSearchController::class)->name('api.users.search');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue