2016-03-05 00:18:10 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
2016-02-29 15:59:36 +00:00
|
|
|
|
|
|
|
|
use App\Models\Event;
|
|
|
|
|
use App\Models\EventStats;
|
2016-03-05 00:18:10 +00:00
|
|
|
use Carbon\Carbon;
|
|
|
|
|
use DateInterval;
|
|
|
|
|
use DatePeriod;
|
|
|
|
|
use DateTime;
|
2016-02-29 15:59:36 +00:00
|
|
|
|
2016-03-05 00:18:10 +00:00
|
|
|
class EventDashboardController extends MyBaseController
|
|
|
|
|
{
|
2016-03-16 12:51:36 +00:00
|
|
|
/**
|
|
|
|
|
* Show the event dashboard
|
|
|
|
|
*
|
|
|
|
|
* @param bool|false $event_id
|
|
|
|
|
* @return \Illuminate\View\View
|
|
|
|
|
*/
|
2016-03-05 00:18:10 +00:00
|
|
|
public function showDashboard($event_id = false)
|
|
|
|
|
{
|
2016-02-29 15:59:36 +00:00
|
|
|
$event = Event::scope()->findOrFail($event_id);
|
|
|
|
|
|
2016-03-05 00:18:10 +00:00
|
|
|
$num_days = 20;
|
|
|
|
|
|
|
|
|
|
/*
|
2016-02-29 15:59:36 +00:00
|
|
|
* This is a fairly hackish way to get the data for the dashboard charts. I'm sure someone
|
|
|
|
|
* with better SQL skill could do it in one simple query.
|
|
|
|
|
*
|
|
|
|
|
* Filling in the missing days here seems to be fast(ish) (with 20 days history), but the work
|
|
|
|
|
* should be done in the DB
|
|
|
|
|
*/
|
|
|
|
|
$chartData = EventStats::where('event_id', '=', $event->id)
|
2016-09-06 20:39:27 +00:00
|
|
|
->where('date', '>', Carbon::now()->subDays($num_days)->format('Y-m-d'))
|
|
|
|
|
->get()
|
|
|
|
|
->toArray();
|
2016-03-05 00:18:10 +00:00
|
|
|
|
2016-02-29 15:59:36 +00:00
|
|
|
$startDate = new DateTime("-$num_days days");
|
|
|
|
|
$dateItter = new DatePeriod(
|
2016-06-15 02:31:24 +00:00
|
|
|
$startDate, new DateInterval('P1D'), $num_days
|
2016-02-29 15:59:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/*
|
2016-06-15 02:31:24 +00:00
|
|
|
* Iterate through each possible date, if no stats exist for this date set default values
|
|
|
|
|
* Otherwise, if a date does exist use these values
|
2016-02-29 15:59:36 +00:00
|
|
|
*/
|
2016-03-05 00:18:10 +00:00
|
|
|
$result = [];
|
2016-04-20 00:47:27 +00:00
|
|
|
$tickets_data = [];
|
2016-02-29 15:59:36 +00:00
|
|
|
foreach ($dateItter as $date) {
|
|
|
|
|
$views = 0;
|
|
|
|
|
$sales_volume = 0;
|
|
|
|
|
$unique_views = 0;
|
|
|
|
|
$tickets_sold = 0;
|
|
|
|
|
$organiser_fees_volume = 0;
|
|
|
|
|
|
2016-06-15 02:31:24 +00:00
|
|
|
foreach ($chartData as $item) {
|
2016-02-29 15:59:36 +00:00
|
|
|
if ($item['date'] == $date->format('Y-m-d')) {
|
2016-09-06 20:39:27 +00:00
|
|
|
$views = $item['views'];
|
|
|
|
|
$sales_volume = $item['sales_volume'];
|
2016-02-29 15:59:36 +00:00
|
|
|
$organiser_fees_volume = $item['organiser_fees_volume'];
|
2016-09-06 20:39:27 +00:00
|
|
|
$unique_views = $item['unique_views'];
|
|
|
|
|
$tickets_sold = $item['tickets_sold'];
|
2016-06-15 02:31:24 +00:00
|
|
|
|
|
|
|
|
break;
|
2016-02-29 15:59:36 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-05 00:18:10 +00:00
|
|
|
$result[] = [
|
|
|
|
|
'date' => $date->format('Y-m-d'),
|
|
|
|
|
'views' => $views,
|
2016-02-29 15:59:36 +00:00
|
|
|
'unique_views' => $unique_views,
|
|
|
|
|
'sales_volume' => $sales_volume + $organiser_fees_volume,
|
2016-03-05 00:18:10 +00:00
|
|
|
'tickets_sold' => $tickets_sold,
|
|
|
|
|
];
|
2016-02-29 15:59:36 +00:00
|
|
|
}
|
|
|
|
|
|
2016-09-06 20:39:27 +00:00
|
|
|
foreach ($event->tickets as $ticket) {
|
2016-04-11 13:39:37 +00:00
|
|
|
$tickets_data[] = [
|
|
|
|
|
'value' => $ticket->quantity_sold,
|
2016-06-15 02:31:24 +00:00
|
|
|
'label' => $ticket->title,
|
2016-04-11 13:39:37 +00:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-29 15:59:36 +00:00
|
|
|
$data = [
|
2016-04-11 13:39:37 +00:00
|
|
|
'event' => $event,
|
|
|
|
|
'chartData' => json_encode($result),
|
|
|
|
|
'ticketData' => json_encode($tickets_data),
|
2016-02-29 15:59:36 +00:00
|
|
|
];
|
|
|
|
|
|
2016-03-16 12:51:36 +00:00
|
|
|
return view('ManageEvent.Dashboard', $data);
|
2016-02-29 15:59:36 +00:00
|
|
|
}
|
|
|
|
|
}
|