-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatriz_grid.php
More file actions
47 lines (42 loc) · 1.26 KB
/
Copy pathmatriz_grid.php
File metadata and controls
47 lines (42 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
/**
* GET /api/matriz_grid.php
* Devuelve la matriz visual 4×4 (NP × NC) con conteos por celda.
*
* Respuesta:
* {
* ok: true,
* grid: [ { probabilidad, consecuencia, nivel, cantidad, detalle[] }, ... 16 celdas ],
* totales: { 'I': n, 'II': n, 'III': n, 'IV': n },
* total: n
* }
*/
require_once __DIR__ . '/../config/database.php';
$sql = "SELECT probabilidad, consecuencia, nivel, cantidad, detalle
FROM v_matriz_np_nc";
try {
$rows = db()->query($sql)->fetchAll();
$totales = ['I' => 0, 'II' => 0, 'III' => 0, 'IV' => 0];
$grid = [];
$total = 0;
foreach ($rows as $r) {
$cantidad = (int)$r['cantidad'];
$grid[] = [
'probabilidad' => $r['probabilidad'],
'consecuencia' => $r['consecuencia'],
'nivel' => $r['nivel'],
'cantidad' => $cantidad,
'detalle' => json_decode($r['detalle'] ?? '[]', true),
];
$totales[$r['nivel']] += $cantidad;
$total += $cantidad;
}
json_response([
'ok' => true,
'grid' => $grid,
'totales' => $totales,
'total' => $total,
]);
} catch (PDOException $e) {
json_response(['ok' => false, 'error' => $e->getMessage()], 500);
}