-
Notifications
You must be signed in to change notification settings - Fork 5
/
db.php
102 lines (85 loc) · 2.44 KB
/
db.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
<?php
include_once 'config.php';
function getCurrentDayConfiguration() {
global $dataDirectory;
global $endHour;
if (date("N") == 6) {
$date = date("Y-m-d", time() + 60 * 60 * 24 * 2);
$humanReadableDay = "Montag";
} else if (date("N") == 7) {
$date = date("Y-m-d", time() + 60 * 60 * 24);
$humanReadableDay = "Montag";
} else if (date("N") == 5 && date("H") >= $endHour) {
$date = date("Y-m-d", time() + 60 * 60 * 24 * 3);
$humanReadableDay = "Montag";
} else if (date("H") >= $endHour) {
$date = date("Y-m-d", time() + 60 * 60 * 24);
$humanReadableDay = "Morgen";
} else {
$date = date("Y-m-d");
$humanReadableDay = "Heute";
}
$filename = $dataDirectory . "/" . $date . ".db";
return [
'filename' => $filename,
'date' => $date,
'humanReadableDay' => $humanReadableDay
];
}
function openFile($filename, $mode) {
if (!file_exists($filename)) {
touch($filename);
}
return fopen($filename, $mode);
}
function checkForDBEntryOfUser($filename, $user_id) {
$fh = openFile($filename,'r');
while ($line = fgets($fh)) {
if (str_replace("\n", "", explode(",", $line)[2]) == $user_id) return $line;
}
fclose($fh);
return FALSE;
}
function replaceLine($filename, $line1, $line2) {
$content = file_get_contents($filename);
$content = str_replace($line1, $line2, $content);
file_put_contents($filename, $content);
}
function appendLine($filename, $line) {
$fh = openFile($filename, "a") or die("Unable to open database!");
fwrite($fh, $line."\n");
fclose($fh);
}
function getAttendance($filename, $get_confidential=FALSE) {
global $verified_colors;
$attendance = [];
$fh = openFile($filename, 'r');
while ($line = fgets($fh)) {
$data = explode(",", str_replace("\n", "", $line));
$attendanceData = array(
"name" => $data[0],
"name_modifiers" => $data[4],
"time" => $data[1],
"canteen" => $data[3],
"color" => ($data[2] != "" && array_key_exists($data[2], $verified_colors)) ? $verified_colors[$data[2]] : "#000000"
);
if ($get_confidential) {
$attendanceData["user_id"] = $data[2];
}
$attendance[] = $attendanceData;
}
usort($attendance, 'sortByDate');
return $attendance;
}
function sortByDate($a1, $a2) {
global $times;
$ai1 = array_search($a1["time"], $times);
$ai2 = array_search($a2["time"], $times);
if ($ai1 == $ai2) {
return 0;
} else if ($ai1 < $ai2) {
return -1;
} else {
return 1;
}
}