aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/components/courseware/blocks/CoursewareFolderBlock.vue
blob: a9e445c9088ff4d7948be8308599608670e0205e (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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<template>
    <div class="cw-block cw-block-folder">
        <courseware-default-block
            :block="block"
            :canEdit="canEdit"
            :isTeacher="isTeacher"
            :preview="true"
            @showEdit="initCurrentData"
            @storeEdit="storeBlock"
            @closeEdit="initCurrentData"
        >
            <template #content>
                <div v-if="currentTitle !== ''" class="cw-block-title">{{ currentTitle }}</div>
                <courseware-companion-box
                    v-if="folderTypeHasChanged && canEdit"
                    mood="pointing"
                    :msgCompanion="$gettext('Der Ordnertyp wurde nach der Erstellung des Blocks geändert! Bitte überprüfen Sie die Einstellungen.')"
                />
                <div v-if="isHomework" class="cw-block-folder-info">
                    <p>
                        {{
                            $gettext(
                                'Dieser Ordner ist ein Hausaufgabenordner. Es können nur Dateien eingestellt werden.'
                            )
                        }}
                    </p>
                    <p v-if="!isTeacher">
                        {{ $gettext('Sie selbst haben folgende Dateien in diesen Ordner eingestellt') }}:
                    </p>
                </div>
                <ul class="cw-block-folder-list">
                    <li v-for="file in files" :key="file.id" class="cw-block-folder-file-item">
                        <a
                            v-if="downloadEnabled"
                            target="_blank"
                            :download="file.attributes.name"
                            :title="$gettext('Datei herunterladen')"
                            :href="file.meta['download-url']"
                        >
                            <span
                                class="cw-block-file-info"
                                :class="['cw-block-file-icon-' + getIcon(file.attributes['mime-type'])]"
                            >
                                {{ file.attributes.name }}
                            </span>
                            <div v-if="isTeacher && isHomework" class="cw-block-file-details">
                                <span class="cw-block-file-owner">
                                    {{ file.relationships.owner.meta.name }}
                                </span>
                                <span class="cw-block-file-mkdate">
                                    {{ getFormattedDate(file.attributes.mkdate) }}
                                </span>
                            </div>
                        </a>
                        <template v-else>
                            <span
                                class="cw-block-file-info download-disabled"
                                :class="['cw-block-file-icon-' + getIcon(file.attributes['mime-type'])]"
                            >
                                {{ file.attributes.name }}
                            </span>
                            <div class="cw-block-file-details">
                                <span class="cw-block-file-mkdate">
                                    {{ getFormattedDate(file.attributes.mkdate) }}
                                </span>
                            </div>
                        </template>
                    </li>
                    <li v-if="files.length === 0">
                        <span class="cw-block-file-info cw-block-file-icon-empty">
                            {{ $gettext('Dieser Ordner ist leer') }}
                        </span>
                    </li>
                </ul>
                <div v-if="uploadEnabled" class="cw-block-folder-upload">
                    <form class="default" @submit.prevent="">
                        <label>
                            {{ $gettext('Dateien zum Hochladen auswählen') }}
                            <input class="cw-file-input" ref="uploadFile" type="file" @change="displayTermSelector" />
                            <button class="button" @click="uploadFile">
                                {{ $gettext('Datei hochladen') }}
                            </button>
                        </label>
                    </form>
                    <studip-dialog
                        v-if="showTermSelector"
                        width="780"
                        height="510"
                        :title="$gettext('Lizenz auswählen')"
                        :confirmText="$gettext('Speichern')"
                        confirmClass="accept"
                        :closeText="$gettext('Lizenzauswahl abbrechen')"
                        closeClass="cancel"
                        @close="showTermSelector = false"
                        @confirm="selectTerm"
                    >
                        <template v-slot:dialogContent>
                            <form class="default" @submit.prevent="">
                                <div style="margin-bottom: 1ex">
                                    {{
                                        $gettext(
                                            'Bereitgestellte Dateien können heruntergeladen und ggf. weiterverbreitet werden. Dabei ist das Urheberrecht sowohl beim Hochladen der Datei als auch bei der Nutzung zu beachten. Bitte geben Sie daher an, um welche Art von Bereitstellung es sich handelt. Diese Angabe dient mehreren Zwecken: Beim Herunterladen wird ein Hinweis angezeigt, welche Nutzung der Datei zulässig ist. Beim Hochladen stellt die Angabe eine Entscheidungshilfe dar, damit Sie sichergehen können, dass die Datei tatsächlich bereitgestellt werden darf.'
                                        )
                                    }}
                                </div>
                                <fieldset class="select_terms_of_use">
                                    <template v-for="term in termsOfUse" :key="term.id">
                                        <input
                                            type="radio"
                                            name="content_terms_of_use_id"
                                            :value="term.id"
                                            v-model="selectedTerm"
                                            :id="'content_terms_of_use-' + term.id"
                                            :checked="selectedTerm === term.id"
                                            :aria-description="term.description"
                                        />
                                        <label @click="selectedTerm = term.id">
                                            <div class="icon">
                                                <studip-icon :shape="term.attributes.icon" :size="32" />
                                            </div>
                                            <div class="text">
                                                {{ term.attributes.name }}
                                            </div>
                                            <studip-icon shape="arr_1down" :size="24" class="arrow" />
                                            <studip-icon shape="check-circle" :size="24" class="check" />
                                        </label>
                                        <div class="terms_of_use_description">
                                            <div class="description">
                                                {{ term.attributes.description }}
                                            </div>
                                        </div>
                                    </template>
                                </fieldset>
                            </form>
                        </template>
                    </studip-dialog>
                </div>
            </template>
            <template v-if="canEdit" #edit>
                <form class="default" @submit.prevent="">
                    <label>
                        {{ $gettext('Überschrift') }}
                        <input type="text" v-model="currentTitle" />
                    </label>
                    <label>
                        {{ $gettext('Ordner') }}
                        <studip-file-chooser v-model="currentFolderId" selectable="folder" :courseId="context.id" :userId="userId" />
                    </label>
                </form>
            </template>
            <template #info>
                <p>{{ $gettext('Informationen zum Dateiordner-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-folder-block',
    mixins: [blockMixin],
    components: Object.assign(BlockComponents, {}),
    props: {
        block: Object,
        canEdit: Boolean,
        isTeacher: Boolean,
    },
    data() {
        return {
            currentTitle: '',
            currentFolderId: '',
            currentFolderType: '',
            showTermSelector: false,
            selectedTerm: null,
        };
    },
    computed: {
        ...mapGetters({
            folderById: 'folders/byId',
            termsOfUse: 'terms-of-use/all',
        }),
        folderType() {
            return this.block?.attributes?.payload?.['folder-type'];
        },
        storedFolderType() {
            return this.block?.attributes?.payload?.type;
        },
        folderTypeHasChanged() {
            return this.folderType !== this.storedFolderType;
        },
        folderId() {
            return this.block?.attributes?.payload?.folder_id;
        },
        title() {
            return this.block?.attributes?.payload?.title;
        },
        files() {
            return this.block?.attributes?.payload?.files;
        },
        isHomework() {
            return this.folderType === 'HomeworkFolder';
        },
        uploadEnabled() {
            return !this.isTeacher && this.isHomework;
        },
        downloadEnabled() {
            return this.isTeacher || !this.isHomework;
        },
    },
    async mounted() {
        await this.loadTermsOfUse();
        this.initCurrentData();
    },
    methods: {
        ...mapActions({
            loadFolder: 'folders/loadById',
            loadBlock: 'courseware-blocks/loadById',
            updateBlock: 'updateBlockInContainer',
            createFile: 'createFile',
            companionWarning: 'companionWarning',
            companionSuccess: 'companionSuccess',
            companionError: 'companionError',
            loadTermsOfUse: 'terms-of-use/loadAll',
        }),
        async initCurrentData() {
            this.currentTitle = this.title;
            this.currentFolderId = this.folderId;
            if (this.$refs?.uploadFile) {
                this.$refs.uploadFile.value = null;
            }
            this.selectedTerm = this.getDefaultTerm();
        },
        async setCurrentFolderType() {
            await this.loadFolder({ id: this.currentFolderId });
            const folder = this.folderById({ id: this.currentFolderId });
            this.currentFolderType = folder?.attributes['folder-type'];
        },
        getIcon(mimeType) {
            let icon = 'file';
            if (mimeType.includes('audio')) {
                icon = 'audio';
            }
            if (mimeType.includes('image')) {
                icon = 'pic';
            }
            if (mimeType.includes('video')) {
                icon = 'video';
            }
            if (mimeType.includes('text')) {
                icon = 'text';
            }
            if (mimeType.includes('pdf')) {
                icon = 'pdf';
            }
            if (mimeType.includes('msword')) {
                icon = 'word';
            }
            if (mimeType.includes('opendocument.text')) {
                icon = 'word';
            }
            if (mimeType.includes('openxmlformats-officedocument. wordprocessingml.document')) {
                icon = 'word';
            }
            if (mimeType.includes('msexcel')) {
                icon = 'spreadsheet';
            }
            if (mimeType.includes('opendocument.spreadsheet')) {
                icon = 'spreadsheet';
            }
            if (mimeType.includes('openxmlformats-officedocument. spreadsheetml.sheet')) {
                icon = 'spreadsheet';
            }
            if (mimeType.includes('mspowerpoint')) {
                icon = 'ppt';
            }
            if (mimeType.includes('zip')) {
                icon = 'archive';
            }

            return icon;
        },
        getFormattedDate(unformattedDate) {
            const date = new Date(unformattedDate);
            const localeDate = date.toLocaleDateString('de-DE', {
                year: 'numeric',
                month: '2-digit',
                day: '2-digit',
            });

            return `${localeDate} ${date.getHours()}:${(date.getMinutes() < 10 ? '0' : '') + date.getMinutes()}:${
                (date.getSeconds() < 10 ? '0' : '') + date.getSeconds()
            }`;
        },
        storeBlock() {
            let attributes = {};
            attributes.payload = {};
            attributes.payload.title = this.currentTitle;
            attributes.payload.folder_id = this.currentFolderId;
            attributes.payload.type = this.currentFolderType;

            this.updateBlock({
                attributes: attributes,
                blockId: this.block.id,
                containerId: this.block.relationships.container.data.id,
            });
        },
        displayTermSelector() {
            this.showTermSelector = true;
        },
        selectTerm() {
            this.showTermSelector = false;
            this.uploadFile();
        },
        async uploadFile() {
            const userFile = this.$refs?.uploadFile?.files[0];
            if (!userFile) {
                this.companionWarning({
                    info: this.$gettext('Bitte wählen Sie eine Datei aus.'),
                });
                return;
            }

            let file = {
                attributes: {
                    name: userFile.name.replace(/\s/g, '_'),
                },
                relationships: {
                    'terms-of-use': {
                        data: {
                            id: this.selectedTerm,
                        },
                    },
                },
            };
            let fileObj = await this.createFile({
                file: file,
                filedata: userFile,
                folder: { id: this.currentFolderId },
            });
            if (fileObj && fileObj.type === 'file-refs') {
                this.companionSuccess({
                    info: this.$gettext('Die Datei wurde erfolgreich im Dateibereich abgelegt.'),
                });
            } else {
                if (this.folderType !== 'HomeworkFolder') {
                    this.companionError({
                        info: this.$gettext('Es ist ein Fehler aufgetretten.'),
                    });
                }
            }
            this.reload();
        },
        async reload() {
            await this.loadBlock({ id: this.block.id });
            this.initCurrentData();
        },
        getDefaultTerm() {
            const defaultTerm = this.termsOfUse.filter((term) => term.attributes['is-default'])[0];
            if (defaultTerm) {
                return defaultTerm.id;
            }
            return null;
        },
    },
    watch: {
        currentFolderId() {
            if (this.canEdit) {
                this.setCurrentFolderType();
            }
        },
    },
};
</script>
<style scoped lang="scss">
@import '../../../../assets/stylesheets/scss/courseware/blocks/files';
</style>