001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/trunk/component/src/java/org/sakaiproject/tool/assessment/integration/helper/integrated/GradebookServiceHelperImpl.java $
003: * $Id: GradebookServiceHelperImpl.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.List;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.sakaiproject.tool.assessment.services.assessment.PublishedAssessmentService;
027: import org.sakaiproject.tool.assessment.services.GradingService;
028: import org.sakaiproject.tool.assessment.facade.GradebookFacade;
029: import org.sakaiproject.tool.api.Tool;
030: import org.sakaiproject.tool.cover.ToolManager;
031: import org.sakaiproject.exception.IdUnusedException;
032: import org.sakaiproject.service.gradebook.shared.GradebookService;
033: import org.sakaiproject.site.api.Site;
034: import org.sakaiproject.site.api.SitePage;
035: import org.sakaiproject.site.api.ToolConfiguration;
036: import org.sakaiproject.site.cover.SiteService;
037: import org.sakaiproject.tool.assessment.data.dao.assessment.PublishedAssessmentData;
038: import org.sakaiproject.tool.assessment.data.dao.grading.AssessmentGradingData;
039: import org.sakaiproject.tool.assessment.data.ifc.assessment.PublishedAssessmentIfc;
040: import org.sakaiproject.tool.assessment.data.ifc.grading.AssessmentGradingIfc;
041: import org.sakaiproject.spring.SpringBeanLocator;
042:
043: import org.apache.commons.logging.Log;
044: import org.apache.commons.logging.LogFactory;
045:
046: import org.sakaiproject.tool.assessment.data.dao.assessment.PublishedAssessmentData;
047: import org.sakaiproject.tool.assessment.data.ifc.grading.AssessmentGradingIfc;
048: import org.sakaiproject.tool.assessment.integration.helper.ifc.GradebookServiceHelper;
049:
050: /**
051: *
052: * <p>Description:
053: * This is an integrated context implementation helper delegate class for
054: * the GradebookService class.
055: * "Integrated" means that Samigo (Tests and Quizzes)
056: * is running within the context of the Sakai portal and authentication
057: * mechanisms, and therefore makes calls on Sakai for things it needs.</p>
058: * <p>Note: To customize behavior you can add your own helper class to the
059: * Spring injection via the integrationContext.xml for your context.
060: * The particular integrationContext.xml to be used is selected by the
061: * build process.
062: * </p>
063: * <p>Sakai Project Copyright (c) 2005</p>
064: * <p> </p>
065: * @author Ed Smiley <esmiley@stanford.edu>
066: */
067: public class GradebookServiceHelperImpl implements
068: GradebookServiceHelper {
069: private static Log log = LogFactory
070: .getLog(GradebookServiceHelperImpl.class);
071:
072: /**
073: * Does a gradebook exist?
074: * @param gradebookUId the gradebook id
075: * @param g the Gradebook Service
076: * @return true if the given gradebook exists
077: */
078: public boolean gradebookExists(String gradebookUId,
079: GradebookService g) {
080: log.debug("GradebookService = " + g);
081: if (gradebookUId == null) {
082: return false;
083: }
084: return g.isGradebookDefined(gradebookUId);
085: }
086:
087: /**
088: * Does a gradebook exist?
089: * @param siteId the site id
090: * @return true if the given gradebook exists
091: */
092: public boolean isGradebookExist(String siteId) {
093: Site currentSite = getCurrentSite(siteId);
094: if (currentSite == null) {
095: return false;
096: }
097: SitePage page = null;
098: String toolId = null;
099: try {
100: // get page
101: List pageList = currentSite.getPages();
102: for (int i = 0; i < pageList.size(); i++) {
103: page = (SitePage) pageList.get(i);
104: List pageToolList = page.getTools();
105: toolId = ((ToolConfiguration) pageToolList.get(0))
106: .getTool().getId();
107: if (toolId.equalsIgnoreCase("sakai.gradebook.tool")) {
108: return true;
109: }
110: }
111: } catch (Exception e) {
112: log.warn(e.getMessage());
113: }
114: return false;
115: }
116:
117: private Site getCurrentSite(String id) {
118: Site site = null;
119: try {
120: site = SiteService.getSite(id);
121: } catch (IdUnusedException e) {
122: log.error(e.getMessage());
123: e.printStackTrace();
124: }
125: return site;
126: }
127:
128: /**
129: * Remove a published assessment from the gradebook.
130: * @param gradebookUId the gradebook id
131: * @param g the Gradebook Service
132: * @param publishedAssessmentId the id of the published assessment
133: * @throws java.lang.Exception
134: */
135: public void removeExternalAssessment(String gradebookUId,
136: String publishedAssessmentId, GradebookService g)
137: throws Exception {
138: if (g.isGradebookDefined(gradebookUId)) {
139: g.removeExternalAssessment(gradebookUId,
140: publishedAssessmentId);
141: }
142: }
143:
144: public boolean isAssignmentDefined(String assessmentTitle,
145: GradebookService g) throws Exception {
146: String gradebookUId = GradebookFacade.getGradebookUId();
147: return g.isAssignmentDefined(gradebookUId, assessmentTitle);
148: }
149:
150: /**
151: * Add a published assessment to gradebook.
152: * @param publishedAssessment the published assessment
153: * @param g the Gradebook Service
154: * @return false: cannot add to gradebook
155: * @throws java.lang.Exception
156: */
157: public boolean addToGradebook(
158: PublishedAssessmentData publishedAssessment,
159: GradebookService g) throws Exception {
160: //log.info("total point(s) is/are =" +
161: // publishedAssessment.getTotalScore().longValue());
162: //log.info("gradebookId =" + GradebookFacade.getGradebookUId());
163: boolean added = false;
164: //log.info("GradebookService instance=" + g);
165: String gradebookUId = GradebookFacade.getGradebookUId();
166: if (gradebookUId == null) {
167: return false;
168: }
169:
170: //log.info("inside addToGradebook, gradebook exists? " +
171: // g.isGradebookDefined(gradebookUId));
172: if (g.isGradebookDefined(gradebookUId)) {
173:
174: // Tool name code added by Josh Holtzman
175: Tool tool = ToolManager.getTool("sakai.samigo");
176: String appName = null;
177:
178: if (tool == null) {
179: log
180: .warn("could not get tool named sakai.samigo, "
181: + "so we're going to assume we're called 'Tests & Quizzes'");
182: appName = "Tests & Quizzes";
183: } else {
184: appName = tool.getTitle();
185: }
186:
187: if (!g.isAssignmentDefined(gradebookUId,
188: publishedAssessment.getTitle())) {
189: g.addExternalAssessment(gradebookUId,
190: publishedAssessment.getPublishedAssessmentId()
191: .toString(), null, publishedAssessment
192: .getTitle(), publishedAssessment
193: .getTotalScore().doubleValue(),
194: publishedAssessment
195: .getAssessmentAccessControl()
196: .getDueDate(), appName); // Use the app name from sakai
197: added = true;
198: }
199: }
200: return added;
201: }
202:
203: /**
204: * Update a gradebook.
205: * @param publishedAssessment the published assessment
206: * @param g the Gradebook Service
207: * @return false: cannot update the gradebook
208: * @throws java.lang.Exception
209: */
210: public boolean updateGradebook(
211: PublishedAssessmentData publishedAssessment,
212: GradebookService g) throws Exception {
213: log.debug("updateGradebook start");
214: String gradebookUId = GradebookFacade.getGradebookUId();
215: if (gradebookUId == null) {
216: return false;
217: }
218:
219: log.debug("before g.isAssignmentDefined()");
220: g.updateExternalAssessment(gradebookUId, publishedAssessment
221: .getPublishedAssessmentId().toString(), null,
222: publishedAssessment.getTitle(), publishedAssessment
223: .getTotalScore().doubleValue(),
224: publishedAssessment.getAssessmentAccessControl()
225: .getDueDate());
226: return true;
227: }
228:
229: /**
230: * Update the grading of the assessment.
231: * @param ag the assessment grading.
232: * @param g the Gradebook Service
233: * @throws java.lang.Exception
234: */
235: public void updateExternalAssessmentScore(AssessmentGradingIfc ag,
236: GradebookService g) throws Exception {
237: boolean testErrorHandling = false;
238: //log.info("GradebookService instance=" + g);
239: PublishedAssessmentService pubService = new PublishedAssessmentService();
240: GradingService gradingService = new GradingService();
241: PublishedAssessmentIfc pub = (PublishedAssessmentIfc) gradingService
242: .getPublishedAssessmentByAssessmentGradingId(ag
243: .getAssessmentGradingId().toString());
244:
245: String gradebookUId = pubService
246: .getPublishedAssessmentOwner(pub
247: .getPublishedAssessmentId());
248: if (gradebookUId == null) {
249: return;
250: }
251: g.updateExternalAssessmentScore(gradebookUId, ag
252: .getPublishedAssessmentId().toString(),
253: ag.getAgentId(), new Double(ag.getFinalScore()
254: .doubleValue()));
255: if (testErrorHandling) {
256: throw new Exception(
257: "Encountered an error in update ExternalAssessmentScore.");
258: }
259: }
260:
261: }
|