⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 checkmultianswersaction.java

📁 老外的在线考试
💻 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.ServletException;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.*;/** *  * CheckMultiAnswersAction is a Struts action to receive and check the user's answers * to all the questions. It is called once for a test that has its * "multiQuestionsMode" enabled. *   * @version $Revision: 1.6 $ at $Date: 2004/06/26 17:14:52 $ by $Author: alexycyap $ * @author Alexander Yap */public final class CheckMultiAnswersAction extends AbstractUserAction{	protected ActionForward doPerform(		ActionMapping mapping, ActionForm form,		HttpServletRequest request, HttpServletResponse response,		HttpSession session) throws Exception	{		// Was this transaction cancelled?		if (request.getParameter(org.apache.struts.taglib.html.Constants.CANCEL_PROPERTY)!=null)		{			cleanup( session );			return mapping.findForward(Constants.CANCELLED);		}		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);		boolean endOfTest = true;		AnswerStatus status = null;		Integer testId = (Integer)session.getAttribute(TesteeConstants.TEST_ID_KEY);		QuestionData[] questions = (QuestionData[])session.getAttribute( TesteeConstants.QUESTION_LIST_KEY);		Integer userId = (Integer)session.getAttribute(Constants.USER_ID_KEY);		Integer questionOffset = (Integer)session.getAttribute(TesteeConstants.QUESTION_LIST_OFFSET_KEY);		int nextOffset = 0;		if (errors.isEmpty())		{			if ( questions==null )			{				throw new ServletException("Missing session attribute "+TesteeConstants.QUESTION_LIST_KEY);			}								if ( testId==null )			{				throw new ServletException("Missing session attribute "+TesteeConstants.TEST_ID_KEY);			}						if ( userId==null )			{				throw new ServletException("Missing session attribute "+Constants.USER_ID_KEY);			}						if ( questionOffset==null )			{				throw new ServletException("Missing session attribute "+TesteeConstants.QUESTION_LIST_OFFSET_KEY);			}								final int offset = questionOffset.intValue();			nextOffset = offset + TesteeConstants.QUESTION_LIST_BLOCK_SIZE;			endOfTest = (questions.length <= nextOffset);						try			{												TestManagerHome testMgrHome = CyberTesterUtils.getTestManagerHome();				TestManager testMgr = (TestManager)testMgrHome.create();				if ((offset > 0) || !testMgr.getTestScoreForUser(testId, userId, false).isTestTaken())				{					int endPos = Math.min(questions.length, nextOffset);					Integer[] quesIds = new Integer[endPos-offset];					String[] answerStrs = new String[quesIds.length];					for (int q=offset, i=0; q<endPos; q++, i++)					{						QuestionData quesData = questions[q];						quesIds[i] = quesData.getId();						String answerStr = null;						if (quesData.isMultipleChoiceType())						{							StringBuffer answerBuf = new StringBuffer();							int choiceCnt = quesData.getAnswerChoices().length;							boolean anyTicked = false;							for (int c=0; c<choiceCnt; c++)							{								boolean ticked = Boolean.valueOf(request.getParameter("answer_"+q+"_"+c)).booleanValue();								if (ticked)								{									if (anyTicked)									{										answerBuf.append(",");									}									else									{										anyTicked = true;																			}									answerBuf.append(c);								}							}															if (!anyTicked)							{								errors.add("MissingAnswer"+q, new ActionError("error.form.missingParam.selectMultiple","Answer "+q));							}							answerStr = answerBuf.toString();						}						else						{							answerStr = request.getParameter("answer_"+q);							if ((answerStr==null) || (answerStr.trim().length()==0) )							{								if (quesData.isSingleChoiceType())								{									errors.add("MissingAnswer"+q, new ActionError("error.form.missingParam.select","Answer "+q));								}								else								{									errors.add("MissingAnswer"+q, new ActionError("error.form.missingParam.enter","Answer "+q));								}							}						}												answerStrs[i] = answerStr;											} // end of for...										if (errors.isEmpty())					{						// Skip time check if test is not ended yet.						int timeSecs = (endOfTest) ? (int)(TesteeTestUtils.checkTestTimeToken(session,testId)/1000) : 0;						status = testMgr.checkMultiQuestionAnswers(testId, quesIds, userId, offset, answerStrs, timeSecs, endOfTest);					}				}				else				{					CyberTesterUtils.getLogger().log( Level.WARNING, "User "+userId+" already taken test "+testId);					errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.test.taken"));				}			}			catch (Exception ex)			{				CyberTesterUtils.getLogger().log( Level.SEVERE, "Error checking answers.", ex);				errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.checking.answers",ex.getMessage()));			}		}		if (!errors.isEmpty())		{			// Force cleanup even if test not ended yet when there is an error.			cleanup(session);						saveErrors(request, errors);			return mapping.findForward(Constants.ERROR);		}		// End of the test, clean up the session		if (endOfTest)		{			cleanup(session);		}		else		{	// Test not yet ended, go to next offset.			session.setAttribute(TesteeConstants.QUESTION_LIST_OFFSET_KEY, new Integer(nextOffset));			request.setAttribute(TesteeConstants.QUESTION_LIST_BLOCK_END_INDEX_KEY, new Integer(Math.min( nextOffset+TesteeConstants.QUESTION_LIST_BLOCK_SIZE, questions.length )-1));			// Set a transactional control token to prevent double posting			saveToken(request);		}		request.setAttribute( TesteeConstants.TEST_ID_KEY,testId);		if (status.isTestTimedOut())		{			request.setAttribute(TesteeConstants.TEST_TIMED_OUT_KEY, Boolean.TRUE);		}		return mapping.findForward( endOfTest ? Constants.SUCCESS : "nextPage");			}	private void cleanup(HttpSession 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_LIST_OFFSET_KEY);		TesteeTestUtils.removeTestTimeToken(session);	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -