/**
*
* OpenOLAT - Online Learning and Training
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at the
* Apache homepage
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Initial code contributed and copyrighted by
* frentix GmbH, http://www.frentix.com
*
*/
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
* @author srosse, stephane.rosse@frentix.com, 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