aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Schemas/Activity.php
blob: 6607a4d37103ebcfb40969f82e3e81ac8c33a797 (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
<?php

namespace JsonApi\Schemas;

use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
use Neomerx\JsonApi\Schema\Link;
use Studip\Activity\Activity as StudipActivity;

class Activity extends SchemaProvider
{
    const TYPE = 'activities';
    const REL_ACTOR = 'actor';
    const REL_CONTEXT = 'context';
    const REL_OBJECT = 'object';

    /**
     * Hier wird der Typ des Schemas festgelegt.
     * {@inheritdoc}
     */


    /**
     * Diese Method entscheidet über die JSON-API-spezifische ID von
     * Activity-Objekten.
     * {@inheritdoc}
     */
    public function getId($activity): ?string
    {
        return $activity->id;
    }

    /**
     * Hier können (ausgewählte) Instanzvariablen eines \Activity-Objekts
     * für die Ausgabe vorbereitet werden.
     * {@inheritdoc}
     */
    public function getAttributes($activity, ContextInterface $context): iterable
    {
        if (preg_match('/\\\\([^\\\\]+)Provider$/', $activity->provider, $matches)) {
            $activityType = strtolower($matches[1]);
        } else {
            $activityType = null;
        }

        $result = [
            'title' => $this->createTitle($activity),
            'mkdate' => date('c', $activity->mkdate),
            'content' => $activity->content,
            'verb' => $activity->verb,
            'activity-type' => $activityType,
        ];

        return $result;
    }

    /**
     * In dieser Methode können Relationships zu anderen Objekten
     * spezifiziert werden.
     * {@inheritdoc}
     */
    public function getRelationships($activity, ContextInterface $context): iterable
    {
        $relationships = [];

        $relationships = $this->getActorRelationship($relationships, $activity, $this->shouldInclude($context, 'actor'));
        $relationships = $this->getObjectRelationship($relationships, $activity, $this->shouldInclude($context, 'object'));
        $relationships = $this->getContextRelationship($relationships, $activity, $this->shouldInclude($context, 'context'));

        return $relationships;
    }

    private function getActorRelationship(array $relationships, StudipActivity $activity, $include)
    {
        $actorType = $activity->actor_type;
        $actorId = $activity->actor_id;

        if ($actorType === 'user') {
            $actor = $include ? \User::findFull($actorId) : \User::build(['id' => $actorId], false);
            $relationships[self::REL_ACTOR] = [
                self::RELATIONSHIP_LINKS => [Link::RELATED => $this->createLinkToResource($actor)],
                self::RELATIONSHIP_DATA => $actor
            ];
        }

        return $relationships;
    }

    private function getObjectRelationship(array $relationships, StudipActivity $activity, $include)
    {
        $mapping = [
            'documents' => \FileRef::class,
            'forum' => \Forum\Posting::class,
            'message' => \Message::class,
            'news' => \StudipNews::class,
            'participants' => \Course::class,
            'schedule' => \Course::class,
            'wiki' => \WikiPage::class,
        ];


        if (isset($mapping[$activity->object_type])) {
            $objectMapping = $mapping[$activity->object_type];

            if ($activity->object_type === 'wiki') {
                $data = \WikiPage::findOneBySQL('`course_id` = ? AND `name` = ?', [$activity->context_id, $activity->object_id]);
            } else {
                $data = $include
                      ? call_user_func([$objectMapping, 'find'], $activity->object_id)
                      : call_user_func([$objectMapping, 'build'], ['id' => $activity->object_id], false);
            }

            if ($data) {
                $link = $this->createLinkToResource($data);
                $relationships[self::REL_OBJECT] = [
                    self::RELATIONSHIP_LINKS => [
                        Link::RELATED => $link
                    ],
                    self::RELATIONSHIP_DATA => $data,
                ];
            }
        } else {
            $relationships[self::REL_OBJECT] = [
                self::RELATIONSHIP_META => [
                    'object-type' => $activity->object_type,
                    'object-id' => $activity->object_id,
                ],
            ];
        }

        return $relationships;
    }

    private function getContextRelationship(array $relationships, StudipActivity $activity, $include)
    {
        if ($data = $this->getContext($activity, $include)) {
            $link = $this->createLinkToResource($data);
            $relationships[self::REL_CONTEXT] = [
                self::RELATIONSHIP_LINKS => [
                    Link::RELATED => $link
                ],
                self::RELATIONSHIP_DATA => $data,
            ];
        }

        return $relationships;
    }

    private function getContext($activity, $include)
    {
        $mapping = [
            'course' => \Course::class,
            'institute' => \Institute::class,
            'user' => \User::class,
        ];

        if (!isset($mapping[$activity->context])) {
            return null;
        }

        $context = $mapping[$activity->context];
        return $include
            ? call_user_func([$context, 'find'], $activity->context_id)
            : call_user_func([$context, 'build'], ['id' => $activity->context_id], false);
    }

    private function createTitle($activity)
    {
        // add i18n auto generated title prefix
        $title = '';

        $class = $activity->provider;
        $objectText = $class::getLexicalField();

        if (in_array($activity->actor_id, array('____%system%____', 'system')) !== false) {
            $actor = _('Stud.IP');
        } else {
            $actor = get_fullname($activity->actor_id);
        }
        $contextName = $activity->getContextObject()->getContextFullname();

        switch ($activity->context) {
        case 'course':
            $title = $actor.' '
                   .sprintf($activity->verbToText(),
                             $objectText.sprintf(_(' im Kurs "%s"'), $contextName)
                   );
            break;

        case 'institute':
            $title = $actor.' '
                   .sprintf($activity->verbToText(),
                             $objectText.sprintf(_(' in der Einrichtung "%s"'), $contextName)
                   );
            break;

        case 'system':
            $title = $actor.' '
                   .sprintf($activity->verbToText(), _('allen')).' '
                   .$objectText;
            break;

        case 'user':
            $title = $actor.' '
                   .sprintf($activity->verbToText(), $contextName).' '
                   .$objectText;
            break;
        }

        return $title;
    }
}