Skip to content
Snippets Groups Projects
Commit 115f8f1e authored by tobiasre's avatar tobiasre
Browse files

Merge branch 'feature/46' into 'main'

feat: Add support for the template dropdowns #46

See merge request !48
parents c3f64cf0 fc587eb9
No related branches found
No related tags found
1 merge request!48feat: Add support for the template dropdowns #46
......@@ -5,6 +5,7 @@ import com.quiz.converter.models.QuestionState;
import com.quiz.converter.models.enums.QuestionType;
import com.quiz.converter.models.enums.QuestionWarningType;
import java.util.List;
import java.util.Optional;
public class QuestionHandler {
......@@ -14,30 +15,30 @@ public class QuestionHandler {
this.state = state;
}
public Optional<Question> handleQuestion(String text) {
public Optional<Question> handleQuestion(String text, List<String> dropdownValues) {
Optional<Question> question = Optional.empty();
if (!state.getAnswerOptions().isEmpty()) {
if (!state.getName().isEmpty()) {
var validationHandler = new QuestionValidationHandler(state);
validationHandler.validateQuestion();
question = Optional.of(state.createQuestion());
state.cleanState();
}
addQuestionType(text);
addQuestionType(text, dropdownValues);
addQuestionName(text);
addQuestionOptions(text);
addQuestionOptions(text, dropdownValues);
return question;
}
private void addQuestionOptions(String text) {
private void addQuestionOptions(String text, List<String> dropdownValues) {
var lowerCaseText = text.toLowerCase();
if (lowerCaseText.matches(".*(no)\s*(shuffle).*")) {
if (matchesRegex(dropdownValues, lowerCaseText, ".*(no)\s*(shuffle).*")) {
state.setShuffle(false);
} else if (lowerCaseText.matches(".*(shuffle).*")) {
} else if (matchesRegex(dropdownValues, lowerCaseText, ".*(shuffle).*")) {
state.setShuffle(true);
}
if (lowerCaseText.matches(".*(no)\s*(partial)\s*(credit).*")) {
if (matchesRegex(dropdownValues, lowerCaseText, ".*(no)\s*(partial)\s*(credit).*")) {
state.setPartialCredit(false);
} else if (lowerCaseText.matches(".*(partial)\s*(credit).*")) {
} else if (matchesRegex(dropdownValues, lowerCaseText, ".*(partial)\s*(credit).*")) {
state.setPartialCredit(true);
}
}
......@@ -52,18 +53,22 @@ public class QuestionHandler {
}
}
private void addQuestionType(String text) {
private void addQuestionType(String text, List<String> dropdownValues) {
var lowerCaseText = text.toLowerCase();
if (lowerCaseText.matches(".*(single)\s*(choice).*")) {
if (matchesRegex(dropdownValues, lowerCaseText, ".*(single)\s*(choice).*")) {
state.setType(QuestionType.SINGLE_CHOICE);
} else if (lowerCaseText.matches(".*(multiple)\s*(choice).*") || lowerCaseText.contains("checkbox")) {
} else if (matchesRegex(dropdownValues, lowerCaseText, ".*(multiple)\s*(choice).*|.*(checkbox).*")) {
state.setType(QuestionType.MULTIPLE_CHOICE);
} else if (lowerCaseText.matches(".*(text)\s*(match).*") || lowerCaseText.matches(".*(short)\s*(answer).*")) {
} else if (matchesRegex(dropdownValues, lowerCaseText, ".*(text)\s*(match).*|.*(short)\s*(answer).*")) {
state.setType(QuestionType.TEXT_MATCH);
} else if (lowerCaseText.matches(".*(regular)\s*(expression).*")) {
} else if (matchesRegex(dropdownValues, lowerCaseText, ".*(regular)\s*(expression).*")) {
state.setType(QuestionType.REGULAR_EXPRESSION);
} else {
state.setType(QuestionType.UNKNOWN);
}
}
private boolean matchesRegex(List<String> dropdownValues, String lowerCaseText, String regex) {
return lowerCaseText.matches(regex) || dropdownValues.stream().anyMatch(s -> s.matches(regex));
}
}
......@@ -18,17 +18,17 @@ public class QuestionValidationHandler {
}
public void validateQuestion() {
if (state.getName().isEmpty()) {
if (state.getAnswerOptions().isEmpty()) {
state.getErrors().add(QuestionErrorType.NO_ANSWER_OPTIONS_FOUND);
}
var correctAnswerCount = state.getAnswerOptions().stream().filter(Answer::isCorrect).count();
if (state.getType().equals(QuestionType.SINGLE_CHOICE) && correctAnswerCount > 1) {
state.getErrors().add(QuestionErrorType.ANSWER_OPTIONS_DONT_MATCH_TYPE);
}
else if (state.getType().equals(QuestionType.UNKNOWN)) {
if (state.getType().equals(QuestionType.UNKNOWN)) {
state.getErrors().add(QuestionErrorType.UNKNOWN_QUESTION_TYPE);
}
else if (state.getType().equals(QuestionType.REGULAR_EXPRESSION)) {
if (state.getType().equals(QuestionType.REGULAR_EXPRESSION)) {
for (var answerOption: state.getAnswerOptions()) {
try {
Pattern.compile(answerOption.getText());
......@@ -38,7 +38,7 @@ public class QuestionValidationHandler {
}
state.getWarnings().add(QuestionWarningType.REGEX_RESTRICTION);
}
else if (correctAnswerCount == 0) {
if (correctAnswerCount == 0) {
state.getErrors().add(QuestionErrorType.NO_CORRECT_ANSWER_FOUND);
}
}
......
......@@ -8,6 +8,7 @@ import com.quiz.converter.models.enums.ParagraphType;
import com.quiz.converter.models.enums.QuestionWarningType;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFSDT;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
......@@ -15,6 +16,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Objects;
@Service
public class FileUploadService {
......@@ -36,13 +38,14 @@ public class FileUploadService {
private void handleParagraph(QuestionState state, ArrayList<Question> questions, XWPFParagraph paragraph) {
var text = paragraph.getParagraphText();
var paragraphPictures = handleParagraphPictures(paragraph);
var dropdownValues = handleDropdownLists(paragraph);
var paragraphType = getParagraphType(text, state);
switch (paragraphType) {
case EMPTY_TEXT -> handleEmptyTextParagraph(paragraphPictures, state);
case QUESTION_DETAILS -> {
var questionHandler = new QuestionHandler(state);
questionHandler.handleQuestion(text).ifPresent(questions::add);
questionHandler.handleQuestion(text, dropdownValues).ifPresent(questions::add);
state.setPreviousParagraphType(paragraphType);
}
case QUESTION_DESCRIPTION -> {
......@@ -64,6 +67,13 @@ public class FileUploadService {
}
}
private List<String> handleDropdownLists(XWPFParagraph paragraph) {
return paragraph.getIRuns().stream()
.filter(r -> r instanceof XWPFSDT)
.map(r -> ((XWPFSDT) r).getContent().getText())
.toList();
}
private static void handleEmptyTextParagraph(List<Picture> paragraphPictures, QuestionState state) {
if (!paragraphPictures.isEmpty()) {
if (state.getPreviousParagraphType().equals(ParagraphType.ANSWER_OPTION)) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment