-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofessores.php
More file actions
121 lines (107 loc) · 5 KB
/
professores.php
File metadata and controls
121 lines (107 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
// professores.php - CRUD de Professores (com associação de disciplinas)
require 'db.php';
$acao = $_REQUEST['acao'] ?? 'listar';
$id = $_REQUEST['id'] ?? null;
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['salvar'])) {
$nome = $_POST['nome'];
$disciplinas_ids = $_POST['disciplinas'] ?? [];
if (empty($id)) {
$stmt = $pdo->prepare("INSERT INTO professores (nome) VALUES (?)");
$stmt->execute([$nome]);
$id = $pdo->lastInsertId(); // Pega o ID do professor recém-criado
} else {
$stmt = $pdo->prepare("UPDATE professores SET nome = ? WHERE id = ?");
$stmt->execute([$nome, $id]);
}
// Atualiza as associações
$stmt_delete = $pdo->prepare("DELETE FROM professor_disciplina WHERE id_professor = ?");
$stmt_delete->execute([$id]);
if (!empty($disciplinas_ids)) {
$stmt_insert = $pdo->prepare("INSERT INTO professor_disciplina (id_professor, id_disciplina) VALUES (?, ?)");
foreach ($disciplinas_ids as $id_disciplina) {
$stmt_insert->execute([$id, $id_disciplina]);
}
}
header("Location: professores.php");
exit;
}
if ($acao === 'deletar' && $id) {
$stmt = $pdo->prepare("DELETE FROM professores WHERE id = ?");
$stmt->execute([$id]);
header("Location: professores.php");
exit;
}
$professor = null;
$disciplinas_do_professor = [];
if ($acao === 'form' && $id) {
$stmt = $pdo->prepare("SELECT * FROM professores WHERE id = ?");
$stmt->execute([$id]);
$professor = $stmt->fetch(PDO::FETCH_ASSOC);
// Busca as disciplinas associadas a este professor
$stmt_disc = $pdo->prepare("SELECT id_disciplina FROM professor_disciplina WHERE id_professor = ?");
$stmt_disc->execute([$id]);
$disciplinas_do_professor = $stmt_disc->fetchAll(PDO::FETCH_COLUMN, 0);
}
$professores = $pdo->query("SELECT * FROM professores ORDER BY nome ASC")->fetchAll(PDO::FETCH_ASSOC);
$todas_disciplinas = $pdo->query("SELECT * FROM disciplinas ORDER BY nome ASC")->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<title>Gerenciar Professores</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
<h1 class="mb-4">Gerenciar Professores</h1>
<a href="index.php" class="btn btn-secondary mb-3">Voltar ao Menu</a>
<div class="card mb-4">
<div class="card-header"><?= $id ? 'Editar Professor' : 'Novo Professor' ?></div>
<div class="card-body">
<form action="professores.php" method="post">
<input type="hidden" name="id" value="<?= $professor['id'] ?? '' ?>">
<div class="mb-3">
<label for="nome" class="form-label">Nome do Professor</label>
<input type="text" class="form-control" id="nome" name="nome" value="<?= $professor['nome'] ?? '' ?>" required>
</div>
<div class="mb-3">
<label class="form-label">Disciplinas que leciona:</label>
<div class="p-3 border rounded" style="max-height: 200px; overflow-y: auto;">
<?php foreach ($todas_disciplinas as $disciplina): ?>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="disciplinas[]" value="<?= $disciplina['id'] ?>" id="disciplina_<?= $disciplina['id'] ?>"
<?= in_array($disciplina['id'], $disciplinas_do_professor) ? 'checked' : '' ?>>
<label class="form-check-label" for="disciplina_<?= $disciplina['id'] ?>">
<?= htmlspecialchars($disciplina['nome']) ?>
</label>
</div>
<?php endforeach; ?>
</div>
</div>
<button type="submit" name="salvar" class="btn btn-primary">Salvar</button>
<a href="professores.php" class="btn btn-light">Cancelar</a>
</form>
</div>
</div>
<h2 class="mt-5">Professores Cadastrados</h2>
<table class="table table-striped table-hover">
<thead> <tr><th>ID</th><th>Nome</th><th>Vezes que Fiscalizou</th><th>Ações</th></tr> </thead>
<tbody>
<?php foreach ($professores as $p): ?>
<tr>
<td><?= htmlspecialchars($p['id']) ?></td>
<td><?= htmlspecialchars($p['nome']) ?></td>
<td><?= htmlspecialchars($p['vezes_fiscalizou']) ?></td>
<td>
<a href="?acao=form&id=<?= $p['id'] ?>" class="btn btn-sm btn-warning">Editar</a>
<a href="?acao=deletar&id=<?= $p['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Tem certeza?')">Deletar</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</body>
</html>