001: /**********************************************************************************
002: *
003: * $Id: CourseGradesToSpreadsheetConverterSample.java 22063 2007-03-01 23:07:02Z ray@media.berkeley.edu $
004: *
005: ***********************************************************************************
006: *
007: * Copyright (c) 2007 The Regents of the University of California
008: *
009: * Licensed under the Educational Community License, Version 1.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.opensource.org/licenses/ecl1.php
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: *
021: **********************************************************************************/package org.sakaiproject.gradebooksample;
022:
023: import java.util.ArrayList;
024: import java.util.List;
025: import java.util.Map;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.sakaiproject.coursemanagement.api.CourseManagementService;
030: import org.sakaiproject.coursemanagement.api.Enrollment;
031: import org.sakaiproject.coursemanagement.api.EnrollmentSet;
032: import org.sakaiproject.coursemanagement.api.Section;
033: import org.sakaiproject.section.api.coursemanagement.CourseSection;
034: import org.sakaiproject.section.api.coursemanagement.EnrollmentRecord;
035: import org.sakaiproject.section.api.coursemanagement.LearningContext;
036: import org.sakaiproject.section.api.coursemanagement.User;
037: import org.sakaiproject.tool.gradebook.CourseGrade;
038: import org.sakaiproject.tool.gradebook.CourseGradeRecord;
039: import org.sakaiproject.tool.gradebook.CourseGradesToSpreadsheetConverter;
040:
041: /**
042: * Sample customization of course grade spreadsheet downloads. This changes
043: * the default behavior in the following ways:
044: *
045: * <ul>
046: * <li> The student name column is dropped.
047: * <li> Another column is added to hold "Comments".
048: * <li> The Course Management service is used to look up each student's
049: * grading scheme.
050: * <li> If the grading scheme is "Pass/Fail" or "Satisfactory/Unsatisfactory",
051: * then the Gradebook's letter grade is converted to "P", "NP", "S", or "U"
052: * based on a string translation table. The converted value is put into the
053: * "Grade" column and the original letter grade is put into the "Comments" column.
054: * </ul>
055: *
056: * Once this has been deployed as a Sakai component, it can be plugged into
057: * the Gradebook application by adding the following line to "sakai.properties":
058: * <p>
059: * gradebook.coursegrades.converter=org.sakaiproject.gradebooksample.CourseGradesToSpreadsheetConverterSample
060: */
061: public class CourseGradesToSpreadsheetConverterSample implements
062: CourseGradesToSpreadsheetConverter {
063: private static final Log log = LogFactory
064: .getLog(CourseGradesToSpreadsheetConverterSample.class);
065:
066: private CourseManagementService courseManagementService;
067: private String letterGradingSchemeName;
068: private String passGradingSchemeName;
069: private String satisfactoryGradingSchemeName;
070: private Map<String, String> letterToPassMap;
071: private Map<String, String> letterToSatisfactoryMap;
072:
073: /**
074: * @see org.sakaiproject.tool.gradebook.CourseGradesToSpreadsheetConverter#getSpreadsheetData(java.util.List,
075: * org.sakaiproject.tool.gradebook.CourseGrade, java.util.Map)
076: */
077: public List<List<Object>> getSpreadsheetData(
078: List<EnrollmentRecord> enrollments,
079: CourseGrade courseGrade,
080: Map<String, CourseGradeRecord> gradesMap) {
081: List<List<Object>> spreadsheetData = new ArrayList<List<Object>>();
082:
083: // Build column headers.
084: List<Object> headerRow = new ArrayList<Object>();
085: headerRow.add("STUDENT ID NUMBER");
086: headerRow.add("GRADE");
087: headerRow.add("COMMENTS");
088: spreadsheetData.add(headerRow);
089:
090: // Build student grade rows.
091: for (EnrollmentRecord enrollment : enrollments) {
092: User student = enrollment.getUser();
093: String studentUid = student.getUserUid();
094: Map studentMap = (Map) gradesMap.get(studentUid);
095: List<Object> row = new ArrayList<Object>();
096: row.add(student.getDisplayId());
097: String grade = null;
098: if (studentMap != null) {
099: CourseGradeRecord gradeRecord = (CourseGradeRecord) studentMap
100: .get(courseGrade.getId());
101: if (gradeRecord != null) {
102: grade = gradeRecord.getDisplayGrade();
103: if (log.isDebugEnabled())
104: log.debug("student UID=" + studentUid
105: + ", displayID="
106: + student.getDisplayId()
107: + ", displayGrade=" + grade);
108: addGradeColumns(row, enrollment, grade);
109: }
110: }
111: spreadsheetData.add(row);
112: }
113:
114: return spreadsheetData;
115: }
116:
117: private void addGradeColumns(List<Object> row,
118: EnrollmentRecord sectionAwarenessEnrollment,
119: String originalGrade) {
120: String gradeColumn = originalGrade;
121: String commentColumn = null;
122: LearningContext lc = sectionAwarenessEnrollment
123: .getLearningContext();
124: if (log.isDebugEnabled())
125: log.debug("lc=" + lc);
126: if (lc instanceof CourseSection) {
127: CourseSection courseSection = (CourseSection) lc;
128: String courseManagementEid = courseSection.getEid();
129: if (log.isDebugEnabled())
130: log.debug("courseManagementEid=" + courseManagementEid);
131: if (courseManagementEid != null) {
132: // Translate from Section Awareness view to Course Management data.
133: Section cmSection = courseManagementService
134: .getSection(courseManagementEid);
135: if (cmSection != null) {
136: EnrollmentSet enrollmentSet = cmSection
137: .getEnrollmentSet();
138: if (log.isDebugEnabled())
139: log.debug("enrollmentSet=" + enrollmentSet);
140: if (enrollmentSet != null) {
141: Enrollment cmEnrollment = courseManagementService
142: .findEnrollment(
143: sectionAwarenessEnrollment
144: .getUser()
145: .getDisplayId(),
146: enrollmentSet.getEid());
147: if (cmEnrollment != null) {
148: String gradingScheme = cmEnrollment
149: .getGradingScheme();
150: if (log.isDebugEnabled())
151: log.debug("gradingScheme="
152: + gradingScheme);
153: if ((gradingScheme != null)
154: && (!gradingScheme
155: .equals(letterGradingSchemeName))) {
156: if (gradingScheme
157: .equals(passGradingSchemeName)) {
158: gradeColumn = letterToPassMap
159: .get(originalGrade);
160: commentColumn = originalGrade;
161: } else if (gradingScheme
162: .equals(satisfactoryGradingSchemeName)) {
163: gradeColumn = letterToSatisfactoryMap
164: .get(originalGrade);
165: commentColumn = originalGrade;
166: }
167: }
168: }
169: }
170: }
171: }
172: }
173: if (log.isDebugEnabled())
174: log.debug("gradeColumn=" + gradeColumn + ", commentColumn="
175: + commentColumn);
176: row.add(gradeColumn);
177: row.add(commentColumn);
178: }
179:
180: public CourseManagementService getCourseManagementService() {
181: return courseManagementService;
182: }
183:
184: public void setCourseManagementService(
185: CourseManagementService courseManagementService) {
186: this .courseManagementService = courseManagementService;
187: }
188:
189: public String getLetterGradingSchemeName() {
190: return letterGradingSchemeName;
191: }
192:
193: public void setLetterGradingSchemeName(
194: String letterGradingSchemeName) {
195: this .letterGradingSchemeName = letterGradingSchemeName;
196: }
197:
198: public Map<String, String> getLetterToPassMap() {
199: return letterToPassMap;
200: }
201:
202: public void setLetterToPassMap(Map<String, String> letterToPassMap) {
203: this .letterToPassMap = letterToPassMap;
204: }
205:
206: public Map<String, String> getLetterToSatisfactoryMap() {
207: return letterToSatisfactoryMap;
208: }
209:
210: public void setLetterToSatisfactoryMap(
211: Map<String, String> letterToSatisfactoryMap) {
212: this .letterToSatisfactoryMap = letterToSatisfactoryMap;
213: }
214:
215: public String getPassGradingSchemeName() {
216: return passGradingSchemeName;
217: }
218:
219: public void setPassGradingSchemeName(String passGradingSchemeName) {
220: this .passGradingSchemeName = passGradingSchemeName;
221: }
222:
223: public String getSatisfactoryGradingSchemeName() {
224: return satisfactoryGradingSchemeName;
225: }
226:
227: public void setSatisfactoryGradingSchemeName(
228: String satisfactoryGradingSchemeName) {
229: this.satisfactoryGradingSchemeName = satisfactoryGradingSchemeName;
230: }
231:
232: }
|