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

namespace JsonApi\Schemas\Courseware;

use Courseware\Task as TaskModel;
use JsonApi\Routes\Courseware\Authority as CoursewareAuthority;
use JsonApi\Schemas\SchemaProvider;
use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
use Neomerx\JsonApi\Schema\Link;

/**
 * @SuppressWarnings(PHPMD.StaticAccess)
 */
class Task extends SchemaProvider
{
    const TYPE = 'courseware-tasks';

    const REL_FEEDBACK = 'task-feedback';
    const REL_PEER_REVIEWS = 'peer-reviews';
    const REL_SOLVER = 'solver';
    const REL_STRUCTURAL_ELEMENT = 'structural-element';
    const REL_TASK_GROUP = 'task-group';

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

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

        return [
            'progress' => (float) $resource->getTaskProgress(),
            'submission-date' => date('c', $resource['submission_date']),
            'submitted' => (bool) $resource['submitted'],
            'renewal' => empty($resource['renewal']) ? null : (string) $resource['renewal'],
            'renewal-date' => date('c', $resource['renewal_date']),
            'visible' => (bool) $resource['visible'],
            'can-peer-review' => $resource->userIsAPeerReviewer($user),
            'can-solve' => $resource->userIsASolver($user),
            'mkdate' => date('c', $resource['mkdate']),
            'chdate' => date('c', $resource['chdate']),
        ];
    }

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

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

        $relationships = $this->addPeerReviews(
            $relationships,
            $resource,
            $this->shouldInclude($context, self::REL_PEER_REVIEWS)
        );

        $user = $this->currentUser;

        if (CoursewareAuthority::canShowTaskSolver($user, $resource)) {
            $relationships[self::REL_SOLVER] = $resource['solver_id']
                ? [
                    self::RELATIONSHIP_LINKS => [
                        Link::RELATED => $this->createLinkToResource($resource->solver),
                    ],
                    self::RELATIONSHIP_DATA => $resource->solver,
                ]
                : [self::RELATIONSHIP_DATA => null];
        } else {
            $relationships[self::REL_SOLVER] = [
                self::RELATIONSHIP_DATA => null,
            ];
        }

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

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

        return $relationships;
    }

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

        if ($includeData) {
            $relationships[self::REL_PEER_REVIEWS][self::RELATIONSHIP_DATA] = $resource->isPeerReviewed()
                ? $resource->peer_reviews->filter(
                    fn($review) => CoursewareAuthority::canShowPeerReview($this->currentUser, $review)
                )
                : [];
        }

        return $relationships;
    }
}