aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/components/courseware/blocks/CoursewareLtiBlock.vue
blob: 626a1a020313db5bc2072396f17a8760e0050c16 (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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<template>
    <div class="cw-block cw-block-lti">
        <courseware-default-block
            :block="block"
            :canEdit="canEdit"
            :isTeacher="isTeacher"
            :preview="false"
            @showEdit="initCurrentData"
            @storeEdit="storeBlock"
            @closeEdit="initCurrentData"
        >
            <template #content>
                <div v-if="currentTitle !== ''" class="cw-block-title">{{ currentTitle }}</div>
                <iframe
                    v-if="toolId !== ''"
                    class="cw-block-lti-content"
                    :src="iframeUrl"
                    :height="currentHeight"
                    width="100%"
                    allowfullscreen
                    sandbox="allow-downloads allow-forms allow-popups allow-pointer-lock allow-same-origin allow-scripts"
                />
                <div v-else class="cw-block-lti-content">
                    <span class="cw-block-lti-icon-tool">
                        {{ $gettext('Kein LTI-Tool konfiguriert') }}
                    </span>
                </div>
            </template>
            <template v-if="canEdit" #edit>
                <courseware-tabs>
                    <courseware-tab :index="0" :name="$gettext('Grunddaten')" :selected="true">
                        <form class="default" @submit.prevent="">
                            <label>
                                {{ $gettext('Titel') }}
                                <input type="text" v-model="currentTitle" />
                            </label>
                            <label>
                                {{ $gettext('Auswahl des externen Tools') }}
                                <select v-model="currentToolId">
                                    <option v-for="tool in tools" :key="tool.id" :value="tool.id">
                                        {{ tool.name }}
                                    </option>
                                    <option value="0">{{ $gettext('Zugangsdaten selbst eingeben...') }}</option>
                                </select>
                            </label>
                            <label v-show="allowCustomUrl">
                                {{ $gettext('URL der Anwendung (optional)') }}
                                <studip-tooltip-icon
                                    :text="$gettext('Sie können direkt auf eine URL in der Anwendung verlinken.')"
                                />
                                <input type="text" v-model="currentLaunchUrl" :placeholder="currentTool?.launch_url" />
                            </label>

                            <div v-show="customToolSelected">
                                <label class="studiprequired">
                                    {{ $gettext('URL der Anwendung') }}
                                    <span
                                        class="asterisk"
                                        :title="$gettext('Dies ist ein Pflichtfeld')"
                                        aria-hidden="true"
                                        >*</span
                                    >
                                    <studip-tooltip-icon
                                        :text="
                                            $gettext(
                                                'Die Betreiber dieses Tools müssen Ihnen eine URL und Zugangsdaten (Consumer-Key und Consumer-Secret) mitteilen.'
                                            )
                                        "
                                    />
                                    <input type="text" v-model="currentLaunchUrl" required />
                                </label>
                                <label class="studiprequired">
                                    {{ $gettext('Consumer-Key des LTI-Tools') }}
                                    <span
                                        class="asterisk"
                                        :title="$gettext('Dies ist ein Pflichtfeld')"
                                        aria-hidden="true"
                                        >*</span
                                    >
                                    <input type="text" v-model="currentConsumerKey" required />
                                </label>
                                <label class="studiprequired">
                                    {{ $gettext('Consumer-Secret des LTI-Tools') }}
                                    <span
                                        class="asterisk"
                                        :title="$gettext('Dies ist ein Pflichtfeld')"
                                        aria-hidden="true"
                                        >*</span
                                    >
                                    <input type="text" v-model="currentConsumerSecret" required />
                                </label>
                                <label>
                                    {{ $gettext('OAuth Signatur Methode des LTI-Tools') }}
                                    <select v-model="currentOauthSignatureMethod">
                                        <option value="sha1">HMAC-SHA1</option>
                                        <option value="sha256">HMAC-SHA256</option>
                                    </select>
                                </label>
                                <label>
                                    <input type="checkbox" v-model="currentSendLisPerson" />
                                    {{ $gettext('Nutzerdaten an LTI-Tool senden') }}
                                    <studip-tooltip-icon
                                        :text="
                                            $gettext(
                                                'Nutzerdaten dürfen nur an das externe Tool gesendet werden, wenn es keine Datenschutzbedenken gibt. Mit Setzen des Hakens bestätigen Sie, dass die Übermittlung der Daten zulässig ist.'
                                            )
                                        "
                                    />
                                </label>
                            </div>
                        </form>
                    </courseware-tab>
                    <courseware-tab :index="1" :name="$gettext('Zusätzliche Einstellungen')">
                        <form class="default" @submit.prevent="">
                            <label>
                                {{ $gettext('Höhe') }}
                                <input type="number" v-model="currentHeight" min="0" />
                            </label>
                            <label>
                                {{ $gettext('Zusätzliche LTI-Parameter') }}
                                <studip-tooltip-icon
                                    :text="$gettext('Ein Wert pro Zeile, Beispiel: Review:Chapter=1.2.56')"
                                />
                                <textarea v-model="currentCustomParameters" />
                            </label>
                        </form>
                    </courseware-tab>
                </courseware-tabs>
            </template>
            <template #info>
                <p>{{ $gettext('Informationen zum LTI-Block') }}</p>
            </template>
        </courseware-default-block>
    </div>
</template>

<script>
import BlockComponents from './block-components.js';
import blockMixin from '@/vue/mixins/courseware/block.js';
import { mapActions, mapGetters } from 'vuex';

export default {
    name: 'courseware-lti-block',
    mixins: [blockMixin],
    components: Object.assign(BlockComponents, {}),
    props: {
        block: Object,
        canEdit: Boolean,
        isTeacher: Boolean,
    },
    data() {
        return {
            currentTitle: '',
            currentHeight: '',
            currentToolId: '',
            currentLaunchUrl: '',
            currentConsumerKey: '',
            currentConsumerSecret: '',
            currentOauthSignatureMethod: '',
            currentSendLisPerson: false,
            currentCustomParameters: '',
        };
    },
    computed: {
        ...mapGetters({
            urlHelper: 'urlHelper',
            ltiTools: 'lti-tools/all',
        }),
        title() {
            return this.block?.attributes?.payload?.title;
        },
        height() {
            return this.block?.attributes?.payload?.height;
        },
        tools() {
            return this.ltiTools.map((tool) => ({
                id: tool.id,
                name: tool.attributes.name,
                launch_url: tool.attributes['launch-url'],
                allow_custom_url: tool.attributes['allow-custom-url'],
            }));
        },
        toolId() {
            return this.block?.attributes?.payload?.tool_id;
        },
        currentTool() {
            return this.tools.find((tool) => tool.id === this.currentToolId);
        },
        allowCustomUrl() {
            return this.currentTool?.allow_custom_url;
        },
        customToolSelected() {
            return this.currentToolId === '0';
        },
        launchUrl() {
            return this.block?.attributes?.payload?.launch_url;
        },
        consumerKey() {
            return this.block?.attributes?.payload?.consumer_key;
        },
        consumerSecret() {
            return this.block?.attributes?.payload?.consumer_secret;
        },
        oauthSignatureMethod() {
            return this.block?.attributes?.payload?.oauth_signature_method ?? 'sha1';
        },
        sendLisPerson() {
            return this.block?.attributes?.payload?.send_lis_person;
        },
        customParameters() {
            return this.block?.attributes?.payload?.custom_parameters;
        },
        iframeUrl() {
            return this.urlHelper.getURL('dispatch.php/courseware/lti/iframe/' + this.block.id);
        },
    },
    async mounted() {
        await this.loadLtiTools();
        this.initCurrentData();
    },
    methods: {
        ...mapActions({
            updateBlock: 'updateBlockInContainer',
            loadLtiTools: 'lti-tools/loadAll',
            companionWarning: 'companionWarning',
        }),
        initCurrentData() {
            this.currentTitle = this.title;
            this.currentHeight = this.height;
            this.currentToolId = this.toolId !== '' ? this.toolId : this.currentToolId; // keep preselected tool
            this.currentLaunchUrl = this.launchUrl;
            this.currentConsumerKey = this.consumerKey;
            this.currentConsumerSecret = this.consumerSecret;
            this.currentOauthSignatureMethod = this.oauthSignatureMethod;
            this.currentSendLisPerson = Boolean(this.sendLisPerson); // prevent undefined value
            this.currentCustomParameters = this.customParameters;
        },
        storeBlock() {
            // require url, key and secret if custom tool is selected
            if (this.currentToolId === '0') {
                if (!this.currentLaunchUrl) {
                    this.companionWarning({
                        info: this.$gettext('Bitte geben Sie eine URL der Anwendung an.'),
                    });
                    return false;
                }
                if (!this.currentConsumerKey) {
                    this.companionWarning({
                        info: this.$gettext('Bitte geben Sie den Consumer-Key des LTI-Tools an.'),
                    });
                    return false;
                }
                if (!this.currentConsumerSecret) {
                    this.companionWarning({
                        info: this.$gettext('Bitte geben Sie den Consumer-Secret des LTI-Tools an.'),
                    });
                    return false;
                }
            }

            let attributes = {};
            attributes.payload = {};
            attributes.payload.title = this.currentTitle;
            attributes.payload.height = this.currentHeight;
            attributes.payload.tool_id = this.currentToolId;
            attributes.payload.launch_url = this.currentLaunchUrl;
            if (this.currentToolId === '0') {
                attributes.payload.consumer_key = this.currentConsumerKey;
                attributes.payload.consumer_secret = this.currentConsumerSecret;
                attributes.payload.oauth_signature_method = this.currentOauthSignatureMethod;
                attributes.payload.send_lis_person = this.currentSendLisPerson;
            }
            attributes.payload.custom_parameters = this.currentCustomParameters;

            this.updateBlock({
                attributes: attributes,
                blockId: this.block.id,
                containerId: this.block.relationships.container.data.id,
            });
        },
    },
    watch: {
        tools(value) {
            // Preselect tool
            if (this.currentToolId === '') {
                if (value.length > 0) {
                    // Preselect first tool
                    this.currentToolId = value[0].id;
                } else {
                    // Preselect custom tool
                    this.currentToolId = '0';
                }
            }
        },
    },
};
</script>
<style scoped lang="scss">
@import '../../../../assets/stylesheets/scss/courseware/blocks/lti.scss';
</style>