[FIX] ServicesController: query assessment_responses reale + NonConformityController: getPagination named keys

This commit is contained in:
DevEnv nis2-agile 2026-03-09 10:22:40 +01:00
parent 159d783ed7
commit 8578cb5c31
2 changed files with 19 additions and 21 deletions

View File

@ -60,8 +60,10 @@ class NonConformityController extends BaseController
$this->requireOrgAccess(); $this->requireOrgAccess();
$orgId = $this->getCurrentOrgId(); $orgId = $this->getCurrentOrgId();
[$page, $perPage] = $this->getPagination(); $pagination = $this->getPagination();
$offset = ($page - 1) * $perPage; $page = $pagination['page'];
$perPage = $pagination['per_page'];
$offset = $pagination['offset'];
// Filters // Filters
$where = 'n.organization_id = ?'; $where = 'n.organization_id = ?';

View File

@ -801,35 +801,31 @@ class ServicesController extends BaseController
$recommendations = []; $recommendations = [];
if ($assessment) { if ($assessment) {
// Calcola score per dominio (10 categorie Art.21) // Calcola score per dominio — response_value: not_implemented=0, partial=2, implemented=4, not_applicable=null
$responses = Database::fetchAll( $responses = Database::fetchAll(
'SELECT ar.*, q.category, q.weight 'SELECT question_code, category, response_value
FROM assessment_responses ar
JOIN (
SELECT question_code, category, weight
FROM (
SELECT question_code,
JSON_UNQUOTE(JSON_EXTRACT(question_data, "$.category")) as category,
CAST(JSON_UNQUOTE(JSON_EXTRACT(question_data, "$.weight")) AS DECIMAL(3,1)) as weight
FROM assessment_responses FROM assessment_responses
WHERE assessment_id = ? WHERE assessment_id = ?',
) t GROUP BY question_code [$assessment['id']]
) q ON q.question_code = ar.question_code
WHERE ar.assessment_id = ?',
[$assessment['id'], $assessment['id']]
); );
// Semplificato: score per categoria
$byCategory = []; $byCategory = [];
foreach ($responses as $r) { foreach ($responses as $r) {
$cat = $r['category'] ?? 'uncategorized'; $cat = $r['category'] ?? 'uncategorized';
if (!isset($byCategory[$cat])) { if (!isset($byCategory[$cat])) {
$byCategory[$cat] = ['total' => 0, 'count' => 0]; $byCategory[$cat] = ['total' => 0, 'count' => 0];
} }
$val = (int) ($r['response_value'] ?? 0); $val = match($r['response_value'] ?? 'not_implemented') {
'implemented' => 4,
'partial' => 2,
'not_implemented' => 0,
default => 0,
};
if ($r['response_value'] !== 'not_applicable') {
$byCategory[$cat]['total'] += $val; $byCategory[$cat]['total'] += $val;
$byCategory[$cat]['count']++; $byCategory[$cat]['count']++;
} }
}
$totalScore = 0; $totalScore = 0;
$catCount = 0; $catCount = 0;