Datasets:

Modalities:
Text
Formats:
json
Languages:
code
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
File size: 14,958 Bytes
eb67da4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
402
403
404
405
406
/**
 * <a href="http://www.openolat.org">
 * OpenOLAT - Online Learning and Training</a><br>
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); <br>
 * you may not use this file except in compliance with the License.<br>
 * You may obtain a copy of the License at the
 * <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
 * <p>
 * Unless required by applicable law or agreed to in writing,<br>
 * software distributed under the License is distributed on an "AS IS" BASIS, <br>
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
 * See the License for the specific language governing permissions and <br>
 * limitations under the License.
 * <p>
 * Initial code contributed and copyrighted by<br>
 * frentix GmbH, http://www.frentix.com
 * <p>
 */
package org.olat.course.assessment.bulk;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.apache.commons.io.IOUtils;
import org.olat.core.gui.UserRequest;
import org.olat.core.gui.components.form.flexible.FormItemContainer;
import org.olat.core.gui.components.form.flexible.elements.FileElement;
import org.olat.core.gui.components.form.flexible.elements.SingleSelection;
import org.olat.core.gui.components.form.flexible.elements.TextElement;
import org.olat.core.gui.components.form.flexible.impl.Form;
import org.olat.core.gui.control.Controller;
import org.olat.core.gui.control.WindowControl;
import org.olat.core.gui.control.generic.wizard.StepFormBasicController;
import org.olat.core.gui.control.generic.wizard.StepsEvent;
import org.olat.core.gui.control.generic.wizard.StepsRunContext;
import org.olat.core.util.FileUtils;
import org.olat.core.util.StringHelper;
import org.olat.core.util.WebappHelper;
import org.olat.core.util.vfs.LocalFileImpl;
import org.olat.core.util.vfs.LocalImpl;
import org.olat.core.util.vfs.VFSContainer;
import org.olat.core.util.vfs.VFSItem;
import org.olat.core.util.vfs.VFSLeaf;
import org.olat.core.util.vfs.VFSManager;
import org.olat.course.assessment.model.BulkAssessmentDatas;
import org.olat.course.assessment.model.BulkAssessmentRow;
import org.olat.course.assessment.model.BulkAssessmentSettings;
import org.olat.course.nodes.CourseNode;
import org.olat.course.nodes.GTACourseNode;
import org.olat.modules.assessment.model.AssessmentEntryStatus;

/**
 *
 * Initial date: 18.11.2013<br>
 * @author srosse, [email protected], http://www.frentix.com
 *
 */
public class DataStepForm extends StepFormBasicController {

	private static final String[] keys = new String[] { "tab", "comma" };
	private static final String[] statusKeys = new String[] { AssessmentEntryStatus.done.name(), AssessmentEntryStatus.inReview.name(), "not" };
	private static final String[] visibilityKeys = new String[] { "visible", "notvisible", "notchanged" };
	private static final String[] submissionKeys = new String[] { "accept", "notchanged" };

	private TextElement dataEl;
	private FileElement returnFileEl;
	private SingleSelection delimiter;
	private SingleSelection statusEl;
	private SingleSelection visibilityEl;
	private SingleSelection acceptSubmissionEl;

	private VFSLeaf targetArchive;
	private BulkAssessmentDatas savedDatas;
	private final CourseNode courseNode;
	private VFSContainer bulkAssessmentTmpDir;

	public DataStepForm(UserRequest ureq, WindowControl wControl, StepsRunContext runContext, Form rootForm) {
		super(ureq, wControl, rootForm, runContext, LAYOUT_VERTICAL, null);

		courseNode = (CourseNode)getFromRunContext("courseNode");

		initForm(ureq);
	}

	public DataStepForm(UserRequest ureq, WindowControl wControl, CourseNode courseNode, BulkAssessmentDatas savedDatas,
			StepsRunContext runContext, Form rootForm) {
		super(ureq, wControl, rootForm, runContext, LAYOUT_VERTICAL, null);

		this.savedDatas = savedDatas;
		this.courseNode = courseNode;
		addToRunContext("courseNode", courseNode);
		initForm(ureq);
	}

	@Override
	protected void initForm(FormItemContainer formLayout, Controller listener, UserRequest ureq) {
		formLayout.setElementCssClass("o_sel_bulk_assessment_data");
		
		// hide data input field in case the element does not have any score, passed or comment field enabled
		BulkAssessmentSettings settings = new BulkAssessmentSettings(courseNode);
		boolean onlyReturnFiles = (!settings.isHasScore() && !settings.isHasPassed() && !settings.isHasUserComment());

		setFormTitle("data.title");
		if (!onlyReturnFiles) {
			setFormDescription("data.description");
		}
		setFormContextHelp("... create a bulk assessment for submission tasks");

		String dataVal = "";
		if(savedDatas != null && StringHelper.containsNonWhitespace(savedDatas.getDataBackupFile())) {
			VFSLeaf file = VFSManager.olatRootLeaf(savedDatas.getDataBackupFile());
			try(InputStream in = file.getInputStream()) {
				dataVal = IOUtils.toString(in, StandardCharsets.UTF_8);
			} catch (IOException e) {
				logError("", e);
			}
		}

		dataEl = uifactory.addTextAreaElement("data", "data", -1, 6, 60, true, false, dataVal, formLayout);
		dataEl.showLabel(false);

		String[] values = new String[] {translate("form.step3.delimiter.tab"),translate("form.step3.delimiter.comma")};
		delimiter = uifactory.addRadiosVertical("delimiter", "form.step3.delimiter", formLayout, keys, values);
		// preset delimiter type to first appearance of either tab or comma when data is available, default to tab for no data
		int firstComma = dataVal.indexOf(',');
		int firstTab = dataVal.indexOf('\t');
		if (firstComma > -1 && (firstTab == -1 || firstTab > firstComma )) {
			delimiter.select("comma", true);
		} else {
			delimiter.select("tab", true);
		}
		
		String[] statusValues = new String[] {
				translate("form.step3.status.assessed"), translate("form.step3.status.review"),
				translate("form.step3.status.dont.change")
		};
		statusEl = uifactory.addRadiosVertical("form.step3.status", "form.step3.status", formLayout, statusKeys, statusValues);
		statusEl.select(statusKeys[statusKeys.length - 1], true);
		String[] visibilityValues = new String[] {
				translate("form.step3.visibility.visible"), translate("form.step3.visibility.notvisible"),
				translate("form.step3.visibility.dont.change")
		};
		visibilityEl = uifactory.addRadiosVertical("form.step3.visibility", "form.step3.visibility", formLayout, visibilityKeys, visibilityValues);
		visibilityEl.select(visibilityKeys[visibilityKeys.length - 1], true);
		
		if(courseNode instanceof GTACourseNode) {
			String[] submissionValues = new String[] {
				translate("form.step3.submission.accept"), translate("form.step3.submission.dont.change")
			};
			acceptSubmissionEl = uifactory.addRadiosVertical("form.step3.submission", "form.step3.submission", formLayout, submissionKeys, submissionValues);
			acceptSubmissionEl.select(submissionKeys[submissionKeys.length - 1], true);
			acceptSubmissionEl.setHelpTextKey("form.step3.submission.help", null);
		}

		// hide data input field in case the element does not have any score, passed or comment field enabled
		if (onlyReturnFiles) {
			dataEl.setVisible(false);
			delimiter.setVisible(false);
		}

		// return files only when configured
		if(settings.isHasReturnFiles()) {
			returnFileEl = uifactory.addFileElement(getWindowControl(), "returnfiles", "return.files", formLayout);
			Set<String> mimes = new HashSet<>();
			mimes.add(WebappHelper.getMimeType("file.zip"));
			returnFileEl.limitToMimeType(mimes, "return.mime", null);
			if(savedDatas != null && StringHelper.containsNonWhitespace(savedDatas.getReturnFiles())) {
				targetArchive = VFSManager.olatRootLeaf(savedDatas.getReturnFiles());
				if(targetArchive.exists()) {
					returnFileEl.setInitialFile(((LocalFileImpl)targetArchive).getBasefile());
				}
			}
		}
	}

	@Override
	protected void doDispose() {
		//
	}

	@Override
	protected void formOK(UserRequest ureq) {
		String val = dataEl.getValue();
		if (!StringHelper.containsNonWhitespace(val) && (returnFileEl != null ? !returnFileEl.isUploadSuccess() : true)) {
			// do not proceed when nothin in input field and no file uploaded
			setFormWarning("form.step2.error");
			return;
		}
		setFormWarning(null); // reset error
		BulkAssessmentDatas datas = (BulkAssessmentDatas)getFromRunContext("datas");
		if(datas == null) {
			datas = new BulkAssessmentDatas();
		}
		
		if(statusEl.isOneSelected()) {
			String selectedStatus = statusEl.getSelectedKey();
			if(AssessmentEntryStatus.isValueOf(selectedStatus)) {
				datas.setStatus(AssessmentEntryStatus.valueOf(selectedStatus));
			}
		}
		
		if(visibilityEl.isOneSelected()) {
			String selectedVisibility = visibilityEl.getSelectedKey();
			if("visible".equals(selectedVisibility)) {
				datas.setVisibility(Boolean.TRUE);
			} else if("notvisible".equals(selectedVisibility)) {
				datas.setVisibility(Boolean.FALSE);
			}
		}
		
		if(acceptSubmissionEl != null && acceptSubmissionEl.isOneSelected()) {
			datas.setAcceptSubmission(acceptSubmissionEl.isSelected(0));
		}

		if(bulkAssessmentTmpDir == null) {
			VFSContainer bulkAssessmentDir = VFSManager.olatRootContainer("/bulkassessment/", null);
			bulkAssessmentTmpDir = bulkAssessmentDir.createChildContainer(UUID.randomUUID().toString());
		}

		backupInputDatas(val, datas, bulkAssessmentTmpDir);
		List<String[]> splittedRows = splitRawData(val);
		addToRunContext("splittedRows", splittedRows);
		List<BulkAssessmentRow> rows = new ArrayList<>(100);
		if(returnFileEl != null) {
			processReturnFiles(datas, rows, bulkAssessmentTmpDir);
		}
		datas.setRows(rows);
		addToRunContext("datas", datas);
		fireEvent(ureq, StepsEvent.ACTIVATE_NEXT);
	}

	private List<String[]> splitRawData(String idata) {
		String[] lines = idata.split("\r?\n");
		int numOfLines = lines.length;

		List<String[]> rows = new ArrayList<>(numOfLines);

		String d;
		if (delimiter.getSelectedKey().startsWith("t")) {
			d = "\t";
		} else {
			d = ",";
		}

		for (int i = 0; i < numOfLines; i++) {
			String line = lines[i];
			if(StringHelper.containsNonWhitespace(line)){
				String[] values = line.split(d,-1);
				rows.add(values);
			}
		}
		return rows;
	}

	/**
	 * Backup the input field for later editing purpose
	 * @param val
	 * @param datas
	 */
	private void backupInputDatas(String val, BulkAssessmentDatas datas, VFSContainer tmpDir) {
		VFSLeaf inputFile = null;
		if(StringHelper.containsNonWhitespace(datas.getDataBackupFile())) {
			inputFile = VFSManager.olatRootLeaf(datas.getDataBackupFile());
		}
		if(inputFile == null) {
			String inputFilename = UUID.randomUUID().toString() + ".csv";
			inputFile = tmpDir.createChildLeaf(inputFilename);
		}

		try(OutputStream out = inputFile.getOutputStream(false)) {
			IOUtils.write(val, out, StandardCharsets.UTF_8);
			datas.setDataBackupFile(inputFile.getRelPath());
		} catch (IOException e) {
			logError("", e);
		}
	}

	private void processReturnFiles(BulkAssessmentDatas datas, List<BulkAssessmentRow> rows, VFSContainer tmpDir) {
		File uploadedFile = returnFileEl.getUploadFile();
		if(uploadedFile == null) {
			File initialFile = returnFileEl.getInitialFile();
			if(initialFile != null && initialFile.exists()) {
				datas.setReturnFiles(targetArchive.getRelPath());
				processReturnFiles(targetArchive, rows);
			}
		} else if(uploadedFile.exists()) {
			//transfer to secured
			try {
				String uploadedFilename = returnFileEl.getUploadFileName();
				if(!StringHelper.containsNonWhitespace(uploadedFilename)) {
					uploadedFilename = "bulkAssessment.zip";
				}

				VFSItem currentTarget = tmpDir.resolve(uploadedFilename);
				if(!isSame(currentTarget, uploadedFile)) {
					if(currentTarget != null && currentTarget.exists()) {
						currentTarget.deleteSilently();
					}

					targetArchive = tmpDir.createChildLeaf(uploadedFilename);
					copyUploadFile(datas, uploadedFile, rows);
				} else {
					datas.setReturnFiles(targetArchive.getRelPath());
					processReturnFiles(targetArchive, rows);
				}
			} catch (IOException e) {
				logError("", e);
			}
		}
	}
	
	private void copyUploadFile(BulkAssessmentDatas datas, File uploadedFile, List<BulkAssessmentRow> rows) throws IOException {
		try(FileInputStream inStream = new FileInputStream(uploadedFile)) {
			if(VFSManager.copyContent(inStream, targetArchive)) {
				datas.setReturnFiles(targetArchive.getRelPath());
				processReturnFiles(targetArchive, rows);
			}
		} catch(IOException e) {
			logError("", e);
			throw e;
		}
	}

	private boolean isSame(VFSItem currentTarget, File uploadedFile) {
		if(currentTarget instanceof LocalImpl) {
			LocalImpl local = (LocalImpl)currentTarget;
			File currentFile = local.getBasefile();
			if(currentFile.length() == uploadedFile.length()) {
				try {
					return org.apache.commons.io.FileUtils.contentEquals(currentFile, uploadedFile);
				} catch (IOException e) {
					logError("", e);
					//do nothing -> return false at the end
				}
			}
		}
		return false;
	}

	private void processReturnFiles(VFSLeaf target, List<BulkAssessmentRow> rows) {
		Map<String, BulkAssessmentRow> assessedIdToRow = new HashMap<>();
		for(BulkAssessmentRow row:rows) {
			assessedIdToRow.put(row.getAssessedId(), row);
		}

		if(target.exists()) {
			File parentTarget = ((LocalImpl)target).getBasefile().getParentFile();

			ZipEntry entry;
			try(InputStream is = target.getInputStream();
					ZipInputStream zis = new ZipInputStream(is)) {
				byte[] b = new byte[FileUtils.BSIZE];
				// BUG: CWE-22 Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
				// while ((entry = zis.getNextEntry()) != null) {
				// FIXED:
				while ((entry = zis.getNextEntry()) != null) {//TODO zip
					if(!entry.isDirectory()) {
						while (zis.read(b) > 0) {
							//continue
						}

						Path op = new File(parentTarget, entry.getName()).toPath();
						if(!Files.isHidden(op) && !op.toFile().isDirectory()) {
							Path parentDir = op.getParent();
							String assessedId = parentDir.getFileName().toString();
							String filename = op.getFileName().toString();

							BulkAssessmentRow row;
							if(assessedIdToRow.containsKey(assessedId)) {
								row = assessedIdToRow.get(assessedId);
							} else {
								row = new BulkAssessmentRow();
								row.setAssessedId(assessedId);
								assessedIdToRow.put(assessedId, row);
								rows.add(row);
							}

							if(row.getReturnFiles() == null) {
								row.setReturnFiles(new ArrayList<String>(2));
							}
							row.getReturnFiles().add(filename);
						}
					}
				}
			} catch(Exception e) {
				logError("", e);
			}
		}
	}
}