001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sections/tags/sakai_2-4-1/sections-app/src/java/org/sakaiproject/tool/section/jsf/backingbean/EditStudentsBean.java $
003: * $Id: EditStudentsBean.java 22767 2007-03-16 17:41:08Z jholtzman@berkeley.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Regents of the University of California and The Regents of the University of Michigan
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.tool.section.jsf.backingbean;
021:
022: import java.io.Serializable;
023: import java.util.ArrayList;
024: import java.util.Collections;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Set;
028:
029: import javax.faces.event.ValueChangeEvent;
030: import javax.faces.model.SelectItem;
031:
032: import org.apache.commons.lang.StringUtils;
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.sakaiproject.section.api.coursemanagement.CourseSection;
036: import org.sakaiproject.section.api.coursemanagement.ParticipationRecord;
037: import org.sakaiproject.section.api.coursemanagement.User;
038: import org.sakaiproject.section.api.exception.RoleConfigurationException;
039: import org.sakaiproject.section.api.facade.Role;
040: import org.sakaiproject.tool.section.decorator.SectionDecorator;
041: import org.sakaiproject.tool.section.jsf.JsfUtil;
042:
043: /**
044: * Controls the edit students page (where students are assigned to sections).
045: *
046: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
047: *
048: */
049: public class EditStudentsBean extends EditManagersBean implements
050: Serializable {
051:
052: private static final long serialVersionUID = 1L;
053:
054: private static final Log log = LogFactory
055: .getLog(EditStudentsBean.class);
056:
057: // For the "View" selectbox
058: private String availableSectionUuid;
059: private String availableSectionTitle;
060: private Integer availableSectionMax;
061: private List availableSectionItems;
062:
063: private Integer sectionMax;
064:
065: public void init() {
066: SectionDecorator currentSection = initializeFields();
067: sectionMax = currentSection.getMaxEnrollments();
068:
069: // Get the current users
070: List enrollments = getSectionManager().getSectionEnrollments(
071: currentSection.getUuid());
072: Collections.sort(enrollments,
073: EditManagersBean.sortNameComparator);
074:
075: populateSelectedUsers(enrollments);
076:
077: // Build the list of items for the left-side box
078: List available;
079: if (StringUtils.trimToNull(availableSectionUuid) == null) {
080: available = getSectionManager().getUnsectionedEnrollments(
081: currentSection.getCourse().getUuid(),
082: currentSection.getCategory());
083: } else {
084: available = getSectionManager().getSectionEnrollments(
085: availableSectionUuid);
086: }
087: Collections
088: .sort(available, EditManagersBean.sortNameComparator);
089:
090: availableUsers = new ArrayList();
091: for (Iterator iter = available.iterator(); iter.hasNext();) {
092: User student = ((ParticipationRecord) iter.next())
093: .getUser();
094: availableUsers.add(new SelectItem(student.getUserUid(),
095: student.getSortName()));
096: }
097:
098: // Build the list of available sections
099: List sectionsInCategory = getSectionManager()
100: .getSectionsInCategory(getSiteContext(),
101: currentSection.getCategory());
102: Collections.sort(sectionsInCategory);
103: availableSectionItems = new ArrayList();
104: availableSectionItems.add(new SelectItem("", JsfUtil
105: .getLocalizedMessage("edit_student_unassigned")));
106: for (Iterator iter = sectionsInCategory.iterator(); iter
107: .hasNext();) {
108: CourseSection section = (CourseSection) iter.next();
109: // Don't include the current section
110: if (section.getUuid().equals(currentSection.getUuid())) {
111: continue;
112: }
113: if (section.getUuid().equals(availableSectionUuid)) {
114: availableSectionTitle = section.getTitle();
115: availableSectionMax = section.getMaxEnrollments();
116: }
117: availableSectionItems.add(new SelectItem(section.getUuid(),
118: section.getTitle()));
119: }
120: }
121:
122: public void processChangeSection(ValueChangeEvent event) {
123: // Reset all lists
124: init();
125: }
126:
127: public String update() {
128: CourseSection section = getSectionManager().getSection(
129: sectionUuid);
130:
131: // The section might have been deleted
132: if (section == null) {
133: JsfUtil.addErrorMessage(JsfUtil
134: .getLocalizedMessage("error_section_deleted"));
135: return "overview";
136: }
137:
138: Set selectedUserUuids = getHighlightedUsers("memberForm:selectedUsers");
139: try {
140: getSectionManager().setSectionMemberships(
141: selectedUserUuids, Role.STUDENT, sectionUuid);
142: } catch (RoleConfigurationException rce) {
143: JsfUtil.addErrorMessage(JsfUtil
144: .getLocalizedMessage("role_config_error"));
145: return null;
146: }
147:
148: // If the "available" box is a section, update that section's members as well
149: Set availableUserUuids = getHighlightedUsers("memberForm:availableUsers");
150: if (StringUtils.trimToNull(availableSectionUuid) != null) {
151: availableUserUuids = getHighlightedUsers("memberForm:availableUsers");
152: try {
153: getSectionManager().setSectionMemberships(
154: availableUserUuids, Role.STUDENT,
155: availableSectionUuid);
156: } catch (RoleConfigurationException rce) {
157: JsfUtil.addErrorMessage(JsfUtil
158: .getLocalizedMessage("role_config_error"));
159: return null;
160: }
161: }
162: StringBuffer titles = new StringBuffer();
163: titles.append(sectionTitle);
164: if (StringUtils.trimToNull(availableSectionUuid) != null) {
165: titles.append(" ");
166: titles.append(JsfUtil.getLocalizedMessage("and"));
167: titles.append(" ");
168: titles.append(availableSectionTitle);
169: }
170:
171: // Add the success message first, before any caveats (see below)
172: JsfUtil.addRedirectSafeInfoMessage(JsfUtil.getLocalizedMessage(
173: "edit_student_successful", new String[] { titles
174: .toString() }));
175:
176: // If the selected section is now overenrolled, let the user know
177: if (sectionMax != null
178: && selectedUserUuids.size() > sectionMax.intValue()) {
179: JsfUtil
180: .addRedirectSafeWarnMessage(JsfUtil
181: .getLocalizedMessage(
182: "edit_student_over_max_warning",
183: new String[] {
184: sectionTitle,
185: Integer
186: .toString(selectedUserUuids
187: .size()),
188: Integer
189: .toString(selectedUserUuids
190: .size()
191: - sectionMax
192: .intValue()) }));
193: }
194:
195: // If the available section is now overenrolled, let the user know
196: if (availableSectionMax != null
197: && availableUserUuids.size() > availableSectionMax
198: .intValue()) {
199: JsfUtil.addRedirectSafeWarnMessage(JsfUtil
200: .getLocalizedMessage(
201: "edit_student_over_max_warning",
202: new String[] {
203: availableSectionTitle,
204: Integer.toString(availableUserUuids
205: .size()),
206: Integer.toString(availableUserUuids
207: .size()
208: - availableSectionMax
209: .intValue()) }));
210: }
211:
212: return "overview";
213: }
214:
215: public String getAvailableSectionUuid() {
216: return availableSectionUuid;
217: }
218:
219: public void setAvailableSectionUuid(String availableSectionUuid) {
220: this .availableSectionUuid = availableSectionUuid;
221: }
222:
223: public List getAvailableSectionItems() {
224: return availableSectionItems;
225: }
226:
227: public Integer getSectionMax() {
228: return sectionMax;
229: }
230: }
|