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);
|
Gate::authorize('create', CourseAssignment::class);
|
||||||
|
|
||||||
$courses = Course::pluck('title', 'id');
|
return view('admin.course-assignments.create');
|
||||||
$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)
|
public function store(Request $request)
|
||||||
|
|
@ -56,7 +51,7 @@ class CourseAssignmentController extends Controller
|
||||||
Gate::authorize('create', CourseAssignment::class);
|
Gate::authorize('create', CourseAssignment::class);
|
||||||
|
|
||||||
$validated = $request->validate([
|
$validated = $request->validate([
|
||||||
'course_id' => 'required|exists:courses,id',
|
'course_ids' => 'nullable|string',
|
||||||
'type' => 'nullable|in:individual,group,organization',
|
'type' => 'nullable|in:individual,group,organization',
|
||||||
'user_ids' => 'nullable|string',
|
'user_ids' => 'nullable|string',
|
||||||
'group_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;
|
$created = 0;
|
||||||
|
|
||||||
// Назначения пользователям
|
// Назначения пользователям
|
||||||
if (!empty($validated['user_ids'])) {
|
if (!empty($userIds)) {
|
||||||
$userIds = array_map('intval', array_filter(explode(',', $validated['user_ids'])));
|
|
||||||
foreach ($userIds as $userId) {
|
foreach ($userIds as $userId) {
|
||||||
CourseAssignment::create([
|
foreach ($courseIds as $courseId) {
|
||||||
'course_id' => $validated['course_id'],
|
CourseAssignment::create([
|
||||||
'user_id' => $userId,
|
'course_id' => $courseId,
|
||||||
'type' => 'individual',
|
'user_id' => $userId,
|
||||||
'start_date' => $validated['start_date'],
|
'type' => 'individual',
|
||||||
'end_date' => $validated['end_date'] ?? null,
|
'start_date' => $validated['start_date'],
|
||||||
'note' => $validated['note'] ?? null,
|
'end_date' => $validated['end_date'] ?? null,
|
||||||
'created_by' => $validated['created_by'],
|
'note' => $validated['note'] ?? null,
|
||||||
'is_active' => $validated['is_active'],
|
'created_by' => $validated['created_by'],
|
||||||
]);
|
'is_active' => $validated['is_active'],
|
||||||
$created++;
|
]);
|
||||||
|
$created++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Назначения группам
|
// Назначения группам
|
||||||
if (!empty($validated['group_ids'])) {
|
if (!empty($groupIds)) {
|
||||||
$groupIds = array_map('intval', array_filter(explode(',', $validated['group_ids'])));
|
|
||||||
foreach ($groupIds as $groupId) {
|
foreach ($groupIds as $groupId) {
|
||||||
CourseAssignment::create([
|
foreach ($courseIds as $courseId) {
|
||||||
'course_id' => $validated['course_id'],
|
CourseAssignment::create([
|
||||||
'group_id' => $groupId,
|
'course_id' => $courseId,
|
||||||
'type' => 'group',
|
'group_id' => $groupId,
|
||||||
'start_date' => $validated['start_date'],
|
'type' => 'group',
|
||||||
'end_date' => $validated['end_date'] ?? null,
|
'start_date' => $validated['start_date'],
|
||||||
'note' => $validated['note'] ?? null,
|
'end_date' => $validated['end_date'] ?? null,
|
||||||
'created_by' => $validated['created_by'],
|
'note' => $validated['note'] ?? null,
|
||||||
'is_active' => $validated['is_active'],
|
'created_by' => $validated['created_by'],
|
||||||
]);
|
'is_active' => $validated['is_active'],
|
||||||
$created++;
|
]);
|
||||||
|
$created++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Назначения организациям
|
// Назначения организациям
|
||||||
if (!empty($validated['organization_ids'])) {
|
if (!empty($organizationIds)) {
|
||||||
$organizationIds = array_map('intval', array_filter(explode(',', $validated['organization_ids'])));
|
|
||||||
foreach ($organizationIds as $organizationId) {
|
foreach ($organizationIds as $organizationId) {
|
||||||
CourseAssignment::create([
|
foreach ($courseIds as $courseId) {
|
||||||
'course_id' => $validated['course_id'],
|
CourseAssignment::create([
|
||||||
'organization_id' => $organizationId,
|
'course_id' => $courseId,
|
||||||
'type' => 'organization',
|
'organization_id' => $organizationId,
|
||||||
'start_date' => $validated['start_date'],
|
'type' => 'organization',
|
||||||
'end_date' => $validated['end_date'] ?? null,
|
'start_date' => $validated['start_date'],
|
||||||
'note' => $validated['note'] ?? null,
|
'end_date' => $validated['end_date'] ?? null,
|
||||||
'created_by' => $validated['created_by'],
|
'note' => $validated['note'] ?? null,
|
||||||
'is_active' => $validated['is_active'],
|
'created_by' => $validated['created_by'],
|
||||||
]);
|
'is_active' => $validated['is_active'],
|
||||||
$created++;
|
]);
|
||||||
|
$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-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>
|
||||||
<select name="course_id" class="form-select @error('course_id') is-invalid @enderror" required>
|
<x-tags-input
|
||||||
<option value="">Выберите курс</option>
|
name="course_ids"
|
||||||
@foreach($courses as $id => $title)
|
url="{{ route('api.courses.search') }}"
|
||||||
<option value="{{ $id }}" {{ old('course_id') == $id ? 'selected' : '' }}>{{ $title }}</option>
|
placeholder="Начните вводить название курса..."
|
||||||
@endforeach
|
badge_color="dark"
|
||||||
</select>
|
/>
|
||||||
@error('course_id')<div class="invalid-feedback">{{ $message }}</div>@enderror
|
<small class="text-muted">Чёрные бейджи — выбранные курсы</small>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="mb-3">
|
<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\OrganizationSearchController;
|
||||||
use App\Http\Controllers\Api\GroupSearchController;
|
use App\Http\Controllers\Api\GroupSearchController;
|
||||||
use App\Http\Controllers\Api\UserSearchController;
|
use App\Http\Controllers\Api\UserSearchController;
|
||||||
|
use App\Http\Controllers\Api\CourseSearchController;
|
||||||
use App\Http\Controllers\DashboardController;
|
use App\Http\Controllers\DashboardController;
|
||||||
use Illuminate\Support\Facades\Route;
|
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/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');
|
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