From 69e161ada3c9d66d40c1c408e8fdae3fec77aa9f Mon Sep 17 00:00:00 2001 From: Pontoporeia Date: Sat, 28 Mar 2026 11:42:44 +0100 Subject: [PATCH] fix(admin): stats bar always shows whole-DB counts, not filtered counts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit admin/index.php showed "TFE total / Publiés / En attente" by running array_filter() over the already-filtered $theses array returned by getThesesList(). When any search or year filter was active the three numbers reflected only the matching subset, making the stats misleading (e.g. searching for a single student would show "1 total, 0 publiés"). Add Database::getThesesStats(): array — a single SQL aggregation query: SELECT COUNT(*), SUM(is_published), SUM(NOT is_published) FROM theses This runs against the raw theses table with no filters, so the counters always display the true whole-database figures regardless of what filter the admin has active. admin/index.php now calls getThesesStats() and reads $stats['total'], $stats['published'], $stats['pending'] instead of the array_filter expressions. --- TODO.md | 2 +- public/admin/index.php | 9 ++++--- src/Database.php | 24 ++++++++++++++++++ .../ad921d60486366258809553a3db49a4a.json | 2 +- storage/test.db | Bin 237568 -> 237568 bytes 5 files changed, 31 insertions(+), 6 deletions(-) diff --git a/TODO.md b/TODO.md index 7034423..a78e203 100644 --- a/TODO.md +++ b/TODO.md @@ -555,7 +555,7 @@ Goal: rename the tables and column to the canonical M2M pattern (`tags`, `thesis manually prepare `SELECT … FROM thesis_files WHERE thesis_id = ?` instead of calling `$db->getThesisFiles($thesisId)` which already exists. Replace with the DB method. -- [ ] **`admin/index.php` stats computed via PHP `array_filter` on full result set** — "total", +- [x] **`admin/index.php` stats computed via PHP `array_filter` on full result set** — "total", "publiés", "en attente" counts are derived by filtering the already-fetched `$theses` array in PHP. When a filter is active the stats reflect only filtered rows, which is misleading. Add `Database::getThesesStats(): array` returning three counts from SQL diff --git a/public/admin/index.php b/public/admin/index.php index e99cad0..6dc0806 100644 --- a/public/admin/index.php +++ b/public/admin/index.php @@ -22,6 +22,7 @@ try { if ($orientationFilter) $filters['orientation'] = $orientationFilter; $theses = $db->getThesesList($filters); + $stats = $db->getThesesStats(); $years = $db->getAllYears(); $orientations = $db->getAllOrientations(); } catch (Exception $e) { @@ -95,18 +96,18 @@ document.addEventListener('DOMContentLoaded', () => { - +
-
+
TFE total
-
$t['is_published'])) ?>
+
Publiés
-
!$t['is_published'])) ?>
+
En attente
diff --git a/src/Database.php b/src/Database.php index b44461f..2b6e734 100644 --- a/src/Database.php +++ b/src/Database.php @@ -540,6 +540,30 @@ class Database { return $stmt->fetchAll(PDO::FETCH_COLUMN); } + /** + * Return whole-database thesis counts, independent of any active filter. + * + * Always reflects the full theses table so the stats bar in admin/index.php + * shows accurate numbers even when a search or year filter is active. + * + * @return array{total: int, published: int, pending: int} + */ + public function getThesesStats(): array { + $stmt = $this->pdo->query( + "SELECT + COUNT(*) AS total, + SUM(CASE WHEN is_published = 1 THEN 1 ELSE 0 END) AS published, + SUM(CASE WHEN is_published = 0 THEN 1 ELSE 0 END) AS pending + FROM theses" + ); + $row = $stmt->fetch(); + return [ + 'total' => (int) $row['total'], + 'published' => (int) $row['published'], + 'pending' => (int) $row['pending'], + ]; + } + // ======================================================================== // CRUD METHODS (from formulaire) // ======================================================================== diff --git a/src/cache/rate_limit/ad921d60486366258809553a3db49a4a.json b/src/cache/rate_limit/ad921d60486366258809553a3db49a4a.json index a2b2955..f3b3c3f 100644 --- a/src/cache/rate_limit/ad921d60486366258809553a3db49a4a.json +++ b/src/cache/rate_limit/ad921d60486366258809553a3db49a4a.json @@ -1 +1 @@ -[1774615474] \ No newline at end of file +[1774694544] \ No newline at end of file diff --git a/storage/test.db b/storage/test.db index bf8505691c7e9590f3273d02eb25fad39164853f..7e572e06a16c0f3e7d7f3d8d129577ae31fd8476 100644 GIT binary patch delta 28 icmZoTz}IkqZ$pqiV?uMVetWP!BM>uf57uX1TL1u*jtQaw delta 28 icmZoTz}IkqZ$pqiV|;V4etWP!BM>uf57uX1TL1u*d