2020-10-21 15:26:21 +02:00
< ? php
2022-01-10 14:02:25 +01:00
namespace App\Http\Controllers\Bo ;
2020-10-21 15:26:21 +02:00
use App\Http\Controllers\Controller ;
2020-11-20 15:18:31 +01:00
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
{
public function index ()
{
2020-11-20 15:18:31 +01:00
// Pour les stats
2022-01-07 19:00:30 +01:00
$settings = app ( Settings :: class );
2021-08-18 17:08:02 +02:00
$participations = Participation :: selectRaw ( " COUNT(*) AS count, DATE(created_at) AS date " ) -> groupByRaw ( " DATE(created_at) " ) -> get ();
2020-11-20 15:18:31 +01:00
$participationsUniquePerDay = Participation :: selectRaw ( " DISTINCT user_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' );
2020-11-20 15:18:31 +01:00
$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 ();
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 ;
}
2020-11-20 15:18:31 +01:00
// Tableau des dates comprises dans la table participation
2021-08-18 17:08:02 +02:00
foreach ( $participations as $participation ) {
2020-11-20 15:18:31 +01:00
$datesParticipations [ Carbon :: parse ( $participation -> date ) -> format ( 'd/m/Y' )] = $participation -> count ;
2021-08-18 17:08:02 +02:00
}
2020-11-20 15:18:31 +01:00
// Création des tableaux avec toutes les dates depuis le début du jeu et le nombre de participations associées
2021-08-18 17:08:02 +02:00
$date = Carbon :: parse ( $settings -> start_date );
2020-11-20 15:18:31 +01:00
$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
2021-08-18 17:08:02 +02:00
$dates = [];
$count = [];
2020-12-01 12:55:34 +01:00
$countUnique = [];
2020-11-20 15:18:31 +01:00
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' )]) ? $datesParticipations [ $date -> format ( 'd/m/Y' )] : 0 ;
2020-11-20 15:18:31 +01:00
$countUnique [] = isset ( $statsUnique [ $date -> format ( 'd/m/Y' )]) ? $statsUnique [ $date -> format ( 'd/m/Y' )] : 0 ;
$date -> addDay ();
}
2020-11-23 14:55:25 +01:00
return view ( 'bo.dashboard.index' , compact ( 'dates' , 'count' , 'countUnique' , 'totalParticipation' , 'avgParticipUser' , 'codesEan' , 'settings' ));
2020-10-21 15:26:21 +02:00
}
}