-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudent.php
More file actions
375 lines (274 loc) · 14.7 KB
/
student.php
File metadata and controls
375 lines (274 loc) · 14.7 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
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
<?php
session_start();
include('credentials.php');
$success = 0;
if (!isset($_SESSION['username'])) {
echo "<script type='text/javascript'> alert('Please log in first!') </script>";
echo "<meta http-equiv='Refresh' content='0;URL=studentLogin.php'>";
exit(0);
}
if (isset($_GET['logout'])) {
session_destroy();
echo "<meta http-equiv='Refresh' content='0;URL=studentLogin.php'>";
exit(0);
}
try {
$dsn = 'mysql:dbname='.$db_database.';host='.$db_host;
$pdo = new PDO($dsn,$db_username,$db_password);
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$rows = $pdo->query("SELECT * FROM Users;");
foreach ($rows as $row) {
if ($row['Email'] == $_SESSION['username']) {
$success = 1;
$_SESSION['userType'] = "student";
}
}
if ($success == 0){
echo "<script type='text/javascript'> alert('You are not a student.') </script>";
session_destroy();
echo "<meta http-equiv='Refresh' content='0;URL=studentLogin.php'>";
exit(0);
}
}
catch (Exception $e) {
echo "<script type='text/javascript'> alert('Error when query if is a valid student.') </script>";
echo "<meta http-equiv='Refresh' content='0;URL=studentLogin.php'>";
exit(0);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Student</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
<link rel="stylesheet" href="css/homepage.css">
</head>
<body>
<div class="wrapper">
<div>
Welcome <?php echo $_SESSION['username']?> !
</div>
<p>
<a href="student.php?logout='1'" style="color: #dddd55">logout</a>
</p>
<form id="requestBox" action="onCancelling.php" style="border-style: dashed;">
<p id="requestInfo" style="padding-top: 10px; padding-left: 10px; padding-right: 10px;"></p>
<p id="requestPosition" style="padding-left: 10px; padding-right: 10px;"></p>
<button id="cancel" class="btn btn-danger" style="margin-bottom: 10px; margin-left: 22%;">Cancel My Request</button>
</form>
<p/>
<p id="currentLab"></p>
<p id="nextLab"></p>
<p id="queryPosition"></p>
<div>
<table id="availableLabs" class="table-hover" style='width: 100%; border-color: white;' border="1">
<caption style="font-weight: bold; font-size: large; caption-side: top; color: #dd5; text-align: center">Available Labs</caption>
<tr style="color: #dddd55">
<th>Lab Title</th>
<th>Lab State</th>
<th>Action</th>
</tr>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include("credentials.php");
date_default_timezone_set('Europe/London');
$dateAndTime = new DateTime('now');
$weekday = $dateAndTime->format('w');
$time = $dateAndTime->format("H:i:s");
$state = 'Waiting';
$currentLabCode = 'null';
$nearbyLabCode = 'null';
//Query the current module code
try {
$dsn = 'mysql:dbname='.$db_database.';host='.$db_host;
$pdo = new PDO($dsn,$db_username,$db_password);
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$stmt = $pdo->prepare("SELECT * FROM Labs WHERE (:time BETWEEN Start_Time AND End_Time) AND Weekday = :weekday;");
$stmt->bindParam(':time', $time);
$stmt->bindParam(':weekday', $weekday);
$stmt->execute();
$rows = $stmt->fetchAll();
if (count($rows) > 1) {
echo "<script type='text/javascript'> alert('There are more than 1 labs in the room, please contact admin!') </script>";
echo "<meta http-equiv='Refresh' content='0;URL=student.php'>";
exit(0);
}
foreach ($rows as $row) {
$currentLabCode = $row['mCode'];
}
} catch (Exception $exception){
echo "<script type='text/javascript'> alert('Error when check the current module code!') </script>";
echo "<meta http-equiv='Refresh' content='0;URL=student.php'>";
exit(0);
}
//Check the current lab is enrolled
try {
$dsn = 'mysql:dbname='.$db_database.';host='.$db_host;
$pdo = new PDO($dsn,$db_username,$db_password);
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$stmt = $pdo->prepare("SELECT * FROM Enrollment WHERE mCode = :code;");
$stmt->bindParam(':code', $currentLabCode);
$stmt->execute();
$rows = $stmt->fetchAll();
foreach ($rows as $row) {
if ($row['uEmail'] == $_SESSION['username']) {
echo "<tr><td>" . $currentLabCode . "</td><td>Ongoing</td>";
echo "<td><a href='makeARequest.php?code=" . $currentLabCode . "' style='color: #dddd55'>Request</a> </td></tr>";
}
}
} catch (Exception $exception){
echo "<script type='text/javascript'> alert('Error when check the current lab is enrolled!') </script>";
echo "<meta http-equiv='Refresh' content='0;URL=student.php'>";
exit(0);
}
date_add($dateAndTime, date_interval_create_from_date_string('10 minutes'));
$weekday = $dateAndTime->format('w');
$time = $dateAndTime->format("H:i:s");
//Check next 10 minutes lab
try {
$dsn = 'mysql:dbname='.$db_database.';host='.$db_host;
$pdo = new PDO($dsn,$db_username,$db_password);
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$stmt = $pdo->prepare("SELECT * FROM Labs WHERE (:time BETWEEN Start_Time AND End_Time) AND Weekday = :weekday;");
$stmt->bindParam(':time', $time);
$stmt->bindParam(':weekday', $weekday);
$stmt->execute();
$rows = $stmt->fetchAll();
if (count($rows) > 1) {
echo "<script type='text/javascript'> alert('There are more than 1 labs in the room 10 minutes later, please contact admin!') </script>";
echo "<meta http-equiv='Refresh' content='0;URL=student.php'>";
exit(0);
}
foreach ($rows as $row) {
$nearbyLabCode = $row['mCode'];
}
} catch (Exception $exception){
echo "<script type='text/javascript'> alert('Error when module code query after 10 minutes!') </script>";
echo "<meta http-equiv='Refresh' content='0;URL=student.php'>";
exit(0);
}
if ($nearbyLabCode != $currentLabCode && $nearbyLabCode != 'null') {
//Check later lab is enrolled
try {
$dsn = 'mysql:dbname='.$db_database.';host='.$db_host;
$pdo = new PDO($dsn,$db_username,$db_password);
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$stmt = $pdo->prepare("SELECT * FROM Enrollment WHERE mCode = :code;");
$stmt->bindParam(':code', $nearbyLabCode);
$stmt->execute();
$rows = $stmt->fetchAll();
foreach ($rows as $row) {
if ($row['uEmail'] == $_SESSION['username']) {
echo "<tr><td>" . $nearbyLabCode . "</td><td>About to Start</td>";
echo "<td><a href='makeARequest.php?code=" . $nearbyLabCode . "' style='color: #dddd55'>Request</a> </td></tr>";
}
}
} catch (Exception $exception){
echo "<script type='text/javascript'> alert('Error when check the later lab is enrolled!') </script>";
echo "<meta http-equiv='Refresh' content='0;URL=student.php'>";
exit(0);
}
} else {
//Check last lab with 15 minutes
date_add($dateAndTime, date_interval_create_from_date_string('-25 minutes'));
$weekday = $dateAndTime->format('w');
$time = $dateAndTime->format("H:i:s");
//Query the module code before 15 minutes
try {
$dsn = 'mysql:dbname='.$db_database.';host='.$db_host;
$pdo = new PDO($dsn,$db_username,$db_password);
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$stmt = $pdo->prepare("SELECT * FROM Labs WHERE (:time BETWEEN Start_Time AND End_Time) AND Weekday = :weekday;");
$stmt->bindParam(':time', $time);
$stmt->bindParam(':weekday', $weekday);
$stmt->execute();
$rows = $stmt->fetchAll();
if (count($rows) > 1) {
echo "<script type='text/javascript'> alert('There are more than 1 labs in the room 15 minutes earlier, please contact admin!') </script>";
echo "<meta http-equiv='Refresh' content='0;URL=student.php'>";
exit(0);
}
foreach ($rows as $row) {
$nearbyLabCode = $row['mCode'];
}
} catch (Exception $exception){
echo "<script type='text/javascript'> alert('Error when module code query 15 minutes earlier!') </script>";
echo "<meta http-equiv='Refresh' content='0;URL=student.php'>";
exit(0);
}
if ($nearbyLabCode != 'null' && $nearbyLabCode != $currentLabCode) {
//Check last lab is enrolled
try {
$dsn = 'mysql:dbname='.$db_database.';host='.$db_host;
$pdo = new PDO($dsn,$db_username,$db_password);
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$stmt = $pdo->prepare("SELECT * FROM Enrollment WHERE mCode = :code;");
$stmt->bindParam(':code', $nearbyLabCode);
$stmt->execute();
$rows = $stmt->fetchAll();
foreach ($rows as $row) {
if ($row['uEmail'] == $_SESSION['username']) {
echo "<tr><td>" . $nearbyLabCode . "</td><td>Just Over</td>";
echo "<td><a href='makeARequest.php?code=" . $nearbyLabCode . "' style='color: #dddd55'>Request</a> </td></tr>";
}
}
} catch (Exception $exception){
echo "<script type='text/javascript'> alert('Error when check the last lab is enrolled!') </script>";
echo "<meta http-equiv='Refresh' content='0;URL=student.php'>";
exit(0);
}
}
}
?>
</table>
</div>
</div>
<script>
function initial() {
$.get("queryPosition.php", function (position) {
if (position == -1) {
document.getElementById("queryPosition").innerHTML = "You have not made a request.";
document.getElementById("requestBox").outerHTML = "";
} else if (position == -2) {
document.getElementById("requestPosition").innerHTML = "Your request has been suspended.";
document.getElementById("availableLabs").outerHTML = "";
} else if (position == 0){
document.getElementById("requestPosition").innerHTML = "The assistant is coming.";
document.getElementById("availableLabs").outerHTML = "";
} else{
document.getElementById("requestPosition").innerHTML = "Your request is at position " + position + ".";
document.getElementById("availableLabs").outerHTML = "";
}
});
$.get("currentLab.php", function (lab) {
if (lab === 'duplicate labs') {
document.getElementById("currentLab").innerHTML = "There are more than 1 labs in the room, please contact admin!";
} else if (lab === "no lab") {
document.getElementById("currentLab").innerHTML = "No lab now.";
} else {
document.getElementById("currentLab").innerHTML = "Current lab is " + lab + ".";
}
});
$.get("nextLab.php", function (lab) {
if (lab === 'no lab after') {
document.getElementById("nextLab").innerHTML = "No more lab today.";
} else {
document.getElementById("nextLab").innerHTML = "Next lab is " + lab + ".";
}
});
$.get("requestedLab.php", function (lab) {
if (lab !== '') {
document.getElementById("requestInfo").innerHTML = "You have requested for lab " + lab + ".";
}
});
}
initial();
setInterval(initial, 5000);
</script>
</body>
</html>