aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Schemas/Module.php
blob: f4800692952f5d8c729040fc69cfbb2587de1462 (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
<?php
namespace JsonApi\Schemas;

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

class Module extends SchemaProvider
{
    const REL_DEPARTMENTS = 'departments';
    const REL_RESPONSIBLE_DEPARTMENT = 'responsible-department';
    const REL_SOURCE_MODULE = 'source-module';
    const REL_VARIANT_MODULE = 'variant-module';
    const REL_START_SEMESTER = 'start-semester';
    const REL_END_SEMESTER = 'end-semester';
    const REL_MODULE_COMPONENTS = 'module-components';
    const REL_LANGUAGES = 'languages';

    const TYPE = 'modules';

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

    public function getAttributes($resource, ContextInterface $context): iterable
    {
        return [
            'display-name' => (string) $resource->getDisplayName(),
            'code' => (string) $resource->code,
            'date-of-decision' => $resource->beschlussdatum ? date('c', $resource->beschlussdatum) : '',
            'edition-number' => (string) $resource->fassung_nr,
            'edition-type' => \Config::get()->MVV_STGTEILVERSION['FASSUNG_TYP'][$resource->fassung_typ] ?? '',
            'version-number' => (string) $resource->version,
            'semester-duration' => (string) $resource->dauer,
            'capacity' => (string) $resource->kapazitaet,
            'cp' => $resource->kp,
            'workload-self' => (string) $resource->wl_selbst,
            'workload-exam' => (string) $resource->wl_pruef,
            'examination-period' => \Config::get()->MVV_MODUL['PRUEF_EBENE']['values'][$resource->pruef_ebene] ?? '',
            'grade-factor' => (string) $resource->faktor_note,
        //    'module-responsible' => (string) $resource->verantwortlich,
            'foreign-key' => (string) $resource->flexnow_modul,
            'name' => (string) $resource->deskriptoren->bezeichnung,
            'responsible' => (string) $resource->deskriptoren->verantwortlich,
            'prerequisite' => (string) $resource->deskriptoren->voraussetzung,
            'objectives' => (string) $resource->deskriptoren->kompetenzziele,
            'content' => (string) $resource->deskriptoren->inhalte,
            'literature' => (string) $resource->deskriptoren->literatur,
            'links' => (string) $resource->deskriptoren->links,
            'comment' => (string) $resource->deskriptoren->kommentar,
            'cycle' => (string) $resource->deskriptoren->turnus,
            'comment-capacity' => (string) $resource->deskriptoren->kommentar_kapazitaet,
            'comment_sws' => (string) $resource->deskriptoren->kommentar_sws,
            'type' => get_class($resource),
            'stat' => $resource->stat,
            'status' => \Config::get()->MVV_MODUL['STATUS']['values'][$resource->stat],
        ];
    }

    public function getRelationships($resource, ContextInterface $context): iterable
    {
        $relationships = [];

        if ($semester = $this->getStartSemester($resource)) {
            $relationships[self::REL_START_SEMESTER] = $semester;
        }
        if ($semester = $this->getEndSemester($resource)) {
            $relationships[self::REL_END_SEMESTER] = $semester;
        }
        if ($responsible_department = $this->getResponsibleDepartment($resource)) {
            $relationships[self::REL_RESPONSIBLE_DEPARTMENT] = $responsible_department;
        }
        /*
        if (!empty($resource->responsible_institute)) {
            $relationships[self::REL_RESPONSIBLE_DEPARTMENT] =
                 [
                     self::RELATIONSHIP_LINKS => [
                         Link::RELATED => $this->createLinkToResource($resource->responsible_institute->institute),
                     ],
                     self::RELATIONSHIP_DATA => $resource->responsible_institute,
                 ];
        }
        */
/*
        $relationships = $this->addResponsibleDepartmentRelationship(
            $relationships,
            $resource,
            $this->shouldInclude($context, self::REL_RESPONSIBLE_DEPARTMENT)
        );
*/
        $relationships = $this->addDepartments(
            $relationships,
            $resource,
            $this->shouldInclude($context, self::REL_DEPARTMENTS)
        );
        $relationships = $this->addModuleComponentsRelationship(
            $relationships,
            $resource,
            $this->shouldInclude($context, self::REL_MODULE_COMPONENTS)
        );

        return $relationships;
    }

    private function getStartSemester(\Modul $modul)
    {
        if (!$semester = \Semester::find($modul->start)) {
            return null;
        }

        return [
            self::RELATIONSHIP_LINKS => [
                Link::RELATED => $this->createLinkToResource($semester),
            ],
            self::RELATIONSHIP_DATA => $semester,
        ];
    }

    private function getEndSemester(\Modul $modul)
    {
        $semester = \Semester::find($modul->end);
        if (!$semester) {
            return null;
        }

        return [
            self::RELATIONSHIP_LINKS => [
                Link::RELATED => $this->createLinkToResource($semester),
            ],
            self::RELATIONSHIP_DATA => $semester,
        ];
    }

    private function getResponsibleDepartment(\Modul $modul)
    {
        $responsible_department = \Institute::build(['id' => $modul->responsible_institute->institut_id]);
        if (!$responsible_department) {
            return null;
        }

        return [
            self::RELATIONSHIP_LINKS => [
                Link::RELATED => $this->createLinkToResource($responsible_department),
            ],
            self::RELATIONSHIP_DATA => $responsible_department,
        ];
    }

    private function addDepartments(array $relationships, $resource, $includeData)
    {
        $departments = $resource->assigned_institutes->orderBy('position')->map(function (\ModulInst $module_inst) {
            return \Institute::build(['id' => $module_inst->institut_id]);
        });

        $relationships[self::REL_DEPARTMENTS][self::RELATIONSHIP_DATA] = $departments;

        return $relationships;
    }

    private function addModuleComponentsRelationship(array $relationships, $resource, $includeData)
    {
        $relationships[self::REL_MODULE_COMPONENTS] = [
            self::RELATIONSHIP_LINKS => [
                Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_MODULE_COMPONENTS),
            ],
        ];

        $relationships[self::REL_MODULE_COMPONENTS][self::RELATIONSHIP_DATA] = $resource->modulteile;

        return $relationships;
    }
}