forked from KaylaFriend/S-STEM-Valloons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashboard.php
392 lines (367 loc) · 10.3 KB
/
dashboard.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
session_start();
include('config.php');
$gamename = $topic = NULL;
$game_name_err = $badInsert = "";
if (isset($_POST['create-game']) && !empty($_POST['create-game']))
{
if (isset($_POST['game-name']) && !empty($_POST['game-name'])) {
$gamename = mysqli_real_escape_string($link, $_POST['game-name']);
} else {
$game_name_err = "The game requires a name.";
}
if (isset($_POST['topic']) && !empty($_POST['topic'])) {
$topic = mysqli_real_escape_string($link, $_POST['topic']);
} else {
$topic = NULL;
}
if (isset($gamename) && empty($game_name_err)) {
$insertGame = "INSERT INTO game(game_name, topic, teacher_id) VALUES(?, ?, ?);";
echo $gamename . ' ' . $numq . ' ' . $_SESSION['user_id'];
if ($insertGPrep = mysqli_prepare($link, $insertGame)) {
$insertGPrep->bind_param("ssi", $gamename, $topic, $_SESSION['user_id']);
if (mysqli_stmt_execute($insertGPrep)) {
$getGameID = "SELECT game_id FROM game WHERE game_name=? AND topic=? AND teacher_id=?;";
if ($getGID = mysqli_prepare($link, $getGameID)) {
$getGID->bind_param("ssi", $gamename, $topic, $_SESSION['user_id']);
if (mysqli_stmt_execute($getGID)) {
$gameID = mysqli_stmt_get_result($getGID);
if (mysqli_num_rows($gameID) == 1)
{
$_SESSION['game_id'] = mysqli_fetch_assoc($gameID)['game_id'];
header("location: edit-game.php");
exit;
} else {
$badInsert = "Too many similar games. Please edit the game by clicking the edit button to the left.";
}
} else {
$badInsert = "Similar games exist. Please find your game in the list to the left.";
}
} else {
$badInsert = "Something went wrong. Please find your game in the list to the left.";
}
} else {
$badInsert = "Something went wrong. Please try again later.";
}
} else {
$badInsert = "Something went wrong. Please contact the system administrator.";
}
} else {
$badInsert = "Your form is missing data.";
}
}
if (isset($_POST['approve-teacher']) && !empty($_POST['approve-teacher'])) {
$teacherID = $_POST['teacher-id'];
$updateTeacher = "UPDATE user SET verified=1 WHERE user_id=?;";
if ($updatePrep = mysqli_prepare($link, $updateTeacher)) {
$updatePrep->bind_param("i", $teacherID);
if (mysqli_stmt_execute($updatePrep)) {
header("Refresh:0");
} else {
echo 'Something went wrong.';
}
} else {
echo 'The preparation did not complete.';
}
}
function show_games_made() {
global $link;
echo ('
<div class="row">
<h2>Games</h2>
</div>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th class="col">Name</th>
<th class="col">Topic</th>
<th class="col">Created</th>
<th class="col"></th>
</tr>
</thead>
<tbody>
');
$getGames = "SELECT * FROM game WHERE teacher_id=? ORDER BY game_id DESC;";
if ($getGamesPrep = mysqli_prepare($link, $getGames))
{
$getGamesPrep->bind_param("i", $_SESSION['user_id']);
if (mysqli_stmt_execute($getGamesPrep)) {
$games = mysqli_stmt_get_result($getGamesPrep);
while ($row = mysqli_fetch_assoc($games)) {
echo ('
<tr>
<td class="col">' . stripslashes($row['game_name']) . '</td>
<td class="col">' . stripslashes($row['topic']) . '</td>
<td class="col">' . $row['date_created'] . '</td>
<td class="col">
<form action="edit-game.php" method="POST">
<input type="hidden" name="game-id" value="' . $row['game_id'] . '">
<input type="submit" class="btn btn-primary" name="edit-game" value="Edit">
</form>
</td>
</tr>
');
}
}
}
echo ('
</tbody>
</table>
');
}
function new_game_form() {
global $game_name_err, $num_q_err, $badInsert, $gamename, $numq;
echo('
<div class="row">
<h2>Create Game</h2>
</div>
<span>' . $badInsert . '</span>
<form method="POST" action="dashboard.php">
<div class="row mb-3">
<div class="col">
<label for="game-name" class="form-label">Game Name</label>
<input type="text" name="game-name" class="form-control
');
if (!empty($game_name_err)) echo 'is-invalid';
echo('
" id="game-name" value="' . $gamename . '" required>
<span class="invalid-feedback">
');
if (!empty($game_name_err))
echo $game_name_err;
echo('
</span>
</div>
<div class="col">
<label for="topic" class="form-label">Topic</label>
<input type="text" name="topic" class="form-control" id="topic" value="' . $topic . '">
</div>
</div>
<input type="submit" class="btn btn-primary" name="create-game" value="Create Game">
</form>
');
}
function approve_teachers() {
global $link;
echo ('
<div class="row">
<h2>Teacher Approval</h2>
</div>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th class="col">Name</th>
<th class="col">Username</th>
<th class="col">Email</th>
<th class="col">URL</th>
<th class="col"></th>
</tr>
</thead>
<tbody>
');
$getTeacherList = "SELECT * FROM user WHERE ut_id=2 AND verified=0;";
if ($getTPrep = mysqli_prepare($link, $getTeacherList)) {
if (mysqli_stmt_execute($getTPrep)) {
$teachers = mysqli_stmt_get_result($getTPrep);
while ($row = mysqli_fetch_assoc($teachers)) {
echo('
<tr>
<td>' . $row['first_name'] . ' ' . $row['last_name'] . '</td>
<td>' . $row['username'] . '</td>
<td>' . $row['email'] . '</td>
<td><a href="' . $row['teacher_url'] . '" target="_blank">' . $row['teacher_url'] . '</a></td>
<td>
<form method="POST" action="dashboard.php">
<input type="hidden" name="teacher-id" value="' . $row['user_id'] . '">
<input type="submit" name="approve-teacher" class="btn btn-primary" value="Approve">
</form>
</td>
</tr>
');
}
}
}
echo('
</tbody>
</table>
');
}
function not_verified() {
echo '<h2 class="center-align">Sorry, but your account has not been verified yet.</h2>';
}
function show_games_played() {
global $link;
echo ('
<div class="row">
<h2>Games Played</h2>
</div>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th class="col">Name</th>
<th class="col">Topic</th>
<th class="col">Score</th>
<th class="col">Date Finished</th>
</tr>
</thead>
<tbody>
');
$getGamesPlayed = "SELECT sg.game_id, sg.score, sg.date_finished, g.game_name, g.topic FROM student_game AS sg JOIN game AS g ON sg.game_id=g.game_id WHERE sg.student_id=? ORDER BY sg.date_finished DESC;";
if ($getGP = mysqli_prepare($link, $getGamesPlayed)) {
$getGP->bind_param("i", $_SESSION['user_id']);
if (mysqli_stmt_execute($getGP)) {
$games = mysqli_stmt_get_result($getGP);
while ($row = mysqli_fetch_assoc($games)) {
echo ('
<tr>
<td>' . stripslashes($row['game_name']) . '</td>
<td>' . stripslashes($row['topic']) . '</td>
<td>' . $row['score'] . '</td>
<td>' . $row['date_finished'] . '</td>
</tr>
');
}
} else echo('<tr></tr>');
}
echo ('
</tbody>
</table>
');
}
function show_public_games() {
global $link;
echo ('
<div class="row">
<div class="col">
<h2>Playable Games</h2>
</div>
<div class="col float-right">
<input type="text" id="game-search" onkeyup="searchGames()" placeholder="Search for a Game..." class="form-control">
</div>
</div>
<table class="table table-striped table-bordered table-hover" id="games">
<thead>
<tr>
<th class="col">Name</th>
<th class="col">Topic</th>
<th class="col"></th>
</tr>
</thead>
<tbody>
');
$getGames = "SELECT * FROM game WHERE published=1 ORDER BY date_created DESC;";
if ($getGamesPrep = mysqli_prepare($link, $getGames)) {
if (mysqli_stmt_execute($getGamesPrep)) {
$games = mysqli_stmt_get_result($getGamesPrep);
while ($row = mysqli_fetch_assoc($games)) {
echo ('
<tr>
<td class="col">' . stripslashes($row['game_name']) . '</td>
<td class="col">' . stripslashes($row['topic']) . '</td>
<td class="col">
<form action="game.php" method="POST">
<input type="hidden" name="game-id" value="' . $row['game_id'] . '">
<input type="submit" class="btn btn-primary" name="play-game" value="Play">
</form>
</td>
</tr>
');
}
}
}
echo ('
</tbody>
</table>
');
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Valloons</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style/style.css">
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<script src="bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
</head>
<body>
<?php
include('header.php');
?>
<div class="container">
<h1 class="center-align">Welcome, <?php echo($_SESSION['username']); ?></h1>
<?php
if ($_SESSION['ut_id'] == 1) {
echo('<article>');
approve_teachers();
echo('</article>');
}
if ($_SESSION['loggedin'] == true) {
if (($_SESSION['ut_id'] == 2 && $_SESSION['verified'] == 1) || $_SESSION['ut_id'] == 1)
{
echo('
<div class="row ex-space">
<div class="col">
');
show_games_made();
echo('
</div>
<div class="col">
');
new_game_form();
echo('
</div>
</div>
');
echo('
<div class="row ex-space">
<div class="col">
');
show_games_played();
echo('
</div>
<div class="col">
');
show_public_games();
echo('
</div>
</div>
');
} else if ($_SESSION['ut_id'] == 2 && $_SESSION['verified'] == 0) {
not_verified();
} else {
echo('<article class="left-half">');
show_games_played();
echo('</article>');
echo('<aside class="right-half">');
show_public_games();
echo('</aside>');
}
} else {
header("location: index.php");
}
?>
</div>
</body>
<script>
function searchGames() {
var input, filter, table, tr, td, i, j, txtVal;
input = document.getElementById('game-search');
filter = input.value.toUpperCase();
table = document.getElementById('games');
tr = table.getElementsByTagName('tr');
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName('td');
for (j = 0; j < td.length; j++) {
if (td[j].innerHTML.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
break;
} else {
tr[i].style.display = "none";
}
}
}
}
</script>
</html>