-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
155 lines (132 loc) · 3.48 KB
/
lib.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
<?php
function load_stats()
{
return json_decode(file_get_contents(dirname(__FILE__) . "/stats.json"), true);
}
function save_stats($post)
{
$stats = load_stats();
$stats['last_update'] = time();
$stats['eligible_voters'] = intval($post['eligible_voters']);
foreach ($stats['days'] as $day => $count) {
$stats['days'][$day] = $post[$day];
}
file_put_contents(dirname(__FILE__) . "/stats.json", json_encode($stats, JSON_PRETTY_PRINT));
}
function format_day_de($day)
{
$date = date_create($day);
return date_format($date, "d.m.Y");
}
function format_day($day)
{
$date = date_create($day);
return date_format($date, "Y-m-d");
}
function format_int_de($number)
{
return number_format($number, 0, ',', '.');
}
function format_int($number)
{
return number_format($number);
}
function format_count_de($number)
{
if (intval($number) === 0) {
if ($number === "Sat") {
return "Samstag";
}
if ($number === "Sun") {
return "Sonntag";
}
return $number;
}
return format_int_de($number);
}
function format_count($number)
{
if (intval($number) === 0) {
if ($number === "Sat") {
return "Saturday";
}
if ($number === "Sun") {
return "Sunday";
}
return $number;
}
return format_int($number);
}
function print_eligible_voters_de($stats)
{
echo format_int_de($stats['eligible_voters']);
}
function print_eligible_voters($stats)
{
echo format_int($stats['eligible_voters']);
}
function print_turnout_de($stats)
{
$turnout = get_number_of_letters_so_far($stats) / $stats['eligible_voters'] * 100;
echo number_format($turnout, 2, ',', '.') . ' %';
}
function print_turnout($stats)
{
$turnout = get_number_of_letters_so_far($stats) / $stats['eligible_voters'] * 100;
echo number_format($turnout, 2, '.', ',') . ' %';
}
function print_number_of_letters_so_far_de($stats)
{
echo format_int_de(get_number_of_letters_so_far($stats));
}
function print_number_of_letters_so_far($stats)
{
echo format_int(get_number_of_letters_so_far($stats));
}
function print_row_de($day, $count, $maxCount)
{
$formatted_day = format_day_de($day);
$formatted_count = format_count_de($count);
$countclass = '';
if ($count <> '-' and intval($count) === 0) {
$countclass = ' muted';
}
$bar = get_bar($count, $maxCount);
echo "<tr>
<td class='date'>$formatted_day</td>
<td class='number$countclass'>$formatted_count</td>
<td class='bar'>$bar</td>
</tr>";
}
function print_row($day, $count, $maxCount)
{
$formatted_day = format_day($day);
$formatted_count = format_count($count);
$countclass = '';
if ($count <> '-' and intval($count) === 0) {
$countclass = ' muted';
}
$bar = get_bar($count, $maxCount);
echo "<tr>
<td class='date'>$formatted_day</td>
<td class='number$countclass'>$formatted_count</td>
<td class='bar'>$bar</td>
</tr>";
}
function get_number_of_letters_so_far($stats)
{
$sum = 0;
foreach ($stats['days'] as $count) {
$sum += intval($count);
}
return $sum;
}
function get_bar($count, $maxCount)
{
$countValue = intval($count);
if ($countValue <= 0) {
return '';
}
$width = number_format($countValue / $maxCount * 100, 2);
return "<div class='colourbar' style='width:$width%;'></div>";
}