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
|
<template>
<form action="#"
method="post"
enctype="multipart/form-data"
class="questionnaire_edit default"
@submit.prevent="submit()"
:data-dialog="asDialog ? true : null"
:data-secure="activateFormSecure"
>
<div class="editor">
<div class="rightside" aria-live="polite" tabindex="0" ref="rightside">
<div class="admin" v-if="activeTab === 'admin'">
<article aria-live="assertive" class="validation_notes studip">
<header>
<h1>
<studip-icon shape="info-circle" role="info" class="text-bottom validation_notes_icon"></studip-icon>
{{ $gettext('Hinweise zum Ausfüllen des Formulars') }}
</h1>
</header>
<div class="required_note">
<div aria-hidden="true">
{{ $gettext('Pflichtfelder sind mit Sternchen gekennzeichnet.') }}
</div>
<div class="sr-only">
{{ $gettext('Dieses Formular enthält Pflichtfelder.') }}
</div>
</div>
<div v-if="validationNotice && !data.title">
{{ $gettext('Folgende Angaben müssen korrigiert werden, um das Formular abschicken zu können:') }}
<ul>
<li aria-describedby="questionnaire_title">{{ $gettext('Titel des Fragebogens') }}</li>
</ul>
</div>
</article>
<div class="formpart">
<label class="studiprequired" for="questionnaire_title">
<span class="textlabel">{{ $gettext('Titel des Fragebogens') }}</span>
<span title="Dies ist ein Pflichtfeld" aria-hidden="true" class="asterisk">*</span>
</label>
<input type="text" id="questionnaire_title" v-model="data.title" v-autofocus>
</div>
<div class="hgroup">
<label>
{{ $gettext('Startzeitpunkt') }}
<datetimepicker v-model="data.startdate"></datetimepicker>
</label>
<label>
{{ $gettext('Endzeitpunkt') }}
<datetimepicker v-model="data.stopdate"></datetimepicker>
</label>
</div>
<label>
<input type="checkbox" v-model="data.copyable" true-value="1" false-value="0">
{{ $gettext('Fragebogen zum Kopieren freigeben') }}
</label>
<label>
<input type="checkbox" v-model="data.anonymous" true-value="1" false-value="0">
{{ $gettext('Teilnehmende anonymisieren') }}
</label>
<label>
<input type="checkbox" v-model="data.editanswers" true-value="1" false-value="0">
{{ $gettext('Teilnehmende dürfen ihre Antworten revidieren') }}
</label>
<label>
{{ $gettext('Ergebnisse einsehbar') }}
<select v-model="data.resultvisibility">
<option value="always">{{ $gettext('Immer') }}</option>
<option value="afterending">{{ $gettext('Nach Ende der Befragung') }}</option>
<option value="afterparticipation">{{ $gettext('Nach der Teilnahme') }}</option>
<option value="never">{{ $gettext('Niemals') }}</option>
</select>
</label>
</div>
<div class="add_question file_select_possibilities" v-else-if="activeTab === 'add_question'">
<div>
<button v-for="(questiontype, key) in questionTypes" :key="key"
href=""
@click.prevent="addQuestion(questiontype.type)"
>
<studip-icon :shape="questiontype.icon" :size="40"></studip-icon>
{{questiontype.name}}
</button>
</div>
</div>
<div v-else>
<component :is="componentForQuestionIndex(indexForQuestion)"
:model-value="data.questions[indexForQuestion].questiondata"
@update:modelValue="(v) => data.questions[indexForQuestion].questiondata = v"
:question_id="data.questions[indexForQuestion].id"
:key="data.questions[indexForQuestion].id">
</component>
</div>
</div>
<aside>
<a class="admin"
:class="{active: activeTab === 'admin'}"
href="#"
@click.prevent="switchTab('admin')">
<span class="icon"><studip-icon shape="evaluation" :size="30" alt=""></studip-icon></span>
{{ $gettext('Einstellungen') }}
</a>
<draggable :list="data.questions"
handle=".drag-handle"
group="questions"
class="questions_container questions"
item-key="id"
>
<template #item="{ element }">
<div @mouseenter="hoverTab = element.id"
@mouseleave="hoverTab = null"
:class="{
active: activeTab === element.id || activeTab === 'meta_' + element.id,
hovered: hoverTab === element.id,
}"
>
<a href="#"
@click.prevent="switchTab(element.id)">
<span class="drag-handle"></span>
<span class="icon type">
<studip-icon :shape="questionTypes[element.questiontype].icon" :size="30" alt=""></studip-icon>
</span>
<div v-if="editInternalName !== element.id">{{ element.internal_name || questionTypes[element.questiontype].name}}</div>
<div v-else class="inline_editing">
<input type="text" ref="editInternalName" v-model="tempInternalName" class="inlineediting_internal_name">
<button @click="saveInternalName(element.id)">
<studip-icon shape="accept" :size="20" :title="$gettext('Internen Namen speichern')"></studip-icon>
</button>
<button @click="editInternalName = null">
<studip-icon shape="decline" :size="20" :title="$gettext('Internen Namen nicht speichern')"></studip-icon>
</button>
</div>
</a>
<studip-action-menu :items="actionMenuItems"
@copy="duplicateQuestion(element.id)"
@rename="renameInternalName(element.id)"
@moveup="moveQuestionUp(element.id)"
@movedown="moveQuestionDown(element.id)"
@delete="deleteQuestion(element.id)"></studip-action-menu>
</div>
</template>
</draggable>
<a :class="activeTab === 'add_question' ? 'add_question active' : 'add_question'"
href="#"
@click.prevent="switchTab('add_question')">
<span class="icon"><studip-icon shape="add" :size="30" alt=""></studip-icon></span>
{{ $gettext('Element hinzufügen') }}
</a>
</aside>
</div>
<footer data-dialog-button>
<button class="button" name="questionnaire_store">
{{ $gettext('Speichern') }}
</button>
<a href="#" class="button cancel">
{{ $gettext('Abbrechen') }}
</a>
</footer>
</form>
</template>
<script>
import draggable from 'vuedraggable';
import md5 from 'md5';
import StudipIcon from '@/vue/components/StudipIcon.vue';
import StudipActionMenu from '@/vue/components/StudipActionMenu.vue';
import Datetimepicker from '@/vue/components/Datetimepicker.vue';
import {defineAsyncComponent} from 'vue';
const loadedComponents = {};
export default {
name: 'questionnaireeditor',
components: {
Datetimepicker,
StudipActionMenu,
StudipIcon,
draggable,
},
props: {
asDialog: {
type: Boolean,
default: false,
},
questionData: Object,
questionTypes: Object,
rangeId: String,
rangeType: String,
},
data() {
return {
activeTab: 'admin',
data: {...this.questionData},
editInternalName: null,
form_secured: true,
hoverTab: null,
oldData: JSON.parse(JSON.stringify(this.questionData)),
tempInternalName: '',
validationNotice: false,
};
},
methods: {
componentForQuestionIndex(index) {
const componentInfo = this.questionTypes[this.data.questions[index].questiontype].component;
if (loadedComponents[componentInfo[0]] === undefined) {
loadedComponents[componentInfo[0]] = componentInfo[1] === ''
? defineAsyncComponent(() => import(`../../components/questionnaires/${componentInfo[0]}.vue`))
: defineAsyncComponent(() => import(/* webpackIgnore: true */componentInfo[1]));
}
return loadedComponents[componentInfo[0]];
},
addQuestion(questiontype) {
let id = md5(`${STUDIP.USER_ID}_QUESTIONTYPE_${Math.random()}`);
this.data.questions.push({
id: id,
questiontype: questiontype,
internal_name: '',
questiondata: {},
});
this.activeTab = id;
},
submit() {
if (!this.data.title) {
this.switchTab('admin');
this.validationNotice = true;
return;
}
const data = {
title: this.data.title,
copyable: this.data.copyable,
anonymous: this.data.anonymous,
editanswers: this.data.editanswers,
startdate: this.data.startdate,
stopdate: this.data.stopdate,
resultvisibility: this.data.resultvisibility
};
const questions = this.data.questions.map(question => ({
id: question.id,
questiontype: question.questiontype,
internal_name: question.internal_name,
questiondata: question.questiondata,
}));
$.post(STUDIP.URLHelper.getURL('dispatch.php/questionnaire/store/' + (this.data.id || '')), {
questionnaire: data,
questions_data: JSON.stringify(questions),
range_type: this.rangeType,
range_id: this.rangeId
}).done(() => {
this.form_secured = false;
this.$nextTick(() => {
location.reload();
});
}).fail(() => {
STUDIP.Report.error('Could not save questionnaire.', '');
});
},
getIndexForQuestion(question_id) {
for (let i in this.data.questions) {
if (
this.data.questions[i].id === question_id
|| this.data.questions[i].id === question_id.substring(5)
) {
return parseInt(i, 10);
}
}
return null;
},
duplicateQuestion(question_id) {
const i = this.getIndexForQuestion(question_id);
const id = md5(`${STUDIP.USER_ID}_QUESTIONTYPE_${Math.random()}`);
this.data.questions.push({
id: id,
questiontype: this.data.questions[i].questiontype,
internal_name: this.data.questions[i].internal_name,
questiondata: JSON.parse(JSON.stringify(this.data.questions[i].questiondata)),
});
this.activeTab = id;
},
deleteQuestion(question_id) {
STUDIP.Dialog.confirm(this.$gettext('Wirklich löschen?')).done(() => {
const index = this.getIndexForQuestion(question_id);
this.data.questions.splice(index, 1);
this.switchTab('add_question');
})
},
switchTab(tab_id) {
this.activeTab = tab_id;
},
objectsEqual(obj1, obj2) {
return _.isEqual(obj1, obj2);
},
renameInternalName(question_id) {
this.editInternalName = question_id;
let index = this.getIndexForQuestion(question_id);
this.tempInternalName = this.data.questions[index].internal_name;
this.$nextTick(() => {
this.$refs.editInternalName[0].focus();
});
},
saveInternalName(question_id) {
let index = this.getIndexForQuestion(question_id);
this.data.questions[index].internal_name = this.tempInternalName;
this.editInternalName = null;
},
moveQuestionDown(question_id) {
let index = this.getIndexForQuestion(question_id);
if (index < this.data.questions.length - 1) {
let question = this.data.questions[index];
this.data.questions[index] = this.data.questions[index + 1];
this.data.questions[index + 1] = question;
this.$forceUpdate();
}
},
moveQuestionUp(question_id) {
let index = this.getIndexForQuestion(question_id);
if (index > 0) {
let question = this.data.questions[index];
this.data.questions[index] = this.data.questions[index - 1];
this.data.questions[index - 1] = question;
this.$forceUpdate();
}
}
},
computed: {
actionMenuItems() {
return [
{label: this.$gettext('Umbenennen'), icon: 'edit', emit: 'rename'},
{label: this.$gettext('Frage kopieren'), icon: 'copy', emit: 'copy'},
{label: this.$gettext('Frage nach oben verschieben'), icon: 'arr_1up', emit: 'moveup'},
{label: this.$gettext('Frage nach unten verschieben'), icon: 'arr_1down', emit: 'movedown'},
{label: this.$gettext('Frage löschen'), icon: 'trash', emit: 'delete'},
];
},
activateFormSecure() {
return this.form_secured && !this.objectsEqual(this.oldData, this.data) ? true : null;
},
indexForQuestion() {
return this.getIndexForQuestion(this.activeTab);
},
},
}
</script>
|