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
|
/* eslint-disable no-await-in-loop */
import { mapActions, mapGetters } from 'vuex';
import JSZip from 'jszip';
import FileSaver from 'file-saver';
import axios from 'axios';
export default {
computed: {
...mapGetters({
courseware: 'courseware',
containerById: 'courseware-containers/byId',
folderById: 'folders/byId',
filesById: 'files/byId',
oerTitle: 'oerTitle',
structuralElementById: 'courseware-structural-elements/byId',
}),
},
data() {
return {
exportFiles: {
json: [],
download: [],
},
elementCounter: 0,
exportElementCounter: 0,
};
},
methods: {
initData() {
this.exportFiles = { json: [], download: [] };
this.elementCounter = 0;
this.exportElementCounter = 0;
},
async sendExportZip(root_id = null, options) {
this.initData();
let view = this;
let zip = await this.createExportFile(root_id, options);
this.setExportState(this.$gettext('Erstelle Zip-Archiv'));
this.setExportProgress(0);
await zip.generateAsync({ type: 'blob' }, function updateCallback(metadata) {
view.setExportProgress(metadata.percent.toFixed(0));
}).then(function (content) {
view.setExportState('');
view.setExportProgress(0);
FileSaver.saveAs(content, 'courseware-export-' + new Date().toISOString().slice(0, 10) + '.zip');
});
},
async createExportFile(root_id = null, options) {
let completeExport = false;
if (!root_id) {
root_id = this.courseware.relationships.root.data.id;
completeExport = true;
}
this.setExportState(this.$gettext('Exportiere Elemente'));
this.setExportProgress(0);
let exportData = await this.exportCourseware(root_id, options);
let zip = new JSZip();
zip.file('courseware.json', JSON.stringify(exportData.json));
zip.file('files.json', JSON.stringify(exportData.files.json));
if (completeExport) {
zip.file('settings.json', JSON.stringify(exportData.settings));
}
// add all additional files from blocks
let i = 1;
let filesCounter = Object.keys(exportData.files.download).length;
this.setExportState(this.$gettext('Lade Dateien'));
this.setExportProgress(0);
for (let id in exportData.files.download) {
zip.file(
id,
await fetch(exportData.files.download[id].url)
.then((response) => response.blob())
.then((textString) => {
return textString;
})
);
this.setExportProgress(parseInt(i / filesCounter * 100));
i++;
}
return zip;
},
async exportCourseware(root_id, options) {
let withChildren = false;
if (options && options.withChildren === true) {
withChildren = true;
}
let root_element = await this.structuralElementById({id: root_id});
//prevent loss of data
root_element = JSON.parse(JSON.stringify(root_element));
// load whole courseware nonetheless, only export relevant elements
let elements = await this.$store.getters['courseware-structural-elements/all'];
this.exportElementCounter = 0;
if (withChildren) {
this.elementCounter = this.countElements(elements);
} else {
this.elementCounter = root_element.relationships.containers.length;
}
root_element.containers = [];
if (root_element.relationships.containers?.data?.length) {
for (var j = 0; j < root_element.relationships.containers.data.length; j++) {
root_element.containers.push(
await this.exportContainer(
this.containerById({
id: root_element.relationships.containers.data[j].id,
})
)
);
this.exportElementCounter++;
}
}
if (withChildren && elements !== []) {
let children = await this.exportStructuralElement(root_id, elements);
if (children.length) {
root_element.children = children;
}
}
root_element.imageId = await this.exportStructuralElementImage(root_element);
delete root_element.relationships;
delete root_element.links;
let settings = {
'editing-permission-level': this.courseware.attributes['editing-permission-level'],
'sequential-progression': this.courseware.attributes['sequential-progression']
};
return {
json: root_element,
files: this.exportFiles,
settings: settings
};
},
countElements(elements) {
let counter = 0;
for (var i = 0; i < elements.length; i++) {
counter++;
counter += elements[i].relationships.containers.data.length;
}
return counter;
},
async exportToOER(element, options) {
let formData = new FormData();
let exportZip = await this.createExportFile(element.id, options);
let zip = await exportZip.generateAsync({ type: 'blob' });
let description = element.attributes.payload.description ? element.attributes.payload.description : '';
let difficulty_start = element.attributes.payload.difficulty_start ? element.attributes.payload.difficulty_start : '1';
let difficulty_end = element.attributes.payload.difficulty_end ? element.attributes.payload.difficulty_end : '12';
if (element.relationships.image.data !== null) {
let image = {};
await axios.get(element.relationships.image.meta['download-url'] , {responseType: 'blob'}).then(response => { image = response.data });
formData.append("image", image);
}
formData.append("data[name]", element.attributes.title);
formData.append("tags[]", "Lernmaterial");
formData.append("file", zip, (element.attributes.title).replace(/\s+/g, '_') + '.zip');
formData.append("data[description]", description);
formData.append("data[difficulty_start]", difficulty_start);
formData.append("data[difficulty_end]", difficulty_end);
formData.append("data[category]", 'elearning');
axios({
method: 'post',
url: STUDIP.URLHelper.getURL('dispatch.php/oer/mymaterial/edit/'),
data: formData,
headers: { "Content-Type": "multipart/form-data"}
}).then( () => {
this.companionInfo({ info: this.$gettextInterpolate('Die Seite wurde an %{ oerTitle } gesendet.', {oerTitle: this.oerTitle}) });
}).catch(error => {
this.companionError({ info: this.$gettext('Beim Veröffentlichen der Seite ist ein Fehler aufgetreten.') });
console.debug(error);
});
},
async exportStructuralElement(parentId, data) {
let children = [];
for (var i = 0; i < data.length; i++) {
if (data[i].relationships.parent.data?.id === parentId && data[i].attributes['can-edit']) {
let new_childs = await this.exportStructuralElement(data[i].id, data);
this.exportElementCounter++;
let content = { ...data[i] };
content.containers = [];
await this.loadStructuralElement(content.id);
let element = this.structuralElementById({ id: content.id });
// load containers, if there are any for this struct
if (element.relationships.containers?.data?.length) {
for (var j = 0; j < element.relationships.containers.data.length; j++) {
content.containers.push(
await this.exportContainer(
this.containerById({
id: element.relationships.containers.data[j].id,
})
)
);
this.exportElementCounter++;
}
}
// export file data (if any)
content.imageId = await this.exportStructuralElementImage(element);
delete content.relationships;
content.children = new_childs;
children.push(content);
}
}
return children;
},
async exportStructuralElementImage(element) {
let fileId = element.relationships.image?.data?.id;
if (fileId) {
await this.$store.dispatch('file-refs/loadById', {id: fileId});
let fileRef = this.$store.getters['file-refs/byId']({id: fileId});
let fileRefData = {};
fileRefData.id = fileRef.id;
fileRefData.attributes = fileRef.attributes;
fileRefData.related_element_id = element.id;
fileRefData.folder = null;
this.exportFiles.json.push(fileRefData);
this.exportFiles.download[fileRef.id] = {
folder: null,
url: fileRef.meta['download-url']
};
}
return fileId;
},
async exportContainer(container_ref) {
// make a local copy of the container
let container = { ...container_ref };
container.blocks = [];
let blocks = this.$store.getters['courseware-blocks/all'];
// now, load the blocks for this container, if there are any
if (blocks.length) {
for (var k = 0; k < blocks.length; k++) {
if (blocks[k].relationships.container?.data.id === container.id) {
container.blocks.push(await this.exportBlock(blocks[k]));
}
}
}
delete container.relationships;
return container;
},
async exportBlock(block_ref) {
// make a local copy of the block
let block = { ...block_ref };
// export file data (if any)
if (block_ref.relationships['file-refs']?.links?.related) {
await this.exportFileRefs(block_ref.id);
}
delete block.relationships;
return block;
},
async exportFileRefs(block_id) {
// load file-ref data
let refs = []
try {
refs = await this.loadFileRefs(block_id);
} catch(e) {
//TODO: Companion explains error
}
// add infos to exportFiles JSON
for (let ref_id in refs) {
let fileref = {};
let folderId = refs[ref_id].relationships.parent.data.id;
let folder = null;
fileref.attributes = refs[ref_id].attributes;
fileref.related_block_id = block_id;
fileref.id = refs[ref_id].id;
try {
await this.loadFolder(folderId);
folder = this.folderById({id: folderId});
} catch(e) {
//TODO: Companion explains error
}
if (folder) {
fileref.folder = {
id: folder.id,
name: folder.attributes.name,
type: folder.attributes['folder-type']
}
} else {
fileref.folder = {
id: folderId,
name: 'Unknown',
type: 'StandardFolder'
}
}
this.exportFiles.json.push(fileref);
// prevent multiple downloads of the same file
this.exportFiles.download[refs[ref_id].id] = {
folder: folderId,
url: refs[ref_id].meta['download-url']
};
}
},
...mapActions([
'loadStructuralElement',
'loadFileRefs',
'loadFolder',
'companionInfo',
'setExportState',
'setExportProgress'
]),
},
watch: {
exportElementCounter(counter) {
if (this.elementCounter !== 0) {
this.setExportProgress(parseInt(counter / this.elementCounter * 100));
}
}
},
};
|