aboutsummaryrefslogtreecommitdiff
path: root/app/routes/Consultations.php
blob: 1bd82c9072f3262eb899831515ab0bebb556cd1a (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
193
194
195
<?php
namespace RESTAPI\Routes;

/**
 * @author  Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @license GPL2 or any later version
 * @deprecated  Since Stud.IP 5.0. Will be removed in Stud.IP 5.2.
 */
class Consultations extends \RESTAPI\RouteMap
{
    /*
     * @get /consultations
     * @get /consultations/blocks
     * @get /consultations/slots
     * @get /consultations/bookings
     *
     * @get /users/:user_id/consultations/slots
     * @get /users/:user_id/consultations/bookings
     */
    public function getDiscovery()
    {
        $routes = $this->router->getRoutes(true);
        foreach ($routes as $uri_template => $methods) {
            foreach ($methods as $method => $route) {
                $routes[$uri_template][$method] = $route['description'];
            }
        }
        return $routes;
    }

    /**
     * @get /consultations/blocks/:block_id
     */
    public function getBlock($block_id)
    {
        return self::blockToJSON($this->requireBlock($block_id));
    }

    /**
     * @get /consultations/slots/bulk
     */
    public function getManySlots()
    {
        $ids = \Request::intArray('ids');
        if (!$ids) {
            $this->halt(400, 'No ids provided');
        }

        return \ConsultationSlot::findAndMapMany(
            function ($slot) {
                return self::slotToJSON($slot);
            },
            $ids
        );
    }

    /**
     * @get /consultations/slots/:slot_id
     */
    public function getSlot($slot_id)
    {
        return self::slotToJSON($this->requireSlot($slot_id));
    }

    /**
     * @get /consultations/bookings/:booking_id
     */
    public function getBooking($booking_id)
    {
        return self::bookingToJSON($this->requireBooking($booking_id));
    }

    /**
     * @delete /consultations/bookings/:booking_id
     */
    public function deleteBooking($booking_id)
    {
        $booking = $this->requireBooking($booking_id);

        if ($GLOBALS['user']->id === $booking->user_id) {
            $booking->delete();
        } elseif ($GLOBALS['user']->id === $booking->slot->block->teacher_id) {
        } else {
            $this->halt(403);
        }


    }

    /**
     * Finds and returns the block with the given id. If no block exists with
     * the given id, a 404 is issued.
     *
     * @param  int $block_id Id of the block
     */
    private function requireBlock($block_id)
    {
        $block = \ConsultationBlock::find($block_id);
        if (!$block) {
            $this->notFound();
        }
        return $block;
    }

    /**
     * Converts the block to it's JSON representation.
     *
     * @param  ConsultationBlock $block
     * @return array
     */
    private static function blockToJSON(\ConsultationBlock $block)
    {
        return [
            'id'         => (int) $block->id,
            'teacher_id' => $block->teacher_id,
            'start'      => (int) $block->start,
            'end'        => (int) $block->end,
            'room'       => (string) $block->room,
            'note'       => (string) $block->note,
            'size'       => (int) $block->size,
            'course_id'  => $block->course_id ?: null,
            'mkdate'     => (int) $block->mkdate,
            'chdate'     => (int) $block->chdate,
        ];
    }

    /**
     * Finds and returns the slot with the given id. If no slot exists with
     * the given id, a 404 is issued.
     *
     * @param  int $slot_id Id of the slot
     */
    private function requireSlot($slot_id)
    {
        $slot = \ConsultationSlot::find($slot_id);
        if (!$slot) {
            $this->notFound();
        }
        return $slot;
    }

    /**
     * Converts the slot to it's JSON representation.
     *
     * @param  ConsultationSlot $slot
     * @return array
     */
    private static function slotToJSON(\ConsultationSlot $slot)
    {
        return [
            'id'         => (int) $slot->id,
            'block_id'   => (int) $slot->block_id,
            'start_time' => (int) $slot->start_time,
            'end_time'   => (int) $slot->end_time,
            'note'       => (string) $slot->note,
            'mkdate'     => (int) $slot->mkdate,
            'chdate'     => (int) $slot->chdate,

            'booking_count' => count($slot->bookings),
        ];
    }

    /**
     * Finds and returns the booking with the given id. If no booking exists
     * with the given id, a 404 is issued.
     *
     * @param  int $booking_id Id of the booking
     */
    private function requireBooking($booking_id)
    {
        $booking = \ConsultationBooking::find($booking_id);
        if (!$booking) {
            $this->notFound();
        }
        return $booking;
    }

    /**
     * Converts the booking to it's JSON representation.
     *
     * @param  ConsultationBooking $booking
     * @return array
     */
    private static function bookingToJSON(\ConsultationBooking $booking)
    {
        return [
            'id'      => (int) $booking->id,
            'slot_id' => $booking->slot_id,
            'user_id' => $booking->user_id,
            'reason'  => (string) $booking->reason,
            'mkdate'  => (int) $booking->mkdate,
            'chdate'  => (int) $booking->chate,
        ];
    }
}