2020-10-21 15:26:21 +02:00
< ? php
namespace App\Http\Controllers\bo ;
use App\Http\Controllers\Controller ;
2020-11-20 15:18:31 +01:00
use App\Models\Participation ;
use App\Models\Settings ;
use Carbon\Carbon ;
2020-10-21 15:26:21 +02:00
use Illuminate\Http\Request ;
2020-11-20 15:18:31 +01:00
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
$settings = Settings :: find ( 1 );
$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 ();
$avgParticipUser = round ( $totalParticipation / count ( $participationsUnique ), 1 );
// 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
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' ));
2020-10-21 15:26:21 +02:00
}
}