327 lines
15 KiB
PHP
327 lines
15 KiB
PHP
<?php
|
|
// Forza la visualizzazione degli errori
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
include('include/headscript.php');
|
|
|
|
// Importa la libreria QR Code
|
|
require_once __DIR__ . '/../../vendor/autoload.php';
|
|
|
|
use Endroid\QrCode\Builder\Builder;
|
|
use Endroid\QrCode\Writer\PngWriter;
|
|
|
|
// Connessione al database
|
|
$dbHandler = DBHandlerSelect::getInstance();
|
|
$pdo = $dbHandler->getConnection();
|
|
|
|
// ID dell'utente loggato (assumiamo sia definito)
|
|
if (!isset($iduserlogin)) {
|
|
die("Errore: ID utente non definito.");
|
|
}
|
|
|
|
// Recupera i dati dell'insegnante
|
|
$stmt = $pdo->prepare("
|
|
SELECT t.*, u.first_name, u.last_name, u.email
|
|
FROM auth_users u
|
|
LEFT JOIN teachers t ON t.user_id = u.id
|
|
WHERE u.id = ?
|
|
");
|
|
$stmt->execute([$iduserlogin]);
|
|
$teacher = $stmt->fetch();
|
|
|
|
if (!$teacher) {
|
|
die("Errore: Utente non trovato.");
|
|
}
|
|
|
|
// Determina se è un nuovo insegnante
|
|
$is_new = !isset($teacher['id']);
|
|
if ($is_new) {
|
|
$teacher = [
|
|
'id' => null,
|
|
'user_id' => $iduserlogin,
|
|
'unique_code' => '',
|
|
'phone' => '',
|
|
'description' => '',
|
|
'specializations' => '',
|
|
'profile_picture' => '',
|
|
'status' => 'active',
|
|
'created_at' => '',
|
|
'updated_at' => '',
|
|
'first_name' => $teacher['first_name'],
|
|
'last_name' => $teacher['last_name'],
|
|
'email' => $teacher['email']
|
|
];
|
|
}
|
|
|
|
// Funzione per generare un codice univoco
|
|
function generateUniqueCode($pdo, $length = 16)
|
|
{
|
|
do {
|
|
$code = bin2hex(random_bytes($length / 2));
|
|
$stmt = $pdo->prepare("SELECT COUNT(*) FROM teachers WHERE unique_code = ?");
|
|
$stmt->execute([$code]);
|
|
$count = $stmt->fetchColumn();
|
|
} while ($count > 0);
|
|
return $code;
|
|
}
|
|
|
|
// Generazione del QR Code
|
|
$qr_code_path = null;
|
|
if (!$is_new) {
|
|
try {
|
|
$unique_code = $teacher['unique_code'];
|
|
if (empty($unique_code)) {
|
|
throw new Exception("Errore: unique_code è vuoto.");
|
|
}
|
|
|
|
$base_dir = __DIR__ . '/../../public/userarea/phototeachers/qrcodes/';
|
|
$qr_code_filename = "{$base_dir}{$iduserlogin}-{$unique_code}.png";
|
|
$qr_code_path = "phototeachers/qrcodes/{$iduserlogin}-{$unique_code}.png";
|
|
|
|
if (!file_exists($qr_code_filename)) {
|
|
if (!is_dir($base_dir)) {
|
|
mkdir($base_dir, 0755, true) or die("Errore: Impossibile creare la directory.");
|
|
}
|
|
if (!is_writable($base_dir)) {
|
|
die("Errore: La directory non è scrivibile.");
|
|
}
|
|
|
|
$builder = new Builder();
|
|
$result = $builder->build(
|
|
writer: new PngWriter(),
|
|
data: $unique_code,
|
|
size: 150,
|
|
margin: 10
|
|
);
|
|
$result->saveToFile($qr_code_filename);
|
|
}
|
|
} catch (Exception $e) {
|
|
$error = "Errore generazione QR Code: " . $e->getMessage();
|
|
error_log($error);
|
|
}
|
|
}
|
|
|
|
// Gestione del form
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$first_name = $_POST['first_name'];
|
|
$last_name = $_POST['last_name'];
|
|
$phone = $_POST['phone'] ?? null;
|
|
$description = $_POST['description'] ?? null;
|
|
$specializations = $_POST['specializations'] ?? null;
|
|
$status = $_POST['status'] === 'active' ? 'active' : 'inactive';
|
|
|
|
// Gestione del caricamento della foto
|
|
$profile_picture = $teacher['profile_picture'];
|
|
if (isset($_FILES['profile_picture']) && $_FILES['profile_picture']['error'] === UPLOAD_ERR_OK) {
|
|
$file = $_FILES['profile_picture'];
|
|
$timestamp = time();
|
|
$original_name = basename($file['name']);
|
|
$extension = strtolower(pathinfo($original_name, PATHINFO_EXTENSION));
|
|
$allowed_extensions = ['jpg', 'jpeg', 'png', 'gif'];
|
|
|
|
if (in_array($extension, $allowed_extensions)) {
|
|
$new_filename = "phototeachers/{$iduserlogin}-{$timestamp}-{$original_name}";
|
|
if (move_uploaded_file($file['tmp_name'], $new_filename)) {
|
|
$profile_picture = $new_filename;
|
|
if ($teacher['profile_picture'] && file_exists($teacher['profile_picture']) && !$is_new) {
|
|
unlink($teacher['profile_picture']);
|
|
}
|
|
} else {
|
|
$error = "Errore durante il caricamento della foto.";
|
|
}
|
|
} else {
|
|
$error = "Estensione del file non consentita. Usa JPG, JPEG, PNG o GIF.";
|
|
}
|
|
}
|
|
|
|
// Aggiorna auth_users
|
|
$stmt = $pdo->prepare("UPDATE auth_users SET first_name = ?, last_name = ? WHERE id = ?");
|
|
$stmt->execute([$first_name, $last_name, $iduserlogin]);
|
|
|
|
if ($is_new) {
|
|
$unique_code = generateUniqueCode($pdo);
|
|
$stmt = $pdo->prepare("
|
|
INSERT INTO teachers (user_id, unique_code, phone, description, specializations, profile_picture, status)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
");
|
|
$success = $stmt->execute([$iduserlogin, $unique_code, $phone, $description, $specializations, $profile_picture, $status]);
|
|
|
|
if ($success) {
|
|
$success_message = "Insegnante creato con successo!";
|
|
$stmt = $pdo->prepare("
|
|
SELECT t.*, u.first_name, u.last_name, u.email
|
|
FROM auth_users u
|
|
LEFT JOIN teachers t ON t.user_id = u.id
|
|
WHERE u.id = ?
|
|
");
|
|
$stmt->execute([$iduserlogin]);
|
|
$teacher = $stmt->fetch();
|
|
$is_new = false;
|
|
|
|
// Genera QR Code per il nuovo insegnante
|
|
try {
|
|
$base_dir = __DIR__ . '/../../public/phototeachers/qrcodes/';
|
|
$qr_code_filename = "{$base_dir}{$iduserlogin}-{$unique_code}.png";
|
|
$qr_code_path = "phototeachers/qrcodes/{$iduserlogin}-{$unique_code}.png";
|
|
|
|
if (!file_exists($qr_code_filename)) {
|
|
if (!is_dir($base_dir)) {
|
|
mkdir($base_dir, 0755, true) or die("Errore: Impossibile creare la directory.");
|
|
}
|
|
$builder = new Builder();
|
|
$result = $builder->build(
|
|
writer: new PngWriter(),
|
|
data: $unique_code,
|
|
size: 150,
|
|
margin: 10
|
|
);
|
|
$result->saveToFile($qr_code_filename);
|
|
}
|
|
} catch (Exception $e) {
|
|
$error = "Errore generazione QR Code: " . $e->getMessage();
|
|
error_log($error);
|
|
}
|
|
} else {
|
|
$error = "Errore durante la creazione dell'insegnante.";
|
|
}
|
|
} else {
|
|
$stmt = $pdo->prepare("
|
|
UPDATE teachers
|
|
SET phone = ?, description = ?, specializations = ?, profile_picture = ?, status = ?
|
|
WHERE user_id = ?
|
|
");
|
|
$success = $stmt->execute([$phone, $description, $specializations, $profile_picture, $status, $iduserlogin]);
|
|
|
|
if ($success) {
|
|
$success_message = "Dati aggiornati con successo!";
|
|
$stmt = $pdo->prepare("
|
|
SELECT t.*, u.first_name, u.last_name, u.email
|
|
FROM auth_users u
|
|
LEFT JOIN teachers t ON t.user_id = u.id
|
|
WHERE u.id = ?
|
|
");
|
|
$stmt->execute([$iduserlogin]);
|
|
$teacher = $stmt->fetch();
|
|
} else {
|
|
$error = "Errore durante l'aggiornamento dei dati.";
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
|
|
<!doctype html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<link rel="icon" href="assets/images/favicon-32x32.png" type="image/png" />
|
|
<?php include('cssinclude.php'); ?>
|
|
<?php include('siteinfo.php'); ?>
|
|
</head>
|
|
|
|
<body>
|
|
<div class="wrapper">
|
|
<?php include('include/navbar.php'); ?>
|
|
<?php include('include/topbar.php'); ?>
|
|
<div class="page-wrapper">
|
|
<div class="page-content">
|
|
<div class="card radius-10">
|
|
<div class="card-header">
|
|
<h6 class="mb-0"><?php echo $is_new ? 'Crea Profilo Insegnante' : 'Profilo Insegnante'; ?></h6>
|
|
</div>
|
|
<div class="card-body">
|
|
<?php if (isset($success_message)): ?>
|
|
<div class="alert alert-success" role="alert">
|
|
<?php echo $success_message; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
<?php if (isset($error)): ?>
|
|
<div class="alert alert-danger" role="alert">
|
|
<?php echo $error; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
<form method="POST" enctype="multipart/form-data">
|
|
<div class="row">
|
|
<div class="col-md-4 text-center">
|
|
<div class="mb-3">
|
|
<img src="<?php echo $teacher['profile_picture'] ? htmlspecialchars($teacher['profile_picture']) : 'phototeachers/ndphoto.png'; ?>"
|
|
alt="Foto Profilo" class="img-fluid rounded-circle" style="width: 150px; height: 150px; object-fit: cover;">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="profile_picture" class="form-label">Carica nuova foto</label>
|
|
<input type="file" class="form-control" id="profile_picture" name="profile_picture" accept="image/*">
|
|
</div>
|
|
<?php if (!$is_new && $qr_code_path): ?>
|
|
<div class="mb-3">
|
|
<label class="form-label">Codice Univoco</label>
|
|
<input type="text" class="form-control" value="<?php echo htmlspecialchars($teacher['unique_code']); ?>" readonly>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">QR Code</label><br>
|
|
<img src="<?php echo htmlspecialchars($qr_code_path); ?>" alt="QR Code" class="img-fluid" style="width: 150px; height: 150px;">
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
<div class="col-md-8">
|
|
<div class="row">
|
|
<div class="col-md-6 mb-3">
|
|
<label for="first_name" class="form-label">Nome</label>
|
|
<input type="text" class="form-control" id="first_name" name="first_name" value="<?php echo htmlspecialchars($teacher['first_name']); ?>" required>
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label for="last_name" class="form-label">Cognome</label>
|
|
<input type="text" class="form-control" id="last_name" name="last_name" value="<?php echo htmlspecialchars($teacher['last_name']); ?>" required>
|
|
</div>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="email" class="form-label">Email</label>
|
|
<input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($teacher['email']); ?>" readonly>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="phone" class="form-label">Telefono</label>
|
|
<input type="text" class="form-control" id="phone" name="phone" value="<?php echo htmlspecialchars($teacher['phone'] ?? ''); ?>">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="description" class="form-label">Descrizione</label>
|
|
<textarea class="form-control" id="description" name="description" rows="3"><?php echo htmlspecialchars($teacher['description'] ?? ''); ?></textarea>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="specializations" class="form-label">Specializzazioni</label>
|
|
<textarea class="form-control" id="specializations" name="specializations" rows="2"><?php echo htmlspecialchars($teacher['specializations'] ?? ''); ?></textarea>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="status" class="form-label">Stato</label>
|
|
<div class="form-check form-switch">
|
|
<input class="form-check-input" type="checkbox" id="status" name="status" value="active" <?php echo $teacher['status'] === 'active' ? 'checked' : ''; ?>>
|
|
<label class="form-check-label" for="status"><?php echo $teacher['status'] === 'active' ? 'Attivo' : 'Inattivo'; ?></label>
|
|
</div>
|
|
</div>
|
|
<?php if (!$is_new): ?>
|
|
<div class="mb-3">
|
|
<label class="form-label">Data Creazione</label>
|
|
<input type="text" class="form-control" value="<?php echo htmlspecialchars($teacher['created_at']); ?>" readonly>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Ultimo Aggiornamento</label>
|
|
<input type="text" class="form-control" value="<?php echo htmlspecialchars($teacher['updated_at']); ?>" readonly>
|
|
</div>
|
|
<?php endif; ?>
|
|
<button type="submit" class="btn btn-primary"><?php echo $is_new ? 'Crea Profilo' : 'Salva Modifiche'; ?></button>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="overlay toggle-icon"></div>
|
|
<a href="javaScript:;" class="back-to-top"><i class='bx bxs-up-arrow-alt'></i></a>
|
|
<?php include('include/footer.php'); ?>
|
|
</div>
|
|
<?php include('jsinclude.php'); ?>
|
|
</body>
|
|
|
|
</html>
|