Skip to content
Merged

Dev #178

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions app/Charts/StatsPerYearChart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Charts;

use ConsoleTVs\Charts\Classes\Chartjs\Chart;

class StatsPerYearChart extends Chart
{
/**
* Initializes the chart.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
}
19 changes: 10 additions & 9 deletions app/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,15 @@ public static function withEvents()

$isos = DB::table('events')
->select(['country_iso'])
->where('status',"=","APPROVED")
->where('status', "=", "APPROVED")
->groupBy('country_iso')
->get()
->pluck('country_iso')
;

->pluck('country_iso');


$countries = Country::findMany($isos)->sortBy('name');



return $countries;

}
Expand All @@ -41,12 +38,11 @@ public static function withActualYearEvents()

$isos = DB::table('events')
->select(['country_iso'])
->where('status',"=","APPROVED")
->where('status', "=", "APPROVED")
->whereYear('end_date', '>=', Carbon::now('Europe/Brussels')->year)
->groupBy('country_iso')
->get()
->pluck('country_iso')
;
->pluck('country_iso');

$countries = Country::findMany($isos)->sortBy('name');

Expand All @@ -57,9 +53,14 @@ public static function withActualYearEvents()

public function events()
{

return $this->hasMany('App\Event');
}

public function approvedEvents($year, $operator)
{
return $this->hasMany(Event::class)
->where('status', '=', 'APPROVED')
->whereYear('end_date', $operator, $year);
}

}
226 changes: 226 additions & 0 deletions app/Http/Controllers/StatsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
<?php

namespace App\Http\Controllers;


use App\Charts\StatsPerYearChart;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class StatsController extends Controller
{


public function getMetrics(Request $request)
{
$year = ($request->has('yearSelector')) ? $request->input('yearSelector') : 2018;
$creatorsWithOneEvent = $this->creatorsWithAtLeastOneEvent($year);
$totalParticipants = (int)$this->totalParticipants($year);
$eventsNotReported = $this->eventsNotReported($year);
$eventsFinished = $this->eventsFinished($year);
$years = $this->getYearsArray();
//dd($creatorsWithOneEvent);



return view('stats',[
'creatorsWithOneEvent' => $creatorsWithOneEvent,
'totalParticipants' => $totalParticipants,
'eventsNotReported' => $eventsNotReported,
'eventsFinished' => $eventsFinished,
'years' => $years,
'selectedYear' => $year,
'flag' => 0
]);
}


public function getEventsPerYear(Request $request)
{
$year = ($request->has('yearSelector')) ? $request->input('yearSelector') : 2018;
$years = $this->getYearsArray();
$total = 0;
$eventsPerYear = $this->eventsPerYear($year, null);

$groupedEvents = $this->groupSmallEvents($eventsPerYear, $total);
$chart = $this->setPieChart(array_sort($groupedEvents), $year);


return view('stats', ['years' => $years,
'eventsPerYear' => $eventsPerYear,
'selectedYear' => $year,
'chart' => $chart,
'total' => $total,
'flag' => 1
]);
}

public function getEventsPerOrganiserType(Request $request)
{
$types = [
'Libraries' => 'library'
, 'Non profit organisations' => 'non profit'
, 'Private Businesses' => 'private business'
, 'Schools' => 'school'
];

$now = Carbon::now('Europe/Brussels');
$year = $now->year;
$type = ($request->has('typeSelector')) ? $types[$request->input('typeSelector')] : $types['Schools'];
$eventsPerYear = $this->eventsPerYear($year, $type);
$total = 0;
$groupedEvents = $this->groupSmallEvents($eventsPerYear, $total, sizeof($eventsPerYear));
$chart = $this->setBarChart($groupedEvents, $year);



return view('stats', ['types' => $types,
'eventsPerYear' => $eventsPerYear,
'selectedType' => array_search($type, $types),
'chart' => $chart,
'year' => $year,
'flag' => 2
]);
}


private function eventsPerYear($year, $type)
{
$eventsPerYear = (!$type)
? DB::table('countries as c')
->selectRaw('c.name, count(c.iso) as events')
->join('events as e', 'c.iso', '=', 'e.country_iso')
->where('e.status', '=', 'APPROVED')
->whereYear('e.end_date', '=', $year)
->groupBy('c.name')
->orderBy('events', 'desc')
->pluck('events', 'c.name')
->toArray()
: DB::table('countries as c')
->selectRaw('c.name, count(c.iso) as events')
->join('events as e', 'c.iso', '=', 'e.country_iso')
->where('e.status', '=', 'APPROVED')
->whereNotNull('e.organizer_type')
->where('e.organizer_type', '=', $type)
->whereYear('e.end_date', '>=', $year)
->groupBy('c.name')
->orderBy('events', 'desc')
->pluck('events', 'c.name')
->toArray();

return $eventsPerYear;
}


private function creatorsWithAtLeastOneEvent($year)
{
$operator = ($year == 2018) ? '>=' : '=';
$count = DB::table('events as e')
->select()
->where('e.status', '=', 'APPROVED')
->whereYear('e.end_date', $operator, $year)
->distinct()
->count('creator_id');
return $count;

}

private function totalParticipants($year)
{
$operator = ($year == 2018) ? '>=' : '=';
$count = DB::table('events as e')
->select()
->where('e.status', '=', 'APPROVED')
->whereYear('e.end_date', $operator, $year)
->where('id', '<>', 183480)
->sum('e.participants_count');
return $count;

}

private function eventsFinished($year)
{
$operator = ($year == 2018) ? '>=' : '=';
$count = DB::table('events as e')
->select()
->where('e.status', '=', 'APPROVED')
->whereYear('e.end_date', $operator, $year)
->where('e.end_date', '<=', Carbon::now('Europe/Brussels'))
->count();
return $count;

}

private function eventsNotReported($year)
{
$operator = ($year == 2018) ? '>=' : '=';
$count = DB::table('events as e')
->select()
->where('e.status', '=', 'APPROVED')
->whereYear('e.start_date', $operator, $year)
->whereYear('e.end_date', $operator, $year)
->where('e.end_date', '<', Carbon::now('Europe/Brussels'))
->whereNull('e.participants_count')
->count();
return $count;

}

private function setPieChart($eventsPerYear, $year)
{
$colors = ['#08a0c4', '#5e9fb2', '#819da1', '#9b9b8f', '#b1997d', '#c3976b', '#d49559', '#e39244', '#f18f2d', '#ff8c00'];

$chart = new StatsPerYearChart();
$chart->displayAxes(false);
$chart->height(500);
$chart->dataset('Event distribution for CodeWeek ' . $year, 'pie', array_values($eventsPerYear))
->backgroundColor(array_reverse($colors));
$chart->labels(array_keys($eventsPerYear));
return $chart;
}


private function setBarChart($eventsPerYear, $year)
{
$colors = ['#DBDE00', '#DBDE00', '#E1B400', '#E1B400', '#E1B400', '#E1B400', '#E1B400',
'#E1B400', '#E56F00', '#E16E03', '#DD6D06', '#D96D09', '#D56C0C', '#D16C10', '#CD6B13',
'#CD6B13', '#C96A16', '#C56A19', '#C2691D', '#BE6920', '#BA6823', '#B66826', '#B2672A',
'#AE662D', '#AA6630', '#A66533', '#A36537', '#9F643A', '#9B643D', '#976340', '#936243',
'#955D42', '#536479', '#0E70B2', '#006EBF', '#004DBF'];

$chart = new StatsPerYearChart();
$chart->dataset('Event distribution for CodeWeek ' . $year, 'bar', array_values($eventsPerYear))
->backgroundColor(array_reverse($colors));
$chart->labels(array_keys($eventsPerYear));
return $chart;
}

private function getYearsArray()
{
$years = [];
$now = Carbon::now(null);
$diffYears = $now->year - 2013;

for ($index = 0; $index < $diffYears; $index++) {
array_push($years, 2014 + $index);
}

return $years;
}

private function groupSmallEvents( $eventsPerYear, &$total)
{
$groupedEvents = ['Others' => 0];
$maxCountEvents = max(array_values($eventsPerYear));
foreach ($eventsPerYear as $country => $events) {
$total += $events;
if ($events <= (round($maxCountEvents * 0.03))) {
$groupedEvents['Others'] += $events;
continue;
}
$groupedEvents[$country] = $events;
}
return $groupedEvents;
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"php": "^7.1.3",
"aws/aws-sdk-php": "^3.67",
"caouecs/laravel-lang": "~3.0",
"consoletvs/charts": "6.*",
"doctrine/dbal": "^2.8",
"fideloper/proxy": "^4.0",
"intervention/image": "^2.4",
Expand Down
Loading