|
|
@@ -4,13 +4,13 @@ import nju.seec.helper.dao.*;
|
|
|
import nju.seec.helper.dto.QuestionStudentAnswerDTO;
|
|
|
import nju.seec.helper.entity.*;
|
|
|
import nju.seec.helper.service.QuizService;
|
|
|
-import nju.seec.helper.util.BOKUtil;
|
|
|
+import nju.seec.helper.util.BokUtil;
|
|
|
import nju.seec.helper.util.enums.ExceptionType;
|
|
|
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.util.exception.HelperException;
|
|
|
-import nju.seec.helper.vo.quiz.QuestionVO;
|
|
|
+import nju.seec.helper.vo.quiz.BaseQuestionVO;
|
|
|
import nju.seec.helper.vo.quiz.QuizResultVO;
|
|
|
import nju.seec.helper.vo.quiz.QuizVO;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
@@ -31,10 +31,10 @@ public class QuizServiceImpl implements QuizService {
|
|
|
private final ChooseDAO chooseDAO;
|
|
|
private final CourseDAO courseDAO;
|
|
|
private final UserDAO userDAO;
|
|
|
- private final BOKUtil bokUtil;
|
|
|
+ private final BokUtil bokUtil;
|
|
|
|
|
|
@Autowired
|
|
|
- public QuizServiceImpl(SlideDAO slideDAO, QuizDAO quizDAO, QuizStudentAnswerDAO quizStudentAnswerDAO, ChooseDAO chooseDAO, CourseDAO courseDAO, UserDAO userDAO, BOKUtil bokUtil) {
|
|
|
+ public QuizServiceImpl(SlideDAO slideDAO, QuizDAO quizDAO, QuizStudentAnswerDAO quizStudentAnswerDAO, ChooseDAO chooseDAO, CourseDAO courseDAO, UserDAO userDAO, BokUtil bokUtil) {
|
|
|
this.slideDAO = slideDAO;
|
|
|
this.quizDAO = quizDAO;
|
|
|
this.quizStudentAnswerDAO = quizStudentAnswerDAO;
|
|
|
@@ -45,8 +45,8 @@ public class QuizServiceImpl implements QuizService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- @Transactional
|
|
|
- public Quiz CreateQuiz(String name, Long uid, SlideState quizTime,
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Quiz createQuiz(String name, Long uid, SlideState quizTime,
|
|
|
Long slideId, List<String> questions){
|
|
|
User teacher =new User();
|
|
|
teacher.setId(uid);
|
|
|
@@ -76,13 +76,17 @@ public class QuizServiceImpl implements QuizService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- @Transactional
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
public Quiz modifyQuiz(Long quizId, String name, List<String> questions, SlideState quizTime, String slideId) {
|
|
|
|
|
|
Quiz quiz =quizDAO.findQuizById(quizId);
|
|
|
if(quiz.getState()!=QuizState.NOT_STARTED){throw HelperException.of(ExceptionType.FORBIDDEN,"不能修改已经开始的测试!");}
|
|
|
- if(name!=null) quiz.setName(name);
|
|
|
- if(questions!=null) quiz.setQuestions(questions);
|
|
|
+ if(name!=null) {
|
|
|
+ quiz.setName(name);
|
|
|
+ }
|
|
|
+ if(questions!=null) {
|
|
|
+ quiz.setQuestions(questions);
|
|
|
+ }
|
|
|
if(quizTime!=null) {
|
|
|
quiz.setQuizTime(quizTime);
|
|
|
checkQuizState(quiz,quiz.getSlide());
|
|
|
@@ -125,14 +129,14 @@ public class QuizServiceImpl implements QuizService {
|
|
|
if(quiz.getState()!=QuizState.CLOSED){throw new RuntimeException("测试未关闭,无法得到测试结果");}
|
|
|
List<QuizResultVO> retList=new ArrayList<>();
|
|
|
final List<StudentScore> studentScoreList = quiz.getStudentScore();
|
|
|
- Map<Long,List<QuizStudentAnswer>> IdQuizStudentAnswerMap=new HashMap<>();
|
|
|
+ Map<Long,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);
|
|
|
+ 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());
|
|
|
+ final List<QuizStudentAnswer> list = idQuizStudentAnswerMap.get(student.getId());
|
|
|
retList.add(new QuizResultVO(studentScore,student,list));
|
|
|
}
|
|
|
return retList;
|
|
|
@@ -145,20 +149,20 @@ public class QuizServiceImpl implements QuizService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- @Transactional
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
public Integer upStudentAnswer(Long quizId, Long uid, List<QuestionStudentAnswerDTO> answers) {
|
|
|
Assert.notNull(quizId,"错误的测试!");
|
|
|
final Quiz quiz = quizDAO.findQuizById(quizId);
|
|
|
if(quiz.getState()!=QuizState.ONGOING){throw HelperException.of(ExceptionType.FORBIDDEN,"不能提交尚未开始的作业!");}
|
|
|
User student =new User();student.setId(uid);
|
|
|
- Set<QuizStudentAnswer> QuizAnswers = quizStudentAnswerDAO.findByStudentAndQuiz(student,quiz);
|
|
|
+ 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){
|
|
|
+ for(QuizStudentAnswer qsa: quizAnswers){
|
|
|
qsaMap.put(qsa.getQuestionId(),qsa);
|
|
|
}
|
|
|
//确定被更新或者新建的学生答案提交
|
|
|
- List<QuizStudentAnswer> toSaveQSA=new ArrayList<>();
|
|
|
+ List<QuizStudentAnswer> quizStudentAnswers=new ArrayList<>();
|
|
|
for(QuestionStudentAnswerDTO studentAnswer:answers){
|
|
|
String id = studentAnswer.getQuestionId();
|
|
|
String answer = studentAnswer.getStudentAnswer().toString();
|
|
|
@@ -169,17 +173,17 @@ public class QuizServiceImpl implements QuizService {
|
|
|
}
|
|
|
qsaMap.get(id).setStudentAnswer(answer);
|
|
|
System.out.println(answer);
|
|
|
- toSaveQSA.add(qsaMap.get(id));
|
|
|
+ quizStudentAnswers.add(qsaMap.get(id));
|
|
|
}else {
|
|
|
QuizStudentAnswer qsa=QuizStudentAnswer.builder()
|
|
|
.questionId(id).quiz(quiz).student(student).studentAnswer(answer)
|
|
|
.build();
|
|
|
- toSaveQSA.add(qsa);
|
|
|
+ quizStudentAnswers.add(qsa);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
- Integer savedNum=toSaveQSA.size();
|
|
|
- quizStudentAnswerDAO.saveAll(toSaveQSA);
|
|
|
+ Integer savedNum=quizStudentAnswers.size();
|
|
|
+ quizStudentAnswerDAO.saveAll(quizStudentAnswers);
|
|
|
return savedNum;
|
|
|
}
|
|
|
|
|
|
@@ -188,42 +192,45 @@ public class QuizServiceImpl implements QuizService {
|
|
|
Assert.notNull(slideId,"错误的幻灯片!");
|
|
|
Slide slide=new Slide();
|
|
|
slide.setId(slideId);
|
|
|
- if(state!=null)
|
|
|
- return quizDAO.findAllBySlideAndState(slide,state, pageable);
|
|
|
- else
|
|
|
- return quizDAO.findAllBySlide(slide,pageable);
|
|
|
+ if(state!=null) {
|
|
|
+ return quizDAO.findAllBySlideAndState(slide, state, pageable);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return quizDAO.findAllBySlide(slide, pageable);
|
|
|
+ }
|
|
|
}
|
|
|
@Override
|
|
|
- @Transactional
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
public QuizState checkQuizState(Quiz quiz, Slide slide) {
|
|
|
- int QuizTime = quiz.getQuizTime().getNum();
|
|
|
+ int quizTime = quiz.getQuizTime().getNum();
|
|
|
int slideTime = slide.getState().getNum();
|
|
|
- if (quiz.getId() == null)
|
|
|
+ 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 {
|
|
|
if(quiz.getState().equals(QuizState.CLOSED)){return QuizState.CLOSED;}
|
|
|
quiz.setState(QuizState.CLOSED);
|
|
|
- Set<QuizStudentAnswer> QuizAnswers = quizStudentAnswerDAO.findAllByQuiz(quiz);
|
|
|
+ Set<QuizStudentAnswer> quizAnswers = quizStudentAnswerDAO.findAllByQuiz(quiz);
|
|
|
final Set<Long> studentIds = chooseDAO.findStudentIdsByCourseId(slide.getCourseId());
|
|
|
- Student2_QuestionId2Answer triadMap = new Student2_QuestionId2Answer(studentIds, quiz.getQuestions().size());
|
|
|
+ Student2QuestionId2Answer triadMap = new Student2QuestionId2Answer(studentIds, quiz.getQuestions().size());
|
|
|
//将已经作答的学生答案加入
|
|
|
- for (QuizStudentAnswer answers : QuizAnswers) {
|
|
|
+ for (QuizStudentAnswer answers : quizAnswers) {
|
|
|
triadMap.addAnswer(answers);
|
|
|
}
|
|
|
//生成未作答的学生答案
|
|
|
triadMap.fullAnswer(quiz);
|
|
|
//检查答案,赋分
|
|
|
- triadMap.checkAnswerAndFullPass(bokUtil.BokFindByIdIn(quiz.getQuestions()), quiz);
|
|
|
+ triadMap.checkAnswerAndFullPass(bokUtil.bokFindByIdIn(quiz.getQuestions()), quiz);
|
|
|
//保存现场
|
|
|
quizStudentAnswerDAO.saveAll(triadMap.getAll());
|
|
|
}
|
|
|
@@ -238,17 +245,17 @@ public class QuizServiceImpl implements QuizService {
|
|
|
Slide slide=slideDAO.findSlideById(slideId);
|
|
|
return checkQuizState(quiz,slide);
|
|
|
}
|
|
|
- private class Student2_QuestionId2Answer{
|
|
|
- Map<Long,Map<String,QuizStudentAnswer>> Student2_QuestionId2Answer;
|
|
|
- Student2_QuestionId2Answer(Set<Long> studentIds, Integer quizQuestionSize) {
|
|
|
- Student2_QuestionId2Answer =new LinkedHashMap<>(studentIds.size());
|
|
|
+ private class Student2QuestionId2Answer {
|
|
|
+ Map<Long,Map<String,QuizStudentAnswer>> student2QuestionId2Answer;
|
|
|
+ Student2QuestionId2Answer(Set<Long> studentIds, Integer quizQuestionSize) {
|
|
|
+ student2QuestionId2Answer =new LinkedHashMap<>(studentIds.size());
|
|
|
for(Long studentId:studentIds){
|
|
|
- Map<String,QuizStudentAnswer> QuestionId2Answer= new LinkedHashMap<>(quizQuestionSize);
|
|
|
- Student2_QuestionId2Answer.put(studentId,QuestionId2Answer);
|
|
|
+ Map<String,QuizStudentAnswer> questionId2Answer= new LinkedHashMap<>(quizQuestionSize);
|
|
|
+ student2QuestionId2Answer.put(studentId,questionId2Answer);
|
|
|
}
|
|
|
}
|
|
|
void addAnswer(QuizStudentAnswer answers){
|
|
|
- Map<String, QuizStudentAnswer> stringQuizStudentAnswerMap = Student2_QuestionId2Answer.get(answers.getStudent().getId());
|
|
|
+ Map<String, QuizStudentAnswer> stringQuizStudentAnswerMap = student2QuestionId2Answer.get(answers.getStudent().getId());
|
|
|
if(stringQuizStudentAnswerMap==null){
|
|
|
return ;
|
|
|
}
|
|
|
@@ -256,7 +263,7 @@ public class QuizServiceImpl implements QuizService {
|
|
|
}
|
|
|
void fullAnswer(Quiz quiz){
|
|
|
List<String> questions=quiz.getQuestions();
|
|
|
- Student2_QuestionId2Answer.forEach((studentId,map)->{
|
|
|
+ student2QuestionId2Answer.forEach((studentId, map)->{
|
|
|
if(questions.size()!=map.size()){
|
|
|
for(String questionId:questions){
|
|
|
if(map.get(questionId)==null){
|
|
|
@@ -270,17 +277,17 @@ public class QuizServiceImpl implements QuizService {
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
- void checkAnswerAndFullPass(List<QuestionVO> questionVOS, Quiz quiz){
|
|
|
- if(quiz.getStudentScore()==null){quiz.setStudentScore(new ArrayList<>(Student2_QuestionId2Answer.size()));}
|
|
|
- Student2_QuestionId2Answer.forEach((studentId,map)->{
|
|
|
+ void checkAnswerAndFullPass(List<BaseQuestionVO> questionVos, Quiz quiz){
|
|
|
+ if(quiz.getStudentScore()==null){quiz.setStudentScore(new ArrayList<>(student2QuestionId2Answer.size()));}
|
|
|
+ student2QuestionId2Answer.forEach((studentId, map)->{
|
|
|
int passCount=0;
|
|
|
- for(QuestionVO vo:questionVOS){
|
|
|
+ for(BaseQuestionVO 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();
|
|
|
+ Double score = (100.0*passCount)/questionVos.size();
|
|
|
User student =new User();student.setId(studentId);
|
|
|
StudentScore studentScore=new StudentScore();
|
|
|
studentScore.setQuizId(quiz.getId());
|
|
|
@@ -291,8 +298,8 @@ public class QuizServiceImpl implements QuizService {
|
|
|
}
|
|
|
List<QuizStudentAnswer> getAll(){
|
|
|
List<QuizStudentAnswer> list=new ArrayList<>();
|
|
|
- for(Long sid:Student2_QuestionId2Answer.keySet()){
|
|
|
- list.addAll(Student2_QuestionId2Answer.get(sid).values());
|
|
|
+ for(Long sid: student2QuestionId2Answer.keySet()){
|
|
|
+ list.addAll(student2QuestionId2Answer.get(sid).values());
|
|
|
}
|
|
|
return list;
|
|
|
}
|
|
|
@@ -301,8 +308,8 @@ public class QuizServiceImpl implements QuizService {
|
|
|
public QuizVO combineQuiz(Long quizId){
|
|
|
Assert.notNull(quizId,"错误的测试!");
|
|
|
final Quiz quizById = quizDAO.findQuizById(quizId);
|
|
|
- final List<QuestionVO> questionVOS = bokUtil.BokFindByIdIn(quizById.getQuestions());
|
|
|
- return new QuizVO(quizById,questionVOS);
|
|
|
+ final List<BaseQuestionVO> questionVoS = bokUtil.bokFindByIdIn(quizById.getQuestions());
|
|
|
+ return new QuizVO(quizById,questionVoS);
|
|
|
}
|
|
|
@Override
|
|
|
public QuizVO combineQuizWithAnswer(Long quizId, Long uid,UserType userType){
|
|
|
@@ -313,27 +320,27 @@ public class QuizServiceImpl implements QuizService {
|
|
|
|
|
|
if(userType==null||userType.equals(UserType.TEACHER)){
|
|
|
//向老师展示
|
|
|
- List<QuestionVO> questionVOS = bokUtil.BokFindByIdIn(quizById.getQuestions());
|
|
|
- return new QuizVO(quizById,questionVOS);
|
|
|
+ List<BaseQuestionVO> questionVoS = bokUtil.bokFindByIdIn(quizById.getQuestions());
|
|
|
+ return new QuizVO(quizById,questionVoS);
|
|
|
}
|
|
|
if(quizById.getState().equals(QuizState.NOT_STARTED)){
|
|
|
//未开始看不到题目
|
|
|
return new QuizVO(quizById);
|
|
|
}
|
|
|
|
|
|
- List<QuestionVO> questionVOS = bokUtil.BokFindByIdIn(quizById.getQuestions());
|
|
|
- final Set<QuizStudentAnswer> QuizAnswers = quizStudentAnswerDAO.findByStudentAndQuiz(student,quizById);
|
|
|
- Map<String,QuizStudentAnswer> qsaMap=new HashMap<>(QuizAnswers.size());
|
|
|
- for(QuizStudentAnswer qsa: QuizAnswers){
|
|
|
+ List<BaseQuestionVO> questionVos = bokUtil.bokFindByIdIn(quizById.getQuestions());
|
|
|
+ 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);
|
|
|
}
|
|
|
- for(QuestionVO vo:questionVOS){
|
|
|
+ for(BaseQuestionVO vo:questionVos){
|
|
|
if(qsaMap.get(vo.getQuestionId())!=null){
|
|
|
vo.exAddAnswer(qsaMap.get(vo.getQuestionId()));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return new QuizVO(quizById,questionVOS);
|
|
|
+ return new QuizVO(quizById,questionVos);
|
|
|
}
|
|
|
|
|
|
|