package com.java1234.action;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.java1234.dao.ExamDao;
import com.java1234.dao.QuestionDao;
import com.java1234.model.Exam;
import com.java1234.model.PageBean;
import com.java1234.model.Question;
import com.java1234.model.Student;
import com.java1234.util.PageUtil;
import com.java1234.util.PropertiesUtil;
import com.java1234.util.StringUtil;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 考试Action类
 * @author Administrator
 *
 */
public class ExamAction extends ActionSupport implements ServletRequestAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private ExamDao examDao=new ExamDao();
	private QuestionDao questionDao=new QuestionDao();
	
	private HttpServletRequest request;
	
	private String mainPage;
	
	private Exam exam;
	private Exam s_exam;
	
	private List<Exam> examList;
	
	private String page;
	private int total;
	private String pageCode;

	public String getMainPage() {
		return mainPage;
	}

	public void setMainPage(String mainPage) {
		this.mainPage = mainPage;
	}

	public Exam getExam() {
		return exam;
	}

	public void setExam(Exam exam) {
		this.exam = exam;
	}
	
	
	
	public List<Exam> getExamList() {
		return examList;
	}

	public void setExamList(List<Exam> examList) {
		this.examList = examList;
	}
	
	
	

	public Exam getS_exam() {
		return s_exam;
	}

	public void setS_exam(Exam s_exam) {
		this.s_exam = s_exam;
	}
	
	

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public String getPageCode() {
		return pageCode;
	}

	public void setPageCode(String pageCode) {
		this.pageCode = pageCode;
	}

	/**
	 * 计算/添加考试成绩
	 * @return
	 * @throws Exception
	 */
	public String add()throws Exception{
		Map<String, String[]> keyMap = new HashMap<String, String[]>();
        keyMap = request.getParameterMap();
        Iterator<Entry<String,String[]>> it2 = keyMap.entrySet().iterator();
        int totalScore=0;
        int singleScore=0;
        int moreScore=0;
        while (it2.hasNext()) {
            Entry<String, String[]> entry = it2.next();  
            String keyStr=entry.getKey();
            String values[]=entry.getValue();
            String key;
            String value="";
            if(keyStr.equals("exam.student.id")||keyStr.equals("exam.paper.id")){
            	continue;
            }
            if(keyStr.split("-")[1].equals("r")){  // 单选
            	key=keyStr.split("-")[2];
            	value=values[0];
            	singleScore+=this.calScore(key, value, "1");
            }else{  // 多选
            	key=keyStr.split("-")[2];
            	for(String s:values){
            		value+=s+",";
            	}
            	value=value.substring(0,value.length()-1);
            	moreScore+=this.calScore(key, value, "2");
            }
        }
        totalScore=singleScore+moreScore;
        exam.setSingleScore(singleScore);
        exam.setMoreScore(moreScore);
        exam.setScore(totalScore);
        exam.setExamDate(new Date());
        examDao.saveExam(exam);
		mainPage="exam/examResult.jsp";
		return SUCCESS;
	}
	
	/**
	 * 计算每道题目的得分
	 * @param questionId
	 * @param userAnswer
	 * @return
	 */
	private int calScore(String questionId,String userAnswer,String type)throws Exception{
		Question question=questionDao.getQuestion(questionId);
		if(userAnswer.equals(question.getAnswer())){
			if("1".equals(type)){
				return 20;
			}else{
				return 30;
			}
		}else{
			return 0;
		}
	}
	
	/**
	 * 获取考试成绩
	 * @return
	 * @throws Exception
	 */
	public String getExams()throws Exception{
		examList=examDao.getExams(s_exam,null);
		mainPage="exam/myExam.jsp";
		return SUCCESS;
	}
	
	/**
	 * 获取所有考试成绩
	 * @return
	 * @throws Exception
	 */
	public String examList()throws Exception{
		HttpSession session=request.getSession();
		if(StringUtil.isEmpty(page)){
			page="1";
		}
		if(s_exam!=null){
			session.setAttribute("s_exam", s_exam);
		}else{
			Object o=session.getAttribute("s_exam");
			if(o!=null){
				s_exam=(Exam)o;
			}else{
				s_exam=new Exam();
			}
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(PropertiesUtil.getValue("pageSize")));
		examList=examDao.getExams(s_exam,pageBean);
		total=examDao.examCount(s_exam);
		pageCode=PageUtil.genPagation(request.getContextPath()+"/exam!examList",total, Integer.parseInt(page), Integer.parseInt(PropertiesUtil.getValue("pageSize")));
		mainPage="exam/examList.jsp";
		return SUCCESS;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request=request;
	}
}
最近下载更多
weilaizhisheng  LV21 2023年9月16日
似鹤不是鹤似鹤不是鹤  LV1 2023年5月5日
sswert  LV2 2023年5月4日
amour1  LV11 2023年5月3日
tujf13763823976  LV3 2023年3月28日
fu8867  LV2 2023年2月20日
wubinbin  LV11 2023年2月11日
shamexidan  LV9 2022年5月20日
shibinf  LV3 2022年4月8日
and123456  LV11 2022年2月7日
最近浏览更多
pokerf  LV5 4月7日
taoshen95  LV14 2月29日
gjw19884260466  LV3 1月8日
WBelong  LV7 2023年12月11日
cpla1998  LV8 2023年11月28日
zhxhcj  LV8 2023年11月26日
hhhh别卷了 2023年10月20日
暂无贡献等级
186092  LV4 2023年9月20日
weilaizhisheng  LV21 2023年9月16日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友