-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard_parent.php
More file actions
342 lines (292 loc) · 11.9 KB
/
Copy pathdashboard_parent.php
File metadata and controls
342 lines (292 loc) · 11.9 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
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
<?php
include_once "backend/session.php";
header('X-Frame-Options: SAMEORIGIN');
if (!isset($_SESSION['userid']) || $_SESSION['usertype'] != 2) {
echo "<script>location.href = 'login_main.php';</script>";
}
?>
<html>
<head>
<meta charset="UTF-8">
<title>Dashboard</title>
<link rel="shortcut icon" type="image/icon" href="assets/logo/favicon.png"/>
</head>
<body>
<style>
#actions {
display: flex;
justify-content: flex-end;
margin-top: 20px;
right: 10%; /* 100% - 70% = 30% */
position: absolute;
}
#btnPurchase {
margin-left: 10px;
font-size: 20px;
padding: 10px;
}
#btnPurchase, #btnReset {
background-color: #4CAF50; /* Green */
border: none;
color: white;
text-align: center;
text-decoration: none;
font-size: 16px;
transition-duration: 0.4s;
cursor: pointer;
}
#btnPurchase:hover, #btnReset:hover {
background-color: white;
color: black;
border: 2px solid #4CAF50;
}
table#linkedlist input[type='number'] {
text-align: center;
}
.sortable {
cursor: pointer;
background-image: url('assets/images/no_sort.png');
background-repeat: no-repeat;
background-position: calc(100% - 30px) center;
background-size: 30px 30px;
}
.sortable.asc {
background-image: url('assets/images/asc.png');
background-repeat: no-repeat;
}
.sortable.desc {
background-image: url('assets/images/desc.png');
background-repeat: no-repeat;
}
table#linkedlist {
margin-left: auto;
margin-right: auto;
margin-top: 10px;
width: 80%;
border: 1px solid black;
}
table#linkedlist th {
background-color: gray;
color: white;
font-weight: bold;
height: 50px;
font-size: 20px;
text-align: center;
}
table#linkedlist td {
text-align: center;
color: black;
font-weight: bold;
background-color: lightyellow;
border: 1px solid black;
border-collapse: collapse;
padding: 10px;
margin: 10px;
width: 20%;
height: 50px;
font-size: 20px;
}
table#linkedlist th.sortable:hover {
color: #007BFF; /* Change the text color to blue when hovering */
background-color: #ddd; /* Change the background color to light gray when hovering */
}
.unlink-button {
border-radius: 12px; /* This will make the button have round corners */
padding: 10px 20px; /* This will make the button bigger */
display: flex; /* This will align the icon and the text side by side */
align-items: center; /* This will vertically center the icon and the text */
justify-content: center; /* This will horizontally center the icon and the text */
margin: auto; /* This will center the button in the cell */
}
.unlink-button:hover {
background-color: #ddd; /* This will change the background color to light gray when hovering */
cursor: pointer; /* This will change the cursor to a hand pointer when hovering */
}
.rubbish-bin-icon {
height: 20px; /* Adjust this value to change the size of the icon */
width: 20px; /* Adjust this value to change the size of the icon */
margin-right: 5px; /* This will add some space between the icon and the text */
}
.topup-button {
border-radius: 12px; /* This will make the button have round corners */
padding: 10px 20px; /* This will make the button bigger */
display: flex; /* This will align the icon and the text side by side */
align-items: center; /* This will vertically center the icon and the text */
justify-content: center; /* This will horizontally center the icon and the text */
margin: auto; /* This will center the button in the cell */
}
.topup-button:hover {
background-color: #ddd; /* This will change the background color to light gray when hovering */
cursor: pointer; /* This will change the cursor to a hand pointer when hovering */
}
.topup-icon {
height: 20px; /* Adjust this value to change the size of the icon */
width: 20px; /* Adjust this value to change the size of the icon */
margin-right: 5px; /* This will add some space between the icon and the text */
}
</style>
<?php
include_once("backend/getStudentList.php");
$studentList = getStudents();
echo "<div id='table-container'>";
echo "<table id='linkedlist'>";
echo "<tr>";
echo "<th>Username</th>";
echo "<th class='sortable' onclick='sortTable(1)'>Full Name</th>";
echo "<th class='sortable' onclick='sortTable(2)'>Balance ($)</th>";
echo "<th>Top Up</th>";
echo "<th>Unlink</th>";
echo "</tr>";
foreach($studentList as $student) {
echo "<tr>";
echo "<td id='row-username'>" . $student['username'] . "</td>";
echo "<td id='row-fullname'>" . $student['name'] . "</td>";
echo "<td id='row-balance'>" . $student['balance'];
echo "<td><button class='topup-button' onclick='topup(" . $student['id'] . ")'><img src='assets/images/topup.png' class='topup-icon'> Top Up</button></td>";
echo "<td><button class='unlink-button' onclick='unlink(" . $student['id'] . ")'><img src='assets/images/unlink.png' class='rubbish-bin-icon'> Unlink</button></td>";
echo "</tr>";
}
echo "</table>";
echo "<div id='actions'>";
echo "<button id='btnPurchase' onclick='newlink()'>+ Link to a student</button>";
echo "</div>";
echo "</div>";
?>
<script>
const numbericColumns = [2];
const tableName = "linkedlist";
var defaultDir = "asc";
var lastSortedColumn = 1;
function sortTable(n, dir = null) {
var table, rows, i, x, y, shouldSwitch;
table = document.getElementById(tableName);
var switching = true;
lastSortedColumn = n;
// if dir is not null, set the default direction to dir
if (dir != null) {
if (dir == "refresh") {
defaultDir = defaultDir === "asc" ? "asc" : "desc";
} else {
defaultDir = dir === "asc" ? "asc" : "desc";
}
} else {
var ths = table.getElementsByTagName("th");
for (var i = 0; i < ths.length; i++) {
// head has class asc or desc
if (ths[i].classList.contains("asc") || ths[i].classList.contains("desc")) {
// defualt direction is the opposite of the current direction
defaultDir = ths[i].classList.contains("asc") ? "desc" : "asc";
}
}
}
while (switching) {
switching = false;
rows = table.rows;
// Loop to go through all rows
for (i = 1; i < (rows.length - 1); i++) {
shouldSwitch = false;
// Compare the two adjacent rows
x = rows[i].getElementsByTagName("TD")[n];
y = rows[i + 1].getElementsByTagName("TD")[n];
// Check if the two rows should switch place
if (numbericColumns.includes(n)) {
if (defaultDir == "asc") {
if (parseFloat(x.innerHTML) > parseFloat(y.innerHTML)) {
shouldSwitch = true;
break;
}
} else {
if (parseFloat(x.innerHTML) < parseFloat(y.innerHTML)) {
shouldSwitch = true;
break;
}
}
} else {
if (defaultDir == "asc") {
if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
shouldSwitch = true;
break;
}
} else {
if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
shouldSwitch = true;
break;
}
}
}
}
if (shouldSwitch) {
// If a switch is needed, perform the switch and mark that a switch has been done
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
}
}
// insert a class (asc or desc) to the current column header [n] and remove the asc or desc class from other column headers after sorting
var headers = table.getElementsByTagName("TH");
for (i = 0; i < headers.length; i++) {
headers[i].classList.remove("asc");
headers[i].classList.remove("desc");
}
headers[n].classList.add(defaultDir);
}
sortTable(lastSortedColumn);
function topup(studentid) {
var amount = window.prompt('Enter the amount to top up:', '');
if (amount !== null) {
if (isNaN(amount) || amount <= 0) {
alert('Invalid amount.');
} else {
var xhr = new XMLHttpRequest();
xhr.open('POST', 'backend/topup.php', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send('targetid=' + studentid + '&amount=' + amount);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
location.reload();
} else if (xhr.readyState == 4 && xhr.status != 200) {
alert('Top up failed. \nReason: ' + xhr.responseText);
}
}
}
}
}
function newlink() {
var studentid = window.prompt('Enter the student username:', '');
var password = window.prompt('Enter the student password:', '');
if (studentid !== null && password !== null) {
if (studentid.length == 0 || password.length == 0) {
alert('Student Username and password are required.');
} else {
var xhr = new XMLHttpRequest();
xhr.open('POST', 'backend/newlink.php', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send('username=' + studentid + '&password=' + password);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
location.reload();
} else if (xhr.readyState == 4 && xhr.status != 200) {
alert('Link failed. \nReason: ' + xhr.responseText);
}
}
}
}
}
function unlink(studentid) {
// pop up a confirmation dialog
if (confirm("Are you sure you want to unlink this student?")) {
var xhr = new XMLHttpRequest();
xhr.open('POST', 'backend/unlink.php', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send('studentid=' + studentid);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
location.reload();
} else if (xhr.readyState == 4 && xhr.status != 200) {
alert('Unlink failed. \nReason: ' + xhr.responseText);
}
}
}
}
</script>
</body>
</html>