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

namespace JsonApi\Schemas\Courseware;

use Courseware\StructuralElement;
use Courseware\TaskGroup as TaskGroupModel;
use JsonApi\Schemas\SchemaProvider;
use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
use Neomerx\JsonApi\Schema\Identifier;
use Neomerx\JsonApi\Schema\Link;

class TaskGroup extends SchemaProvider
{
    const TYPE = 'courseware-task-groups';

    const REL_COURSE = 'course';
    const REL_LECTURER = 'lecturer';
    const REL_PEER_REVIEW_PROCESSES = 'peer-review-processes';
    const REL_SOLVERS = 'solvers';
    const REL_TARGET = 'target';
    const REL_TASK_TEMPLATE = 'task-template';
    const REL_TASKS = 'tasks';

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

    /**
     * {@inheritdoc}
     */
    public function getAttributes($resource, ContextInterface $context): iterable
    {
        return [
            'solver-may-add-blocks' => (bool) $resource['solver_may_add_blocks'],
            'title' => (string) $resource->title,
            'start-date' => date('c', $resource['start_date']),
            'end-date' => date('c', $resource['end_date']),
            'mkdate' => date('c', $resource['mkdate']),
            'chdate' => date('c', $resource['chdate']),
        ];
    }

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

        $relationships[self::REL_COURSE] = $resource['seminar_id']
            ? [
                self::RELATIONSHIP_LINKS => [
                    Link::RELATED => $this->createLinkToResource($resource->course),
                ],
                self::RELATIONSHIP_DATA => $resource->course,
            ]
            : [self::RELATIONSHIP_DATA => null];

        $relationships[self::REL_LECTURER] = $resource['lecturer_id']
            ? [
                self::RELATIONSHIP_LINKS => [
                    Link::RELATED => $this->createLinkToResource($resource->lecturer),
                ],
                self::RELATIONSHIP_DATA => $resource->lecturer,
            ]
            : [self::RELATIONSHIP_DATA => null];

        $relationships = $this->addPeerReviewProcessesRelationship($relationships, $resource, $context);

        $relationships[self::REL_SOLVERS] = [
            self::RELATIONSHIP_DATA => $resource->getSolvers(),
        ];

        $target = StructuralElement::build(['id' => $resource['target_id']]);
        $relationships[self::REL_TARGET] = [
            self::RELATIONSHIP_LINKS => [
                Link::RELATED => $this->createLinkToResource($target),
            ],
            self::RELATIONSHIP_DATA => $this->shouldInclude($context, self::REL_TARGET) ? $resource['target'] : $target,
        ];

        $taskTemplate = StructuralElement::build(['id' => $resource['task_template_id']]);
        $relationships[self::REL_TASK_TEMPLATE] = [
            self::RELATIONSHIP_LINKS => [
                Link::RELATED => $this->createLinkToResource($taskTemplate),
            ],
            self::RELATIONSHIP_DATA => $this->shouldInclude($context, self::REL_TASK_TEMPLATE)
                ? $resource['task_template']
                : $taskTemplate,
        ];

        $relationships[self::REL_TASKS] = [
            self::RELATIONSHIP_DATA => $this->shouldInclude($context, self::REL_TASKS)
                ? $resource['tasks']
                : \DBManager::get()->fetchFirst(
                    'SELECT id FROM cw_tasks WHERE task_group_id = ?',
                    [$resource->getId()],
                    function ($id) {
                        return new Identifier($id, Task::TYPE);
                    }
                ),
        ];

        return $relationships;
    }

    private function addPeerReviewProcessesRelationship(iterable $relationships, TaskGroupModel $resource, ContextInterface $context): iterable
    {
        $relationships[self::REL_PEER_REVIEW_PROCESSES] = [
            self::RELATIONSHIP_LINKS => [
                Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_PEER_REVIEW_PROCESSES),
            ],
        ];

        if ($this->shouldInclude($context, self::REL_PEER_REVIEW_PROCESSES)) {
            $relationships[self::REL_PEER_REVIEW_PROCESSES][self::RELATIONSHIP_DATA] = $resource->peer_review_processes;
        }

        return $relationships;
    }
}