summaryrefslogtreecommitdiff
path: root/includes/pages/guest_stats.php
blob: bb07f4dc798daa22ba5e22c69ce0a19b41aad79f (plain)
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
<?php

use Engelsystem\Database\DB;

function guest_stats()
{
    $apiKey = config('api_key');
    $request = request();

    if ($request->has('api_key')) {
        if (!empty($apiKey) && $request->input('api_key') == $apiKey) {
            $stats = [];

            list($user_count) = DB::select('SELECT count(*) AS `user_count` FROM `User`');
            $stats['user_count'] = $user_count['user_count'];

            list($arrived_user_count) = DB::select('
                SELECT count(*) AS `user_count`
                FROM `User`
                WHERE `Gekommen`=1
            ');
            $stats['arrived_user_count'] = $arrived_user_count['user_count'];

            $done_shifts_seconds = DB::selectOne('
                SELECT SUM(`Shifts`.`end` - `Shifts`.`start`)
                FROM `ShiftEntry`
                JOIN `Shifts` USING (`SID`)
                WHERE `Shifts`.`end` < UNIX_TIMESTAMP()
            ');
            $done_shifts_seconds = (int)array_shift($done_shifts_seconds);
            $stats['done_work_hours'] = round($done_shifts_seconds / (60 * 60), 0);

            $users_in_action = DB::select('
                SELECT `Shifts`.`start`, `Shifts`.`end`
                FROM `ShiftEntry`
                JOIN `Shifts` ON `Shifts`.`SID`=`ShiftEntry`.`SID`
                WHERE UNIX_TIMESTAMP() BETWEEN `Shifts`.`start` AND `Shifts`.`end`
            ');
            $stats['users_in_action'] = count($users_in_action);

            header('Content-Type: application/json');
            raw_output(json_encode($stats));
            return;
        }
        raw_output(json_encode([
            'error' => 'Wrong api_key.'
        ]));
    }
    raw_output(json_encode([
        'error' => 'Missing parameter api_key.'
    ]));
}