aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Schemas/Courseware/Instance.php
blob: dd8f4cb9cd2e2ff7f05acfa75c3cec387ea2a3f6 (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
<?php

namespace JsonApi\Schemas\Courseware;

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

class Instance extends SchemaProvider
{
    const TYPE = 'courseware-instances';

    const REL_BOOKMARKS = 'bookmarks';
    const REL_ROOT = 'root';

    /**
     * {@inheritdoc}
     */
    public function getId($resource): ?string
    {
        $root = $resource->getRoot();
        $unit = \Courseware\Unit::findOneBySQL('structural_element_id = ?', [$root->id]);

        return implode('_', [
            $root->range_type,
            $root->range_id,
            $unit->id ?? '',
        ]);
    }

    /**
     * {@inheritdoc}
     *
     * @param \Courseware\Instance $resource
     */
    public function getAttributes($resource, ContextInterface $context): iterable
    {
        $user = $this->currentUser;

        return [
            'block-types' => array_map([$this, 'mapBlockType'], $resource->getBlockTypes()),
            'container-types' => array_map([$this, 'mapContainerType'], $resource->getContainerTypes()),
            'favorite-block-types' => $resource->getFavoriteBlockTypes($user),
            'root-layout' => $resource->getRootLayout(),
            'sequential-progression' => $resource->getSequentialProgression(),
            'editing-permission-level' => $resource->getEditingPermissionLevel(),
            'show-feedback-popup' => $resource->getShowFeedbackPopup(),
            'show-feedback-in-contentbar' => $resource->getShowFeedbackInContentbar(),
            'certificate-settings' => $resource->getCertificateSettings(),
            'reminder-settings' => $resource->getReminderSettings(),
            'reset-progress-settings' => $resource->getResetProgressSettings(),
            'root-id' => $resource->getRoot()->id,
            'is-teacher' => $GLOBALS['perm']->have_studip_perm($resource->getEditingPermissionLevel(), $resource->getRoot()->range_id),
            'linked-units' => $resource->getLinkedUnits()
        ];
    }

    /**
     * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
     */
    private function mapBlockType(string $typeClass): array
    {
        return [
            'type' => $typeClass::getType(),
            'title' => $typeClass::getTitle(),
            'description' => $typeClass::getDescription(),
            'categories' => $typeClass::getCategories(),
            'content_types' => $typeClass::getContentTypes(),
            'file_types' => $typeClass::getFileTypes(),
            'tags' => $typeClass::getTags(),
            'is-activated' => $typeClass::isActivated(),
        ];
    }

    /**
     * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
     */
    private function mapContainerType(string $typeClass): array
    {
        return [
            'type' => $typeClass::getType(),
            'title' => $typeClass::getTitle(),
            'description' => $typeClass::getDescription(),
            'is-activated' => $typeClass::isActivated(),
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getRelationships($resource, ContextInterface $context): iterable
    {
        $relationships = [];

        $user = $this->currentUser;
        $relationships[self::REL_BOOKMARKS] = [
            self::RELATIONSHIP_LINKS_SELF => true,
            self::RELATIONSHIP_LINKS => [
                Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_BOOKMARKS),
            ],
            self::RELATIONSHIP_DATA => $resource->getUsersBookmarks($user),
        ];

        $relationships[self::REL_ROOT] = [
            self::RELATIONSHIP_LINKS => [
                Link::RELATED => $this->createLinkToResource($resource->getRoot()),
            ],
            self::RELATIONSHIP_DATA => $resource->getRoot(),
        ];

        return $relationships;
    }
}