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

📄 testmanagerbean.java

📁 老外的在线考试
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	/**	 * 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.	 *	 * @ejb.interface-method	 * @ejb.transaction type="Required"	 *	 * @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 SchoolException if the test is missing.	 */	public ScoreData getTestScoreForUser(Integer testId, Integer userId, boolean applySuppressResults ) throws SchoolException	{		LocalUserTestAssoc assoc = findTestUserAssoc(testId, userId);		if (assoc==null)		{			return null;				}		LocalTest test =  null;		try		{			test = s_testHome.findByPrimaryKey(testId);		}		catch (FinderException fex)		{			throw new SchoolException(SchoolException.TEST_NOT_FOUND, "Cannot find Test with ID "+testId, fex);		}		if (applySuppressResults && test.isSuppressTestEndFeedback() )		{			return null;		}		int score = assoc.getScore();		int maxScore = assoc.getMaxScore();		boolean taken = assoc.isTestCompleted() || (maxScore>0);		boolean passed =  (maxScore>0) ? ( ((score*100)/maxScore) >= test.getPassPercentage() ) : false;		// maxScore greater than 0 or test completed means that this testee has already taken the test.				return new ScoreData( assoc.getUserId(), assoc.getTestId(), test.getName(),			score, maxScore, passed , assoc.isExceededTestTime() , 			taken, assoc.isTestCompleted(), 			assoc.getLastModifiedDateTime() );	}		/**	 * Adds/associates a set of tests to a set of users.	 * If a test is already associated with a user, this method continues on with other tests/users.	 *	 * @ejb.interface-method	 * @ejb.transaction type="Required"	 *	 * @param testIds Array of test IDs to add	 * @param userIds Array of user IDs to add tests to	 * @return Number of users that are associated with tests by this method call.	 * @throws SchoolException if a test is missing or some other error.	 */	public int addTestsToUsers( Integer[] testIds, Integer[] userIds) throws SchoolException	{		int numAdded = 0;		for (int u=0; u<userIds.length; u++)		{			for (int t=0; t<testIds.length; t++)			{				if (addTestToUser(testIds[t], userIds[u]))				{					numAdded++;				}			}		}		return numAdded;	}		/**	 * Simultaneously removes/disassociates a set of tests from the user, then adds/associates another set of tests to the user.	 * This method is more efficient than calling removeTestFromUser(...) and addTestToUser(...)	 * repeatedly to add/remove a large set of tests.	 * If a test is already associated with this user during the association process, this method continues on with other tests.	 *	 * @ejb.interface-method	 * @ejb.transaction type="Required"	 *	 * @param testIdsToRemove Array of test IDs to remove	 * @param testIdsToAdd Array of test IDs to add	 * @param userId User ID	 * @throws SchoolException if the test is missing or some other error.	 */	public void synchronizeTestsWithUser( Integer[] testIdsToRemove, Integer[] testIdsToAdd, Integer userId) throws SchoolException	{		if (testIdsToRemove!=null)		{			for (int t=0; t<testIdsToRemove.length; t++)			{				removeTestFromUser(testIdsToRemove[t], userId);			}		}				if (testIdsToAdd!=null)		{			for (int t=0; t<testIdsToAdd.length; t++)			{				addTestToUser(testIdsToAdd[t], userId);			}		}	}		/**	 * Associates a test to a user. If this association already exists, this method does nothing.	 *	 * @ejb.interface-method	 * @ejb.transaction type="Required"	 *	 * @param testId Test ID	 * @param userId User ID	 * @return true if this method call adds the test to with the user, false if they are already associated beforehand.	 * @throws SchoolException if the test is missing.	 */	public boolean addTestToUser( Integer testId, Integer userId) throws SchoolException	{		boolean added = false;		if (findTestUserAssoc(testId,userId)==null)		{			try			{				s_testHome.findByPrimaryKey(testId);			}			catch (FinderException fex)			{				throw new SchoolException(SchoolException.TEST_NOT_FOUND, "Cannot find Test with ID "+testId, fex);			}				s_logger.fine("Adding test "+testId+" to user "+userId);			try			{				s_userTestAssocHome.create(testId, userId);					added = true;			}			catch (CreateException ex)			{				throw new SchoolException( "Failed to create LocalUserTestAssoc with Test ID="+testId+", User ID="+userId,ex);			}		}		return added;	}	/**	 * Disassociates a test from a user if such an association exists.	 *	 * @ejb.interface-method	 * @ejb.transaction type="Required"	 *	 * @param testId Test ID	 * @param userId User ID	 * @throws SchoolException if the test is missing or some other error.	 */	public void removeTestFromUser( Integer testId, Integer userId) throws SchoolException	{				try		{			s_testHome.findByPrimaryKey(testId);		}		catch (FinderException fex)		{			throw new SchoolException(SchoolException.TEST_NOT_FOUND, "Cannot find Test with ID "+testId, fex);		}		s_logger.fine("Removing test "+testId+" from user "+userId);		UserTestAssocKey key = new UserTestAssocKey();		key.setTestId(testId);		key.setUserId(userId);		LocalUserTestAssoc assoc = null;		try		{			assoc = s_userTestAssocHome.findByPrimaryKey( key );		}		catch (FinderException fex)		{			assoc = null;		}		if (assoc!=null)		{			try			{				assoc.remove();			}			catch (Exception ex)			{				throw new SchoolException("Failed to remove LocalUserTestAssoc for Test ID="+testId+", User ID="+userId, ex);			}		}	}		/**	 * Gets a user's historical records for a test and user.	 *	 * @ejb.interface-method	 * @ejb.transaction type="Required"	 *	 * @param testId Test ID	 * @param userId User ID	 * @return List of AnswerHistoryData instances.	 */	public List getAnswerHistory(Integer testId, Integer userId) throws SchoolException	{		s_logger.fine("Getting answer history for test "+testId+", user "+userId);		LinkedList historyList = new LinkedList();		try		{			Collection historyCol = s_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.getQuestionType(),					record.getQuestionIndex(), record.getQuestionScore(),					record.getUserAnswer(), record.getCorrectAnswer(),					record.isAnswerCorrect(), record.getUserScore(),					record.getTimestamp()) 				);			}		}		catch (Exception ex)		{			throw new SchoolException("Error getting answer history for test "+testId+", user "+userId, ex);		}		return historyList;	}    /**     * Dummy create method.     *     * @ejb.create-method view-type="remote"     */    public void ejbCreate()    {        // Nothing here    }	private boolean canDeactivateTest(LocalTest test)	{		if (!test.isActive())		{			return false;		}				boolean hasAssoc = false;		try		{			hasAssoc = !(s_userTestAssocHome.findByTest(test.getId()).isEmpty());		}		catch (FinderException fex)		{			// Ignore exception;		}				return !hasAssoc;	}		private LocalUserTestAssoc findTestUserAssoc(Integer testId, Integer userId)	{		LocalUserTestAssoc assoc = null;		try		{			Iterator assocIter = s_userTestAssocHome.findByUserAndTest(userId, testId).iterator();			if (assocIter.hasNext())			{				assoc = (LocalUserTestAssoc)assocIter.next();			}		}		catch (Exception ex)		{			s_logger.log( Level.FINER, "LocalUserTestAssoc does not exist yet for Test ID="+testId+", User ID="+userId);			assoc = null;		}		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(s_quesHome.findByTest(test.getId()));		List unrelatedQuesList = new ArrayList(s_quesHome.findUnassignedToTest(true));		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());			relatedQuesDataArr[ridx].getProperties().put("description", StringUtils.truncate(ques.getContent(),30) );			relatedQuesDataArr[ridx].getProperties().put("score", new Integer(ques.getScore()) );		}		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());			unrelatedQuesDataArr[uidx].getProperties().put("description", StringUtils.truncate(ques.getContent(),30) );			unrelatedQuesDataArr[uidx].getProperties().put("score", new Integer(ques.getScore()) );		}		data.setRelatedResources( relatedQuesDataArr );		data.setUnrelatedResources( unrelatedQuesDataArr );		return data;	}	private static LocalTestHome s_testHome = null;	private static LocalQuestionHome s_quesHome = null;	private static LocalUserTestAssocHome s_userTestAssocHome = null;	private static LocalAnswerHistoryRecordHome s_answerHistoryRecordHome = null;	static	{		try		{			Context ctx = new InitialContext();			try			{				s_quesHome = (LocalQuestionHome)ctx.lookup(JNDI_PREFIX+LocalQuestionHome.JNDI_NAME);				s_testHome = (LocalTestHome)ctx.lookup(JNDI_PREFIX+LocalTestHome.JNDI_NAME);				s_userTestAssocHome = (LocalUserTestAssocHome)ctx.lookup(JNDI_PREFIX+LocalUserTestAssocHome.JNDI_NAME);				s_answerHistoryRecordHome = (LocalAnswerHistoryRecordHome)ctx.lookup(JNDI_PREFIX+LocalAnswerHistoryRecordHome.JNDI_NAME);			}			finally			{				ctx.close();			}		}		catch (NamingException nex)		{			throw new EJBException("Error retrieving homes", nex);		}	}	private static Logger s_logger = Logger.getLogger( TestManagerBean.class.getName());	} 

⌨️ 快捷键说明

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