001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/trunk/component/src/java/org/sakaiproject/tool/assessment/integration/helper/integrated/SectionAwareServiceHelperImpl.java $
003: * $Id: SectionAwareServiceHelperImpl.java 9273 2006-05-10 22:34:28Z daisyf@stanford.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
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.assessment.integration.helper.integrated;
021:
022: import java.util.*;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026:
027: import org.sakaiproject.section.api.SectionAwareness;
028: import org.sakaiproject.section.api.coursemanagement.CourseSection;
029: import org.sakaiproject.section.api.coursemanagement.EnrollmentRecord;
030: import org.sakaiproject.section.api.facade.Role;
031: import org.sakaiproject.tool.assessment.integration.helper.ifc.SectionAwareServiceHelper;
032: import org.sakaiproject.tool.assessment.integration.helper.integrated.FacadeUtils;
033:
034: /**
035: * An implementation of Samigo-specific authorization (based on Gradebook's) needs based
036: * on the shared Section Awareness API.
037: */
038: public class SectionAwareServiceHelperImpl extends AbstractSectionsImpl
039: implements SectionAwareServiceHelper {
040: private static final Log log = LogFactory
041: .getLog(SectionAwareServiceHelperImpl.class);
042:
043: public boolean isUserAbleToGrade(String siteid, String userUid) {
044: return (getSectionAwareness().isSiteMemberInRole(siteid,
045: userUid, Role.INSTRUCTOR) || getSectionAwareness()
046: .isSiteMemberInRole(siteid, userUid, Role.TA));
047: }
048:
049: public boolean isUserAbleToGradeAll(String siteid, String userUid) {
050: return getSectionAwareness().isSiteMemberInRole(siteid,
051: userUid, Role.INSTRUCTOR);
052: }
053:
054: public boolean isUserAbleToGradeSection(String sectionUid,
055: String userUid) {
056: return getSectionAwareness().isSectionMemberInRole(sectionUid,
057: userUid, Role.TA);
058: }
059:
060: public boolean isUserAbleToEdit(String siteid, String userUid) {
061: return getSectionAwareness().isSiteMemberInRole(siteid,
062: userUid, Role.INSTRUCTOR);
063: }
064:
065: public boolean isUserGradable(String siteid, String userUid) {
066: return getSectionAwareness().isSiteMemberInRole(siteid,
067: userUid, Role.STUDENT);
068: }
069:
070: /**
071: */
072: public List getAvailableEnrollments(String siteid, String userUid) {
073: List enrollments;
074: if (isUserAbleToGradeAll(siteid, userUid)) {
075: enrollments = getSectionAwareness().getSiteMembersInRole(
076: siteid, Role.STUDENT);
077: } else {
078: // We use a map because we may have duplicate students among the section
079: // participation records.
080: Map enrollmentMap = new HashMap();
081: List sections = getAvailableSections(siteid, userUid);
082: for (Iterator iter = sections.iterator(); iter.hasNext();) {
083: CourseSection section = (CourseSection) iter.next();
084: List sectionEnrollments = getSectionEnrollmentsTrusted(
085: section.getUuid(), userUid);
086: for (Iterator eIter = sectionEnrollments.iterator(); eIter
087: .hasNext();) {
088: EnrollmentRecord enr = (EnrollmentRecord) eIter
089: .next();
090: enrollmentMap.put(enr.getUser().getUserUid(), enr);
091: }
092: }
093: enrollments = new ArrayList(enrollmentMap.values());
094: }
095: return enrollments;
096: }
097:
098: public List getAvailableSections(String siteid, String userUid) {
099:
100: List availableSections = new ArrayList();
101:
102: SectionAwareness sectionAwareness = getSectionAwareness();
103: if (sectionAwareness == null) {
104: } else {
105:
106: // Get the list of sections. For now, just use whatever default
107: // sorting we get from the Section Awareness component.
108: /*
109: List sectionCategories = sectionAwareness.getSectionCategories(siteid);
110: for (Iterator catIter = sectionCategories.iterator(); catIter.hasNext(); ) {
111: String category = (String)catIter.next();
112: List sections = sectionAwareness.getSectionsInCategory(siteid, category);
113: for (Iterator iter = sections.iterator(); iter.hasNext(); ) {
114: CourseSection section = (CourseSection)iter.next();
115: if (isUserAbleToGradeAll(siteid, userUid) || isUserAbleToGradeSection(section.getUuid(), userUid)) {
116: availableSections.add(section);
117: }
118: }
119: }
120: */
121:
122: List sections = sectionAwareness.getSections(siteid);
123: for (Iterator iter = sections.iterator(); iter.hasNext();) {
124: CourseSection section = (CourseSection) iter.next();
125: // if (isUserAbleToGradeAll(gradebookUid) || isUserAbleToGradeSection(section.getUuid())) {
126: if (isUserAbleToGradeAll(siteid, userUid)
127: || isUserAbleToGradeSection(section.getUuid(),
128: userUid)) {
129: availableSections.add(section);
130: }
131: }
132: }
133:
134: return availableSections;
135: }
136:
137: private List getSectionEnrollmentsTrusted(String sectionUid,
138: String userUid) {
139: return getSectionAwareness().getSectionMembersInRole(
140: sectionUid, Role.STUDENT);
141: }
142:
143: public List getSectionEnrollments(String siteid, String sectionUid,
144: String userUid) {
145: List enrollments;
146: if (isUserAbleToGradeAll(siteid, userUid)
147: || isUserAbleToGradeSection(sectionUid, userUid)) {
148: enrollments = getSectionEnrollmentsTrusted(sectionUid,
149: userUid);
150: } else {
151: enrollments = new ArrayList();
152: log.warn("getSectionEnrollments for sectionUid="
153: + sectionUid + " called by unauthorized userUid="
154: + userUid);
155: }
156: return enrollments;
157: }
158:
159: public List findMatchingEnrollments(String siteid,
160: String searchString, String optionalSectionUid,
161: String userUid) {
162: List enrollments;
163: List allEnrollmentsFilteredBySearch = getSectionAwareness()
164: .findSiteMembersInRole(siteid, Role.STUDENT,
165: searchString);
166:
167: if (allEnrollmentsFilteredBySearch.isEmpty()
168: || ((optionalSectionUid == null) && isUserAbleToGradeAll(
169: siteid, userUid))) {
170: enrollments = allEnrollmentsFilteredBySearch;
171: } else {
172: if (optionalSectionUid == null) {
173: enrollments = getAvailableEnrollments(siteid, userUid);
174: } else {
175: // The user has selected a particular section.
176: enrollments = getSectionEnrollments(siteid,
177: optionalSectionUid, userUid);
178: }
179: Set availableStudentUids = FacadeUtils
180: .getStudentUids(enrollments);
181: enrollments = new ArrayList();
182: for (Iterator iter = allEnrollmentsFilteredBySearch
183: .iterator(); iter.hasNext();) {
184: EnrollmentRecord enr = (EnrollmentRecord) iter.next();
185: if (availableStudentUids.contains(enr.getUser()
186: .getUserUid())) {
187: enrollments.add(enr);
188: }
189: }
190: }
191:
192: return enrollments;
193: }
194:
195: public boolean isSectionMemberInRoleStudent(String sectionId,
196: String studentId) {
197: return getSectionAwareness().isSectionMemberInRole(sectionId,
198: studentId, Role.STUDENT);
199: }
200:
201: }
|