aboutsummaryrefslogtreecommitdiff
path: root/app/routes/Feedback.php
blob: 9a2834779742d06813e8ba496928eecc0c1edd88 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php

namespace RESTAPI\Routes;

/**
 * @author     Nils Gehrke <nils.gehrke@uni-goettingen.de>
 * @deprecated Since Stud.IP 5.0. Will be removed in Stud.IP 6.0.
 *
 * @condition feedback_id ^\d*$
 * @condition course_id ^[a-f0-9]{32}$
 *
 */
class Feedback extends \RESTAPI\RouteMap
{
    /**
     * Create feedback element for a range
     *
     * @post /feedback/range/:range_id/:range_type
     *
     */
    public function createFeedbackElement($range_id, $range_type)
    {
        $course_id = $range_type::find($range_id)->getRangeCourseId();
        if (!\Feedback::hasRangeAccess($range_id, $range_type) || !\Feedback::hasCreatePerm($course_id)) {
            $this->error(403);
        }
        $feedback = \FeedbackElement::build([
            'range_id'          => $range_id,
            'range_type'        => $range_type,
            'user_id'           => $GLOBALS['user']->id,
            'course_id'         => $course_id,
            'question'          => $this->data['question'],
            'description'       => $this->data['description'],
            'results_visible'   => intval($this->data['results_visible']),
            'commentable'       => intval($this->data['commentable']),
            'mode'              => $this->data['mode']
        ]);
        $feedback->store();
        return $feedback->toArray();
    }

    /**
     * Get a feedback element
     *
     * @get /feedback/:feedback_id
     *
     */
    public function getFeedbackElement($feedback_id)
    {
        if (!$feedback = \FeedbackElement::find($feedback_id)) {
            $this->error(404);
        }
        if (!\Feedback::hasRangeAccess($feedback->range_id, $feedback->range_type)) {
            $this->error(403);
        }
        return $feedback->toArray();
    }


    /**
     * Get all entries of a feedback element
     *
     * @get /feedback/:feedback_id/entries
     *
     */
    public function getFeedbackEntries($feedback_id)
    {
        if (!$feedback = \FeedbackElement::find($feedback_id)) {
            $this->error(404);
        }
        if (!\Feedback::hasRangeAccess($feedback->range_id, $feedback->range_type)) {
            $this->error(403);
        }
        if ($feedback->results_visible == 1 && !$feedback->isFeedbackable()) {
            foreach($feedback->entries as $entry) {
                $result['entries'][] = $entry->toArray();
            }
        } elseif (!$feedback->isFeedbackable()) {
            $result['entries'][] = $feedback->getOwnEntry()->toArray();
        } else {
            $result = [];
        }

        return $result;
    }

    /**
     * Edit a feedback element
     *
     * @put /feedback/:feedback_id
     *
     */
    public function editFeedbackElement($feedback_id)
    {
        if (!$feedback = \FeedbackElement::find($feedback_id)) {
            $this->error(404);
        }
        $course_id = $feedback->course_id;
        if (!\Feedback::hasRangeAccess($feedback->range_id, $feedback->range_type) || !\Feedback::hasAdminPerm($course_id)) {
            $this->error(403);
        }
        $feedback->question = $this->data['question'] !== null ? $this->data['question'] : $feedback->question;
        $feedback->description = $this->data['description'] !== null ? $this->data['description'] : $feedback->description;
        $feedback->results_visible = $this->data['results_visible'] !== null ?
            intval($this->data['results_visible']) : $feedback->results_visible;
        $feedback->store();
        return $feedback->toArray();
    }

    /**
     * Delete a feedback element
     *
     * @delete /feedback/:feedback_id
     *
     */
    public function deleteFeedbackElement($feedback_id)
    {
        if (!$feedback = \FeedbackElement::find($feedback_id)) {
            $this->error(404);
        }
        $course_id = $feedback->course_id;
        if (!\Feedback::hasRangeAccess($feedback->range_id, $feedback->range_type) || !\Feedback::hasAdminPerm($course_id)) {
            $this->error(403);
        }
        $feedback->delete();
        $this->halt(200);
    }

    /**
     * List all feedback elements for a range
     *
     * @get /feedback/range/:range_id/:range_type
     *
     * @param string $range_id
     * @param string $range_type
     */
    public function getFeedbackElementsForRange($range_id, $range_type)
    {
        if (!\Feedback::hasRangeAccess($range_id, $range_type)) {
            $this->error(403, 'You may not access the given range object.');
        }
        $feedback_elements = \FeedbackElement::findBySQL('range_id = ? AND range_type = ?  ORDER BY mkdate DESC', [$range_id, $range_type]);
        foreach($feedback_elements as $feedback) {
            $result['feedback_elements'][] = $feedback->toArray();
        }
        return $result;
    }

    /**
     * List all feedback elements of a course
     *
     * @get /course/:course_id/feedback
     *
     */
    public function getFeedbackElementsForCourse($course_id)
    {
        if (!\Feedback::hasAdminPerm($course_id)) {
            $this->error(403, 'You may not list all feedback elements of the course. Only feedback admins can.');
        }
        $feedback_elements  = \FeedbackElement::findBySQL('course_id = ? ORDER BY mkdate DESC', [$course_id]);
        foreach($feedback_elements as $feedback) {
            $result['feedback_elements'][] = $feedback->toArray();
        }
        return $result;
    }

    /**
     * add an entry for a feedback element
     *
     * @post /feedback/:feedback_id/entry
     *
     */
    public function addFeedbackEntry($feedback_id)
    {
        if (!$feedback = \FeedbackElement::find($feedback_id)) {
            $this->error(404);
        }
        if (!$feedback->isFeedbackable()) {
            $this->error(403, 'You may not add an entry here. Maybe you have already given feedback or you are the author of the feedback element.');
        }
        $entry = \FeedbackEntry::build([
            'feedback_id'   => $feedback->id,
            'user_id'       => $GLOBALS['user']->id
        ]);

        $entry->rating = $this->getRating(
            $feedback->mode,
            (int) $this->data['rating']
        );

        if ($feedback->commentable) {
            $entry->comment = $this->data['comment'];
        }

        $entry->store();
        return $entry->toArray();
    }

    /**
     * edit an entry of a feedback element
     *
     * @put /feedback/entry/:entry_id
     *
     */
    public function editFeedbackEntry($entry_id)
    {
        $entry = \FeedbackEntry::find($entry_id);

        if (!$entry) {
            $this->notFound();
        }

        if (!$entry->isEditable()) {
            $this->error(403);
        }

        $entry->rating = $this->getRating(
            $entry->feedback->mode,
            (int) $this->data['rating']
        );

        if ($entry->feedback->commentable) {
            $entry->comment = $this->data['comment'] ?? $entry->comment;
        }

        $entry->store();
        return $entry->toArray();
    }

    /**
     * delete an entry of a feedback element
     *
     * @delete /feedback/entry/:entry_id
     *
     */
    public function deleteFeedbackEntry($entry_id)
    {
        if (!$entry = \FeedbackEntry::find($entry_id)) {
            $this->error(404);
        }
        if ($entry->delete()){
            $this->halt(200);
        }
    }

    /**
     * @param int $mode
     * @param int $rating
     * @return int
     */
    private function getRating(int $mode, int $rating): int
    {
        if ($mode === 0) {
            return 0;
        }

        if ($rating === 0) {
            return 1;
        }

        if ($mode === 1) {
            return min(5, $rating);
        }

        if ($mode === 2) {
            return min(10, $rating);
        }

        throw new \InvalidArgumentException("Invalid mode {$mode}");
    }
}