Index: lams_common/src/java/org/lamsfoundation/lams/learningdesign/Activity.java =================================================================== diff -u -rbe76a46bdfdb956f074ba2e507852bc9623cf1e1 -r097386be99ea405bd2786be1fca936840b75d3ac --- lams_common/src/java/org/lamsfoundation/lams/learningdesign/Activity.java (.../Activity.java) (revision be76a46bdfdb956f074ba2e507852bc9623cf1e1) +++ lams_common/src/java/org/lamsfoundation/lams/learningdesign/Activity.java (.../Activity.java) (revision 097386be99ea405bd2786be1fca936840b75d3ac) @@ -641,20 +641,36 @@ /** * Return the group information for the requested user when he is running - * current activity instance. + * current activity instance, based on the grouping data in the activity. * @param learner the requested user * @return the group that this user belongs to. */ public Group getGroupFor(User learner) { - if(this.getGrouping()==null) + return getGroupFor(learner, this.getGrouping()); + } + + /** + * Return the group information for the requested user when he is running + * current activity instance, based on the given grouping. + *

+ * If we are using the grouping set up in the activity, the grouping will be + * this.getGrouping(). If the activity isn't grouped, then it should use + * the class grouping. + * + * @param learner the requested user + * @return the group that this user belongs to. + */ + protected Group getGroupFor(User learner, Grouping inGrouping) + { + if(inGrouping==null) throw new IllegalArgumentException("Exception occured in " + "getGroupFor, no grouping has been defined"); - for(Iterator i=this.getGrouping().getGroups().iterator();i.hasNext();) + for(Iterator i=inGrouping.getGroups().iterator();i.hasNext();) { Group group = (Group)i.next(); - if(this.getGrouping().isLearnerGroup(group)&&group.hasLearner(learner)) + if(inGrouping.isLearnerGroup(group)&&group.hasLearner(learner)) return group; } Index: lams_common/src/java/org/lamsfoundation/lams/learningdesign/ToolActivity.java =================================================================== diff -u -r79c858a92c17ab3ca404cab4a0cf6094254e68d5 -r097386be99ea405bd2786be1fca936840b75d3ac --- lams_common/src/java/org/lamsfoundation/lams/learningdesign/ToolActivity.java (.../ToolActivity.java) (revision 79c858a92c17ab3ca404cab4a0cf6094254e68d5) +++ lams_common/src/java/org/lamsfoundation/lams/learningdesign/ToolActivity.java (.../ToolActivity.java) (revision 097386be99ea405bd2786be1fca936840b75d3ac) @@ -28,8 +28,10 @@ import java.util.SortedSet; import org.apache.commons.lang.builder.ToStringBuilder; +import org.apache.log4j.Logger; import org.lamsfoundation.lams.learningdesign.strategy.ToolActivityStrategy; import org.lamsfoundation.lams.lesson.Lesson; +import org.lamsfoundation.lams.lesson.LessonClass; import org.lamsfoundation.lams.tool.GroupedToolSession; import org.lamsfoundation.lams.tool.NonGroupedToolSession; import org.lamsfoundation.lams.tool.Tool; @@ -43,7 +45,9 @@ public class ToolActivity extends SimpleActivity implements Serializable { - /** Holds value of property toolContentId. */ + private static Logger log = Logger.getLogger(ToolActivity.class); + + /** Holds value of property toolContentId. */ private Long toolContentId; /** Holds value of property tool.*/ @@ -168,25 +172,81 @@ } /** - * Factory method to create a new tool session for the user when he is - * running current activity. + * Factory method to create a new tool session for a single user when he is + * running current activity. Does not check to see if a tool session already exists. + *

+ * If the activity has groupingSupportType = GROUPING_SUPPORT_NONE then + * a new tool session is created for each learner. + *

+ * If the activity has groupingSupportType = GROUPING_SUPPORT_REQUIRED + * then a new tool session is created for each group of learners. It will fall back + * to a class group if no grouping is found - the user interface should not + * have allowed this! + *

+ * If the activity has groupingSupportType = GROUPING_SUPPORT_OPTIONAL + * then a new tool session is created for each group of learners. If no grouping + * is available then a whole of class group is created. + *

+ * If groupingSupportType is not set then defaults to GROUPING_SUPPORT_NONE. + * If for some reason a grouped session if also does the equivalent of GROUPING_SUPPORT_NONE. + * This way the system will still function, if not as expected! + *

* @param learner the user who should be using this tool session. * @return the new tool session. */ public ToolSession createToolSessionForActivity(User learner,Lesson lesson) { - if(this.getApplyGrouping().booleanValue()) - return new GroupedToolSession(this, - new Date(System.currentTimeMillis()), - ToolSession.STARTED_STATE, - this.getGroupFor(learner), - lesson); - else - return new NonGroupedToolSession(this, - new Date(System.currentTimeMillis()), - ToolSession.STARTED_STATE, - learner, - lesson); + Date now = new Date(System.currentTimeMillis()); + Integer supportType = getGroupingSupportType(); + ToolSession session = null; + + if ( supportType != null && + ( supportType.intValue() == GROUPING_SUPPORT_REQUIRED || supportType.intValue() == GROUPING_SUPPORT_OPTIONAL ) ) { + + // Both cases create a small group if a grouping exists, otherwise creates a class group. + Group learners = null; + if( getApplyGrouping().booleanValue()) { + learners = this.getGroupFor(learner); + } + + if ( supportType.intValue() == GROUPING_SUPPORT_REQUIRED && learners == null ) { + log.error("Activity "+getActivityId() + +" requires grouping (groupingSupportType=GROUPING_SUPPORT_REQUIRED) but no grouping was available. " + +" applyGrouping = "+getApplyGrouping() + +" grouping = "+getGrouping() + +". Falling back to a class grouping."); + } + + if ( learners == null ) { + LessonClass lessonClass = lesson.getLessonClass(); + learners = this.getGroupFor(learner, lessonClass); + } + + if ( learners != null ) { + session = new GroupedToolSession(this, + now, + ToolSession.STARTED_STATE, + learners, + lesson); + } else { + log.error("Unable to get the group for a new tool session for Activity "+getActivityId() + +" Falling back to one learner per session." + +" Learner "+learner + +", lesson is "+lesson); + } + } + + if ( session == null ) { + // Either GROUPING_SUPPORT_NONE was selected, supportType == null or the grouped tool sessions could not be created. + // So create one session per user. + session = new NonGroupedToolSession(this, + now, + ToolSession.STARTED_STATE, + learner, + lesson); + } + + return session; } public String toString()