📄 testmanagerbean.java
字号:
{ LocalTest test = m_testHome.findByPrimaryKey(id); test.setName( data.getName() ); test.setOwnerHierarchyId( data.getOwnerHierId() ); test.setPassPercentage(data.getPassPercentage()); test.setMultiQuestionsMode( data.isMultiQuestionsMode()); test.setSuppressQuestionFeedback(data.isSuppressQuestionFeedback()); test.setSuppressTestEndFeedback(data.isSuppressTestEndFeedback()); test.setTimeLimitSeconds( data.getTimeLimitSeconds()); test.setLastModifiedMillis(System.currentTimeMillis()); String[] resIdsToRemove = data.getResourceIdsToRemove(); for (int r=0; r<resIdsToRemove.length; r++) { removeQuestionFromTest(id, resIdsToRemove[r]); } String[] resIdsToAdd = data.getResourceIdsToAdd(); for (int r=0; r<resIdsToAdd.length; r++) { addQuestionToTest(id, resIdsToAdd[r]); } } catch (FinderException fex) { s_logger.log( Level.SEVERE, "Error finding Test ID="+id, fex); throw SchoolUtils.convertToEJBException(fex); } catch (Exception ex) { s_logger.log( Level.SEVERE, "Error setting data for Test ID="+id, ex); if (ex instanceof RuntimeException) { throw (RuntimeException)ex; } throw new RuntimeException(ex); } } /** * Checks the user's answer for a question. The user is * awarded a score if the answer is correct. This method also * creates an AnswerHistoryRecordBean instance to log this answer * and the awarded score. * * @param testId Test ID * @param quesId Question ID * @param userId User ID * @param quesIndex Index of the question within the test. * @param userAnswer User's answer as an index to the multiple answer choices. * @param testSecs Amount of time in seconds the user took to complete the test (applicable only if multiQuestionsMode is enabled). * @param quesSecs Amount of time in seconds the user took to answer this question (applicable only if multiQuestionsMode is disabled). * @return The result status of the answer, one of the constants defined in IAnswerStatus. * @throws EJBException if the test or question is missing, or some other error. */ public int checkQuestionAnswer(String testId, String quesId, String userId, int quesIndex, Integer userAnswer, int testSecs, int quesSecs) { LocalTest test = null; try { test = m_testHome.findByPrimaryKey(testId); } catch (FinderException fex) { s_logger.log( Level.SEVERE, "Error finding Test ID="+testId, fex); throw SchoolUtils.convertToEJBException(fex); } int status = IAnswerStatus.WRONG; boolean exceedTestTime = false; boolean exceedQuesTime = false; int quesScore = 0; Integer quesAnswer = null; try { LocalQuestion ques = m_quesHome.findByPrimaryKey(quesId); if (ques.checkResponse(userAnswer)) { status = IAnswerStatus.CORRECT; } quesScore = ques.getScore(); quesAnswer = ques.getAnswer(); if (test.isMultiQuestionsMode()) { if (test.getTimeLimitSeconds()>0) { exceedTestTime = (testSecs > test.getTimeLimitSeconds()); status = exceedTestTime ? IAnswerStatus.TEST_TIMED_OUT : status; } } else { if (ques.getTimeLimitSeconds()>0) { exceedQuesTime = (quesSecs > ques.getTimeLimitSeconds()); status = exceedQuesTime ? IAnswerStatus.QUESTION_TIMED_OUT : status; } } } catch (FinderException fex) { s_logger.log( Level.SEVERE, "Error finding Question ID="+quesId, fex); throw SchoolUtils.convertToEJBException(fex); } int scoreToAdd = (status == IAnswerStatus.CORRECT) ? quesScore : 0; try { s_logger.fine("Adding score "+scoreToAdd+" for test "+testId+", question "+quesId+" to user "+userId); LocalUserTestAssoc assoc = findTestUserAssoc(testId, userId); if (assoc==null) { assoc = m_userTestAssocHome.create(testId, userId); } assoc.addScore(scoreToAdd, quesScore); if (exceedTestTime) { assoc.exceededTestTime(); } } catch (CreateException cex) { s_logger.log( Level.SEVERE, "Error creating Score for Test ID="+testId+", User ID="+userId, cex); throw SchoolUtils.convertToEJBException(cex); } catch (RuntimeException ex) { s_logger.log( Level.SEVERE, "Error adding score for Test ID="+testId+", User ID="+userId, ex); throw ex; } try { m_answerHistoryRecordHome.create(testId, quesId, userId, quesIndex, quesScore, scoreToAdd, userAnswer.intValue(), quesAnswer.intValue(), exceedQuesTime); } catch (CreateException cex) { s_logger.log( Level.SEVERE, "Error creating history record for Test ID="+testId+", Question ID="+quesId+", User ID="+userId, cex); throw SchoolUtils.convertToEJBException(cex); } catch (RuntimeException ex) { s_logger.log( Level.SEVERE, "Error creating history record for Test ID="+testId+", Question ID="+quesId+", User ID="+userId, ex); throw ex; } return status; } /** * Gets the test score data for a user. Optionally consider the test's suppressTestEndFeedback * setting to hide the score when appropriate. If the score is hidden, this method returns null. * @param testId Test ID * @param userId User ID * @param applySuppressResults true to consider the test's suppressTestEndFeedback setting, false to ignore it. * @return ScoreData instance storing score information, null if the user has not taken the test yet or if it has been suppressed by the test's suppressTestEndFeedback setting. * @throws EJBException if the test is missing. */ public ScoreData getTestScoreForUser(String testId, String userId, boolean applySuppressResults ) { LocalUserTestAssoc assoc = findTestUserAssoc(testId, userId); if (assoc==null) { return null; } LocalTest test = null; try { test = m_testHome.findByPrimaryKey(testId); } catch (FinderException fex) { s_logger.log( Level.SEVERE, "Error finding Test ID="+testId, fex); throw SchoolUtils.convertToEJBException(fex); } if (test==null) { return null; } if (applySuppressResults && test.isSuppressTestEndFeedback() ) { return null; } int score = assoc.getScore(); int maxScore = assoc.getMaxScore(); boolean passed = (maxScore>0) ? ( ((score*100)/maxScore) >= test.getPassPercentage() ) : false; return new ScoreData( assoc.getUserId(), assoc.getTestId(), test.getName(), score, maxScore, passed , assoc.isExceededTestTime() , assoc.getLastModifiedDateTime() ); } public void addTestToUser( String testId, String userId) { s_logger.fine("Adding test "+testId+" to user "+userId); try { m_userTestAssocHome.create(testId, userId); } catch (CreateException ex) { s_logger.warning( "Failed to create LocalUserTestAssoc with Test ID="+testId+", User ID="+userId); throw SchoolUtils.convertToEJBException(ex); } } /** * Removes a test from a user. * @param testId Test ID * @param userId User ID * @throws EJBException if the test is missing or some other error. */ public void removeTestFromUser( String testId, String userId) { s_logger.fine("Removing test "+testId+" from user "+userId); UserTestAssocKey key = new UserTestAssocKey(); key.testId = testId; key.userId = userId; try { LocalUserTestAssoc assoc = m_userTestAssocHome.findByPrimaryKey( key ); assoc.remove(); } catch (Exception ex) { s_logger.warning( "Failed to remove LocalUserTestAssoc for Test ID="+testId+", User ID="+userId); throw SchoolUtils.convertToEJBException(ex); } } /** * Gets a user's historical records for a test and user. * @param testId Test ID * @param userId User ID * @return List of AnswerHistoryData instances. */ public List getAnswerHistory(String testId, String userId) { s_logger.fine("Getting answer history for test "+testId+", user "+userId); LinkedList historyList = new LinkedList(); try { Collection historyCol = m_answerHistoryRecordHome.findByUserAndTest(userId,testId); Iterator hIter=historyCol.iterator(); while (hIter.hasNext()) { LocalAnswerHistoryRecord record = (LocalAnswerHistoryRecord)hIter.next(); historyList.add( new AnswerHistoryData ( record.getTestId(), record.getQuestionId(), record.getUserId(), record.getQuestionIndex(), record.getQuestionScore(), record.getUserAnswer(), record.getCorrectAnswer(), record.isAnswerCorrect(), record.getTimestamp()) ); } } catch (FinderException fex) { s_logger.log( Level.SEVERE, "Error getting answer history for test "+testId+", user "+userId, fex); throw SchoolUtils.convertToEJBException(fex); } catch (RuntimeException ex) { s_logger.log( Level.SEVERE, "Error getting answer history for test "+testId+", user "+userId, ex); throw ex; } return historyList; } private boolean canDeactivateTest(LocalTest test) { if (!test.isActive()) { return false; } boolean hasAssoc = false; try { hasAssoc = !(m_userTestAssocHome.findByTest(test.getId()).isEmpty()); } catch (FinderException fex) { // Ignore exception; } return !hasAssoc; } private LocalUserTestAssoc findTestUserAssoc(String testId, String userId) { LocalUserTestAssoc assoc = null; try { Iterator scoreIter = m_userTestAssocHome.findByUserAndTest(userId, testId.toString()).iterator(); if (scoreIter.hasNext()) { assoc = (LocalUserTestAssoc)scoreIter.next(); } } catch (Exception ex) { s_logger.log( Level.FINE, "LocalUserTestAssoc does not exist yet for Test ID="+testId+", User ID="+userId); } return assoc; } private TestData getDataFromTest(LocalTest test) throws FinderException { TestData data = new TestData(test.getId(), test.getName(), test.getOwner(), test.getOwnerHierarchyId(), test.getPassPercentage(), test.getTimeLimitSeconds(), test.isMultiQuestionsMode(), test.isSuppressQuestionFeedback(), test.isSuppressTestEndFeedback(), canDeactivateTest(test)); List relatedQuesList = new ArrayList(m_quesHome.findByTest(test.getId())); List unrelatedQuesList = new ArrayList(m_quesHome.findByTest("")); ResourceListData[] relatedQuesDataArr = new ResourceListData[ relatedQuesList.size() ]; for (int ridx=0; ridx<relatedQuesDataArr.length; ridx++) { LocalQuestion ques = (LocalQuestion)relatedQuesList.get(ridx); relatedQuesDataArr[ridx] = new ResourceListData(ques.getId(), ques.getName()); } ResourceListData[] unrelatedQuesDataArr = new ResourceListData[ unrelatedQuesList.size() ]; for (int uidx=0; uidx<unrelatedQuesDataArr.length; uidx++) { LocalQuestion ques = (LocalQuestion)unrelatedQuesList.get(uidx); unrelatedQuesDataArr[uidx] = new ResourceListData(ques.getId(), ques.getName()); } data.setRelatedResources( relatedQuesDataArr ); data.setUnrelatedResources( unrelatedQuesDataArr ); return data; } //----------------------------- // Container methods. //----------------------------- public void ejbCreate() {} public void ejbPostCreate() { } public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(SessionContext sc) { m_context = sc; } private SessionContext m_context = null; private LocalTestHome m_testHome = null; private LocalQuestionHome m_quesHome = null; private LocalUserTestAssocHome m_userTestAssocHome = null; private LocalAnswerHistoryRecordHome m_answerHistoryRecordHome = null; private static Logger s_logger = Logger.getLogger( TestManagerBean.class.getName()); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -