-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainpage.php
More file actions
188 lines (160 loc) · 5.96 KB
/
mainpage.php
File metadata and controls
188 lines (160 loc) · 5.96 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
<?php
/**
* @Author: Ismail Hasan && Justin Gonzales
* @Version: 0.1
* @Since: 6/11/2018
*
*/
include("connect.php");
session_start();
date_default_timezone_set("America/Los_Angeles");
if (isset($_POST['clockin'])) {
clockIn();
} else if (isset($_POST['clockout'])) {
clockOut();
}else if(isset($_POST['saveFile'])){
saveFile();
}
function clockIn()
{
$name = $_SESSION['login_user'];
$clockInEntry = date("Y/m/d") . " " . date("H:i:s");
// $clockInSQL = "INSERT INTO clockLogs (Username, ClockedIn) VALUES ('$name', TRUE)";
// mysqli_query($GLOBALS['con'], $clockInSQL);
$sql = "SELECT * FROM clockLogs WHERE Username = '$name'";
$result = mysqli_query($GLOBALS['con'], $sql);
$count = mysqli_num_rows($result);
if ($count > 0) {
$clockInSQL = "UPDATE clockLogs SET ClockedIn = TRUE WHERE Username = '$name'";
mysqli_query($GLOBALS['con'], $clockInSQL);
$timeSQL = "INSERT INTO timeLogs (Username, clockIn) VALUES ('$name', '$clockInEntry')";
mysqli_query($GLOBALS['con'], $timeSQL);
} else {
$clockInSQL = "INSERT INTO clockLogs (Username, ClockedIn) VALUES ('$name', TRUE)";
mysqli_query($GLOBALS['con'], $clockInSQL);
$timeSQL = "INSERT INTO timeLogs (Username, clockIn) VALUES ('$name', '$clockInEntry')";
mysqli_query($GLOBALS['con'], $timeSQL);
}
}
function clockOut()
{
/*
* Fills in the data that was when clocking in, which is the clockOut column. The TimeSpent column isn't filled yet.
*/
$name = $_SESSION['login_user'];
$firstNameSQL = "SELECT clockIn FROM timeLogs WHERE Username = '$name' AND clockOut = '0000-00-00 00:00:00'";
$result = mysqli_query($GLOBALS['con'], $firstNameSQL);
$row = mysqli_fetch_array($result);
$clockInEntry = $row['clockIn'];
$clockOutEntry = date("Y/m/d") . " " . date("H:i:s");
$clockOutSQL = "UPDATE clockLogs SET ClockedIn = FALSE WHERE Username = '$name'";
mysqli_query($GLOBALS['con'], $clockOutSQL);
$timeSQL = "UPDATE timeLogs SET clockOut = '$clockOutEntry' WHERE Username = '$name' AND clockIn = '$clockInEntry'";
mysqli_query($GLOBALS['con'], $timeSQL);
/*
* This is where the timeSpent column is filled in, the math is done using a SQL function.
*/
$timeSpentQuery = mysqli_query($GLOBALS['con'], "SELECT TIMESTAMPDIFF(SECOND, '$clockInEntry', '$clockOutEntry')");
$timeSpentResult = mysqli_fetch_array($timeSpentQuery);
$timeSpent = $timeSpentResult["TIMESTAMPDIFF(SECOND, '$clockInEntry', '$clockOutEntry')"];
$formatQuery = mysqli_query($GLOBALS['con'], "SELECT SEC_TO_TIME('$timeSpent')");
$formatResult = mysqli_fetch_array($formatQuery);
$formattedTimeSpent = $formatResult["SEC_TO_TIME('$timeSpent')"];
$timeSpentSQL = "UPDATE timeLogs SET timeSpent = '$formattedTimeSpent' WHERE Username = '$name' AND clockIn = '$clockInEntry' AND clockOut = '$clockOutEntry'";
mysqli_query($GLOBALS['con'], $timeSpentSQL);
/*
* Once all the data is clocked in, and the entry is fully completed, we will export all the table data for this
*/
$csvString = "";
$exportQuery = "SELECT * FROM timeLogs WHERE Username='$name'";
$exportResult = mysqli_query($GLOBALS['con'], $exportQuery);
while($row = mysqli_fetch_array($exportResult)){
$a = $row["Username"];
$b = $row["clockIn"];
$c = $row["clockOut"];
$d = $row["timeSpent"];
$csvString .= "\"$a\",\"$b\",\"$c\",\"$d\"\n";
}
file_put_contents("exportedData/".$name.".csv", $csvString);
}
function saveFile(){
$url = "https://i.ytimg.com/vi/gvfDAcKzCco/maxresdefault.jpg";
$filename = basename($url);
file_put_contents($filename, file_get_contents($url));
}
?>
<html>
<head>
<meta charset="utf-8">
<title>SP Logger</title>
</head>
<body>
<div style="text-align: right; font-size: 35px;">
<?php
echo "Logged in as " . $_SESSION['login_user'];
?>
</div>
<br>
<div style="text-align: center">
<button onclick="location.href='index.php'">Home</button>
</div>
<br>
<div style="text-align: center">
<form method="POST" action="logout.php">
<input type="submit" value="Log Out" name="logOut">
</form>
<form method="POST" action="mainpage.php">
<?php
$name = $_SESSION['login_user'];
$queryResult = mysqli_query($con, "SELECT ClockedIn FROM clockLogs WHERE Username='$name'");
$resultArray = mysqli_fetch_array($queryResult);
$clockedIn = $resultArray["ClockedIn"];
if ($clockedIn == 0) {
echo "<input type='submit' value='Clock In' name='clockin'>";
} else if ($clockedIn == 1) {
echo "<input type='submit' value='Clock Out' name='clockout'>";
}
?>
</form>
</div>
<br>
<div style="text-align: center">
<?php
$name = $_SESSION['login_user'];
$queryResult = mysqli_query($con, "SELECT ClockedIn FROM clockLogs WHERE Username='$name'");
$resultArray = mysqli_fetch_array($queryResult);
$clockedIn = $resultArray["ClockedIn"];
if ($clockedIn == 0) {
echo "Clocked out.";
} else if ($clockedIn == 1) {
echo "Clocked in.";
}
?>
</div>
<br>
<div style="text-align: center">
<?php
$name = $_SESSION['login_user'];
echo "Total time spent on project: ";
$timeSQL = "SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( timeSpent ) ) ) FROM timeLogs WHERE Username='$name'";
$timeResult = mysqli_query($con, $timeSQL);
$timeRows = mysqli_fetch_array($timeResult);
$timeNumberRows = mysqli_num_rows($timeResult);
$timeTotal = $timeRows['SEC_TO_TIME( SUM( TIME_TO_SEC( timeSpent ) ) )'];
print_r($timeTotal);
?>
</div>
<br>
<br>
<div style="text-align: center;">
<!-- <form method="post" action="mainpage.php">-->
<!-- <input type="submit" value="Download" name="saveFile">-->
<!-- </form>-->
<?php
echo "<a href=exportedData/$name.csv download=$name.csv>
<button>Download</button>
</a>"
?>
</div>
</body>
</html>