Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ public String deleteQuestion(@PathVariable Integer id, Model model) {
* */
@GetMapping("/quiz")
public String showAnswer(Model model, Principal principal) {
String username = principal.getName();
questionsService.clearResults(username);
List<Question> questions = questionsService.loadQuizzes();
List<Question> shuffledQuestions = questions.stream()
.map(q -> {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/app/questionnaire/service/QuestionsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ public boolean validateAnswer(String username, Integer questionId, String select
return isCorrect;
}

/**
* Clears all quiz results for a user so they start fresh on their next attempt.
* Called when the user loads the quiz page (/quiz GET).
* @param username the user whose results should be cleared
*/
public void clearResults(String username) {
userAnswersMap.remove(username);
userResultsMap.remove(username);
}

/**
* Retrieves quiz results for a specific user.
* @param username the user
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/static/css/quizlist.css
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ tbody td {
}

.btn {
display: inline-block;
display: inline-flex;
align-items: center;
padding: 8px 18px;
font-size: 14px;
font-weight: 600;
Expand All @@ -73,6 +74,8 @@ tbody td {
cursor: pointer;
text-decoration: none;
transition: all 0.3s ease;
font-family: inherit;
line-height: 1.4;
}

.btn-primary {
Expand Down
10 changes: 6 additions & 4 deletions src/main/resources/templates/quizlist.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ <h1>List of questionnaires.</h1>
<td th:text="${question.options[3]}"></td>
<td th:text="${question.correctAnswer}"></td>
<td>
<a th:href="@{/quizlist/edit/{id}(id=${question.id})}" class="btn btn-edit">Edit</a>
<form th:action="@{/quizlist/delete/{id}(id=${question.id})}" method="post" style="display: inline;">
<button type="submit" class="btn btn-delete" onclick="return confirm('Are you sure you want to delete this questionnaire?')">Delete</button>
</form>
<div style="display: flex; gap: 6px;">
<a th:href="@{/quizlist/edit/{id}(id=${question.id})}" class="btn btn-edit">Edit</a>
<form th:action="@{/quizlist/delete/{id}(id=${question.id})}" method="post" style="display: inline;">
<button type="submit" class="btn btn-delete" onclick="return confirm('Are you sure you want to delete this questionnaire?')">Delete</button>
</form>
</div>
</td>
</tr>
</tbody>
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/templates/result.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ <h2>User: <span th:text="${username}"></span></h2>
</div>
<div class="button-group">
<a th:href="@{/quiz}" class="btn btn-primary">Retry Quiz</a>
<form th:action="@{/logout}" method="post" style="display: inline;">
<button type="submit" class="btn btn-logout">Log out</button>
</form>
</div>
<form th:action="@{/logout}" method="post" style="display: inline;">
<button type="submit" class="btn btn-logout">Log out</button>
</form>
</div>
</body>
</html>
Loading