aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Schemas/CourseSet.php
blob: 2c11ccc6533e610d8959e6abbb5df54a7246c73e (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
<?php

namespace JsonApi\Schemas;

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

class CourseSet extends SchemaProvider
{
    const TYPE = 'course-sets';

    const REL_RULES = 'admission-rules';
    const REL_INSTITUTES = 'institutes';
    const REL_SEMESTER = 'semester';
    const REL_COURSES = 'courses';
    const REL_OWNER = 'owner';

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

    public function getAttributes($courseset, ContextInterface $context): iterable
    {
        return [
            'name' => $courseset->getName(),
            'infotext' => $courseset->getInfoText(),
            'private' => (bool) $courseset->getPrivate(),
            'algorithm' => $courseset->getAlgorithm(),
            'algorithm-run' => (bool) $courseset->hasAlgorithmRun(),
            'num-applicants' => (int) $courseset->getNumApplicants(),
            'userlists' => (array) $courseset->getUserLists(),
            'chdate' => date('c', $courseset->getChdate()),
        ];
    }

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

        $relationships = $this->addOwnerRelationship(
            $relationships,
            $resource,
            $this->shouldInclude($context, self::REL_OWNER)
        );

        $relationships = $this->addInstitutesRelationship(
            $relationships,
            $resource,
            $this->shouldInclude($context, self::REL_INSTITUTES)
        );

        $relationships = $this->addCoursesRelationship(
            $relationships,
            $resource,
            $this->shouldInclude($context, self::REL_COURSES)
        );

        $relationships = $this->addRulesRelationship(
            $relationships,
            $resource,
            $this->shouldInclude($context, self::REL_RULES)
        );

        $relationships = $this->addSemesterRelationship(
            $relationships,
            $resource,
            $this->shouldInclude($context, self::REL_SEMESTER)
        );

        return $relationships;
    }

    private function addRulesRelationship(
        array $relationships,
              $resource,
              $includeData
    ) {
        $relation = [
            self::RELATIONSHIP_LINKS => [
                Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_RULES),
            ]
        ];
        if ($includeData) {
            $related = $resource->getAdmissionRules();
            $relation[self::RELATIONSHIP_DATA] = $related;
        }

        return array_merge($relationships, [self::REL_RULES => $relation]);
    }

    private function addOwnerRelationship(
        array $relationships,
              $resource,
              $includeData
    ) {
        $relation = [
            self::RELATIONSHIP_LINKS => [
                Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_OWNER),
            ]
        ];
        if ($includeData) {
            $related = $resource->getUserId() ? \User::find($resource->getUserId()) : null;
            $relation[self::RELATIONSHIP_DATA] = $related;
        }

        return array_merge($relationships, [self::REL_OWNER => $relation]);
    }

    private function addInstitutesRelationship(
        array $relationships,
              $resource,
              $includeData
    ) {
        $relation = [
            self::RELATIONSHIP_LINKS => [
                Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_INSTITUTES),
            ]
        ];
        if ($includeData) {
            $related = $resource->getInstituteIds() ? \Institute::findMany(array_keys($resource->getInstituteIds())) : [];
            $relation[self::RELATIONSHIP_DATA] = $related;
        }

        return array_merge($relationships, [self::REL_INSTITUTES => $relation]);
    }

    private function addCoursesRelationship(
        array $relationships,
              $resource,
              $includeData
    ) {
        $relation = [
            self::RELATIONSHIP_LINKS => [
                Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_COURSES),
            ]
        ];
        if ($includeData) {
            $related = \Course::findMany($resource->getCourses(), "ORDER BY `VeranstaltungsNummer`, `Name`");
            $relation[self::RELATIONSHIP_DATA] = $related;
        }

        return array_merge($relationships, [self::REL_COURSES => $relation]);
    }

    private function addSemesterRelationship(
        array $relationships,
              $resource,
              $includeData
    ) {
        $relation = [
            self::RELATIONSHIP_LINKS => [
                Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_SEMESTER),
            ]
        ];
        if ($includeData) {
            $related = $resource->getSemester() ? \Semester::find($resource->getSemester()) : null;
            $relation[self::RELATIONSHIP_DATA] = $related;
        }

        return array_merge($relationships, [self::REL_SEMESTER => $relation]);
    }
}