|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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"); |
|
|
|
|
|
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); |
|
|
|
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); |
|
} |
|
|
|
|
|
if (onlyReturnFiles) { |
|
dataEl.setVisible(false); |
|
delimiter.setVisible(false); |
|
} |
|
|
|
|
|
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)) { |
|
|
|
setFormWarning("form.step2.error"); |
|
return; |
|
} |
|
setFormWarning(null); |
|
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; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
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()) { |
|
|
|
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); |
|
|
|
} |
|
} |
|
} |
|
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]; |
|
|
|
|
|
|
|
while ((entry = zis.getNextEntry()) != null) { |
|
if(!entry.isDirectory()) { |
|
while (zis.read(b) > 0) { |
|
|
|
} |
|
|
|
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); |
|
} |
|
} |
|
} |
|
} |