Ajouter les statistiques par période

This commit is contained in:
Alexandre NOEL 2026-10-02 12:11:30 +02:00
commit 86642095cd
14 changed files with 315 additions and 125 deletions

View file

@ -54,12 +54,19 @@ $groups = [
<div class="text-muted"><?= $municipality !== '' ? t('statistics.city_help') : t('statistics.help') ?></div>
</div>
<form method="get" action="/statistics" class="d-flex flex-wrap align-items-end gap-2">
<div><label class="form-label"><?= h(t('statistics.period')) ?></label><select class="form-select"
name="year">
<option value="all" <?= $allYears ? 'selected' : '' ?>><?= h(t('statistics.all_years')) ?></option>
<?php foreach (array_reverse($years) as $y): ?><option value="<?= $y ?>"
<?= !$allYears && $y === $year ? 'selected' : '' ?>><?= $y ?></option><?php endforeach; ?>
<div><label class="form-label"><?= h(t('statistics.period')) ?></label><select class="form-select" name="period_type" id="statisticsPeriodType">
<option value="all" <?= $periodType === 'all' ? 'selected' : '' ?>><?= h(t('statistics.all_years')) ?></option>
<option value="year" <?= $periodType === 'year' ? 'selected' : '' ?>><?= h(t('statistics.by_year')) ?></option>
<option value="month" <?= $periodType === 'month' ? 'selected' : '' ?>><?= h(t('statistics.by_month')) ?></option>
<option value="custom" <?= $periodType === 'custom' ? 'selected' : '' ?>><?= h(t('statistics.custom_period')) ?></option>
</select></div>
<div data-period-field="year"><label class="form-label"><?= h(t('statistics.year')) ?></label><select class="form-select" name="year">
<?php foreach (array_reverse($years) as $y): ?><option value="<?= $y ?>"
<?= $y === $year ? 'selected' : '' ?>><?= $y ?></option><?php endforeach; ?>
</select></div>
<div data-period-field="month"><label class="form-label"><?= h(t('statistics.month')) ?></label><input class="form-control" type="month" name="month" value="<?= h($month) ?>"></div>
<div data-period-field="custom"><label class="form-label"><?= h(t('statistics.from')) ?></label><input class="form-control" type="date" name="date_from" value="<?= h($dateFrom) ?>"></div>
<div data-period-field="custom"><label class="form-label"><?= h(t('statistics.to')) ?></label><input class="form-control" type="date" name="date_to" value="<?= h($dateTo) ?>"></div>
<div><label class="form-label"><?= h(t('statistics.origin_city')) ?></label><select class="form-select"
name="municipality">
<option value=""><?= h(t('statistics.all_cities')) ?></option>
@ -69,7 +76,7 @@ $groups = [
</select></div><button
class="btn btn-cyan"><?= h(t('common.show')) ?></button><?php if ($municipality !== ''): ?><a
class="btn btn-outline-secondary"
href="/statistics?year=<?= h($allYears ? 'all' : (string)$year) ?>"><?= h(t('common.clear')) ?></a><button
href="/statistics?<?= h(http_build_query($periodQuery)) ?>"><?= h(t('common.clear')) ?></a><button
class="btn btn-outline-primary" type="button" onclick="window.print()">🖨
<?= h(t('common.print')) ?></button><?php endif; ?>
</form>
@ -78,7 +85,7 @@ $groups = [
<?php if ($municipality !== ''): ?><div class="alert alert-info d-flex flex-wrap align-items-center gap-2">
<strong><?= h($municipality) ?> ·
<?= h($allYears ? t('statistics.all_years') : (string)$year) ?></strong><span><?= h(t('statistics.city_filter_summary', ['count' => (int)$stats['handled']])) ?></span><span
<?= h($periodLabel) ?></strong><span><?= h(t('statistics.city_filter_summary', ['count' => (int)$stats['handled']])) ?></span><span
class="ms-auto small"><?= h(t('statistics.filter_applied')) ?></span></div><?php endif; ?>
<?php if ($municipality === '' && !empty($municipalityCoverage)): ?><div class="alert alert-info mb-4">
<div class="d-flex flex-wrap align-items-center gap-2">
@ -104,7 +111,7 @@ $groups = [
<div class="text-muted small text-uppercase"><?= h($label) ?></div>
<div class="display-6 fw-bold"><?= (int)$stats[$key] ?></div>
<div class="small text-muted">
<?= $allYears ? t('statistics.since_start') : t('statistics.in_year', ['year' => (int)$year]) ?>
<?= $allYears ? t('statistics.since_start') : h(t('statistics.in_period', ['period' => $periodLabel])) ?>
</div>
</div>
</div>
@ -138,7 +145,7 @@ $groups = [
<div class="col-md-6">
<h4 class="mb-1"><?= h(t('statistics.by_structure')) ?></h4>
<div class="text-muted small mb-2"><?= h(t('statistics.structure_cost_help')) ?></div>
<?php foreach (($costs['by_clinic'] ?? []) as $r): $structureUrl = !empty($r['contact_id']) ? '/directory/view?' . http_build_query(['id' => (int)$r['contact_id'],'year' => $allYears ? 'all' : $year,'municipality' => $municipality]) : null; ?>
<?php foreach (($costs['by_clinic'] ?? []) as $r): $structureUrl = !empty($r['contact_id']) ? '/directory/view?' . http_build_query(['id' => (int)$r['contact_id'],'municipality' => $municipality] + $periodQuery) : null; ?>
<div class="d-flex justify-content-between align-items-start gap-3 border-bottom py-2">
<span><?php if ($structureUrl): ?><a class="fw-semibold" href="<?= h($structureUrl) ?>"
title="<?= h(t('statistics.open_structure_cost', ['structure' => $r['label']])) ?>"><?= h($r['label']) ?></a><?php else: ?><?= h($r['label']) ?><?php endif; ?><span
@ -186,7 +193,7 @@ if (empty($costs['by_clinic'])):?><span class="text-muted"><?= h(t('statistics.n
</tr>
</thead>
<tbody>
<?php foreach (($municipalityCosts ?? []) as $row): $cityUrl = '/statistics?' . http_build_query(['year' => $allYears ? 'all' : $year,'municipality' => $row['municipality']]); ?>
<?php foreach (($municipalityCosts ?? []) as $row): $cityUrl = '/statistics?' . http_build_query($periodQuery + ['municipality' => $row['municipality']]); ?>
<tr>
<td><a class="fw-semibold" href="<?= h($cityUrl) ?>"
title="<?= h(t('statistics.cost_city_open', ['city' => $row['municipality']])) ?>"><?= h($row['municipality']) ?></a>
@ -245,7 +252,7 @@ if (empty($costs['by_clinic'])):?><span class="text-muted"><?= h(t('statistics.n
</div><?php else: ?><div class="card h-100">
<div class="card-header">
<div>
<h3 class="card-title"><?= h(t('grant.comparison', ['year' => $year - 1])) ?></h3>
<h3 class="card-title"><?= h(t('statistics.previous_period_comparison')) ?></h3>
<div class="text-muted small"><?= h(t('statistics.no_previous_percent')) ?></div>
</div>
</div>
@ -254,8 +261,8 @@ if (empty($costs['by_clinic'])):?><span class="text-muted"><?= h(t('statistics.n
<thead>
<tr>
<th><?= h(t('grant.indicator')) ?></th>
<th class="text-end"><?= $year - 1 ?></th>
<th class="text-end"><?= $year ?></th>
<th class="text-end"><?= h($previousPeriodLabel) ?></th>
<th class="text-end"><?= h($periodLabel) ?></th>
<th class="text-end"><?= h(t('grant.evolution')) ?></th>
</tr>
</thead>
@ -297,7 +304,7 @@ if (empty($costs['by_clinic'])):?><span class="text-muted"><?= h(t('statistics.n
<div class="d-flex flex-wrap align-items-baseline gap-2 mb-3">
<h2 class="mb-0"><?= h(t('statistics.profiles')) ?></h2><span
class="text-muted"><?= h($allYears ? t('statistics.profiles_help_all') : t('statistics.profiles_help_year', ['year' => (int)$year])) ?></span>
class="text-muted"><?= h($allYears ? t('statistics.profiles_help_all') : t('statistics.profiles_help_period', ['period' => $periodLabel])) ?></span>
</div>
<div class="row row-cards"><?php foreach ($groups as $group): [$title,$items,$labels,$color] = $group;
$linkType = $group[4] ?? null;
@ -317,7 +324,7 @@ if (empty($costs['by_clinic'])):?><span class="text-muted"><?= h(t('statistics.n
<?php foreach ($items as $label => $total): $display = $labels[$label] ?? $distributionLabels[$label] ?? $label;
$width = $maximum > 0 ? max(4, round($total / $maximum * 100)) : 0;
$pct = $sum > 0 ? round($total / $sum * 100) : 0;
$cityUrl = '/statistics?' . http_build_query(['year' => $allYears ? 'all' : $year,'municipality' => $label]);
$cityUrl = '/statistics?' . http_build_query($periodQuery + ['municipality' => $label]);
$filterValue = $linkType === 'sex' ? match ((string)$label) {
'Femelles' => 'F', 'Mâles' => 'M', default => ''
} : (string)$label;
@ -339,6 +346,18 @@ if (empty($costs['by_clinic'])):?><span class="text-muted"><?= h(t('statistics.n
</div><?php endforeach; ?></div><?php endif; ?></div>
</div>
</div><?php endforeach; ?></div>
<script>
(() => {
const selector = document.getElementById('statisticsPeriodType');
if (!selector) return;
const refresh = () => document.querySelectorAll('[data-period-field]').forEach((field) => {
field.hidden = field.dataset.periodField !== selector.value;
field.querySelectorAll('input,select').forEach((input) => input.disabled = field.hidden);
});
selector.addEventListener('change', refresh);
refresh();
})();
</script>
<?php if ($allYears): ?><script>
document.addEventListener('DOMContentLoaded', () => {
const canvas = document.getElementById('activityHistoryChart');