aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/components/courseware/structural-element/CoursewareStructuralElementDialogAdd.vue
blob: 1c3a6fb51ca67289ee3c772f58ce65e99f3b26b0 (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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<template>
    <studip-wizard-dialog
        :title="$gettext('Seite hinzufügen')"
        :confirmText="$gettext('Erstellen')"
        :closeText="$gettext('Abbrechen')"
        :slots="wizardSlots"
        :lastRequiredSlotId="1"
        :requirements="requirements"
        @close="closeAddDialog"
        @confirm="createElement"
    >
        <template v-slot:basic>
            <form class="default" @submit.prevent="">
                <label>
                    {{ $gettext('Position der neuen Seite') }}
                    <select v-model="pageParent" name="relativePosition">
                        <option v-if="!isRoot && canEditParent" value="sibling">
                            {{ $gettext('Neben der aktuellen Seite') }}
                        </option>
                        <option value="descendant">{{ $gettext('Unterhalb der aktuellen Seite') }}</option>
                    </select>
                </label>
                <label>
                    <span>{{ text.title }}</span>
                    <span aria-hidden="true" class="wizard-required">*</span>
                    <input type="text" v-model="title" name="title" required />
                </label>
                <label>
                    <span>{{ $gettext('Beschreibung') }}</span>
                    <textarea v-model="description" name="description" required />
                </label>
            </form>
        </template>
        <template v-slot:template>
            <form v-if="hasTemplates" class="default" @submit.prevent="">
                <label>
                    {{ $gettext('Art der Vorlage') }}
                    <select v-model="templatePurpose" name="templatePurpose">
                        <option value="content">{{ $gettext('Inhalt') }}</option>
                        <option value="oer">{{ $gettext('OER-Material') }}</option>
                        <option value="portfolio">{{ $gettext('ePortfolio') }}</option>
                        <option value="draft">{{ $gettext('Entwurf') }}</option>
                        <option v-if="!inCourseContext" value="template">{{ $gettext('Aufgabenvorlage') }}</option>
                        <option value="other">{{ $gettext('Sonstiges') }}</option>
                    </select>
                </label>
                <label>
                    <span>{{ $gettext('Vorlage') }}</span>
                    <select v-model="selectedTemplate" name="template">
                        <option :value="null">{{ $gettext('ohne Vorlage') }}</option>
                        <option v-for="template in selectableTemplates" :key="template.id" :value="template">
                            {{ template.attributes.name }}
                        </option>
                    </select>
                </label>
            </form>
            <courseware-companion-box
                v-else
                :msgCompanion="$gettext('Es wurden keine Vorlagen gefunden.')"
                mood="pointing"
                class="cw-companion-box-in-form"
            />
        </template>
        <template v-slot:layout>
            <form class="default" @submit.prevent="">
                <label>
                    {{ $gettext('Bild') }}
                    <input
                        class="cw-file-input"
                        ref="upload_image"
                        type="file"
                        accept="image/*"
                        name="image"
                        @change="checkUploadFile"
                    />
                    <courseware-companion-box
                        v-if="uploadFileError"
                        :msgCompanion="uploadFileError"
                        mood="sad"
                        class="cw-companion-box-in-form"
                    />
                </label>
                <label for="color">
                    {{ $gettext('Farbe') }}
                </label>
                <StudipSelect
                    id="color"
                    v-model="color"
                    :options="colors"
                    :reduce="(color) => color.class"
                    label="name"
                    name="color"
                    :clearable="false"
                >
                    <template #open-indicator="{ selectAttributes }">
                        <span v-bind="selectAttributes"><studip-icon shape="arr_1down" :size="10" /></span>
                    </template>
                    <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>
            </form>
        </template>
        <template v-slot:advanced>
            <form class="default" @submit.prevent="">
                <label>
                    {{ $gettext('Art des Lernmaterials') }}
                    <select v-model="purpose" name="purpose">
                        <option value="content">{{ $gettext('Inhalt') }}</option>
                        <option value="oer">{{ $gettext('OER-Material') }}</option>
                        <option value="portfolio">{{ $gettext('ePortfolio') }}</option>
                        <option value="draft">{{ $gettext('Entwurf') }}</option>
                        <option v-if="!inCourseContext" value="template">{{ $gettext('Aufgabenvorlage') }}</option>
                        <option value="other">{{ $gettext('Sonstiges') }}</option>
                    </select>
                </label>
                <template v-if="purposeIsOer">
                    <label>
                        {{ $gettext('Lizenztyp') }}
                        <select v-model="license_type">
                            <option v-for="license in licenses" :key="license.id" :value="license.id">
                                {{ license.name }}
                            </option>
                        </select>
                    </label>
                    <label>
                        {{ $gettext('Geschätzter zeitlicher Aufwand') }}
                        <input type="text" v-model="required_time" />
                    </label>
                    <label>
                        {{ $gettext('Niveau') }}<br />
                        {{ $gettext('von') }}
                        <select v-model="difficulty_start">
                            <option value="">-</option>
                            <option v-for="difficulty_start in 12" :key="difficulty_start" :value="difficulty_start">
                                {{ difficulty_start }}
                            </option>
                        </select>
                        {{ $gettext('bis') }}
                        <select v-model="difficulty_end">
                            <option value="">-</option>
                            <option v-for="difficulty_end in 12" :key="difficulty_end" :value="difficulty_end">
                                {{ difficulty_end }}
                            </option>
                        </select>
                    </label>
                </template>
            </form>
        </template>
    </studip-wizard-dialog>
</template>

<script>
import CoursewareCompanionBox from '../layouts/CoursewareCompanionBox.vue';
import StudipSelect from '../../StudipSelect.vue';
import StudipWizardDialog from '../../StudipWizardDialog.vue';
import colorMixin from '@/vue/mixins/courseware/colors.js';
import wizardMixin from '@/vue/mixins/courseware/wizard.js';
import { mapActions, mapGetters } from 'vuex';

export default {
    name: 'courseware-structural-element-dialog-add',
    mixins: [colorMixin, wizardMixin],
    components: {
        CoursewareCompanionBox,
        StudipWizardDialog,
        StudipSelect,
    },
    props: {
        structuralElement: Object,
        isRoot: Boolean,
        canEditParent: Boolean,
    },
    data() {
        return {
            wizardSlots: [
                {
                    id: 1,
                    valid: false,
                    name: 'basic',
                    title: this.$gettext('Grundeinstellungen'),
                    icon: 'courseware',
                    target: 'title',
                    description: this.$gettext(
                        'Wählen Sie einen kurzen, prägnanten Titel und beschreiben Sie in einigen Worten den Inhalt der Seite.'
                    ),
                },
                {
                    id: 2,
                    valid: true,
                    name: 'template',
                    title: this.$gettext('Vorlage'),
                    icon: 'content2',
                    target: 'templatePurpose',
                    description: this.$gettext('Vorlagen enthalten Abschnitte und Blöcke, die bereits für bestimmte Zwecke angeordent sind. Beim anlegen der Seite, wird diese mit Abschnitten und Blöcken befüllt.'),
                },
                {
                    id: 3,
                    valid: true,
                    name: 'layout',
                    title: this.$gettext('Erscheinung'),
                    icon: 'picture',
                    target: 'image',
                    description: this.$gettext(
                        'Ein Vorschaubild motiviert Lernende die Seite zu erkunden. Die Kombination aus Bild und Farbe erleichtert das wiederfinden der Seite in einem Inhaltsverzeichnisblock.'
                    ),
                },
                {
                    id: 4,
                    valid: true,
                    name: 'advanced',
                    title: this.$gettext('Zusatzangaben'),
                    icon: 'info-list',
                    target: 'purpose',
                    description: this.$gettext(
                        'Hier können Sie detaillierte Angaben zur Seite eintragen. Diese sind besonders interessant wenn die Seite als OER geteilt wird.'
                    ),
                },
            ],
            text: {
                title: this.$gettext('Titel der neuen Seite'),
            },
            uploadFileError: '',
            requirements: [],

            pageParent: '',
            title: '',
            description: '',
            purpose: '',
            color: '',
            license_type: '',
            required_time: '',
            difficulty_start: '',
            difficulty_end: '',
            templatePurpose: '',
            selectedTemplate: null,
        };
    },
    computed: {
        ...mapGetters({
            licenses: 'licenses',
            context: 'context',
            lastCreatedStructuralElement: 'courseware-structural-elements/lastCreated',
            structuralElementById: 'courseware-structural-elements/byId',
            templates: 'courseware-templates/all',
        }),
        inCourseContext() {
            return this.context.type === 'courses';
        },
        colors() {
            return this.mixinColors.filter((color) => color.darkmode);
        },
        selectableTemplates() {
            return this.templates.filter((template) => {
                return template.attributes.purpose === this.templatePurpose;
            });
        },
        hasTemplates() {
            return this.templates.length > 0;
        },
        purposeIsOer() {
            return this.purpose === 'oer';
        },
    },
    methods: {
        ...mapActions({
            createStructuralElementWithTemplate: 'createStructuralElementWithTemplate',
            updateStructuralElement: 'updateStructuralElement',
            companionError: 'companionError',
            companionInfo: 'companionInfo',
            companionSuccess: 'companionSuccess',
            showElementAddDialog: 'showElementAddDialog',
            loadStructuralElementById: 'courseware-structural-elements/loadById',
            uploadImageForStructuralElement: 'uploadImageForStructuralElement',
        }),
        initWizardData() {
            this.pageParent = 'descendant';
            this.title = '';
            this.description = '';
            this.purpose = 'content';
            this.color = 'studip-blue';
            this.license_type = '';
            this.required_time = '';
            this.difficulty_start = '';
            this.difficulty_end = '';
            this.templatePurpose = 'content';
            this.selectedTemplate = null;
            this.requirements.push({ slot: this.wizardSlots[0], text: this.text.title, target: 'title' });
        },
        closeAddDialog() {
            this.showElementAddDialog(false);
            this.initWizardData();
        },
        checkUploadFile() {
            this.uploadFileError = this.checkUploadImageFile(this.$refs?.upload_image?.files[0]);
        },
        async createElement() {
            let parent_id = this.structuralElement.id; // new page is descandant as default

            this.errorEmptyChapterName = this.title.trim();
            if (this.errorEmptyChapterName === '') {
                return;
            }
            if (this.pageParent === 'sibling') {
                parent_id = this.structuralElement.relationships.parent.data.id;
            }
            this.showElementAddDialog(false);

            const file = this.$refs?.upload_image?.files[0];
            const element = {
                attributes: {
                    title: this.title,
                    purpose: this.purpose,
                    payload: {
                        description: this.description,
                        color: this.color,
                        license_type: this.purposeIsOer ? this.license_type : '',
                        required_time: this.purposeIsOer ? this.required_time : '',
                        difficulty_start: this.purposeIsOer ? this.difficulty_start : '',
                        difficulty_end: this.purposeIsOer ? this.difficulty_end : '',
                    },
                },
                templateId: this.selectedTemplate ? this.selectedTemplate.id : null,
                parentId: parent_id,
                currentId: this.structuralElement.id,
            };

            try {
                await this.createStructuralElementWithTemplate(element);
            } catch (e) {
                let errorMessage = this.$gettext(
                    'Es ist ein Fehler aufgetreten. Die Seite konnte nicht erstellt werden.'
                );
                if (e.status === 403) {
                    errorMessage = this.$gettext(
                        'Die Seite konnte nicht erstellt werden. Sie haben nicht die notwendigen Schreibrechte.'
                    );
                }

                this.companionError({ info: errorMessage });
                return;
            }

            const newCreated = this.lastCreatedStructuralElement;
            await this.loadStructuralElementById({ id: newCreated.id });
            const newElement = this.structuralElementById({ id: newCreated.id });
            this.companionSuccess({
                info: this.$gettext(
                    'Die Seite %{ pageTitle } wurde erfolgreich angelegt.',
                    { pageTitle: newElement.attributes.title }
                ),
            });

            if (file && this.uploadFileError === '') {
                try {
                    await this.uploadImageForStructuralElement({ structuralElement: newElement, file });
                } catch (error) {
                    console.error(error);
                    this.companionError({
                        info: this.$gettext('Das Bild für das neue Lernmaterial konnte nicht gespeichert werden.'),
                    });
                }
                this.loadStructuralElementById({ id: newElement.id, options: { include: 'children' } });
            }
            this.initWizardData();
            this.$router.push(newElement.id);
        },
    },
    mounted() {
        this.initWizardData();
        if (!this.hasTemplates) {
                this.wizardSlots.splice(1,1);
                this.wizardSlots[1]['id'] = 2;
                this.wizardSlots[2]['id'] = 3;
        }
    },
    watch: {
        title(newTitle) {
            this.requirements = [];
            const slot = this.wizardSlots[0];
            if (newTitle === '') {
                slot.valid = false;
                this.requirements.push({ slot: slot, text: this.text.title, target: 'title' });
            } else {
                slot.valid = true;
            }
        },
        templatePurpose() {
            this.selectedTemplate = null;
        },
    },
};
</script>