use of org.olat.ims.qti.editor.beecom.objects.Mattext in project openolat by klemens.
the class FIBItemController method event.
/**
* @see org.olat.core.gui.control.DefaultController#event(org.olat.core.gui.UserRequest,
* org.olat.core.gui.components.Component, org.olat.core.gui.control.Event)
*/
@Override
public void event(UserRequest ureq, Component source, Event event) {
if (source == main) {
// olat::: as: improve easy fix since almost all operations change the main vc.
main.setDirty(true);
String cmd = event.getCommand();
String sPosid = ureq.getParameter("posid");
int posid = 0;
if (sPosid != null)
posid = Integer.parseInt(sPosid);
if (cmd.equals("up")) {
List<Response> elements = item.getQuestion().getResponses();
if (posid > 0 && posid < item.getQuestion().getResponses().size()) {
Response obj = elements.remove(posid);
elements.add(posid - 1, obj);
} else {
logError("posid doesn't match responses length: " + posid + "/" + elements.size(), null);
}
} else if (cmd.equals("down")) {
List<Response> elements = item.getQuestion().getResponses();
if (posid >= 0 && posid < elements.size() - 1) {
Response obj = elements.remove(posid);
elements.add(posid + 1, obj);
} else {
logError("posid doesn't match responses length: " + posid + "/" + elements.size(), null);
}
} else if (cmd.equals("editq")) {
editQuestion = item.getQuestion().getQuestion();
displayMaterialFormController(ureq, editQuestion, restrictedEdit);
} else if (cmd.equals("editr")) {
List<Response> elements = item.getQuestion().getResponses();
if (posid >= 0 && posid < elements.size()) {
editResponse = elements.get(posid);
Material responseMat = elements.get(posid).getContent();
displayMaterialFormController(ureq, responseMat, restrictedEdit);
} else {
logError("posid doesn't match responses length: " + posid + "/" + elements.size(), null);
}
} else if (cmd.equals("addtext")) {
FIBQuestion fib = (FIBQuestion) item.getQuestion();
FIBResponse response = new FIBResponse();
response.setType(FIBResponse.TYPE_CONTENT);
Material mat = new Material();
mat.add(new Mattext(translate("newtextelement")));
response.setContent(mat);
fib.getResponses().add(response);
} else if (cmd.equals("addblank")) {
FIBQuestion fib = (FIBQuestion) item.getQuestion();
FIBResponse response = new FIBResponse();
response.setType(FIBResponse.TYPE_BLANK);
response.setCorrectBlank("");
// default value
response.setPoints(1f);
fib.getResponses().add(response);
} else if (cmd.equals("del")) {
delYesNoCtrl = DialogBoxUIFactory.createYesNoDialog(ureq, getWindowControl(), null, translate("confirm.delete.element"));
listenTo(delYesNoCtrl);
delYesNoCtrl.setUserObject(new Integer(posid));
delYesNoCtrl.activate();
} else if (cmd.equals("sfib")) {
// submit fib
FIBQuestion question = (FIBQuestion) item.getQuestion();
// Survey specific variables
if (surveyMode) {
List<Response> responses = question.getResponses();
for (int i = 0; i < responses.size(); i++) {
FIBResponse response = (FIBResponse) responses.get(i);
if (FIBResponse.TYPE_BLANK.equals(response.getType())) {
// Set size of input field
String size = ureq.getParameter("size_q" + i);
if (size != null)
response.setSizeFromString(size);
String maxLength = ureq.getParameter("maxl_q" + i);
if (maxLength != null)
response.setMaxLengthFromString(maxLength);
}
}
} else {
// set min/max values before single_correct !!
if (!restrictedEdit) {
// only in full edit mode the following fields are available:
// min_value, max_value, valuation_method
question.setMinValue(ureq.getParameter("min_value"));
question.setMaxValue(ureq.getParameter("max_value"));
question.setSingleCorrect("single".equals(ureq.getParameter("valuation_method")));
if (question.isSingleCorrect()) {
question.setSingleCorrectScore(ureq.getParameter("single_score"));
} else {
question.setSingleCorrectScore(0.0f);
}
}
NodeBeforeChangeEvent nce = new NodeBeforeChangeEvent();
nce.setItemIdent(item.getIdent());
List<Response> responses = question.getResponses();
for (int i = 0; i < responses.size(); i++) {
FIBResponse response = (FIBResponse) responses.get(i);
nce.setResponseIdent(response.getIdent());
fireEvent(ureq, nce);
response.setPoints(ureq.getParameter("points_q" + i));
if (FIBResponse.TYPE_BLANK.equals(response.getType())) {
response.setCorrectBlank(ureq.getParameter("content_q" + i));
// Set case sensitiveness
String caseSensitive = ureq.getParameter("case_q" + i);
if (caseSensitive == null)
caseSensitive = "No";
response.setCaseSensitive(caseSensitive);
// Set size of input field
String size = ureq.getParameter("size_q" + i);
if (size != null)
response.setSizeFromString(size);
String maxLength = ureq.getParameter("maxl_q" + i);
if (maxLength != null)
response.setMaxLengthFromString(maxLength);
// find longest correct blank in all synonyms of
// correct answers, fix max lenght if a longer value
// is found
String[] allCorrect = response.getCorrectBlank().split(";");
int longestCorrect = 0;
for (int j = 0; j < allCorrect.length; j++) {
String singleCorrect = allCorrect[j];
if (singleCorrect.length() > longestCorrect) {
longestCorrect = singleCorrect.length();
}
}
if (longestCorrect > response.getMaxLength())
response.setMaxLength(longestCorrect);
}
}
}
}
qtiPackage.serializeQTIDocument();
}
}
use of org.olat.ims.qti.editor.beecom.objects.Mattext in project OpenOLAT by OpenOLAT.
the class CSVToQuestionConverterTest method importMultipleChoice.
@Test
public void importMultipleChoice() throws IOException, URISyntaxException {
URL importTxtUrl = CSVToQuestionConverterTest.class.getResource("question_import_mc.txt");
Assert.assertNotNull(importTxtUrl);
File importTxt = new File(importTxtUrl.toURI());
String input = FileUtils.readFileToString(importTxt, "UTF-8");
Translator translator = new KeyTranslator(Locale.ENGLISH);
ImportOptions options = new ImportOptions();
options.setShuffle(true);
CSVToQuestionConverter converter = new CSVToQuestionConverter(translator, options);
converter.parse(input);
List<ItemAndMetadata> items = converter.getItems();
Assert.assertNotNull(items);
Assert.assertEquals(1, items.size());
ItemAndMetadata importedItem = items.get(0);
Item item = importedItem.getItem();
Assert.assertNotNull(item);
Assert.assertEquals("Fussball: Austragungsort", item.getTitle());
Assert.assertEquals("Die Fussball WM wird alle vier Jahre von einem anderen Land ausgerichtet.", item.getObjectives());
Assert.assertEquals(Question.TYPE_MC, item.getQuestion().getType());
Assert.assertTrue(item.getQuestion() instanceof ChoiceQuestion);
Material questionMat = item.getQuestion().getQuestion();
Assert.assertNotNull(questionMat);
Assert.assertNotNull(questionMat.getElements());
Assert.assertEquals(1, questionMat.getElements().size());
QTIObject questionMatEl = questionMat.getElements().get(0);
Assert.assertTrue(questionMatEl instanceof Mattext);
String text = ((Mattext) questionMatEl).getContent();
Assert.assertEquals("In welchen L\u00E4ndern wurde zwischen dem Jahr 2000 und 2015 eine Fussball Weltmeisterschaft ausgetragen?", text);
ChoiceQuestion question = (ChoiceQuestion) item.getQuestion();
Assert.assertNotNull(question.getResponses());
Assert.assertEquals(7, question.getResponses().size());
List<Response> responses = question.getResponses();
Assert.assertEquals(1.0f, responses.get(0).getPoints(), 0.0001);
Assert.assertEquals(1.0f, responses.get(1).getPoints(), 0.0001);
Assert.assertEquals(1.0f, responses.get(2).getPoints(), 0.0001);
Assert.assertEquals(-1.0f, responses.get(3).getPoints(), 0.0001);
Assert.assertEquals(-1.0f, responses.get(4).getPoints(), 0.0001);
Assert.assertEquals(-1.0f, responses.get(5).getPoints(), 0.0001);
Assert.assertEquals(-1.0f, responses.get(6).getPoints(), 0.0001);
// after it will be set to true for all of them
Assert.assertTrue(responses.get(0).isCorrect());
Assert.assertTrue(responses.get(1).isCorrect());
Assert.assertTrue(responses.get(2).isCorrect());
Assert.assertFalse(responses.get(3).isCorrect());
Assert.assertFalse(responses.get(4).isCorrect());
Assert.assertFalse(responses.get(5).isCorrect());
Assert.assertFalse(responses.get(6).isCorrect());
String feedbackMastery = QTIEditHelper.getFeedbackMasteryText(item);
Assert.assertEquals("Bravo! Die Antwort ich absolut korrekt.", feedbackMastery);
String feedbackFail = QTIEditHelper.getFeedbackFailText(item);
Assert.assertEquals("Leider falsch. Probieren Sie es noch einmal.", feedbackFail);
}
use of org.olat.ims.qti.editor.beecom.objects.Mattext in project OpenOLAT by OpenOLAT.
the class MaterialFormController method formOK.
/**
* @see org.olat.core.gui.components.form.flexible.impl.FormBasicController#formOK(org.olat.core.gui.UserRequest)
*/
@Override
protected void formOK(UserRequest ureq) {
// trust authors, don't to XSS filtering
String newHtml = richText.getRawValue();
// the text fragment is saved in a cdata, remove cdata from movie plugin
newHtml = newHtml.replace("// <![CDATA[", "").replace("// ]]>", "");
// Use explicit return which create a P tag if you want a line break.
if (newHtml.startsWith("<br />") && newHtml.length() > 6)
newHtml = newHtml.substring(6);
if (newHtml.endsWith("<br />") && newHtml.length() > 6)
newHtml = newHtml.substring(0, newHtml.length() - 6);
// Remove any conditional comments due to strange behavior in test (OLAT-4518)
Filter conditionalCommentFilter = FilterFactory.getConditionalHtmlCommentsFilter();
newHtml = conditionalCommentFilter.filter(newHtml);
//
if (htmlContent.equals(newHtml)) {
// No changes. Cancel editing.
fireEvent(ureq, Event.CANCELLED_EVENT);
} else {
if (isRestrictedEditMode) {
// In restricted edit mode, if the content has changed, write a memento
// (by firing the before change event).
QTIObjectBeforeChangeEvent qobce = new QTIObjectBeforeChangeEvent();
qobce.init(mat.getId(), htmlContent);
fireEvent(ureq, qobce);
}
// Collect the content of all MatElements in a single text element
// (text/html) and save it (for Material objects with multiple elements
// such as images, videos, text, breaks, etc. this can be regarded as
// "lazy migration" to the new rich text style).
Mattext textHtml = new Mattext(newHtml);
// A single text/html element will be left over.
List<QTIObject> elements = new ArrayList<QTIObject>(1);
elements.add(textHtml);
mat.setElements(elements);
fireEvent(ureq, Event.DONE_EVENT);
}
}
use of org.olat.ims.qti.editor.beecom.objects.Mattext in project OpenOLAT by OpenOLAT.
the class QTIEditHelper method createSCItem.
/**
* Creates a new Single Choice item
* @param trans
* @return New Singe Choice item.
*/
public static Item createSCItem(Translator trans) {
Item newItem = new Item();
newItem.setIdent(EDITOR_IDENT + ":" + ITEM_TYPE_SC + ":" + String.valueOf(CodeHelper.getRAMUniqueID()));
newItem.setTitle(trans.translate("editor.newquestion"));
newItem.setLabel("");
// controls
Control control = new Control();
List<Control> controls = new ArrayList<Control>();
controls.add(control);
newItem.setItemcontrols(controls);
// pepare question
ChoiceQuestion question = new ChoiceQuestion();
question.setLable(trans.translate("editor.newquestion"));
question.getQuestion().getElements().add(new Mattext(trans.translate("editor.newquestiontext")));
question.setType(Question.TYPE_SC);
question.setSingleCorrect(true);
question.setSingleCorrectScore(1);
ChoiceResponse newChoice = new ChoiceResponse();
newChoice.setCorrect(true);
newChoice.getContent().add(new Mattext(trans.translate("editor.newresponsetext")));
question.getResponses().add(newChoice);
QTIEditHelper.setFeedbackMastery(newItem, "");
QTIEditHelper.setFeedbackFail(newItem, "");
newItem.setQuestion(question);
return newItem;
}
use of org.olat.ims.qti.editor.beecom.objects.Mattext in project OpenOLAT by OpenOLAT.
the class QTIEditHelper method createFIBItem.
/**
* Creates a new FIB item
* @param trans
* @return New fib item.
*/
public static Item createFIBItem(Translator trans) {
// create item
Item newItem = new Item();
newItem.setIdent(EDITOR_IDENT + ":" + ITEM_TYPE_FIB + ":" + String.valueOf(CodeHelper.getRAMUniqueID()));
newItem.setTitle(trans.translate("editor.newquestion"));
newItem.setLabel("");
// conrols
Control control = new Control();
List<Control> controls = new ArrayList<Control>();
controls.add(control);
newItem.setItemcontrols(controls);
QTIEditHelper.setFeedbackMastery(newItem, "");
QTIEditHelper.setFeedbackFail(newItem, "");
FIBQuestion fibquestion = new FIBQuestion();
fibquestion.getQuestion().getElements().add(new Mattext(trans.translate("editor.newquestiontext")));
fibquestion.setSingleCorrect(true);
fibquestion.setSingleCorrectScore(1);
FIBResponse response = new FIBResponse();
response.setType(FIBResponse.TYPE_CONTENT);
Material mat = new Material();
mat.add(new Mattext(trans.translate("editor.newtextelement")));
response.setContent(mat);
fibquestion.getResponses().add(response);
newItem.setQuestion(fibquestion);
return newItem;
}
Aggregations