/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <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>
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <p>
*/
package org.olat.fileresource;
import java.io.File;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.olat.core.gui.components.form.Form;
import org.olat.core.gui.formelements.HTMLTextAreaElement;
import org.olat.core.gui.formelements.StaticTextElement;
import org.olat.core.gui.translator.Translator;
import org.olat.core.id.OLATResourceable;
import org.olat.core.util.vfs.LocalFileImpl;
import org.olat.core.util.vfs.LocalFolderImpl;
import org.olat.core.util.vfs.VFSContainer;
import org.olat.core.util.vfs.VFSItem;
import org.olat.fileresource.types.ImsCPFileResource;
import org.olat.fileresource.types.ScormCPFileResource;
import org.olat.fileresource.types.SurveyFileResource;
import org.olat.fileresource.types.TestFileResource;
import org.olat.ims.qti.process.QTIHelper;
/**
* Initial Date: Apr 19, 2004
*
* @author Mike Stock
*
*/
public class FileDetailsForm extends Form {
private OLATResourceable res;
private File file;
/**
* Create details form with values from resourceable res.
* @param name
* @param locale
* @param res
*/
public FileDetailsForm(String name, Translator translator, OLATResourceable res) {
super(name, translator);
this.res = res;
file = FileResourceManager.getInstance().getFileResource(res);
init();
}
/**
* Initialize the form.
*/
public void init() {
addFormElement("size", new StaticTextElement("fr.size",
new Long(file.length() / 1024).toString() + " KB"));
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, getLocale());
addFormElement("last", new StaticTextElement("fr.last", df.format(new Date(file.lastModified()))));
String resType = res.getResourceableTypeName();
if (resType.equals(TestFileResource.TYPE_NAME) || resType.equals(SurveyFileResource.TYPE_NAME)) addQTIDetails();
else if (resType.equals(ImsCPFileResource.TYPE_NAME) || resType.equals(ScormCPFileResource.TYPE_NAME)) addIMSCPDetails();
setDisplayOnly(true);
}
/**
* @see org.olat.core.gui.components.Form#validate(org.olat.core.gui.UserRequest)
*/
public boolean validate() {
return true;
}
/**
*
*/
private void addIMSCPDetails() {
return;
}
/**
*
*/
private void addQTIDetails() {
FileResourceManager frm = FileResourceManager.getInstance();
File unzippedRoot = frm.unzipFileResource(res);
//with VFS FIXME:pb:c: remove casts to LocalFileImpl and LocalFolderImpl if no longer needed.
VFSContainer vfsUnzippedRoot = new LocalFolderImpl(unzippedRoot);
VFSItem vfsQTI = vfsUnzippedRoot.resolve("qti.xml");
//getDocument(..) ensures that InputStream is closed in every case.
Document doc = QTIHelper.getDocument((LocalFileImpl) vfsQTI);
//if doc is null an error loading the document occured (IOException, qti.xml does not exist)
if (doc == null) return;
// extract title
Element el_assess = (Element)doc.selectSingleNode("questestinterop/assessment");
String title = el_assess.attributeValue("title");
if (title != null) addFormElement("qti.title", new StaticTextElement("qti.title", title));
else addFormElement("qti.title", new StaticTextElement("qti.title", "-"));
// extract objectives
HTMLTextAreaElement htmlTA = new HTMLTextAreaElement("qti.objectives", 10, 60);
htmlTA.setReadOnly(true);
Element el_objectives = (Element)doc.selectSingleNode("//questestinterop/assessment/objectives");
if (el_objectives != null) {
Element el_mat = (Element)el_objectives.selectSingleNode("material/mattext");
if (el_mat != null)
htmlTA.setValue(el_mat.getTextTrim());
} else htmlTA.setValue("-");
addFormElement("qti.objectives", htmlTA);
// extract num of questions
List items = doc.selectNodes("//item");
if (items.size() > 0)
addFormElement("qti.questions", new StaticTextElement("qti.questions", "" + items.size()));
else addFormElement("qti.questions", new StaticTextElement("qti.questions", "-"));
// extract time limit
Element el_duration = (Element)el_assess.selectSingleNode("duration");
if (el_duration != null) {
long dur = QTIHelper.parseISODuration(el_duration.getTextTrim());
long min = dur / 1024 / 60;
long sec = (dur - (min * 60 * 1024)) / 1024;
addFormElement("qti.timelimit", new StaticTextElement("qti.timelimit", min + "' " + sec + "''"));
} else addFormElement("qti.timelimit", new StaticTextElement("qti.timelimit", "-"));
}
}