001: /**********************************************************************************
002: *
003: * $Id: CourseGradeRecord.java 21205 2007-02-09 20:00:15Z ray@media.berkeley.edu $
004: *
005: ***********************************************************************************
006: *
007: * Copyright (c) 2005 The Regents of the University of California, The MIT Corporation
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.tool.gradebook;
022:
023: import java.util.Comparator;
024: import java.util.List;
025:
026: import org.apache.commons.lang.StringUtils;
027:
028: /**
029: * A CourseGradeRecord is a grade record that can be associated with a CourseGrade.
030: *
031: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
032: */
033: public class CourseGradeRecord extends AbstractGradeRecord {
034: private String enteredGrade;
035: private Double autoCalculatedGrade; // Not persisted
036: private Double calculatedPointsEarned; // Not persisted
037:
038: public static Comparator<CourseGradeRecord> calcComparator;
039:
040: static {
041: calcComparator = new Comparator<CourseGradeRecord>() {
042: public int compare(CourseGradeRecord cgr1,
043: CourseGradeRecord cgr2) {
044: if (cgr1 == null && cgr2 == null) {
045: return 0;
046: }
047: if (cgr1 == null) {
048: return -1;
049: }
050: if (cgr2 == null) {
051: return 1;
052: }
053: return cgr1.getPointsEarned().compareTo(
054: cgr2.getPointsEarned());
055: }
056: };
057: }
058:
059: public static Comparator<CourseGradeRecord> getOverrideComparator(
060: final GradeMapping mapping) {
061: return new Comparator<CourseGradeRecord>() {
062: public int compare(CourseGradeRecord cgr1,
063: CourseGradeRecord cgr2) {
064:
065: if (cgr1 == null && cgr2 == null) {
066: return 0;
067: }
068: if (cgr1 == null) {
069: return -1;
070: }
071: if (cgr2 == null) {
072: return 1;
073: }
074:
075: String enteredGrade1 = StringUtils.trimToEmpty(cgr1
076: .getEnteredGrade());
077: String enteredGrade2 = StringUtils.trimToEmpty(cgr2
078: .getEnteredGrade());
079:
080: // Grading scales are always defined in descending order.
081: List<String> grades = mapping.getGradingScale()
082: .getGrades();
083: int gradePos1 = -1;
084: int gradePos2 = -1;
085: for (int i = 0; (i < grades.size())
086: && ((gradePos1 == -1) || (gradePos2 == -1)); i++) {
087: String grade = grades.get(i);
088: if (grade.equals(enteredGrade1))
089: gradePos1 = i;
090: if (grade.equals(enteredGrade2))
091: gradePos2 = i;
092: }
093: return gradePos2 - gradePos1;
094: }
095: };
096:
097: }
098:
099: /**
100: * The graderId and dateRecorded properties will be set explicitly by the
101: * grade manager before the database is updated.
102: * @param courseGrade
103: * @param studentId
104: */
105: public CourseGradeRecord(CourseGrade courseGrade, String studentId) {
106: this .gradableObject = courseGrade;
107: this .studentId = studentId;
108: }
109:
110: /**
111: * Default no-arg constructor
112: */
113: public CourseGradeRecord() {
114: super ();
115: }
116:
117: /**
118: * This method will fail unless this course grade was fetched "with statistics",
119: * since it relies on having the total number of points possible available to
120: * calculate the percentage.
121: *
122: * @see org.sakaiproject.tool.gradebook.AbstractGradeRecord#getGradeAsPercentage()
123: */
124: public Double getGradeAsPercentage() {
125: if (enteredGrade == null) {
126: return autoCalculatedGrade;
127: } else {
128: return getCourseGrade().getGradebook()
129: .getSelectedGradeMapping().getValue(enteredGrade);
130: }
131: }
132:
133: /**
134: * Convenience method to get the correctly cast CourseGrade that this
135: * CourseGradeRecord references.
136: *
137: * @return CourseGrade referenced by this GradableObject
138: */
139: public CourseGrade getCourseGrade() {
140: return (CourseGrade) super .getGradableObject();
141: }
142:
143: /**
144: * @return Returns the enteredGrade.
145: */
146: public String getEnteredGrade() {
147: return enteredGrade;
148: }
149:
150: /**
151: * @param enteredGrade The enteredGrade to set.
152: */
153: public void setEnteredGrade(String enteredGrade) {
154: this .enteredGrade = enteredGrade;
155: }
156:
157: /**
158: * @return Returns the autoCalculatedGrade.
159: */
160: public Double getAutoCalculatedGrade() {
161: return autoCalculatedGrade;
162: }
163:
164: public Double getPointsEarned() {
165: return calculatedPointsEarned;
166: }
167:
168: /**
169: * @return Returns the displayGrade.
170: */
171: public String getDisplayGrade() {
172: if (enteredGrade != null) {
173: return enteredGrade;
174: } else {
175: return getCourseGrade().getGradebook()
176: .getSelectedGradeMapping().getGrade(
177: autoCalculatedGrade);
178: }
179: }
180:
181: /**
182: * @see org.sakaiproject.tool.gradebook.AbstractGradeRecord#isCourseGradeRecord()
183: */
184: public boolean isCourseGradeRecord() {
185: return true;
186: }
187:
188: /**
189: * For use by the Course Grade UI.
190: */
191: public Double getNonNullAutoCalculatedGrade() {
192: Double percent = getAutoCalculatedGrade();
193: if (percent == null) {
194: percent = new Double(0);
195: }
196: return percent;
197: }
198:
199: public void initNonpersistentFields(double totalPointsPossible,
200: double totalPointsEarned) {
201: Double percentageEarned;
202: calculatedPointsEarned = totalPointsEarned;
203: if (totalPointsPossible == 0.0) {
204: percentageEarned = null;
205: } else {
206: percentageEarned = new Double(totalPointsEarned
207: / totalPointsPossible * 100);
208: }
209: autoCalculatedGrade = percentageEarned;
210: }
211: }
|