summaryrefslogtreecommitdiff
path: root/includes/model/Stats.php
blob: 5618cdc68089b9cb0e51961df8414417980c3b88 (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
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
<?php
use Engelsystem\Database\Db;

/**
 * Returns the number of angels currently working.
 */
function stats_currently_working()
{
    $result = Db::selectOne("
        SELECT SUM(
            (SELECT COUNT(*) FROM `ShiftEntry` WHERE `ShiftEntry`.`SID`=`Shifts`.`SID` AND `freeloaded`=0)
            ) as `count`
        FROM `Shifts`
        WHERE (`end` >= ? AND `start` <= ?)", [
        time(),
        time()
    ]);
    if (empty($result['count'])) {
        return '-';
    }
    return $result['count'];
}

/**
 * Return the number of hours still to work.
 */
function stats_hours_to_work()
{
    $result = Db::selectOne("
        SELECT ROUND(SUM(
            (SELECT SUM(`count`) FROM `NeededAngelTypes` WHERE `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`)
            * (`Shifts`.`end` - `Shifts`.`start`)/3600
        )) as `count`
        FROM `Shifts`
        WHERE `end` >= ?", [
        time()
    ]);
    if (empty($result['count'])) {
        return '-';
    }
    return $result['count'];
}

/**
 * Returns the number of needed angels in the next 3 hours
 */
function stats_angels_needed_three_hours()
{
    $now = time();
    $in3hours = $now + 3 * 60 * 60;
    $result = Db::selectOne("
        SELECT SUM(
            (SELECT SUM(`count`) FROM `NeededAngelTypes` WHERE `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`)
            - (SELECT COUNT(*) FROM `ShiftEntry` WHERE `ShiftEntry`.`SID`=`Shifts`.`SID` AND `freeloaded`=0)
            ) as `count`
        FROM `Shifts`
        WHERE ((`end` > ? AND `end` < ?) OR (`start` > ? AND `start` < ?))", [
        $now,
        $in3hours,
        $now,
        $in3hours
    ]);
    if (empty($result['count'])) {
        return '-';
    }
    return $result['count'];
}

/**
 * Returns the number of needed angels for nightshifts (between 2 and 8)
 */
function stats_angels_needed_for_nightshifts()
{
    $night_start = parse_date('Y-m-d H:i', date('Y-m-d', time() + 12 * 60 * 60) . ' 02:00');
    $night_end = $night_start + 6 * 60 * 60;
    $result = Db::selectOne("
        SELECT SUM(
            (SELECT SUM(`count`) FROM `NeededAngelTypes` WHERE `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`)
            - (SELECT COUNT(*) FROM `ShiftEntry` WHERE `ShiftEntry`.`SID`=`Shifts`.`SID` AND `freeloaded`=0)
            ) as `count`
        FROM `Shifts`
        WHERE ((`end` > ? AND `end` < ?) OR (`start` > ? AND `start` < ?))", [
        $night_start,
        $night_end,
        $night_start,
        $night_end
    ]);
    if (empty($result['count'])) {
        return '-';
    }
    return $result['count'];
}

?>