-
Notifications
You must be signed in to change notification settings - Fork 0
/
attendance.php
74 lines (52 loc) · 2.38 KB
/
attendance.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
<?php
include_once 'config/connect.php';
date_default_timezone_set('Africa/Nairobi');
$response = [];
// echo $_SERVER['REQUEST_METHOD'];
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if(!empty($_POST)){
$reg_no = mysqli_real_escape_string($con, $_POST['reg_no']);
$timetable_id = mysqli_real_escape_string($con, $_POST['timetable_id']);
$imeis = mysqli_real_escape_string($con, $_POST['imeis']);
$current_year = date('Y');
$current_date = date('Y-m-d');
$current_time = date('H:i');
$current_day = date('l');
// STEP 1: Check if the user is already present in the session
$present_check_sql = "SELECT * FROM attendance
WHERE date = '" . $current_date . "'
AND imei = '" . $imeis . "'
AND timetable_id = '" . $timetable_id . "'
AND student_id = ( SELECT id FROM users WHERE reg_no = '" . $reg_no . "')";
$present_check_res = mysqli_query($con, $present_check_sql);
if(mysqli_error($con)){
$response['status'] = 'Error';
$response['message'] = 'Sorry!, An error has occured.' . mysqli_error($con);
}else{
if(mysqli_num_rows($present_check_res) > 0){
$response['status'] = 'Ok';
$response['message'] = 'You are already present for this session!';
}else{
$present_sql = "INSERT INTO attendance
(timetable_id, student_id, imei, status, date)
VALUES ('".$timetable_id."', (SELECT id FROM users WHERE reg_no = '".$reg_no."'), '".$imeis."', 'PRESENT', curdate())";
$present_res = mysqli_query($con, $present_sql);
if(mysqli_error($con)){
$response['status'] = 'Error';
$response['message'] = 'Sorry!, An error has occured.' . mysqli_error($con);
}else{
$response['status'] = 'Ok';
$response['message'] = 'You are PRESENT for this session.';
}
}
}
}else{
$response['status'] = 'Error';
$response['message'] = 'Your request was empty!';
}
}else{
$response['status'] = 'Error';
$response['message'] = 'Incorrect request method!';
}
echo json_encode($response);
?>