Index: lams_learning/src/java/org/lamsfoundation/lams/learning/web/util/LearningWebUtil.java =================================================================== RCS file: /usr/local/cvsroot/lams_learning/src/java/org/lamsfoundation/lams/learning/web/util/LearningWebUtil.java,v diff -u -r1.27 -r1.27.2.1 --- lams_learning/src/java/org/lamsfoundation/lams/learning/web/util/LearningWebUtil.java 3 Apr 2009 04:42:06 -0000 1.27 +++ lams_learning/src/java/org/lamsfoundation/lams/learning/web/util/LearningWebUtil.java 22 Apr 2009 04:26:28 -0000 1.27.2.1 @@ -21,7 +21,7 @@ * **************************************************************** */ -/* $$Id$$ */ +/* $$Id$$ */ package org.lamsfoundation.lams.learning.web.util; import java.io.UnsupportedEncodingException; @@ -33,7 +33,6 @@ import org.apache.log4j.Logger; import org.apache.struts.action.ActionForward; -import org.apache.struts.action.ActionMapping; import org.apache.struts.action.DynaActionForm; import org.lamsfoundation.lams.learning.service.ICoreLearnerService; import org.lamsfoundation.lams.learning.service.LearnerServiceException; @@ -52,311 +51,323 @@ import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; - /** * * @author Jacky Fang - * @since 2005-3-10 + * @since 2005-3-10 * @version * */ -public class LearningWebUtil -{ - - private static Logger log = Logger.getLogger(LearningWebUtil.class); +public class LearningWebUtil { + + private static Logger log = Logger.getLogger(LearningWebUtil.class); //--------------------------------------------------------------------- // Class level constants - session attributes //--------------------------------------------------------------------- - public static final String PARAM_PROGRESS_ID = "progressID"; -// public static final String POPUP_WINDOW_NAME = "LearnerActivity"; -// public static final String LEARNER_WINDOW_NAME = "lWindow"; - + public static final String PARAM_PROGRESS_ID = "progressID"; + + // public static final String POPUP_WINDOW_NAME = "LearnerActivity"; + // public static final String LEARNER_WINDOW_NAME = "lWindow"; + /** - * Helper method to retrieve the user data. Gets the id from the user details - * in the shared session + * Helper method to retrieve the user data. Gets the id from the user + * details in the shared session + * * @return the user id */ - public static Integer getUserId() - { - HttpSession ss = SessionManager.getSession(); - UserDTO learner = (UserDTO) ss.getAttribute(AttributeNames.USER); - return learner != null ? learner.getUserID() : null; + public static Integer getUserId() { + HttpSession ss = SessionManager.getSession(); + UserDTO learner = (UserDTO) ss.getAttribute(AttributeNames.USER); + return learner != null ? learner.getUserID() : null; } - + /** - * Helper method to retrieve the user data. Gets the id from the user details - * in the shared session then retrieves the real user object. + * Helper method to retrieve the user data. Gets the id from the user + * details in the shared session then retrieves the real user object. */ - public static User getUser(ICoreLearnerService learnerService) - { - HttpSession ss = SessionManager.getSession(); - UserDTO learner = (UserDTO) ss.getAttribute(AttributeNames.USER); - return learner != null ? (User)learnerService.getUserManagementService().findById(User.class,learner.getUserID()) : null; + public static User getUser(ICoreLearnerService learnerService) { + HttpSession ss = SessionManager.getSession(); + UserDTO learner = (UserDTO) ss.getAttribute(AttributeNames.USER); + return learner != null ? (User) learnerService.getUserManagementService().findById(User.class, + learner.getUserID()) : null; } - - /** - * Put the learner progress in the request. This allows some optimisation between the - * code that updates the progress and the next action which will access the progress. - */ - public static void putLearnerProgressInRequest(HttpServletRequest request, LearnerProgress progress) { - if ( progress != null ) { - request.setAttribute(ActivityAction.LEARNER_PROGRESS_REQUEST_ATTRIBUTE, progress); - } else { - request.removeAttribute(ActivityAction.LEARNER_PROGRESS_REQUEST_ATTRIBUTE); - } + + /** + * Put the learner progress in the request. This allows some optimisation + * between the code that updates the progress and the next action which will + * access the progress. + */ + public static void putLearnerProgressInRequest(HttpServletRequest request, LearnerProgress progress) { + if (progress != null) { + request.setAttribute(ActivityAction.LEARNER_PROGRESS_REQUEST_ATTRIBUTE, progress); + } else { + request.removeAttribute(ActivityAction.LEARNER_PROGRESS_REQUEST_ATTRIBUTE); } - - /** - * Get the current learner progress. Check the request - in some cases it may be there. - * - * If not, the learner progress id might be in the request (if we've just come from complete activity). - * If so, get it from the db using the learner progress. - * - * If the learner progress id isn't available, then we have to look it up using activity - * based on the activity / activity id in the request. - */ - public static LearnerProgress getLearnerProgress(HttpServletRequest request, ICoreLearnerService learnerService) { - LearnerProgress learnerProgress = (LearnerProgress)request.getAttribute(ActivityAction.LEARNER_PROGRESS_REQUEST_ATTRIBUTE); - if ( learnerProgress != null ) { - if ( log.isDebugEnabled() ) { - log.debug("getLearnerProgress: found progress in the request"); - } - return learnerProgress; + } + + /** + * Get the current learner progress. Check the request - in some cases it + * may be there. + * + * If not, the learner progress id might be in the request (if we've just + * come from complete activity). If so, get it from the db using the learner + * progress. + * + * If the learner progress id isn't available, then we have to look it up + * using activity based on the activity / activity id in the request. + */ + public static LearnerProgress getLearnerProgress(HttpServletRequest request, ICoreLearnerService learnerService) { + LearnerProgress learnerProgress = (LearnerProgress) request + .getAttribute(ActivityAction.LEARNER_PROGRESS_REQUEST_ATTRIBUTE); + if (learnerProgress != null) { + if (log.isDebugEnabled()) { + log.debug("getLearnerProgress: found progress in the request"); + } + return learnerProgress; + } + + if (learnerProgress == null) { + Long learnerProgressId = WebUtil.readLongParam(request, LearningWebUtil.PARAM_PROGRESS_ID, true); + // temp hack until Flash side updates it call. + if (learnerProgressId == null) { + learnerProgressId = WebUtil.readLongParam(request, "progressId", true); + if (learnerProgressId != null) + log.warn("Flash client still using progressId, instead of progressID in a learner call"); + } + + if (learnerProgressId != null) { + learnerProgress = learnerService.getProgressById(new Long(learnerProgressId)); + if (learnerProgress != null && log.isDebugEnabled()) { + log.debug("getLearnerProgress: found progress via progress id"); } - - if (learnerProgress == null) - { - Long learnerProgressId = WebUtil.readLongParam(request,LearningWebUtil.PARAM_PROGRESS_ID, true); - // temp hack until Flash side updates it call. - if ( learnerProgressId == null ) { - learnerProgressId = WebUtil.readLongParam(request,"progressId", true); - if ( learnerProgressId != null ) - log.warn("Flash client still using progressId, instead of progressID in a learner call"); - } - - if ( learnerProgressId != null ) { - learnerProgress = learnerService.getProgressById(new Long(learnerProgressId)); - if ( learnerProgress != null && log.isDebugEnabled() ) { - log.debug("getLearnerProgress: found progress via progress id"); - } - } - - } - - if (learnerProgress == null) - { - Integer learnerId = getUserId(); - Activity act = getActivityFromRequest(request, learnerService); - Lesson lesson = learnerService.getLessonByActivity(act); - learnerProgress = learnerService.getProgress(learnerId, lesson.getLessonId()); - if ( learnerProgress != null && log.isDebugEnabled() ) { - log.debug("getLearnerProgress: found progress via learner id and activity"); - } - } - - putLearnerProgressInRequest(request, learnerProgress); - return learnerProgress; + } + } - + + if (learnerProgress == null) { + Integer learnerId = getUserId(); + Activity act = getActivityFromRequest(request, learnerService); + Lesson lesson = learnerService.getLessonByActivity(act); + learnerProgress = learnerService.getProgress(learnerId, lesson.getLessonId()); + if (learnerProgress != null && log.isDebugEnabled()) { + log.debug("getLearnerProgress: found progress via learner id and activity"); + } + } + + putLearnerProgressInRequest(request, learnerProgress); + return learnerProgress; + } + /** * Get the activity from request. We assume there is a parameter coming in - * if there is no activity can be found in the http request. Then the + * if there is no activity can be found in the http request. Then the * activity id parameter is used to retrieve from database. + * * @param request * @return */ - public static Activity getActivityFromRequest(HttpServletRequest request, - ICoreLearnerService learnerService) - { - Activity activity = (Activity)request.getAttribute(ActivityAction.ACTIVITY_REQUEST_ATTRIBUTE); - - if(activity == null) - { - long activityId = WebUtil.readLongParam(request,AttributeNames.PARAM_ACTIVITY_ID); - - activity = learnerService.getActivity(new Long(activityId)); - - if ( activity != null ) { - // getActivityFromRequest() may be called multiple times, so make it quicker next time - request.setAttribute(ActivityAction.ACTIVITY_REQUEST_ATTRIBUTE, activity); - } - } - return activity; + public static Activity getActivityFromRequest(HttpServletRequest request, ICoreLearnerService learnerService) { + Activity activity = (Activity) request.getAttribute(ActivityAction.ACTIVITY_REQUEST_ATTRIBUTE); + + if (activity == null) { + long activityId = WebUtil.readLongParam(request, AttributeNames.PARAM_ACTIVITY_ID); + + activity = learnerService.getActivity(new Long(activityId)); + + if (activity != null) { + // getActivityFromRequest() may be called multiple times, so make it quicker next time + request.setAttribute(ActivityAction.ACTIVITY_REQUEST_ATTRIBUTE, activity); + } + } + return activity; } - + /** - * Put an activity into the request. Calls LearnerService to get the activity, to ensure - * that it is a "real" activity, not one of the cglib proxies. - * activity. + * Put an activity into the request. Calls LearnerService to get the + * activity, to ensure that it is a "real" activity, not one of the cglib + * proxies. activity. + * * @param request * @param activity */ public static void putActivityInRequest(HttpServletRequest request, Activity activity, - ICoreLearnerService learnerService) - { - if ( activity != null ) { - Activity realActivity = learnerService.getActivity(activity.getActivityId()); - request.setAttribute(ActivityAction.ACTIVITY_REQUEST_ATTRIBUTE, realActivity); - } else { - request.setAttribute(ActivityAction.ACTIVITY_REQUEST_ATTRIBUTE, null); - } + ICoreLearnerService learnerService) { + if (activity != null) { + Activity realActivity = learnerService.getActivity(activity.getActivityId()); + request.setAttribute(ActivityAction.ACTIVITY_REQUEST_ATTRIBUTE, realActivity); + } else { + request.setAttribute(ActivityAction.ACTIVITY_REQUEST_ATTRIBUTE, null); + } } - /** "Complete" an activity from the web layer's perspective. Used for CompleteActivityAction and the Gate and Grouping actions. - * Calls the learningService to actually complete the activity and progress. - * @param redirect Should this call redirect to the next screen (true) or use a forward (false) - * @param windowName Name of the window that triggered this code. Normally LearnerActivity (the popup window) or lWindow (normal learner window) - * @throws UnsupportedEncodingException - * + /** + * "Complete" an activity from the web layer's perspective. Used for + * CompleteActivityAction and the Gate and Grouping actions. Calls the + * learningService to actually complete the activity and progress. + * + * @param redirect + * Should this call redirect to the next screen (true) or use + * a forward (false) + * @param windowName + * Name of the window that triggered this code. Normally + * LearnerActivity (the popup window) or lWindow (normal + * learner window) + * @throws UnsupportedEncodingException + * */ public static ActionForward completeActivity(HttpServletRequest request, HttpServletResponse response, - ActivityMapping actionMappings, LearnerProgress currentProgress, Activity currentActivity, - Integer learnerId, ICoreLearnerService learnerService, boolean redirect) throws LearnerServiceException, UnsupportedEncodingException { - - LearnerProgress progress=currentProgress; - Lesson lesson = progress.getLesson(); - - if ( currentActivity == null ) { - progress = learnerService.joinLesson(learnerId, lesson.getLessonId()); - } else if ( progress.getCompletedActivities().containsKey(currentActivity) ) { - return actionMappings.getCloseForward(currentActivity, lesson.getLessonId()); - } else { - // Set activity as complete - progress = learnerService.completeActivity(learnerId, currentActivity,progress); - } - - if ( currentActivity.isFloating() ) - return actionMappings.getCloseForward(currentActivity, lesson.getLessonId()); - - LearningWebUtil.putActivityInRequest(request, progress.getNextActivity(), learnerService); - LearningWebUtil.putLearnerProgressInRequest(request,progress); - return actionMappings.getProgressForward(progress, redirect, false, request, learnerService); + ActivityMapping actionMappings, LearnerProgress currentProgress, Activity currentActivity, + Integer learnerId, ICoreLearnerService learnerService, boolean redirect) throws LearnerServiceException, + UnsupportedEncodingException { + + LearnerProgress progress = currentProgress; + Lesson lesson = progress.getLesson(); + + if (currentActivity == null) { + progress = learnerService.joinLesson(learnerId, lesson.getLessonId()); + } else if (progress.getCompletedActivities().containsKey(currentActivity)) { + return actionMappings.getCloseForward(currentActivity, lesson.getLessonId()); + } else { + // Set activity as complete + progress = learnerService.completeActivity(learnerId, currentActivity, progress); + } + + if (currentActivity.isFloating()) + return actionMappings.getCloseForward(currentActivity, lesson.getLessonId()); + + LearningWebUtil.putActivityInRequest(request, progress.getNextActivity(), learnerService); + LearningWebUtil.putLearnerProgressInRequest(request, progress); + return actionMappings.getProgressForward(progress, redirect, false, request, learnerService); } - - /** - * Get the ActionMappings. - */ - public static ActivityMapping getActivityMapping(ServletContext context) { - WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(context); - return (ActivityMapping)wac.getBean("activityMapping"); + + /** + * Get the ActionMappings. + */ + public static ActivityMapping getActivityMapping(ServletContext context) { + WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(context); + return (ActivityMapping) wac.getBean("activityMapping"); + } + + /** Setup the progress string, version and lesson id in the activityForm. */ + public static void setupProgressInRequest(ActivityForm activityForm, HttpServletRequest request, + LearnerProgress learnerProgress) { + + putLearnerProgressInRequest(request, learnerProgress); + + // Calculate the progress summary. On join this method gets called twice, and we + // only want to calculate once + String progressSummary = activityForm.getProgressSummary(); + if (progressSummary == null) { + progressSummary = getProgressSummary(learnerProgress); + activityForm.setProgressSummary(progressSummary); } - - /** Setup the progress string, version and lesson id in the activityForm. */ - public static void setupProgressInRequest(ActivityForm activityForm, HttpServletRequest request, LearnerProgress learnerProgress) { - - putLearnerProgressInRequest(request, learnerProgress); - - // Calculate the progress summary. On join this method gets called twice, and we - // only want to calculate once - String progressSummary = activityForm.getProgressSummary(); - if ( progressSummary == null ) { - progressSummary = getProgressSummary(learnerProgress); - activityForm.setProgressSummary(progressSummary); - } - - Lesson currentLesson = learnerProgress.getLesson(); - if(currentLesson != null){ - activityForm.setLessonID(currentLesson.getLessonId()); - - LearningDesign currentDesign = currentLesson.getLearningDesign(); - if(currentDesign != null) - activityForm.setVersion(currentDesign.getDesignVersion()); - } - - - if(log.isDebugEnabled()) - log.debug("Entering activity: progress summary is "+activityForm.getProgressSummary()); - + + Lesson currentLesson = learnerProgress.getLesson(); + if (currentLesson != null) { + activityForm.setLessonID(currentLesson.getLessonId()); + + LearningDesign currentDesign = currentLesson.getLearningDesign(); + if (currentDesign != null) + activityForm.setVersion(currentDesign.getDesignVersion()); } - - /** Setup the progress string, version and lesson id in the actionForm. The values will go in the map with the - * keys "progressSummary", "lessonID", "version". */ - public static void setupProgressInRequest(DynaActionForm actionForm, HttpServletRequest request, LearnerProgress learnerProgress) { - - putLearnerProgressInRequest(request, learnerProgress); - // Calculate the progress summary. On join this method gets called twice, and we - // only want to calculate once - String progressSummary = (String) actionForm.get("progressSummary"); - if ( progressSummary == null ) { - progressSummary = getProgressSummary(learnerProgress); - actionForm.set("progressSummary",progressSummary); - } - - Lesson currentLesson = learnerProgress.getLesson(); - if(currentLesson != null){ - actionForm.set("lessonID",currentLesson.getLessonId()); - - LearningDesign currentDesign = currentLesson.getLearningDesign(); - if(currentDesign != null) - actionForm.set("version",currentDesign.getDesignVersion()); - } - - - if(log.isDebugEnabled()) - log.debug("Entering activity: progress summary is "+actionForm.get("progressSummary")); - + if (log.isDebugEnabled()) + log.debug("Entering activity: progress summary is " + activityForm.getProgressSummary()); + + } + + /** + * Setup the progress string, version and lesson id in the actionForm. The + * values will go in the map with the keys "progressSummary", "lessonID", + * "version". + */ + public static void setupProgressInRequest(DynaActionForm actionForm, HttpServletRequest request, + LearnerProgress learnerProgress) { + + putLearnerProgressInRequest(request, learnerProgress); + + // Calculate the progress summary. On join this method gets called twice, and we + // only want to calculate once + String progressSummary = (String) actionForm.get("progressSummary"); + if (progressSummary == null) { + progressSummary = getProgressSummary(learnerProgress); + actionForm.set("progressSummary", progressSummary); } - private static String getProgressSummary(LearnerProgress learnerProgress) { - StringBuffer progressSummary = new StringBuffer(100); - if ( learnerProgress == null ) { - progressSummary.append("attempted=&completed=¤t="); - progressSummary.append("&lessonID="); - Lesson currentLesson = learnerProgress.getLesson(); - if(currentLesson != null){ - progressSummary.append(currentLesson.getLessonId()); - } + Lesson currentLesson = learnerProgress.getLesson(); + if (currentLesson != null) { + actionForm.set("lessonID", currentLesson.getLessonId()); + + LearningDesign currentDesign = currentLesson.getLearningDesign(); + if (currentDesign != null) + actionForm.set("version", currentDesign.getDesignVersion()); + } + + if (log.isDebugEnabled()) + log.debug("Entering activity: progress summary is " + actionForm.get("progressSummary")); + + } + + private static String getProgressSummary(LearnerProgress learnerProgress) { + StringBuffer progressSummary = new StringBuffer(100); + if (learnerProgress == null) { + progressSummary.append("attempted=&completed=¤t="); + progressSummary.append("&lessonID="); + Lesson currentLesson = learnerProgress.getLesson(); + if (currentLesson != null) { + progressSummary.append(currentLesson.getLessonId()); + } + } else { + progressSummary.append("attempted="); + boolean first = true; + for (Object obj : learnerProgress.getAttemptedActivities().keySet()) { + Activity activity = (Activity) obj; + if (!first) { + progressSummary.append("_"); } else { - progressSummary.append("attempted="); - boolean first = true; - for (Object obj : learnerProgress.getAttemptedActivities().keySet()) { - Activity activity = (Activity ) obj; - if ( ! first ) { - progressSummary.append("_"); - } else { - first = false; - } - progressSummary.append(activity.getActivityId()); - } - - progressSummary.append("&completed="); - first = true; - for ( Object obj : learnerProgress.getCompletedActivities().keySet() ) { - Activity activity = (Activity ) obj; - if ( ! first ) { - progressSummary.append("_"); - } else { - first = false; - } - progressSummary.append(activity.getActivityId()); - } + first = false; + } + progressSummary.append(activity.getActivityId()); + } - progressSummary.append("¤t="); - Activity currentActivity = learnerProgress.getCurrentActivity(); - if ( currentActivity != null ) { - progressSummary.append(currentActivity.getActivityId()); - } - + progressSummary.append("&completed="); + first = true; + for (Object obj : learnerProgress.getCompletedActivities().keySet()) { + Activity activity = (Activity) obj; + if (!first) { + progressSummary.append("_"); + } else { + first = false; } - return progressSummary.toString(); + progressSummary.append(activity.getActivityId()); + } + + progressSummary.append("¤t="); + Activity currentActivity = learnerProgress.getCurrentActivity(); + if (currentActivity != null) { + progressSummary.append(currentActivity.getActivityId()); + } + } + return progressSummary.toString(); + } - public static ActivityURL getActivityURL( ActivityMapping activityMapping, LearnerProgress learnerProgress, Activity activity, boolean defaultURL, boolean isFloating) { - ActivityURL activityURL = new ActivityURL(); - String url = activityMapping.getActivityURL(activity); - activityURL.setUrl(url); - activityURL.setActivityId(activity.getActivityId()); - activityURL.setTitle(activity.getTitle()); - activityURL.setDescription(activity.getDescription()); - - byte status = learnerProgress.getProgressState(activity); - activityURL.setStatus(status); - if ( status == LearnerProgress.ACTIVITY_COMPLETED) { - activityURL.setComplete(true); - } - activityURL.setFloating(isFloating); - activityURL.setDefaultURL(defaultURL); - return activityURL; + public static ActivityURL getActivityURL(ActivityMapping activityMapping, LearnerProgress learnerProgress, + Activity activity, boolean defaultURL, boolean isFloating) { + ActivityURL activityURL = new ActivityURL(); + String url = activityMapping.getActivityURL(activity); + activityURL.setUrl(url); + activityURL.setActivityId(activity.getActivityId()); + activityURL.setTitle(activity.getTitle()); + activityURL.setDescription(activity.getDescription()); + + byte status = learnerProgress.getProgressState(activity); + activityURL.setStatus(status); + if (status == LearnerProgress.ACTIVITY_COMPLETED) { + activityURL.setComplete(true); } + activityURL.setFloating(isFloating); + activityURL.setDefaultURL(defaultURL); + return activityURL; + } }