|
|
@@ -1,7 +1,7 @@
|
|
|
package nju.seec.helper.service.impl;
|
|
|
|
|
|
import nju.seec.helper.dao.*;
|
|
|
-import nju.seec.helper.dto.LoginUser;
|
|
|
+import nju.seec.helper.dto.QuestionStudentAnswerDTO;
|
|
|
import nju.seec.helper.entity.*;
|
|
|
import nju.seec.helper.service.QuizService;
|
|
|
import nju.seec.helper.util.BOKUtil;
|
|
|
@@ -9,6 +9,7 @@ import nju.seec.helper.util.enums.QuizState;
|
|
|
import nju.seec.helper.util.enums.SlideState;
|
|
|
import nju.seec.helper.util.enums.UserType;
|
|
|
import nju.seec.helper.vo.quiz.QuestionVO;
|
|
|
+import nju.seec.helper.vo.quiz.QuizResultVO;
|
|
|
import nju.seec.helper.vo.quiz.QuizVO;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
@@ -111,6 +112,66 @@ public class QuizServiceImpl implements QuizService {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public List<QuizResultVO> getQuizResult(Long quizId) {
|
|
|
+ Assert.notNull(quizId,"QUIZ ID can't be null!");
|
|
|
+ final Quiz quiz = quizDAO.findQuizById(quizId);
|
|
|
+ if(quiz.getState()!=QuizState.CLOSED){throw new RuntimeException("quiz not close!");}
|
|
|
+ List<QuizResultVO> retList=new ArrayList<>();
|
|
|
+ final List<StudentScore> studentScoreList = quiz.getStudentScore();
|
|
|
+ Map<Integer,List<QuizStudentAnswer>> IdQuizStudentAnswerMap=new HashMap<>();
|
|
|
+ for(QuizStudentAnswer qsa:quizStudentAnswerDAO.findAllByQuiz(quiz)){
|
|
|
+ IdQuizStudentAnswerMap.computeIfAbsent(qsa.getStudent().getId(), k -> new ArrayList<>());
|
|
|
+ IdQuizStudentAnswerMap.get(qsa.getStudent().getId()).add(qsa);
|
|
|
+ }
|
|
|
+ for(StudentScore studentScore:studentScoreList){
|
|
|
+ final User student = studentScore.getStudent();
|
|
|
+ final List<QuizStudentAnswer> list = IdQuizStudentAnswerMap.get(student.getId());
|
|
|
+ retList.add(new QuizResultVO(studentScore,student,list));
|
|
|
+ }
|
|
|
+ return retList;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void modifySlideState(Slide slide) {
|
|
|
+ final List<Quiz> quizzes = quizDAO.findAllBySlide(slide);
|
|
|
+ quizzes.forEach(quiz->checkQuizState(quiz,slide));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public Integer upStudentAnswer(Long quizId, Integer uid, List<QuestionStudentAnswerDTO> answers) {
|
|
|
+ Assert.notNull(quizId,"QUIZ ID can't be null!");
|
|
|
+ final Quiz quiz = quizDAO.findQuizById(quizId);
|
|
|
+ if(quiz.getState()!=QuizState.ONGOING){throw new RuntimeException("can't put not ONGOING quiz answer!");}
|
|
|
+ User student =new User();student.setId(uid);
|
|
|
+ Set<QuizStudentAnswer> QuizAnswers = quizStudentAnswerDAO.findByStudentAndQuiz(student,quiz);
|
|
|
+ Set<String> questionIdSet = new HashSet<>(quiz.getQuestions());
|
|
|
+ Map<String,QuizStudentAnswer> qsaMap=new HashMap<>(questionIdSet.size());
|
|
|
+ for(QuizStudentAnswer qsa: QuizAnswers){
|
|
|
+ qsaMap.put(qsa.getQuestionId(),qsa);
|
|
|
+ }
|
|
|
+ //确定被更新或者新建的学生答案提交
|
|
|
+ List<QuizStudentAnswer> toSaveQSA=new ArrayList<>();
|
|
|
+ for(QuestionStudentAnswerDTO studentAnswer:answers){
|
|
|
+ String id = studentAnswer.getQuestionId();
|
|
|
+ String answer = studentAnswer.getStudentAnswer().toString();
|
|
|
+ if(!questionIdSet.contains(id)){break;}
|
|
|
+ if(qsaMap.get(id)!=null){
|
|
|
+ qsaMap.get(id).setStudentAnswer(answer);
|
|
|
+ toSaveQSA.add(qsaMap.get(id));
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ QuizStudentAnswer qsa=QuizStudentAnswer.builder()
|
|
|
+ .questionId(id).quiz(quiz).student(student).studentAnswer(answer)
|
|
|
+ .build();
|
|
|
+ toSaveQSA.add(qsa);
|
|
|
+ }
|
|
|
+ Integer savedNum=toSaveQSA.size();
|
|
|
+ quizStudentAnswerDAO.saveAll(toSaveQSA);
|
|
|
+ return savedNum;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public Page<Quiz> getAllBasicQuizBySlide(Integer slideId, Pageable pageable, QuizState state, Integer uid) {
|
|
|
Assert.notNull(slideId,"slide can't be null");
|
|
|
@@ -121,33 +182,115 @@ public class QuizServiceImpl implements QuizService {
|
|
|
else
|
|
|
return quizDAO.findAllBySlide(slide,pageable);
|
|
|
}
|
|
|
-
|
|
|
- public void checkQuizState(Quiz quiz,Slide slide){
|
|
|
- int QuizTime=quiz.getQuizTime().getNum();
|
|
|
- int slideTime=slide.getState().getNum();
|
|
|
- if(quiz.getId()==null)
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public QuizState checkQuizState(Quiz quiz, Slide slide) {
|
|
|
+ int QuizTime = quiz.getQuizTime().getNum();
|
|
|
+ int slideTime = slide.getState().getNum();
|
|
|
+ if (quiz.getId() == null)
|
|
|
//未持久化的quiz
|
|
|
- quiz.setState(slideTime<QuizTime?QuizState.NOT_STARTED:QuizState.ONGOING);
|
|
|
+ quiz.setState(slideTime < QuizTime ? QuizState.NOT_STARTED : QuizState.ONGOING);
|
|
|
else {
|
|
|
- //持久化后,不仅仅需要推进状态,还需要判分等步骤
|
|
|
- if(slideTime<QuizTime){
|
|
|
+ //持久化后,不仅仅需要推进状态,还需要判分,保存等步骤
|
|
|
+ if (slideTime < QuizTime) {
|
|
|
+ if(quiz.getState().equals(QuizState.NOT_STARTED)){return QuizState.NOT_STARTED;}
|
|
|
//未开始
|
|
|
quiz.setState(QuizState.NOT_STARTED);
|
|
|
- }else if(slideTime==QuizTime){
|
|
|
+ } else if (slideTime == QuizTime) {
|
|
|
+ if(quiz.getState().equals(QuizState.ONGOING)){return QuizState.ONGOING;}
|
|
|
quiz.setState(QuizState.ONGOING);
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
+ if(quiz.getState().equals(QuizState.CLOSED)){return QuizState.CLOSED;}
|
|
|
quiz.setState(QuizState.CLOSED);
|
|
|
+ Set<QuizStudentAnswer> QuizAnswers = quizStudentAnswerDAO.findAllByQuiz(quiz);
|
|
|
+ final Set<Integer> studentIds = chooseDAO.findStudentIdsByCourseId(slide.getCourseId());
|
|
|
+ Student2_QuestionId2Answer triadMap = new Student2_QuestionId2Answer(studentIds, quiz.getQuestions().size());
|
|
|
+ //将已经作答的学生答案加入
|
|
|
+ for (QuizStudentAnswer answers : QuizAnswers) {
|
|
|
+ triadMap.addAnswer(answers);
|
|
|
+ }
|
|
|
+ //生成未作答的学生答案
|
|
|
+ triadMap.fullAnswer(quiz);
|
|
|
+ //检查答案,赋分
|
|
|
+ triadMap.checkAnswerAndFullPass(bokUtil.BokFindByIdIn(quiz.getQuestions()), quiz);
|
|
|
+ //保存现场
|
|
|
+ quizStudentAnswerDAO.saveAll(triadMap.getAll());
|
|
|
}
|
|
|
|
|
|
+ quizDAO.save(quiz);
|
|
|
+ }
|
|
|
+ return quiz.getState();
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public QuizState checkQuizState(Long quizId, Integer slideId){
|
|
|
+ Quiz quiz=quizDAO.findQuizById(quizId);
|
|
|
+ Slide slide=slideDAO.findSlideById(slideId);
|
|
|
+ return checkQuizState(quiz,slide);
|
|
|
+ }
|
|
|
+ private class Student2_QuestionId2Answer{
|
|
|
+ Map<Integer,Map<String,QuizStudentAnswer>> Student2_QuestionId2Answer;
|
|
|
+ public Student2_QuestionId2Answer(Set<Integer> studentIds,Integer quizQuestionSize) {
|
|
|
+ Student2_QuestionId2Answer =new LinkedHashMap<>(studentIds.size());
|
|
|
+ for(Integer studentId:studentIds){
|
|
|
+ Map<String,QuizStudentAnswer> QuestionId2Answer= new LinkedHashMap<>(quizQuestionSize);
|
|
|
+ Student2_QuestionId2Answer.put(studentId,QuestionId2Answer);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public void addAnswer(QuizStudentAnswer answers){
|
|
|
+ Map<String, QuizStudentAnswer> stringQuizStudentAnswerMap = Student2_QuestionId2Answer.get(answers.getStudent().getId());
|
|
|
+ if(stringQuizStudentAnswerMap==null){
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+ stringQuizStudentAnswerMap.put(answers.getQuestionId(),answers);
|
|
|
+ }
|
|
|
+ public void fullAnswer(Quiz quiz){
|
|
|
+ List<String> questions=quiz.getQuestions();
|
|
|
+ Student2_QuestionId2Answer.forEach((studentId,map)->{
|
|
|
+ if(questions.size()!=map.size()){
|
|
|
+ for(String questionId:questions){
|
|
|
+ if(map.get(questionId)==null){
|
|
|
+ User student =new User();student.setId(studentId);
|
|
|
+ QuizStudentAnswer qsa=QuizStudentAnswer.builder()
|
|
|
+ .questionId(questionId).quiz(quiz).student(student)
|
|
|
+ .build();
|
|
|
+ map.put(questionId,qsa);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ public void checkAnswerAndFullPass(List<QuestionVO> questionVOS,Quiz quiz){
|
|
|
+ if(quiz.getStudentScore()==null){quiz.setStudentScore(new ArrayList<>(Student2_QuestionId2Answer.size()));}
|
|
|
+ Student2_QuestionId2Answer.forEach((studentId,map)->{
|
|
|
+ int passCount=0;
|
|
|
+ for(QuestionVO vo:questionVOS){
|
|
|
+ final String studentAnswer = map.get(vo.getQuestionId()).getStudentAnswer();
|
|
|
+ boolean pass=vo.checkAnswer(studentAnswer);
|
|
|
+ if(pass){passCount++;}
|
|
|
+ map.get(vo.getQuestionId()).setPass(pass);
|
|
|
+ }
|
|
|
+ Double score = (100.0*passCount)/questionVOS.size();
|
|
|
+ User student =new User();student.setId(studentId);
|
|
|
+ StudentScore studentScore=new StudentScore();
|
|
|
+ studentScore.setQuizId(quiz.getId());
|
|
|
+ studentScore.setScore(Long.valueOf(Math.round(score)).intValue());
|
|
|
+ studentScore.setStudent(student);
|
|
|
+ quiz.getStudentScore().add(studentScore);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ public List<QuizStudentAnswer> getAll(){
|
|
|
+ List<QuizStudentAnswer> list=new ArrayList<>();
|
|
|
+ for(Integer sid:Student2_QuestionId2Answer.keySet()){
|
|
|
+ list.addAll(Student2_QuestionId2Answer.get(sid).values());
|
|
|
+ }
|
|
|
+ return list;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
@Override
|
|
|
public QuizVO combineQuiz(Long quizId){
|
|
|
Assert.notNull(quizId,"QUIZ ID can't be null!");
|
|
|
final Quiz quizById = quizDAO.findQuizById(quizId);
|
|
|
final List<QuestionVO> questionVOS = bokUtil.BokFindByIdIn(quizById.getQuestions());
|
|
|
- System.out.println(questionVOS);
|
|
|
QuizVO ret= new QuizVO(quizById,questionVOS);
|
|
|
return ret;
|
|
|
}
|
|
|
@@ -169,7 +312,7 @@ public class QuizServiceImpl implements QuizService {
|
|
|
}
|
|
|
|
|
|
List<QuestionVO> questionVOS = bokUtil.BokFindByIdIn(quizById.getQuestions());
|
|
|
- final Set<QuizStudentAnswer> QuizAnswers = quizStudentAnswerDAO.findByQuizAndStudent(quizById, student);
|
|
|
+ final Set<QuizStudentAnswer> QuizAnswers = quizStudentAnswerDAO.findByStudentAndQuiz(student,quizById);
|
|
|
Map<String,QuizStudentAnswer> qsaMap=new HashMap<>(QuizAnswers.size());
|
|
|
for(QuizStudentAnswer qsa: QuizAnswers){
|
|
|
qsaMap.put(qsa.getQuestionId(),qsa);
|