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

namespace JsonApi\Schemas\Courseware;

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

class Unit extends SchemaProvider
{
    const TYPE = 'courseware-units';

    const REL_CREATOR= 'creator';
    const REL_RANGE = 'range';
    const REL_STRUCTURAL_ELEMENT = 'structural-element';
    const REL_FEEDBACK_ELEMENT = 'feedback-element';

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

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

        return [
            'content-type' => (string) $resource['content_type'],
            'position' => (int) $resource['position'],
            'public' => (int) $resource['public'],
            'permission-scope' => (string) $resource['permission_scope'],
            'permission-type' => (string) $resource['permission_type'],
            'visible' => (string) $resource['visible'],
            'visible-all' => (bool) $resource['visible_all'],
            'visible-start-date' => $resource['visible_start_date'] ? date('c', $resource['visible_start_date']) : null,
            'visible-end-date' => $resource['visible_end_date'] ? date('c', $resource['visible_end_date']) : null,
            'writable' => (string) $resource['writable'],
            'writable-all' => (bool) $resource['writable_all'],
            'writable-start-date' => $resource['writable_start_date'] ? date('c', $resource['writable_start_date']) : null,
            'writable-end-date' => $resource['writable_end_date'] ? date('c', $resource['writable_end_date']) : null,
            'visible-approval' => json_decode($resource['visible_approval']),
            'writable-approval' => json_decode($resource['writable_approval']),
            'config' => json_decode($resource['config']),
            'can-read' => $resource->canRead($user),
            'can-edit' => $resource->canEdit($user),
            'can-edit-content' => $resource->canEditContent($user),
            'mkdate' => date('c', $resource['mkdate']),
            'chdate' => date('c', $resource['chdate']),
        ];
    }

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

        $relationships[self::REL_CREATOR] = $resource->creator
            ? [
                self::RELATIONSHIP_LINKS => [
                    Link::RELATED => $this->createLinkToResource($resource->creator),
                ],
                self::RELATIONSHIP_DATA => $resource->creator,
            ]
            : [self::RELATIONSHIP_DATA => null];

        $relationships[self::REL_STRUCTURAL_ELEMENT] = $resource->structural_element
            ? [
                self::RELATIONSHIP_LINKS => [
                    Link::RELATED => $this->createLinkToResource($resource->structural_element),
                ],
                self::RELATIONSHIP_DATA => $resource->structural_element,
            ]
            : [self::RELATIONSHIP_DATA => null];

        $rangeType = $resource->range_type;
        $range = $resource->$rangeType;

        $relationships[self::REL_RANGE] = $range
            ? [
                self::RELATIONSHIP_LINKS => [
                    Link::RELATED => $this->createLinkToResource($range),
                ],
                self::RELATIONSHIP_DATA => $range,
            ]
            : [self::RELATIONSHIP_DATA => null];

        $feedback = $resource->getFeedbackElement();
        $relationships[self::REL_FEEDBACK_ELEMENT] = $feedback
            ? [
                self::RELATIONSHIP_LINKS => [
                    Link::RELATED => $this->createLinkToResource($feedback),
                ],
                self::RELATIONSHIP_DATA => $feedback,
            ]
            : [self::RELATIONSHIP_DATA => null];

        return $relationships;
    }
}