2022-09-20 19:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Forms;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Models\Forms\Form;
|
|
|
|
use Carbon\CarbonPeriod;
|
|
|
|
|
|
|
|
class FormStatsController extends Controller
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->middleware('auth');
|
|
|
|
}
|
|
|
|
|
2023-12-07 12:01:41 +00:00
|
|
|
public function getFormStats(string $workspaceId, string $formId)
|
2022-09-20 19:59:52 +00:00
|
|
|
{
|
2023-08-30 07:58:29 +00:00
|
|
|
$form = Form::findOrFail($formId);
|
|
|
|
|
2022-09-20 19:59:52 +00:00
|
|
|
$this->authorize('view', $form);
|
|
|
|
|
2024-02-23 10:54:12 +00:00
|
|
|
$formStats = $form->statistics()->where('date', '>', now()->subDays(29)->startOfDay())->get();
|
|
|
|
$periodStats = ['views' => [], 'submissions' => []];
|
2022-09-20 19:59:52 +00:00
|
|
|
foreach (CarbonPeriod::create(now()->subDays(29), now()) as $dateObj) {
|
|
|
|
$date = $dateObj->format('d-m-Y');
|
|
|
|
|
|
|
|
$statisticData = $formStats->where('date', $dateObj->format('Y-m-d'))->first();
|
2024-02-23 10:54:12 +00:00
|
|
|
$periodStats['views'][$date] = $statisticData->data['views'] ?? 0;
|
|
|
|
$periodStats['submissions'][$date] = $statisticData->data['submissions'] ?? 0;
|
2022-09-20 19:59:52 +00:00
|
|
|
|
2024-02-23 10:54:12 +00:00
|
|
|
if ($dateObj->toDateString() === now()->toDateString()) {
|
|
|
|
$periodStats['views'][$date] += $form->views()->count();
|
|
|
|
$periodStats['submissions'][$date] += $form->submissions()->whereDate('created_at', '>=', now()->startOfDay())->count();
|
2022-09-20 19:59:52 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-23 10:54:12 +00:00
|
|
|
|
2022-09-20 19:59:52 +00:00
|
|
|
return $periodStats;
|
|
|
|
}
|
|
|
|
}
|