📄 checkansweraction.java
字号:
/* * CyberTester - J2EE Web application for creating, delivering and managing tests/exams/surveys. * Copyright (C) 2004 CyberDemia Research and Services Pty Ltd * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program (see the file COPYING); if not, write to the * Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * See the COPYING file located in the top-level-directory of * the archive of this program for complete text of license. */package com.cyberdemia.cybertester.testee.test;import javax.servlet.*;import javax.servlet.http.*;import org.apache.struts.action.*;import com.cyberdemia.school.*;import com.cyberdemia.cybertester.*;import com.cyberdemia.cybertester.testee.TesteeConstants;import java.util.logging.*;/** * * CheckAnswerAction is a Struts action to receive and check the user's answer * to a single question. It is called for each question in a test that has its * "multiQuestionsMode" disabled. * * @version $Revision: 1.5 $ at $Date: 2004/06/26 17:14:52 $ by $Author: alexycyap $ * @author Alexander Yap */public final class CheckAnswerAction extends AbstractUserAction{ protected ActionForward doPerform( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception { Integer indexInt = (Integer)session.getAttribute(TesteeConstants.QUESTION_INDEX_KEY); if (indexInt==null) { throw new ServletException("Missing session attribute "+TesteeConstants.QUESTION_INDEX_KEY); } int index = indexInt.intValue(); CyberTesterUtils.getLogger().finer("Checking answer for question index "+index); ActionErrors errors = new ActionErrors(); // If submitted out of order, go to error page immediately. if (!isTokenValid(request)) { errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.transaction.token")); } resetToken(request); QuestionData[] questions = null; TestData testData = null; QuestionData quesData = null; boolean endOfTest = false; if (errors.isEmpty()) { questions = (QuestionData[])session.getAttribute( TesteeConstants.QUESTION_LIST_KEY); if (questions==null) { throw new ServletException( "Missing session attribute "+TesteeConstants.QUESTION_LIST_KEY); } if ((index<0) || (index>=questions.length)) { throw new ServletException("Question index "+index+" is out of range."); } endOfTest = (index == (questions.length-1)); quesData = questions[index]; Integer testId = (Integer)session.getAttribute(TesteeConstants.TEST_ID_KEY); testData = (TestData)session.getAttribute(TesteeConstants.TEST_DATA_KEY); if (testData==null) { throw new ServletException( "Missing session attribute "+TesteeConstants.TEST_DATA_KEY); } int timeSecs = (int)(TesteeTestUtils.checkQuestionTimeToken(session,testId,quesData.getId())/1000); request.setAttribute(TesteeConstants.ANSWER_RECEIVED_KEY,Boolean.TRUE); QuestionForm quesForm = (QuestionForm)form; try { TestManagerHome testMgrHome = CyberTesterUtils.getTestManagerHome(); TestManager testMgr = (TestManager)testMgrHome.create(); String userAnswer = quesForm.getAnswer(); AnswerStatus status = testMgr.checkQuestionAnswer(testId, quesData.getId(), (Integer)session.getAttribute(Constants.USER_ID_KEY), index, userAnswer, 0, timeSecs, endOfTest); request.setAttribute(TesteeConstants.ANSWER_STATUS_KEY, status); } catch (Exception ex) { CyberTesterUtils.getLogger().log( Level.SEVERE, "Error checking answer.", ex); errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.checking.answer",String.valueOf(quesForm.getAnswer()),ex.getMessage())); } if (index==questions.length-1) { request.setAttribute(TesteeConstants.TEST_ID_KEY, testId); request.setAttribute(TesteeConstants.TEST_DATA_KEY, session.getAttribute(TesteeConstants.TEST_DATA_KEY)); request.setAttribute( TesteeConstants.QUESTION_COUNT_KEY, session.getAttribute(TesteeConstants.QUESTION_COUNT_KEY)); // End of the question list in the test, clean up the session session.removeAttribute(TesteeConstants.TEST_ID_KEY); session.removeAttribute(TesteeConstants.TEST_DATA_KEY); session.removeAttribute(TesteeConstants.QUESTION_LIST_KEY); session.removeAttribute(TesteeConstants.QUESTION_COUNT_KEY); session.removeAttribute(TesteeConstants.QUESTION_INDEX_KEY); } } if (!errors.isEmpty()) { saveErrors(request, errors); return mapping.findForward(Constants.ERROR); } String forwardTarget = null; if (testData.isSuppressQuestionFeedback()) { // Go directly to the next question if any. // If end of test, go directly to the results view if (!endOfTest) { forwardTarget = "nextQuestion"; } else { forwardTarget = "viewResults"; } } else { // Return the feedback to the question page if (form==null) { form = new QuestionForm(); if ("request".equals(mapping.getScope())) request.setAttribute(mapping.getAttribute(), form); else session.setAttribute(mapping.getAttribute(), form); } QuestionForm questionForm = (QuestionForm)form; questionForm.setQuesIndex(index); questionForm.setQuesId(quesData.getId()); request.setAttribute( TesteeConstants.QUESTION_DATA_KEY, quesData ); forwardTarget = "feedback"; } if (!endOfTest) { request.setAttribute("hasNextQuestion", Boolean.TRUE); } CyberTesterUtils.getLogger().finer("forwardTarget="+forwardTarget); return mapping.findForward(forwardTarget); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -