001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/trunk/component/src/java/org/sakaiproject/tool/assessment/integration/helper/standalone/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.standalone;
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.standalone.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: List availableSections = new ArrayList();
100:
101: SectionAwareness sectionAwareness = getSectionAwareness();
102: if (sectionAwareness == null) {
103: } else {
104:
105: // Get the list of sections. For now, just use whatever default
106: // sorting we get from the Section Awareness component.
107: /*
108: List sectionCategories = sectionAwareness.getSectionCategories(siteid);
109: for (Iterator catIter = sectionCategories.iterator(); catIter.hasNext(); ) {
110: String category = (String)catIter.next();
111: List sections = sectionAwareness.getSectionsInCategory(siteid, category);
112: for (Iterator iter = sections.iterator(); iter.hasNext(); ) {
113: CourseSection section = (CourseSection)iter.next();
114: if (isUserAbleToGradeAll(siteid, userUid) || isUserAbleToGradeSection(section.getUuid(), userUid)) {
115: availableSections.add(section);
116: }
117: }
118: }
119: }
120: */
121: List sections = sectionAwareness.getSections(siteid);
122: for (Iterator iter = sections.iterator(); iter.hasNext();) {
123: CourseSection section = (CourseSection) iter.next();
124: // if (isUserAbleToGradeAll(gradebookUid) || isUserAbleToGradeSection(section.getUuid())) {
125: if (isUserAbleToGradeAll(siteid, userUid)
126: || isUserAbleToGradeSection(section.getUuid(),
127: userUid)) {
128: availableSections.add(section);
129: }
130: }
131: }
132:
133: return availableSections;
134: }
135:
136: private List getSectionEnrollmentsTrusted(String sectionUid,
137: String userUid) {
138: return getSectionAwareness().getSectionMembersInRole(
139: sectionUid, Role.STUDENT);
140: }
141:
142: public List getSectionEnrollments(String siteid, String sectionUid,
143: String userUid) {
144: List enrollments;
145: if (isUserAbleToGradeAll(siteid, userUid)
146: || isUserAbleToGradeSection(sectionUid, userUid)) {
147: enrollments = getSectionEnrollmentsTrusted(sectionUid,
148: userUid);
149: } else {
150: enrollments = new ArrayList();
151: log.warn("getSectionEnrollments for sectionUid="
152: + sectionUid + " called by unauthorized userUid="
153: + userUid);
154: }
155: return enrollments;
156: }
157:
158: public List findMatchingEnrollments(String siteid,
159: String searchString, String optionalSectionUid,
160: String userUid) {
161: List enrollments;
162: List allEnrollmentsFilteredBySearch = getSectionAwareness()
163: .findSiteMembersInRole(siteid, Role.STUDENT,
164: searchString);
165:
166: if (allEnrollmentsFilteredBySearch.isEmpty()
167: || ((optionalSectionUid == null) && isUserAbleToGradeAll(
168: siteid, userUid))) {
169: enrollments = allEnrollmentsFilteredBySearch;
170: } else {
171: if (optionalSectionUid == null) {
172: enrollments = getAvailableEnrollments(siteid, userUid);
173: } else {
174: // The user has selected a particular section.
175: enrollments = getSectionEnrollments(siteid,
176: optionalSectionUid, userUid);
177: }
178: Set availableStudentUids = FacadeUtils
179: .getStudentUids(enrollments);
180: enrollments = new ArrayList();
181: for (Iterator iter = allEnrollmentsFilteredBySearch
182: .iterator(); iter.hasNext();) {
183: EnrollmentRecord enr = (EnrollmentRecord) iter.next();
184: if (availableStudentUids.contains(enr.getUser()
185: .getUserUid())) {
186: enrollments.add(enr);
187: }
188: }
189: }
190:
191: return enrollments;
192: }
193:
194: public boolean isSectionMemberInRoleStudent(String sectionId,
195: String studentId) {
196: return getSectionAwareness().isSectionMemberInRole(sectionId,
197: studentId, Role.STUDENT);
198: }
199:
200: }
|