-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannouncement_create.php
More file actions
127 lines (109 loc) · 4.61 KB
/
Copy pathannouncement_create.php
File metadata and controls
127 lines (109 loc) · 4.61 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
122
123
124
125
126
127
<?php
/**
* Create Announcement
* Only Admin and Teacher can create
*/
require_once 'config/config.php';
require_once 'config/db.php';
require_once 'includes/auth.php';
requireRoles([ROLE_ADMIN, ROLE_TEACHER]);
$error = '';
$success = '';
$user_id = $_SESSION['user_id'];
// Get classes if teacher
$classes = [];
if (isTeacher()) {
$classes_result = $mysqli->query(
"SELECT id, class_name FROM classes WHERE teacher_id = {$user_id} ORDER BY class_name"
);
$classes = $classes_result->fetch_all(MYSQLI_ASSOC);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$title = trim($_POST['title'] ?? '');
$message = trim($_POST['message'] ?? '');
$class_id = intval($_POST['class_id'] ?? 0);
if (empty($title) || empty($message)) {
$error = 'Title and message are required.';
} else {
$class_id_insert = $class_id > 0 ? $class_id : null;
$stmt = $mysqli->prepare("
INSERT INTO announcements (title, message, class_id, posted_by)
VALUES (?, ?, ?, ?)
");
$stmt->bind_param("ssii", $title, $message, $class_id_insert, $user_id);
if ($stmt->execute()) {
$success = 'Announcement posted successfully!';
$_POST = [];
} else {
$error = 'Error creating announcement. Please try again.';
}
$stmt->close();
}
}
$page_title = 'Create Announcement';
include 'includes/header.php';
include 'includes/nav.php';
?>
<div class="container mt-4">
<div class="row justify-content-center">
<div class="col-md-8">
<h2 class="mb-3">Create Announcement</h2>
<?php if ($error): ?>
<div class="alert alert-danger" role="alert">
<?php echo htmlspecialchars($error); ?>
</div>
<?php endif; ?>
<?php if ($success): ?>
<div class="alert alert-success" role="alert">
<?php echo htmlspecialchars($success); ?>
<a href="announcements_list.php" class="alert-link">View all announcements</a>
</div>
<?php endif; ?>
<div class="card">
<div class="card-body">
<form method="POST" action="">
<div class="mb-3">
<label for="title" class="form-label">Title</label>
<input
type="text"
class="form-control"
id="title"
name="title"
value="<?php echo htmlspecialchars($_POST['title'] ?? ''); ?>"
required
>
</div>
<div class="mb-3">
<label for="message" class="form-label">Message</label>
<textarea
class="form-control"
id="message"
name="message"
rows="6"
required
><?php echo htmlspecialchars($_POST['message'] ?? ''); ?></textarea>
</div>
<?php if (isTeacher()): ?>
<div class="mb-3">
<label for="class_id" class="form-label">Broadcast to Class (Optional)</label>
<select class="form-control" id="class_id" name="class_id">
<option value="">All Users</option>
<?php foreach ($classes as $class): ?>
<option value="<?php echo $class['id']; ?>" <?php echo (intval($_POST['class_id'] ?? 0) === $class['id']) ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($class['class_name']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<?php endif; ?>
<div class="d-grid gap-2">
<button type="submit" class="btn btn-primary">Post Announcement</button>
<a href="announcements_list.php" class="btn btn-secondary">Cancel</a>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<?php include 'includes/footer.php'; ?>