這篇文章主要介紹“Java Online Exam在線考試系統怎么實現”,在日常操作中,相信很多人在Java Online Exam在線考試系統怎么實現問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”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在線考試系統怎么實現”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。