-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zert.php
337 lines (284 loc) · 13.2 KB
/
zert.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Dashboard</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<!-- Top Bar Navbar -->
<header class="navbar-section">
<nav class="navbar navbar-expand-lg">
<div class="container-fluid">
<a class="navbar-brand" href="login.php">
Debating Tool
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<a class="nav-link" href="#contact">Contact</a>
</li>
<li class="nav-item">
<a class="nav-link" href="logout.php">Logout</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
<!-- Main Content -->
<main class="container mt-5">
<h1>Admin Dashboard</h1>
<p>Welcome, admin! The competition date is: <?php echo $competition_date;
ob_start();
session_start();
require_once('connection_pdo.php');
// Ensure only admins can access this page
if (!isset($_SESSION['username_admin'])) {
header("location:admin_login.php");
exit();
}
try {
$stmt = $connection_pdo->query("SELECT competition_date FROM dates WHERE id = 1");
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($row) {
$competition_date = date("d.m.Y", strtotime($row['competition_date']));
} else {
echo "Competition date not found.";
}
} catch (PDOException $e) {
echo "Error fetching competition date: " . $e->getMessage();
}
// Function to generate the ODT certificate with unique filenames and proper XML handling
function generate_odt_certificate($name, $school, $place, $league, $date) {
global $connection_pdo; // Ensure the connection is accessible
$st = $connection_pdo->query("SELECT name FROM host");
$rw = $st->fetch(PDO::FETCH_ASSOC);
$hostname = $rw['name']; // This is the school name
$template = 'vorlage.odt';
$cleanName = preg_replace('/\s+/', '_', $name); // Ensure no spaces in file names
$outputFile = 'certificates/urkunde_' . $cleanName . '_' . $place . '.odt'; // Unique file name using place and name
// Ensure the certificates directory exists
if (!file_exists('certificates')) {
mkdir('certificates', 0755, true);
}
// Copy template to create a new file for this certificate
if (!copy($template, $outputFile)) {
echo 'Failed to copy template.';
return false;
}
// Open the ODT file as a Zip archive
$zip = new ZipArchive;
if ($zip->open($outputFile) === true) {
// Extract content.xml
$content = $zip->getFromName('content.xml');
// Encode XML special characters to prevent XML format errors
$name = htmlspecialchars($name, ENT_XML1, 'UTF-8');
$school = htmlspecialchars($school, ENT_XML1, 'UTF-8');
$place = htmlspecialchars($place, ENT_XML1, 'UTF-8');
$league = htmlspecialchars($league, ENT_XML1, 'UTF-8');
$date = htmlspecialchars($date, ENT_XML1, 'UTF-8');
// Add custom styles for Bold and Underline text in the ODT file
$automaticStyles = '
<style:style style:name="Bold" style:family="text">
<style:text-properties fo:font-weight="bold"/>
</style:style>
<style:style style:name="Underline" style:family="text">
<style:text-properties style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color"/>
</style:style>
';
// Inject the automatic styles into the content.xml
$content = preg_replace('/(<office:automatic-styles[^>]*>)/', '$1' . $automaticStyles, $content);
// Replace placeholders with actual values
$content = str_replace('{{Name}}', '<text:span text:style-name="Underline">' . $name . '</text:span>', $content);
$content = str_replace('{{School}}', '<text:span text:style-name="Underline">' . $school . '</text:span>', $content);
$content = str_replace('{{PLACE}}', '<text:span text:style-name="Bold">' . $place . '</text:span>', $content);
$content = str_replace('{{League}}', $league, $content);
$content = str_replace('{{Date}}', $date, $content);
$content = str_replace('{{SCHOOLNAME}}', '<text:span text:style-name="Bold">' . $hostname . '</text:span>', $content);
// Write modified content.xml back into the ODT file
$zip->addFromString('content.xml', $content);
$zip->close();
return $outputFile; // Return the path to the generated certificate
} else {
echo 'Failed to open template file.';
return false;
}
}
//generate_odt_certificate("N", "S", "P", "jr", $competition_date);
// Fetch all teams
function fetch_teams($connection_pdo) {
$teamsStmt = $connection_pdo->query("SELECT * FROM teams ORDER BY team_wins DESC, team_points DESC");
return $teamsStmt->fetchAll(PDO::FETCH_ASSOC);
}
// Fetch all players and group them by league
function fetch_players_by_league($connection_pdo, $teams) {
$playersStmt = $connection_pdo->query("SELECT * FROM players");
$players = $playersStmt->fetchAll(PDO::FETCH_ASSOC);
$leagues = ['Sr.' => [], 'Jr.' => []];
foreach ($players as $player) {
foreach ($teams as $team) {
if ($player['TeamID'] == $team['Team_ID']) {
if (strpos($team['team_name'], 'Sr.') !== false) {
$leagues['Sr.'][] = ['player' => $player, 'team' => $team];
} elseif (strpos($team['team_name'], 'Jr.') !== false) {
$leagues['Jr.'][] = ['player' => $player, 'team' => $team];
}
break;
}
}
}
return $leagues;
}
// Get top 3 teams from a league
function get_top_teams($teams, $league) {
$top_teams = [];
foreach ($teams as $team) {
if (strpos($team['team_name'], $league) !== false) {
$top_teams[] = $team;
}
}
usort($top_teams, function($a, $b) {
return $b['team_wins'] - $a['team_wins'];
});
return array_slice($top_teams, 0, 3);
}
function get_best_speakers($players) {
usort($players, function($a, $b) {
// Check if player_Max_points exists before using it
$maxPointsA = isset($a['player_Max_points']) ? $a['player_Max_points'] : 0;
$maxPointsB = isset($b['player_Max_points']) ? $b['player_Max_points'] : 0;
return $maxPointsB - $maxPointsA;
});
return array_slice($players, 0, 3);
}
// Fetch all teams
$teams = fetch_teams($connection_pdo);
// Fetch all players and divide them into Sr. and Jr. leagues
$leagues = fetch_players_by_league($connection_pdo, $teams);
// Get Top 3 teams for Senior League
$top_teams_sr = get_top_teams($teams, 'Sr.');
$top_teams_jr = get_top_teams($teams, 'Jr.');
// Fetch all teams and players
$teams = fetch_teams($connection_pdo);
$leagues = fetch_players_by_league($connection_pdo, $teams);
// Fetch top best speakers (1st, 2nd, 3rd)
$best_speakers_sr = get_best_speakers($leagues['Sr.']);
$best_speakers_jr = get_best_speakers($leagues['Jr.']);
// Function to display teams, players, and generate certificates
function display_top_teams($leagues, $top_teams, $league_name, $competition_date) {
$places = ['FIRST PLACE', 'SECOND PLACE', 'THIRD PLACE'];
for ($i = 0; $i < count($top_teams); $i++) {
echo "<h3>{$places[$i]} Place Team ({$league_name})</h3>";
echo "<table border='1'>";
echo "<tr><th>Player Name</th><th>Team School</th><th>Points</th><th>Max Points</th><th>Wins</th><th>Top 3 Team</th><th>Download Certificate</th></tr>";
foreach ($leagues as $entry) {
$player = $entry['player'];
$team = $entry['team'];
if ($team == $top_teams[$i]) {
// Generate ODT certificate and get the file path
$place = strtoupper($places[$i]);
$certificate_file = generate_odt_certificate($player['player_name'], $team['team_school'], $place, strtolower($league_name), $competition_date);
echo "<tr>";
echo "<td>{$player['player_name']}</td>";
echo "<td>{$team['team_school']}</td>";
echo "<td>{$player['player_points']}</td>";
echo "<td>" . (isset($player['player_Max_points']) ? $player['player_Max_points'] : 'N/A') . "</td>";
echo "<td>{$player['player_wins']}</td>";
echo "<td>Yes</td>";
if ($certificate_file) {
echo "<td><a href='$certificate_file' download>Download Certificate</a></td>";
} else {
echo "<td>Failed to generate certificate</td>";
}
echo "</tr>";
}
}
echo "</table>";
}
}
// Display Senior League top teams
display_top_teams($leagues['Sr.'], $top_teams_sr, 'senior', $competition_date);
// Display Junior League top teams
display_top_teams($leagues['Jr.'], $top_teams_jr, 'junior', $competition_date);
function display_best_speaker_table($best_speakers, $league_name, $competition_date) {
// Sort the speakers by player_Max_points in descending order
usort($best_speakers, function($a, $b) {
return $b['player']['player_Max_points'] - $a['player']['player_Max_points'];
});
echo "<h3>Top 3 Best Speakers ($league_name)</h3>";
echo "<table border='1'>";
echo "<tr><th>Rank</th><th>Player Name</th><th>Team School</th><th>Points</th><th>Download Best Speaker Certificate</th></tr>";
$rank_names = ['1st Best Speaker', '2nd Best Speaker', '3rd Best Speaker'];
// Loop through the sorted list, but only display top 3
for ($i = 0; $i < 3 && $i < count($best_speakers); $i++) {
$entry = $best_speakers[$i];
$player = $entry['player'];
$team = $entry['team'];
// Generate best speaker certificate with rank (1st, 2nd, 3rd)
$certificate_file = generate_odt_certificate($player['player_name'], $team['team_school'], $rank_names[$i], $league_name, $competition_date);
echo "<tr>";
echo "<td>{$rank_names[$i]}</td>";
echo "<td>{$player['player_name']}</td>";
echo "<td>{$team['team_school']}</td>";
echo "<td>{$player['player_Max_points']}</td>";
if ($certificate_file) {
echo "<td><a href='$certificate_file' download>Download Certificate</a></td>";
} else {
echo "<td>Failed to generate certificate</td>";
}
echo "</tr>";
}
echo "</table>";
}
// Display Best Speakers for both Senior and Junior leagues
display_best_speaker_table($best_speakers_sr, "senior", $competition_date);
display_best_speaker_table($best_speakers_jr, "junior", $competition_date);
ob_end_flush();
?></p>
<!-- Additional admin content here -->
</main>
<!-- Footer -->
<footer>
<div class="container">
<div class="row">
<div class="col-lg-4 col-md-12 col-sm-12">
<p class="logo">
Debating Tool
</p>
</div>
<div class="col-lg-5 col-md-12 col-sm-12">
<ul class="d-flex">
<li><a href="admin_login.php">Home</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="admin_login.php">Admin</a></li>
</ul>
</div>
<div class="col-lg-2 col-md-12 col-sm-12">
<p>©
<a href="https://github.com/Cybersky4" target="_blank" style="color: inherit; text-decoration: underline;">2024_Cyber</a>,
<a href="https://github.com/silverhadch" target="_blank" style="color: inherit; text-decoration: underline;">Hadi</a>
</p>
</div>
<div class="col-lg-1 col-md-12 col-sm-12">
<!-- Back to top -->
<a href="#" class="back-to-top d-flex align-items-center justify-content-center">
<i class="bi bi-arrow-up-short"></i>
</a>
</div>
</div>
</div>
</footer>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz"
crossorigin="anonymous"></script>
</body>
</html>