73 lines
3 KiB
PHP
73 lines
3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Participation;
|
|
use App\Models\Settings;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class DashboardController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
// Pour les stats.
|
|
$settings = app(Settings::class);
|
|
$participations = Participation::selectRaw("COUNT(*) AS count, DATE(created_at) AS date")
|
|
->groupByRaw("DATE(created_at)")->get();
|
|
$participationsUniquePerDay = Participation::selectRaw("DISTINCT user_id AS user, COUNT(*) AS count, DATE(created_at) AS date")
|
|
->groupByRaw("DATE(created_at), user")->get();
|
|
$statsUnique = [];
|
|
foreach ($participationsUniquePerDay as $participationPerDay) {
|
|
$dateUnique = Carbon::parse($participationPerDay->date)->format('d/m/Y');
|
|
$statsUnique[$dateUnique] = isset($statsUnique[$dateUnique]) ? $statsUnique[$dateUnique] + 1 : 1;
|
|
}
|
|
$participationsUnique = Participation::selectRaw("DISTINCT user_id AS user, COUNT(*) AS count")
|
|
->groupBy("user")->orderBy("user")->get();
|
|
|
|
// Données calculées à afficher.
|
|
$totalParticipation = Participation::count();
|
|
if (count($participationsUnique) > 0) {
|
|
$avgParticipUser = round($totalParticipation / count($participationsUnique), 1);
|
|
} else {
|
|
$avgParticipUser = 0;
|
|
}
|
|
|
|
// Tableau des dates comprises dans la table participation.
|
|
foreach ($participations as $participation) {
|
|
$datesParticipations[Carbon::parse($participation->date)->format('d/m/Y')] = $participation->count;
|
|
}
|
|
|
|
// Création des tableaux avec toutes les dates depuis le début du jeu et le nombre de participations associées.
|
|
$date = Carbon::parse($settings->start_date);
|
|
$dateFin = now() < $settings->end_date ? Carbon::now() : Carbon::parse($settings->end_date);
|
|
|
|
// Récupération des 3 codes EAN les plus utilisés.
|
|
$codesEan = Participation::select(
|
|
DB::raw('COUNT(code_ean_id) AS nb_usage, code_ean_id')
|
|
)->groupBy('code_ean_id')->orderByDesc('nb_usage')->limit(3)->get();
|
|
|
|
// Génération des tableaux de date depuis le début du jeu.
|
|
$dates = [];
|
|
$count = [];
|
|
$countUnique = [];
|
|
while ($dateFin > $date) {
|
|
$dates[] = $date->format('d/m/Y');
|
|
$count[] = isset($datesParticipations[$date->format('d/m/Y')]) ?
|
|
$datesParticipations[$date->format('d/m/Y')] : 0;
|
|
$countUnique[] = isset($statsUnique[$date->format('d/m/Y')]) ? $statsUnique[$date->format('d/m/Y')] : 0;
|
|
$date->addDay();
|
|
}
|
|
|
|
return view('bo.dashboard.index', compact(
|
|
'dates',
|
|
'count',
|
|
'countUnique',
|
|
'totalParticipation',
|
|
'avgParticipUser',
|
|
'codesEan',
|
|
'settings'
|
|
));
|
|
}
|
|
}
|