202 lines
7.5 KiB
PHP
202 lines
7.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\StatsByBrandRequest;
|
|
use App\Http\Requests\Bo\StatsByQuestionRequest;
|
|
use App\Models\Brand;
|
|
use App\Models\BrandStat;
|
|
use App\Models\ModelAnswer;
|
|
use App\Models\Question;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use League\Csv\Writer;
|
|
|
|
class StatsController extends Controller
|
|
{
|
|
/**
|
|
* Shows stats by brand.
|
|
*
|
|
* @param \App\Http\Requests\Bo\StatsByBrandRequest $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function viewStatsByBrand(StatsByBrandRequest $request)
|
|
{
|
|
Gate::authorize('viewStatsByBrand');
|
|
$stats = null;
|
|
if ($request->post()) {
|
|
$stats = BrandStat::query()->whereDate('created_at', '>=', $request->start)
|
|
->whereDate('created_at', '<=', $request->end)
|
|
->whereBrandId($request->brand)->get();
|
|
}
|
|
return view('bo.pages.stats.view-stats-by-brands', \compact('stats'));
|
|
}
|
|
|
|
/**
|
|
* Exports stats by brand as a csv file.
|
|
*
|
|
* @param \App\Http\Requests\Bo\StatsByBrandRequest $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function exportByBrandCsv(StatsByBrandRequest $request): \Illuminate\Http\Response
|
|
{
|
|
$brand = Brand::query()->findOrFail($request->brand);
|
|
$stats = BrandStat::query()->with('brand')->whereDate('created_at', '>=', $request->start)
|
|
->whereDate('created_at', '<=', $request->end)
|
|
->whereBrandId($request->brand)->get();
|
|
|
|
$columns = collect(Schema::getColumnListing((new BrandStat())->getTable()))
|
|
->filter(function ($value) {
|
|
return !in_array($value, ['created_at', 'brand_id']);
|
|
})->all();
|
|
|
|
$header = '"date";"marque";' . implode(';', $columns);
|
|
|
|
$w = Writer::createFromString($header . PHP_EOL);
|
|
$w->setDelimiter(';');
|
|
$w->setEnclosure('"');
|
|
$w->setOutputBOM(Writer::BOM_UTF8);
|
|
|
|
foreach ($stats as $stat) {
|
|
$cols = [
|
|
$stat->created_at->isoFormat('YYYY-MM-DD'),
|
|
$stat->brand->name
|
|
];
|
|
foreach ($columns as $colname) {
|
|
$cols[] = $stat->{$colname};
|
|
}
|
|
$w->insertOne($cols);
|
|
}
|
|
return response((string) $w, 200, [
|
|
'Content-Encoding' => 'none',
|
|
'Content-Type' => 'text/csv; charset=UTF-8',
|
|
// phpcs:ignore Generic.Files.LineLength.TooLong
|
|
'Content-Disposition' => "attachment; filename=\"Statistiques du $request->start au $request->end pour $brand->name.csv\"",
|
|
'Content-Description' => 'File Transfer',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Views stats for a specific question.
|
|
*
|
|
* @param \App\Http\Requests\Bo\StatsByQuestionRequest $request
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function viewByQuestion(StatsByQuestionRequest $request): \Illuminate\Contracts\View\View
|
|
{
|
|
Gate::authorize('viewByQuestion');
|
|
$question = null;
|
|
$answers = null;
|
|
$answersByDays = null;
|
|
if ($request->post()) {
|
|
$question = Question::query()->findOrFail($request->question);
|
|
$answers = $question->answers;
|
|
$start = Carbon::parse($request->start);
|
|
$end = Carbon::parse($request->end);
|
|
|
|
$answersByDays = $this->answersByDays($answers, $start, $end);
|
|
}
|
|
|
|
return view('bo.pages.stats.view-stats-by-questions', \compact('question', 'answers', 'answersByDays'));
|
|
}
|
|
|
|
/**
|
|
* Exports stats for a specific question as csv.
|
|
*
|
|
* @param \App\Http\Requests\Bo\StatsByQuestionRequest $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function exportByQuestionCsv(StatsByQuestionRequest $request): \Illuminate\Http\Response
|
|
{
|
|
$question = Question::query()->findOrFail($request->question);
|
|
$answers = $question->answers;
|
|
$start = Carbon::parse($request->start);
|
|
$end = Carbon::parse($request->end);
|
|
|
|
$answersByDays = $this->answersByDays($answers, $start, $end);
|
|
|
|
$header = '"date";' . implode(';', $answers->map(function ($answer) {
|
|
return "\"$answer->answer\"";
|
|
})->all());
|
|
|
|
$w = Writer::createFromString($header . PHP_EOL);
|
|
$w->setDelimiter(';');
|
|
$w->setEnclosure('"');
|
|
$w->setOutputBOM(Writer::BOM_UTF8);
|
|
|
|
foreach ($answersByDays as $day => $dayAnswers) {
|
|
$w->insertOne(array_merge(
|
|
[
|
|
$day
|
|
],
|
|
$dayAnswers->values()->all()
|
|
));
|
|
}
|
|
return response((string) $w, 200, [
|
|
'Content-Encoding' => 'none',
|
|
'Content-Type' => 'text/csv; charset=UTF-8',
|
|
// phpcs:ignore Generic.Files.LineLength.TooLong
|
|
'Content-Disposition' => "attachment; filename=\"Statistiques du $request->start au $request->end pour $question->question.csv\"",
|
|
'Content-Description' => 'File Transfer',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Makes a collection of total users answers by day.
|
|
*
|
|
* @param \Illuminate\Support\Collection $answers A collection of Answer.
|
|
* @param \Illuminate\Support\Carbon $start The wanted start period.
|
|
* @param \Illuminate\Support\Carbon $end The wanted end period.
|
|
* @return \Illuminate\Support\Collection A collection with date as Y_m_d index
|
|
* with array of answers and their total.
|
|
*/
|
|
private function answersByDays(Collection $answers, Carbon $start, Carbon $end): \Illuminate\Support\Collection
|
|
{
|
|
Gate::authorize('answersByDays');
|
|
// * Totalize by date all answers.
|
|
$answersByDays = ModelAnswer::query()
|
|
->whereDate('created_at', '>=', $start)
|
|
->whereDate('created_at', '<=', $end)
|
|
->whereIn('answer_id', $answers->modelKeys())
|
|
->get()->map(function ($custAnswer) {
|
|
return [
|
|
'answer_id' => $custAnswer->answer_id,
|
|
'created_at' => $custAnswer->created_at->isoFormat('YYYY-MM-DD'),
|
|
];
|
|
})->groupBy(['created_at', 'answer_id'])->map(function ($day) {
|
|
return $day->map(function ($custAnswers) {
|
|
return $custAnswers->count();
|
|
});
|
|
});
|
|
|
|
do {
|
|
$currentDate = $start->isoFormat('YYYY-MM-DD');
|
|
/** @var \Illuminate\Support\Collection $answersByDays */
|
|
if (!$answersByDays->has($currentDate)) {
|
|
$answersByDays->put(
|
|
$currentDate,
|
|
$answers->mapWithKeys(function ($answer) {
|
|
return [$answer->answer => 0];
|
|
})
|
|
);
|
|
} else {
|
|
$curentAnswers = $answersByDays->pull($currentDate);
|
|
$answersByDays->put(
|
|
$currentDate,
|
|
$answers->mapWithKeys(function ($answer) use ($curentAnswers) {
|
|
return [$answer->answer =>
|
|
$curentAnswers->has($answer->id) ?
|
|
$curentAnswers->get($answer->id) : 0];
|
|
})
|
|
);
|
|
}
|
|
|
|
$start = $start->addDay();
|
|
} while ($start->lessThanOrEqualTo($end));
|
|
|
|
return $answersByDays;
|
|
}
|
|
}
|