83 lines
3.2 KiB
PHP
83 lines
3.2 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\Arr;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class DashboardController extends Controller
|
|
{
|
|
/**
|
|
* Dashboard Page
|
|
*
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function index(): \Illuminate\Contracts\View\View
|
|
{
|
|
// * Pour les stats.
|
|
/** @var \App\Models\Settings */
|
|
$settings = app(Settings::class);
|
|
$avgParticipUser = 0;
|
|
$participationPeriod = [];
|
|
$statsUnique = [];
|
|
$query = DB::table((new Participation())->getTable());
|
|
$dateFrmtr = function (object $row) {
|
|
$row->date = Carbon::parse($row->date)->isoFormat('DD/MM/YYYY');
|
|
return $row;
|
|
};
|
|
|
|
/** @var \Illuminate\Support\Collection<string,object> */
|
|
$participationsPerDay = (clone $query)
|
|
->selectRaw('COUNT(*) AS count, DATE(created_at) AS date')
|
|
->groupByRaw('DATE(created_at)')->get()->map($dateFrmtr)->keyBy('date');
|
|
|
|
/** @var \Illuminate\Support\Collection<object> */
|
|
$participationsUniquePerDay = (clone $query)
|
|
->selectRaw('DISTINCT customer_id AS user, COUNT(*) AS count, DATE(created_at) AS date')
|
|
->groupByRaw('DATE(created_at), user')->get()->map($dateFrmtr);
|
|
|
|
/** @var \Illuminate\Support\Collection<object> */
|
|
$participationsUniquePerCustomer = (clone $query)
|
|
->selectRaw("DISTINCT customer_id AS user, COUNT(*) AS count")
|
|
->groupBy('user')->orderBy('user')->get();
|
|
|
|
$participationsTotal = Participation::query()->count();
|
|
|
|
/** @var \Illuminate\Support\Collection<object> 3 codes EAN les plus utilisés */
|
|
$codesEan = (clone $query)
|
|
->selectRaw('COUNT(product_id) AS nb_usage, product_id')
|
|
->groupBy('product_id')
|
|
->orderByDesc('nb_usage')->limit(3)->get();
|
|
|
|
$participationsUniquePerDay->each(function (object $row) use (&$statsUnique) {
|
|
Arr::set($statsUnique, $row->date, (Arr::get($statsUnique, $row->date) ?? 0) + 1);
|
|
});
|
|
|
|
// * Calcul participation moyenne par utilisateur.
|
|
if ($participationsUniquePerCustomer->count() > 0) {
|
|
$avgParticipUser = round($participationsTotal / $participationsUniquePerCustomer->count(), 1);
|
|
}
|
|
|
|
// * Création des tableaux avec toutes les dates depuis le début du jeu
|
|
// * et le nombre de participations associées pour le graph JS.
|
|
$start_date = $settings->start_date;
|
|
$end_date = now()->lt($settings->end_date) ? now() : $settings->end_date;
|
|
$dates = $participationsPerDay->keys();
|
|
$count = $participationsPerDay->pluck('count');
|
|
$countUnique = $dates->map(fn (string $formated_date) => Arr::get($statsUnique, $formated_date, 0));
|
|
|
|
return view('back::pages.dashboard.index', compact(
|
|
'dates',
|
|
'count',
|
|
'countUnique',
|
|
'participationsTotal',
|
|
'avgParticipUser',
|
|
'codesEan',
|
|
'settings'
|
|
));
|
|
}
|
|
}
|