Index: lams_build/lib/lams/lams.jar =================================================================== diff -u -r2e88e16c3c927a535d8c077f06d01a8a977878d8 -rfbeb734a6ccb4d08e312fbf1b7fc6eb7bae46535 Binary files differ Index: lams_common/.classpath =================================================================== diff -u -rf17ef475206dfa5950b19434ec135b157201c987 -rfbeb734a6ccb4d08e312fbf1b7fc6eb7bae46535 --- lams_common/.classpath (.../.classpath) (revision f17ef475206dfa5950b19434ec135b157201c987) +++ lams_common/.classpath (.../.classpath) (revision fbeb734a6ccb4d08e312fbf1b7fc6eb7bae46535) @@ -4,8 +4,10 @@ - + + + Index: lams_common/src/java/org/lamsfoundation/lams/lesson/LessonClass.java =================================================================== diff -u -r08950e1090443c3423a3d1c587416a2fccd8bbdf -rfbeb734a6ccb4d08e312fbf1b7fc6eb7bae46535 --- lams_common/src/java/org/lamsfoundation/lams/lesson/LessonClass.java (.../LessonClass.java) (revision 08950e1090443c3423a3d1c587416a2fccd8bbdf) +++ lams_common/src/java/org/lamsfoundation/lams/lesson/LessonClass.java (.../LessonClass.java) (revision fbeb734a6ccb4d08e312fbf1b7fc6eb7bae46535) @@ -23,10 +23,14 @@ /* $$Id$$ */ package org.lamsfoundation.lams.lesson; +import java.util.HashSet; +import java.util.Iterator; import java.util.Set; +import org.apache.log4j.Logger; import org.lamsfoundation.lams.learningdesign.Group; import org.lamsfoundation.lams.learningdesign.Grouping; +import org.lamsfoundation.lams.usermanagement.Organisation; import org.lamsfoundation.lams.usermanagement.User; /** @@ -37,6 +41,8 @@ */ public class LessonClass extends Grouping { + private static Logger log = Logger.getLogger(LessonClass.class); + private Group staffGroup; private Lesson lesson; @@ -104,4 +110,83 @@ return staff!=null && staff.hasLearner(user); } + /** + * Add a learner to the lesson class. Checks for duplicates. + * + * @return true if added user, returns false if the user already a learner and hence not added. + */ + public boolean addLearner(User user) { + 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 = null; + Iterator iter = getGroups().iterator(); + if (iter.hasNext()) { + learnersGroup = (Group) iter.next(); + } + 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)); + } + + if ( ! learnersGroup.hasLearner(user) ) { + if ( log.isDebugEnabled() ) { + log.debug("Adding learner "+user.getLogin()+" to LessonClass "+getGroupingId()); + } + learnersGroup.getUsers().add(user); + return true; + } + if ( log.isDebugEnabled() ) { + log.debug("Not adding learner "+user.getLogin()+" to LessonClass "+getGroupingId()+". User is already a learner."); + } + return false; + } + + /** + * Add a staff member to the lesson class. Checks for duplicates. + * + * @return true if added user, returns false if the user already a staff member and hence not added. + */ + public boolean addStaffMember(User user) { + + if ( user == null ) + return false; + + // 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(); + } + + if ( ! staffGroup.hasLearner(user) ) { + if ( log.isDebugEnabled() ) { + log.debug("Adding staff member "+user.getLogin()+" to LessonClass "+getGroupingId()); + } + staffGroup.getUsers().add(user); + return true; + } + if ( log.isDebugEnabled() ) { + log.debug("Not adding staff member "+user.getLogin()+" to LessonClass "+getGroupingId()+". User is already a staff member."); + } + return false; + + } + } \ No newline at end of file Index: lams_common/src/java/org/lamsfoundation/lams/lesson/lessonApplicationContext.xml =================================================================== diff -u -r66c453cb9ccc5964de6f0919b493345e29c37229 -rfbeb734a6ccb4d08e312fbf1b7fc6eb7bae46535 --- lams_common/src/java/org/lamsfoundation/lams/lesson/lessonApplicationContext.xml (.../lessonApplicationContext.xml) (revision 66c453cb9ccc5964de6f0919b493345e29c37229) +++ lams_common/src/java/org/lamsfoundation/lams/lesson/lessonApplicationContext.xml (.../lessonApplicationContext.xml) (revision fbeb734a6ccb4d08e312fbf1b7fc6eb7bae46535) @@ -25,6 +25,7 @@ + Index: lams_common/src/java/org/lamsfoundation/lams/lesson/service/ILessonService.java =================================================================== diff -u -r08950e1090443c3423a3d1c587416a2fccd8bbdf -rfbeb734a6ccb4d08e312fbf1b7fc6eb7bae46535 --- lams_common/src/java/org/lamsfoundation/lams/lesson/service/ILessonService.java (.../ILessonService.java) (revision 08950e1090443c3423a3d1c587416a2fccd8bbdf) +++ lams_common/src/java/org/lamsfoundation/lams/lesson/service/ILessonService.java (.../ILessonService.java) (revision fbeb734a6ccb4d08e312fbf1b7fc6eb7bae46535) @@ -135,4 +135,18 @@ */ public void removeGroup(GroupingActivity groupingActivity, Long groupId) throws LessonServiceException; + /** + * Add a learner to the lesson class. Checks for duplicates. + * @paran user new learner + * @return true if added user, returns false if the user already a learner and hence not added. + */ + public boolean addLearner(Long lessonId, User user); + + /** + * Add a new staff member to the lesson class. Checks for duplicates. + * @paran user new learner + * @return true if added user, returns false if the user already a staff member and hence not added. + */ + public boolean addStaffMember(Long lessonId, User user); + } \ No newline at end of file Index: lams_common/src/java/org/lamsfoundation/lams/lesson/service/LessonService.java =================================================================== diff -u -r08950e1090443c3423a3d1c587416a2fccd8bbdf -rfbeb734a6ccb4d08e312fbf1b7fc6eb7bae46535 --- lams_common/src/java/org/lamsfoundation/lams/lesson/service/LessonService.java (.../LessonService.java) (revision 08950e1090443c3423a3d1c587416a2fccd8bbdf) +++ lams_common/src/java/org/lamsfoundation/lams/lesson/service/LessonService.java (.../LessonService.java) (revision fbeb734a6ccb4d08e312fbf1b7fc6eb7bae46535) @@ -33,6 +33,8 @@ import org.lamsfoundation.lams.learningdesign.dao.IGroupingDAO; import org.lamsfoundation.lams.learningdesign.exception.GroupingException; import org.lamsfoundation.lams.lesson.Lesson; +import org.lamsfoundation.lams.lesson.LessonClass; +import org.lamsfoundation.lams.lesson.dao.ILessonClassDAO; import org.lamsfoundation.lams.lesson.dao.ILessonDAO; import org.lamsfoundation.lams.lesson.dto.LessonDTO; import org.lamsfoundation.lams.lesson.dto.LessonDetailsDTO; @@ -65,6 +67,7 @@ private static Logger log = Logger.getLogger(LessonService.class); private ILessonDAO lessonDAO; + private ILessonClassDAO lessonClassDAO; private IGroupingDAO groupingDAO; private MessageService messageService; @@ -73,6 +76,10 @@ this.lessonDAO = lessonDAO; } + public void setLessonClassDAO(ILessonClassDAO lessonClassDAO) { + this.lessonClassDAO = lessonClassDAO; + } + public void setGroupingDAO(IGroupingDAO groupingDAO) { this.groupingDAO = groupingDAO; } @@ -293,5 +300,50 @@ } } + /** + * Add a learner to the lesson class. Checks for duplicates. + * @param user new learner + * @return true if added user, returns false if the user already a learner and hence not added. + */ + public boolean addLearner(Long lessonId, User user) 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."); + } + + boolean ret = lessonClass.addLearner(user); + if ( ret ) { + lessonClassDAO.updateLessonClass(lessonClass); + } + return ret; +} + + /** + * Add a new staff member to the lesson class. Checks for duplicates. + * @param user new learner + * @return true if added user, returns false if the user already a staff member and hence not added. + */ + public boolean addStaffMember(Long lessonId, User user) { + Lesson lesson = lessonDAO.getLesson(lessonId); + if ( lesson == null ) { + throw new LessonServiceException("Lesson "+lessonId+" does not exist. Unable to add staff member to lesson."); + } + + LessonClass lessonClass = lesson.getLessonClass(); + if ( lessonClass == null ) { + throw new LessonServiceException("Lesson class for "+lessonId+" does not exist. Unable to add staff member to lesson."); + } + + boolean ret = lessonClass.addStaffMember(user); + if ( ret ) { + lessonClassDAO.updateLessonClass(lessonClass); + } + return ret; + } } Index: lams_common/test/java/org/lamsfoundation/lams/lesson/service/TestLessonService.java =================================================================== diff -u --- lams_common/test/java/org/lamsfoundation/lams/lesson/service/TestLessonService.java (revision 0) +++ lams_common/test/java/org/lamsfoundation/lams/lesson/service/TestLessonService.java (revision fbeb734a6ccb4d08e312fbf1b7fc6eb7bae46535) @@ -0,0 +1,136 @@ +/**************************************************************** + * Copyright (C) 2005 LAMS Foundation (http://lamsfoundation.org) + * ============================================================= + * License Information: http://lamsfoundation.org/licensing/lams/2.0/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2.0 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 + * USA + * + * http://www.gnu.org/licenses/gpl.txt + * **************************************************************** + */ +/* $$Id$$ */ +package org.lamsfoundation.lams.lesson.service; + +import java.util.HashSet; +import java.util.Set; + +import org.lamsfoundation.lams.dao.IBaseDAO; +import org.lamsfoundation.lams.learningdesign.Group; +import org.lamsfoundation.lams.lesson.Lesson; +import org.lamsfoundation.lams.lesson.LessonClass; +import org.lamsfoundation.lams.lesson.dao.ILessonClassDAO; +import org.lamsfoundation.lams.lesson.dao.ILessonDAO; +import org.lamsfoundation.lams.test.AbstractLamsTestCase; +import org.lamsfoundation.lams.usermanagement.Organisation; +import org.lamsfoundation.lams.usermanagement.User; +import org.lamsfoundation.lams.usermanagement.service.IUserManagementService; +import org.lamsfoundation.lams.usermanagement.service.UserManagementService; + +public class TestLessonService extends AbstractLamsTestCase { + + private ILessonDAO lessonDAO; + private ILessonClassDAO lessonClassDAO; + private ILessonService lessonService; + private IBaseDAO baseDAO; + protected IUserManagementService userManagementService; + + private static final Long TEST_ORG_ID = new Long(1); + protected static final String MMM_USER_LOGIN = "mmm"; + protected static final String TEST1_USER_LOGIN = "test1"; + + public TestLessonService(String name) { + super(name); + } + + protected void setUp()throws Exception{ + super.setUp(); + lessonService =(ILessonService)context.getBean("lessonService"); + lessonDAO =(ILessonDAO)context.getBean("lessonDAO"); + lessonClassDAO =(ILessonClassDAO)context.getBean("lessonClassDAO"); + baseDAO =(IBaseDAO)context.getBean("baseDAO"); + userManagementService = (UserManagementService)context.getBean("userManagementService"); + } + + protected String getHibernateSessionFactoryName() { + return "coreSessionFactory"; + } + protected String[] getContextConfigLocation() { + return new String[] {"org/lamsfoundation/lams/localApplicationContext.xml", + "org/lamsfoundation/lams/lesson/lessonApplicationContext.xml"}; + } + + public void testAddUser() throws Exception { + + // set up the lesson and lesson class + + User mmm = userManagementService.getUserByLogin(MMM_USER_LOGIN); + User test1 = userManagementService.getUserByLogin(TEST1_USER_LOGIN); + + // this lesson isn't valid as it doesn't have a learning design + Lesson newLesson = Lesson.createNewLessonWithoutClass("test lesson", "test lesson", mmm, null); + lessonDAO.saveLesson(newLesson); + + Organisation organisation = (Organisation) baseDAO.find(Organisation.class, TEST_ORG_ID); + + Set staff = new HashSet(); + staff.add(mmm); + + LessonClass newLessonClass = new LessonClass(null, //grouping id + new HashSet(),//groups + null, // activities + null, //staff group + newLesson); + + lessonClassDAO.saveLessonClass(newLessonClass); + + //setup staff group + newLessonClass.setStaffGroup(Group.createStaffGroup(newLessonClass,"staffGroupName", + new HashSet(staff))); + //setup learner group + newLessonClass.getGroups() + .add(Group.createLearnerGroup(newLessonClass, "learnerGroupName", + new HashSet())); + + newLesson.setLessonClass(newLessonClass); + newLesson.setOrganisation(organisation); + + lessonClassDAO.updateLessonClass(newLessonClass); + + lessonDAO.updateLesson(newLesson); + + // add some users. + checkCount(newLessonClass, "initial setup", 0,1); + + lessonService.addLearner(newLesson.getLessonId(), mmm); + + lessonService.addLearner(newLesson.getLessonId(), test1); + checkCount(newLessonClass, "added test1 to learner:", 1,1); + lessonService.addStaffMember(newLesson.getLessonId(), test1); + checkCount(newLessonClass, "added test1 to staff:", 2,1); + + lessonService.addLearner(newLesson.getLessonId(), test1); + checkCount(newLessonClass, "added test1 to learner again:", 1,1); // no change as already in group + lessonService.addStaffMember(newLesson.getLessonId(), test1); + checkCount(newLessonClass, "added test1 to staff:", 2,1); // no change as already in group + + } + + private void checkCount(LessonClass lessonClass, String test, int numberLearners, int numberStaff) { + assertEquals(test+": Expected 1 lesson group", lessonClass.getGroups().size(), 1); + assertEquals(test+": Expected number of learners", lessonClass.getLearners().size(), numberLearners); + assertEquals(test+": Expected number of staff", lessonClass.getStaffGroup().getUsers().size(), numberStaff); + } + +}