aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Schemas/SeminarCycleDate.php
blob: 6ad9c89f7f0a70c7905689c12a042df9fbab0d2b (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
<?php

namespace JsonApi\Schemas;

use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
use Neomerx\JsonApi\Schema\Link;

class SeminarCycleDate extends SchemaProvider
{
    const TYPE = 'seminar-cycle-dates';
    const REL_OWNER = 'owner';



    public function getId($entry): ?string
    {
        return $entry->id;
    }

    public function getAttributes($entry, ContextInterface $context): iterable
    {
        $course = \Course::find($entry->seminar_id);

        return [
            'title' => self::createTitle($course),
            'description' => mb_strlen(trim($entry->description)) ? $entry->description : null,

            'start' => sprintf('%02d:%02d', $entry->start_hour, $entry->start_minute),
            'end' => sprintf('%02d:%02d', $entry->end_hour, $entry->end_minute),
            'weekday' => (int) $entry->weekday,

            'recurrence' => $this->getRecurring($entry),

            'locations' => self::createLocation($entry),
        ];
    }

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function getRelationships($entry, ContextInterface $context): iterable
    {
        $relationships = [];

        if ($course = \Course::find($entry->seminar_id)) {
            $link = $this->createLinkToResource($course);
            $relationships = [
                self::REL_OWNER => [self::RELATIONSHIP_LINKS => [Link::RELATED => $link], self::RELATIONSHIP_DATA => $course],
            ];
        }

        return $relationships;
    }

    private function getRecurring($entry)
    {
        $dateFn = function ($date) {
            return self::icalDate($date['date']);
        };

        $recurring = [
            'FREQ' => 'WEEKLY',
            'INTERVAL' => $entry->cycle + 1,
            'DTSTART' => $dateFn($entry->dates->first()),
            'UNTIL' => $dateFn($entry->dates->last()),
        ];

        if (count($entry->exdates)) {
            $recurring['EXDATES'] = $entry->exdates->map($dateFn);
        }

        return $recurring;
    }

    private static function icalDate($dateTime0)
    {
        return date('c', $dateTime0);
    }

    private static function createTitle($course)
    {
        if (!isset($course)) {
            return null;
        }

        if ($course->veranstaltungsnummer) {
            $title = sprintf('%s %s', $course->veranstaltungsnummer, $course->name);
        } else {
            $title = $course->name;
        }

        return $title;
    }

    private static function createLocation(\SeminarCycleDate $entry)
    {
        // check, if the date is assigned to a room
        if ($rooms = $entry->getMostBookedRooms()) {
            return array_unique(getPlainRooms($rooms));
        } elseif ($rooms = $entry->getMostUsedFreetextRoomNames()) {
            return $rooms;
        }

        return [];
    }
}