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

use Config;
use Resource;
use Room;
use SingleCalendar;
use SingleDate;
use Seminar;
use Issue;
use CalendarExport;
use CalendarWriterICalendar;

/**
 * @author     André Klaßen <andre.klassen@elan-ev.de>
 * @author     <mlunzena@uos.de>
 * @license    GPL 2 or later
 * @deprecated Since Stud.IP 5.0. Will be removed in Stud.IP 6.0.
 *
 * @condition course_id ^[a-f0-9]{1,32}$
 * @condition user_id ^[a-f0-9]{1,32}$
 * @condition semester_id ^[a-f0-9]{1,32}$
 */
class Events extends \RESTAPI\RouteMap
{
    public function before($router, &$handler, &$parameters)
    {
        require_once 'lib/calendar/CalendarExportFile.class.php';
        require_once 'lib/calendar/CalendarWriterICalendar.class.php';
    }

    /**
     * returns all upcoming events within the next two weeks for a given user
     *
     * @get /user/:user_id/events
     */
    public function getEvents($user_id)
    {
        if ($user_id !== $GLOBALS['user']->id) {
            $this->error(401);
        }

        $start = time();
        $end   = strtotime('+2 weeks', $start);
        $list = SingleCalendar::getEventList($user_id, $start, $end, null, [], [
            'CourseEvent',
            'CourseCancelledEvent',
            'CourseMarkedEvent',
        ]);

        $json = [];
        $events = array_slice($list, $this->offset, $this->limit); ;
        foreach ($events as $event) {
            $singledate = new SingleDate($event->id);

            $course_uri = $this->urlf('/course/%s', [htmlReady($event->getSeminarId())]);

            $json[] = [
                'event_id'    => $event->id,
                'course'      => $course_uri,
                'start'       => $event->getStart(),
                'end'         => $event->getEnd(),
                'title'       => $event->getTitle(),
                'description' => $event->getDescription() ?: '',
                'categories'  => $event->toStringCategories() ?: '',
                'room'        => html_entity_decode(strip_tags($singledate->getRoom() ?: $singledate->getFreeRoomText() ?: '')),
                'canceled'    => $singledate->isHoliday() ?: false,
            ];
        }

        $this->etag(md5(serialize($json)));

        return $this->paginated($json, count($list), compact('user_id'));
    }

    /**
     *  returns an iCAL Export of all events for a given user
     *
     * @get /user/:user_id/events.ics
     */
    public function getEventsICAL($user_id)
    {
        if ($user_id !== $GLOBALS['user']->id) {
            $this->error(401);
        }
        $calender_writer = new CalendarWriterICalendar();
        $export = new CalendarExport($calender_writer);
        $export->exportFromDatabase($user_id, 0, 2114377200, 'ALL_EVENTS');

        if ($GLOBALS['_calendar_error']->getMaxStatus(\ErrorHandler::ERROR_CRITICAL)) {
            $this->halt(500);
        }

        $content = implode($export->getExport());

        $this->contentType('text/calendar');
        $this->headers([
            'Content-Length'      => strlen($content),
            'Content-Disposition' => 'attachment; ' . encode_header_parameter('filename', 'studip.ics'),
        ]);
        $this->halt(200, $this->response->headers, function () use ($content) {
            echo $content;
        });
    }


    /**
     * returns events for a given course
     *
     * @get /course/:course_id/events
     */
    public function getEventsForCourse($course_id)
    {
        if (!$GLOBALS['perm']->have_studip_perm('user', $course_id, $GLOBALS['user']->id)) {
            $this->error(401);
        }

        $seminar = new Seminar($course_id);
        $dates = getAllSortedSingleDates($seminar);
        $total = sizeof($dates);

        $events = [];
        foreach (array_slice($dates, $this->offset, $this->limit) as $date) {

            // get issue titles
            $issue_titles = [];
            if (is_array($issues = $date->getIssueIDs())) {
                foreach ($issues as $is) {
                    $issue = new Issue(['issue_id' => $is]);
                    $issue_titles[] = $issue->getTitle();
                }
            }

            $room = self::getRoomForSingleDate($date);
            $events[] = [
                'event_id'    => $date->getSingleDateID(),
                'start'       => $date->getStartTime(),
                'end'         => $date->getEndTime(),
                'title'       => $date->toString(),
                'description' => implode(', ', $issue_titles),
                'categories'  => $date->getTypeName() ?: '',
                'room'        => $room ?: '',
                'deleted'     => $date->isExTermin(),
                'canceled'    => $date->isHoliday() ?: false,
            ];
        }

        $this->etag(md5(serialize($events)));

        return $this->paginated($events, $total, compact('course_id'));
    }

    private static function getRoomForSingleDate($val) {

        /* css-Klasse auswählen, sowie Template-Feld für den Raum mit Text füllen */
        if (Config::get()->RESOURCES_ENABLE) {

            if ($val->getResourceID()) {
                $resObj = Resource::find($val->getResourceID());
                if ($resObj) {
                    $room_object = $resObj->getDerivedClassInstance();
                    if ($room_object instanceof Room) {
                        $room = _("Raum: ");
                        $room .= $room_object->getActionURL('booking_plan');
                    }
                }
            } else {
                $room = _("keine Raumangabe");

                if ($val->isExTermin()) {
                    if ($name = $val->isHoliday()) {
                        $room = '('.$name.')';
                    } else {
                        $room = '('._('fällt aus').')';
                    }
                }

                else {
                    if ($val->getFreeRoomText()) {
                        $room = '('.htmlReady($val->getFreeRoomText()).')';
                    }
                }
            }
        } else {
            $room = '';
            if ($val->getFreeRoomText()) {
                $room = '('.htmlReady($val->getFreeRoomText()).')';
            }
        }

        return html_entity_decode(strip_tags($room));
    }

}