aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/components/courseware/structural-element/CoursewareTreeUnits.vue
blob: 9023e065315b18bce4dc3b5514310a24d2b8d999 (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
<template>
    <div v-if="canEditRoot || linkedUnits.length > 0" class="cw-tree-units">
        <div class="cw-tree-unit-title">{{ $gettext('Weitere Lernmaterialien') }}</div>
        <div v-if="!processing">
            <ol v-if="linkedUnits.length > 0">
                <courseware-tree-unit
                    v-for="unit in linkedUnits"
                    :unit="unit"
                    :canEditRoot="canEditRoot"
                    :key="unit.id"
                    @removeUnitLink="removeUnitLink"
                ></courseware-tree-unit>
            </ol>
            <div v-if="canEditRoot && units.length > 0" class="cw-tree-units-adder">
                <form v-if="showForm" class="default cw-tree-units-adder-form" @submit.prevent="">
                    <label>
                        <span class="sr-only">{{ $gettext('Lernmaterial') }}</span>
                        <select v-model="selectedUnit" name="addUnit" @change="addUnitLink">
                            <option v-show="false" value="" disabled>
                                {{ $gettext('Link zum Lernmaterial auswählen') }}
                            </option>
                            <option v-for="(unit, index) in units" :key="index" :value="unit.id">
                                {{ getUnitTitle(unit) }}
                            </option>
                        </select>
                    </label>
                    <button
                        v-if="canEditRoot"
                        class="button cancel"
                        :title="$gettext('Auswahl abbrechen')"
                        @click.prevent="showForm = false"
                    ></button>
                </form>
                <button
                    v-else
                    class="add-element"
                    :title="$gettext('Link zum Lernmaterial hinzufügen')"
                    @click="showForm = true"
                >
                    <studip-icon shape="add" />
                </button>
            </div>
        </div>
        <studip-progress-indicator v-else :description="$gettext('Vorgang wird bearbeitet...')" />
    </div>
</template>

<script>
import CoursewareTreeUnit from './CoursewareTreeUnit.vue';
import StudipProgressIndicator from '../../StudipProgressIndicator.vue';
import colorMixin from '@/vue/mixins/courseware/colors.js';
import { mapActions, mapGetters } from 'vuex';

export default {
    name: 'CoursewareTreeUnits',
    mixins: [colorMixin],
    components: {
        CoursewareTreeUnit,
        StudipProgressIndicator,
    },
    data() {
        return {
            processing: false,
            showForm: false,
            selectedUnit: '',
            currentInstance: null,
            identimage: '',
        };
    },
    computed: {
        ...mapGetters({
            context: 'context',
            courseUnits: 'courseware-units/all',
            currentRootElement: 'currentRootElement',
            unitById: 'courseware-units/byId',
            instanceById: 'courseware-instances/byId',
            structuralElementById: 'courseware-structural-elements/byId',
        }),

        instance() {
            if (this.context.type === 'courses') {
                return this.instanceById({ id: 'course_' + this.context.id + '_' + this.context.unit });
            } else {
                return this.instanceById({ id: 'user_' + this.context.id + '_' + this.context.unit });
            }
        },

        canEditRoot() {
            return this.currentRootElement?.attributes['can-edit'];
        },

        units() {
            // returns all course units that are not already linked
            const units = this.courseUnits;
            const unitsWithoutSelf = units.filter((unit) => unit.id !== this.context.unit);
            const linkedUnits = this.currentInstance?.attributes['linked-units'];
            if (linkedUnits) {
                return unitsWithoutSelf.filter((unit) => !this.instance.attributes['linked-units'].includes(unit.id));
            } else {
                return unitsWithoutSelf;
            }
        },

        linkedUnits() {
            // returns the required unit data of all linked units
            const units = this.courseUnits;
            const linkedUnitIds = this.currentInstance?.attributes['linked-units'];

            if (linkedUnitIds) {
                // filter out not linked units
                const filteredUnits = units.filter((unit) =>
                    this.instance.attributes['linked-units'].includes(unit.id)
                );
                // map units to their unit ids instead of array keys to return the correct order
                const mappedUnits = new Map(filteredUnits.map((unit) => [unit.id, unit]));

                return linkedUnitIds.map((unit) => mappedUnits.get(unit));
            }
            return [];
        },
    },

    mounted() {
        this.initData();
    },

    methods: {
        ...mapActions({
            loadCourseUnits: 'loadCourseUnits',
            storeCoursewareLinkedUnits: 'storeCoursewareLinkedUnits',
        }),

        async initData() {
            if (this.context.type === 'courses') {
                this.currentInstance = this.instance;
                const linkedUnits = this.currentInstance?.attributes['linked-units'];
                if (this.canEditRoot || linkedUnits.length > 0) {
                    this.processing = true;
                    await this.loadCourseUnits(this.context.id);
                    this.processing = false;
                }
            }
        },

        getUnitTitle(unit) {
            const rootElement = this.structuralElementById({
                id: unit.relationships['structural-element'].data.id,
            });
            return rootElement.attributes.title;
        },

        async addUnitLink() {
            this.showForm = false;
            this.processing = true;
            const linkedUnits = this.currentInstance.attributes['linked-units'];
            if (!linkedUnits) {
                await this.storeCoursewareLinkedUnits({
                    instance: this.currentInstance,
                    linkedUnits: [this.selectedUnit],
                });
            } else if (!linkedUnits.includes(this.selectedUnit)) {
                this.currentInstance.attributes['linked-units'].push(this.selectedUnit);
                await this.storeCoursewareLinkedUnits({
                    instance: this.currentInstance,
                    linkedUnits: linkedUnits,
                });
            }
            this.processing = false;
        },

        async removeUnitLink(id) {
            let linkedUnits = this.currentInstance.attributes['linked-units'].filter((unitId) => unitId !== id);
            await this.storeCoursewareLinkedUnits({
                instance: this.currentInstance,
                linkedUnits: linkedUnits,
            });
            this.selectedUnit = '';
        },
    },
};
</script>
<style lang="scss">
.cw-tree-units {
    .progress-indicator-wrapper {
        margin-top: 15px;
    }
}
</style>