getConnection(); if (!isset($iduserlogin)) { die("Errore: ID utente non definito."); } /** * QR helper compatible with older/newer Endroid versions (best-effort) */ function writeQrPng($text, $filename, $size = 150, $margin = 10) { // Your installed version seems to require text in constructor $qrCode = new \Endroid\QrCode\QrCode($text); if (method_exists($qrCode, 'setSize')) { $qrCode->setSize($size); } elseif (method_exists($qrCode, 'setModuleSize')) { $module = max(3, (int)round($size / 25)); $qrCode->setModuleSize($module); } if (method_exists($qrCode, 'setMargin')) { $qrCode->setMargin($margin); } elseif (method_exists($qrCode, 'setPadding')) { $qrCode->setPadding($margin); } $writer = new \Endroid\QrCode\Writer\PngWriter(); if (method_exists($writer, 'writeFile')) { $writer->writeFile($qrCode, $filename); } else { $result = $writer->write($qrCode); if (is_object($result) && method_exists($result, 'saveToFile')) { $result->saveToFile($filename); } else { file_put_contents($filename, (string)$result); } } } 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]); } while ($stmt->fetchColumn() > 0); return $code; } /** * Detect view mode * - Owner view: teacher_page.php?id=TEACHER_ID (teachers.id) * - Teacher self view: teacher_page.php */ $teacher_id = (int)($_GET['id'] ?? 0); $is_owner_view = ($teacher_id > 0); $success_message = $error = null; /** * 1) LOAD teacher row */ if ($is_owner_view) { // OWNER VIEW: load teacher by teachers.id only if owner has rights $stmt = $pdo->prepare(" SELECT t.*, u.first_name, u.last_name, u.email FROM teachers t JOIN auth_users u ON t.user_id = u.id JOIN teacher_schools ts ON ts.teacher_id = t.id JOIN schools s ON s.id = ts.school_id WHERE t.id = ? AND s.owner_id = ? LIMIT 1 "); $stmt->execute([$teacher_id, $iduserlogin]); $teacher = $stmt->fetch(PDO::FETCH_ASSOC); if (!$teacher) { die("Errore: insegnante non trovata o non hai permessi."); } } else { // TEACHER SELF VIEW: load by logged user $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 = ? LIMIT 1 "); $stmt->execute([$iduserlogin]); $teacher = $stmt->fetch(PDO::FETCH_ASSOC); // If not exists in teachers, CREATE IT before showing the form (auto-create) if (empty($teacher['id'])) { // ✅ SOLO QUI: quando auto-crei il profilo teacher (SELF VIEW), aggiungi created_by = iduserlogin $unique_code = generateUniqueCode($pdo); $stmtIns = $pdo->prepare(" INSERT INTO teachers (user_id, unique_code, phone, description, specializations, profile_picture, status, created_by) VALUES (?, ?, NULL, '', '', '', 'active', ?) "); $ok = $stmtIns->execute([$iduserlogin, $unique_code, $iduserlogin]); if (!$ok) { die("Errore: impossibile creare il profilo insegnante."); } // Reload teacher after insert $stmt = $pdo->prepare(" SELECT t.*, u.first_name, u.last_name, u.email FROM teachers t JOIN auth_users u ON t.user_id = u.id WHERE t.user_id = ? LIMIT 1 "); $stmt->execute([$iduserlogin]); $teacher = $stmt->fetch(PDO::FETCH_ASSOC); } } /** * Now teacher MUST exist (in owner view and in self view due to auto-create) */ $is_new = empty($teacher['id']); // should be false at this point /** * 2) HANDLE POST (save) */ if ($_SERVER['REQUEST_METHOD'] === 'POST') { // target user is the teacher being edited $target_user_id = $is_owner_view ? (int)$teacher['user_id'] : (int)$iduserlogin; $first_name = trim($_POST['first_name'] ?? ''); $last_name = trim($_POST['last_name'] ?? ''); $phone = trim($_POST['phone'] ?? ''); $description = trim($_POST['description'] ?? ''); $specializations = trim($_POST['specializations'] ?? ''); $status = (($_POST['status'] ?? 'active') === 'active') ? 'active' : 'inactive'; // Update auth_users names for the target teacher $stmt = $pdo->prepare("UPDATE auth_users SET first_name = ?, last_name = ? WHERE id = ?"); $stmt->execute([$first_name, $last_name, $target_user_id]); // Photo upload (use target user id in filename) $profile_picture = $teacher['profile_picture'] ?? ''; if (!empty($_FILES['profile_picture']['name']) && $_FILES['profile_picture']['error'] === UPLOAD_ERR_OK) { $ext = strtolower(pathinfo($_FILES['profile_picture']['name'], PATHINFO_EXTENSION)); if (in_array($ext, ['jpg', 'jpeg', 'png', 'gif'])) { $new_name = "phototeachers/{$target_user_id}-" . time() . "-profile.$ext"; if (move_uploaded_file($_FILES['profile_picture']['tmp_name'], $new_name)) { if ($profile_picture && file_exists($profile_picture)) { @unlink($profile_picture); } $profile_picture = $new_name; } else { $error = "Errore caricamento foto."; } } else { $error = "Solo JPG, PNG, GIF ammessi."; } } if (!$error) { // Update teachers row (always exists at this point) $stmt = $pdo->prepare(" UPDATE teachers SET phone = ?, description = ?, specializations = ?, profile_picture = ?, status = ? WHERE user_id = ? "); $success = $stmt->execute([ $phone ?: null, $description, $specializations, $profile_picture, $status, $target_user_id ]); if ($success) { $success_message = "Dati aggiornati!"; // Reload teacher (with correct target user) $stmt = $pdo->prepare(" SELECT t.*, u.first_name, u.last_name, u.email FROM teachers t JOIN auth_users u ON t.user_id = u.id WHERE t.user_id = ? LIMIT 1 "); $stmt->execute([$target_user_id]); $teacher = $stmt->fetch(PDO::FETCH_ASSOC); } else { $error = "Errore aggiornamento."; } } } /** * 3) QR generation (after teacher is loaded) */ $qr_code_path = null; if (!empty($teacher['unique_code'])) { try { $unique_code = $teacher['unique_code']; // IMPORTANT: file name uses target user_id (teacher user), not owner id $qr_user_id = (int)$teacher['user_id']; $base_dir = __DIR__ . '/../../public/phototeachers/qrcodes/'; $qr_filename = "{$base_dir}{$qr_user_id}-{$unique_code}.png"; $qr_code_path = "phototeachers/qrcodes/{$qr_user_id}-{$unique_code}.png"; if (!file_exists($qr_filename)) { if (!is_dir($base_dir)) mkdir($base_dir, 0755, true); writeQrPng($unique_code, $qr_filename, 150, 10); } } catch (Exception $e) { error_log("Errore QR: " . $e->getMessage()); } } ?>