diff --git a/code/view_sessions.php b/code/view_sessions.php new file mode 100644 index 0000000000000000000000000000000000000000..f5a11f9bb98941adb0bb94f1fe88dcab0d173eb9 --- /dev/null +++ b/code/view_sessions.php @@ -0,0 +1,106 @@ +<?php +ini_set('display_errors', 1); +ini_set('display_startup_errors', 1); +error_reporting(E_ALL); +include 'config.php'; +session_start(); + +if (!isset($_SESSION['user']) || $_SESSION['user']['role'] !== 'student') { + header('Location: login.php'); + exit; +} + +$user = $_SESSION['user']; + +// Supprimer une inscription si une requête POST est envoyée +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['withdraw_session_id'])) { + $session_id = $conn->real_escape_string($_POST['withdraw_session_id']); + $student_id = $_SESSION['user']['id']; + + // Supprimer l'inscription + $delete_registration = $conn->prepare("DELETE FROM SessionStudent WHERE session_id = ? AND student_id = ?"); + $delete_registration->bind_param("ii", $session_id, $student_id); + if ($delete_registration->execute()) { + $success = "Successfully withdrawn from the session!"; + } else { + $error = "Error withdrawing from the session: " . $conn->error; + } + $delete_registration->close(); +} + +// Récupérer les sessions auxquelles l'étudiant est inscrit +$sessions = []; +$sql = " + SELECT s.id, s.subject, s.start_time, s.end_time, u.first_name, u.last_name + FROM SessionStudent ss + JOIN Session s ON ss.session_id = s.id + JOIN User u ON s.tutor_id = u.id + WHERE ss.student_id = ? +"; +$stmt = $conn->prepare($sql); +$stmt->bind_param("i", $_SESSION['user']['id']); +$stmt->execute(); +$result = $stmt->get_result(); +while ($row = $result->fetch_assoc()) { + $sessions[] = $row; +} +$stmt->close(); +?> +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>My Sessions</title> + <link rel="stylesheet" href="style.css"> +</head> +<body> + <?php include 'sidebar.php'; ?> + + <div class="main-content"> + <div class="view-session"> + <h1>Mes sessions</h1> + + <?php if (isset($success)): ?> + <p class="success"><?= htmlspecialchars($success) ?></p> + <?php endif; ?> + + <?php if (isset($error)): ?> + <p class="error"><?= htmlspecialchars($error) ?></p> + <?php endif; ?> + + <?php if (!empty($sessions)): ?> + <table border="1" cellpadding="10" cellspacing="0"> + <thead> + <tr> + <th>Matière</th> + <th>Date de début</th> + <th>Date de fin</th> + <th>Tuteur</th> + <th>Actions</th> + </tr> + </thead> + <tbody> + <?php foreach ($sessions as $session): ?> + <tr> + <td><?= htmlspecialchars($session['subject']) ?></td> + <td><?= htmlspecialchars(date('d/m/Y H:i', strtotime($session['start_time']))) ?></td> + <td><?= htmlspecialchars(date('H:i', strtotime($session['end_time']))) ?></td> + <td><?= htmlspecialchars($session['first_name']) . ' ' . htmlspecialchars($session['last_name']) ?></td> + <td> + <form method="POST" style="display:inline;"> + <input type="hidden" name="withdraw_session_id" value="<?= htmlspecialchars($session['id']) ?>"> + <button type="submit" onclick="return confirm('Are you sure you want to withdraw from this session?')">Désincrire</button> + </form> + </td> + </tr> + <?php endforeach; ?> + </tbody> + </table> + <?php else: ?> + <h1>Vous n'etes pas encore inscrit(e) à aucune session</h1> + <?php endif; ?> + </div> + </div> +</body> +</html>