summaryrefslogtreecommitdiff
path: root/includes/controller/public_dashboard_controller.php
blob: 45a54423f03ae68bfd1174ca2c695f53dd260b78 (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

/**
 * Loads all data for the public dashboard
 */
function public_dashboard_controller()
{
    $stats = [
        'needed-3-hours' => stats_angels_needed_three_hours(),
        'needed-night' => stats_angels_needed_for_nightshifts(),
        'angels-working' => stats_currently_working(),
        'hours-to-work' => stats_hours_to_work()
    ];
    
    $free_shifts_source = Shifts_free(time(), time() + 12 * 60 * 60);
    $free_shifts = [];
    foreach ($free_shifts_source as $shift) {
        $free_shift = public_dashboard_controller_free_shift($shift);
        if(count($free_shift['needed_angels']) > 0) {
            $free_shifts[] = $free_shift;
        }
    }
    
    return [
        _('Public Dashboard'),
        public_dashboard_view($stats, $free_shifts)
    ];
}

/**
 * Gathers informations for free shifts to display.
 *
 * @param array $shift            
 */
function public_dashboard_controller_free_shift($shift)
{
    $shifttype = ShiftType($shift['shifttype_id']);
    $room = Room($shift['RID']);
    
    $free_shift = [
        'SID' => $shift['SID'],
        'style' => 'default',
        'start' => date('H:i', $shift['start']),
        'end' => date('H:i', $shift['end']),
        'duration' => round(($shift['end'] - $shift['start']) / 3600),
        'shifttype_name' => $shifttype['name'],
        'title' => $shift['title'],
        'room_name' => $room['Name'],
        'needed_angels' => []
    ];
    
    if (time() + 3 * 60 * 60 > $shift['start']) {
        $free_shift['style'] = 'warning';
    }
    if (time() > $shift['start']) {
        $free_shift['style'] = 'danger';
    }
    
    $free_shift['needed_angels'] = public_dashboard_needed_angels($shift['NeedAngels']);
    
    return $free_shift;
}

/**
 * Gathers informations for needed angels on dashboard
 *
 * @param array $needed_angels            
 */
function public_dashboard_needed_angels($needed_angels)
{
    $result = [];
    foreach ($needed_angels as $needed_angel) {
        $need = $needed_angel['count'] - $needed_angel['taken'];
        if ($need > 0) {
            $angeltype = AngelType($needed_angel['TID']);
            if ($angeltype['show_on_dashboard']) {
                $result[] = [
                    'need' => $need,
                    'angeltype_name' => $angeltype['name']
                ];
            }
        }
    }
    return $result;
}

/**
 * Returns url to public dashboard
 */
function public_dashboard_link()
{
    return page_link_to('public-dashboard');
}
?>