aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/components/courseware/unit/CoursewareShelfDialogImport.vue
blob: 39d80f5b53d1ea1364b2bbc3e228c9ecaabdb600 (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
382
<template>
    <div class="cw-shelf-dialog-import-wrapper">
        <studip-wizard-dialog
            v-if="!importRunning"
            :title="$gettext('Lernmaterial importieren')"
            :confirmText="$gettext('Importieren')"
            :closeText="$gettext('Abbrechen')"
            :slots="wizardSlots"
            :lastRequiredSlotId="1"
            :requirements="requirements"
            @close="setShowUnitImportDialog(false)"
            @confirm="importCoursewareArchiv"
        >
            <template v-slot:file>
                <form class="default" @submit.prevent="">
                    <label>
                        <span>{{ text.import }}</span><span aria-hidden="true" class="wizard-required">*</span>
                        <input v-show="importZipFile === null" ref="fileInput" class="cw-file-input" type="file" accept=".zip" @change="setImport" />
                        <p v-show="importZipFile !== null" class="cw-file-input-change">
                            <button class="button" @click="$refs.fileInput.click()">{{ $gettext('Datei ändern')}}</button><span>{{ importZipFile?.name }}</span>
                        </p>
                    </label>
                    <fieldset v-show="archiveErrors.length > 0">
                        <legend>{{$gettext('Fehler im Import-Archiv')}}</legend>
                        <ul>
                            <li v-for="(error, index) in archiveErrors" :key="index"> {{error}} </li>
                        </ul>
                    </fieldset>
                </form>
            </template>
            <template v-slot:edit>
                <form v-if="hasValidFile" class="default" @submit.prevent="">
                    <label>
                        {{ text.title }}
                        <input type="text" v-model="modifiedData.title" :placeholder="loadedTitle" />
                    </label>
                    <label for="color">
                        {{ text.color }}
                    </label>
                    <StudipSelect
                        id="color"
                        v-model="modifiedData.color"
                        :options="colors"
                        :reduce="(color) => color.class"
                        :clearable="false"
                        label="name"
                    >
                        <template #no-options>
                            {{$gettext('Es steht keine Auswahl zur Verfügung.')}}
                        </template>
                        <template #selected-option="option">
                            <span class="vs__option-color" :style="{ 'background-color': option.hex }"></span
                            ><span>{{ option.name }}</span>
                        </template>
                        <template #option="option">
                            <span class="vs__option-color" :style="{ 'background-color': option.hex }"></span
                            ><span>{{ option.name }}</span>
                        </template>
                    </StudipSelect>
                    <label>
                        {{ text.description }}
                        <textarea v-model="modifiedData.description" :placeholder="loadedDescription" />
                    </label>
                </form>
                <courseware-companion-box
                    v-else
                    :msgCompanion="$gettext('Bitte wählen Sie ein Import-Archiv aus.')"
                    mood="unsure"
                />
            </template>
        </studip-wizard-dialog>
        <studip-dialog
            v-if="importRunning"
            :title="$gettext('Lernmaterial importieren')"
            :closeText="$gettext('Schließen')"
            height="420"
            @close="setShowUnitImportDialog(false)"
        >
            <template v-slot:dialogContent>
                <div role="status" aria-live="polite">
                    <courseware-companion-box
                        v-show="importDone && importErrors.length === 0"
                        :msgCompanion="$gettext('Import erfolgreich!')"
                        mood="special"
                    />
                    <courseware-companion-box
                        v-show="importDone && importErrors.length > 0"
                        :msgCompanion="$gettext('Import abgeschlossen. Es sind Fehler aufgetreten!')"
                        mood="unsure"
                    />
                    <courseware-companion-box
                        v-show="!importDone"
                        :msgCompanion="$gettext('Import läuft. Bitte schließen Sie den Dialog nicht bis der Import abgeschlossen wurde.')"
                        mood="pointing"
                    />
                </div>
                <form v-if="!importDone" class="default" @submit.prevent="">
                    <fieldset>
                        <div v-if="!fileImportDone" class="cw-import-zip">
                            <header>{{$gettext('Importiere Dateien')}}:</header>
                            <div class="progress-bar-wrapper">
                                <div class="progress-bar" role="progressbar" :style="{width: importFilesProgress + '%'}" :aria-valuenow="importFilesProgress" aria-valuemin="0" aria-valuemax="100">{{ importFilesProgress }}%</div>
                            </div>
                            {{ importFilesState }}
                        </div>
                        <div v-if="fileImportDone" class="cw-import-zip">
                            <header>{{$gettext('Importiere Elemente')}}:</header>
                            <div class="progress-bar-wrapper">
                                <div class="progress-bar" role="progressbar" :style="{width: importStructuresProgress + '%'}" :aria-valuenow="importStructuresProgress" aria-valuemin="0" aria-valuemax="100">{{ importStructuresProgress }}%</div>
                            </div>
                            {{ importStructuresState }}
                        </div>
                    </fieldset>
                </form>
                <form v-if="importErrors.length > 0" class="default" @submit.prevent="">
                    <fieldset>
                        <legend>{{$gettext('Fehlermeldungen')}}</legend>
                        <ul>
                            <li v-for="(error, index) in importErrors" :key="index"> {{error}} </li>
                        </ul>
                    </fieldset>
                </form>
            </template>
        </studip-dialog>
    </div>
</template>

<script>
import CoursewareCompanionBox from '../layouts/CoursewareCompanionBox.vue';
import CoursewareImport from '@/vue/mixins/courseware/import.js';
import colorMixin from '@/vue/mixins/courseware/colors.js';
import StudipWizardDialog from '../../StudipWizardDialog.vue';

import { mapActions, mapGetters } from 'vuex'
import JSZip from 'jszip';

export default {
    name: 'courseware-shelf-dialog-import',
    components: {
        StudipWizardDialog,
        CoursewareCompanionBox
    },
    mixins: [CoursewareImport, colorMixin],
    data() {
        return {
            wizardSlots: [
                { id: 1, valid: false, name: 'file', title: this.$gettext('Import-Archiv'), icon: 'file-archive',
                  description: this.$gettext('Wählen Sie hier eine Courseware-Export-Archiv-Datei von Ihrer Festplatte aus. Bei Courseware-Export-Archiven handelt es sich um Zip-Dateien. Diese sollten mindestens die Dateien files.json und courseware.json enthalten.') },
                { id: 2, valid: true, name: 'edit', title: this.$gettext('Anpassen'), icon: 'edit', description: this.$gettext('Sie können hier die Daten des zu importierenden Lernmaterials anpassen. Eine Anpassung ist optional, Sie können das Archiv auch unverändert importieren.') },
            ],
            modifiedData: {
                title: '',
                color: 'studip-blue',
                description: ''
            },
            importArchivFile: null,
            importRunning: false,
            importZipFile: null,
            zip: null,

            loadedZipData: null,
            archiveErrors: [],

            requirements: [],
            text: {
                import: this.$gettext('Importdatei'),
                title: this.$gettext('Titel'),
                color: this.$gettext('Farbe'),
                description: this.$gettext('Beschreibung'),
            }
        }
    },
    computed: {
        ...mapGetters({
            context: 'context',
            importFilesState: 'importFilesState',
            importFilesProgress: 'importFilesProgress',
            importStructuresState: 'importStructuresState',
            importStructuresProgress: 'importStructuresProgress',
            importErrors: 'importErrors',
            lastCreateCoursewareUnit: 'courseware-units/lastCreated',

        }),
        colors() {
            return this.mixinColors.filter(color => color.darkmode);
        },
        fileImportDone() {
            return this.importFilesProgress === 100;
        },
        importDone() {
            if (!this.importZipFile) {
                this.setImportFilesProgress(0);
                this.setImportStructuresProgress(0);
            }
            return this.importFilesProgress === 100 && this.importStructuresProgress === 100;
        },
        hasValidFile() {
            return this.archiveErrors.length === 0 && this.loadedZipData !== null;
        },
        loadedTitle() {
            return this.loadedZipData.courseware.attributes.title ?? '';
        },
        loadedDescription() {
            return this.loadedZipData.courseware.attributes.payload.description ?? '';
        }
    },
    methods: {
        ...mapActions({
            setShowUnitImportDialog: 'setShowUnitImportDialog',
            createCoursewareUnit: 'courseware-units/create',
            setImportFilesProgress: 'setImportFilesProgress',
            setImportStructuresProgress: 'setImportStructuresProgress',
            setImportErrors: 'setImportErrors',
            loadStructuralElementById: 'courseware-structural-elements/loadById',
            companionSuccess: 'companionSuccess',
        }),
        setImport(event) {
            this.importZipFile = event.target.files[0];
            this.loadZipData();
        },

        async loadZipData() {
            const slot = this.wizardSlots[0];
            const text = this.text.import;
            this.archiveErrors = [];
            this.loadedZipData = null;
            this.modifiedData.title = '';
            this.modifiedData.color = 'studip-blue';
            this.modifiedData.description = '';
            let filesError = false;
            if (!this.importZipFile.type.includes('zip')) {
                this.archiveErrors.push(this.$gettext('Die gewählte Datei ist kein Archiv.'));
                filesError = true;
            }
            if (!filesError) {
                try {
                    this.zip = await JSZip.loadAsync(this.importZipFile);
                } catch {
                    this.zip = null;
                    this.archiveErrors.push(this.$gettext('Beim laden des Archivs ist ein Fehler aufgetreten. Vermutlich ist das Archiv beschädigt.'));
                    filesError = true;
                }

                if (this.zip) {
                    if (this.zip.file('courseware.json') === null) {
                        this.archiveErrors.push(this.$gettext('Das Archiv enthält keine courseware.json Datei.'));
                        filesError = true;
                    }
                    if (this.zip.file('files.json') === null) {
                        this.archiveErrors.push(this.$gettext('Das Archiv enthält keine files.json Datei.'));
                        filesError = true;
                    }
                    if (this.zip.file('data.xml') !== null) {
                        this.archiveErrors.push(this.$gettext(
                            'Das Archiv enthält eine data.xml Datei. Möglicherweise handelt es sich um einen Export aus dem Courseware-Plugin. Diese Archive sind nicht kompatibel mit dieser Courseware.'
                        ));
                        filesError = true;
                    }
                }
            }
            if (filesError) {
                this.updateRequirements(slot, text, false);
                slot.valid = false;
                return;
            } else {
                this.updateRequirements(slot, text, true);
                slot.valid = true;
            }

            let data = await this.zip.file('courseware.json').async('string');
            let courseware = null;
            let data_settings = null;
            let settings = null;
            let data_files = await this.zip.file('files.json').async('string');
            let files = null;
            let jsonErrors = false;

            try {
                courseware = JSON.parse(data);
            } catch (error) {
                jsonErrors = true;
                this.archiveErrors.push(this.$gettext('Die Beschreibung der Courseware-Inhalte ist nicht valide.'));
                this.archiveErrors.push(error);
            }

            if (this.zip.file('settings.json') !== null) {
                data_settings = await this.zip.file('settings.json').async('string');
                try {
                    settings = JSON.parse(data_settings);
                } catch (error) {
                    jsonErrors = true;
                    this.archiveErrors.push(this.$gettext('Die Beschreibung der Courseware-Einstellungen ist nicht valide.'));
                    this.archiveErrors.push(error);
                }
            }

            try {
                files = JSON.parse(data_files);
            } catch (error) {
                jsonErrors = true;
                this.archiveErrors.push(this.$gettext('Die Beschreibung der Dateien ist nicht valide.'));
                this.archiveErrors.push(error);
            }
            if (jsonErrors) {
                return;
            }

            this.loadedZipData = {
                courseware: courseware,
                files: files,
                settings: settings
            }

            this.modifiedData.title = courseware.attributes.title;
            this.modifiedData.color = courseware.attributes.payload.color ?? 'studip-blue';
            this.modifiedData.description = courseware.attributes.payload.description ?? '';
        },

        async importCoursewareArchiv() {
            if (this.loadedZipData === null) {
                return false;
            }

            this.setImportFilesProgress(0);
            this.setImportStructuresProgress(0);
            this.setImportErrors([]);

            this.importRunning = true;

            const title = this.modifiedData.title !==  '' ? this.modifiedData.title : this.loadedTitle;
            const description = this.modifiedData.description !==  '' ? this.modifiedData.description : this.loadedDescription;

            const unit = {
                    attributes: {
                        title: title,
                        payload: {
                            description: description,
                            color: this.modifiedData.color,
                        }
                    },
                    relationships: {
                        range: {
                            data: {
                                type: this.context.type,
                                id: this.context.id
                            }
                        }
                    }
                };
            this.sortChildren(this.loadedZipData.courseware.children);
            await this.createCoursewareUnit(unit, { root: true });
            const newElementId = this.lastCreateCoursewareUnit.relationships['structural-element'].data.id;
            await this.loadStructuralElementById({ id: newElementId });

            const newStructuralElement = this.structuralElementById({id: newElementId});

            await this.importCourseware(this.loadedZipData.courseware, newStructuralElement.id, this.loadedZipData.files, 'migrate', this.loadedZipData.settings);
            this.companionSuccess({ info: this.$gettext('Lernmaterial importiert.') });
        },
        updateRequirements(slot, text, valid) {
            const index = this.requirements.findIndex(req =>  req.slot.id === slot.id && req.text === text);
            if (valid) {
                if (index !== -1) {
                    this.requirements.splice(index, 1);
                }
            } else {
                if (index === -1) {
                   this.requirements.push({slot: slot, text: text});
                }
            }
        },
        sortChildren(children) {
            children?.sort((a, b) => {
                return a.attributes.position - b.attributes.position;
            });
            children?.forEach(child => {
                this.sortChildren(child.children);
            });
        }
    }
}
</script>