Browse Source

improvement: 增加QuizStudentAnswerVO每道题答案是否正确

ChenSiTong 6 năm trước cách đây
mục cha
commit
3724573c49

+ 9 - 1
src/main/java/nju/seec/helper/controller/QuestionController.java

@@ -2,12 +2,14 @@ package nju.seec.helper.controller;
 
 import nju.seec.helper.aspect.auth.Auth;
 import nju.seec.helper.controller.response.PageResponse;
+import nju.seec.helper.dto.question.BaseQuestionDTO;
 import nju.seec.helper.dto.user.LoginUser;
 import nju.seec.helper.service.QuestionService;
 import nju.seec.helper.util.enums.UserType;
 import nju.seec.helper.vo.question.BaseQuestionVO;
 import org.springframework.data.domain.Pageable;
 import org.springframework.data.web.PageableDefault;
+import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 
 import java.util.List;
@@ -43,10 +45,16 @@ public class QuestionController {
      */
     @Auth(roles = {UserType.TEACHER, UserType.STUDENT}, message = "获得测试题目")
     @GetMapping("/quiz/{quizId}")
-    public List<BaseQuestionVO> getQuestionsOfQuiz(LoginUser user, @PathVariable Long quizId) {
+    public List<BaseQuestionVO> getQuestionsByQuiz(LoginUser user, @PathVariable Long quizId) {
         return questionService.getQuestionsByQuiz(user, quizId);
     }
 
+//    @Auth(roles = UserType.TEACHER, message = "创建题目")
+//    @PostMapping
+//    public BaseQuestionVO createQuestion(LoginUser user, @Validated @RequestBody BaseQuestionDTO baseQuestionDTO) {
+//        return questionService.createQuestion(user, baseQuestionDTO);
+//    }
+
 //    /**
 //     * 获得题目列表
 //     */

+ 10 - 0
src/main/java/nju/seec/helper/dao/BaseQuestionDAO.java

@@ -0,0 +1,10 @@
+//package nju.seec.helper.dao;
+//
+//import nju.seec.helper.entity.question.BaseQuestion;
+//import org.springframework.data.jpa.repository.JpaRepository;
+//
+///**
+// * @author cst
+// */
+//public interface BaseQuestionDAO extends JpaRepository<BaseQuestion, Long> {
+//}

+ 21 - 9
src/main/java/nju/seec/helper/dto/question/BaseQuestionDTO.java

@@ -1,26 +1,38 @@
 package nju.seec.helper.dto.question;
 
+import com.fasterxml.jackson.annotation.JsonSubTypes;
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
 import lombok.Data;
-import nju.seec.helper.util.enums.QuestionType;
+import nju.seec.helper.dto.groups.Create;
 import org.hibernate.validator.constraints.Length;
 
 import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.NotNull;
 
 /**
  * @author cst
  */
 @Data
+@JsonTypeInfo(
+        use = JsonTypeInfo.Id.NAME,
+        include = JsonTypeInfo.As.EXISTING_PROPERTY,
+        property = "type",
+        visible = true
+)
+@JsonSubTypes({
+        @JsonSubTypes.Type(value = ChoiceQuestionDTO.class, name = BaseQuestionDTO.CHOICE),
+        @JsonSubTypes.Type(value = TrueOrFalseQuestionDTO.class, name = BaseQuestionDTO.TRUE_FALSE)
+})
 public abstract class BaseQuestionDTO {
+    protected static final String CHOICE = "CHOICE";
+    protected static final String TRUE_FALSE = "TRUE_FALSE";
+
     @NotBlank(message = "题干不能为空")
     @Length(max = 1000, message = "题干长度不能超过1000")
-    private String stem;
+    protected String stem;
     @Length(max = 1000, message = "解析长度不能超过1000")
-    private String analysis;
+    protected String analysis;
+    @NotNull(message = "缺少题目类型", groups = Create.class)
+    protected String type;
 
-    /**
-     * 获取题目类型
-     *
-     * @return
-     */
-    public abstract QuestionType getQuestionType();
 }

+ 4 - 4
src/main/java/nju/seec/helper/dto/question/ChoiceQuestionDTO.java

@@ -2,7 +2,7 @@ package nju.seec.helper.dto.question;
 
 import lombok.Data;
 import lombok.EqualsAndHashCode;
-import nju.seec.helper.util.enums.QuestionType;
+import lombok.ToString;
 import org.hibernate.validator.constraints.Length;
 
 import javax.validation.constraints.NotBlank;
@@ -15,6 +15,7 @@ import java.util.Map;
  * @author cst
  */
 @EqualsAndHashCode(callSuper = true)
+@ToString(callSuper = true)
 @Data
 public class ChoiceQuestionDTO extends BaseQuestionDTO implements Serializable {
     private static final long serialVersionUID = -2991414748803093291L;
@@ -26,8 +27,7 @@ public class ChoiceQuestionDTO extends BaseQuestionDTO implements Serializable {
     @Length(max = 10, message = "答案长度不能超过10")
     private String answer;
 
-    @Override
-    public QuestionType getQuestionType() {
-        return QuestionType.CHOICE;
+    public ChoiceQuestionDTO() {
+        this.type = BaseQuestionDTO.CHOICE;
     }
 }

+ 21 - 21
src/main/java/nju/seec/helper/dto/question/SubjectiveQuestionDTO.java

@@ -1,21 +1,21 @@
-package nju.seec.helper.dto.question;
-
-import lombok.Data;
-import lombok.EqualsAndHashCode;
-import nju.seec.helper.util.enums.QuestionType;
-
-import java.io.Serializable;
-
-/**
- * @author cst
- */
-@EqualsAndHashCode(callSuper = true)
-@Data
-public class SubjectiveQuestionDTO extends BaseQuestionDTO implements Serializable {
-    private static final long serialVersionUID = -4391297345486351815L;
-
-    @Override
-    public QuestionType getQuestionType() {
-        return QuestionType.SUBJECTIVE;
-    }
-}
+//package nju.seec.helper.dto.question;
+//
+//import lombok.Data;
+//import lombok.EqualsAndHashCode;
+//import nju.seec.helper.util.enums.QuestionType;
+//
+//import java.io.Serializable;
+//
+///**
+// * @author cst
+// */
+//@EqualsAndHashCode(callSuper = true)
+//@Data
+//public class SubjectiveQuestionDTO extends BaseQuestionDTO implements Serializable {
+//    private static final long serialVersionUID = -4391297345486351815L;
+//
+//    @Override
+//    public QuestionType getQuestionType() {
+//        return QuestionType.SUBJECTIVE;
+//    }
+//}

+ 4 - 4
src/main/java/nju/seec/helper/dto/question/TrueOrFalseQuestionDTO.java

@@ -2,7 +2,7 @@ package nju.seec.helper.dto.question;
 
 import lombok.Data;
 import lombok.EqualsAndHashCode;
-import nju.seec.helper.util.enums.QuestionType;
+import lombok.ToString;
 
 import javax.validation.constraints.NotNull;
 import java.io.Serializable;
@@ -11,14 +11,14 @@ import java.io.Serializable;
  * @author cst
  */
 @EqualsAndHashCode(callSuper = true)
+@ToString(callSuper = true)
 @Data
 public class TrueOrFalseQuestionDTO extends BaseQuestionDTO implements Serializable {
     private static final long serialVersionUID = -2256887591745338790L;
     @NotNull(message = "缺少答案")
     private Boolean answer;
 
-    @Override
-    public QuestionType getQuestionType() {
-        return QuestionType.TRUE_FALSE;
+    public TrueOrFalseQuestionDTO() {
+        this.type = BaseQuestionDTO.TRUE_FALSE;
     }
 }

+ 34 - 0
src/main/java/nju/seec/helper/entity/question/BaseQuestion.java

@@ -0,0 +1,34 @@
+//package nju.seec.helper.entity.question;
+//
+//import lombok.Data;
+//import lombok.experimental.Accessors;
+//import nju.seec.helper.entity.User;
+//import nju.seec.helper.util.enums.QuestionType;
+//
+//import javax.persistence.*;
+//
+///**
+// * @author cst
+// */
+//@Data
+//@Accessors(chain = true)
+//@MappedSuperclass
+//public abstract class BaseQuestion {
+//    @Id
+//    @GeneratedValue(strategy = GenerationType.IDENTITY)
+//    protected Long id;
+//
+//    @Column(nullable = false, columnDefinition = "text not null")
+//    protected String stem;
+//
+//    @Column(nullable = false, columnDefinition = "text not null")
+//    protected String analysis;
+//
+//    @Enumerated(EnumType.STRING)
+//    @Column(nullable = false)
+//    protected QuestionType type;
+//
+//    @ManyToOne(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.LAZY)
+//    @JoinColumn(name = "teacher_id", nullable = false, foreignKey = @ForeignKey(name = "question_teacher"))
+//    protected User teacher;
+//}

+ 32 - 0
src/main/java/nju/seec/helper/entity/question/ChoiceQuestion.java

@@ -0,0 +1,32 @@
+//package nju.seec.helper.entity.question;
+//
+//import lombok.Data;
+//import lombok.EqualsAndHashCode;
+//import lombok.experimental.Accessors;
+//import nju.seec.helper.entity.converter.MapConverter;
+//import nju.seec.helper.util.enums.QuestionType;
+//
+//import javax.persistence.Convert;
+//import javax.persistence.Entity;
+//import javax.persistence.Table;
+//import java.util.Map;
+//
+///**
+// * @author cst
+// */
+//@EqualsAndHashCode(callSuper = true)
+//@Data
+//@Accessors(chain = true)
+//@Entity
+//@Table(name = "question_choice")
+//public class ChoiceQuestion extends BaseQuestion {
+//    @SuppressWarnings("all")
+//    @Convert(converter = MapConverter.class)
+//    private Map<String, String> options;
+//
+//    private String answer;
+//
+//    public ChoiceQuestion() {
+//        this.type = QuestionType.CHOICE;
+//    }
+//}

+ 25 - 0
src/main/java/nju/seec/helper/entity/question/TrueOrFalseQuestion.java

@@ -0,0 +1,25 @@
+//package nju.seec.helper.entity.question;
+//
+//import lombok.Data;
+//import lombok.EqualsAndHashCode;
+//import lombok.experimental.Accessors;
+//import nju.seec.helper.util.enums.QuestionType;
+//
+//import javax.persistence.Entity;
+//import javax.persistence.Table;
+//
+///**
+// * @author cst
+// */
+//@EqualsAndHashCode(callSuper = true)
+//@Data
+//@Accessors(chain = true)
+//@Entity
+//@Table(name = "question_true_or_false")
+//public class TrueOrFalseQuestion extends BaseQuestion {
+//    private Boolean answer;
+//
+//    public TrueOrFalseQuestion() {
+//        this.type = QuestionType.TRUE_FALSE;
+//    }
+//}

+ 5 - 6
src/main/java/nju/seec/helper/service/QuestionService.java

@@ -1,5 +1,6 @@
 package nju.seec.helper.service;
 
+import nju.seec.helper.dto.question.BaseQuestionDTO;
 import nju.seec.helper.dto.user.LoginUser;
 import nju.seec.helper.entity.Quiz;
 import nju.seec.helper.vo.question.BaseQuestionVO;
@@ -50,12 +51,10 @@ public interface QuestionService {
     List<BaseQuestionVO> getQuestionsByQuiz(Quiz quiz);
 
 //    /**
-//     * @param newQuestion
+//     * 创建题目
+//     * @param user
+//     * @param baseQuestionDTO
 //     * @return
 //     */
-//    BaseQuestionVO createQuestion(BaseQuestionVO newQuestion);
-//
-//    BaseQuestionVO updateQuestion(BaseQuestionVO newQuestion);
-//
-//    void deleteQuestion(String questionId);
+//    BaseQuestionVO createQuestion(LoginUser user, BaseQuestionDTO baseQuestionDTO);
 }

+ 34 - 20
src/main/java/nju/seec/helper/service/impl/QuestionServiceImpl.java

@@ -1,6 +1,7 @@
 package nju.seec.helper.service.impl;
 
 import nju.seec.helper.dao.QuizDAO;
+import nju.seec.helper.dao.UserDAO;
 import nju.seec.helper.dto.user.LoginUser;
 import nju.seec.helper.entity.Quiz;
 import nju.seec.helper.service.QuestionService;
@@ -24,11 +25,19 @@ import java.util.List;
 @Service
 public class QuestionServiceImpl implements QuestionService {
     private final QuizDAO quizDAO;
+    private final UserDAO userDAO;
+//    private final BaseQuestionDAO baseQuestionDAO;
+
     private final BokUtil bokUtil;
 
     @Autowired
-    public QuestionServiceImpl(QuizDAO quizDAO, BokUtil bokUtil) {
+    public QuestionServiceImpl(QuizDAO quizDAO
+            , UserDAO userDAO
+//            , BaseQuestionDAO baseQuestionDAO
+            , BokUtil bokUtil) {
         this.quizDAO = quizDAO;
+        this.userDAO = userDAO;
+//        this.baseQuestionDAO = baseQuestionDAO;
         this.bokUtil = bokUtil;
     }
 
@@ -57,27 +66,32 @@ public class QuestionServiceImpl implements QuestionService {
         return bokUtil.bokFindByIdIn(questionIds, true);
     }
 
-    //    @Override
-//    public BaseQuestionVO newQuestion(BaseQuestionVO newQuestion) {
-//        try {
-//            return bokUtil.postNewQuestion(newQuestion);
-//        } catch (JsonProcessingException e) {
-//            throw HelperException.of(ExceptionType.ERROR, "请求BOK题库数据错误:" + e.getMessage());
-//        }
-//    }
-//
+//    @Transactional(rollbackFor = Exception.class)
 //    @Override
-//    public BaseQuestionVO updateQuestion(BaseQuestionVO newQuestion) {
-//        try {
-//            return bokUtil.putQuestion(newQuestion);
-//        } catch (JsonProcessingException e) {
-//            throw HelperException.of(ExceptionType.ERROR, "请求BOK题库数据错误:" + e.getMessage());
-//        }
+//    public BaseQuestionVO createQuestion(LoginUser user, BaseQuestionDTO baseQuestionDTO) {
+//        BaseQuestion baseQuestion = getQuestion(baseQuestionDTO);
+//        baseQuestion.setTeacher(userDAO.findUserById(user.getId()));
+//        baseQuestion = baseQuestionDAO.save(baseQuestion);
+//        return BaseQuestionVO.convertBaseQuestionToVO(baseQuestion, true);
 //    }
 //
-//    @Override
-//    public void deleteQuestion(String questionId) {
-//        bokUtil.deleteQuestion(questionId);
+//    private BaseQuestion getQuestion(BaseQuestionDTO baseQuestionDTO) {
+//        switch (QuestionType.valueOf(baseQuestionDTO.getType())) {
+//            case CHOICE:
+//                ChoiceQuestion choiceQuestion = new ChoiceQuestion();
+//                choiceQuestion.setStem(baseQuestionDTO.getStem());
+//                choiceQuestion.setAnalysis(baseQuestionDTO.getAnalysis());
+//                choiceQuestion.setOptions(((ChoiceQuestionDTO) baseQuestionDTO).getOptions());
+//                choiceQuestion.setAnswer(((ChoiceQuestionDTO) baseQuestionDTO).getAnswer());
+//                return choiceQuestion;
+//            case TRUE_FALSE:
+//                TrueOrFalseQuestion trueOrFalseQuestion = new TrueOrFalseQuestion();
+//                trueOrFalseQuestion.setStem(baseQuestionDTO.getStem());
+//                trueOrFalseQuestion.setAnalysis(baseQuestionDTO.getAnalysis());
+//                trueOrFalseQuestion.setAnswer(((TrueOrFalseQuestionDTO) baseQuestionDTO).getAnswer());
+//                return trueOrFalseQuestion;
+//            default:
+//                throw HelperException.of(ExceptionType.FORBIDDEN, "暂时无法创建该类型的题目");
+//        }
 //    }
-
 }

+ 4 - 3
src/main/java/nju/seec/helper/service/impl/QuizStudentAnswerServiceImpl.java

@@ -88,13 +88,14 @@ public class QuizStudentAnswerServiceImpl implements QuizStudentAnswerService {
                 .setAnswers(answers)
                 .setScore(score);
 
-        return new QuizStudentAnswerVO(quizStudentAnswerDAO.save(quizStudentAnswer));
+        return new QuizStudentAnswerVO(quizStudentAnswerDAO.save(quizStudentAnswer), questionVOList);
     }
 
     @Transactional(readOnly = true)
     @Override
     public Page<QuizStudentAnswerVO> teacherGetQuizStudentAnswers(LoginUser user, Long quizId, Pageable pageable) {
-        return quizStudentAnswerDAO.findByQuiz(quizDAO.findQuizById(quizId), pageable).map(QuizStudentAnswerVO::new);
+        return quizStudentAnswerDAO.findByQuiz(quizDAO.findQuizById(quizId), pageable)
+                .map(quizStudentAnswer -> new QuizStudentAnswerVO(quizStudentAnswer, questionService.getQuestionsByQuiz(quizDAO.findQuizById(quizId))));
     }
 
     @Transactional(readOnly = true)
@@ -102,7 +103,7 @@ public class QuizStudentAnswerServiceImpl implements QuizStudentAnswerService {
     public QuizStudentAnswerVO studentGetSelfQuizStudentAnswer(LoginUser user, Long quizId) {
         return quizStudentAnswerDAO
                 .findByQuizAndStudent(quizDAO.findQuizById(quizId), userDAO.findUserById(user.getId()))
-                .map(QuizStudentAnswerVO::new)
+                .map(quizStudentAnswer -> new QuizStudentAnswerVO(quizStudentAnswer, questionService.getQuestionsByQuiz(quizDAO.findQuizById(quizId))))
                 .orElse(null);
     }
 }

+ 11 - 48
src/main/java/nju/seec/helper/vo/question/BaseQuestionVO.java

@@ -31,6 +31,17 @@ public abstract class BaseQuestionVO {
         }
     }
 
+//    public static BaseQuestionVO convertBaseQuestionToVO(@NonNull BaseQuestion baseQuestion, boolean withAnswer) {
+//        switch (baseQuestion.getType()) {
+//            case CHOICE:
+//                return new ChoiceQuestionVO((ChoiceQuestion) baseQuestion, withAnswer);
+//            case TRUE_FALSE:
+//                return new TrueOrFalseQuestionVO((TrueOrFalseQuestion) baseQuestion, withAnswer);
+//            default:
+//                throw HelperException.of(ExceptionType.ERROR,"暂不支持该类型的题目");
+//        }
+//    }
+
     /**
      * 检查答案是否正确
      *
@@ -39,51 +50,3 @@ public abstract class BaseQuestionVO {
      */
     public abstract boolean pass(Object answer);
 }
-//QuestionSerializer
-//问题的详细定义
-//
-//Attribute
-//type QuestionSerializer =
-//  | ChoiceQuestionSerializer
-//  | TrueOrFalseQuestionSerializer;
-//
-//type QuestionWithoutAnswerSerializer = Omit<
-//  QuestionSerializer,
-//  "answer" | "analysis"
-//>;
-//
-//type QuestionWithStudentAnswerSerializer =
-//  | ChoiceQuestionWithStudentAnswer
-//  | TrueOrFalseQuestionWithStudentAnswer;
-//
-//interface Question {
-//  id: number; // 题目id
-//  kind: "CHOICE" | "TRUE_FALSE";
-//  stem: string; // 题干
-//}
-//
-//interface ChoiceQuestionSerializer extends Question {
-//  kind: "CHOICE";
-//  options: {
-//    [key: string]: string;
-//  };
-//  answer: string; // 答案 [与 options 的 key 一致]
-//  analysis?: string; // 解析
-//}
-//
-//interface ChoiceQuestionWithStudentAnswer extends ChoiceQuestionSerializer {
-//  studentAnswer: string;
-//  pass: boolean; // 是否正确
-//}
-//
-//interface TrueOrFalseQuestionSerializer extends Question {
-//  kind: "TRUE_FALSE";
-//  answer: boolean; // 答案
-//  analysis?: string; // 解析
-//}
-//
-//interface TrueOrFalseQuestionWithStudentAnswer
-//  extends TrueOrFalseQuestionSerializer {
-//  studentAnswer: boolean;
-//  pass: boolean; // 是否正确
-//}

+ 12 - 0
src/main/java/nju/seec/helper/vo/question/ChoiceQuestionVO.java

@@ -30,6 +30,18 @@ public class ChoiceQuestionVO extends BaseQuestionVO {
         }
     }
 
+//    public ChoiceQuestionVO(ChoiceQuestion baseQuestion, boolean withAnswer) {
+//        this.id = String.valueOf(baseQuestion.getId());
+//        this.type = QuestionType.CHOICE;
+//        this.stem = baseQuestion.getStem();
+//        this.options = baseQuestion.getOptions();
+//
+//        if (withAnswer) {
+//            this.answer = baseQuestion.getAnswer();
+//            this.analysis = baseQuestion.getAnalysis();
+//        }
+//    }
+
     @Override
     public boolean pass(Object answer) {
         return this.answer.equals(String.valueOf(answer));

+ 11 - 0
src/main/java/nju/seec/helper/vo/question/TrueOrFalseQuestionVO.java

@@ -16,6 +16,17 @@ import nju.seec.helper.util.enums.QuestionType;
 public class TrueOrFalseQuestionVO extends BaseQuestionVO {
     private Boolean answer;
 
+//    public TrueOrFalseQuestionVO(TrueOrFalseQuestion baseQuestion, boolean withAnswer) {
+//        this.id = String.valueOf(baseQuestion.getId());
+//        this.type = QuestionType.TRUE_FALSE;
+//        this.stem = baseQuestion.getStem();
+//
+//        if (withAnswer) {
+//            this.answer = baseQuestion.getAnswer();
+//            this.analysis = baseQuestion.getAnalysis();
+//        }
+//    }
+
     @Override
     public boolean pass(Object answer) {
         return this.answer.equals(Boolean.valueOf(String.valueOf(answer)));

+ 24 - 3
src/main/java/nju/seec/helper/vo/quiz/QuizStudentAnswerVO.java

@@ -1,12 +1,16 @@
 package nju.seec.helper.vo.quiz;
 
+import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.NonNull;
 import nju.seec.helper.entity.QuizStudentAnswer;
+import nju.seec.helper.vo.question.BaseQuestionVO;
 
 import java.math.BigDecimal;
 import java.time.LocalDateTime;
+import java.util.List;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 /**
  * @author cst
@@ -21,7 +25,7 @@ public class QuizStudentAnswerVO {
     /**
      * 回答情况
      */
-    private Map<String, Object> answers;
+    private Map<String, QuestionStudentAnswerInfo> answers;
 
     /**
      * 提交时间
@@ -36,14 +40,31 @@ public class QuizStudentAnswerVO {
      */
     private BigDecimal score;
 
-    public QuizStudentAnswerVO(@NonNull QuizStudentAnswer quizStudentAnswer) {
+    public QuizStudentAnswerVO(@NonNull QuizStudentAnswer quizStudentAnswer, List<BaseQuestionVO> baseQuestionVOList) {
         this.quizId = quizStudentAnswer.getQuiz().getId();
         this.quizName = quizStudentAnswer.getQuiz().getName();
         this.studentId = quizStudentAnswer.getStudent().getId();
         this.studentName = quizStudentAnswer.getStudent().getName();
-        this.answers = quizStudentAnswer.getAnswers();
+
+        Map<String, Object> answers = quizStudentAnswer.getAnswers();
+        this.answers = baseQuestionVOList.parallelStream()
+                .map(baseQuestionVO -> {
+                    String questionId = baseQuestionVO.getId();
+                    Object questionStudentAnswer = answers.get(questionId);
+                    return QuestionStudentAnswerInfo.of(questionId, questionStudentAnswer, baseQuestionVO.pass(questionStudentAnswer));
+                })
+                .collect(Collectors.toMap(QuestionStudentAnswerInfo::getQuestionId, questionStudentAnswerInfo -> questionStudentAnswerInfo));
+
         this.submitAt = quizStudentAnswer.getSubmitAt();
         this.submitNumber = quizStudentAnswer.getSubmitNumber();
         this.score = quizStudentAnswer.getScore();
     }
+
+    @Data
+    @AllArgsConstructor(staticName = "of")
+    private static class QuestionStudentAnswerInfo {
+        private String questionId;
+        private Object answer;
+        private Boolean correct;
+    }
 }