aboutsummaryrefslogtreecommitdiff
path: root/app/routes/ResourceBooking.php
blob: a5d027f44b23912791820e7c6e1c5572d28f1e0c (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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
namespace RESTAPI\Routes;

/**
 * This file contains the REST class for the
 * room and resource management system.
 *
 * @author      Moritz Strohm <strohm@data-quest.de>
 * @copyright   2017-2019
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @since       4.5
 * @deprecated  Since Stud.IP 5.0. Will be removed in Stud.IP 6.0.
 */
class ResourceBooking extends \RESTAPI\RouteMap
{

    /**
     * Helper method that either returns the specified data
     * or simply an empty string in case that no request result
     * is requested.
     */
    protected function sendReturnData($data)
    {
        if (\Request::submitted('quiet')) {
            //Return nothing.
            return '';
        }

        //Return data.
        return $data;
    }


    /**
     * Moves a resource booking, if permitted.
     *
     * @post /resources/booking/:booking_id/move
     */
    public function move($booking_id)
    {
        $booking = \ResourceBooking::find($booking_id);
        if (!$booking) {
            $this->notFound('Resource booking object not found!');
        }

        $current_user = \User::findCurrent();

        if ($booking->isReadOnlyForUser($current_user)) {
            throw new \AccessDeniedException();
        }

        $resource_id = \Request::get('resource_id');
        $begin_str = \Request::get('begin');
        $end_str = \Request::get('end');
        $interval_id = \Request::get('interval_id');

        //Try the ISO format first: YYYY-MM-DDTHH:MM:SS±ZZ:ZZ
        $begin = \DateTime::createFromFormat(\DateTime::RFC3339, $begin_str);
        $end = \DateTime::createFromFormat(\DateTime::RFC3339, $end_str);
        if (!($begin instanceof \DateTime) || !($end instanceof \DateTime)) {
            $tz = new \DateTime();
            $tz = $tz->getTimezone();
            //Try the ISO format without timezone:
            $begin = \DateTime::createFromFormat('Y-m-d\TH:i:s', $begin_str, $tz);
            $end = \DateTime::createFromFormat('Y-m-d\TH:i:s', $end_str, $tz);
        }

        //Check if a specific interval has been moved:
        if ($interval_id) {
            $interval = \ResourceBookingInterval::findOneBySql(
                'interval_id = :interval_id AND booking_id = :booking_id',
                [
                    'interval_id' => $interval_id,
                    'booking_id' => $booking->id
                ]
            );
            if (!$interval) {
                $this->notFound('Resource booking interval not found!');
            }
            $interval_begin = new \DateTime();
            $interval_begin->setTimestamp($interval->begin);
            $interval_end = new \DateTime();
            $interval_end->setTimestamp($interval->end);

            //Calculate the difference from the interval time range
            //to the time range from the request. That difference
            //is then applied to the booking.
            $begin_diff = $interval_begin->diff($begin);
            $end_diff = $interval_end->diff($end);

            $new_booking_begin = new \DateTime();
            $new_booking_begin->setTimestamp($booking->begin);
            $new_booking_end = new \DateTime();
            $new_booking_end->setTimestamp($booking->end);

            $new_booking_begin = $new_booking_begin->add($begin_diff);
            $new_booking_end = $new_booking_end->add($end_diff);
            //We must substract the preparation time to the begin timestamp
            //to get the real begin:
            $real_begin = clone $new_booking_begin;
            if ($booking->preparation_time > 0) {
                $real_begin->sub(new \DateInterval('PT' . ($booking->preparation_time / 60 ) . 'M'));
            }
            $booking->begin = $real_begin->getTimestamp();
            $booking->end = $new_booking_end->getTimestamp();
        } else {
            //We must substract the preparation time to the begin timestamp
            //to get the real begin:
            $real_begin = clone $begin;
            if ($booking->preparation_time > 0) {
                $real_begin->sub(new \DateInterval('PT' . ($booking->preparation_time / 60 ) . 'M'));
            }
            $booking->begin = $real_begin->getTimestamp();
            $booking->end = $end->getTimestamp();
        }
        if ($resource_id) {
            //The resource-ID has changed:
            //The booking was moved from one resource to another.
            $booking->resource_id = $resource_id;
        }

        //Update the booking_user_id field:
        $booking->booking_user_id = \User::findCurrent()->id;

        try {
            $booking->store();
            return $this->sendReturnData($booking->toRawArray());
        } catch (\Exception $e) {
            $this->halt(500, $e->getMessage());
        }
    }


    /**
     * Retrieves the intervals of the resource booking.
     * These can be filtered by a time range.
     *
     * @get /resources/booking/:booking_id/intervals
     */
    public function getIntervals($booking_id)
    {
        $booking = \ResourceBooking::find($booking_id);
        if (!$booking) {
            $this->notFound('Resource booking object not found!');
        }

        $current_user = \User::findCurrent();

        $resource = $booking->resource->getDerivedClassInstance();
        if (!$resource->bookingPlanVisibleForUser($current_user)) {
            throw new \AccessDeniedException();
        }

        //Get begin and end:
        $begin_str = \Request::get('begin');
        $end_str = \Request::get('end');
        $begin = null;
        $end = null;
        if ($begin_str && $end_str) {
            //Try the ISO format first: YYYY-MM-DDTHH:MM:SS±ZZ:ZZ
            $begin = \DateTime::createFromFormat(\DateTime::RFC3339, $begin_str);
            $end = \DateTime::createFromFormat(\DateTime::RFC3339, $end_str);
            if (!($begin instanceof \DateTime) || !($end instanceof \DateTime)) {
                $tz = new \DateTime();
                $tz = $tz->getTimezone();
                //Try the ISO format without timezone:
                $begin = \DateTime::createFromFormat('Y-m-d\TH:i:s', $begin_str, $tz);
                $end = \DateTime::createFromFormat('Y-m-d\TH:i:s', $end_str, $tz);
            }
        }

        $sql = "booking_id = :booking_id ";
        $sql_data = ['booking_id' => $booking->id];
        if (($begin instanceof \DateTime) && ($end instanceof \DateTime)) {
            $sql .= "AND begin >= :begin AND end <= :end ";
            $sql_data['begin'] = $begin->getTimestamp();
            $sql_data['end'] = $end->getTimestamp();
        }
        if (\Request::submitted('exclude_cancelled_intervals')) {
            $sql .= "AND takes_place = '1' ";
        }
        $sql .= "ORDER BY begin ASC, end ASC";
        $intervals = \ResourceBookingInterval::findBySql($sql, $sql_data);

        $result = [];
        foreach ($intervals as $interval) {
            $result[] = $interval->toRawArray();
        }

        return $result;
    }
}