Java implements online examination system (student function)

The student module has relatively few functions, including two major functions: taking exams and checking your own grades.

The function of students taking the test is more complicated (the verification is done first, it is not within the time frame of the test, and the test cannot be carried out)
write picture description here

write picture description here
An exam question is a set of questions randomly generated by the database according to the conditions when the exam is issued. Because the number of questions in each exam is different, we continue to use JSON format to save the data. When the status is 1: it means the exam is in progress; the status is 2: the exam has ended; the status is 3: the teacher has read After the test paper
(1) When the test is taken, a set of test questions will be randomly generated for the test and stored in the database. If the computer suddenly breaks down during the test, it can be guaranteed to be reopened or the previous test questions
(2) When the test time ends, it will be automatically After submitting the test, the test questions with the student's grades will be saved to the database, and the teacher will read the test paper from the JSON
. The score of each question
(the countdown to the test is calculated based on the test time and the length of the test. If the test ends at 4:00 pm and the student logs in to the system at 3:30, there will only be half an hour to take the test)
write picture description here

@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
	
	@RequestMapping("/list")
	public String showExam(HttpServletRequest request,Integer curr, Model model){
		if (curr == null){
			curr = 1;
		}
		HttpSession session = request.getSession();
		Student student = (Student) session.getAttribute("studentInfo");
		Integer classesid = student.getClassesid();
		if (classesid != null){
			PageInfo<Publishexam> publishexamList = publishExamService.selectAll(classesid, curr, 5);
			model.addAttribute("publishexamList", publishexamList);
		}
		model.addAttribute("studentInfo", student);
		return "/exam/list";
	}
	
	//进入考试页面,初始化数据
	@SuppressWarnings("unchecked")
	@RequestMapping("/exam")
	public String exam(HttpServletRequest request,int id,Model model){
		
		//0表示0.5小时,1表示1小时,2表示1.5小时,3表示2小时,4表示2.5小时,5表示3小时
		//考试时间所对应的毫秒数
		long[] dateLong = {30*60*1000, 60*60*1000, 90*60*1000, 120*60*1000, 150*60*1000, 180*60*1000};
		
		PublishexamWithBLOBs publishexam = publishExamService.selectByKey(id);
		HttpSession session = request.getSession();
		Student student = (Student) session.getAttribute("studentInfo");
		
		//在考试记录表中添加一条考试信息
		Examhis examhis = examhisService.studentIsNotExam(student.getId(), publishexam.getSubjectId(), publishexam.getExamtime());
		List<Text> textList = null;
		ArrayList<TextModel> list = JsonUtils.toBean(publishexam.getExam(), ArrayList.class, TextModel.class);
		Date date = new Date();
		Date date2 = new Date();
		date2.setTime(publishexam.getExamtime().getTime() + dateLong[publishexam.getExamlength()]);
		//当前时间如果在考试时间之前,考试还未开始
		if (date.compareTo(publishexam.getExamtime()) <= 0){
			model.addAttribute("message", "对不起当前考试还未开始!");
			return "/message";
		}
		
		/*1.当前时间>考试时间+考试时长,考试已经结束
		  2.examhis=null,则证明作者第一次点击开始考试
			如果examhis不为空
				状态为1:表示正在考试
				状态为2:表示已经考试结束,
				状态为3:表示老师已经阅完试卷
		*/
		if (date.compareTo(date2) >= 0 || (examhis != null && examhis.getStatus() != 1)){
			model.addAttribute("message", "对不起当前考试已经结束!");
			return "/message";
		}
		if (examhis == null){
			examhis = new Examhis();
			examhis.setStudentid(student.getId());
			examhis.setStudentname(student.getName());
			examhis.setSubjectid(publishexam.getSubjectId());
			examhis.setSubjectname(publishexam.getSubjectName());
			examhis.setClassesid(student.getClassesid());
			examhis.setClassesname(student.getClassesname());
			examhis.setPublishexamid(id);
			examhis.setExamtime(publishexam.getExamtime());
			examhis.setStatus(1);		
			textList = new ArrayList<Text>();		
			for (TextModel textModel : list) {
				List<Text> text = textService.beginExam(textModel,publishexam.getSubjectId());
				for (Text text2 : text) {
					text2.setTexId(textModel.getTextModelId());
				}
				textList.addAll(text);
			}
			String json = JsonUtils.toJson(textList);
			examhis.setExamtest(json);
			examhisService.insert(examhis);
		}else{
			textList = JsonUtils.toBean(examhis.getExamtest(), ArrayList.class, Text.class);
		}
		
		int sum = 0;
		for (TextModel textModel : list) {
			sum += textModel.getGrade()*textModel.getTextCount();
		}
		List<Textmodel> textModelList = textModelService.selectTextModel(list);
		model.addAttribute("textModelList", textModelList);
		model.addAttribute("studentInfo", student);
		model.addAttribute("textList", textList);
		model.addAttribute("publishexam", publishexam);
		model.addAttribute("sum", sum);
		model.addAttribute("examTimeLength", dateLong[publishexam.getExamlength()]/1000);
		model.addAttribute("examTime", (date2.getTime() - date.getTime())/1000);
		return "/exam/exam";
	}
	
	//提交考试方法
	@RequestMapping(value="/examEnd", method=RequestMethod.POST)
	@ResponseBody
	public AjaxResult examEnd(HttpServletRequest request, ExamList examList, int id){

		PublishexamWithBLOBs publishexam = publishExamService.selectByKey(id);
		HttpSession session = request.getSession();
		Student student = (Student) session.getAttribute("studentInfo");
		//将考试信息转成json,改变考试状态,更新到考试历史记录表中
		Examhis examhis = examhisService.studentIsNotExam(student.getId(), publishexam.getSubjectId(), publishexam.getExamtime());
		List<Exam> list = examList.getExamList();
		list.remove(0);
		for (Exam exam : list) {
			exam.setText(textService.selectOne(exam.getTextId()));
			if (exam.getAnswer() == null){
				exam.setAnswer(" ");
			}
		}
		String json = JsonUtils.toJson(list);
		examhis.setExamtest(json);
		examhis.setStatus(2);
		examhisService.update(examhis);
		return AjaxResult.successInstance("您已成功提交考试");
	}

 <script type="text/javascript">
 //JS实现倒计时,考试结束提交试卷
    function startTime(){  
    	//定义考试剩余时间,时间为毫秒数
        //examTime = "${examTime}";//对考试剩余时间赋值
    	//var exam = parseInt("${examTime}");
    	var examTime = parseInt(examTimes.innerHTML);  
	    var examTimeLength;//考试时长
	    examTimeLength = "${examTimeLength}"; 
	    if ((examTime)<0){
	    	alert("考试时间到!\n即将提交试卷!");
	    	document.forms[0].submit();
	    }else{
	    	//var lm = Math.floor((examTimeLength - examTime) / 60000);
	    	var lh = Math.floor(((examTimeLength - examTime) / 3600) % 60)
	    	var lm = Math.floor(((examTimeLength - examTime) / 60) % 60);
	    	var ls = (examTimeLength - examTime) % 60;
	    	var yh = Math.floor((examTime / 3600) % 60);
	    	var ym = Math.floor((examTime / 60) % 60);
	    	var ys = examTime % 60;
	       document.getElementById("tTime").innerHTML = "考试已经开始了" + lh + "时" + lm + "分" + ls + "秒" + ",剩余" + yh + "时"  + ym + "分" + ys + "秒";
	    }
	    examTime--;
	    examTimes.innerHTML=examTime;
	    setTimeout(startTime,1000);
    }
    
    var timer=null;
    //当页面加载后,启动周期性定时器,每过1秒执行startTime
    window.onload=function(){
    	startTime();
    }
    </script>

Because it is relatively simple to query the results, that is, to query the results of students and display them on the page, and the results of the ongoing exam are empty.
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324187463&siteId=291194637