|
|
@@ -1,22 +1,24 @@
|
|
|
package nju.seec.helper.service.impl;
|
|
|
|
|
|
-import nju.seec.helper.dao.QuizDAO;
|
|
|
-import nju.seec.helper.dao.QuizStudentAnswerDAO;
|
|
|
-import nju.seec.helper.dao.SlideDAO;
|
|
|
+import nju.seec.helper.dao.*;
|
|
|
import nju.seec.helper.dto.LoginUser;
|
|
|
import nju.seec.helper.entity.*;
|
|
|
import nju.seec.helper.service.QuizService;
|
|
|
import nju.seec.helper.util.BOKUtil;
|
|
|
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.QuizVO;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.data.domain.Pageable;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.Assert;
|
|
|
|
|
|
import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
|
public class QuizServiceImpl implements QuizService {
|
|
|
@@ -27,9 +29,16 @@ public class QuizServiceImpl implements QuizService {
|
|
|
@Autowired
|
|
|
private QuizStudentAnswerDAO quizStudentAnswerDAO;
|
|
|
@Autowired
|
|
|
+ private ChooseDAO chooseDAO;
|
|
|
+ @Autowired
|
|
|
+ private CourseDAO courseDAO;
|
|
|
+ @Autowired
|
|
|
+ private UserDAO userDAO;
|
|
|
+ @Autowired
|
|
|
private BOKUtil bokUtil;
|
|
|
+ @Override
|
|
|
@Transactional
|
|
|
- public QuizVO CreateQuiz(String name, Integer uid, SlideState quizTime,
|
|
|
+ public Quiz CreateQuiz(String name, Integer uid, SlideState quizTime,
|
|
|
Integer slideId, List<String> questions){
|
|
|
User teacher =new User();
|
|
|
teacher.setId(uid);
|
|
|
@@ -44,25 +53,123 @@ public class QuizServiceImpl implements QuizService {
|
|
|
quiz.setQuizTime(quizTime);
|
|
|
quiz.setSlide(slide);
|
|
|
quiz.setTeacher(teacher);
|
|
|
- quiz.setState(quizTime.getNum()<slide.getState().getNum()?QuizState.CLOSED:QuizState.ONGOING);
|
|
|
+
|
|
|
+ checkQuizState(quiz,slide);
|
|
|
+
|
|
|
quiz=quizDAO.saveAndFlush(quiz);
|
|
|
- return combineQuiz(quiz.getId());
|
|
|
+ return quiz;
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public String deleteQuiz(Long quizId) {
|
|
|
+ Quiz quiz =quizDAO.findQuizById(quizId);
|
|
|
+ if(quiz.getState()!=QuizState.NOT_STARTED){return "can't delete started quiz!";}
|
|
|
+ quizDAO.delete(quiz);
|
|
|
+ return "delete success";
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ 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 new RuntimeException("can't delete started quiz!");}
|
|
|
+ if(name!=null) quiz.setName(name);
|
|
|
+ if(questions!=null) quiz.setQuestions(questions);
|
|
|
+ if(quizTime!=null) {
|
|
|
+ quiz.setQuizTime(quizTime);
|
|
|
+ checkQuizState(quiz,quiz.getSlide());
|
|
|
+ }
|
|
|
+ final Quiz quiz1 = quizDAO.saveAndFlush(quiz);
|
|
|
+ return quiz1;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<Quiz> getAllBasicQuizByUser(Pageable pageable, QuizState state, Integer uid) {
|
|
|
+ User user=userDAO.findUserById(uid);
|
|
|
+ //todo: 这里写的太狗屎了,可以用重写sql或者用实例查询
|
|
|
+ if(user.getType().equals(UserType.STUDENT)){
|
|
|
+ final Set<Integer> courseIdsByStudentId = chooseDAO.findCourseIdsByStudentId(uid);
|
|
|
+ final Set<Course> collect = courseIdsByStudentId.stream().map(i -> {
|
|
|
+ Course c = new Course();
|
|
|
+ c.setId(i);
|
|
|
+ return c;
|
|
|
+ }).collect(Collectors.toSet());
|
|
|
+ if(state!=null){
|
|
|
+ return quizDAO.findAllByCourseInAndState(collect,state,pageable);
|
|
|
+ }else{
|
|
|
+ return quizDAO.findAllByCourseIn(collect,pageable);
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ if(state!=null){
|
|
|
+ return quizDAO.findByTeacherAndState(user,state,pageable);
|
|
|
+ }else{
|
|
|
+ return quizDAO.findByTeacher(user,pageable);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<Quiz> getAllBasicQuizBySlide(Integer slideId, Pageable pageable, QuizState state, Integer uid) {
|
|
|
+ Assert.notNull(slideId,"slide can't be null");
|
|
|
+ Slide slide=new Slide();
|
|
|
+ slide.setId(slideId);
|
|
|
+ if(state!=null)
|
|
|
+ return quizDAO.findAllBySlideAndState(slide,state, pageable);
|
|
|
+ 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)
|
|
|
+ //未持久化的quiz
|
|
|
+ quiz.setState(slideTime<QuizTime?QuizState.NOT_STARTED:QuizState.ONGOING);
|
|
|
+ else {
|
|
|
+ //持久化后,不仅仅需要推进状态,还需要判分等步骤
|
|
|
+ if(slideTime<QuizTime){
|
|
|
+ //未开始
|
|
|
+ quiz.setState(QuizState.NOT_STARTED);
|
|
|
+ }else if(slideTime==QuizTime){
|
|
|
+ quiz.setState(QuizState.ONGOING);
|
|
|
+ }else{
|
|
|
+ quiz.setState(QuizState.CLOSED);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @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;
|
|
|
}
|
|
|
- public QuizVO combineQuizWithAnswer(Long quizId, Integer uid){
|
|
|
+ @Override
|
|
|
+ public QuizVO combineQuizWithAnswer(Long quizId, Integer uid,UserType userType){
|
|
|
Assert.notNull(quizId,"QUIZ ID can't be null!");
|
|
|
final Quiz quizById = quizDAO.findQuizById(quizId);
|
|
|
User student =new User();
|
|
|
student.setId(uid);
|
|
|
- final Set<QuizStudentAnswer> QuizAnswers = quizStudentAnswerDAO.findByQuizAndStudent(quizById, student);
|
|
|
+
|
|
|
+ if(userType.equals(UserType.TEACHER)){
|
|
|
+ //向老师展示
|
|
|
+ List<QuestionVO> 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.findByQuizAndStudent(quizById, student);
|
|
|
Map<String,QuizStudentAnswer> qsaMap=new HashMap<>(QuizAnswers.size());
|
|
|
for(QuizStudentAnswer qsa: QuizAnswers){
|
|
|
qsaMap.put(qsa.getQuestionId(),qsa);
|
|
|
@@ -71,9 +178,11 @@ public class QuizServiceImpl implements QuizService {
|
|
|
if(qsaMap.get(vo.getQuestionId())!=null){
|
|
|
vo.exAddAnswer(qsaMap.get(vo.getQuestionId()));
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
+
|
|
|
QuizVO ret= new QuizVO(quizById,questionVOS);
|
|
|
return ret;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
}
|