Feat: Organize videos by sector

This commit is contained in:
Xavier Oliveira
2026-06-16 12:25:58 +01:00
parent c5ed77945e
commit 84e9bf7307
21 changed files with 477 additions and 116 deletions

View File

@@ -22,12 +22,17 @@ class DashboardController extends Controller
$userId = $user->id;
$role = $user->role_id;
$userSector = $user->sector_id;
$videos = Video::select('id', 'title', 'thumbnail', 'is_active', 'order')
->where('is_active', true)
->whereDoesntHave('views', function ($q) use ($user) {
$q->where('user_id', $user->id);
})
->when($role !== 1, fn($q) => $q->whereHas('sectors', function ($s) use ($userSector) {
$s->where('sectors.id', $userSector)
->orWhere('sectors.slug', "global");
}))
->orderBy('order', 'asc')
->limit(3)
->get()
@@ -41,19 +46,33 @@ class DashboardController extends Controller
];
});
if ($role === 1) {
$stats = Video::selectRaw('COUNT(CASE WHEN is_active = 1 THEN 1 END) as active_count')->first();
} else {
$stats = Video::selectRaw('
COUNT(CASE WHEN is_active = 1 THEN 1 END) as active_count,
COUNT(CASE WHEN is_active = 1 AND EXISTS (
SELECT 1 FROM video_views
WHERE video_views.video_id = videos.id
AND video_views.user_id = ?
) THEN 1 END) as watched_count
', [$userId])
->first();
}
if ($role !== 1) {
$stats = Video::query()
->where('is_active', true)
->whereHas('sectors', function ($s) use ($userSector) {
$s->where('sectors.id', $userSector)
->orWhere('sectors.slug', "global");
})
->selectRaw('
COUNT(*) as active_count,
COUNT(CASE WHEN EXISTS (
SELECT 1 FROM video_views
WHERE video_views.video_id = videos.id
AND video_views.user_id = ?
) THEN 1 END) as watched_count
', [$userId])
->first();
} else {
$stats = Video::selectRaw('
COUNT(CASE WHEN is_active = 1 THEN 1 END) as active_count,
COUNT(CASE WHEN is_active = 1 AND EXISTS (
SELECT 1 FROM video_views
WHERE video_views.video_id = videos.id
AND video_views.user_id = ?
) THEN 1 END) as watched_count
', [$userId])
->first();
}
$workshops = Workshop::select(['id', 'title', 'image', 'date', 'time_start', 'time_end', 'status'])
->with(['users:id'])