Feat: Назначения с несколькими курсами
✅ tags-input для курсов (чёрные бейджи) ✅ CourseSearchController API ✅ Массовое создание назначений (курсы × получатели) ✅ Обновлён create.blade.php Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
parent
81828de9a3
commit
fefe12367d
|
|
@ -43,12 +43,7 @@ class CourseAssignmentController extends Controller
|
|||
{
|
||||
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'));
|
||||
return view('admin.course-assignments.create');
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
|
|
@ -56,7 +51,7 @@ class CourseAssignmentController extends Controller
|
|||
Gate::authorize('create', CourseAssignment::class);
|
||||
|
||||
$validated = $request->validate([
|
||||
'course_id' => 'required|exists:courses,id',
|
||||
'course_ids' => 'nullable|string',
|
||||
'type' => 'nullable|in:individual,group,organization',
|
||||
'user_ids' => 'nullable|string',
|
||||
'group_ids' => 'nullable|string',
|
||||
|
|
@ -81,60 +76,69 @@ class CourseAssignmentController extends Controller
|
|||
}
|
||||
}
|
||||
|
||||
// Создаём назначения для каждого выбранного элемента
|
||||
// Получаем списки ID
|
||||
$courseIds = !empty($validated['course_ids']) ? array_map('intval', array_filter(explode(',', $validated['course_ids']))) : [];
|
||||
$userIds = !empty($validated['user_ids']) ? array_map('intval', array_filter(explode(',', $validated['user_ids']))) : [];
|
||||
$groupIds = !empty($validated['group_ids']) ? array_map('intval', array_filter(explode(',', $validated['group_ids']))) : [];
|
||||
$organizationIds = !empty($validated['organization_ids']) ? array_map('intval', array_filter(explode(',', $validated['organization_ids']))) : [];
|
||||
|
||||
// Создаём назначения для каждой комбинации
|
||||
$created = 0;
|
||||
|
||||
// Назначения пользователям
|
||||
if (!empty($validated['user_ids'])) {
|
||||
$userIds = array_map('intval', array_filter(explode(',', $validated['user_ids'])));
|
||||
if (!empty($userIds)) {
|
||||
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++;
|
||||
foreach ($courseIds as $courseId) {
|
||||
CourseAssignment::create([
|
||||
'course_id' => $courseId,
|
||||
'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'])));
|
||||
if (!empty($groupIds)) {
|
||||
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++;
|
||||
foreach ($courseIds as $courseId) {
|
||||
CourseAssignment::create([
|
||||
'course_id' => $courseId,
|
||||
'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'])));
|
||||
if (!empty($organizationIds)) {
|
||||
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++;
|
||||
foreach ($courseIds as $courseId) {
|
||||
CourseAssignment::create([
|
||||
'course_id' => $courseId,
|
||||
'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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Course;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class CourseSearchController extends Controller
|
||||
{
|
||||
public function __invoke(Request $request)
|
||||
{
|
||||
$query = $request->get('q', '');
|
||||
|
||||
$courses = Course::query()
|
||||
->with('category')
|
||||
->where('title', 'like', "%{$query}%")
|
||||
->orderBy('title')
|
||||
->limit(50)
|
||||
->get()
|
||||
->map(function($course) {
|
||||
return [
|
||||
'id' => $course->id,
|
||||
'text' => $course->title . ($course->category ? " ({$course->category->name})" : ''),
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json($courses);
|
||||
}
|
||||
}
|
||||
|
|
@ -18,14 +18,14 @@
|
|||
<div class="card-header bg-primary text-white"><h5 class="mb-0">Основная информация</h5></div>
|
||||
<div class="card-body">
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Курс *</label>
|
||||
<select name="course_id" class="form-select @error('course_id') is-invalid @enderror" required>
|
||||
<option value="">Выберите курс</option>
|
||||
@foreach($courses as $id => $title)
|
||||
<option value="{{ $id }}" {{ old('course_id') == $id ? 'selected' : '' }}>{{ $title }}</option>
|
||||
@endforeach
|
||||
</select>
|
||||
@error('course_id')<div class="invalid-feedback">{{ $message }}</div>@enderror
|
||||
<label class="form-label">Курсы *</label>
|
||||
<x-tags-input
|
||||
name="course_ids"
|
||||
url="{{ route('api.courses.search') }}"
|
||||
placeholder="Начните вводить название курса..."
|
||||
badge_color="dark"
|
||||
/>
|
||||
<small class="text-muted">Чёрные бейджи — выбранные курсы</small>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ use App\Http\Controllers\Admin\GroupUserController;
|
|||
use App\Http\Controllers\Api\OrganizationSearchController;
|
||||
use App\Http\Controllers\Api\GroupSearchController;
|
||||
use App\Http\Controllers\Api\UserSearchController;
|
||||
use App\Http\Controllers\Api\CourseSearchController;
|
||||
use App\Http\Controllers\DashboardController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
|
|
@ -62,5 +63,6 @@ Route::middleware('auth')->group(function () {
|
|||
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/users/search', UserSearchController::class)->name('api.users.search');
|
||||
Route::get('/api/courses/search', CourseSearchController::class)->name('api.courses.search');
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue