Java Online Exam在线考试系统的实现

一、项目简述

本系统主要实现的功能有: 学生以及老师的注册登录,在线考试,错题查询,学生管理,问题管理,错题管理,错题查询,分数查询,试卷管 理,人工组卷。自动组卷,教师,班级,统计等等管理功能。

二、项目运行

环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe (IntelliJ IDEA,Eclispe,MyEclispe,Sts 都支持)

项目技术: VUE+Springboot+ SpringMVC + MyBatis + ThymeLeaf + JavaScript + JQuery + Ajax + maven等等

课程信息控制器:


/**
 * yy
 */
@RestController
@RequestMapping(value = "/v1/subjects")
public class SubjectController {

    private static Logger logger = LoggerFactory.getLogger(SubjectController.class);

    @Autowired
    SubjectService subjectService;

    @ApiOperation(value = "获取科目列表", notes = "")
    @RequestMapping(value = "", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public PageInfo<Subject> getSubjectList(@RequestParam(required = false) Integer pageIndex,
                                        @RequestParam(required = false) Integer pageSize,
                                        @RequestParam(required = false) Integer limit,
                                        @RequestParam(required = false) Integer offset) {
        if(pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        List<Subject> subjects = subjectService.getSubjectList();
        PageInfo pageInfo = new PageInfo(subjects);
        return pageInfo;
    }

    @ApiOperation(value = "根据名字获取科目信息", notes = "根据科目名称获取科目详细信息")
    @ApiImplicitParam(name = "name", value = "科目名称", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "/{name}/name", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public List<Subject> getSubjectByName(@PathVariable String name) {
        return subjectService.getSubjectFuzzy(name);
    }

    @ApiOperation(value = "获取课程信息", notes = "根据课程id获取课程详细信息")
    @ApiImplicitParam(name = "idOrName", value = "课程ID或名称", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "/search/{idOrName}", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public List<Subject> getSubjectForSearch(@PathVariable String idOrName) {
        List<Subject> subjects = new ArrayList<Subject>();
        Subject subject = subjectService.getSubjectByName(idOrName);
        if (subject == null) {
            try {
                subject = subjectService.getSubjectById(idOrName);
            } catch (Exception e) {

            }
        }
        if (subject != null) {
            subjects.add(subject);
        }
        return subjects;
    }

    @ApiOperation(value = "创建课程", notes = "创建课程")
    @ApiImplicitParam(name = "subject", value = "课程实体Subject", required = true, dataType = "Subject")
    @RequestMapping(value = "", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public ResponseEntity<?> postSubject(@RequestBody Subject subject) {
        if(subjectService.getSubjectByName(subject.getName()) != null) {
            return new ResponseEntity<Object>(new Dto("课程已存在!"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        subjectService.saveSubject(subject);
        return new ResponseEntity(HttpStatus.CREATED);
    }

    @ApiOperation(value = "获取课程信息", notes = "根据课程id获取课程详细信息")
    @ApiImplicitParam(name = "id", value = "课程ID", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public Subject getSubject(@PathVariable String id) {
        return subjectService.getSubjectById(id);
    }

    @ApiOperation(value = "更新课程信息", notes = "根据课程id更新用户信息")
    @ApiImplicitParam(name = "subject", value = "课程实体", required = true, dataType = "Subject")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public ResponseEntity<?> putSubject(@RequestBody Subject subject) {
        subjectService.updateSubject(subject);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation(value = "删除课程", notes = "根据课程id删除课程")
    @ApiImplicitParam(name = "id", value = "课程ID", required = true, dataType = "Long", paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public ResponseEntity<?> deleteSubject(@PathVariable String id) {
        try {
            subjectService.deleteSubject(id);
        }catch (RuntimeException e) {
            return new ResponseEntity(new Dto("该课程包含有考试,不能删除"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(HttpStatus.OK);
    }
}
 

题目信息控制器:

/**
 * yy
 */

@RestController
@RequestMapping(value = "/v1/questions")
public class QuestionController {

    private static Logger logger = LoggerFactory.getLogger(QuestionController.class);

    @Autowired
    QuestionService questionService;

    @Autowired
    PaperAnswerPaperService paperAnswerPaperService;

    @ApiOperation(value = "获取题目分页列表", notes = "")
    @RequestMapping(value = "", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public PageInfo<Question> getQuestionListByPage(@RequestParam(required = false) Integer pageIndex,
                                                    @RequestParam(required = false) Integer pageSize,
                                                    @RequestParam(required = false) Integer limit,
                                                    @RequestParam(required = false) Integer offset) {
        if(pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        List<Question> questions = questionService.getQuestionList();
        PageInfo pageInfo = new PageInfo(questions);
        return pageInfo;
    }

    @ApiOperation(value = "获取试卷题目分页列表", notes = "")
    @RequestMapping(value = "/papers/{paperId}", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public PageInfo<Question> getQuestionListByPaper(@PathVariable String paperId,
                                                     @RequestParam(required = false) Integer pageIndex,
                                                     @RequestParam(required = false) Integer pageSize,
                                                     @RequestParam(required = false) Integer limit,
                                                     @RequestParam(required = false) Integer offset) {
        if(pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        List<Question> questions = questionService.getQuestionListByPaper(paperId);
        PageInfo pageInfo = new PageInfo(questions);
        return pageInfo;
    }

    @ApiOperation(value = "获取试卷题目数量", notes = "")
    @RequestMapping(value = "/papers/{paperId}/count", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "') or hasAuthority('" + Role.ROLE_STUDENT + "')")
    public ResponseEntity<?> getQuestionCountByPaper(@PathVariable String paperId) {
        Integer count = questionService.countByPaperId(paperId);
        return new ResponseEntity<Object>(count, HttpStatus.OK);
    }

    @ApiOperation(value = "创建题目", notes = "创建题目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "question", value = "题目实体Question", required = true, dataType = "Question"),
            @ApiImplicitParam(name = "id", value = "试卷id", required = true, dataType = "String", paramType = "path")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public ResponseEntity<?> postQuestion(@PathVariable("id") String id, @RequestBody Question question) {
        questionService.saveQuestion(id, question);
        return new ResponseEntity(HttpStatus.CREATED);
    }

    @ApiOperation(value = "获取题目信息", notes = "根据题目id获取题目详细信息")
    @ApiImplicitParam(name = "id", value = "题目ID", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "') or hasAuthority('" + Role.ROLE_STUDENT + "')")
    public Question getQuestion(@PathVariable String id) {
        return questionService.getQuestion(id);
    }

    @ApiOperation(value = "根据试卷id和题目编号获取题目信息", notes = "根据题目id获取题目详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paperId", value = "试卷ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "number", value = "题目编号", required = true, dataType = "String", paramType = "path")
    })
    @RequestMapping(value = "/papers/{paperId}/questions/{number}", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "') or hasAuthority('" + Role.ROLE_STUDENT + "')")
    public Question getQuestionByPaperIdAndQuestionId(@PathVariable String paperId,
                                                      @PathVariable Integer number,
                                                      @RequestParam(required = false) String answerPaperId) {
        PaperAnswerPaper paperAnswerPaper = null;
        //传入的是答卷Id
        if(answerPaperId != null) {
            // TODO: 2017-04-17
            paperAnswerPaper = paperAnswerPaperService.getByAnswerPaperId(answerPaperId);
            if(paperAnswerPaper != null) {
                return questionService.getQuestionByPaperIdAndQuestionNumber(paperAnswerPaper.getPaperId(), number);
            }else {
                logger.error("根据答卷id获取答卷失败");
            }
        }
        return questionService.getQuestionByPaperIdAndQuestionNumber(paperId, number);
    }

    @ApiOperation(value = "获取题目信息", notes = "根据题目name获取题目详细信息")
    @ApiImplicitParam(name = "name", value = "试卷name", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "/name/{name}", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "') or hasAuthority('" + Role.ROLE_STUDENT + "')")
    public List<Question> getQuestionByName(@PathVariable String name) {
        //模糊查询
        return questionService.getQuestionFuzzy(name);
    }

    @ApiOperation(value = "获取题目信息", notes = "根据试卷id获取所有题目")
    @ApiImplicitParam(name = "paperId", value = "试卷ID", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "/papers/{paperId}/questions", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "') or hasAuthority('" + Role.ROLE_STUDENT + "')")
    public List<Question> getQuestionByPaperId(@PathVariable String paperId) {
        return questionService.getQuestionByPaperId(paperId);
    }

    @ApiOperation(value = "获取题目信息", notes = "根据试卷id获取所有题目,但不返回答案")
    @ApiImplicitParam(name = "paperId", value = "试卷ID", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "/papers/{paperId}/ignore", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "') or hasAuthority('" + Role.ROLE_STUDENT + "')")
    public List<Question> getQuestionByPaperIdIgnoreAnswer(@PathVariable String paperId) {
        return questionService.getQuestionByPaperIdIgnoreAnswer(paperId);
    }

    @ApiOperation(value = "更新题目信息", notes = "根据题目id更新题目信息")
    @ApiImplicitParam(name = "question", value = "题目实体", required = true, dataType = "Question")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public ResponseEntity<?> putQuestion(@RequestBody Question question) {
        questionService.updateQuestion(question);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation(value = "删除题目", notes = "根据题目id删除试卷")
    @ApiImplicitParam(name = "id", value = "题目ID", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public ResponseEntity<?> deleteQuestion(@PathVariable String id) {
        questionService.deleteQuestion(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}

考试控制层,负责试卷提交等:

/**
 * 考试控制层,负责试卷提交等
 */
@RestController
@RequestMapping("/v1/exam")
public class ExamController {

    @Autowired
    ExamService examService;

    @Autowired
    AnswerPaperService answerPaperService;

    @Autowired
    AnswerQuestionService answerQuestionService;

    @Autowired
    AnswerPaperQuestionService answerPaperQuestionService;

    @Autowired
    QuestionService questionService;

    @Autowired
    PaperService paperService;

    @Autowired
    WrongQuestionService wrongQuestionService;

    @Autowired
    PaperAnswerPaperService paperAnswerPaperService;

    @ApiOperation(value = "根据试卷id和题目编号获取题目信息", notes = "根据题目id获取题目详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paperId", value = "试卷ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "number", value = "题目编号", required = true, dataType = "String", paramType = "path")
    })
    @RequestMapping(value = "/questions/{number}", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "') or hasAuthority('" + Role.ROLE_STUDENT + "')")
    public Question getQuestionByPaperIdAndQuestionId(@RequestParam String paperId,
                                                      @RequestParam String username,
                                                      @RequestParam(required = false) String answerPaperId,
                                                      @PathVariable Integer number) {
        Question question = null;
        AnswerQuestion answerQuestion = null;
        if(answerPaperId == null) {
            Paper paper = paperService.getPaperById(paperId);
            if(paper != null) {
                AnswerPaper answerPaper = answerPaperService.findByAnswerUserAndPaperName(username, paper.getName());
                if(answerPaper != null) {
                    answerQuestion = answerQuestionService.getAnswerQuestionByPaperIdAndQuestionNumber(answerPaper.getId(), number);
                }
            }
        }else {
            answerQuestion = answerQuestionService.getAnswerQuestionByPaperIdAndQuestionNumber(answerPaperId, number);
        }

        if(answerQuestion == null) {
            question = questionService.getQuestionByPaperIdAndQuestionNumber(paperId, number);
            if(question != null) {
                //答案不返回
                question.setAnswer("");
            }
        } else {
            question = new Question();
            question.setId(answerQuestion.getId());
            question.setNumber(answerQuestion.getNumber());
            question.setTitle(answerQuestion.getTitle());
            question.setScore(answerQuestion.getScore());
            question.setType(answerQuestion.getType());
            question.setOptionA(answerQuestion.getOptionA());
            question.setOptionB(answerQuestion.getOptionB());
            question.setOptionC(answerQuestion.getOptionC());
            question.setOptionD(answerQuestion.getOptionD());
            question.setAnswer(answerQuestion.getAnswer());
        }
        return question;
    }

    @RequestMapping(value = "/submit/{type}/{username}", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "') or hasAuthority('" + Role.ROLE_STUDENT + "')")
    public ResponseEntity<?> submit(@RequestBody Paper paper, @PathVariable String type,
                                    @PathVariable String username,
                                    @RequestParam(required = false) String answerPaperId) {
        /**
         * 更改试卷状态,finished:true
         */
        if(type.equals("official")) {
            /**
             * 正式考试
             */
            AnswerPaper answerPaper = new AnswerPaper();
            if(answerPaperId != null) {
                answerPaper.setId(answerPaperId);
            }else {
                return new ResponseEntity<Object>(HttpStatus.INTERNAL_SERVER_ERROR);
            }
            answerPaper.setAnswerTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            answerPaper.setPaperName(paper.getName());
            answerPaper.setAnswerUser(username);
            answerPaper.setChecked("false");
            answerPaper.setFinished("true");
            answerPaper.setType("official");
            examService.updateAnswerPaper(answerPaper);
        } else if(type.equals("simulate")) {
            /**
             * 模拟考试
             */
            AnswerPaper answerPaper = new AnswerPaper();
            if(answerPaperId != null) {
                answerPaper.setId(answerPaperId);
            }else {
                return new ResponseEntity<Object>(HttpStatus.INTERNAL_SERVER_ERROR);
            }
            answerPaper.setAnswerTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            answerPaper.setPaperName(paper.getName());
            answerPaper.setAnswerUser(username);
            answerPaper.setChecked("false");
            answerPaper.setFinished("true");
            answerPaper.setType("simulate");
            examService.updateAnswerPaper(answerPaper);
        }else if(type.equals("practice")) {
            /**
             * 1.接收提交的试卷
             * 2.计算成绩
             * 3.记录考试记录
             * 4.返回计算结果
             */
            int score = 0;

            //正确题目数
            double right = 0.0;

            //错误题目数
            double wrong = 0.0;

            double correctRate = 0.0;

            List<Question> questions = questionService.getQuestionByPaperId(paper.getId());

            AnswerPaper answerPaper = answerPaperService.findByAnswerUserAndPaperName(username, paper.getName());

            List<AnswerQuestion> answerQuestions = answerQuestionService.findByAnswerPaperId(answerPaper.getId());

            /*保存题目信息,返回给前端*/
            List<DtoRightAndWrong> results = new ArrayList<DtoRightAndWrong>();

            DtoRightAndWrong dtoRightAndWrong = null;

            //遍历提交的试卷的题目
            for(AnswerQuestion answerQuestion : answerQuestions) {

                //遍历包含正确答案的题目
                for(Question question : questions) {
                    /**
                     * 1.题目序号相同
                     * 2.结果与答案相同
                     */
                    if(answerQuestion.getNumber().equals(question.getNumber())) {
                        if(answerQuestion.getAnswer().equals(question.getAnswer())) {
                            /*累计得分*/
                            score += Integer.parseInt(question.getScore());
                            right ++;
                        }else {
                            wrong ++;
                            //记录错题
                            dtoRightAndWrong = new DtoRightAndWrong();
                            dtoRightAndWrong.setQuestion(question);
                            dtoRightAndWrong.setAnswerQuestion(answerQuestion);
                            results.add(dtoRightAndWrong);

                            //保存错题
                            WrongQuestion wrongQuestion = new WrongQuestion();
                            try{
                                BeanUtils.copyProperties(wrongQuestion, answerQuestion);
                                wrongQuestion.setUsername(username);
                                wrongQuestion.setRightAnswer(question.getAnswer());
                                wrongQuestion.setAnalysis(question.getAnalysis());
                                if(wrongQuestionService.getWrongQuestion(wrongQuestion.getId()) == null) {
                                    wrongQuestionService.saveQuestion(wrongQuestion);
                                }
                            }catch (Exception e) {
                                System.out.println(wrongQuestion.toString());
                            }

                        }
                    }
                }
            }
            //计算正确率
            correctRate = (right/(right + wrong)) * 100;

            DtoResult result = new DtoResult();
            result.setScore(score);
            result.setRight(right);
            result.setWrong(wrong);
            result.setCorrectRate(correctRate);
            result.setResults(results);

            Paper paper1 = paperService.getPaperById(paper.getId());
            //更新参与人数
            paper1.setPeoples(String.valueOf(Integer.parseInt(paper1.getPeoples()) + 1));
            paperService.updatePaper(paper1);

            return new ResponseEntity<Object>(result, HttpStatus.OK);
        }
        Paper paper1 = paperService.getPaperById(paper.getId());
        //更新参与人数
        paper1.setPeoples(String.valueOf(Integer.parseInt(paper1.getPeoples() + 1)));
        paperService.updatePaper(paper1);
        return new ResponseEntity<Object>(HttpStatus.OK);
    }

    /**
     * 提交题目
     * @param username
     * @param dtoAnswerPaper
     * @return
     */
    @RequestMapping(value = "/submit/one/{username}", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "') or hasAuthority('" + Role.ROLE_STUDENT + "')")
    public ResponseEntity<?> submitOne(@PathVariable String username, @RequestBody DtoAnswerPaper dtoAnswerPaper) {
        Paper paper = dtoAnswerPaper.getPaper();
        Question question = dtoAnswerPaper.getQuestion();
        //判断数据库是否保存了这次答卷
        AnswerPaper answerPaper = answerPaperService.getAnswerPaperByNameAndUser(paper.getName(), username);
        AnswerQuestion answerQuestion = null;
        AnswerPaperQuestion answerPaperQuestion = null;
        List<AnswerQuestion> answerQuestions = null;
        //重新生成id
        String answerPaperId = IdGen.uuid();
        String answerQuestionId = IdGen.uuid();
        //答卷为空,则执行保存
        if(answerPaper == null) {
            answerPaper = new AnswerPaper();
            answerPaper.setId(answerPaperId);
            answerPaper.setAnswerTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            answerPaper.setPaperName(paper.getName());
            answerPaper.setType(paper.getType());
            answerPaper.setAnswerUser(username);
            answerPaper.setChecked("false");
            answerPaper.setFinished("false");

            //保存答卷
            answerPaperService.saveAnswerPaper(answerPaper);

            // TODO: 2017-04-17 保存试卷答卷
            PaperAnswerPaper paperAnswerPaper = new PaperAnswerPaper();
            paperAnswerPaper.setPaperId(paper.getId());
            paperAnswerPaper.setAnswerPaperId(answerPaperId);
            paperAnswerPaperService.save(paperAnswerPaper);

            //新记录
            answerQuestion = new AnswerQuestion();
            //初始化信息
            answerQuestion.setId(answerQuestionId);
            answerQuestion.setTitle(question.getTitle());
            answerQuestion.setType(question.getType());
            answerQuestion.setNumber(question.getNumber());
            answerQuestion.setOptionA(question.getOptionA());
            answerQuestion.setOptionB(question.getOptionB());
            answerQuestion.setOptionC(question.getOptionC());
            answerQuestion.setOptionD(question.getOptionD());
            answerQuestion.setContent(question.getContent());
            answerQuestion.setScore(question.getScore());
            answerQuestion.setAnalysis(question.getAnalysis());
            answerQuestion.setAnswer(question.getAnswer());

            answerPaperQuestion = new AnswerPaperQuestion();
            answerPaperQuestion.setAnswerPaperId(answerPaper.getId());
            answerPaperQuestion.setAnswerQuestionId(answerQuestionId);

            //保存
            answerQuestionService.saveAnswerQuestion(answerQuestion);
            answerPaperQuestionService.saveAnswerPaperQuestion(answerPaperQuestion);

            return new ResponseEntity<Object>(answerPaper, HttpStatus.OK);
        } else {
            answerQuestions = answerQuestionService.findByAnswerPaperId(answerPaper.getId());
            if(answerQuestions != null && answerQuestions.size() > 0) {
                int count = 0;
                AnswerQuestion existAnswerQuestion = null;
                for(AnswerQuestion question1 : answerQuestions) {
                    if (question1.getNumber().equals(question.getNumber())) {
                        count++;
                        existAnswerQuestion = question1;//保存当前存在的记录
                    }
                }
                //记录不存在
                if(count == 0) {
                    //新记录
                    answerQuestion = new AnswerQuestion();
                    answerPaperQuestion = new AnswerPaperQuestion();

                    answerQuestion = new AnswerQuestion();
                    //初始化信息
                    answerQuestion.setId(answerQuestionId);
                    answerQuestion.setTitle(question.getTitle());
                    answerQuestion.setType(question.getType());
                    answerQuestion.setNumber(question.getNumber());
                    answerQuestion.setOptionA(question.getOptionA());
                    answerQuestion.setOptionB(question.getOptionB());
                    answerQuestion.setOptionC(question.getOptionC());
                    answerQuestion.setOptionD(question.getOptionD());
                    answerQuestion.setContent(question.getContent());
                    answerQuestion.setScore(question.getScore());
                    answerQuestion.setAnalysis(question.getAnalysis());
                    answerQuestion.setAnswer(question.getAnswer());

                    answerPaperQuestion = new AnswerPaperQuestion();
                    answerPaperQuestion.setAnswerPaperId(answerPaper.getId());
                    answerPaperQuestion.setAnswerQuestionId(answerQuestionId);

                    //保存
                    answerQuestionService.saveAnswerQuestion(answerQuestion);
                    answerPaperQuestionService.saveAnswerPaperQuestion(answerPaperQuestion);
                } else {
                    //记录存在,则执行更新
                    // TODO: 2017/3/30
                    //更新当前存在的记录
                    existAnswerQuestion.setAnswer(question.getAnswer());

                    answerQuestionService.updateAnswerQuestion(existAnswerQuestion);
                }
            }
        }
        return new ResponseEntity<Object>(answerPaper, HttpStatus.OK);
    }
}

答卷控制层,用于获取已经提交的答卷:

/**
 * 答卷控制层,用于获取已经提交的答卷
 */
@RestController
@RequestMapping("/v1/answer-papers")
public class AnswerPaperController {

    @Autowired
    AnswerPaperService answerPaperService;

    @Autowired
    AnswerQuestionService answerQuestionService;

    /**
     * 根据ID查找
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "') or hasAuthority('" + Role.ROLE_STUDENT + "')")
    public AnswerPaper getAnswerPaper(@PathVariable String id) {
        return answerPaperService.getAnswerPaperById(id);
    }

    /**
     * 根据name查找
     * @param name
     * @return
     */
    @RequestMapping(value = "/name/{name}", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public List<AnswerPaper> getAnswerPaperByName(@PathVariable String name) {
        return answerPaperService.getAnswerPaperFuzzy(name);
    }

    /**
     * 根据答卷id和题目编号获取题目信息
     * @param paperId
     * @param number
     * @return
     */
    @RequestMapping(value = "/papers/{paperId}/questions/{number}", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "') or hasAuthority('" + Role.ROLE_STUDENT + "')")
    public AnswerQuestion getQuestionByPaperIdAndQuestionId(@PathVariable String paperId, @PathVariable Integer number) {
        AnswerQuestion answerQuestion = answerQuestionService.getAnswerQuestionByPaperIdAndQuestionNumber(paperId, number);
        return answerQuestion;
    }

    /**
     * 已分页方式获取数据
     * @param username
     * @param pageIndex
     * @param pageSize
     * @param limit
     * @param offset
     * @return
     */
    @RequestMapping(value = "/users/{username}", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "') or hasAuthority('" + Role.ROLE_STUDENT + "')")
    public PageInfo<AnswerPaper> getListByUser(@PathVariable("username") String username,
                                               @RequestParam(required = false) Integer pageIndex,
                                               @RequestParam(required = false) Integer pageSize,
                                               @RequestParam(required = false) Integer limit,
                                               @RequestParam(required = false) Integer offset) {
        if(pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        List<AnswerPaper> answerPapers = answerPaperService.getAnswerPaperListByAnswerUser(username);
        PageInfo pageInfo = new PageInfo(answerPapers);
        return pageInfo;
    }

    @RequestMapping(value = "/users/{username}/type/{type}", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "') or hasAuthority('" + Role.ROLE_STUDENT + "')")
    public PageInfo<AnswerPaper> getListByUserAndType(@PathVariable("username") String username,
                                                @PathVariable("type") String type,
                                                @RequestParam(required = false) Integer pageIndex,
                                                @RequestParam(required = false) Integer pageSize,
                                                @RequestParam(required = false) Integer limit,
                                                @RequestParam(required = false) Integer offset) {
        if(pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        List<AnswerPaper> answerPapers = answerPaperService.getAnswerPaperListByAnswerUserAndType(username, type);
        PageInfo pageInfo = new PageInfo(answerPapers);
        return pageInfo;
    }

    /**
     * 获取未批改或已批改的答卷数量,
     * @return
     */
    @RequestMapping("/check")
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public DtoTask countUnCheckAnswerPaper() {
        DtoTask dtoTask = new DtoTask();
        Integer checked = answerPaperService.countCheck("true");
        Integer unChecked = answerPaperService.countCheck("false");
        dtoTask.setChecked(checked);
        dtoTask.setUnChecked(unChecked);
        return dtoTask;
    }

    /**
     * 以分页方式获取数据
     * @param pageIndex
     * @param pageSize
     * @param limit
     * @param offset
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public PageInfo<AnswerPaper> getListByUser(@RequestParam(required = false) Integer pageIndex,
                                               @RequestParam(required = false) Integer pageSize,
                                               @RequestParam(required = false) Integer limit,
                                               @RequestParam(required = false) Integer offset) {
        if(pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        List<AnswerPaper> answerPapers = answerPaperService.getAnswerPaperList();
        PageInfo pageInfo = new PageInfo(answerPapers);
        return pageInfo;
    }

    /**
     * 更新
     * @param answerPaper
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public ResponseEntity<?> putPaper(@RequestBody AnswerPaper answerPaper) {
        answerPaperService.updatePaper(answerPaper);
        return new ResponseEntity(HttpStatus.OK);
    }

    /**
     * 计算考试成绩
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/calculate", method = RequestMethod.PUT)
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "') or hasAuthority('" + Role.ROLE_STUDENT + "')")
    public ResponseEntity<?> CalculationScore(@PathVariable String id) {
        /**
         * 计算成绩
         */
        List<AnswerQuestion> questions = answerQuestionService.findByAnswerPaperId(id);
        if(questions != null && questions.size() > 0) {

            int score = 0;
            try {
                for(AnswerQuestion question : questions) {
                    score += Integer.parseInt(question.getMarkScore());
                }
            } catch (Exception e) {
                // TODO: 2017/4/1
            }

            /**
             * 保存成绩
             */

            AnswerPaper answerPaper = new AnswerPaper();
            answerPaper.setId(id);
            answerPaper.setScore(Integer.toString(score));
            answerPaper.setChecked("true");
            answerPaperService.updatePaper(answerPaper);
        } else {
            // TODO: 2017/4/1
        }
        return new ResponseEntity<Object>(HttpStatus.OK);
    }

    @RequestMapping(value = "/analysis/paper")
    @PreAuthorize("hasAuthority('" + Role.ROLE_TEACHER + "') or hasAuthority('" + Role.ROLE_ADMIN + "')")
    public List<PaperAnalysis> analysisPaper() {
        return answerPaperService.analysisPaper();
    }
}

以上就是Java Online Exam在线考试系统的实现的详细内容,更多关于Java 在线考试系统的资料请关注我们其它相关文章!

(0)

相关推荐

  • Java项目实战之在线考试系统的实现(系统介绍)

    1.本系统和现在有的考试系统有以下几种优势: a.和现在有的系统比较起来,本系统有科目.章节.老师.学生.班级等信息的管理,还有批阅试卷查看已批阅试卷等.传统的考试系统划分并不细,业务功能简单. b.和学校的考试系统还有外面的考试系统比较起来,本系统是B/S结构,学校的考试系统一般为C/S结构,性能方面不如B/S结构,并且C/S接口需要安装客户端,客户端压力很大,我的系统只需要电脑具有浏览器,在同一局域网就可以进行考试. c.从架构方面来讲,我们的系统为分布式架构,传统的考试系统没有我们的架构合

  • Java 实战范例之校园二手市场系统的实现

    一.项目简述( +IW文档) 功能:本系统分用户前台和管理员后台. 本系统用例模型有三种,分别是游客.注册用户和系统管 理员.下面分别对这三个角色的功能进行描述: 1) 诞 游客是未注册的用户,他们可以浏览物物品,可以搜索物 品,如需购买物品,必须先注册成为网站用户.游客主要 功触吓: a.浏览物品 b.搜索物品 c.注册成为网站用户 2) 注册用户 注册用户是经过网站合法认证的用户,登录网站后可以浏 览物品.搜索物品.发布物品.关注物品.购买物品和查 看个人中心. 3) 系统管理员 系统管理员

  • Java swing读取txt文件实现学生考试系统

    本文实例为大家分享了Java swing读取txt文件实现学生考试系统的具体代码,供大家参考,具体内容如下 主要实现了一个简单的倒计时答题系统 源码Testquestion 类 public class Testquestion { private String questionText ="";//定义题目 private String standardkey = "";// 定义正确答案 private String selectKey =""

  • Java 实战项目之小说在线阅读系统的实现流程

    一.项目简述 功能包括(管理员和游客角色): 1:用户及主要操作功能 游客可以浏览网站的主页,但是需要注册为会员后部分小 说才能对网络小说进免费行阅读或阅读.用户可以收藏书 架,留言评论,上次阅读,阅读历史,章节选择等等功 能. 2:管理模块 网络小说管理模块包括不同网络小说类别的添加,删除以 及网络小说的上传,删除管理.可以包括武侠小书,都市 言情,穿越小书等各个类等等功能. 二.项目运行 环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe (IntelliJ

  • Java 实战项目之毕业设计管理系统的实现流程

    一.项目简述 功能包括: 该系统不错分为学生,教师,管理员,教导主任四种角 色,包括学生管理,教师管理,学生选题,教师选题,主 任审核,管理员审核,开题报告,中期检查,论文提交, 文件管理等等非常不错. 二.项目运行 环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe (IntelliJ IDEA,Eclispe,MyEclispe,Sts 都支持) 项目技术: JSP +Spring + SpringMVC + MyBatis + html+ css + Ja

  • Java 实战项目之家政服务平台系统的实现流程

    一.项目简述 功能包括: 家政服务网站系统,用户注册,登录,分为家政人员,普 通用户,以及最高管理员,包括家政分类查询,展示,线 上预约服务,家政申请,评论,留言沟通・,联系家政服 务,家政人员的认证,职业认证,以及后台的维护等等功能. 二.项目运行 环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe (IntelliJ IDEA,Eclispe,MyEclispe,Sts 都支持) 项目技术: JSP +Spring + SpringMVC + MyBatis

  • Java 实战图书管理系统的实现流程

    一.项目简述 功能包括(管理员和学生角色): 管理员和学生登录,图书管理,图书添加删除修改,图书 借阅,图书归还,图书查看,学生管理,借还管理,读者 管理,密码管理,信息修改等等功能. 二.项目运行 环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe (IntelliJ IDEA,Eclispe,MyEclispe,Sts 都支持) 项目技术: JSP +Spring + SpringMVC + MyBatis + html+ css + JavaScript

  • Java实现在线考试系统与设计(学生功能)

    学生模块功能比较少,就是进行考试和查看自己成绩两个大的功能. 学生进行考试的功能比较复杂(首先做了校验,不在考试时间范围内,不能进行考试) 考试试题是数据库根据发布考试时的条件随机产生的一套试题.因为每次考试题型题量都是不同的,因此我们继续采用JSON的格式去保存数据,当状态为1:表示正在考试:状态为2:表示已经考试结束:状态为3:表示老师已经阅完试卷 (1)当考试考试的时候,会给考上随机产生一套试题,并存储到数据库中,如果考试中电脑突然坏了可以保证重新打开还是之前的试题 (2)考试时间结束会自

  • Java 实战项目之教材管理系统的实现流程

    一.项目简述 功能包括: 管理员可以增删改查教材.教材商.入库教材.用户(用 户包括学生和教师)可以对教材商.教材进行.Excel的导入 导出操作.教师以领取入库的教材,可以退还教材.学生只能在对应的教师那里领取教材,并且可以退还教材. 查询自己已经领取的教材.并且对已领教材付款等等. 二.项目运行 环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe (IntelliJ IDEA,Eclispe,MyEclispe,Sts 都支持) 项目技术: JSP +Spr

  • Java 实战项目之学生信息管理系统的实现流程

    一.项目简述 功能包括: 用户的登录注册,学生信息管理,教师信息管理,班级信 息管理,采用mvcx项目架构,覆盖增删改查,包括学生, 教币班级的信息导出上传导入等等功能. 二.项目运行 环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe (IntelliJ IDEA,Eclispe,MyEclispe,Sts 都支持) 项目技术: JSP +Spring + SpringMVC + MyBatis + html+ css + JavaScript + JQuer

随机推荐