Index: lams_build/lib/lams/lams-monitoring.jar =================================================================== diff -u -rafc26d0befd7d03913fa35c462d9d9a0cbefc83f -r8b271e1b06efcbe0b323b7071cdecf693e322c60 Binary files differ Index: lams_common/src/java/org/lamsfoundation/lams/learningdesign/Group.java =================================================================== diff -u -r920894ca746cba5e080023c5cc80167d64d1653d -r8b271e1b06efcbe0b323b7071cdecf693e322c60 --- lams_common/src/java/org/lamsfoundation/lams/learningdesign/Group.java (.../Group.java) (revision 920894ca746cba5e080023c5cc80167d64d1653d) +++ lams_common/src/java/org/lamsfoundation/lams/learningdesign/Group.java (.../Group.java) (revision 8b271e1b06efcbe0b323b7071cdecf693e322c60) @@ -274,13 +274,7 @@ */ public boolean hasLearner(User learner) { - for(Iterator i=this.getUsers().iterator();i.hasNext();) - { - User user = (User)i.next(); - if(user.getUserId().intValue()==learner.getUserId().intValue()) - return true; - } - return false; + return this.getUsers().contains(learner); } /** Index: lams_common/src/java/org/lamsfoundation/lams/lesson/LessonClass.java =================================================================== diff -u -r315fad9f727a91c28b274654eab02cac89d76b9c -r8b271e1b06efcbe0b323b7071cdecf693e322c60 --- lams_common/src/java/org/lamsfoundation/lams/lesson/LessonClass.java (.../LessonClass.java) (revision 315fad9f727a91c28b274654eab02cac89d76b9c) +++ lams_common/src/java/org/lamsfoundation/lams/lesson/LessonClass.java (.../LessonClass.java) (revision 8b271e1b06efcbe0b323b7071cdecf693e322c60) @@ -23,6 +23,7 @@ /* $$Id$$ */ package org.lamsfoundation.lams.lesson; +import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import java.util.Set; @@ -119,28 +120,53 @@ if ( user == null ) return false; - // should be one ordinary group for lesson class, and this is all the learners in the lesson class - Group learnersGroup = getLearnersGroup(); - if ( learnersGroup == null ) { - Organisation lessonOrganisation = getLesson() != null ? getLesson().getOrganisation() : null; - if ( lessonOrganisation == null ) { - log.warn("Adding a learner to a lesson class with no related organisation. Learner group name will be \'learners'."); - } - String learnerGroupName = lessonOrganisation != null ? lessonOrganisation.getName() : ""; - learnerGroupName = learnerGroupName + "learners"; - Set users = new HashSet(); - users.add(user); - getGroups().add(Group.createLearnerGroup(this, learnerGroupName,users)); - } - + Group learnersGroup = createLearnerGroupIfMissing(); + if ( ! learnersGroup.hasLearner(user) ) { learnersGroup.getUsers().add(user); return true; } return false; } - public Group getLearnersGroup() { + /** When the users's are added from an external LMS e.g. Moodle, the Learner Group may not exist. + * If that happens, then this code will ensure that there is a learners group, etc and so adding + * a user won't throw an exception. This is just fallback code!!!! + * @return the learner group + */ + private Group createLearnerGroupIfMissing() { + Group learnersGroup = getLearnersGroup(); + if ( learnersGroup == null ) { + // should be one ordinary group for lesson class, and this is all the learners in the lesson class + Organisation lessonOrganisation = getLesson() != null ? getLesson().getOrganisation() : null; + if ( lessonOrganisation == null ) { + log.warn("Adding a learner to a lesson class with no related organisation. Learner group name will be \'learners'."); + } + String learnerGroupName = lessonOrganisation != null ? lessonOrganisation.getName() : ""; + learnerGroupName = learnerGroupName + "learners"; + getGroups().add(Group.createLearnerGroup(this, learnerGroupName,new HashSet())); + learnersGroup = getLearnersGroup(); + } + return learnersGroup; + } + + /** + * Add one or more learners to the lesson class. Doesn't bother checking for duplicates as it goes into a set, + * and User does a check on userID field for equals anyway. + * @return number of learners added + */ + public int addLearners(Collection newLearners) { + if ( newLearners == null ) + return 0; + + Group learnersGroup = createLearnerGroupIfMissing(); + int originalNumber = learnersGroup.getUsers().size(); + learnersGroup.getUsers().addAll(newLearners); + int newNumber = learnersGroup.getUsers().size(); + return originalNumber - newNumber; + } + + public Group getLearnersGroup() { Group learnersGroup = null; Iterator iter = getGroups().iterator(); while (learnersGroup==null && iter.hasNext()) { @@ -165,16 +191,7 @@ // should be one ordinary group for lesson class, and this is all the learners in the lesson class Group staffGroup = getStaffGroup(); if ( staffGroup == null ) { - Organisation lessonOrganisation = getLesson() != null ? getLesson().getOrganisation() : null; - if ( lessonOrganisation == null ) { - log.warn("Adding a staff member to a lesson class with no related organisation. Staff group name will be \'staff\'."); - } - String staffGroupName = lessonOrganisation != null ? lessonOrganisation.getName() : ""; - staffGroupName = staffGroupName + "staff"; - Set users = new HashSet(); - users.add(user); - setStaffGroup(Group.createStaffGroup(this, staffGroupName,users)); - staffGroup = getStaffGroup(); + staffGroup = createStaffGroupIfMissing(); } if ( ! staffGroup.hasLearner(user) ) { @@ -185,4 +202,41 @@ } + /** + * Add one or more staff members to the lesson class. Doesn't bother checking for duplicates it goes + * into a set, and User does a check on userID field for equals anyway. + * @return number of learners added + */ + public int addStaffMembers(Collection newStaff) { + if ( newStaff == null ) + return 0; + + Group staffGroup = createStaffGroupIfMissing(); + int originalNumber = staffGroup.getUsers().size(); + staffGroup.getUsers().addAll(newStaff); + int newNumber = staffGroup.getUsers().size(); + return originalNumber - newNumber; + } + + /** + * When the users's are added from an external LMS e.g. Moodle, the Staff Group may not exist. + * If that happens, then this code will ensure that there is a learners group, etc and so adding + * a user won't throw an exception. This is just fallback code!!!! + * @return the staff group + */ + private Group createStaffGroupIfMissing() { + Group staffGroup = getStaffGroup(); + if ( staffGroup == null ) { + Organisation lessonOrganisation = getLesson() != null ? getLesson().getOrganisation() : null; + if ( lessonOrganisation == null ) { + log.warn("Adding a staff member to a lesson class with no related organisation. Staff group name will be \'staff\'."); + } + String staffGroupName = lessonOrganisation != null ? lessonOrganisation.getName() : ""; + staffGroupName = staffGroupName + "staff"; + setStaffGroup(Group.createStaffGroup(this, staffGroupName,new HashSet())); + staffGroup = getStaffGroup(); + } + return staffGroup; + } + } \ No newline at end of file Index: lams_common/src/java/org/lamsfoundation/lams/lesson/service/ILessonService.java =================================================================== diff -u -r2f232c2fe9781a2deca52b5782759e05fe25f6b9 -r8b271e1b06efcbe0b323b7071cdecf693e322c60 --- lams_common/src/java/org/lamsfoundation/lams/lesson/service/ILessonService.java (.../ILessonService.java) (revision 2f232c2fe9781a2deca52b5782759e05fe25f6b9) +++ lams_common/src/java/org/lamsfoundation/lams/lesson/service/ILessonService.java (.../ILessonService.java) (revision 8b271e1b06efcbe0b323b7071cdecf693e322c60) @@ -24,6 +24,7 @@ package org.lamsfoundation.lams.lesson.service; +import java.util.Collection; import java.util.List; import org.lamsfoundation.lams.learningdesign.GroupingActivity; @@ -145,13 +146,57 @@ * @paran userId new learner id * @return true if added user, returns false if the user already a learner and hence not added. */ - public boolean addLearner(Long lessonId, Integer userId); + public boolean addLearner(Long lessonId, Integer userId) throws LessonServiceException; /** + * Add a set of learners to the lesson class. + * + * If version of the method is designed to be called from Moodle or some other external system, + * and is less efficient in that it has to look up the user from the user id. + * If we don't do this, then we may get a a session closed issue if this code is called from the + * LoginRequestValve (as the users will be from a previous session) + * + * @param lessonId new learner id + * @param userIds array of new learner ids + */ + public void addLearners(Long lessonId, Integer[] userIds) throws LessonServiceException; + + /** + * Add a set of learners to the lesson class. To be called within LAMS - see + * addLearners(Long lessonId, Integer[] userIds) if calling from an external system. + * + * @param lesson lesson + * @param users the users to add as learners + */ + public void addLearners(Lesson lesson, Collection users) throws LessonServiceException; + + + /** * Add a new staff member to the lesson class. Checks for duplicates. * @paran userId new learner id * @return true if added user, returns false if the user already a staff member and hence not added. */ - public boolean addStaffMember(Long lessonId, Integer userId); + public boolean addStaffMember(Long lessonId, Integer userId) throws LessonServiceException; + /** + * Add a set of staff to the lesson class. + * + * If version of the method is designed to be called from Moodle or some other external system, + * and is less efficient in that it has to look up the user from the user id. + * If we don't do this, then we may get a a session closed issue if this code is called from the + * LoginRequestValve (as the users will be from a previous session) + * + * @param lessonId + * @param userIds array of new staff ids + */ + public void addStaffMembers(Long lessonId, Integer[] userIds) throws LessonServiceException; + + /** + * Add a set of staff members to the lesson class. To be called within LAMS - see + * addLearners(Long lessonId, Integer[] userIds) if calling from an external system. + * + * @param lesson lesson + * @param users the users to add as learners + */ + public void addStaffMembers(Lesson lesson, Collection users) throws LessonServiceException; } \ No newline at end of file Index: lams_common/src/java/org/lamsfoundation/lams/lesson/service/LessonService.java =================================================================== diff -u -r2f232c2fe9781a2deca52b5782759e05fe25f6b9 -r8b271e1b06efcbe0b323b7071cdecf693e322c60 --- lams_common/src/java/org/lamsfoundation/lams/lesson/service/LessonService.java (.../LessonService.java) (revision 2f232c2fe9781a2deca52b5782759e05fe25f6b9) +++ lams_common/src/java/org/lamsfoundation/lams/lesson/service/LessonService.java (.../LessonService.java) (revision 8b271e1b06efcbe0b323b7071cdecf693e322c60) @@ -24,7 +24,10 @@ /* $$Id$$ */ package org.lamsfoundation.lams.lesson.service; +import java.util.Collection; +import java.util.HashSet; import java.util.List; +import java.util.Set; import org.apache.log4j.Logger; import org.lamsfoundation.lams.dao.IBaseDAO; @@ -346,9 +349,65 @@ lessonClassDAO.updateLessonClass(lessonClass); } return ret; -} + } /** + * Add a set of learners to the lesson class. + * + * If version of the method is designed to be called from Moodle or some other external system, + * and is less efficient in that it has to look up the user from the user id. + * If we don't do this, then we may get a a session closed issue if this code is called from the + * LoginRequestValve (as the users will be from a previous session) + * + * @param lessonId + * @param userIds array of new learner ids + */ + public void addLearners(Long lessonId, Integer[] userIds) throws LessonServiceException { + + Lesson lesson = lessonDAO.getLesson(lessonId); + if ( lesson == null ) { + throw new LessonServiceException("Lesson "+lessonId+" does not exist. Unable to add learner to lesson."); + } + LessonClass lessonClass = lesson.getLessonClass(); + if ( lessonClass == null ) { + throw new LessonServiceException("Lesson class for "+lessonId+" does not exist. Unable to add learner to lesson."); + } + + // initialise the lesson group, or we might get a lazy loading error in the future + // when logging in from an external system. Should only be two groups - learner and staff + // yes this is a bit of a hack! + Group learnersGroup = lessonClass.getLearnersGroup(); + if ( learnersGroup != null ) + lessonDAO.initialize(learnersGroup); + + Set users = new HashSet(); + for ( Integer userId: userIds) { + User user = (User) baseDAO.find(User.class,userId); + users.add(user); + } + addLearners(lesson, users); + } + + /** + * Add a set of learners to the lesson class. To be called within LAMS - see + * addLearners(Long lessonId, Integer[] userIds) if calling from an external system. + * + * @param lesson lesson + * @param users the users to add as learners + */ + public void addLearners(Lesson lesson, Collection users) throws LessonServiceException { + + LessonClass lessonClass = lesson.getLessonClass(); + int numAdded = lessonClass.addLearners(users); + if ( numAdded > 0 ) { + lessonClassDAO.updateLessonClass(lessonClass); + } + if ( log.isDebugEnabled() ) { + log.debug("Added "+numAdded+" learners to lessonClass "+lessonClass.getGroupingId()); + } + } + + /** * Add a new staff member to the lesson class. Checks for duplicates. * @param userId new learner id * @return true if added user, returns false if the user already a staff member and hence not added. @@ -375,4 +434,58 @@ return ret; } + /** + * Add a set of staff to the lesson class. + * + * If version of the method is designed to be called from Moodle or some other external system, + * and is less efficient in that it has to look up the user from the user id. + * If we don't do this, then we may get a a session closed issue if this code is called from the + * LoginRequestValve (as the users will be from a previous session) + * + * @param lessonId + * @param userIds array of new staff ids + */ + public void addStaffMembers(Long lessonId, Integer[] userIds) throws LessonServiceException { + + Lesson lesson = lessonDAO.getLesson(lessonId); + if ( lesson == null ) { + throw new LessonServiceException("Lesson "+lessonId+" does not exist. Unable to add learner to lesson."); + } + LessonClass lessonClass = lesson.getLessonClass(); + if ( lessonClass == null ) { + throw new LessonServiceException("Lesson class for "+lessonId+" does not exist. Unable to add learner to lesson."); + } + + // initialise the lesson group, or we might get a lazy loading error in the future + // when logging in from an external system. Should only be two groups - learner and staff + // yes this is a bit of a hack! + lessonDAO.initialize(lessonClass.getStaffGroup()); + + Set users = new HashSet(); + for ( Integer userId: userIds) { + User user = (User) baseDAO.find(User.class,userId); + users.add(user); + } + addStaffMembers(lesson, users); + } + + /** + * Add a set of staff members to the lesson class. To be called within LAMS - see + * addLearners(Long lessonId, Integer[] userIds) if calling from an external system. + * + * @param lesson lesson + * @param users the users to add as learners + */ + public void addStaffMembers(Lesson lesson, Collection users) throws LessonServiceException { + + LessonClass lessonClass = lesson.getLessonClass(); + int numAdded = lessonClass.addStaffMembers(users); + if ( numAdded > 0 ) { + lessonClassDAO.updateLessonClass(lessonClass); + } + if ( log.isDebugEnabled() ) { + log.debug("Added "+numAdded+" staff members to lessonClass "+lessonClass.getGroupingId()); + } + } + } Index: lams_common/src/java/org/lamsfoundation/lams/resources/lamsauthentication.dtd =================================================================== diff -u --- lams_common/src/java/org/lamsfoundation/lams/resources/lamsauthentication.dtd (revision 0) +++ lams_common/src/java/org/lamsfoundation/lams/resources/lamsauthentication.dtd (revision 8b271e1b06efcbe0b323b7071cdecf693e322c60) @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file Index: lams_monitoring/src/java/org/lamsfoundation/lams/monitoring/service/IMonitoringService.java =================================================================== diff -u -rcc025cd2b501eb8acd8f17b3246660f8f53d493a -r8b271e1b06efcbe0b323b7071cdecf693e322c60 --- lams_monitoring/src/java/org/lamsfoundation/lams/monitoring/service/IMonitoringService.java (.../IMonitoringService.java) (revision cc025cd2b501eb8acd8f17b3246660f8f53d493a) +++ lams_monitoring/src/java/org/lamsfoundation/lams/monitoring/service/IMonitoringService.java (.../IMonitoringService.java) (revision 8b271e1b06efcbe0b323b7071cdecf693e322c60) @@ -107,7 +107,7 @@ * @param staffs a list of staffs who will be in charge of this lesson. * @return the lesson with lesson class and organization */ - public Lesson createLessonClassForLesson(long lessonId, Organisation organisation,String leanerGroupName, List organizationUsers,String staffGroupName, List staffs, Integer userID) throws UserAccessDeniedException; + public Lesson createLessonClassForLesson(long lessonId, Organisation organisation,String leanerGroupName, List organizationUsers,String staffGroupName, List staffs, Integer userID) throws UserAccessDeniedException; /** * Start the specified the lesson. It must be created before calling this Index: lams_monitoring/src/java/org/lamsfoundation/lams/monitoring/service/MonitoringService.java =================================================================== diff -u -rcc025cd2b501eb8acd8f17b3246660f8f53d493a -r8b271e1b06efcbe0b323b7071cdecf693e322c60 --- lams_monitoring/src/java/org/lamsfoundation/lams/monitoring/service/MonitoringService.java (.../MonitoringService.java) (revision cc025cd2b501eb8acd8f17b3246660f8f53d493a) +++ lams_monitoring/src/java/org/lamsfoundation/lams/monitoring/service/MonitoringService.java (.../MonitoringService.java) (revision 8b271e1b06efcbe0b323b7071cdecf693e322c60) @@ -518,40 +518,43 @@ */ public Lesson createLessonClassForLesson(long lessonId, Organisation organisation, - String learnerGroupName, List organizationUsers, - String staffGroupName, List staffs, Integer userId) + String learnerGroupName, List organizationUsers, + String staffGroupName, List staffs, Integer userId) { Lesson newLesson = lessonDAO.getLesson(new Long(lessonId)); if ( newLesson == null) { throw new MonitoringServiceException("Lesson for id="+lessonId+" is missing. Unable to create class for lesson."); } checkOwnerOrStaffMember(userId, newLesson, "create lesson class"); - // make sure lesson isn't started - if ( newLesson.isLessonStarted() ) { - throw new MonitoringServiceException("Lesson for id="+lessonId+" has been started. Unable to create/change class for lesson."); - } + // if lesson isn't started, can add and remove users, so its just easier to recreate the lesson class + if ( ! newLesson.isLessonStarted() ) { - if ( newLesson == null) { - throw new MonitoringServiceException("Lesson for id="+lessonId+" is missing. Unable to create class for lesson."); - } + if ( newLesson == null) { + throw new MonitoringServiceException("Lesson for id="+lessonId+" is missing. Unable to create class for lesson."); + } + + LessonClass oldLessonClass = newLesson.getLessonClass(); + + LessonClass newLessonClass = this.createLessonClass(organisation, + learnerGroupName, + organizationUsers, + staffGroupName, + staffs, + newLesson); + newLessonClass.setLesson(newLesson); + newLesson.setLessonClass(newLessonClass); + newLesson.setOrganisation(organisation); + + lessonDAO.updateLesson(newLesson); + + if ( oldLessonClass != null ) { + lessonClassDAO.deleteLessonClass(oldLessonClass); + } - LessonClass oldLessonClass = newLesson.getLessonClass(); - - LessonClass newLessonClass = this.createLessonClass(organisation, - learnerGroupName, - organizationUsers, - staffGroupName, - staffs, - newLesson); - newLessonClass.setLesson(newLesson); - newLesson.setLessonClass(newLessonClass); - newLesson.setOrganisation(organisation); - - lessonDAO.updateLesson(newLesson); - - if ( oldLessonClass != null ) { - lessonClassDAO.deleteLessonClass(oldLessonClass); + } else { // if started, can only add users + lessonService.addLearners(newLesson, organizationUsers); + lessonService.addStaffMembers(newLesson, staffs); } return newLesson;