-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_moderatorok.php
executable file
·163 lines (150 loc) · 5.62 KB
/
add_moderatorok.php
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
//add_moderator_championship.php
include 'header.php';
require_once 'conn.php'; // Include database connection settings
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Fetch tournaments
$tournamentQuery = "
SELECT t.id, t.name AS tournament_name,
IFNULL(GROUP_CONCAT(u.username SEPARATOR ', '), '') AS moderators
FROM tournaments t
LEFT JOIN tournament_moderators tm ON t.id = tm.tournament_id
LEFT JOIN users u ON tm.user_id = u.id
GROUP BY t.id
ORDER BY t.name
";
$tournamentResult = $conn->query($tournamentQuery);
if (!$tournamentResult) {
die("Error fetching tournaments: " . $conn->error);
}
// Fetch users
$userQuery = "SELECT id, username FROM users ORDER BY username";
$userResult = $conn->query($userQuery);
if (!$userResult) {
die("Error fetching users: " . $conn->error);
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['delete'])) {
$tournamentId = $_POST['tournament_id'];
$moderatorId = $_POST['moderator_id'];
$deleteQuery = "DELETE FROM tournament_moderators WHERE tournament_id = ? AND user_id = ?";
$stmt = $conn->prepare($deleteQuery);
$stmt->bind_param('ii', $tournamentId, $moderatorId);
if ($stmt->execute()) {
echo "<p style='color: green;'>Moderator removed from the tournament.</p>";
} else {
echo "<p style='color: red;'>Error removing moderator: " . htmlspecialchars($stmt->error) . "</p>";
}
$stmt->close();
} elseif (isset($_POST['edit'])) {
$tournamentId = $_POST['tournament_id'];
$moderatorId = $_POST['moderator_id'];
$insertQuery = "INSERT INTO tournament_moderators (tournament_id, user_id) VALUES (?, ?) ON DUPLICATE KEY UPDATE user_id = user_id";
$stmt = $conn->prepare($insertQuery);
$stmt->bind_param('ii', $tournamentId, $moderatorId);
if ($stmt->execute()) {
echo "<p style='color: green;'>Moderator added to the tournament.</p>";
} else {
echo "<p style='color: red;'>Error adding moderator: " . htmlspecialchars($stmt->error) . "</p>";
}
$stmt->close();
}
// Refresh the page to reflect changes
header("Location: " . $_SERVER['PHP_SELF']);
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Assign Moderator</title>
<style>
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
table, th, td {
border: 1px solid #ddd;
}
th, td {
padding: 10px;
text-align: left;
}
th {
background-color: #f4f4f4;
}
</style>
</head>
<body>
<h1>Assign a Moderator to a Tournament</h1>
<form action="" method="POST">
<label for="tournament_id">Select Tournament:</label>
<select name="tournament_id" id="tournament_id" required>
<option value="">-- Select Tournament --</option>
<?php $tournamentResult->data_seek(0); while ($tournament = $tournamentResult->fetch_assoc()) { ?>
<option value="<?php echo $tournament['id']; ?>">
<?php echo htmlspecialchars($tournament['tournament_name']); ?>
</option>
<?php } ?>
</select>
<br><br>
<label for="moderator_id">Select Moderator:</label>
<select name="moderator_id" id="moderator_id" required>
<option value="">-- Select User --</option>
<?php while ($user = $userResult->fetch_assoc()) { ?>
<option value="<?php echo $user['id']; ?>">
<?php echo htmlspecialchars($user['username']); ?>
</option>
<?php } ?>
</select>
<br><br>
<button type="submit" name="edit">Add Moderator</button>
</form>
<h2>Tournaments and Moderators</h2>
<table>
<thead>
<tr>
<th>Tournament Name</th>
<th>Moderators</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
$tournamentResult->data_seek(0);
while ($row = $tournamentResult->fetch_assoc()) {
echo "<tr>";
echo "<td>" . htmlspecialchars($row['tournament_name']) . "</td>";
echo "<td>" . htmlspecialchars($row['moderators']) . "</td>";
echo "<td>";
$moderators = $row['moderators'] !== '' ? explode(', ', $row['moderators']) : [];
foreach ($moderators as $moderator) {
$moderatorIdQuery = "SELECT id FROM users WHERE username = ?";
$stmt = $conn->prepare($moderatorIdQuery);
$stmt->bind_param('s', $moderator);
$stmt->execute();
$stmt->bind_result($moderatorId);
$stmt->fetch();
$stmt->close();
echo "<form action='' method='POST' style='display:inline;'>";
echo "<input type='hidden' name='tournament_id' value='" . $row['id'] . "'>";
echo "<input type='hidden' name='moderator_id' value='" . $moderatorId . "'>";
echo "<button type='submit' name='delete'>Remove " . htmlspecialchars($moderator) . "</button>";
echo "</form> ";
}
echo "</td>";
echo "</tr>";
}
?>
</tbody>
</table>
</body>
</html>
<?php
$conn->close();
?>