📄 questionform.java
字号:
/* * CyberTester - J2EE Web application for creating, delivering and managing tests/exams/surveys. * Copyright (C) 2003 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.admin.test;import java.util.*;import javax.servlet.http.HttpServletRequest;import org.apache.struts.action.*;import com.cyberdemia.cybertester.admin.*;import com.cyberdemia.cybertester.CyberTesterUtils;import com.cyberdemia.school.DifficultyEnum;import com.cyberdemia.school.IQuestionType;/** * * QuestionForm is a Struts ActionForm to send, receive and validate values from the * question editor form. * * @author Alexander Yap */public final class QuestionForm extends ActionForm{ public Integer getQuesId() { return m_id; } public void setQuesId(Integer id) { m_id = id; } public String getMode() { return m_mode; } public void setMode(String mode) { m_mode = mode; } public boolean isNew() { return AdminConstants.EDITOR_NEW_MODE.equalsIgnoreCase(m_mode); } public boolean isNewAnswers() { return AdminConstants.EDITOR_NEW_ANSWERS_MODE.equalsIgnoreCase(m_mode); } public String getTitle() { return m_title; } public void setTitle(String title) { m_title = title; } public String getQuestionText() { return m_quesText; } public void setQuestionText(String quesText) { m_quesText = quesText; } public String getHintText() { return m_hintText; } public void setHintText(String hintText) { m_hintText = hintText; } public String getExplanationText() { return m_explanationText; } public void setExplanationText(String explanationText) { m_explanationText = explanationText; } public String getAnswer() { return m_answer; } public void setAnswer(String ans) { m_answer = ans; } public Integer getType() { return m_type; } public void setType(Integer type) { m_type = type; } public boolean isSingleChoiceType() { return IQuestionType.SINGLE_CHOICE_TYPE.equals(m_type); } public boolean isMultipleChoiceType() { return IQuestionType.MULTIPLE_CHOICE_TYPE.equals(m_type); } public boolean isKeywordsType() { return IQuestionType.KEYWORDS_TYPE.equals(m_type); } public List getAnswerChoicesList() { return m_answerChoices; } public String[] getAnswerChoices() { return (String[])m_answerChoices.toArray(new String[0]); } public void setAnswerChoices(String[] choices) { m_answerChoices.clear(); m_answerChoices.addAll( Arrays.asList(choices) ); } public String getAnswerChoiceIndexed(int index) { if (index>=m_answerChoices.size()) { return ""; } return m_answerChoices.get(index).toString(); } public void setAnswerChoiceIndexed(int index, String choice) { while (index >= m_answerChoices.size()) { m_answerChoices.add(""); } m_answerChoices.set(index, choice); } public void setCorrectAnswersStr(String ansStr) { m_answerCorrectStates.clear(); String[] ansArr = ansStr.split(","); for (int a=0; a<ansArr.length; a++) { int aidx = Integer.parseInt(ansArr[a]); setAnswerCorrectIndexed(aidx, true); } } public String getCorrectAnswersStr() { StringBuffer ansStrBuf = new StringBuffer(); int corCnt = m_answerCorrectStates.size(); boolean hasCorrect=false; for (int c=0; c<corCnt; c++) { if (((Boolean)m_answerCorrectStates.get(c)).booleanValue()) { if (hasCorrect) { ansStrBuf.append(","); } else { hasCorrect = true; } ansStrBuf.append(c); } } return ansStrBuf.toString(); } public boolean getAnswerCorrectIndexed(int index) { if (index >= m_answerCorrectStates.size()) { return false; } return ((Boolean)m_answerCorrectStates.get(index)).booleanValue(); } public void setAnswerCorrectIndexed(int index, boolean correct) { while (index >= m_answerCorrectStates.size()) { m_answerCorrectStates.add(Boolean.FALSE); } m_answerCorrectStates.set(index, correct ? Boolean.TRUE : Boolean.FALSE); } public List getKeywordsList() { return m_keywords; } public void setKeywordsStr(String keywordsStr) { m_keywords.clear(); m_keywords.addAll( Arrays.asList(keywordsStr.split(",")) ); } public String getKeywordsStr() { StringBuffer keywordStrBuf = new StringBuffer(); int kCnt = m_keywords.size(); for (int k=0; k<kCnt; k++) { if (k>0) { keywordStrBuf.append(","); } keywordStrBuf.append(m_keywords.get(k)); } return keywordStrBuf.toString(); } public String getKeywordIndexed(int index) { if (index >= m_keywords.size()) { return ""; } return m_keywords.get(index).toString(); } public void setKeywordIndexed(int index, String keyword) { while (index >= m_keywords.size()) { m_keywords.add(""); } m_keywords.set(index, keyword.trim().toLowerCase()); } public void setDifficultyStr( String diffStr) { m_diffStr = diffStr; } public String getDifficultyStr() { return m_diffStr; } public int getTimeLimitSeconds() { return m_timeLimitSecs; } public void setTimeLimitSeconds(int secs) { m_timeLimitSecs = secs; } public int getScore() { return m_score; } public void setScore(int score) { m_score = score; } public int getChoicesOrKeywordsCount() { return m_choicesOrKeywordsCount; } public void setChoicesOrKeywordsCount(int cnt) { m_choicesOrKeywordsCount = cnt; } public boolean isTypeDecided() { return m_typeDecided; } public void setTypeDecided(boolean decided) { m_typeDecided = decided; } public boolean isMustMatchAllKeywords() { return m_mustMatchAllKeywords; } public void setMustMatchAllKeywords(boolean match) { m_mustMatchAllKeywords = match; } /** * Reset all properties to their default values. * * @param mapping The mapping used to select this instance * @param request The servlet request we are processing */ public void reset(ActionMapping mapping, HttpServletRequest request) { m_title=null; m_quesText=null; m_hintText=null; m_diffStr=DifficultyEnum.TRIVIAL.getStr(); m_id=DEFAULT_ID; m_answer = "0"; m_type = IQuestionType.SINGLE_CHOICE_TYPE; m_mode=AdminConstants.EDITOR_NEW_MODE; m_timeLimitSecs = 0; m_choicesOrKeywordsCount = 4; m_answerChoices.clear(); m_answerCorrectStates.clear(); m_keywords.clear(); m_typeDecided = false; m_mustMatchAllKeywords = false; m_score = 1; } public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); CyberTesterUtils.validateRequiredFormEnteredParam("Title", m_title, errors); CyberTesterUtils.validateRequiredFormEnteredParam("Question text", m_quesText, errors); CyberTesterUtils.validateRequiredFormSelectedParam("Difficulty", m_diffStr, errors); if (m_timeLimitSecs<0) { errors.add("Time limit", new ActionError("error.form.negativeIntegerParam","Time limit",String.valueOf(m_timeLimitSecs))); } if (m_score<=0) { errors.add("Score", new ActionError("error.form.nonPositiveIntegerParam","Score",String.valueOf(m_score))); } int answerChoiceCount = m_answerChoices.size(); if (!m_typeDecided) { //CyberTesterUtils.validateRequiredFormSelectedParam("Type", m_type, errors); } else if (IQuestionType.SINGLE_CHOICE_TYPE.equals(m_type)) { try { int ansInt = Integer.parseInt(m_answer); if ((ansInt<0) || (ansInt>=answerChoiceCount)) { errors.add("Answer", new ActionError("error.form.outOfRangeNumericParam","Answer", m_answer, "0", String.valueOf(answerChoiceCount))); } } catch (NumberFormatException nfex) { errors.add("Answer", new ActionError("error.form.nonIntegerParam","Answer", m_answer)); } for (int aidx=0; aidx<answerChoiceCount; aidx++) { CyberTesterUtils.validateRequiredFormEnteredParam("Answer choice "+aidx, getAnswerChoiceIndexed(aidx), errors); } } else if (IQuestionType.MULTIPLE_CHOICE_TYPE.equals(m_type)) { if (m_answerCorrectStates.isEmpty()) { errors.add("Answer correct", new ActionError("error.form.missingParam.select", "Answer correct")); } for (int aidx=0; aidx<answerChoiceCount; aidx++) { CyberTesterUtils.validateRequiredFormEnteredParam("Answer choice "+aidx, getAnswerChoiceIndexed(aidx), errors); } } else if (IQuestionType.KEYWORDS_TYPE.equals(m_type)) { if (m_keywords.isEmpty()) { errors.add("Keywords", new ActionError("error.form.missingParam.enter", "Keywords")); } int kcnt = m_keywords.size(); for (int k=0; k<kcnt; k++) { String keyword = getKeywordIndexed(k); if (CyberTesterUtils.validateRequiredFormEnteredParam("Keyword "+k, keyword, errors)) { if (keyword.indexOf(',')>=0) { errors.add("Keywords", new ActionError("error.form.invalidParam", "Keyword "+keyword, ",")); } } } } return errors; } private Integer m_id=DEFAULT_ID; private String m_title=null; private String m_quesText=null; private String m_hintText=null; private String m_explanationText=null; private int m_choicesOrKeywordsCount = 4; private List m_answerChoices = new ArrayList(); private List m_answerCorrectStates = new ArrayList(); private List m_keywords = new ArrayList(); private String m_answer = "0"; private Integer m_type = IQuestionType.SINGLE_CHOICE_TYPE; private String m_mode=AdminConstants.EDITOR_NEW_MODE; private String m_diffStr = DifficultyEnum.TRIVIAL.getStr(); private int m_score = 1; private int m_timeLimitSecs = 0; private boolean m_typeDecided = false; private boolean m_mustMatchAllKeywords = false; private static final Integer DEFAULT_ID = new Integer(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -