aboutsummaryrefslogtreecommitdiff
path: root/app/routes/ResourceRequest.php
blob: 6d53b4671b3d11499ea056198638d4de8967aeab (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
<?php
namespace RESTAPI\Routes;

/**
 * This file contains the REST class for resource requests from 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 5.2.
 */
class ResourceRequest 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 request, if permitted.
     *
     * @post /resources/request/:request_id/move
     */
    public function move($request_id)
    {
        $request = \ResourceRequest::find($request_id);
        if (!$request) {
            $this->notFound('Resource request object not found!');
        }

        $current_user = \User::findCurrent();

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

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

        //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();
            $begin = \DateTime::createFromFormat('Y-m-d\TH:i:s', $begin_str, $tz);
            $end = \DateTime::createFromFormat('Y-m-d\TH:i:s', $end_str, $tz);
        }

        $request->begin = $begin->getTimestamp();
        $request->end = $end->getTimestamp();

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


    /**
     * Changes the reply comment of a request.
     *
     * @post /resources/request/:request_id/edit_reply_comment
     */
    public function editReplyComment($request_id)
    {
        $request = \ResourceRequest::find($request_id);
        if (!$request) {
            $this->notFound('Resource request object not found!');
        }

        $current_user = \User::findCurrent();

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

        $quiet = \Request::get('quiet');
        $new_reply_comment = \Request::get('reply_comment');

        $request->reply_comment = $new_reply_comment;

        if ($request->isDirty()) {
            try {
                $request->store();
                if ($this->quiet) {
                    return '';
                } else {
                    return $this->sendReturnData($request->toRawArray());
                }
            } catch (Exception $e) {
                $this->halt(500, $e->getMessage());
            }
        } else {
            if ($this->quiet) {
                return '';
            } else {
                return $this->sendReturnData($request->toRawArray());
            }
        }
    }


    /**
     * Changes the reply comment of a request.
     *
     * @post /resources/request/:request_id/toggle_marked
     */
    public function toggleMarkedFlag($request_id)
    {
        $request = \ResourceRequest::find($request_id);
        if (!$request) {
            $this->notFound('Resource request object not found!');
        }

        $current_user = \User::findCurrent();

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

        //Switch to the next marking state or return to the unmarked state
        //if the next marking state would be after the last defined
        //marking state.
        $request->marked = (++$request->marked % \ResourceRequest::MARKING_STATES);

        if ($request->isDirty()) {
            $request->store();
        }

        return $request;
    }
}