aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/admin/lti/resources.php
blob: ca83434dce5e43bf5d5c0bfa566dec0a88dac336 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php

use Lti\Registration;
use Lti\ResourceLink;
use Lti\Config as LtiConfig;
use Studip\Lti\Enum\ConfigurableType;
use Studip\Lti\Enum\RegistrationStatus;
use Studip\Lti\Enum\ResourceLaunchContainer;
use Studip\Lti\Controller\AdminBaseController;

class Admin_Lti_ResourcesController extends AdminBaseController
{
    public function before_filter(&$action, &$args)
    {
        parent::before_filter($action, $args);

        if (Navigation::hasItem('/course/lti/index')) {
            Navigation::activateItem('/course/lti/index');
        }

        $widget = Sidebar::get()->addWidget(new ActionsWidget());

        $widget->addLink(
            _('LTI-Ressource hinzufügen'),
            $this->url_for('admin/lti/resources/create'),
            Icon::create('add')
        );
    }

    public function index_action(): void
    {
        PageLayout::setTitle(_('LTI-Ressourcen'));

        $this->render_vue_app(
            Studip\VueApp::create('lti/resources/Index')
        );
    }

    public function create_action()
    {
        PageLayout::setTitle(_('LTI-Ressource hinzufügen'));

        $this->render_vue_app(
            Studip\VueApp::create('lti/resources/Create')
                ->withProps([
                    'registrations' => $this->getTransformedRegistrations(),
                    'icons' => $this->getStudipIcons()
                ])
        );
    }

    public function store_action(): void
    {
        CSRFProtection::verifyUnsafeRequest();

        $deploymentId = Registration::find(Request::get('registration_id'))?->getDefaultDeployment()->id;

        foreach ($this->extractResourcesFromRequest() as $resource) {
            $resourceModel = ResourceLink::create([
                ...$resource,
                'deployment_id' => $deploymentId,
                'course_id' => $this->range_id
            ]);

            $this->syncResourceConfigs($resourceModel->id, $resource['configs']);
        }

        PageLayout::postSuccess(
            _('Folgende LTI-Ressourcen wurden hinzugefügt.'),
            Request::getArray('title')
        );

        $this->redirect('course/lti');
    }

    public function edit_action(ResourceLink $resourceLink): void
    {
        PageLayout::setTitle(_('LTI-Ressource bearbeiten'));

        $this->render_vue_app(
            Studip\VueApp::create('lti/resources/Edit')
                ->withProps([
                    'resource' => $resourceLink->transformData(['registration']),
                    'registrations' => $this->getTransformedRegistrations(RegistrationStatus::all()),
                    'icons' => $this->getStudipIcons()
                ])
        );
    }

    public function update_action(ResourceLink $resourceLink): void
    {
        CSRFProtection::verifyUnsafeRequest();

        $deploymentId = null;
        if (Request::get('registration_id')) {
            $deploymentId = Registration::find(Request::get('registration_id'))?->getDefaultDeployment()->id;
        }

        $resourcesArray = $this->extractResourcesFromRequest();
        if (count($resourcesArray) === 1) {
            $resourceLink->setData([
                ...$resourcesArray[0],
                'deployment_id' => $deploymentId ?? $resourceLink->deployment_id
            ]);

            $resourceLink->store();

            $this->syncResourceConfigs($resourceLink->id, $resourcesArray[0]['configs']);
        } else {
            foreach ($resourcesArray as $resource) {
                $resourceModel = ResourceLink::create([
                    ...$resource,
                    'position' => $resourceLink->position,
                    'course_id' => $resourceLink->course_id,
                    'deployment_id' => $deploymentId ?? $resourceLink->deployment_id
                ]);

                $this->syncResourceConfigs($resourceModel->id, $resource['configs']);
            }

            $resourceLink->delete();
        }

        PageLayout::postSuccess(
            _('Folgende LTI-Ressourcen wurden gespeichert.'),
            Request::getArray('title')
        );

        $this->redirect('course/lti');
    }

    public function delete_action(ResourceLink $resourceLink): void
    {
        CSRFProtection::verifyUnsafeRequest();

        $resourceTitle = $resourceLink->title;
        $resourceLink->delete();

        PageLayout::postSuccess(
            sprintf(
                _('Der LTI-Ressource „%s“ wurde gelöscht.'),
                htmlReady($resourceTitle)
            )
        );

        $this->redirect('course/lti');
    }

    private function getTransformedRegistrations(array $status = []): array
    {
        $registrations = Registration::findBySQL(
            "`role`= 'tool' AND `status` IN (:status) AND `range_id` IN (:range_ids) ORDER BY `mkdate`, `name`",
            [
                'status' => [
                    ...$status,
                    RegistrationStatus::Active->value
                ],
                'range_ids' => [$this->range_id, 'global']
            ]
        );

        return array_map(fn ($r) => $r->transformData(), $registrations);
    }

    private function getStudipIcons(): array
    {
        $icons = [];

        foreach (scandir($GLOBALS['STUDIP_BASE_PATH'] . '/public/assets/images/icons/blue') as $iconPath) {
            if (!is_dir(
                    $GLOBALS['STUDIP_BASE_PATH'] . '/public/assets/images/icons/blue/'
                ) . $iconPath && $iconPath[0] !== '.') {
                if (stripos($iconPath, '.svg')) {
                    $iconPath = substr($iconPath, 0, stripos($iconPath, '.svg'));
                }
                $icons[] = $iconPath;
            }
        }

        return array_unique($icons);
    }

    private function extractResourcesFromRequest(): array
    {
        $resources = [];

        for ($index = 0; $index < count(Request::getArray('resource_id')); $index++) {
            $resources[] = [
                'resource_id' => Request::getArray('resource_id', $index),
                'title' => Request::getArray('title', $index),
                'description' => Request::getArray('description', $index),
                'launch_url' => Request::getArray('launch_url', $index),
                'custom_parameters' => Request::getArray('custom_parameters', $index),
                'configs' => [
                    'color' => Request::getArray('color', $index) ?? null,
                    'icon' => Request::getArray('icon', $index) ?? null,
                    'grade_synchronization' => Request::getArray('grade_synchronization', $index) ?? null,
                    'launch_container' => Request::getArray('launch_container', $index) ?? ResourceLaunchContainer::Window->value,
                ]
            ];
        }

        return $resources;
    }

    private function syncResourceConfigs(int $resourceId, array $configs): void
    {
        foreach ($configs as $key => $value) {
            if (empty($value)) {
                LtiConfig::deleteBySQL(
                    "configurable_id = :configurable_id AND configurable_type = :configurable_type AND name = :name",
                    [
                        'configurable_id' => $resourceId,
                        'configurable_type' => ConfigurableType::ResourceLink->value,
                        'name' => strtolower($key)
                    ]
                );

                continue;
            }

            LtiConfig::updateOrCreate(
                [
                    'configurable_id' => $resourceId,
                    'configurable_type' => ConfigurableType::ResourceLink->value,
                    'name' => strtolower($key)
                ],
                [
                    'value' => $value
                ]
            );
        }
    }

}