ldc-gen-game-web/app/Http/Controllers/Bo/DashboardController.php

83 lines
3.3 KiB
PHP
Raw Normal View History

2020-10-21 15:26:21 +02:00
<?php
namespace App\Http\Controllers\Bo;
2020-10-21 15:26:21 +02:00
use App\Http\Controllers\Controller;
use App\Models\Participation;
use App\Models\Settings;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
2020-10-21 15:26:21 +02:00
class DashboardController extends Controller
{
2023-08-23 13:29:54 +00:00
/**
* Dashboard Page
*
* @return \Illuminate\Contracts\View\View
*/
public function index(): \Illuminate\Contracts\View\View
2020-10-21 15:26:21 +02:00
{
2023-11-30 11:37:17 +01:00
// FIXME: refactor and simply this
// * Pour les stats.
$settings = app(Settings::class);
2023-11-30 11:37:17 +01:00
$participations = Participation::query()->selectRaw("COUNT(*) AS count, DATE(created_at) AS date")
->groupByRaw("DATE(created_at)")->get();
2023-11-30 11:37:17 +01:00
$participationsUniquePerDay = Participation::query()->selectRaw(
2023-08-24 12:59:51 +00:00
"DISTINCT customer_id AS user, COUNT(*) AS count, DATE(created_at) AS date"
)->groupByRaw("DATE(created_at), user")->get();
2021-08-18 17:08:02 +02:00
$statsUnique = [];
foreach ($participationsUniquePerDay as $participationPerDay) {
$dateUnique = Carbon::parse($participationPerDay->date)->format('d/m/Y');
$statsUnique[$dateUnique] = isset($statsUnique[$dateUnique]) ? $statsUnique[$dateUnique] + 1 : 1;
}
2023-11-30 11:37:17 +01:00
$participationsUnique = Participation::query()->selectRaw("DISTINCT customer_id AS user, COUNT(*) AS count")
2023-11-20 17:22:24 +01:00
->groupBy("user")->orderBy("user")->get();
2023-11-30 11:37:17 +01:00
// * Données calculées à afficher.
$totalParticipation = Participation::query()->count();
2021-08-18 17:08:02 +02:00
if (count($participationsUnique) > 0) {
2020-12-01 12:46:56 +01:00
$avgParticipUser = round($totalParticipation / count($participationsUnique), 1);
} else {
$avgParticipUser = 0;
}
2023-11-30 11:37:17 +01:00
// * Tableau des dates comprises dans la table participation.
2021-08-18 17:08:02 +02:00
foreach ($participations as $participation) {
$datesParticipations[Carbon::parse($participation->date)->format('d/m/Y')] = $participation->count;
2021-08-18 17:08:02 +02:00
}
2023-11-30 11:37:17 +01:00
// * Création des tableaux avec toutes les dates depuis le début du jeu et le nombre de participations associées.
2023-08-23 13:29:54 +00:00
$date = $settings->start_date;
2023-11-30 11:37:17 +01:00
$dateFin = now()->lt($settings->end_date) ? now() : $settings->end_date;
// Récupération des 3 codes EAN les plus utilisés.
2023-11-30 11:37:17 +01:00
$codesEan = Participation::query()->select(
DB::raw('COUNT(product_id) AS nb_usage, product_id')
)->groupBy('product_id')->orderByDesc('nb_usage')->limit(3)->get();
2023-08-23 13:29:54 +00:00
// TODO : Carbon Interval.
2023-11-30 11:37:17 +01:00
// * Génération des tableaux de date depuis le début du jeu.
2021-08-18 17:08:02 +02:00
$dates = [];
$count = [];
2020-12-01 12:55:34 +01:00
$countUnique = [];
while ($dateFin > $date) {
2021-08-18 17:08:02 +02:00
$dates[] = $date->format('d/m/Y');
$count[] = isset($datesParticipations[$date->format('d/m/Y')]) ?
2023-08-23 13:29:54 +00:00
$datesParticipations[$date->format('d/m/Y')] : 0;
$countUnique[] = isset($statsUnique[$date->format('d/m/Y')]) ? $statsUnique[$date->format('d/m/Y')] : 0;
$date->addDay();
}
2023-11-20 17:22:24 +01:00
return view('back::pages.dashboard.index', compact(
'dates',
'count',
'countUnique',
'totalParticipation',
'avgParticipUser',
'codesEan',
'settings'
));
2020-10-21 15:26:21 +02:00
}
}