package org.wolfking.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.wolfking.apputil.DAO;
import org.wolfking.apputil.Factory;
import org.wolfking.apputil.SharePool;
import org.wolfking.apputil.TestUtil;
import org.wolfking.apputil.Timer;
import org.wolfking.persistence.Paper;
import org.wolfking.persistence.Question;
import org.wolfking.persistence.SubType;
import org.wolfking.persistence.Temp;
import org.wolfking.persistence.User;
import org.wolfking.strutsform.Params;
public class TestController extends Action {
// private static TimeController timeCtr = Factory.getTimeController();
private static TestUtil testUtil = new TestUtil();
@SuppressWarnings("unchecked")
@Override
public ActionForward execute(ActionMapping mapping, ActionForm actionForm,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String require = request.getParameter("require");
/*
* 考生开始考试
*/
if (require.equals("come")) {
List<String> exams = new ArrayList<String>();
if (SharePool.get("ex") != null) {
exams.addAll((Set) SharePool.get("ex"));
request.getSession().setAttribute("exams", exams);
}
return mapping.findForward("select");
}
/*
* 页面传过来的select表示选择考场,这里处理完之后将跳转到test-begin<br> 处理时要将考场标识添加到Session中<br>
* 此分支由王真提供
*/
if (require.equals("select")) {
String select = request.getParameter("select");
request.getSession().setAttribute("examroom", select);
Paper paper = (Paper) (SharePool
.get((String) SharePool.get(select)));
request.setAttribute("timelong", paper.getTimeLast());
SubType st = (SubType) Factory.getDAO().get(SubType.class,
paper.getSubTypeId());
if (st != null)
request.setAttribute("sub", st);
return mapping.findForward("testbegin");
}
/* 正式开始的话进入考试页面test.jsp */
if (require.equals("start"))
return startTest(mapping, actionForm, request, response);
/* 考生答题 */
if (require.equals("answer"))
return handle(mapping, actionForm, request, response);
// 交卷
if (require.equals("hand")) {
String examroom = (String) request.getSession().getAttribute(
"examroom");
if (examroom == null)
return mapping.findForward("timeover");
TimeController timeCtr = Factory.getTimeController(examroom);
if (timeCtr == null)
return mapping.findForward("timeover");
handle(mapping, actionForm, request, response);
User user = (User) request.getSession().getAttribute("user");
Timer timer = null;
timer = timeCtr.getTimer(user.getUserId());
if (timer != null)
timer.stop();
return mapping.findForward("timeover");
}
/* 查看任一试题 */
if (require.equals("view"))
return view(mapping, actionForm, request);
/* 查看剩余时间 */
if (require.equals("lefttime"))
return viewLeftTime(mapping, request);
{
// 可能是页面出错,或是有人搞恶意破坏
}
return mapping.findForward("");
}// ~execute
/**
* 查看任一道试题
*
* @param mapping
* @param request
* @return
*/
private ActionForward view(ActionMapping mapping, ActionForm form,
HttpServletRequest request) {
String examroom = (String) request.getSession()
.getAttribute("examroom");
if (examroom == null)
return mapping.findForward("timeover");
TimeController timeCtr = Factory.getTimeController(examroom);
if (timeCtr == null)
return mapping.findForward("timeover");
int qid = Integer.parseInt(request.getParameter("qid"));
User user = (User) request.getSession().getAttribute("user");
Params params = (Params) form;
qid = qid <= 0 ? 1 : qid;
params.setPage(qid);
setData(mapping, params, request, testUtil.getQuestions(examroom), user);
return mapping.findForward("test");
}// ~view
/**
* 查看剩余时间
*
* @param mapping
* @param request
* @return
*/
private ActionForward viewLeftTime(ActionMapping mapping,
HttpServletRequest request) {
String examroom = (String) request.getSession()
.getAttribute("examroom");
if (examroom == null)
return mapping.findForward("timeover");
TimeController timeCtr = Factory.getTimeController(examroom);
if (timeCtr == null)
return mapping.findForward("timeover");
User user = (User) request.getSession().getAttribute("user");
Timer timer = null;
if (timeCtr != null) {
timer = timeCtr.getTimer(user.getUserId());
request.setAttribute("lefttime", timer.getLeftTime());
}
return mapping.findForward("lefttime");
}// ~viewLeftTime
/**
* 方法startTest实现考试的初始化所有准备。设置成功考生将可以进行正式考试
*
* @author 郭华辉 修改:王红燕
*/
@SuppressWarnings("unchecked")
private ActionForward startTest(ActionMapping mapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) {
String examroom = (String) request.getSession()
.getAttribute("examroom");
if (examroom == null)
return mapping.findForward("timeover");
TimeController timeCtr = Factory.getTimeController(examroom);
if (timeCtr == null)
return mapping.findForward("timeover");
Params params = (Params) actionForm;
// 取出考场对应的试卷,从而取得试题列表
String pname = (String) SharePool.get(examroom);
Paper paper = (Paper) SharePool.get(pname);
List<Question> questions = new ArrayList<Question>();
questions.addAll(paper.getQuestions());
User user = (User) request.getSession().getAttribute("user");
Long userId = user.getUserId();
if (timeCtr.initialed() == false) {
timeCtr.setTime(paper.getStartTime(), paper.getTimeLast() * 60);
timeCtr.setExamroom(examroom);
}
// 如果考生ID对应没有计时器,说明是开始
if (timeCtr.getTimer(userId) == null) {
// 开始计时
timeCtr.add(userId, paper.getTimeLast() * 60);
DAO dao = Factory.getDAO();
for (int i = 0; i < questions.size(); i++) {
Question question = (Question) questions.get(i);
Long questionId = question.getQuestionId();
Long knowId = question.getKnow().getId();
Temp temp = new Temp();
temp.setExamroom(examroom);
temp.setUserId(userId);
temp.setQuestionId(questionId);
if (dao.get(Temp.class, temp) != null)
break;
temp.setKnowId(knowId);
dao.save(temp);
}
}// end if
SharePool.add(String.valueOf(userId), new HashSet());
// 设置考试开始的第一道题及初始信息
Question firstQuestion = (Question) questions.get(0);
params.setQuestionID(firstQuestion.getQuestionId());
params.setPage(1);
request.setAttribute("question", firstQuestion);
request.setAttribute("curPage", 1);
request.getSession().setAttribute("total", questions.size());
request.getSession().setAttribute("timeslong", paper.getTimeLast());
request.setAttribute("answered", SharePool.get(String.valueOf(user
.getUserId())));
return mapping.findForward("test");
}// ~startTest
/**
* 处理考生答题
*/
@SuppressWarnings("unchecked")
private ActionForward handle(ActionMapping mapping, ActionForm actionForm,
HttpServletRequest request, HttpServletResponse response) {
String examroom = (String) request.getSession()
.getAttribute("examroom");
if (examroom == null)
return mapping.findForward("timeover");
TimeController timeCtr = Factory.getTimeController(examroom);
if (timeCtr == null)
return mapping.findForward("timeover");
User user = (User) request.getSession().getAttribute("user");
if (user == null
评论0
最新资源