|
|
@@ -1,12 +1,29 @@
|
|
|
package nju.seec.helper.service.impl;
|
|
|
|
|
|
-import nju.seec.helper.dto.LoginUser;
|
|
|
-//import nju.seec.helper.dto.QuizDTO;
|
|
|
+import com.google.common.collect.ImmutableSet;
|
|
|
+import nju.seec.helper.dao.*;
|
|
|
+import nju.seec.helper.dto.quiz.QuizDTO;
|
|
|
+import nju.seec.helper.dto.user.LoginUser;
|
|
|
+import nju.seec.helper.entity.Course;
|
|
|
+import nju.seec.helper.entity.Quiz;
|
|
|
+import nju.seec.helper.entity.Slide;
|
|
|
+import nju.seec.helper.service.MessageService;
|
|
|
import nju.seec.helper.service.QuizService;
|
|
|
-//import nju.seec.helper.vo.quiz.QuizVO;
|
|
|
+import nju.seec.helper.service.util.AuthUtils;
|
|
|
+import nju.seec.helper.service.util.StringUtils;
|
|
|
+import nju.seec.helper.util.enums.*;
|
|
|
+import nju.seec.helper.util.exception.HelperException;
|
|
|
+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.data.jpa.domain.Specification;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import java.util.List;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* @author xst
|
|
|
@@ -15,356 +32,172 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
*/
|
|
|
@Service
|
|
|
public class QuizServiceImpl implements QuizService {
|
|
|
-// private final SlideDAO slideDAO;
|
|
|
-// private final QuizDAO quizDAO;
|
|
|
-// private final QuizStudentAnswerDAO quizStudentAnswerDAO;
|
|
|
-// private final ChooseDAO chooseDAO;
|
|
|
-// private final CourseDAO courseDAO;
|
|
|
-// private final UserDAO userDAO;
|
|
|
-// private final BokUtil bokUtil;
|
|
|
-//
|
|
|
-// @Autowired
|
|
|
-// 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;
|
|
|
-// this.chooseDAO = chooseDAO;
|
|
|
-// this.courseDAO = courseDAO;
|
|
|
-// this.userDAO = userDAO;
|
|
|
-// this.bokUtil = bokUtil;
|
|
|
-// }
|
|
|
+ private final SlideDAO slideDAO;
|
|
|
+ private final QuizDAO quizDAO;
|
|
|
+ private final QuizStudentAnswerDAO quizStudentAnswerDAO;
|
|
|
+ private final ChooseDAO chooseDAO;
|
|
|
+ private final CourseDAO courseDAO;
|
|
|
+ private final UserDAO userDAO;
|
|
|
+
|
|
|
+ private final MessageService messageService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public QuizServiceImpl(SlideDAO slideDAO, QuizDAO quizDAO, QuizStudentAnswerDAO quizStudentAnswerDAO, ChooseDAO chooseDAO, CourseDAO courseDAO, UserDAO userDAO, MessageService messageService) {
|
|
|
+ this.slideDAO = slideDAO;
|
|
|
+ this.quizDAO = quizDAO;
|
|
|
+ this.quizStudentAnswerDAO = quizStudentAnswerDAO;
|
|
|
+ this.chooseDAO = chooseDAO;
|
|
|
+ this.courseDAO = courseDAO;
|
|
|
+ this.userDAO = userDAO;
|
|
|
+ this.messageService = messageService;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public QuizVO createQuiz(LoginUser user, QuizDTO quizDTO) {
|
|
|
+ Slide slide = slideDAO.findSlideById(quizDTO.getSlideId());
|
|
|
+
|
|
|
+ AuthUtils.checkDataAuth(user.getId(), slide.getTeacher().getId(), "您无权创建该课件的测试");
|
|
|
+ checkSameName(0L, quizDTO.getSlideId(), quizDTO.getName());
|
|
|
+ checkQuizTypeOkToSlideState(slide.getState(), quizDTO.getType());
|
|
|
+
|
|
|
+ Quiz quiz = new Quiz()
|
|
|
+ .setCourse(slide.getCourse())
|
|
|
+ .setSlide(slide)
|
|
|
+ .setTeacher(slide.getTeacher())
|
|
|
+ .setType(quizDTO.getType())
|
|
|
+ .setState(QuizState.getQuizStateBySlideStateAndQuizType(slide.getState(), quizDTO.getType()))
|
|
|
+ .setName(quizDTO.getName())
|
|
|
+ .setMaxSubmitNumber(quizDTO.getMaxSubmitTime())
|
|
|
+ .setQuestions(quizDTO.getQuestions());
|
|
|
+
|
|
|
+ return new QuizVO(quizDAO.save(quiz));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public QuizVO modifyQuiz(LoginUser user, QuizDTO quizDTO) {
|
|
|
+ Quiz quiz = quizDAO.findQuizById(quizDTO.getId());
|
|
|
+
|
|
|
+ AuthUtils.checkDataAuth(user.getId(), quiz.getId(), "您无权修改该测试");
|
|
|
+ checkSubmit(quiz);
|
|
|
+
|
|
|
+ Slide slide = quiz.getSlide();
|
|
|
+ checkQuizTypeOkToSlideState(slide.getState(), quizDTO.getType());
|
|
|
+
|
|
|
+ checkSameName(quiz.getId(), quizDTO.getSlideId(), quizDTO.getName());
|
|
|
+
|
|
|
+ quiz.setType(quizDTO.getType())
|
|
|
+ .setState(QuizState.getQuizStateBySlideStateAndQuizType(slide.getState(), quizDTO.getType()))
|
|
|
+ .setName(quizDTO.getName())
|
|
|
+ .setMaxSubmitNumber(quizDTO.getMaxSubmitTime())
|
|
|
+ .setQuestions(quizDTO.getQuestions());
|
|
|
+
|
|
|
+ return new QuizVO(quizDAO.save(quiz));
|
|
|
+ }
|
|
|
+
|
|
|
+ private void checkSameName(Long quizId, Long slideId, String name) {
|
|
|
+ long exists = quizDAO.count((Specification<Quiz>) (root, query, cb) ->
|
|
|
+ cb.and(cb.notEqual(root.get("id"), quizId),
|
|
|
+ cb.equal(root.get("slide").get("id"), slideId),
|
|
|
+ cb.equal(root.get("name"), name)
|
|
|
+ ));
|
|
|
+ if (exists > 0) {
|
|
|
+ throw HelperException.of(ExceptionType.CONFLICT, "该测试名已使用");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void checkQuizTypeOkToSlideState(SlideState slideState, QuizType quizType) {
|
|
|
+ if (!QuizType.getQuizTypesBySlideState(slideState).contains(quizType)) {
|
|
|
+ throw HelperException.of(ExceptionType.FORBIDDEN, "课件暂时不支持创建该类型的测试,请检查课件状态");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void checkSubmit(Quiz quiz) {
|
|
|
+ if (quizStudentAnswerDAO.existsByQuiz(quiz)) {
|
|
|
+ throw HelperException.of(ExceptionType.FORBIDDEN, "该测试已有提交,无法修改");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public void deleteQuiz(LoginUser user, Long quizId) {
|
|
|
+ Quiz quiz = quizDAO.findQuizById(quizId);
|
|
|
+ AuthUtils.checkDataAuth(user.getId(), quiz.getId(), "您无权删除该测试");
|
|
|
+ quiz.setDeleteAt(System.currentTimeMillis());
|
|
|
+ quizDAO.save(quiz);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(readOnly = true)
|
|
|
+ @Override
|
|
|
+ public Page<QuizVO> teacherGetQuizzes(LoginUser user, Long slideId, String key, Pageable pageable) {
|
|
|
+ return findBySlideIdAndKey(slideId, key, pageable).map(QuizVO::new);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static final Set<QuizState> STUDENT_NOT_GET_SET = ImmutableSet.of(QuizState.NOT_STARTED);
|
|
|
+
|
|
|
+ @Transactional(readOnly = true)
|
|
|
+ @Override
|
|
|
+ public Page<QuizVO> studentGetQuizzes(LoginUser user, Long slideId, String key, Pageable pageable) {
|
|
|
+ return findBySlideIdAndStateNotInAndKey(slideId, STUDENT_NOT_GET_SET, key, pageable).map(QuizVO::new);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(readOnly = true)
|
|
|
+ @Override
|
|
|
+ public QuizVO getOneQuiz(LoginUser user, Long quizId) {
|
|
|
+ Quiz quiz = quizDAO.findQuizById(quizId);
|
|
|
+ checkDataAccessAuth(user, quiz);
|
|
|
+ return new QuizVO(quiz);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public void modifyQuizState(Slide slide) {
|
|
|
+ List<Quiz> quizzes = quizDAO.findAllBySlide(slide);
|
|
|
+ Course course = slide.getCourse();
|
|
|
+ Set<Long> chooseStudentIds = chooseDAO.findStudentIdsByCourseId(course.getId());
|
|
|
+ quizzes.forEach(quiz -> {
|
|
|
+ QuizState quizState = QuizState.getQuizStateBySlideStateAndQuizType(slide.getState(), quiz.getType());
|
|
|
+ quiz.setState(quizState);
|
|
|
+ if (quizState == QuizState.ONGOING) {
|
|
|
+ messageService.createMessage(chooseStudentIds, MessageType.QUIZ_NEW, String.format("[%s] - [%s] 的测试 [%s] 已开放", course.getName(), slide.getName(), quiz.getName()), slide.getId());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void checkDataAccessAuth(LoginUser user, Quiz quiz) {
|
|
|
+ HelperException forbiddenEx = HelperException.of(ExceptionType.FORBIDDEN, "您无权访问该测试");
|
|
|
+
|
|
|
+ switch (user.getType()) {
|
|
|
+ case TEACHER:
|
|
|
+ break;
|
|
|
+ case STUDENT:
|
|
|
+ if (quiz.getState() == QuizState.NOT_STARTED) {
|
|
|
+ throw forbiddenEx;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ throw forbiddenEx;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Page<Quiz> findBySlideIdAndKey(Long slideId, String key, Pageable pageable) {
|
|
|
+ return quizDAO.findAll(
|
|
|
+ (Specification<Quiz>) (root, query, cb) -> cb.and(
|
|
|
+ cb.equal(root.get("slide").get("id"), slideId),
|
|
|
+ cb.equal(root.get("deleteAt"), 0L),
|
|
|
+ cb.like(root.get("name"), StringUtils.keyPattern(key))),
|
|
|
+ pageable);
|
|
|
+ }
|
|
|
|
|
|
-// @Transactional(rollbackFor = Exception.class)
|
|
|
-// @Override
|
|
|
-// public QuizVO createQuiz(LoginUser user, QuizDTO quizDTO) {
|
|
|
-// Slide slide = slideDAO.findSlideById(quizDTO.getSlideId());
|
|
|
-//
|
|
|
-// AuthUtils.checkDataAuth(user.getId(), slide.getTeacher().getId(), "您无权创建该课件的测试");
|
|
|
-// checkSameName(0L, quizDTO.getSlideId(), quizDTO.getName());
|
|
|
-// checkQuizTypeOkToSlideState(slide.getState(), quizDTO.getType());
|
|
|
-//
|
|
|
-// Quiz quiz = new Quiz()
|
|
|
-// .setSlide(slide)
|
|
|
-// .setTeacher(slide.getTeacher())
|
|
|
-// .setType(quizDTO.getType())
|
|
|
-// .setName(quizDTO.getName())
|
|
|
-// .setQuestions(quizDTO.getQuestions());
|
|
|
-//
|
|
|
-// return new QuizVO(quizDAO.save(quiz));
|
|
|
-// }
|
|
|
|
|
|
-// @Transactional(rollbackFor = Exception.class)
|
|
|
-// @Override
|
|
|
-// public QuizVO modifyQuiz(LoginUser user, QuizDTO quizDTO) {
|
|
|
-// Quiz quiz = quizDAO.findQuizById(quizDTO.getId());
|
|
|
-//
|
|
|
-// AuthUtils.checkDataAuth(user.getId(), quiz.getId(), "您无权创建该测试");
|
|
|
-// checkSameName(0L, quizDTO.getSlideId(), quizDTO.getName());
|
|
|
-//
|
|
|
-// Slide slide = quiz.getSlide();
|
|
|
-// checkQuizTypeOkToSlideState(slide.getState(), quizDTO.getType());
|
|
|
-//
|
|
|
-// }
|
|
|
-//
|
|
|
-// @Override
|
|
|
-// public String deleteQuiz(Long quizId) {
|
|
|
-// Quiz quiz = quizDAO.findQuizById(quizId);
|
|
|
-// if (quiz.getState() != QuizState.NOT_STARTED) {
|
|
|
-// throw HelperException.of(ExceptionType.FORBIDDEN, "不能删除已经开始的测试!");
|
|
|
-// }
|
|
|
-// quizDAO.delete(quiz);
|
|
|
-// return "delete success";
|
|
|
-// }
|
|
|
-//
|
|
|
-// @Override
|
|
|
-// public Page<Quiz> getAllBasicQuizByUser(Pageable pageable, QuizState state, Long uid) {
|
|
|
-// User user = userDAO.findUserById(uid);
|
|
|
-// //todo: 这里写的太狗屎了,可以用重写sql或者用实例查询
|
|
|
-// if (user.getType().equals(UserType.STUDENT)) {
|
|
|
-// final Set<Long> 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 List<QuizResultVO> getQuizResult(Long quizId) {
|
|
|
-// Assert.notNull(quizId, "错误的测试!");
|
|
|
-// final Quiz quiz = quizDAO.findQuizById(quizId);
|
|
|
-// 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<>();
|
|
|
-// 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(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<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> quizStudentAnswers = 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) {
|
|
|
-// if (qsaMap.get(id).getStudentAnswer().equals(answer)) {
|
|
|
-// continue;
|
|
|
-// }
|
|
|
-// qsaMap.get(id).setStudentAnswer(answer);
|
|
|
-// System.out.println(answer);
|
|
|
-// quizStudentAnswers.add(qsaMap.get(id));
|
|
|
-// } else {
|
|
|
-// QuizStudentAnswer qsa = QuizStudentAnswer.builder()
|
|
|
-// .questionId(id).quiz(quiz).student(student).studentAnswer(answer)
|
|
|
-// .build();
|
|
|
-// quizStudentAnswers.add(qsa);
|
|
|
-// }
|
|
|
-//
|
|
|
-// }
|
|
|
-// Integer savedNum = quizStudentAnswers.size();
|
|
|
-// quizStudentAnswerDAO.saveAll(quizStudentAnswers);
|
|
|
-// return savedNum;
|
|
|
-// }
|
|
|
-//
|
|
|
-// @Override
|
|
|
-// public Page<Quiz> getAllBasicQuizBySlide(Long slideId, Pageable pageable, QuizState state, Long uid) {
|
|
|
-// 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);
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-// @Override
|
|
|
-// @Transactional(rollbackFor = Exception.class)
|
|
|
-// 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);
|
|
|
-// } else {
|
|
|
-// //持久化后,不仅仅需要推进状态,还需要判分,保存等步骤
|
|
|
-// if (slideTime < quizTime) {
|
|
|
-// if (quiz.getState().equals(QuizState.NOT_STARTED)) {
|
|
|
-// return QuizState.NOT_STARTED;
|
|
|
-// }
|
|
|
-// //未开始
|
|
|
-// quiz.setState(QuizState.NOT_STARTED);
|
|
|
-// } 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);
|
|
|
-// final Set<Long> studentIds = chooseDAO.findStudentIdsByCourseId(slide.getCourse().getId());
|
|
|
-// Student2QuestionId2Answer triadMap = new Student2QuestionId2Answer(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, Long slideId) {
|
|
|
-// Quiz quiz = quizDAO.findQuizById(quizId);
|
|
|
-// Slide slide = slideDAO.findSlideById(slideId);
|
|
|
-// return checkQuizState(quiz, slide);
|
|
|
-// }
|
|
|
-//
|
|
|
-// 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);
|
|
|
-// student2QuestionId2Answer.put(studentId, questionId2Answer);
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-// void addAnswer(QuizStudentAnswer answers) {
|
|
|
-// Map<String, QuizStudentAnswer> stringQuizStudentAnswerMap = student2QuestionId2Answer.get(answers.getStudent().getId());
|
|
|
-// if (stringQuizStudentAnswerMap == null) {
|
|
|
-// return;
|
|
|
-// }
|
|
|
-// stringQuizStudentAnswerMap.put(answers.getQuestionId(), answers);
|
|
|
-// }
|
|
|
-//
|
|
|
-// void fullAnswer(Quiz quiz) {
|
|
|
-// List<String> questions = quiz.getQuestions();
|
|
|
-// student2QuestionId2Answer.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);
|
|
|
-// }
|
|
|
-// }
|
|
|
-// }
|
|
|
-// });
|
|
|
-// }
|
|
|
-//
|
|
|
-// 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 (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();
|
|
|
-// 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);
|
|
|
-// });
|
|
|
-// }
|
|
|
-//
|
|
|
-// List<QuizStudentAnswer> getAll() {
|
|
|
-// List<QuizStudentAnswer> list = new ArrayList<>();
|
|
|
-// for (Long sid : student2QuestionId2Answer.keySet()) {
|
|
|
-// list.addAll(student2QuestionId2Answer.get(sid).values());
|
|
|
-// }
|
|
|
-// return list;
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-// @Override
|
|
|
-// public QuizVO combineQuiz(Long quizId) {
|
|
|
-// Assert.notNull(quizId, "错误的测试!");
|
|
|
-// final Quiz quizById = quizDAO.findQuizById(quizId);
|
|
|
-// final List<BaseQuestionVO> questionVoS = bokUtil.bokFindByIdIn(quizById.getQuestions());
|
|
|
-// return new QuizVO(quizById, questionVoS);
|
|
|
-// }
|
|
|
-//
|
|
|
-// @Override
|
|
|
-// public QuizVO combineQuizWithAnswer(Long quizId, Long uid, UserType userType) {
|
|
|
-// Assert.notNull(quizId, "错误的测试!");
|
|
|
-// final Quiz quizById = quizDAO.findQuizById(quizId);
|
|
|
-// User student = new User();
|
|
|
-// student.setId(uid);
|
|
|
-//
|
|
|
-// if (userType == null || userType.equals(UserType.TEACHER)) {
|
|
|
-// //向老师展示
|
|
|
-// List<BaseQuestionVO> questionVoS = bokUtil.bokFindByIdIn(quizById.getQuestions());
|
|
|
-// return new QuizVO(quizById, questionVoS);
|
|
|
-// }
|
|
|
-// if (quizById.getState().equals(QuizState.NOT_STARTED)) {
|
|
|
-// //未开始看不到题目
|
|
|
-// return new QuizVO(quizById);
|
|
|
-// }
|
|
|
-//
|
|
|
-// 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 (BaseQuestionVO vo : questionVos) {
|
|
|
-// if (qsaMap.get(vo.getQuestionId()) != null) {
|
|
|
-// vo.exAddAnswer(qsaMap.get(vo.getQuestionId()));
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-// return new QuizVO(quizById, questionVos);
|
|
|
-// }
|
|
|
-//
|
|
|
-// private void checkSameName(Long quizId, Long slideId, String name) {
|
|
|
-// long exists = quizDAO.count((Specification<Quiz>) (root, query, cb) ->
|
|
|
-// cb.and(cb.notEqual(root.get("id"), quizId),
|
|
|
-// cb.equal(root.get("slide").get("id"), slideId),
|
|
|
-// cb.equal(root.get("name"), name)
|
|
|
-// ));
|
|
|
-// if (exists > 0) {
|
|
|
-// throw HelperException.of(ExceptionType.CONFLICT, "该测试名已使用");
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-// private void checkQuizTypeOkToSlideState(SlideState slideState, QuizType quizType) {
|
|
|
-// if (!QuizType.getQuizTypesBySlideState(slideState).contains(quizType)) {
|
|
|
-// throw HelperException.of(ExceptionType.FORBIDDEN, "课件暂时不支持创建该类型的测试,请检查课件状态");
|
|
|
-// }
|
|
|
-// }
|
|
|
+ private Page<Quiz> findBySlideIdAndStateNotInAndKey(Long slideId, Set<QuizState> exclusiveStates, String key, Pageable pageable) {
|
|
|
+ return quizDAO.findAll(
|
|
|
+ (Specification<Quiz>) (root, query, cb) -> cb.and(
|
|
|
+ cb.equal(root.get("slide").get("id"), slideId),
|
|
|
+ cb.equal(root.get("deleteAt"), 0L),
|
|
|
+ cb.like(root.get("name"), StringUtils.keyPattern(key)),
|
|
|
+ cb.not(root.get("state").in(exclusiveStates))),
|
|
|
+ pageable);
|
|
|
+ }
|
|
|
}
|