Index: lams_central/src/java/org/lamsfoundation/lams/web/OrganisationGroupAction.java =================================================================== diff -u -r3ead910e766fb518a08fc2d2cf53382431313cbf -r0b087f6386e7e8cfd72dd34e3eec37533cb97d5f --- lams_central/src/java/org/lamsfoundation/lams/web/OrganisationGroupAction.java (.../OrganisationGroupAction.java) (revision 3ead910e766fb518a08fc2d2cf53382431313cbf) +++ lams_central/src/java/org/lamsfoundation/lams/web/OrganisationGroupAction.java (.../OrganisationGroupAction.java) (revision 0b087f6386e7e8cfd72dd34e3eec37533cb97d5f) @@ -49,6 +49,7 @@ import org.lamsfoundation.lams.learningdesign.Grouping; import org.lamsfoundation.lams.learningdesign.GroupingActivity; import org.lamsfoundation.lams.lesson.Lesson; +import org.lamsfoundation.lams.lesson.service.ILessonService; import org.lamsfoundation.lams.monitoring.web.GroupingAJAXAction; import org.lamsfoundation.lams.usermanagement.OrganisationGroup; import org.lamsfoundation.lams.usermanagement.OrganisationGrouping; @@ -161,6 +162,7 @@ private static Logger log = Logger.getLogger(OrganisationGroupAction.class); private static IUserManagementService userManagementService; + private static ILessonService lessonService; private static final String MAPPING_VIEW_GROUPINGS = "viewGroupings"; private static final String MAPPING_VIEW_GROUPS = "viewGroups"; @@ -172,7 +174,7 @@ public ActionForward viewGroupings(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws JSONException { Long activityID = WebUtil.readLongParam(request, AttributeNames.PARAM_ACTIVITY_ID, true); - boolean lessonGroupsExist = getLessonGroups(request, activityID, false) != null; + boolean lessonGroupsExist = getLessonGrouping(request, activityID, false) != null; if (lessonGroupsExist) { // this is lesson mode and user have already chosen a grouping before, so show it return viewGroups(mapping, form, request, response); @@ -200,7 +202,7 @@ + organisationId); } request.setAttribute(AttributeNames.PARAM_ORGANISATION_ID, organisationId); - request.setAttribute("canEdit", isGroupSuperuser || activityID != null); + request.setAttribute("canEdit", isGroupSuperuser || (activityID != null)); Set orgGroupingDTOs = new TreeSet(); List orgGroupings = getUserManagementService().findByProperty(OrganisationGrouping.class, @@ -240,8 +242,8 @@ OrganisationGroupAction.log.debug("Displaying course groups for user " + userId + " and organisation " + organisationId); } - Long activityID = WebUtil.readLongParam(request, AttributeNames.PARAM_ACTIVITY_ID, true); - request.setAttribute("canEdit", isGroupSuperuser || activityID != null); + Long activityId = WebUtil.readLongParam(request, AttributeNames.PARAM_ACTIVITY_ID, true); + request.setAttribute("canEdit", isGroupSuperuser || (activityId != null)); JSONObject orgGroupingJSON = new JSONObject(); orgGroupingJSON.put("organisationId", organisationId); @@ -259,8 +261,24 @@ } // check if any groups already exist in this grouping - Set lessonGroups = getLessonGroups(request, activityID, true); + Grouping lessonGrouping = getLessonGrouping(request, activityId, true); + Set lessonGroups = lessonGrouping == null ? null : lessonGrouping.getGroups(); + if ((activityId != null) && (orgGroupingId != null) && isDefaultChosenGrouping(lessonGroups)) { + if (OrganisationGroupAction.log.isDebugEnabled()) { + OrganisationGroupAction.log.debug("Removing default groups for grouping " + orgGroupingId); + } + Set groupIDs = new HashSet(lessonGroups.size()); + for (Group group : lessonGroups) { + groupIDs.add(group.getGroupId()); + } + for (Long groupId : groupIDs) { + getLessonService().removeGroup(lessonGrouping, groupId); + } + + lessonGroups = null; + } + JSONArray orgGroupsJSON = null; Vector learners = getUserManagementService().getUsersFromOrganisationByRole(organisationId, Role.LEARNER, false, true); @@ -453,34 +471,42 @@ * Checks if lesson-level groups exist for the given activity. */ @SuppressWarnings("unchecked") - private Set getLessonGroups(HttpServletRequest request, Long activityID, boolean allowEmpty) { + private Grouping getLessonGrouping(HttpServletRequest request, Long activityID, boolean allowDefault) { if (activityID != null) { GroupingActivity groupingActivity = (GroupingActivity) getUserManagementService().findById( GroupingActivity.class, activityID); Grouping grouping = groupingActivity.getCreateGrouping(); - if ((grouping != null) && (grouping.getGroups() != null) && !grouping.getGroups().isEmpty()) { + if ((grouping != null) && (grouping.getGroups() != null)) { + Set groups = grouping.getGroups(); // not very obvious place to use it, but it made most sense boolean isUsedForBranching = grouping.isUsedForBranching(); request.setAttribute(GroupingAJAXAction.PARAM_USED_FOR_BRANCHING, isUsedForBranching); - - // to check if a grouping exists at all, empty groups are allowed - // to check if a grouping can be discarded, there must be no groups at all - if (allowEmpty || isUsedForBranching) { - return grouping.getGroups(); - } - - for (Group existingGroup : (Set) grouping.getGroups()) { - if (!existingGroup.getUsers().isEmpty()) { - return grouping.getGroups(); - } - } + + // check if it is immutable (for branching) or default groups are allowed + return !groups.isEmpty() && (isUsedForBranching || allowDefault || !isDefaultChosenGrouping(groups)) ? grouping + : null; } } return null; } + /** + * Check if the give groups are default for chosen grouping. + */ + private boolean isDefaultChosenGrouping(Set groups) { + if ((groups == null) || (groups.size() != 2)) { + return false; + } + for (Group group : groups) { + if (!group.getUsers().isEmpty()) { + return false; + } + } + return true; + } + private UserDTO getUserDTO() { HttpSession ss = SessionManager.getSession(); return (UserDTO) ss.getAttribute(AttributeNames.USER); @@ -495,4 +521,13 @@ } return OrganisationGroupAction.userManagementService; } + + private ILessonService getLessonService() { + if (OrganisationGroupAction.lessonService == null) { + WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServlet() + .getServletContext()); + OrganisationGroupAction.lessonService = (ILessonService) ctx.getBean("lessonService"); + } + return OrganisationGroupAction.lessonService; + } } \ No newline at end of file Index: lams_central/web/includes/javascript/orgGroup.js =================================================================== diff -u -r3ead910e766fb518a08fc2d2cf53382431313cbf -r0b087f6386e7e8cfd72dd34e3eec37533cb97d5f --- lams_central/web/includes/javascript/orgGroup.js (.../orgGroup.js) (revision 3ead910e766fb518a08fc2d2cf53382431313cbf) +++ lams_central/web/includes/javascript/orgGroup.js (.../orgGroup.js) (revision 0b087f6386e7e8cfd72dd34e3eec37533cb97d5f) @@ -10,12 +10,14 @@ $.each(grouping.groups, function(){ // if a course grouping is being copied as lesson grouping, // do not use group IDs as they will be created when assiging users to created groups - var groupId = !lessonMode || skipInitialAssigning ? this.groupId : null; + var groupId = !lessonMode || skipAssigningWhenCreatingGroup ? this.groupId : null; var group = addGroup(groupId, this.name, this.users); if (this.locked) { markGroupLocked(group); } }); + // after initial group creation, all new groups will be created on server + skipAssigningWhenCreatingGroup = false; // initialisation based on mode if (lessonMode) { @@ -72,8 +74,9 @@ group.find('input').val(name); - if (canEdit) { - group.insertBefore('#newGroupPlaceholder'); + var newGroupPlaceholder = $('#newGroupPlaceholder'); + if (newGroupPlaceholder.length > 0) { + group.insertBefore(newGroupPlaceholder); } else { // there is no placeholder in read-only mode $('#groupsCell').append(group); @@ -89,6 +92,9 @@ * Makes a list of users and adds drag&drop functionality to them. */ function fillGroup(users, container) { + // make calls to server or just create users in HTML + var createOnServer = lessonMode && !skipAssigningWhenCreatingGroup + && container.attr('id') != 'unassignedUserCell'; if (users) { // create user DIVs $.each(users, function(index, userJSON) { @@ -156,7 +162,7 @@ }); } - if (lessonMode && !skipInitialAssigning && container.attr('id') != 'unassignedUserCell') { + if (createOnServer) { // copy course groups as lesson groups, creating new instances if (assignUsersToGroup([userJSON.id], container)) { $('.userContainer', container).append(userDiv); @@ -170,6 +176,9 @@ }); sortUsers(container); + } else if (createOnServer) { + // just createa an empty group + assignUsersToGroup(null, container); } $('.sortUsersButton', container).click(function(){ @@ -446,7 +455,7 @@ 'activityID' : groupingActivityId, 'groupID' : groupId, 'name' : groupName, - 'members' : userIds.join() + 'members' : userIds ? userIds.join() : null }, type : 'POST', success : function(response) { @@ -471,7 +480,16 @@ */ function toggleBackButton() { if (lessonMode) { - $('#backButton').button('option', 'disabled', $('.groupContainer[groupId]').length > 0); + var backButton = $('#backButton'); + var disabled = $('.groupContainer[groupId]').length > 0; + if (disabled) { + backButton.off('click').button('option', 'disabled', true); + } else { + backButton.click(function(){ + document.location.href = LAMS_URL + 'OrganisationGroup.do?method=viewGroupings&activityID=' + + groupingActivityId + '&organisationID=' + grouping.organisationId; + }).button('option', 'disabled', false); + } } } Index: lams_central/web/orgGroup.jsp =================================================================== diff -u -r3ead910e766fb518a08fc2d2cf53382431313cbf -r0b087f6386e7e8cfd72dd34e3eec37533cb97d5f --- lams_central/web/orgGroup.jsp (.../orgGroup.jsp) (revision 3ead910e766fb518a08fc2d2cf53382431313cbf) +++ lams_central/web/orgGroup.jsp (.../orgGroup.jsp) (revision 0b087f6386e7e8cfd72dd34e3eec37533cb97d5f) @@ -23,7 +23,7 @@ var lessonMode = ${lessonMode}; // This attribute can be empty. // When true, it means that it shows already existing groups so no calls to server are needed. - var skipInitialAssigning = ${skipInitialAssigning eq true}; + var skipAssigningWhenCreatingGroup = ${skipInitialAssigning eq true}; // This attribute can be empty. // When true, it means that groups can not be added or removed, but user can be still moved. var usedForBranching = ${usedForBranching eq true}; Index: lams_monitoring/src/java/org/lamsfoundation/lams/monitoring/web/GroupingAJAXAction.java =================================================================== diff -u -r3ead910e766fb518a08fc2d2cf53382431313cbf -r0b087f6386e7e8cfd72dd34e3eec37533cb97d5f --- lams_monitoring/src/java/org/lamsfoundation/lams/monitoring/web/GroupingAJAXAction.java (.../GroupingAJAXAction.java) (revision 3ead910e766fb518a08fc2d2cf53382431313cbf) +++ lams_monitoring/src/java/org/lamsfoundation/lams/monitoring/web/GroupingAJAXAction.java (.../GroupingAJAXAction.java) (revision 0b087f6386e7e8cfd72dd34e3eec37533cb97d5f) @@ -36,6 +36,7 @@ import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; +import org.apache.commons.lang.StringUtils; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; @@ -544,42 +545,44 @@ boolean result = true; Long activityID = WebUtil.readLongParam(request, AttributeNames.PARAM_ACTIVITY_ID); - String members = WebUtil.readStrParam(request, GroupingAJAXAction.PARAM_MEMBERS); - String[] membersSplit = members.split(","); + String membersParam = WebUtil.readStrParam(request, GroupingAJAXAction.PARAM_MEMBERS, true); + String[] members = StringUtils.isBlank(membersParam) ? null : membersParam.split(","); // remove users from current group IMonitoringService monitoringService = MonitoringServiceProxy.getMonitoringService(getServlet() .getServletContext()); - GroupingActivity groupingActivity = monitoringService.getGroupingActivityById(activityID); - Grouping grouping = groupingActivity.getCreateGrouping(); - User exampleUser = (User) MonitoringServiceProxy.getUserManagementService(getServlet().getServletContext()) - .findById(User.class, Integer.valueOf(membersSplit[0])); - Group group = grouping.getGroupBy(exampleUser); - // null group means that user is not assigned anywhere in this grouping - if (!group.isNull()) { - // check if user can be moved outside of this group - result = group.mayBeDeleted(); + if (members != null) { + GroupingActivity groupingActivity = monitoringService.getGroupingActivityById(activityID); + Grouping grouping = groupingActivity.getCreateGrouping(); + User exampleUser = (User) MonitoringServiceProxy.getUserManagementService(getServlet().getServletContext()) + .findById(User.class, Integer.valueOf(members[0])); + Group group = grouping.getGroupBy(exampleUser); + // null group means that user is not assigned anywhere in this grouping + if (!group.isNull()) { + // check if user can be moved outside of this group + result = group.mayBeDeleted(); - if (result) { - if (LamsDispatchAction.log.isDebugEnabled()) { - LamsDispatchAction.log.debug("Removing users " + members.toString() + " from group " - + group.getGroupId() + " in activity " + activityID); + if (result) { + if (LamsDispatchAction.log.isDebugEnabled()) { + LamsDispatchAction.log.debug("Removing users " + membersParam.toString() + " from group " + + group.getGroupId() + " in activity " + activityID); + } + + try { + monitoringService.removeUsersFromGroup(activityID, group.getGroupId(), members); + } catch (LessonServiceException e) { + LamsDispatchAction.log.error(e); + result = false; + } } - try { - monitoringService.removeUsersFromGroup(activityID, group.getGroupId(), membersSplit); - } catch (LessonServiceException e) { - LamsDispatchAction.log.error(e); - result = false; + if (!result) { + // let JSP page know that this group became immutable + responseJSON.put("locked", true); } } - - if (!result) { - // let JSP page know that this group became immutable - responseJSON.put("locked", true); - } } - + Long groupID = WebUtil.readLongParam(request, AttributeNames.PARAM_GROUP_ID, true); // no group ID means that it has to be created // group ID = -1 means that user is not being assigned to any new group, i.e. becomse unassigned @@ -589,7 +592,7 @@ if (LamsDispatchAction.log.isDebugEnabled()) { LamsDispatchAction.log.debug("Creating group with name \"" + name + "\" in activity " + activityID); } - group = monitoringService.addGroup(activityID, name, true); + Group group = monitoringService.addGroup(activityID, name, true); if (group == null) { // group creation failed result = false; @@ -600,15 +603,15 @@ } } - if (result) { + if (result && members != null) { if (LamsDispatchAction.log.isDebugEnabled()) { - LamsDispatchAction.log.debug("Adding users " + members.toString() + " to group " + groupID + LamsDispatchAction.log.debug("Adding users " + membersParam.toString() + " to group " + groupID + " in activity " + activityID); } // add users to the given group try { - monitoringService.addUsersToGroup(activityID, groupID, membersSplit); + monitoringService.addUsersToGroup(activityID, groupID, members); } catch (LessonServiceException e) { LamsDispatchAction.log.error(e); result = false;