yogiboook_new/public/userarea/teacher_list.php
2026-01-21 10:29:37 +01:00

317 lines
14 KiB
PHP

<?php
// teacher_list.php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include('include/headscript.php');
require_once 'class/mailer.php';
$dbHandler = DBHandlerSelect::getInstance();
$pdo = $dbHandler->getConnection();
if (!isset($iduserlogin)) {
die("Errore: utente non loggato.");
}
// Recupera scuola corrente
$stmt = $pdo->prepare("
SELECT id, name, owner_id, email AS school_email
FROM schools
WHERE owner_id = ?
");
$stmt->execute([$iduserlogin]);
$school = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$school) {
die("Errore: nessuna scuola trovata per questo proprietario.");
}
$school_id = $school['id'];
$school_name = $school['name'];
$school_email = $school['school_email'];
// Messaggi
$success = $_GET['success'] ?? null;
$error = $_GET['error'] ?? null;
// COLLEGAMENTO TRAMITE CODICE
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'link_by_code') {
$unique_code = trim($_POST['unique_code'] ?? '');
$link_id = (int)$pdo->lastInsertId();
if (empty($unique_code)) {
$error = "Inserisci un codice univoco valido.";
} else {
$stmt = $pdo->prepare("
SELECT t.id AS teacher_id, u.first_name, u.last_name, u.email
FROM teachers t
JOIN auth_users u ON t.user_id = u.id
WHERE t.unique_code = ?
");
$stmt->execute([$unique_code]);
$teacher = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$teacher) {
$error = "Nessun insegnante trovato con questo codice.";
} else {
$check = $pdo->prepare("SELECT id FROM teacher_schools WHERE teacher_id = ? AND school_id = ?");
$check->execute([$teacher['teacher_id'], $school_id]);
if ($check->fetch()) {
$error = "Insegnante già collegata.";
} else {
$stmt = $pdo->prepare("
INSERT INTO teacher_schools
(teacher_id, school_id, status, created_at, updated_at)
VALUES (?, ?, 'pending', NOW(), NOW())
");
$stmt->execute([$teacher['teacher_id'], $school_id]);
// Email richiesta
$subject = "Richiesta collegamento a {$school_name}";
$body = "
<h2>Ciao {$teacher['first_name']},</h2>
<p>{$school_name} vorrebbe collegarti alla sua scuola su YogiBoook.</p>
<p style='margin:30px 0;'>
<a href='http://localhost/yogiboook/public/userarea/confirm_link.php?code=" . urlencode($unique_code) . "&school={$school_id}'
style='background:#0d6efd;color:white;padding:12px 24px;text-decoration:none;border-radius:6px;'>
Accetta
</a>
</p>
<p>Se non riconosci questa richiesta, ignora l'email.</p>
";
$result = sendEmail($teacher['email'], $subject, $body);
$success = $result['success']
? "Richiesta inviata!"
: "Collegamento creato, ma errore email: " . $result['message'];
}
}
}
}
// LISTA INSEGNANTI
$stmt = $pdo->prepare("
SELECT
t.id, t.user_id, t.phone, t.description, t.specializations, t.profile_picture,
u.first_name, u.last_name, u.email,
ts.status AS link_status, ts.created_at AS linked_at,
(t.created_by = ?) AS can_edit
FROM teacher_schools ts
JOIN teachers t ON ts.teacher_id = t.id
JOIN auth_users u ON t.user_id = u.id
WHERE ts.school_id = ? AND ts.status IN ('active','pending')
ORDER BY u.last_name, u.first_name
");
$stmt->execute([$iduserlogin, $school_id]);
$teachers = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!doctype html>
<html lang="it">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Insegnanti - <?= htmlspecialchars($school_name) ?></title>
<?php include('cssinclude.php'); ?>
<link href="https://cdn.jsdelivr.net/npm/quill@2.0.2/dist/quill.snow.css" rel="stylesheet" />
<style>
.quill-wrapper {
min-height: 260px;
border: 1px solid #ced4da;
border-radius: 0.375rem;
overflow: hidden;
}
.ql-container {
min-height: 220px;
}
.ql-editor {
min-height: 220px;
}
</style>
</head>
<body>
<div class="wrapper">
<?php include('include/navbar.php'); ?>
<?php include('include/topbar.php'); ?>
<div class="page-wrapper">
<div class="page-content">
<h4 class="mb-4">Insegnanti di <?= htmlspecialchars($school_name) ?></h4>
<?php if ($success): ?>
<div class="alert alert-success alert-dismissible fade show"><?= $success ?><button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert alert-danger alert-dismissible fade show"><?= htmlspecialchars($error) ?><button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>
<?php endif; ?>
<div class="card radius-10">
<div class="card-body">
<div class="d-flex justify-content-end mb-3">
<button class="btn btn-primary me-2" data-bs-toggle="modal" data-bs-target="#addTeacherModal">
<i class="bx bx-plus"></i> Nuova insegnante
</button>
<button class="btn btn-outline-primary" data-bs-toggle="modal" data-bs-target="#linkTeacherModal">
<i class="bx bx-link"></i> Collega esistente
</button>
</div>
<?php if (empty($teachers)): ?>
<div class="text-center py-5 text-muted">
<i class="bx bx-user-x fs-1 mb-3 opacity-50"></i>
<p>Nessuna insegnante collegata.</p>
</div>
<?php else: ?>
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>Foto</th>
<th>Nome</th>
<th>Email</th>
<th>Telefono</th>
<th>Stato</th>
<th>Azioni</th>
</tr>
</thead>
<tbody>
<?php foreach ($teachers as $t): ?>
<tr>
<td>
<?php if ($t['profile_picture']): ?>
<img src="<?= htmlspecialchars($t['profile_picture']) ?>" class="rounded" width="50" height="50" style="object-fit:cover;">
<?php else: ?>
<i class="bx bx-user-circle fs-3 text-muted"></i>
<?php endif; ?>
</td>
<td><?= htmlspecialchars($t['first_name'] . ' ' . $t['last_name']) ?></td>
<td><?= htmlspecialchars($t['email']) ?></td>
<td><?= htmlspecialchars($t['phone'] ?: '—') ?></td>
<td>
<span class="badge bg-<?= $t['link_status'] === 'active' ? 'success' : 'warning' ?>">
<?= ucfirst($t['link_status']) ?>
</span>
</td>
<td>
<?php if ($t['can_edit']): ?>
<a href="teacher_profile.php?id=<?= $t['id'] ?>" class="btn btn-sm btn-warning"><i class="bx bx-edit"></i></a>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<!-- MODALE AGGIUNGI -->
<div class="modal fade" id="addTeacherModal" tabindex="-1" aria-labelledby="addTeacherLabel">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addTeacherLabel">Nuova insegnante</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<form action="add_teacher.php" method="POST" enctype="multipart/form-data" id="addTeacherForm">
<div class="modal-body">
<input type="hidden" name="school_id" value="<?= $school_id ?>">
<div class="row g-3">
<div class="col-md-6"><label>Nome *</label><input type="text" name="first_name" class="form-control" required></div>
<div class="col-md-6"><label>Cognome *</label><input type="text" name="last_name" class="form-control" required></div>
<div class="col-md-6"><label>Email *</label><input type="email" name="email" class="form-control" required></div>
<div class="col-md-6"><label>Telefono</label><input type="tel" name="phone" class="form-control"></div>
<div class="col-12"><label>Specializzazioni</label><textarea name="specializations" class="form-control" rows="2"></textarea></div>
<div class="col-12">
<label>Descrizione</label>
<div id="quill-add-editor" style="min-height:260px;"></div>
<input type="hidden" name="description" id="add-desc-hidden">
</div>
<div class="col-md-6"><label>Foto</label><input type="file" name="profile_picture" class="form-control" accept="image/*"></div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Annulla</button>
<button type="submit" class="btn btn-primary">Crea</button>
</div>
</form>
</div>
</div>
</div>
<!-- MODALE COLLEGAMENTO -->
<div class="modal fade" id="linkTeacherModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5>Collega insegnante esistente</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<form method="POST">
<div class="modal-body">
<input type="hidden" name="action" value="link_by_code">
<label>Codice univoco</label>
<input type="text" name="unique_code" class="form-control" required>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Annulla</button>
<button type="submit" class="btn btn-primary">Collega</button>
</div>
</form>
</div>
</div>
</div>
<?php include('include/footer.php'); ?>
</div>
<?php include('jsinclude.php'); ?>
<script>
// Quill per modale - inizializza DOPO apertura modale
const quillAdd = new Quill('#quill-add-editor', {
theme: 'snow',
modules: {
toolbar: [
['bold', 'italic', 'underline', 'strike'],
[{
'color': []
}, {
'background': []
}],
[{
'list': 'ordered'
}, {
'list': 'bullet'
}],
['link', 'clean']
]
}
});
document.getElementById('addTeacherModal').addEventListener('shown.bs.modal', function() {
quillAdd.update(); // Forza refresh dopo apertura
});
document.getElementById('addTeacherForm').addEventListener('submit', function(e) {
document.getElementById('add-desc-hidden').value = quillAdd.root.innerHTML;
});
</script>
</body>
</html>