-
Notifications
You must be signed in to change notification settings - Fork 0
/
adminPage.php
268 lines (233 loc) · 8.75 KB
/
adminPage.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
<?php
session_start();
//Database connection parameters into constants
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'jywqbgf_admin');
define('DB_PASSWORD', 'g*2pd]s$?b&T');
define('DB_NAME', 'jywqbgf_Willowbank');
$newActivity_message = ""; // Initialises the new activity message variable
//Establishes the database connection
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
//Checks the connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Fetches students from the User table in the database using UserRole
$sql = "SELECT username, userID FROM `User` WHERE userRole = 'student'";
$result = $conn->query($sql);
//Initialises an empty array to store the fetched students.
$students = [];
// Check if there are any results from this query
if ($result->num_rows > 0) {
//If so fetches the students and store them in the students array.
while ($row = $result->fetch_assoc()) {
$students[] = $row;
}
}
//Fetches all the activities from the activites table in the database
$sql = "SELECT * FROM `activities`";
$result = $conn->query($sql);
//Initialises an empty array to store the fetched activities
$activities = [];
//Checks if there are any results from this query.
if ($result->num_rows > 0) {
//If so fetches activities and store them in the activities array.
while ($row = $result->fetch_assoc()) {
$activities[] = $row;
}
}
//Adding the new activity to the Activity Table in Database
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['activityName']) && isset($_POST['activityXP']) && isset($_POST['activityType'])) {
//Retrieves the form's data inputed by the user
$activityName = $_POST['activityName'];
$activityXP = $_POST['activityXP'];
$activityType = $_POST['activityType'];
//Prepares and executes the SQL INSERT query
$sql = "INSERT INTO activities (Activityname, XPpoints, activitytype) VALUES (?, ?, ?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("sis", $activityName, $activityXP, $activityType);
if ($stmt->execute()) {
$newActivity_message = "Activity Added Successfully!";
} else {
$newActivity_message = "Error: " . $sql . "<br>" . $conn->error;
}
$stmt->close();
}
//Adding the user activity to the Useract Table in Database
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['activityID']) && isset($_POST['userID'])) {
//Retrieves the form data from user response
$activityID = $_POST['activityID'];
$userID = $_POST['userID'];
//Prepares and executes the SQL INSERT query
$sql = "INSERT INTO userAct (activityID, userID) VALUES (?, ?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ii", $activityID, $userID);
if ($stmt->execute()) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$stmt->close();
}
//Fetches the user activities from the database
$sql = "SELECT ua.activityID, ua.userID, a.Activityname, u.username
FROM userAct ua
INNER JOIN activities a ON ua.activityID = a.activityID
INNER JOIN `User` u ON ua.userID = u.userID";
$result = $conn->query($sql);
//Initialises an empty array to store the fetched user activities
$userActivities = [];
//Checks if there are any results from this query
if ($result->num_rows > 0) {
//If so fetchs the user activities and store them in the userActivities array
while ($row = $result->fetch_assoc()) {
$userActivities[] = $row;
}
}
//Closes the database connection
$conn->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Page</title>
<link rel="stylesheet" href="AdminStyle.css"/>
</head>
<body>
<header>
<div class="logo">
<img src="logo.png" alt="Logo">
</div>
<div class="user">
<span>Welcome, Admin</span>
</div>
<!-- Colour themes for page -->
<div class="setTheme">
<button onclick="setTheme('light-mode')">Light Mode</button>
<button onclick="setTheme('high-contrast-mode')">High Contrast Mode</button>
<button onclick="setTheme('dark-mode')">Dark Mode</button>
</div>
</header>
<main>
<!-- Activities Table -->
<div class="container">
<h2>Activities</h2>
<div class="activities-table-container">
<table id="activitiesTable">
<tr>
<th>Activity ID</th>
<th>Activity Name</th>
<th>XP Points</th>
<th>Type</th>
</tr>
<!-- Populate activities table rows with fetched activities -->
<?php foreach ($activities as $activity) : ?>
<tr onclick='selectActivity(<?= $activity["activityID"] ?>, "<?= $activity["Activityname"] ?>", <?= $activity["XPpoints"] ?>, "<?= $activity["activitytype"] ?>")'>
<td><?= $activity["activityID"] ?></td>
<td><?= $activity["Activityname"] ?></td>
<td><?= $activity["XPpoints"] ?></td>
<td><?= $activity["activitytype"] ?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
<div class="container">
<!-- Form to add new activity from user input -->
<form id="addActivityForm" method="post">
<input type="text" id="activityName" name="activityName" placeholder="Activity Name">
<input type="number" id="activityXP" name="activityXP" placeholder="XP Points">
<select id="activityType" name="activityType">
<option value="Fishing">Fishing</option>
<option value="Archery">Archery</option>
<option value="Forestschool">Forest School</option>
<option value="Cooking">Cooking</option>
</select>
<button type="submit">Add Activity</button>
</form>
<!-- Message for the new activity -->
<p><?= $newActivity_message ?></p>
</div>
<!-- Students Table -->
<div class="container">
<h2>Students</h2>
<table id="studentsTable">
<tr>
<th>User ID</th>
<th>Username</th>
</tr>
<!-- Populates students table rows with fetched students -->
<?php foreach ($students as $student) : ?>
<tr onclick='selectStudent(<?= $student["userID"] ?>, "<?= $student["username"] ?>")'>
<td><?= $student["userID"] ?></td>
<td><?= $student["username"] ?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
<!-- Form to add new user activity using user input -->
<div class="container">
<form id="addUserActivityForm" method="post">
<input type="text" id="activityID" name="activityID" placeholder="Activity ID">
<input type="text" id="userID" name="userID" placeholder="User ID">
<button type="submit">Add User Activity</button>
</form>
</div>
<!-- User Activities Table -->
<div class="container">
<h2>Student's Activities</h2>
<table id="userActivitiesTable">
<tr>
<th>Activity ID</th>
<th>User ID</th>
<th>Activity Name</th>
<th>Username</th>
</tr>
<?php foreach ($userActivities as $activity) : ?>
<tr>
<td><?= $activity['activityID'] ?></td>
<td><?= $activity['userID'] ?></td>
<td><?= $activity['Activityname'] ?></td>
<td><?= $activity['username'] ?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
</main>
<footer>
<!-- Certificate Button -->
<a href="adminCertificate.html" id="certificatesBtn">Certificates</a>
</footer>
<script>
/* Theme Colour */
function setTheme(theme) {
document.documentElement.className = theme;
}
// Function to handle adding the activity
function handleAddActivity(event) {
event.preventDefault();
const activityName = document.getElementById("activityName").value;
const activityXP = document.getElementById("activityXP").value;
const activityType = document.getElementById("activityType").value;
fetch('add_activity.php', {
method: 'POST',
body: JSON.stringify({ activityName: activityName, activityXP: activityXP, activityType: activityType }),
headers: {
'Content-Type': 'application/json'
}
})
.then(response => response.text())
.then(message => {
console.log(message);
location.reload();
})
.catch(error => console.error('Error adding activity:', error));
document.getElementById("activityName").value = "";
document.getElementById("activityXP").value = "";
}
//Calls the populateActivitiesTable function to populate the activities table.
populateActivitiesTable();
</script>
</body>
</html>