aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/resources/statistics.php
blob: 6738833889cf1636005b25343a47beb0350f1499 (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
<?php

/**
 * request.php - contains Resources_StatisticsController
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @author      Moritz Strohm <strohm@data-quest.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @copyright   2018-2019
 * @category    Stud.IP
 * @since       4.5
 */


/**
 * Resources_StatisticsController contains actions to display statistics
 * related to the room and resource management system.
 */
class Resources_StatisticsController extends AuthenticatedController
{
    public function before_filter(&$action, &$args)
    {
        parent::before_filter($action, $args);

        if (Navigation::hasItem('/room_management/statistics')) {
            Navigation::activateItem('/room_management/statistics');
        }
    }

    public function room_load_action()
    {
        if (Navigation::hasItem('/room_management/statistics/room_load')) {
            Navigation::activateItem('/room_management/statistics/room_load');
        }

        $year_begin = new DateTime();
        $year_begin->setDate(intval(date('Y')), 1, 1);
        $year_begin->setTime(0,0,0);
        $year_end = clone $year_begin;
        $year_end = $year_end->add(
            new DateInterval('P1Y')
        )->sub(
            new DateInterval('PT1S')
        );
        $db = DBManager::get();

        $sum_stmt = $db->prepare(
            "SELECT SUM(rbi.end - rbi.begin) FROM resource_booking_intervals rbi
            INNER JOIN resource_bookings rb
            ON rbi.booking_id = rb.id
            WHERE
            rb.booking_type IN ( :types )
            AND
            rbi.begin < :end AND rbi.end > :begin"
        );

        $sum_stmt->execute(
            [
                'types' => [
                    ResourceBooking::TYPE_NORMAL,
                    ResourceBooking::TYPE_RESERVATION,
                ],
                'begin' => $year_begin->getTimestamp(),
                'end' => $year_end->getTimestamp()
            ]
        );
        $total_booked_time = $sum_stmt->fetchColumn();

        $sum_stmt->execute(
            [
                'types' => [ResourceBooking::TYPE_LOCK],
                'begin' => $year_begin->getTimestamp(),
                'end' => $year_end->getTimestamp()
            ]
        );
        $total_locked_time = $sum_stmt->fetchColumn();

        $total_seconds = intval($year_end->getTimestamp()) - intval($year_begin->getTimestamp());

        if ($total_seconds > 0) {
            $this->booked_share = $total_booked_time / $total_seconds;
            $this->locked_share = $total_locked_time / $total_seconds;

            $this->unused_share = 1 - $this->booked_share - $this->locked_share;
        }
    }
}