matrix skills

This commit is contained in:
Claudio 2026-02-02 17:25:47 +01:00
parent 340ebdcbce
commit 31f22b4d92
2 changed files with 638 additions and 5 deletions

View File

@ -8,7 +8,7 @@ $db = DBHandlerSelect::getInstance();
$pdo = $db->getConnection();
/* ==========================================
AJAX HANDLERS (ADD / EDIT / DELETE)
AJAX HANDLERS (ADD / EDIT / DELETE / SKILLS)
========================================== */
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['ajax'] == '1') {
header('Content-Type: application/json');
@ -17,6 +17,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj
try {
if ($action === 'add') {
// Codice originale per add
$employee_code = trim($_POST['employee_code'] ?? '');
$first_name = trim($_POST['first_name'] ?? '');
$last_name = trim($_POST['last_name'] ?? '');
@ -59,6 +60,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj
}
if ($action === 'edit') {
// Codice originale per edit
$id = (int)($_POST['id'] ?? 0);
$employee_code = trim($_POST['employee_code'] ?? '');
$first_name = trim($_POST['first_name'] ?? '');
@ -115,6 +117,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj
}
if ($action === 'delete') {
// Codice originale per delete
$id = (int)($_POST['id'] ?? 0);
if ($id <= 0) {
@ -129,9 +132,52 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj
exit;
}
if ($action === 'get_employee_skills') {
$id = (int)$_POST['id'];
if ($id <= 0) {
echo json_encode(['success' => false, 'message' => 'Invalid ID']);
exit;
}
$stmt = $pdo->prepare("SELECT skill_id, level FROM employee_skills WHERE employee_id = ?");
$stmt->execute([$id]);
$skills = [];
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$skills[$row['skill_id']] = $row['level'];
}
echo json_encode(['success' => true, 'skills' => $skills]);
exit;
}
if ($action === 'save_employee_skills') {
$id = (int)$_POST['id'];
$skills_json = $_POST['skills'] ?? '';
$skills = json_decode($skills_json, true);
if ($id <= 0 || !is_array($skills)) {
echo json_encode(['success' => false, 'message' => 'Invalid data']);
exit;
}
$pdo->beginTransaction();
$stmtDelete = $pdo->prepare("DELETE FROM employee_skills WHERE employee_id = ?");
$stmtDelete->execute([$id]);
$stmtInsert = $pdo->prepare("INSERT INTO employee_skills (employee_id, skill_id, level) VALUES (?, ?, ?)");
foreach ($skills as $skill_id => $level) {
$stmtInsert->execute([$id, (int)$skill_id, $level]);
}
$pdo->commit();
echo json_encode(['success' => true]);
exit;
}
echo json_encode(['success' => false, 'message' => 'Unknown action.']);
exit;
} catch (Exception $e) {
if ($pdo->inTransaction()) $pdo->rollBack();
echo json_encode([
'success' => false,
'message' => $e->getMessage()
@ -141,7 +187,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['aj
}
/* ==========================================
PAGE DATA (LIST + USERS LIST)
PAGE DATA (LIST + USERS LIST + SKILLS LIST)
========================================== */
// Employees list
@ -172,6 +218,16 @@ $sqlUsers = "
";
$stmtUsers = $pdo->query($sqlUsers);
$users = $stmtUsers->fetchAll(PDO::FETCH_ASSOC);
// Skills list for JS
$sqlSkills = "
SELECT s.id, s.name, pl.name as line_name, pl.line_number
FROM skills s
LEFT JOIN production_lines pl ON s.production_line_id = pl.id
ORDER BY IFNULL(pl.line_number, 999), s.name
";
$stmtSkills = $pdo->query($sqlSkills);
$allSkills = $stmtSkills->fetchAll(PDO::FETCH_ASSOC);
?>
<!doctype html>
@ -276,6 +332,21 @@ $users = $stmtUsers->fetchAll(PDO::FETCH_ASSOC);
background-color: #fee2e2;
color: #b91c1c;
}
.btn-matrix {
background-color: #198754;
color: #fff;
border-radius: 8px;
padding: 10px 20px;
font-weight: 500;
transition: all 0.2s ease-in-out;
margin-right: 10px;
}
.btn-matrix:hover {
background-color: #157347;
transform: scale(1.02);
}
</style>
</head>
@ -297,10 +368,15 @@ $users = $stmtUsers->fetchAll(PDO::FETCH_ASSOC);
<div class="card-body">
<div class="d-flex justify-content-between align-items-center mb-3">
<h6 class="fw-semibold mb-0">Elenco Completo</h6>
<div>
<button class="btn btn-matrix" onclick="location.href='skill_matrix.php'">
📊 Matrice Skills
</button>
<button class="btn btn-add" data-bs-toggle="modal" data-bs-target="#addEmployeeModal">
Aggiungi Dipendente
</button>
</div>
</div>
<!-- TABELLA -->
<div class="table-responsive">
@ -369,6 +445,13 @@ $users = $stmtUsers->fetchAll(PDO::FETCH_ASSOC);
✏️ Modifica
</button>
<button
class="btn btn-sm btn-outline-info manage-skills"
data-id="<?= (int)$row['id'] ?>"
data-name="<?= htmlspecialchars($fullName, ENT_QUOTES) ?>">
🛠️ Skills
</button>
<button
class="btn btn-sm btn-outline-danger delete-employee"
data-id="<?= (int)$row['id'] ?>"
@ -544,9 +627,33 @@ $users = $stmtUsers->fetchAll(PDO::FETCH_ASSOC);
</div>
</div>
<!-- MODALE GESTIONE SKILLS -->
<div class="modal fade" id="manageSkillsModal" tabindex="-1">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header" style="background-color:#cfe3ff;">
<h5 class="modal-title">Gestione Skills per <span id="skillsEmployeeName"></span></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<form id="manageSkillsForm">
<input type="hidden" id="skillsEmployeeId">
<div id="skillsContainer"></div>
<div class="text-center">
<button type="submit" class="btn btn-add">💾 Salva Skills</button>
</div>
</form>
</div>
</div>
</div>
</div>
<?php include('jsinclude.php'); ?>
<script>
const allSkills = <?= json_encode($allSkills) ?>;
$(document).ready(function() {
// DataTable
$('#tabellaDipendenti').DataTable({
@ -743,6 +850,139 @@ $users = $stmtUsers->fetchAll(PDO::FETCH_ASSOC);
});
});
});
/* -------- OPEN SKILLS MODAL -------- */
$(document).on('click', '.manage-skills', function() {
const id = $(this).data('id');
const name = $(this).data('name');
$('#skillsEmployeeId').val(id);
$('#skillsEmployeeName').text(name);
const payload = new URLSearchParams();
payload.append('ajax', '1');
payload.append('action', 'get_employee_skills');
payload.append('id', id);
fetch('', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: payload.toString()
})
.then(r => r.json())
.then(data => {
if (data.success) {
const currentSkills = data.skills || {};
let html = '';
let currentLine = '';
allSkills.forEach(skill => {
let line = skill.line_name || 'Generali';
if (line !== currentLine) {
if (currentLine) html += '</div>';
html += `<h6 class="mt-3">${line}</h6><div class="row">`;
currentLine = line;
}
let level = currentSkills[skill.id] || 'NON RICH.';
let options = '';
if (skill.name === 'TURNO NOTTURNO') {
options = `
<option value="si" ${level === 'si' ? 'selected' : ''}>Si</option>
<option value="no" ${level === 'no' ? 'selected' : ''}>No</option>
`;
} else {
options = `
<option value="DF" ${level === 'DF' ? 'selected' : ''}>Da formare</option>
<option value="C" ${level === 'C' ? 'selected' : ''}>Conosce l'attività</option>
<option value="CQ" ${level === 'CQ' ? 'selected' : ''}>Conoscenza media</option>
<option value="Q" ${level === 'Q' ? 'selected' : ''}>Qualificato</option>
<option value="NON RICH." ${level === 'NON RICH.' ? 'selected' : ''}>Non richiesto</option>
`;
}
html += `
<div class="col-md-6 mb-3">
<label class="form-label">${skill.name}</label>
<select name="skills[${skill.id}]" class="form-select">
${options}
</select>
</div>
`;
});
if (currentLine) html += '</div>';
$('#skillsContainer').html(html);
$('#manageSkillsModal').modal('show');
} else {
Swal.fire({
icon: 'error',
title: 'Error',
text: data.message || 'Unable to load skills.'
});
}
})
.catch(err => {
Swal.fire({
icon: 'error',
title: 'Error',
text: 'Communication error.'
});
console.error(err);
});
});
/* -------- SAVE SKILLS -------- */
$('#manageSkillsForm').on('submit', function(e) {
e.preventDefault();
const id = $('#skillsEmployeeId').val();
const skills = {};
$('select[name^="skills["]').each(function() {
const skillId = $(this).attr('name').match(/\[(\d+)\]/)[1];
skills[skillId] = $(this).val();
});
const payload = new URLSearchParams();
payload.append('ajax', '1');
payload.append('action', 'save_employee_skills');
payload.append('id', id);
payload.append('skills', JSON.stringify(skills));
fetch('', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: payload.toString()
})
.then(r => r.json())
.then(data => {
if (data.success) {
Swal.fire({
icon: 'success',
title: 'Skills salvate!'
});
$('#manageSkillsModal').modal('hide');
} else {
Swal.fire({
icon: 'error',
title: 'Error',
text: data.message || 'Unable to save skills.'
});
}
})
.catch(err => {
Swal.fire({
icon: 'error',
title: 'Error',
text: 'Communication error.'
});
console.error(err);
});
});
});
</script>

View File

@ -0,0 +1,393 @@
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
include('include/headscript.php');
$db = DBHandlerSelect::getInstance();
$pdo = $db->getConnection();
// AJAX salvataggio singolo (ora gestisce anche testo libero)
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['ajax'] == '1') {
header('Content-Type: application/json');
if ($_POST['action'] === 'save_single_skill') {
$employee_id = (int)($_POST['employee_id'] ?? 0);
$skill_id = (int)($_POST['skill_id'] ?? 0);
$level = trim($_POST['level'] ?? '');
try {
$pdo->prepare("DELETE FROM employee_skills WHERE employee_id = ? AND skill_id = ?")
->execute([$employee_id, $skill_id]);
// Salva solo se c'è valore (non vuoto)
if ($level !== '') {
$pdo->prepare("INSERT INTO employee_skills (employee_id, skill_id, level) VALUES (?, ?, ?)")
->execute([$employee_id, $skill_id, $level]);
}
echo json_encode(['success' => true]);
} catch (Exception $e) {
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
exit;
}
echo json_encode(['success' => false, 'message' => 'Azione non valida']);
exit;
}
// DATI
$employees = $pdo->query("
SELECT id, CONCAT(first_name, ' ', last_name) AS nome
FROM employees
ORDER BY id
")->fetchAll(PDO::FETCH_ASSOC);
$skills = $pdo->query("
SELECT
s.id,
s.name AS nome_completo,
COALESCE(s.abbreviato, SUBSTRING(s.name, 1, 12)) AS acronimo,
pl.line_number,
pl.name AS linea
FROM skills s
LEFT JOIN production_lines pl ON s.production_line_id = pl.id
ORDER BY s.ordinamento ASC, COALESCE(pl.line_number, 999), s.id
")->fetchAll(PDO::FETCH_ASSOC);
$livelli = [];
$stmt = $pdo->query("SELECT employee_id, skill_id, level FROM employee_skills");
while ($r = $stmt->fetch(PDO::FETCH_ASSOC)) {
$livelli[$r['employee_id']][$r['skill_id']] = $r['level'];
}
?>
<!doctype html>
<html lang="it">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<?php include('cssinclude.php'); ?>
<title>Matrice Skills</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap5.min.css">
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap5.min.js"></script>
<style>
body {
font-size: 0.95rem;
background: #f8fafc;
}
.card {
border-radius: 16px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}
.back-btn {
background: #cfe3ff !important;
color: #1f2d3d !important;
border: 1px solid #bcd4f4 !important;
border-radius: 10px;
font-weight: 600;
padding: 8px 16px;
}
.table th,
.table td {
text-align: center;
vertical-align: middle;
padding: 6px 8px;
}
.table thead th {
background: #cfe3ff;
color: #1f2d3d;
white-space: nowrap;
}
.table thead th.group {
background: #a5d8ff;
font-weight: bold;
font-size: 1.05rem;
}
.fixed-name {
min-width: 220px !important;
max-width: 260px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
font-weight: 600;
background: #e9f3ff !important;
position: sticky;
left: 0;
z-index: 1;
}
.skill-col {
min-width: 60px;
}
.skill-header {
font-size: 0.72rem !important;
line-height: 1.0;
padding: 4px 6px !important;
}
.table select,
.table input[type="text"] {
width: 100%;
font-size: 0.85rem;
padding: 4px 6px;
text-align: center;
border-radius: 4px;
border: 1px solid #ccc;
}
.table input[type="text"] {
background: #fffacd;
/* giallo chiaro per distinguere il campo testo */
}
/* COLORI LIVELLI (solo per select) */
.level-Q {
background: #d4edda;
color: #155724;
font-weight: 600;
}
.level-CQ {
background: #fff3cd;
color: #856404;
}
.level-C {
background: #ffeeba;
color: #856404;
}
.level-DF {
background: #f8d7da;
color: #721c24;
}
.level-si {
background: #28a745;
color: white;
font-weight: bold;
}
.level-no {
background: #dc3545;
color: white;
}
.level-NON-RICH {
background: #e9ecef;
color: #6c757d;
}
.table-responsive {
overflow-x: auto;
}
</style>
</head>
<body>
<div class="wrapper toggled">
<?php include('include/navbar.php'); ?>
<?php include('include/topbar.php'); ?>
<div class="page-wrapper">
<div class="page-content">
<div class="card p-3">
<div class="card-header d-flex justify-content-between align-items-center">
<h5 class="mb-0">Matrice Skills</h5>
<button class="btn back-btn" onclick="location.href='employees.php'">↩️ Dipendenti</button>
</div>
<div class="card-body p-0">
<div class="table-responsive">
<table id="matrix" class="table table-bordered table-sm">
<thead>
<tr>
<th rowspan="2" class="fixed-name" style="background:#cfe3ff;">Dipendente</th>
<th colspan="2" class="group text-center" style="background:#fff;"></th>
<th colspan="2" class="group text-center">UHF</th>
<th colspan="2" class="group text-center">BSF</th>
<th colspan="2" class="group text-center">Gerlach</th>
</tr>
<tr>
<?php foreach ($skills as $s): ?>
<th class="skill-col skill-header"
title="<?= htmlspecialchars($s['nome_completo']) ?>">
<?= htmlspecialchars($s['acronimo']) ?>
</th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach ($employees as $emp): ?>
<tr>
<td class="fixed-name"><?= htmlspecialchars($emp['nome']) ?></td>
<?php foreach ($skills as $s):
$val = $livelli[$emp['id']][$s['id']] ?? '';
$cls = str_replace(['.', ' '], '-', $val);
?>
<td>
<?php if ($s['id'] == 29): ?>
<!-- Campo testo libero per id=29 -->
<input type="text"
class="form-control"
value="<?= htmlspecialchars($val) ?>"
onblur="salvaTesto(<?= $emp['id'] ?>, <?= $s['id'] ?>, this.value)">
<?php else: ?>
<!-- Tendina normale per tutte le altre -->
<select class="form-select level-<?= $cls ?>"
onchange="salva(<?= $emp['id'] ?>, <?= $s['id'] ?>, this.value, this)">
<?php if (strpos($s['nome_completo'], 'TURNO NOTTURNO') !== false): ?>
<option value="si" <?= $val === 'si' ? 'selected' : '' ?>>si</option>
<option value="no" <?= $val === 'no' ? 'selected' : '' ?>>no</option>
<?php else: ?>
<option value="DF" <?= $val === 'DF' ? 'selected' : '' ?>>DF</option>
<option value="C" <?= $val === 'C' ? 'selected' : '' ?>>C</option>
<option value="CQ" <?= $val === 'CQ' ? 'selected' : '' ?>>CQ</option>
<option value="Q" <?= $val === 'Q' ? 'selected' : '' ?>>Q</option>
<option value="NON RICH." <?= $val === 'NON RICH.' ? 'selected' : '' ?>></option>
<?php endif; ?>
</select>
<?php endif; ?>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<?php include('include/footer.php'); ?>
</div>
<script>
// Salvataggio per tendina (onchange)
function salva(empId, skillId, val, selectElement) {
fetch('', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `ajax=1&action=save_single_skill&employee_id=${empId}&skill_id=${skillId}&level=${encodeURIComponent(val)}`
})
.then(r => r.json())
.then(data => {
if (data.success) {
Swal.fire({
icon: 'success',
title: 'OK',
timer: 700,
showConfirmButton: false
});
// Aggiorna colore SOLO se selectElement è valido
if (selectElement && selectElement.tagName === 'SELECT') {
// Rimuovi tutte le classi level-*
selectElement.classList.forEach(c => {
if (c.startsWith('level-')) selectElement.classList.remove(c);
});
// Aggiungi la nuova classe level-*
const newCls = 'level-' + val.replace(/[\.\s]/g, '-');
selectElement.classList.add(newCls);
// (sicurezza) assicurati che resti form-select
selectElement.classList.add('form-select');
}
} else {
Swal.fire({
icon: 'error',
title: 'Errore server',
text: data.message || '?'
});
}
})
.catch(err => {
console.error('Fetch error:', err);
Swal.fire({
icon: 'error',
title: 'Errore rete o parsing',
text: err.message || 'Controlla console'
});
});
}
// Salvataggio per campo testo (onblur)
function salvaTesto(empId, skillId, val) {
val = val.trim();
fetch('', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `ajax=1&action=save_single_skill&employee_id=${empId}&skill_id=${skillId}&level=${encodeURIComponent(val)}`
})
.then(r => r.json())
.then(data => {
if (data.success) {
Swal.fire({
icon: 'success',
title: 'Salvato',
timer: 700,
showConfirmButton: false
});
} else {
Swal.fire({
icon: 'error',
title: 'Errore',
text: data.message || '?'
});
}
})
.catch(() => Swal.fire({
icon: 'error',
title: 'Errore rete'
}));
}
$(document).ready(function() {
$('#matrix').DataTable({
scrollX: true,
scrollCollapse: true,
paging: false,
searching: false,
info: false,
ordering: false,
fixedColumns: {
left: 1
},
language: {
url: 'https://cdn.datatables.net/plug-ins/1.13.6/i18n/it-IT.json'
},
columnDefs: [{
targets: 0,
className: 'fixed-name'
}]
});
});
</script>
</body>
</html>