517 lines
21 KiB
PHP
517 lines
21 KiB
PHP
<?php
|
|
require_once('include/headscript.php');
|
|
|
|
/**
|
|
* SUPERADMIN - Membri di una scuola (school_user)
|
|
* Aggiunta per email, cambio ruolo, attiva/disattiva, rimozione.
|
|
* Protezione: una scuola non può restare senza owner attivo.
|
|
*/
|
|
SchoolContext::requireSuperadmin();
|
|
$pdo = DBHandlerSelect::getInstance()->getConnection();
|
|
|
|
if (!function_exists('e')) {
|
|
function e($value): string
|
|
{
|
|
return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
|
|
}
|
|
}
|
|
|
|
if (empty($_SESSION['csrf_sa'])) {
|
|
$_SESSION['csrf_sa'] = bin2hex(random_bytes(16));
|
|
}
|
|
$csrf = $_SESSION['csrf_sa'];
|
|
|
|
$roles = [
|
|
'owner' => 'Titolare',
|
|
'admin' => 'Admin',
|
|
'teacher' => 'Insegnante',
|
|
'student' => 'Allievo',
|
|
];
|
|
|
|
$sid = (int) ($_GET['id'] ?? $_POST['idschool'] ?? 0);
|
|
$st = $pdo->prepare("SELECT * FROM schools WHERE idschool = ?");
|
|
$st->execute([$sid]);
|
|
$school = $st->fetch();
|
|
if (!$school) {
|
|
header('Location: sa-schools.php');
|
|
exit;
|
|
}
|
|
$back = 'sa-school-members.php?id=' . $sid;
|
|
|
|
/** Owner attivi della scuola, escluso eventualmente un membro */
|
|
function activeOwnersExcept(PDO $pdo, int $sid, int $exceptId): int
|
|
{
|
|
$q = $pdo->prepare(
|
|
"SELECT COUNT(*) FROM school_user
|
|
WHERE idschool = ? AND school_role = 'owner' AND active = 'Y' AND idschooluser <> ?"
|
|
);
|
|
$q->execute([$sid, $exceptId]);
|
|
return (int) $q->fetchColumn();
|
|
}
|
|
|
|
/** Riga membro (solo se appartiene a questa scuola) */
|
|
function memberRow(PDO $pdo, int $sid, int $idsu): ?array
|
|
{
|
|
$q = $pdo->prepare("SELECT * FROM school_user WHERE idschooluser = ? AND idschool = ?");
|
|
$q->execute([$idsu, $sid]);
|
|
return $q->fetch() ?: null;
|
|
}
|
|
|
|
/* ----------------------------------------------------------------------
|
|
* AZIONI
|
|
* -------------------------------------------------------------------- */
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
if (!hash_equals($csrf, $_POST['token'] ?? '')) {
|
|
http_response_code(400);
|
|
die('Richiesta non valida.');
|
|
}
|
|
|
|
$action = $_POST['action'] ?? '';
|
|
$idsu = (int) ($_POST['idschooluser'] ?? 0);
|
|
|
|
try {
|
|
if ($action === 'add') {
|
|
$email = trim((string) ($_POST['email'] ?? ''));
|
|
$role = $_POST['role'] ?? 'student';
|
|
if (!isset($roles[$role]) || $email === '') {
|
|
header("Location: $back&error=data");
|
|
exit;
|
|
}
|
|
$u = $pdo->prepare("SELECT id FROM auth_users WHERE email = ? LIMIT 1");
|
|
$u->execute([$email]);
|
|
$uid = (int) $u->fetchColumn();
|
|
if ($uid === 0) {
|
|
header("Location: $back&error=notfound");
|
|
exit;
|
|
}
|
|
$pdo->prepare(
|
|
"INSERT INTO school_user (user_id, idschool, school_role, active)
|
|
VALUES (?, ?, ?, 'Y')
|
|
ON DUPLICATE KEY UPDATE school_role = ?, active = 'Y'"
|
|
)->execute([$uid, $sid, $role, $role]);
|
|
header("Location: $back&msg=added");
|
|
exit;
|
|
}
|
|
|
|
$row = $idsu > 0 ? memberRow($pdo, $sid, $idsu) : null;
|
|
if (!$row) {
|
|
header("Location: $back&error=data");
|
|
exit;
|
|
}
|
|
$isActiveOwner = ($row['school_role'] === 'owner' && $row['active'] === 'Y');
|
|
|
|
if ($action === 'role') {
|
|
$role = $_POST['role'] ?? '';
|
|
if (!isset($roles[$role])) {
|
|
header("Location: $back&error=data");
|
|
exit;
|
|
}
|
|
if ($isActiveOwner && $role !== 'owner' && activeOwnersExcept($pdo, $sid, $idsu) === 0) {
|
|
header("Location: $back&error=lastowner");
|
|
exit;
|
|
}
|
|
$pdo->prepare("UPDATE school_user SET school_role = ? WHERE idschooluser = ? AND idschool = ?")
|
|
->execute([$role, $idsu, $sid]);
|
|
header("Location: $back&msg=role");
|
|
exit;
|
|
}
|
|
|
|
if ($action === 'toggle') {
|
|
if ($isActiveOwner && activeOwnersExcept($pdo, $sid, $idsu) === 0) {
|
|
header("Location: $back&error=lastowner");
|
|
exit;
|
|
}
|
|
$pdo->prepare("UPDATE school_user SET active = IF(active = 'Y', 'N', 'Y') WHERE idschooluser = ? AND idschool = ?")
|
|
->execute([$idsu, $sid]);
|
|
header("Location: $back&msg=toggled");
|
|
exit;
|
|
}
|
|
|
|
if ($action === 'remove') {
|
|
if ($isActiveOwner && activeOwnersExcept($pdo, $sid, $idsu) === 0) {
|
|
header("Location: $back&error=lastowner");
|
|
exit;
|
|
}
|
|
$pdo->prepare("DELETE FROM school_user WHERE idschooluser = ? AND idschool = ?")
|
|
->execute([$idsu, $sid]);
|
|
header("Location: $back&msg=removed");
|
|
exit;
|
|
}
|
|
} catch (PDOException $ex) {
|
|
error_log('sa-school-members: ' . $ex->getMessage());
|
|
header("Location: $back&error=db");
|
|
exit;
|
|
}
|
|
|
|
header("Location: $back");
|
|
exit;
|
|
}
|
|
|
|
/* ----------------------------------------------------------------------
|
|
* ELENCO (filtri: ruolo, ricerca)
|
|
* -------------------------------------------------------------------- */
|
|
$fRole = $_GET['role'] ?? '';
|
|
$fQ = trim((string) ($_GET['q'] ?? ''));
|
|
|
|
$where = ["su.idschool = ?"];
|
|
$params = [$sid];
|
|
if (isset($roles[$fRole])) {
|
|
$where[] = "su.school_role = ?";
|
|
$params[] = $fRole;
|
|
}
|
|
if ($fQ !== '') {
|
|
$where[] = "(u.first_name LIKE ? OR u.last_name LIKE ? OR u.email LIKE ?)";
|
|
$like = '%' . $fQ . '%';
|
|
array_push($params, $like, $like, $like);
|
|
}
|
|
|
|
$sql = "SELECT su.*, u.first_name, u.last_name, u.email
|
|
FROM school_user su
|
|
JOIN auth_users u ON u.id = su.user_id
|
|
WHERE " . implode(' AND ', $where) . "
|
|
ORDER BY FIELD(su.school_role, 'owner', 'admin', 'teacher', 'student'), u.last_name, u.first_name
|
|
LIMIT 300";
|
|
$stm = $pdo->prepare($sql);
|
|
$stm->execute($params);
|
|
$members = $stm->fetchAll();
|
|
|
|
$cnt = $pdo->prepare("SELECT school_role, COUNT(*) AS n FROM school_user WHERE idschool = ? GROUP BY school_role");
|
|
$cnt->execute([$sid]);
|
|
$counts = array_column($cnt->fetchAll(), 'n', 'school_role');
|
|
|
|
$msgMap = [
|
|
'added' => 'Membro aggiunto.',
|
|
'role' => 'Ruolo aggiornato.',
|
|
'toggled' => 'Stato del membro aggiornato.',
|
|
'removed' => 'Membro rimosso dalla scuola.',
|
|
];
|
|
$errMap = [
|
|
'data' => 'Dati non validi.',
|
|
'notfound' => 'Nessun utente registrato con questa email. L\'utente deve prima registrarsi.',
|
|
'lastowner' => 'Operazione bloccata: la scuola resterebbe senza un titolare attivo. Nomina prima un altro owner.',
|
|
'db' => 'Errore database, operazione non eseguita.',
|
|
];
|
|
$roleColors = [
|
|
'owner' => ['#fff4d6', '#8a6d00'],
|
|
'admin' => ['#e8f1fb', '#2980b9'],
|
|
'teacher' => ['#f1e8fb', '#8e44ad'],
|
|
'student' => ['#e3f2ec', '#1a7f52'],
|
|
];
|
|
?>
|
|
<!doctype html>
|
|
<html lang="it">
|
|
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<title>YogiBook - Membri <?php echo e($school['name']); ?></title>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<link rel="shortcut icon" href="assets/images/favicon.ico">
|
|
<link href="assets/css/bootstrap.min.css" id="bootstrap-style" rel="stylesheet" type="text/css" />
|
|
<link href="assets/css/icons.min.css" rel="stylesheet" type="text/css" />
|
|
<link href="assets/css/app.min.css" id="app-style" rel="stylesheet" type="text/css" />
|
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
|
|
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
|
|
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
|
|
<style>
|
|
.sa-card {
|
|
background: #fff;
|
|
border-radius: 14px;
|
|
box-shadow: 0 3px 16px rgba(0, 0, 0, 0.06);
|
|
padding: 22px;
|
|
margin-bottom: 22px;
|
|
}
|
|
|
|
.sa-card h5 {
|
|
font-weight: 700;
|
|
color: #1f2937;
|
|
margin-bottom: 16px;
|
|
}
|
|
|
|
.sa-card label {
|
|
font-size: 13px;
|
|
font-weight: 600;
|
|
color: #374151;
|
|
}
|
|
|
|
.count-chips {
|
|
display: flex;
|
|
gap: 8px;
|
|
flex-wrap: wrap;
|
|
margin-bottom: 16px;
|
|
}
|
|
|
|
.count-chip {
|
|
border: 1px solid #e2e4e9;
|
|
background: #fff;
|
|
color: #374151;
|
|
font-size: 13px;
|
|
font-weight: 600;
|
|
padding: 6px 14px;
|
|
border-radius: 20px;
|
|
text-decoration: none;
|
|
}
|
|
|
|
.count-chip.active,
|
|
.count-chip:hover {
|
|
background: #1ebf73;
|
|
border-color: #1ebf73;
|
|
color: #fff;
|
|
}
|
|
|
|
.member-row {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 12px;
|
|
padding: 10px 14px;
|
|
border-radius: 10px;
|
|
background: #f8fafc;
|
|
border: 1px solid #eef0f3;
|
|
margin-bottom: 8px;
|
|
}
|
|
|
|
.member-row.inactive {
|
|
opacity: .55;
|
|
}
|
|
|
|
.avatar {
|
|
flex: 0 0 38px;
|
|
width: 38px;
|
|
height: 38px;
|
|
border-radius: 50%;
|
|
background: #E1D0B8;
|
|
color: #5a4630;
|
|
font-weight: 800;
|
|
font-size: 14px;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
text-transform: uppercase;
|
|
}
|
|
|
|
.member-info {
|
|
flex: 1;
|
|
min-width: 0;
|
|
}
|
|
|
|
.member-name {
|
|
font-weight: 600;
|
|
color: #1f2937;
|
|
}
|
|
|
|
.member-email {
|
|
font-size: 12px;
|
|
color: #6b7280;
|
|
}
|
|
|
|
.role-pill {
|
|
font-size: 11px;
|
|
font-weight: 700;
|
|
padding: 2px 9px;
|
|
border-radius: 20px;
|
|
margin-left: 6px;
|
|
}
|
|
|
|
.member-actions {
|
|
display: flex;
|
|
gap: 6px;
|
|
align-items: center;
|
|
flex-shrink: 0;
|
|
}
|
|
|
|
.member-actions select {
|
|
font-size: 12px;
|
|
padding: 4px 8px;
|
|
height: auto;
|
|
width: auto;
|
|
}
|
|
|
|
.btn-soft {
|
|
border: none;
|
|
border-radius: 8px;
|
|
padding: 6px 11px;
|
|
font-size: 12px;
|
|
font-weight: 600;
|
|
cursor: pointer;
|
|
}
|
|
|
|
.btn-soft.toggle {
|
|
background: #eef2f7;
|
|
color: #374151;
|
|
}
|
|
|
|
.btn-soft.remove {
|
|
background: #fdecea;
|
|
color: #c0392b;
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
<div id="layout-wrapper">
|
|
<header id="page-topbar" class="isvertical-topbar">
|
|
<div class="navbar-header">
|
|
<div class="d-flex">
|
|
<?php include('include/logoarea.php'); ?>
|
|
<button type="button" class="btn btn-sm px-3 font-size-24 header-item waves-effect vertical-menu-btn">
|
|
<i class="bx bx-menu align-middle"></i>
|
|
</button>
|
|
<div class="page-title-box align-self-center d-none d-md-block">
|
|
<h4 class="page-title mb-0">Membri: <?php echo e($school['name']); ?></h4>
|
|
</div>
|
|
</div>
|
|
<div class="d-flex">
|
|
<?php include('include/schoolswitcher.php'); ?>
|
|
<?php include('include/languageselection.php'); ?>
|
|
<?php include('include/profiletopbar.php'); ?>
|
|
</div>
|
|
</div>
|
|
</header>
|
|
<?php include('include/sidebar.php'); ?>
|
|
|
|
<div class="main-content">
|
|
<div class="page-content">
|
|
<div class="container-fluid">
|
|
|
|
<a href="sa-schools.php" class="d-inline-block mb-3"><i class="fas fa-arrow-left me-1"></i> Torna alle scuole</a>
|
|
|
|
<?php if (isset($_GET['msg'], $msgMap[$_GET['msg']])) : ?>
|
|
<div class="alert alert-success"><?php echo e($msgMap[$_GET['msg']]); ?></div>
|
|
<?php endif; ?>
|
|
<?php if (isset($_GET['error'])) : ?>
|
|
<div class="alert alert-danger"><?php echo e($errMap[$_GET['error']] ?? 'Si è verificato un errore.'); ?></div>
|
|
<?php endif; ?>
|
|
|
|
<!-- AGGIUNGI MEMBRO -->
|
|
<div class="sa-card">
|
|
<h5><i class="fas fa-user-plus me-1" style="color:#1ebf73;"></i> Aggiungi membro</h5>
|
|
<form method="post" action="sa-school-members.php" class="row g-2 align-items-end">
|
|
<input type="hidden" name="token" value="<?php echo e($csrf); ?>">
|
|
<input type="hidden" name="action" value="add">
|
|
<input type="hidden" name="idschool" value="<?php echo $sid; ?>">
|
|
<div class="col-md-6">
|
|
<label>Email utente registrato</label>
|
|
<input type="email" name="email" class="form-control" required>
|
|
</div>
|
|
<div class="col-md-3">
|
|
<label>Ruolo</label>
|
|
<select name="role" class="form-select">
|
|
<?php foreach ($roles as $k => $lbl) : ?>
|
|
<option value="<?php echo $k; ?>" <?php echo $k === 'student' ? 'selected' : ''; ?>><?php echo e($lbl); ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-3">
|
|
<button type="submit" class="btn btn-primary w-100"><i class="fas fa-plus me-1"></i> Aggiungi</button>
|
|
</div>
|
|
</form>
|
|
<small class="text-muted d-block mt-2">Se l'utente è già membro, viene aggiornato il ruolo e riattivato.</small>
|
|
</div>
|
|
|
|
<!-- ELENCO MEMBRI -->
|
|
<div class="sa-card">
|
|
<h5><i class="fas fa-users me-1" style="color:#1ebf73;"></i> Membri</h5>
|
|
|
|
<div class="count-chips">
|
|
<a class="count-chip <?php echo $fRole === '' ? 'active' : ''; ?>" href="<?php echo $back; ?>">Tutti (<?php echo array_sum($counts); ?>)</a>
|
|
<?php foreach ($roles as $k => $lbl) : ?>
|
|
<a class="count-chip <?php echo $fRole === $k ? 'active' : ''; ?>" href="<?php echo $back; ?>&role=<?php echo $k; ?>"><?php echo e($lbl); ?> (<?php echo (int) ($counts[$k] ?? 0); ?>)</a>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
|
|
<form method="get" action="sa-school-members.php" class="row g-2 mb-3">
|
|
<input type="hidden" name="id" value="<?php echo $sid; ?>">
|
|
<input type="hidden" name="role" value="<?php echo e($fRole); ?>">
|
|
<div class="col-md-6">
|
|
<input type="text" name="q" class="form-control" placeholder="Cerca per nome, cognome o email" value="<?php echo e($fQ); ?>">
|
|
</div>
|
|
<div class="col-md-2">
|
|
<button class="btn btn-light w-100"><i class="fas fa-search"></i> Cerca</button>
|
|
</div>
|
|
</form>
|
|
|
|
<?php if (empty($members)) : ?>
|
|
<p class="text-center text-muted py-3">Nessun membro trovato.</p>
|
|
<?php endif; ?>
|
|
|
|
<?php foreach ($members as $mb) :
|
|
$idsu = (int) $mb['idschooluser'];
|
|
$full = trim($mb['first_name'] . ' ' . $mb['last_name']);
|
|
$ini = mb_substr($mb['first_name'] ?? '', 0, 1) . mb_substr($mb['last_name'] ?? '', 0, 1);
|
|
$rc = $roleColors[$mb['school_role']] ?? ['#eef2f7', '#374151'];
|
|
$active = $mb['active'] === 'Y';
|
|
?>
|
|
<div class="member-row <?php echo $active ? '' : 'inactive'; ?>">
|
|
<div class="avatar"><?php echo e($ini ?: '?'); ?></div>
|
|
<div class="member-info">
|
|
<span class="member-name"><?php echo e($full ?: '(senza nome)'); ?></span>
|
|
<span class="role-pill" style="background:<?php echo $rc[0]; ?>;color:<?php echo $rc[1]; ?>;"><?php echo e($roles[$mb['school_role']] ?? $mb['school_role']); ?></span>
|
|
<?php if (!$active) : ?><span class="role-pill" style="background:#1f2937;color:#fff;">Disattivato</span><?php endif; ?>
|
|
<div class="member-email"><?php echo e($mb['email']); ?></div>
|
|
</div>
|
|
<div class="member-actions">
|
|
<form method="post" action="sa-school-members.php" class="m-0">
|
|
<input type="hidden" name="token" value="<?php echo e($csrf); ?>">
|
|
<input type="hidden" name="action" value="role">
|
|
<input type="hidden" name="idschool" value="<?php echo $sid; ?>">
|
|
<input type="hidden" name="idschooluser" value="<?php echo $idsu; ?>">
|
|
<select name="role" class="form-select" onchange="this.form.submit()">
|
|
<?php foreach ($roles as $k => $lbl) : ?>
|
|
<option value="<?php echo $k; ?>" <?php echo $k === $mb['school_role'] ? 'selected' : ''; ?>><?php echo e($lbl); ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</form>
|
|
<form method="post" action="sa-school-members.php" class="m-0">
|
|
<input type="hidden" name="token" value="<?php echo e($csrf); ?>">
|
|
<input type="hidden" name="action" value="toggle">
|
|
<input type="hidden" name="idschool" value="<?php echo $sid; ?>">
|
|
<input type="hidden" name="idschooluser" value="<?php echo $idsu; ?>">
|
|
<button type="submit" class="btn-soft toggle" title="<?php echo $active ? 'Disattiva' : 'Riattiva'; ?>">
|
|
<i class="fas <?php echo $active ? 'fa-user-slash' : 'fa-user-check'; ?>"></i>
|
|
</button>
|
|
</form>
|
|
<form method="post" action="sa-school-members.php" class="m-0 form-remove" data-name="<?php echo e($full); ?>">
|
|
<input type="hidden" name="token" value="<?php echo e($csrf); ?>">
|
|
<input type="hidden" name="action" value="remove">
|
|
<input type="hidden" name="idschool" value="<?php echo $sid; ?>">
|
|
<input type="hidden" name="idschooluser" value="<?php echo $idsu; ?>">
|
|
<button type="submit" class="btn-soft remove" title="Rimuovi dalla scuola"><i class="fas fa-trash"></i></button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
<?php if (count($members) === 300) : ?>
|
|
<p class="text-muted small mt-2">Mostrati i primi 300 risultati: usa la ricerca per restringere.</p>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
</div>
|
|
<?php include('include/footer.php'); ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="assets/libs/bootstrap/js/bootstrap.bundle.min.js"></script>
|
|
<script src="assets/libs/metismenujs/metismenujs.min.js"></script>
|
|
<script src="assets/libs/simplebar/simplebar.min.js"></script>
|
|
<script src="assets/libs/eva-icons/eva.min.js"></script>
|
|
<script src="assets/js/app.js"></script>
|
|
<script>
|
|
$(document).on('submit', '.form-remove', function(e) {
|
|
e.preventDefault();
|
|
const form = this;
|
|
Swal.fire({
|
|
title: 'Rimuovere dalla scuola?',
|
|
html: '<strong>' + $('<div>').text($(form).data('name')).html() + '</strong><br><br>L\'account resta attivo sul portale, ma perde l\'accesso a questa scuola.',
|
|
icon: 'warning',
|
|
showCancelButton: true,
|
|
confirmButtonColor: '#e74c3c',
|
|
confirmButtonText: 'Sì, rimuovi',
|
|
cancelButtonText: 'Annulla'
|
|
}).then((r) => {
|
|
if (r.isConfirmed) form.submit();
|
|
});
|
|
});
|
|
</script>
|
|
</body>
|
|
|
|
</html>
|