Skip to content

Commit

Permalink
Merge pull request #72 from educ-ai-org/feat/return-score-classworks-…
Browse files Browse the repository at this point in the history
…student

Feat/return score classworks student
  • Loading branch information
luuh-oliveira authored Jun 11, 2024
2 parents 031f462 + b544862 commit 29b34c3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public ResponseEntity<List<UserDTO>> getClassroomParticipants(@PathVariable Obje
return status(200).body(classroomService.getClassroomParticipants(id));
}

@Operation(summary = "Retorna atividades de uma sala de aula")
@Operation(summary = "Retorna atividades de uma sala de aula para professor")
@Secured("ROLE_TEACHER")
@GetMapping("/{id}/classworks")
public ResponseEntity<List<ClassworkDTO>> getClassworksByClassroom(@PathVariable ObjectId id) {
List<ClassworkDTO> classworks = classroomService.getClassworks(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class ClassworkUserDTO {
@NotNull
private Integer totalQuestions;
private boolean hasAnswered;
private Double correctPercentage;

public ClassworkUserDTO(Classwork classwork) {
this.id = String.valueOf(classwork.getId());
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/api/educai/services/ClassroomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,16 @@ public List<ClassworkUserDTO> getClassworksByUser(ObjectId id, ObjectId userId)
User user = userService.getUserById(userId);
return classroom.getClassworks().stream().map(classwork -> {
ClassworkUserDTO classworkUserDTO = new ClassworkUserDTO(classwork);
classworkUserDTO.setHasAnswered(classwork.getAnswers().stream()
.anyMatch(answer -> answer.getUser().equals(user)));
Answer answer = classwork.getAnswers().stream()
.filter(a -> a.getUser().equals(user))
.findFirst()
.orElse(null);
if (answer != null) {
classworkUserDTO.setHasAnswered(true);
classworkUserDTO.setCorrectPercentage(answer.getCorrectPercentage());
} else {
classworkUserDTO.setHasAnswered(false);
}
return classworkUserDTO;
}).toList();
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/api/educai/services/ClassworkService.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void addAnswer(Answer answer, ObjectId userId, ObjectId classworkId) {

answer.setUser(userService.getUserById(userId));
answer.setClasswork(classwork);
answer.setCorrectPercentage(getAnswerScore(answer) * 10.0);
answer.setCorrectPercentage(getAnswerScore(answer));
answerRepository.save(answer);

classwork.addAnswer(answer);
Expand Down Expand Up @@ -117,7 +117,7 @@ public List<AnswerDTO> getAnswers(ObjectId classworkId) {
return classwork.getAnswers().stream().map(AnswerDTO::new).toList();
}

public int getAnswerScore(Answer answer) {
public double getAnswerScore(Answer answer) {

List<Question> questions = answer.getClasswork().getQuestions();
List<QuestionAnswerDTO> answers = answer.getQuestionAnswers();
Expand All @@ -133,7 +133,7 @@ public int getAnswerScore(Answer answer) {
int score = userCorrectAnswers.size() * CLASSWORK_SCORE / questions.size();
userService.updateScore(score, answer.getUser());

return score;
return (int) ((userCorrectAnswers.size() * 100.0) / questions.size());

}

Expand Down

0 comments on commit 29b34c3

Please sign in to comment.