From d93fc9b139ee65836ab47376237accf9de439372 Mon Sep 17 00:00:00 2001 From: Alex Prudencio Date: Wed, 7 Aug 2024 07:39:48 -0400 Subject: [PATCH] changed assessment time limit --- .../model/Assessment.java | 11 ++++++-- .../primefactorsolutions/model/Question.java | 1 + .../views/EvaluationView.java | 27 ++++++++++++++----- .../views/QuestionView.java | 7 ++++- 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/primefactorsolutions/model/Assessment.java b/src/main/java/com/primefactorsolutions/model/Assessment.java index e120caa..c33a415 100644 --- a/src/main/java/com/primefactorsolutions/model/Assessment.java +++ b/src/main/java/com/primefactorsolutions/model/Assessment.java @@ -8,6 +8,7 @@ import lombok.NoArgsConstructor; import java.time.Instant; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.Optional; @Entity @@ -38,8 +39,14 @@ public class Assessment extends BaseEntity { .map(AssessmentEvent::getTimestamp) .findFirst(); - return started.map(instant -> 300L - (Instant.now().getEpochSecond() - instant.getEpochSecond())) - .orElse(300L); + final Integer totalTimeMinutes = questions.stream() + .map(Question::getTimeMinutes) + .filter(Objects::nonNull) + .reduce(Integer::sum).orElse(30); + final long totalTimeSeconds = totalTimeMinutes * 60; + + return started.map(instant -> totalTimeSeconds - (Instant.now().getEpochSecond() - instant.getEpochSecond())) + .orElse(totalTimeSeconds); } public Instant getStartingTime() { diff --git a/src/main/java/com/primefactorsolutions/model/Question.java b/src/main/java/com/primefactorsolutions/model/Question.java index 35481f3..4523e3c 100644 --- a/src/main/java/com/primefactorsolutions/model/Question.java +++ b/src/main/java/com/primefactorsolutions/model/Question.java @@ -18,4 +18,5 @@ public class Question extends BaseEntity{ private String description; @Lob private String content; + private Integer timeMinutes; } diff --git a/src/main/java/com/primefactorsolutions/views/EvaluationView.java b/src/main/java/com/primefactorsolutions/views/EvaluationView.java index ee9e516..401d9dc 100644 --- a/src/main/java/com/primefactorsolutions/views/EvaluationView.java +++ b/src/main/java/com/primefactorsolutions/views/EvaluationView.java @@ -40,6 +40,7 @@ import com.vaadin.flow.theme.lumo.LumoUtility.Margin; import com.vaadin.flow.theme.lumo.LumoUtility.Overflow; import com.vaadin.flow.theme.lumo.LumoUtility.Padding; import com.vaadin.flow.theme.lumo.LumoUtility.TextColor; +import lombok.extern.slf4j.Slf4j; import org.jetbrains.annotations.NotNull; import org.springframework.context.annotation.Scope; @@ -55,6 +56,7 @@ import java.util.stream.Collectors; @Scope("prototype") @Route(value = "/evaluation", layout = MainLayout.class) @AnonymousAllowed +@Slf4j public class EvaluationView extends Main implements HasUrlParameter { final CompilerService compilerService; @@ -82,6 +84,8 @@ public class EvaluationView extends Main implements HasUrlParameter { Section startSection = null; Section completedSection = null; + private H3 questionTitle = null; + private Text questionDescription = null; public EvaluationView(final CompilerService compilerService, final AssessmentService assessmentService) { this.compilerService = compilerService; @@ -170,6 +174,14 @@ public class EvaluationView extends Main implements HasUrlParameter { private void initEditorSection() { editorSection = new Section(); editorSection.addClassNames(Display.FLEX, FlexDirection.COLUMN, Flex.GROW, Height.FULL); + + VerticalLayout header = new VerticalLayout(); + + questionTitle = new H3(""); + questionDescription = new Text(""); + header.add(questionTitle); + header.add(questionDescription); + questionEditor = getCodeEditor(); final MenuBar runMenuBar = new MenuBar(); @@ -186,7 +198,7 @@ public class EvaluationView extends Main implements HasUrlParameter { final MenuBar navMenuBar = new MenuBar(); prev = navMenuBar.addItem("Anterior pregunta", (ComponentEventListener>) menuItemClickEvent -> { - System.out.println(">>> prev"); + log.info(">>> prev"); this.currSubmission.setResponse(this.questionEditor.getValue()); this.assessmentService.saveSubmission(assessment.getId(), this.currSubmission); this.currSubmission = this.assessmentService.getPrevSubmission(assessment.getId(), this.currSubmission); @@ -208,7 +220,7 @@ public class EvaluationView extends Main implements HasUrlParameter { setInlineBlock(runMenuBar); setInlineBlock(navMenuBar); - editorSection.add(menuBar, questionEditor); + editorSection.add(header, menuBar, questionEditor); } private void initCompletedSection() { @@ -239,7 +251,7 @@ public class EvaluationView extends Main implements HasUrlParameter { start = new Button("Empezar"); start.addThemeVariants(ButtonVariant.LUMO_PRIMARY, ButtonVariant.LUMO_SUCCESS); start.addClickListener((ComponentEventListener>) buttonClickEvent -> { - System.out.println(">>> start"); + log.info(">>> start"); this.assessment = this.assessmentService.startAssessment(this.assessment.getId()); if (tf.getValue().trim().equalsIgnoreCase(this.assessment.getAppUser().getEmail())) { @@ -304,7 +316,7 @@ public class EvaluationView extends Main implements HasUrlParameter { } private void goToNext() { - System.out.println(">>> next"); + log.info(">>> next"); Submission found = this.assessmentService.getNextSubmission(assessment.getId(), this.currSubmission.getId()); if (found == null) { @@ -330,7 +342,7 @@ public class EvaluationView extends Main implements HasUrlParameter { timer = new SimpleTimer(0); timer.setMinutes(true); timer.addTimerEndEvent((ComponentEventListener) timerEndedEvent -> { - Notification.show("Tiempo completado.", 5, Notification.Position.TOP_CENTER); + Notification.show("Tiempo completado.", 5_000, Notification.Position.TOP_CENTER); this.currSubmission.setResponse(this.questionEditor.getValue()); this.assessmentService.saveSubmission(assessment.getId(), this.currSubmission); this.assessment = assessmentService.completeAssessment(assessment.getId()); @@ -357,6 +369,8 @@ public class EvaluationView extends Main implements HasUrlParameter { } else { if (currSubmission != null) { questionEditor.setValue(this.currSubmission.getResponse()); + questionTitle.setText(this.currSubmission.getQuestion().getTitle()); + questionDescription.setText(this.currSubmission.getQuestion().getDescription()); } editorSection.setVisible(true); @@ -383,8 +397,9 @@ public class EvaluationView extends Main implements HasUrlParameter { ); } + final Long remainingTime = this.assessment.getRemainingTimeSeconds(); timer.pause(); - timer.setStartTime(this.assessment.getRemainingTimeSeconds()); + timer.setStartTime(remainingTime > 0 ? remainingTime : 3); timer.setMinutes(true); timer.reset(); timer.start(); diff --git a/src/main/java/com/primefactorsolutions/views/QuestionView.java b/src/main/java/com/primefactorsolutions/views/QuestionView.java index 31aab82..4f085d6 100644 --- a/src/main/java/com/primefactorsolutions/views/QuestionView.java +++ b/src/main/java/com/primefactorsolutions/views/QuestionView.java @@ -3,6 +3,7 @@ package com.primefactorsolutions.views; import com.primefactorsolutions.model.Question; import com.primefactorsolutions.service.QuestionService; import com.vaadin.flow.component.Component; +import com.vaadin.flow.component.textfield.IntegerField; import com.vaadin.flow.component.textfield.TextArea; import com.vaadin.flow.component.textfield.TextField; import com.vaadin.flow.router.BeforeEvent; @@ -29,6 +30,7 @@ public class QuestionView extends BeanValidationForm implements HasUrl private TextField title = null; private TextArea description = null; private TextArea content = null; + private IntegerField timeMinutes = null; public QuestionView(final QuestionService questionService) { super(Question.class); @@ -42,6 +44,9 @@ public class QuestionView extends BeanValidationForm implements HasUrl content = new TextArea(); content.setLabel("Content"); + timeMinutes = new IntegerField(); + timeMinutes.setLabel("TimeMinutes"); + setSavedHandler((SavedHandler) questionService::createOrUpdate); } @@ -58,6 +63,6 @@ public class QuestionView extends BeanValidationForm implements HasUrl @Override protected List getFormComponents() { - return List.of(title, description, content); + return List.of(title, description, timeMinutes, content); } }