0001: /**********************************************************************************
0002: * $URL: https://source.sakaiproject.org/svn/sam/trunk/component/src/java/org/sakaiproject/tool/assessment/facade/PublishedAssessmentFacadeQueries.java $
0003: * $Id: PublishedAssessmentFacadeQueries.java 9273 2006-05-10 22:34:28Z daisyf@stanford.edu $
0004: ***********************************************************************************
0005: *
0006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
0007: *
0008: * Licensed under the Educational Community License, Version 1.0 (the"License");
0009: * you may not use this file except in compliance with the License.
0010: * You may obtain a copy of the License at
0011: *
0012: * http://www.opensource.org/licenses/ecl1.php
0013: *
0014: * Unless required by applicable law or agreed to in writing, software
0015: * distributed under the License is distributed on an "AS IS" BASIS,
0016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0017: * See the License for the specific language governing permissions and
0018: * limitations under the License.
0019: *
0020: **********************************************************************************/package org.sakaiproject.tool.assessment.facade;
0021:
0022: import java.sql.SQLException;
0023: import java.util.ArrayList;
0024: import java.util.Date;
0025: import java.util.HashMap;
0026: import java.util.HashSet;
0027: import java.util.Iterator;
0028: import java.util.List;
0029: import java.util.Set;
0030: import java.util.Vector;
0031: import java.util.Collections;
0032: import java.util.Comparator;
0033:
0034: import org.apache.commons.logging.Log;
0035: import org.apache.commons.logging.LogFactory;
0036: import org.springframework.orm.hibernate3.HibernateCallback;
0037: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
0038: import org.hibernate.HibernateException;
0039: import org.hibernate.Query;
0040: import org.hibernate.Session;
0041:
0042: import org.sakaiproject.service.gradebook.shared.GradebookService;
0043: import org.sakaiproject.spring.SpringBeanLocator;
0044: import org.sakaiproject.tool.assessment.data.dao.assessment.Answer;
0045: import org.sakaiproject.tool.assessment.data.dao.assessment.AnswerFeedback;
0046: import org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentAccessControl;
0047: import org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentAttachment;
0048: import org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentData;
0049: import org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentFeedback;
0050: import org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentMetaData;
0051: import org.sakaiproject.tool.assessment.data.dao.assessment.EvaluationModel;
0052: import org.sakaiproject.tool.assessment.data.dao.assessment.ItemAttachment;
0053: import org.sakaiproject.tool.assessment.data.dao.assessment.ItemData;
0054: import org.sakaiproject.tool.assessment.data.dao.assessment.ItemFeedback;
0055: import org.sakaiproject.tool.assessment.data.dao.assessment.ItemMetaData;
0056: import org.sakaiproject.tool.assessment.data.dao.assessment.ItemText;
0057: import org.sakaiproject.tool.assessment.data.dao.assessment.PublishedAccessControl;
0058: import org.sakaiproject.tool.assessment.data.dao.assessment.PublishedAnswer;
0059: import org.sakaiproject.tool.assessment.data.dao.assessment.PublishedAnswerFeedback;
0060: import org.sakaiproject.tool.assessment.data.dao.assessment.PublishedAssessmentData;
0061: import org.sakaiproject.tool.assessment.data.dao.assessment.PublishedAssessmentAttachment;
0062: import org.sakaiproject.tool.assessment.data.dao.assessment.PublishedEvaluationModel;
0063: import org.sakaiproject.tool.assessment.data.dao.assessment.PublishedFeedback;
0064: import org.sakaiproject.tool.assessment.data.dao.assessment.PublishedItemAttachment;
0065: import org.sakaiproject.tool.assessment.data.dao.assessment.PublishedItemData;
0066: import org.sakaiproject.tool.assessment.data.dao.assessment.PublishedItemFeedback;
0067: import org.sakaiproject.tool.assessment.data.dao.assessment.PublishedItemMetaData;
0068: import org.sakaiproject.tool.assessment.data.dao.assessment.PublishedItemText;
0069: import org.sakaiproject.tool.assessment.data.dao.assessment.PublishedMetaData;
0070: import org.sakaiproject.tool.assessment.data.dao.assessment.PublishedSectionAttachment;
0071: import org.sakaiproject.tool.assessment.data.dao.assessment.PublishedSectionData;
0072: import org.sakaiproject.tool.assessment.data.dao.assessment.PublishedSectionMetaData;
0073: import org.sakaiproject.tool.assessment.data.dao.assessment.PublishedSecuredIPAddress;
0074: import org.sakaiproject.tool.assessment.data.dao.assessment.SectionData;
0075: import org.sakaiproject.tool.assessment.data.dao.assessment.SectionMetaData;
0076: import org.sakaiproject.tool.assessment.data.dao.assessment.SectionAttachment;
0077: import org.sakaiproject.tool.assessment.data.dao.assessment.SecuredIPAddress;
0078: import org.sakaiproject.tool.assessment.data.dao.authz.AuthorizationData;
0079: import org.sakaiproject.tool.assessment.data.dao.grading.AssessmentGradingData;
0080: import org.sakaiproject.tool.assessment.data.ifc.assessment.AssessmentAccessControlIfc;
0081: import org.sakaiproject.tool.assessment.data.ifc.assessment.EvaluationModelIfc;
0082: import org.sakaiproject.tool.assessment.data.ifc.assessment.PublishedAssessmentIfc;
0083: import org.sakaiproject.tool.assessment.data.ifc.assessment.SectionDataIfc;
0084: import org.sakaiproject.tool.assessment.integration.context.IntegrationContextFactory;
0085: import org.sakaiproject.tool.assessment.integration.helper.ifc.GradebookServiceHelper;
0086: import org.sakaiproject.tool.assessment.integration.helper.ifc.PublishingTargetHelper;
0087: import org.sakaiproject.tool.assessment.osid.shared.impl.IdImpl;
0088: import org.sakaiproject.tool.assessment.services.PersistenceService;
0089: import org.sakaiproject.tool.assessment.services.assessment.AssessmentService;
0090: import org.sakaiproject.tool.assessment.facade.util.PagingUtilQueriesAPI;
0091: import org.sakaiproject.tool.assessment.qti.constants.AuthoringConstantStrings;
0092: import org.sakaiproject.component.cover.ServerConfigurationService;
0093: import org.sakaiproject.content.api.ContentResource;
0094:
0095: public class PublishedAssessmentFacadeQueries extends
0096: HibernateDaoSupport implements
0097: PublishedAssessmentFacadeQueriesAPI {
0098:
0099: private static Log log = LogFactory
0100: .getLog(PublishedAssessmentFacadeQueries.class);
0101:
0102: public static final String STARTDATE = "assessmentAccessControl.startDate";
0103:
0104: public static final String DUEDATE = "assessmentAccessControl.dueDate";
0105:
0106: public static final String RETRACTDATE = "assessmentAccessControl.retractDate";
0107:
0108: public static final String RELEASETO = "assessmentAccessControl.releaseTo";
0109:
0110: public static final String PUB_RELEASETO = "releaseTo";
0111:
0112: public static final String PUB_STARTDATE = "startDate";
0113:
0114: public static final String PUB_DUEDATE = "dueDate";
0115:
0116: public static final String TITLE = "title";
0117:
0118: public static final String GRADE = "finalScore";
0119:
0120: public static final String DUE = "dueDate";
0121:
0122: public static final String RAW = "totalAutoScore";
0123:
0124: public static final String TIME = "timeElapsed";
0125:
0126: public static final String SUBMITTED = "submittedDate";
0127:
0128: public PublishedAssessmentFacadeQueries() {
0129: }
0130:
0131: public IdImpl getId(String id) {
0132: return new IdImpl(id);
0133: }
0134:
0135: public IdImpl getId(Long id) {
0136: return new IdImpl(id);
0137: }
0138:
0139: public IdImpl getId(long id) {
0140: return new IdImpl(id);
0141: }
0142:
0143: public IdImpl getAssessmentId(String id) {
0144: return new IdImpl(id);
0145: }
0146:
0147: public IdImpl getAssessmentId(Long id) {
0148: return new IdImpl(id);
0149: }
0150:
0151: public IdImpl getAssessmentId(long id) {
0152: return new IdImpl(id);
0153: }
0154:
0155: public IdImpl getAssessmentTemplateId(String id) {
0156: return new IdImpl(id);
0157: }
0158:
0159: public IdImpl getAssessmentTemplateId(Long id) {
0160: return new IdImpl(id);
0161: }
0162:
0163: public IdImpl getAssessmentTemplateId(long id) {
0164: return new IdImpl(id);
0165: }
0166:
0167: public PublishedAssessmentData preparePublishedAssessment(
0168: AssessmentData a) {
0169: String protocol = ServerConfigurationService.getServerUrl();
0170: PublishedAssessmentData publishedAssessment = new PublishedAssessmentData(
0171: a.getTitle(), a.getDescription(), a.getComments(),
0172: TypeFacade.HOMEWORK, a.getInstructorNotification(), a
0173: .getTesteeNotification(), a
0174: .getMultipartAllowed(), a.getStatus(),
0175: AgentFacade.getAgentString(), new Date(), AgentFacade
0176: .getAgentString(), new Date());
0177: // publishedAssessment.setAssessment(a);
0178: publishedAssessment.setAssessmentId(a.getAssessmentBaseId());
0179:
0180: // section set
0181: Set publishedSectionSet = preparePublishedSectionSet(
0182: publishedAssessment, a.getSectionSet(), protocol);
0183: publishedAssessment.setSectionSet(publishedSectionSet);
0184:
0185: // access control
0186: PublishedAccessControl publishedAccessControl = preparePublishedAccessControl(
0187: publishedAssessment, (AssessmentAccessControl) a
0188: .getAssessmentAccessControl());
0189: publishedAssessment
0190: .setAssessmentAccessControl(publishedAccessControl);
0191:
0192: // evaluation model
0193: PublishedEvaluationModel publishedEvaluationModel = preparePublishedEvaluationModel(
0194: publishedAssessment, (EvaluationModel) a
0195: .getEvaluationModel());
0196: publishedAssessment
0197: .setEvaluationModel(publishedEvaluationModel);
0198:
0199: // feedback
0200: PublishedFeedback publishedFeedback = preparePublishedFeedback(
0201: publishedAssessment, (AssessmentFeedback) a
0202: .getAssessmentFeedback());
0203: publishedAssessment.setAssessmentFeedback(publishedFeedback);
0204:
0205: // metadata
0206: Set publishedMetaDataSet = preparePublishedMetaDataSet(
0207: publishedAssessment, a.getAssessmentMetaDataSet());
0208: log
0209: .debug("******* metadata set"
0210: + a.getAssessmentMetaDataSet());
0211: log.debug("******* published metadata set"
0212: + publishedMetaDataSet);
0213: publishedAssessment
0214: .setAssessmentMetaDataSet(publishedMetaDataSet);
0215:
0216: // let's check if we need a publishedUrl
0217: String releaseTo = publishedAccessControl.getReleaseTo();
0218: if (releaseTo != null) {
0219: boolean anonymousAllowed = ((releaseTo)
0220: .indexOf(AuthoringConstantStrings.ANONYMOUS) > -1);
0221: if (anonymousAllowed) {
0222: // generate an alias to the pub assessment
0223: String alias = AgentFacade.getAgentString()
0224: + (new Date()).getTime();
0225: PublishedMetaData meta = new PublishedMetaData(
0226: publishedAssessment, "ALIAS", alias);
0227: publishedMetaDataSet.add(meta);
0228: publishedAssessment
0229: .setAssessmentMetaDataSet(publishedMetaDataSet);
0230: }
0231: }
0232:
0233: // IPAddress
0234: Set publishedIPSet = preparePublishedSecuredIPSet(
0235: publishedAssessment, a.getSecuredIPAddressSet());
0236: publishedAssessment.setSecuredIPAddressSet(publishedIPSet);
0237:
0238: // attachmentSet
0239: Set publishedAssessmentAttachmentSet = preparePublishedAssessmentAttachmentSet(
0240: publishedAssessment, a.getAssessmentAttachmentSet(),
0241: protocol);
0242: publishedAssessment
0243: .setAssessmentAttachmentSet(publishedAssessmentAttachmentSet);
0244:
0245: return publishedAssessment;
0246: }
0247:
0248: public PublishedFeedback preparePublishedFeedback(
0249: PublishedAssessmentData p, AssessmentFeedback a) {
0250: if (a == null) {
0251: return null;
0252: }
0253: PublishedFeedback publishedFeedback = new PublishedFeedback(a
0254: .getFeedbackDelivery(), a.getFeedbackAuthoring(), a
0255: .getEditComponents(), a.getShowQuestionText(), a
0256: .getShowStudentResponse(), a.getShowCorrectResponse(),
0257: a.getShowStudentScore(), a
0258: .getShowStudentQuestionScore(), a
0259: .getShowQuestionLevelFeedback(), a
0260: .getShowSelectionLevelFeedback(), a
0261: .getShowGraderComments(), a.getShowStatistics());
0262: publishedFeedback.setAssessmentBase(p);
0263: return publishedFeedback;
0264: }
0265:
0266: public PublishedAccessControl preparePublishedAccessControl(
0267: PublishedAssessmentData p, AssessmentAccessControl a) {
0268: if (a == null) {
0269: return new PublishedAccessControl();
0270: }
0271: PublishedAccessControl publishedAccessControl = new PublishedAccessControl(
0272: a.getSubmissionsAllowed(), a.getSubmissionsSaved(), a
0273: .getAssessmentFormat(), a.getBookMarkingItem(),
0274: a.getTimeLimit(), a.getTimedAssessment(), a
0275: .getRetryAllowed(), a.getLateHandling(), a
0276: .getStartDate(), a.getDueDate(), a
0277: .getScoreDate(), a.getFeedbackDate());
0278: publishedAccessControl.setRetractDate(a.getRetractDate());
0279: publishedAccessControl.setAutoSubmit(a.getAutoSubmit());
0280: publishedAccessControl.setItemNavigation(a.getItemNavigation());
0281: publishedAccessControl.setItemNumbering(a.getItemNumbering());
0282: publishedAccessControl.setSubmissionMessage(a
0283: .getSubmissionMessage());
0284: publishedAccessControl.setReleaseTo(a.getReleaseTo());
0285: publishedAccessControl.setUsername(a.getUsername());
0286: publishedAccessControl.setPassword(a.getPassword());
0287: publishedAccessControl.setFinalPageUrl(a.getFinalPageUrl());
0288: publishedAccessControl.setUnlimitedSubmissions(a
0289: .getUnlimitedSubmissions());
0290: publishedAccessControl.setAssessmentBase(p);
0291: return publishedAccessControl;
0292: }
0293:
0294: public PublishedEvaluationModel preparePublishedEvaluationModel(
0295: PublishedAssessmentData p, EvaluationModel e) {
0296: if (e == null) {
0297: return null;
0298: }
0299: PublishedEvaluationModel publishedEvaluationModel = new PublishedEvaluationModel(
0300: e.getEvaluationComponents(), e.getScoringType(), e
0301: .getNumericModelId(), e.getFixedTotalScore(), e
0302: .getGradeAvailable(), e.getIsStudentIdPublic(),
0303: e.getAnonymousGrading(), e.getAutoScoring(), e
0304: .getToGradeBook());
0305: publishedEvaluationModel.setAssessmentBase(p);
0306: return publishedEvaluationModel;
0307: }
0308:
0309: public Set preparePublishedMetaDataSet(PublishedAssessmentData p,
0310: Set metaDataSet) {
0311: HashSet h = new HashSet();
0312: Iterator i = metaDataSet.iterator();
0313: while (i.hasNext()) {
0314: AssessmentMetaData metaData = (AssessmentMetaData) i.next();
0315: PublishedMetaData publishedMetaData = new PublishedMetaData(
0316: p, metaData.getLabel(), metaData.getEntry());
0317: h.add(publishedMetaData);
0318: }
0319: return h;
0320: }
0321:
0322: public Set preparePublishedSecuredIPSet(PublishedAssessmentData p,
0323: Set ipSet) {
0324: HashSet h = new HashSet();
0325: Iterator i = ipSet.iterator();
0326: while (i.hasNext()) {
0327: SecuredIPAddress ip = (SecuredIPAddress) i.next();
0328: PublishedSecuredIPAddress publishedIP = new PublishedSecuredIPAddress(
0329: p, ip.getHostname(), ip.getIpAddress());
0330: h.add(publishedIP);
0331: }
0332: return h;
0333: }
0334:
0335: public Set preparePublishedSectionSet(
0336: PublishedAssessmentData publishedAssessment,
0337: Set sectionSet, String protocol) {
0338: log.debug("**published section size = " + sectionSet.size());
0339: HashSet h = new HashSet();
0340: Iterator i = sectionSet.iterator();
0341: while (i.hasNext()) {
0342: SectionData section = (SectionData) i.next();
0343:
0344: // TODO note: 4/28 need to check if a part is random draw , if it is
0345: // then need to add questions from pool to this section, at this
0346: // point,
0347:
0348: PublishedSectionData publishedSection = new PublishedSectionData(
0349: section.getDuration(), section.getSequence(),
0350: section.getTitle(), section.getDescription(),
0351: section.getTypeId(), section.getStatus(), section
0352: .getCreatedBy(), section.getCreatedDate(),
0353: section.getLastModifiedBy(), section
0354: .getLastModifiedDate());
0355: Set publishedSectionAttachmentSet = preparePublishedSectionAttachmentSet(
0356: publishedSection,
0357: section.getSectionAttachmentSet(), protocol);
0358: publishedSection
0359: .setSectionAttachmentSet(publishedSectionAttachmentSet);
0360: Set publishedItemSet = preparePublishedItemSet(
0361: publishedSection, section.getItemSet(), protocol);
0362: publishedSection.setItemSet(publishedItemSet);
0363: Set publishedMetaDataSet = preparePublishedSectionMetaDataSet(
0364: publishedSection, section.getSectionMetaDataSet());
0365: publishedSection
0366: .setSectionMetaDataSet(publishedMetaDataSet);
0367: publishedSection.setAssessment(publishedAssessment);
0368: h.add(publishedSection);
0369: }
0370: return h;
0371: }
0372:
0373: public Set preparePublishedSectionMetaDataSet(
0374: PublishedSectionData publishedSection, Set metaDataSet) {
0375: HashSet h = new HashSet();
0376: Iterator n = metaDataSet.iterator();
0377: while (n.hasNext()) {
0378: SectionMetaData sectionMetaData = (SectionMetaData) n
0379: .next();
0380: PublishedSectionMetaData publishedSectionMetaData = new PublishedSectionMetaData(
0381: publishedSection, sectionMetaData.getLabel(),
0382: sectionMetaData.getEntry());
0383: h.add(publishedSectionMetaData);
0384: }
0385: return h;
0386: }
0387:
0388: public Set preparePublishedItemSet(
0389: PublishedSectionData publishedSection, Set itemSet,
0390: String protocol) {
0391: log.debug("**published item size = " + itemSet.size());
0392: HashSet h = new HashSet();
0393: Iterator j = itemSet.iterator();
0394: while (j.hasNext()) {
0395: ItemData item = (ItemData) j.next();
0396: PublishedItemData publishedItem = new PublishedItemData(
0397: publishedSection, item.getSequence(), item
0398: .getDuration(), item.getInstruction(), item
0399: .getDescription(), item.getTypeId(), item
0400: .getGrade(), item.getScore(), item
0401: .getHint(), item.getHasRationale(), item
0402: .getStatus(), item.getCreatedBy(), item
0403: .getCreatedDate(),
0404: item.getLastModifiedBy(), item
0405: .getLastModifiedDate(), null, null, null, // set ItemTextSet, itemMetaDataSet and
0406: // itemFeedbackSet later
0407: item.getTriesAllowed());
0408: Set publishedItemTextSet = preparePublishedItemTextSet(
0409: publishedItem, item.getItemTextSet());
0410: Set publishedItemMetaDataSet = preparePublishedItemMetaDataSet(
0411: publishedItem, item.getItemMetaDataSet());
0412: Set publishedItemFeedbackSet = preparePublishedItemFeedbackSet(
0413: publishedItem, item.getItemFeedbackSet());
0414: Set publishedItemAttachmentSet = preparePublishedItemAttachmentSet(
0415: publishedItem, item.getItemAttachmentSet(),
0416: protocol);
0417: publishedItem.setItemTextSet(publishedItemTextSet);
0418: publishedItem.setItemMetaDataSet(publishedItemMetaDataSet);
0419: publishedItem.setItemFeedbackSet(publishedItemFeedbackSet);
0420: publishedItem
0421: .setItemAttachmentSet(publishedItemAttachmentSet);
0422: h.add(publishedItem);
0423: }
0424: return h;
0425: }
0426:
0427: public Set preparePublishedItemTextSet(
0428: PublishedItemData publishedItem, Set itemTextSet) {
0429: log.debug("**published item text size = " + itemTextSet.size());
0430: HashSet h = new HashSet();
0431: Iterator k = itemTextSet.iterator();
0432: while (k.hasNext()) {
0433: ItemText itemText = (ItemText) k.next();
0434: log.debug("**item text id =" + itemText.getId());
0435: PublishedItemText publishedItemText = new PublishedItemText(
0436: publishedItem, itemText.getSequence(), itemText
0437: .getText(), null);
0438: Set publishedAnswerSet = preparePublishedAnswerSet(
0439: publishedItemText, itemText.getAnswerSet());
0440: publishedItemText.setAnswerSet(publishedAnswerSet);
0441: h.add(publishedItemText);
0442: }
0443: return h;
0444: }
0445:
0446: public Set preparePublishedItemMetaDataSet(
0447: PublishedItemData publishedItem, Set itemMetaDataSet) {
0448: HashSet h = new HashSet();
0449: Iterator n = itemMetaDataSet.iterator();
0450: while (n.hasNext()) {
0451: ItemMetaData itemMetaData = (ItemMetaData) n.next();
0452: PublishedItemMetaData publishedItemMetaData = new PublishedItemMetaData(
0453: publishedItem, itemMetaData.getLabel(),
0454: itemMetaData.getEntry());
0455: h.add(publishedItemMetaData);
0456: }
0457: return h;
0458: }
0459:
0460: public Set preparePublishedItemFeedbackSet(
0461: PublishedItemData publishedItem, Set itemFeedbackSet) {
0462: HashSet h = new HashSet();
0463: Iterator o = itemFeedbackSet.iterator();
0464: while (o.hasNext()) {
0465: ItemFeedback itemFeedback = (ItemFeedback) o.next();
0466: PublishedItemFeedback publishedItemFeedback = new PublishedItemFeedback(
0467: publishedItem, itemFeedback.getTypeId(),
0468: itemFeedback.getText());
0469: h.add(publishedItemFeedback);
0470: }
0471: return h;
0472: }
0473:
0474: public Set preparePublishedItemAttachmentSet(
0475: PublishedItemData publishedItem, Set itemAttachmentSet,
0476: String protocol) {
0477: HashSet h = new HashSet();
0478: Iterator o = itemAttachmentSet.iterator();
0479: while (o.hasNext()) {
0480: ItemAttachment itemAttachment = (ItemAttachment) o.next();
0481: try {
0482: // create a copy of the resource
0483: AssessmentService service = new AssessmentService();
0484: ContentResource cr_copy = service
0485: .createCopyOfContentResource(itemAttachment
0486: .getResourceId(), itemAttachment
0487: .getFilename());
0488: // get relative path
0489: String url = getRelativePath(cr_copy.getUrl(), protocol);
0490:
0491: PublishedItemAttachment publishedItemAttachment = new PublishedItemAttachment(
0492: null, publishedItem, cr_copy.getId(),
0493: itemAttachment.getFilename(), itemAttachment
0494: .getMimeType(), itemAttachment
0495: .getFileSize(), itemAttachment
0496: .getDescription(), url, itemAttachment
0497: .getIsLink(), itemAttachment
0498: .getStatus(), itemAttachment
0499: .getCreatedBy(), itemAttachment
0500: .getCreatedDate(), itemAttachment
0501: .getLastModifiedBy(), itemAttachment
0502: .getLastModifiedDate());
0503: h.add(publishedItemAttachment);
0504: } catch (Exception e) {
0505: log.warn(e.getMessage());
0506: }
0507: }
0508: return h;
0509: }
0510:
0511: public String getRelativePath(String url, String protocol) {
0512: // replace whitespace with %20
0513: url = replaceSpace(url);
0514: String location = url;
0515: int index = url.lastIndexOf(protocol);
0516: if (index == 0) {
0517: location = url.substring(protocol.length());
0518: }
0519: return location;
0520: }
0521:
0522: public Set preparePublishedSectionAttachmentSet(
0523: PublishedSectionData publishedSection,
0524: Set sectionAttachmentSet, String protocol) {
0525: HashSet h = new HashSet();
0526: Iterator o = sectionAttachmentSet.iterator();
0527: while (o.hasNext()) {
0528: SectionAttachment sectionAttachment = (SectionAttachment) o
0529: .next();
0530: try {
0531: // create a copy of the resource
0532: AssessmentService service = new AssessmentService();
0533: ContentResource cr_copy = service
0534: .createCopyOfContentResource(sectionAttachment
0535: .getResourceId(), sectionAttachment
0536: .getFilename());
0537:
0538: // get relative path
0539: String url = getRelativePath(cr_copy.getUrl(), protocol);
0540:
0541: PublishedSectionAttachment publishedSectionAttachment = new PublishedSectionAttachment(
0542: null, publishedSection, cr_copy.getId(),
0543: sectionAttachment.getFilename(),
0544: sectionAttachment.getMimeType(),
0545: sectionAttachment.getFileSize(),
0546: sectionAttachment.getDescription(), url,
0547: sectionAttachment.getIsLink(),
0548: sectionAttachment.getStatus(),
0549: sectionAttachment.getCreatedBy(),
0550: sectionAttachment.getCreatedDate(),
0551: sectionAttachment.getLastModifiedBy(),
0552: sectionAttachment.getLastModifiedDate());
0553: h.add(publishedSectionAttachment);
0554: } catch (Exception e) {
0555: log.warn(e.getMessage());
0556: }
0557: }
0558: return h;
0559: }
0560:
0561: public Set preparePublishedAssessmentAttachmentSet(
0562: PublishedAssessmentData publishedAssessment,
0563: Set assessmentAttachmentSet, String protocol) {
0564: HashSet h = new HashSet();
0565: Iterator o = assessmentAttachmentSet.iterator();
0566: while (o.hasNext()) {
0567: AssessmentAttachment assessmentAttachment = (AssessmentAttachment) o
0568: .next();
0569: try {
0570: // create a copy of the resource
0571: AssessmentService service = new AssessmentService();
0572: ContentResource cr_copy = service
0573: .createCopyOfContentResource(
0574: assessmentAttachment.getResourceId(),
0575: assessmentAttachment.getFilename());
0576:
0577: // get relative path
0578: String url = getRelativePath(cr_copy.getUrl(), protocol);
0579:
0580: PublishedAssessmentAttachment publishedAssessmentAttachment = new PublishedAssessmentAttachment(
0581: null, publishedAssessment, cr_copy.getId(),
0582: assessmentAttachment.getFilename(),
0583: assessmentAttachment.getMimeType(),
0584: assessmentAttachment.getFileSize(),
0585: assessmentAttachment.getDescription(), url,
0586: assessmentAttachment.getIsLink(),
0587: assessmentAttachment.getStatus(),
0588: assessmentAttachment.getCreatedBy(),
0589: assessmentAttachment.getCreatedDate(),
0590: assessmentAttachment.getLastModifiedBy(),
0591: assessmentAttachment.getLastModifiedDate());
0592: h.add(publishedAssessmentAttachment);
0593: } catch (Exception e) {
0594: log.warn(e.getMessage());
0595: }
0596: }
0597: return h;
0598: }
0599:
0600: public Set preparePublishedAnswerSet(
0601: PublishedItemText publishedItemText, Set answerSet) {
0602: log.debug("**published answer size = " + answerSet.size());
0603: HashSet h = new HashSet();
0604: Iterator l = answerSet.iterator();
0605: while (l.hasNext()) {
0606: Answer answer = (Answer) l.next();
0607: PublishedAnswer publishedAnswer = new PublishedAnswer(
0608: publishedItemText, answer.getText(), answer
0609: .getSequence(), answer.getLabel(), answer
0610: .getIsCorrect(), answer.getGrade(), answer
0611: .getScore(), null);
0612: Set publishedAnswerFeedbackSet = preparePublishedAnswerFeedbackSet(
0613: publishedAnswer, answer.getAnswerFeedbackSet());
0614: publishedAnswer
0615: .setAnswerFeedbackSet(publishedAnswerFeedbackSet);
0616: h.add(publishedAnswer);
0617: }
0618: return h;
0619: }
0620:
0621: public Set preparePublishedAnswerFeedbackSet(
0622: PublishedAnswer publishedAnswer, Set answerFeedbackSet) {
0623: HashSet h = new HashSet();
0624: Iterator m = answerFeedbackSet.iterator();
0625: while (m.hasNext()) {
0626: AnswerFeedback answerFeedback = (AnswerFeedback) m.next();
0627: PublishedAnswerFeedback publishedAnswerFeedback = new PublishedAnswerFeedback(
0628: publishedAnswer, answerFeedback.getTypeId(),
0629: answerFeedback.getText());
0630: h.add(publishedAnswerFeedback);
0631: }
0632: return h;
0633: }
0634:
0635: public PublishedAssessmentFacade getPublishedAssessment(
0636: Long assessmentId) {
0637: PublishedAssessmentData a = loadPublishedAssessment(assessmentId);
0638: a.setSectionSet(getSectionSetForAssessment(a));
0639: PublishedAssessmentFacade f = new PublishedAssessmentFacade(a);
0640: return f;
0641: }
0642:
0643: public Long getPublishedAssessmentId(Long assessmentId) {
0644: List list = getHibernateTemplate()
0645: .find(
0646: "from PublishedAssessmentData as p where p.assessmentId=? order by p.createdDate desc",
0647: assessmentId);
0648: Long publishedId = new Long(0);
0649: if (!list.isEmpty()) {
0650: PublishedAssessmentData f = (PublishedAssessmentData) list
0651: .get(0);
0652: publishedId = f.getPublishedAssessmentId();
0653: }
0654: return publishedId;
0655:
0656: }
0657:
0658: public PublishedAssessmentFacade publishAssessment(
0659: AssessmentFacade assessment) throws Exception {
0660:
0661: PublishedAssessmentData publishedAssessment = preparePublishedAssessment((AssessmentData) assessment
0662: .getData());
0663:
0664: try {
0665: saveOrUpdate(publishedAssessment);
0666: } catch (Exception e) {
0667: throw e;
0668: }
0669:
0670: // add to gradebook
0671: if (publishedAssessment.getEvaluationModel() != null) {
0672: String toGradebook = publishedAssessment
0673: .getEvaluationModel().getToGradeBook();
0674:
0675: boolean integrated = IntegrationContextFactory
0676: .getInstance().isIntegrated();
0677: GradebookService g = null;
0678: if (integrated) {
0679: g = (GradebookService) SpringBeanLocator
0680: .getInstance()
0681: .getBean(
0682: "org.sakaiproject.service.gradebook.GradebookService");
0683: }
0684:
0685: GradebookServiceHelper gbsHelper = IntegrationContextFactory
0686: .getInstance().getGradebookServiceHelper();
0687:
0688: if (gbsHelper.gradebookExists(GradebookFacade
0689: .getGradebookUId(), g)
0690: && toGradebook != null
0691: && toGradebook
0692: .equals(EvaluationModelIfc.TO_DEFAULT_GRADEBOOK
0693: .toString())) {
0694: try {
0695: gbsHelper.addToGradebook(publishedAssessment, g);
0696: } catch (Exception e) {
0697: log.error("Removing published assessment: " + e);
0698: delete(publishedAssessment);
0699: throw e;
0700: }
0701: }
0702: }
0703:
0704: // write authorization
0705: createAuthorization(publishedAssessment);
0706: return new PublishedAssessmentFacade(publishedAssessment);
0707: }
0708:
0709: // This method is specific for publish an assessment for preview assessment,
0710: // because it will be deleted after preview is done, and shouldn't talk to
0711: // gradebook.
0712: public PublishedAssessmentFacade publishPreviewAssessment(
0713: AssessmentFacade assessment) {
0714: // boolean addedToGradebook = false;
0715: PublishedAssessmentData publishedAssessment = preparePublishedAssessment((AssessmentData) assessment
0716: .getData());
0717: publishedAssessment
0718: .setStatus(PublishedAssessmentIfc.DEAD_STATUS);
0719: try {
0720: saveOrUpdate(publishedAssessment);
0721: } catch (Exception e) {
0722: log.warn(e.getMessage());
0723: }
0724: // write authorization
0725: createAuthorization(publishedAssessment);
0726: return new PublishedAssessmentFacade(publishedAssessment);
0727: }
0728:
0729: public void createAuthorization(PublishedAssessmentData p) {
0730: String qualifierIdString = p.getPublishedAssessmentId()
0731: .toString();
0732: Vector v = new Vector();
0733:
0734: // 1. get all possible publishing targets (agentKey, agentId)
0735: PublishingTargetHelper ptHelper = IntegrationContextFactory
0736: .getInstance().getPublishingTargetHelper();
0737:
0738: HashMap targets = ptHelper.getTargets();
0739: // Fixed for SAK-7251
0740: HashMap trimedTargets = new HashMap();
0741: Iterator iter = targets.keySet().iterator();
0742: String key;
0743: String value;
0744: while (iter.hasNext()) {
0745: key = (String) iter.next();
0746: value = (String) targets.get(key);
0747: trimedTargets.put(key.trim(), value);
0748: }
0749:
0750: // 2. get the key of the target selected, it is stored in
0751: // accessControl.releaseTo
0752: AssessmentAccessControlIfc control = p
0753: .getAssessmentAccessControl();
0754: String releaseTo = control.getReleaseTo();
0755: if (releaseTo != null) {
0756: String[] targetSelected = releaseTo.split(",");
0757: for (int i = 0; i < targetSelected.length; i++) {
0758: String agentKey = targetSelected[i].trim();
0759: // add agentId into v
0760: if (trimedTargets.get(agentKey) != null) {
0761: v.add((String) trimedTargets.get(agentKey));
0762: }
0763: }
0764: }
0765: // 3. give selected site right to view Published Assessment
0766: PersistenceService.getInstance().getAuthzQueriesFacade()
0767: .createAuthorization(AgentFacade.getCurrentSiteId(),
0768: "OWN_PUBLISHED_ASSESSMENT", qualifierIdString);
0769: // 4. create authorization for all the agentId in v
0770: for (int i = 0; i < v.size(); i++) {
0771: String agentId = (String) v.get(i);
0772: log.debug("** agentId=" + agentId);
0773: PersistenceService.getInstance().getAuthzQueriesFacade()
0774: .createAuthorization(agentId,
0775: "TAKE_PUBLISHED_ASSESSMENT",
0776: qualifierIdString);
0777: PersistenceService.getInstance().getAuthzQueriesFacade()
0778: .createAuthorization(agentId,
0779: "VIEW_PUBLISHED_ASSESSMENT_FEEDBACK",
0780: qualifierIdString);
0781: PersistenceService.getInstance().getAuthzQueriesFacade()
0782: .createAuthorization(agentId,
0783: "GRADE_PUBLISHED_ASSESSMENT",
0784: qualifierIdString);
0785: PersistenceService.getInstance().getAuthzQueriesFacade()
0786: .createAuthorization(agentId,
0787: "VIEW_PUBLISHED_ASSESSMENT",
0788: qualifierIdString);
0789: }
0790: }
0791:
0792: public AssessmentData loadAssessment(Long assessmentId) {
0793: return (AssessmentData) getHibernateTemplate().load(
0794: AssessmentData.class, assessmentId);
0795: }
0796:
0797: public PublishedAssessmentData loadPublishedAssessment(
0798: Long assessmentId) {
0799: return (PublishedAssessmentData) getHibernateTemplate().load(
0800: PublishedAssessmentData.class, assessmentId);
0801: }
0802:
0803: public ArrayList getAllTakeableAssessments(String orderBy,
0804: boolean ascending, final Integer status) {
0805:
0806: String query = "from PublishedAssessmentData as p where p.status=? order by p."
0807: + orderBy;
0808:
0809: if (ascending) {
0810: query += " asc";
0811: } else {
0812: query += " desc";
0813: }
0814: log.debug("Order by " + orderBy);
0815:
0816: final String hql = query;
0817: final HibernateCallback hcb = new HibernateCallback() {
0818: public Object doInHibernate(Session session)
0819: throws HibernateException, SQLException {
0820: Query q = session.createQuery(hql);
0821: q.setInteger(0, status.intValue());
0822: return q.list();
0823: };
0824: };
0825: List list = getHibernateTemplate().executeFind(hcb);
0826:
0827: // List list = getHibernateTemplate().find(query, new Object[] {status}
0828: // ,
0829: // new org.hibernate.type.Type[] {
0830: // Hibernate.
0831: // INTEGER});
0832: ArrayList assessmentList = new ArrayList();
0833: for (int i = 0; i < list.size(); i++) {
0834: PublishedAssessmentData a = (PublishedAssessmentData) list
0835: .get(i);
0836: log.debug("Title: " + a.getTitle());
0837: // Don't need sections for list of assessments
0838: // a.setSectionSet(getSectionSetForAssessment(a));
0839: PublishedAssessmentFacade f = new PublishedAssessmentFacade(
0840: a);
0841: assessmentList.add(f);
0842: }
0843: return assessmentList;
0844: }
0845:
0846: public Integer getNumberOfSubmissions(
0847: final String publishedAssessmentId, final String agentId) {
0848: final String query = "select count(a) from AssessmentGradingData a where a.publishedAssessmentId=? and a.agentId=? and a.forGrade=?";
0849: // Object[] objects = new Object[3];
0850: // objects[0] = new Long(publishedAssessmentId);
0851: // objects[1] = agentId;
0852: // objects[2] = new Boolean(true);
0853: // Type[] types = new Type[3];
0854: // types[0] = Hibernate.LONG;
0855: // types[1] = Hibernate.STRING;
0856: // types[2] = Hibernate.BOOLEAN;
0857:
0858: final HibernateCallback hcb = new HibernateCallback() {
0859: public Object doInHibernate(Session session)
0860: throws HibernateException, SQLException {
0861: Query q = session.createQuery(query);
0862: q.setLong(0, Long.parseLong(publishedAssessmentId));
0863: q.setString(1, agentId);
0864: q.setBoolean(2, true);
0865: return q.list();
0866: };
0867: };
0868: List list = getHibernateTemplate().executeFind(hcb);
0869:
0870: // List list = getHibernateTemplate().find(query, objects, types);
0871: return (Integer) list.get(0);
0872: }
0873:
0874: public List getNumberOfSubmissionsOfAllAssessmentsByAgent(
0875: final String agentId) {
0876: final String query = "select new AssessmentGradingData("
0877: + " a.publishedAssessmentId, count(a)) "
0878: + " from AssessmentGradingData as a where a.agentId=? and a.forGrade=?"
0879: + " group by a.publishedAssessmentId";
0880:
0881: final HibernateCallback hcb = new HibernateCallback() {
0882: public Object doInHibernate(Session session)
0883: throws HibernateException, SQLException {
0884: Query q = session.createQuery(query);
0885: q.setString(0, agentId);
0886: q.setBoolean(1, true);
0887: return q.list();
0888: };
0889: };
0890: return getHibernateTemplate().executeFind(hcb);
0891: }
0892:
0893: public List getNumberOfSubmissionsOfAllAssessmentsByAgent(
0894: final String agentId, final String siteId) {
0895: final String query = "select new AssessmentGradingData("
0896: + " a.publishedAssessmentId, count(a)) "
0897: + " from AssessmentGradingData as a, AuthorizationData as az "
0898: + " where a.agentId=? and a.forGrade=? and az.agentIdString=? "
0899: + " and az.functionId='TAKE_PUBLISHED_ASSESSMENT' and az.qualifierId=a.publishedAssessmentId"
0900: + " group by a.publishedAssessmentId";
0901:
0902: final HibernateCallback hcb = new HibernateCallback() {
0903: public Object doInHibernate(Session session)
0904: throws HibernateException, SQLException {
0905: Query q = session.createQuery(query);
0906: q.setString(0, agentId);
0907: q.setBoolean(1, true);
0908: q.setString(2, siteId);
0909: return q.list();
0910: };
0911: };
0912: return getHibernateTemplate().executeFind(hcb);
0913: }
0914:
0915: public ArrayList getAllPublishedAssessments(String sortString) {
0916: String orderBy = getOrderBy(sortString);
0917: List list = getHibernateTemplate().find(
0918: "from PublishedAssessmentData p order by p." + orderBy);
0919: ArrayList assessmentList = new ArrayList();
0920: for (int i = 0; i < list.size(); i++) {
0921: PublishedAssessmentData a = (PublishedAssessmentData) list
0922: .get(i);
0923: a.setSectionSet(getSectionSetForAssessment(a));
0924: PublishedAssessmentFacade f = new PublishedAssessmentFacade(
0925: a);
0926: assessmentList.add(f);
0927: }
0928: return assessmentList;
0929: }
0930:
0931: public ArrayList getAllPublishedAssessments(String sortString,
0932: final Integer status) {
0933: final String orderBy = getOrderBy(sortString);
0934:
0935: final HibernateCallback hcb = new HibernateCallback() {
0936: public Object doInHibernate(Session session)
0937: throws HibernateException, SQLException {
0938: Query q = session
0939: .createQuery("from PublishedAssessmentData as p where p.status=? order by p."
0940: + orderBy);
0941: q.setInteger(0, status.intValue());
0942: return q.list();
0943: };
0944: };
0945: List list = getHibernateTemplate().executeFind(hcb);
0946:
0947: // List list = getHibernateTemplate().find(
0948: // "from PublishedAssessmentData as p where p.status=? order by p." +
0949: // orderBy,
0950: // new Object[] {status}
0951: // , new org.hibernate.type.Type[] {Hibernate.INTEGER});
0952: ArrayList assessmentList = new ArrayList();
0953: for (int i = 0; i < list.size(); i++) {
0954: PublishedAssessmentData a = (PublishedAssessmentData) list
0955: .get(i);
0956: a.setSectionSet(getSectionSetForAssessment(a));
0957: PublishedAssessmentFacade f = new PublishedAssessmentFacade(
0958: a);
0959: assessmentList.add(f);
0960: }
0961: return assessmentList;
0962: }
0963:
0964: public ArrayList getAllPublishedAssessments(int pageSize,
0965: int pageNumber, String sortString, Integer status) {
0966: String orderBy = getOrderBy(sortString);
0967: String queryString = "from PublishedAssessmentData p order by p."
0968: + orderBy;
0969: if (!status.equals(PublishedAssessmentFacade.ANY_STATUS)) {
0970: queryString = "from PublishedAssessmentData p where p.status ="
0971: + status.intValue() + " order by p." + orderBy;
0972: }
0973: PagingUtilQueriesAPI pagingUtilQueries = PersistenceService
0974: .getInstance().getPagingUtilQueries();
0975: List pageList = pagingUtilQueries.getAll(pageSize, pageNumber,
0976: queryString);
0977: log.debug("**** pageList=" + pageList);
0978: ArrayList assessmentList = new ArrayList();
0979: for (int i = 0; i < pageList.size(); i++) {
0980: PublishedAssessmentData a = (PublishedAssessmentData) pageList
0981: .get(i);
0982: a.setSectionSet(getSectionSetForAssessment(a));
0983: log.debug("**** published assessment=" + a.getTitle());
0984: PublishedAssessmentFacade f = new PublishedAssessmentFacade(
0985: a);
0986: log
0987: .debug("**** published assessment title="
0988: + f.getTitle());
0989: assessmentList.add(f);
0990: }
0991: return assessmentList;
0992: }
0993:
0994: public void removeAssessment(Long assessmentId) {
0995: PublishedAssessmentData assessment = (PublishedAssessmentData) getHibernateTemplate()
0996: .load(PublishedAssessmentData.class, assessmentId);
0997: // if AssessmentGradingData exist, simply set pub assessment to inactive
0998: // else delete assessment
0999: List count = getHibernateTemplate()
1000: .find(
1001: "select count(g) from AssessmentGradingData g where g.publishedAssessmentId=?",
1002: assessment.getPublishedAssessmentId());
1003: log.debug("no. of Assessment Grading =" + count.size());
1004: Iterator iter = count.iterator();
1005: int i = ((Integer) iter.next()).intValue();
1006: if (i > 0) {
1007: assessment.setStatus(PublishedAssessmentIfc.DEAD_STATUS);
1008: try {
1009: saveOrUpdate(assessment);
1010: } catch (Exception e) {
1011: log.warn(e.getMessage());
1012: }
1013: } else {
1014: delete(assessment);
1015: // remove authorization
1016: PersistenceService.getInstance().getAuthzQueriesFacade()
1017: .removeAuthorizationByQualifier(
1018: assessment.getPublishedAssessmentId()
1019: .toString(), true);
1020: }
1021: }
1022:
1023: private String getOrderBy(String sortString) {
1024: String startDate = (PublishedAssessmentFacadeQueries.STARTDATE)
1025: .substring((PublishedAssessmentFacadeQueries.STARTDATE)
1026: .lastIndexOf(".") + 1);
1027: String dueDate = (PublishedAssessmentFacadeQueries.DUEDATE)
1028: .substring((PublishedAssessmentFacadeQueries.DUEDATE)
1029: .lastIndexOf(".") + 1);
1030: String releaseTo = (PublishedAssessmentFacadeQueries.RELEASETO)
1031: .substring((PublishedAssessmentFacadeQueries.RELEASETO)
1032: .lastIndexOf(".") + 1);
1033:
1034: if ((sortString).equals(startDate)) {
1035: return PublishedAssessmentFacadeQueries.STARTDATE;
1036: } else if ((sortString).equals(dueDate)) {
1037: return PublishedAssessmentFacadeQueries.DUEDATE;
1038: } else if ((sortString).equals(releaseTo)) {
1039: return PublishedAssessmentFacadeQueries.RELEASETO;
1040: } else {
1041: return PublishedAssessmentFacadeQueries.TITLE;
1042: }
1043: }
1044:
1045: public void saveOrUpdate(PublishedAssessmentIfc assessment)
1046: throws Exception {
1047: PublishedAssessmentData data;
1048: if (assessment instanceof PublishedAssessmentFacade)
1049: data = (PublishedAssessmentData) ((PublishedAssessmentFacade) assessment)
1050: .getData();
1051: else
1052: data = (PublishedAssessmentData) assessment;
1053:
1054: int retryCount = PersistenceService.getInstance()
1055: .getRetryCount().intValue();
1056: while (retryCount > 0) {
1057: try {
1058: getHibernateTemplate().saveOrUpdate(data);
1059: retryCount = 0;
1060: } catch (Exception e) {
1061: log.warn("problem save or update assessment: "
1062: + e.getMessage());
1063: retryCount = PersistenceService.getInstance()
1064: .retryDeadlock(e, retryCount);
1065: if (retryCount == 0)
1066: throw e;
1067: }
1068: }
1069: }
1070:
1071: public ArrayList getBasicInfoOfAllActivePublishedAssessments(
1072: String sortString, final String siteAgentId,
1073: boolean ascending) {
1074: Date currentDate = new Date();
1075: String orderBy = getOrderBy(sortString);
1076:
1077: String query = "select new PublishedAssessmentData(p.publishedAssessmentId, p.title, "
1078: + " c.releaseTo, c.startDate, c.dueDate, c.retractDate) "
1079: + " from PublishedAssessmentData p, PublishedAccessControl c, AuthorizationData z "
1080: + " where c.assessment = p and p.status=1 and "
1081: + " p.publishedAssessmentId=z.qualifierId and z.functionId='OWN_PUBLISHED_ASSESSMENT' "
1082: + " and z.agentIdString= ? order by p." + orderBy;
1083: if (ascending == true)
1084: query += " asc";
1085: else
1086: query += " desc";
1087:
1088: final String hql = query;
1089: final HibernateCallback hcb = new HibernateCallback() {
1090: public Object doInHibernate(Session session)
1091: throws HibernateException, SQLException {
1092: Query q = session.createQuery(hql);
1093: q.setString(0, siteAgentId);
1094: return q.list();
1095: };
1096: };
1097: List l = getHibernateTemplate().executeFind(hcb);
1098:
1099: // List l = getHibernateTemplate().find(query,
1100: // new Object[] {siteAgentId},
1101: // new org.hibernate.type.Type[] {Hibernate.STRING});
1102:
1103: // we will filter the one that is past duedate & retract date
1104: ArrayList list = new ArrayList();
1105: for (int j = 0; j < l.size(); j++) {
1106: PublishedAssessmentData p = (PublishedAssessmentData) l
1107: .get(j);
1108: if ((p.getDueDate() == null || (p.getDueDate())
1109: .after(currentDate))
1110: && (p.getRetractDate() == null || (p
1111: .getRetractDate()).after(currentDate))) {
1112: list.add(p);
1113: }
1114: }
1115:
1116: ArrayList pubList = new ArrayList();
1117: for (int i = 0; i < list.size(); i++) {
1118: PublishedAssessmentData p = (PublishedAssessmentData) list
1119: .get(i);
1120: PublishedAssessmentFacade f = new PublishedAssessmentFacade(
1121: p.getPublishedAssessmentId(), p.getTitle(), p
1122: .getReleaseTo(), p.getStartDate(), p
1123: .getDueDate());
1124: pubList.add(f);
1125: }
1126: return pubList;
1127: }
1128:
1129: /**
1130: * According to Marc inactive means either the dueDate or the retractDate
1131: * has passed for 1.5 release (IM on 12/17/04)
1132: *
1133: * @param sortString
1134: * @return
1135: */
1136: public ArrayList getBasicInfoOfAllInActivePublishedAssessments(
1137: String sortString, final String siteAgentId,
1138: boolean ascending) {
1139: String orderBy = getOrderBy(sortString);
1140: String query = "select new PublishedAssessmentData(p.publishedAssessmentId, p.title,"
1141: + " c.releaseTo, c.startDate, c.dueDate, c.retractDate) from PublishedAssessmentData p,"
1142: + " PublishedAccessControl c, AuthorizationData z "
1143: + " where c.assessment=p and (p.status=0 or c.dueDate<= ? or c.retractDate<= ?)"
1144: + " and p.publishedAssessmentId=z.qualifierId and z.functionId='OWN_PUBLISHED_ASSESSMENT' "
1145: + " and z.agentIdString= ? order by p." + orderBy;
1146:
1147: if (ascending)
1148: query += " asc";
1149: else
1150: query += " desc";
1151:
1152: final String hql = query;
1153: final HibernateCallback hcb = new HibernateCallback() {
1154: public Object doInHibernate(Session session)
1155: throws HibernateException, SQLException {
1156: Query q = session.createQuery(hql);
1157: q.setTimestamp(0, new Date());
1158: q.setTimestamp(1, new Date());
1159: q.setString(2, siteAgentId);
1160: return q.list();
1161: };
1162: };
1163: List list = getHibernateTemplate().executeFind(hcb);
1164:
1165: // List list = getHibernateTemplate().find(query,
1166: // new Object[] {new Date(), new Date(),siteAgentId} ,
1167: // new org.hibernate.type.Type[] {Hibernate.TIMESTAMP,
1168: // Hibernate.TIMESTAMP,
1169: // Hibernate.STRING});
1170:
1171: ArrayList pubList = new ArrayList();
1172: for (int i = 0; i < list.size(); i++) {
1173: PublishedAssessmentData p = (PublishedAssessmentData) list
1174: .get(i);
1175: PublishedAssessmentFacade f = new PublishedAssessmentFacade(
1176: p.getPublishedAssessmentId(), p.getTitle(), p
1177: .getReleaseTo(), p.getStartDate(), p
1178: .getDueDate());
1179: pubList.add(f);
1180: }
1181: return pubList;
1182: }
1183:
1184: /**
1185: * return a set of PublishedSectionData IMPORTANT: 1. we have declared
1186: * SectionData as lazy loading, so we need to initialize it using
1187: * getHibernateTemplate().initialize(java.lang.Object). Unfortunately, we
1188: * are using Spring 1.0.2 which does not support this Hibernate feature. I
1189: * tried upgrading Spring to 1.1.3. Then it failed to load all the OR maps
1190: * correctly. So for now, I am just going to initialize it myself. I will
1191: * take a look at it again next year. - daisyf (12/13/04)
1192: */
1193: public HashSet getSectionSetForAssessment(
1194: PublishedAssessmentIfc assessment) {
1195: List sectionList = getHibernateTemplate().find(
1196: "from PublishedSectionData s where s.assessment.publishedAssessmentId="
1197: + assessment.getPublishedAssessmentId());
1198: HashSet set = new HashSet();
1199: for (int j = 0; j < sectionList.size(); j++) {
1200: set.add((PublishedSectionData) sectionList.get(j));
1201: }
1202: return set;
1203: }
1204:
1205: // IMPORTANT:
1206: // 1. we do not want any Section info, so set loadSection to false
1207: // 2. We have also declared SectionData as lazy loading. If loadSection is
1208: // set
1209: // to true, we will see null pointer
1210: public PublishedAssessmentFacade getSettingsOfPublishedAssessment(
1211: Long assessmentId) {
1212: PublishedAssessmentData a = loadPublishedAssessment(assessmentId);
1213: Boolean loadSection = Boolean.FALSE;
1214: PublishedAssessmentFacade f = new PublishedAssessmentFacade(a,
1215: loadSection);
1216: return f;
1217: }
1218:
1219: public PublishedItemData loadPublishedItem(Long itemId) {
1220: return (PublishedItemData) getHibernateTemplate().load(
1221: PublishedItemData.class, itemId);
1222: }
1223:
1224: public PublishedItemText loadPublishedItemText(Long itemTextId) {
1225: return (PublishedItemText) getHibernateTemplate().load(
1226: PublishedItemText.class, itemTextId);
1227: }
1228:
1229: // added by daisy - please check the logic - I based this on the
1230: // getBasicInfoOfAllActiveAssessment
1231: public ArrayList getBasicInfoOfAllPublishedAssessments(
1232: String orderBy, boolean ascending, final Integer status,
1233: final String siteId) {
1234:
1235: String query = "select new PublishedAssessmentData(p.publishedAssessmentId, p.title, "
1236: + " c.releaseTo, c.startDate, c.dueDate, c.retractDate, "
1237: + " c.feedbackDate, f.feedbackDelivery, f.feedbackAuthoring, c.lateHandling, "
1238: + " c.unlimitedSubmissions, c.submissionsAllowed) "
1239: + " from PublishedAssessmentData as p, PublishedAccessControl as c,"
1240: + " PublishedFeedback as f, AuthorizationData as az"
1241: + " where c.assessment.publishedAssessmentId=p.publishedAssessmentId "
1242: + " and p.publishedAssessmentId = f.assessment.publishedAssessmentId "
1243: + " and p.status=? and az.agentIdString=? "
1244: + " and az.functionId='TAKE_PUBLISHED_ASSESSMENT' and az.qualifierId=p.publishedAssessmentId"
1245: + " order by ";
1246:
1247: if (ascending == false) {
1248:
1249: if (orderBy.equals(DUE)) {
1250: query += " c." + orderBy + " desc";
1251: } else {
1252: query += " p." + orderBy + " desc";
1253: }
1254: } else {
1255: if (orderBy.equals(DUE)) {
1256: query += " c." + orderBy + " asc";
1257: } else {
1258: query += " p." + orderBy + " asc";
1259: }
1260: }
1261:
1262: final String hql = query;
1263: final HibernateCallback hcb = new HibernateCallback() {
1264: public Object doInHibernate(Session session)
1265: throws HibernateException, SQLException {
1266: Query q = session.createQuery(hql);
1267: q.setInteger(0, status.intValue());
1268: q.setString(1, siteId);
1269: return q.list();
1270: };
1271: };
1272: List list = getHibernateTemplate().executeFind(hcb);
1273: ArrayList pubList = new ArrayList();
1274: for (int i = 0; i < list.size(); i++) {
1275: PublishedAssessmentData p = (PublishedAssessmentData) list
1276: .get(i);
1277: PublishedAssessmentFacade f = new PublishedAssessmentFacade(
1278: p.getPublishedAssessmentId(), p.getTitle(), p
1279: .getReleaseTo(), p.getStartDate(), p
1280: .getDueDate(), p.getRetractDate(), p
1281: .getFeedbackDate(),
1282: p.getFeedbackDelivery(), p.getFeedbackAuthoring(),
1283: p.getLateHandling(), p.getUnlimitedSubmissions(), p
1284: .getSubmissionsAllowed());
1285: pubList.add(f);
1286: }
1287: return pubList;
1288: }
1289:
1290: /**
1291: * return an array list of the last AssessmentGradingFacade per assessment
1292: * that a user has submitted for grade.
1293: *
1294: * @param agentId
1295: * @param orderBy
1296: * @param ascending
1297: * @return
1298: */
1299: public ArrayList getBasicInfoOfLastSubmittedAssessments(
1300: final String agentId, String orderBy, boolean ascending) {
1301: // 1. get total no. of submission per assessment by the given agent
1302: // HashMap h = getTotalSubmissionPerAssessment(agentId);
1303: final String query = "select new AssessmentGradingData("
1304: + " a.assessmentGradingId, p.publishedAssessmentId, p.title, a.agentId,"
1305: + " a.submittedDate, a.isLate,"
1306: + " a.forGrade, a.totalAutoScore, a.totalOverrideScore,a.finalScore,"
1307: + " a.comments, a.status, a.gradedBy, a.gradedDate, a.attemptDate,"
1308: + " a.timeElapsed) "
1309: + " from AssessmentGradingData a, PublishedAssessmentData p"
1310: + " where a.publishedAssessmentId = p.publishedAssessmentId and a.forGrade=1 and a.agentId=?"
1311: + " order by p.publishedAssessmentId DESC, a.submittedDate DESC";
1312:
1313: /*
1314: * The sorting for each type will be done in the action listener. if
1315: * (orderBy.equals(TITLE)) { query += ", p." + orderBy; } else if
1316: * (!orderBy.equals(SUBMITTED)) { query += ", a." + orderBy; } if
1317: * (!orderBy.equals(SUBMITTED)) { if (ascending == false) { query += "
1318: * desc"; } else { query += " asc"; } }
1319: */
1320:
1321: final HibernateCallback hcb = new HibernateCallback() {
1322: public Object doInHibernate(Session session)
1323: throws HibernateException, SQLException {
1324: Query q = session.createQuery(query);
1325: q.setString(0, agentId);
1326: return q.list();
1327: };
1328: };
1329: List list = getHibernateTemplate().executeFind(hcb);
1330:
1331: // ArrayList list = (ArrayList) getHibernateTemplate().find(query,
1332: // new Object[] {agentId}
1333: // ,
1334: // new org.hibernate.type.Type[] {Hibernate.STRING});
1335:
1336: ArrayList assessmentList = new ArrayList();
1337: Long current = new Long("0");
1338: // Date currentDate = new Date();
1339: for (int i = 0; i < list.size(); i++) {
1340: AssessmentGradingData a = (AssessmentGradingData) list
1341: .get(i);
1342: // criteria: only want the most recently submitted assessment from a
1343: // given user.
1344: if (!a.getPublishedAssessmentId().equals(current)) {
1345: current = a.getPublishedAssessmentId();
1346: AssessmentGradingFacade f = new AssessmentGradingFacade(
1347: a);
1348: assessmentList.add(f);
1349: }
1350: }
1351: return assessmentList;
1352: }
1353:
1354: /**
1355: * total submitted for grade returns HashMap (Long publishedAssessmentId,
1356: * Integer totalSubmittedForGrade);
1357: */
1358: public HashMap getTotalSubmissionPerAssessment(String agentId) {
1359: List l = getNumberOfSubmissionsOfAllAssessmentsByAgent(agentId);
1360: HashMap h = new HashMap();
1361: for (int i = 0; i < l.size(); i++) {
1362: AssessmentGradingData d = (AssessmentGradingData) l.get(i);
1363: h.put(d.getPublishedAssessmentId(), new Integer(d
1364: .getTotalSubmitted()));
1365: log.debug("pId=" + d.getPublishedAssessmentId()
1366: + " submitted=" + d.getTotalSubmitted());
1367: }
1368: return h;
1369: }
1370:
1371: public HashMap getTotalSubmissionPerAssessment(String agentId,
1372: String siteId) {
1373: List l = getNumberOfSubmissionsOfAllAssessmentsByAgent(agentId,
1374: siteId);
1375: HashMap h = new HashMap();
1376: for (int i = 0; i < l.size(); i++) {
1377: AssessmentGradingData d = (AssessmentGradingData) l.get(i);
1378: h.put(d.getPublishedAssessmentId(), new Integer(d
1379: .getTotalSubmitted()));
1380: log.debug("pId=" + d.getPublishedAssessmentId()
1381: + " submitted=" + d.getTotalSubmitted());
1382: }
1383: return h;
1384: }
1385:
1386: public Integer getTotalSubmission(final String agentId,
1387: final Long publishedAssessmentId) {
1388: final String query = "select count(a) from AssessmentGradingData a where a.forGrade=1 "
1389: + " and a.agentId=? and a.publishedAssessmentId=?";
1390:
1391: final HibernateCallback hcb = new HibernateCallback() {
1392: public Object doInHibernate(Session session)
1393: throws HibernateException, SQLException {
1394: Query q = session.createQuery(query);
1395: q.setString(0, agentId);
1396: q.setLong(1, publishedAssessmentId.longValue());
1397: return q.list();
1398: };
1399: };
1400: List l = getHibernateTemplate().executeFind(hcb);
1401:
1402: // List l = getHibernateTemplate().find(query,
1403: // new Object[] {agentId,
1404: // publishedAssessmentId}
1405: // ,
1406: // new org.hibernate.type.Type[] {
1407: // Hibernate.STRING, Hibernate.LONG});
1408: return (Integer) l.get(0);
1409: }
1410:
1411: public PublishedAssessmentFacade getPublishedAssessmentIdByAlias(
1412: String alias) {
1413: return getPublishedAssessmentIdByMetaLabel("ALIAS", alias);
1414: }
1415:
1416: public PublishedAssessmentFacade getPublishedAssessmentIdByMetaLabel(
1417: final String label, final String entry) {
1418: final String query = "select p "
1419: + " from PublishedAssessmentData p, "
1420: + " PublishedMetaData m where p=m.assessment "
1421: + " and m.label=? and m.entry=?";
1422:
1423: final HibernateCallback hcb = new HibernateCallback() {
1424: public Object doInHibernate(Session session)
1425: throws HibernateException, SQLException {
1426: Query q = session.createQuery(query);
1427: q.setString(0, label);
1428: q.setString(1, entry);
1429: return q.list();
1430: };
1431: };
1432: List l = getHibernateTemplate().executeFind(hcb);
1433:
1434: // List l = getHibernateTemplate().find(query,
1435: // new Object[] {label, entry}
1436: // ,
1437: // new org.hibernate.type.Type[] {
1438: // Hibernate.STRING, Hibernate.STRING});
1439: if (l.size() > 0) {
1440: PublishedAssessmentData p = (PublishedAssessmentData) l
1441: .get(0);
1442: p.setSectionSet(getSectionSetForAssessment(p));
1443: PublishedAssessmentFacade f = new PublishedAssessmentFacade(
1444: p);
1445: return f;
1446: } else {
1447: return null;
1448: }
1449: }
1450:
1451: public void saveOrUpdateMetaData(PublishedMetaData meta) {
1452: int retryCount = PersistenceService.getInstance()
1453: .getRetryCount().intValue();
1454: while (retryCount > 0) {
1455: try {
1456: getHibernateTemplate().saveOrUpdate(meta);
1457: retryCount = 0;
1458: } catch (Exception e) {
1459: log.warn("problem save or update meta data: "
1460: + e.getMessage());
1461: retryCount = PersistenceService.getInstance()
1462: .retryDeadlock(e, retryCount);
1463: }
1464: }
1465: }
1466:
1467: public HashMap getFeedbackHash() {
1468: HashMap h = new HashMap();
1469: String query = "select new PublishedFeedback("
1470: + " p.assessment.publishedAssessmentId,"
1471: + " p.feedbackDelivery, p.feedbackAuthoring, p.editComponents, p.showQuestionText,"
1472: + " p.showStudentResponse, p.showCorrectResponse,"
1473: + " p.showStudentScore,"
1474: + " p.showStudentQuestionScore,"
1475: + " p.showQuestionLevelFeedback, p.showSelectionLevelFeedback,"
1476: + " p.showGraderComments, p.showStatistics)"
1477: + " from PublishedFeedback p";
1478: List l = getHibernateTemplate().find(query);
1479: for (int i = 0; i < l.size(); i++) {
1480: PublishedFeedback f = (PublishedFeedback) l.get(i);
1481: h.put(f.getAssessmentId(), f);
1482: }
1483: return h;
1484: }
1485:
1486: /**
1487: * this return a HashMap containing (Long publishedAssessmentId,
1488: * PublishedAssessmentFacade publishedAssessment) Note that the
1489: * publishedAssessment is a partial object used for display only. do not use
1490: * it for persisting. It only contains title, releaseTo, startDate, dueDate &
1491: * retractDate
1492: */
1493: public HashMap getAllAssessmentsReleasedToAuthenticatedUsers() {
1494: HashMap h = new HashMap();
1495: String query = "select new PublishedAssessmentData(p.publishedAssessmentId, p.title, "
1496: + " c.releaseTo, c.startDate, c.dueDate, c.retractDate) "
1497: + " from PublishedAssessmentData p, PublishedAccessControl c "
1498: + " where c.assessment = p and c.releaseTo like '%Authenticated Users%'";
1499: List l = getHibernateTemplate().find(query);
1500: for (int i = 0; i < l.size(); i++) {
1501: PublishedAssessmentData p = (PublishedAssessmentData) l
1502: .get(i);
1503: h.put(p.getPublishedAssessmentId(),
1504: new PublishedAssessmentFacade(p));
1505: }
1506: return h;
1507: }
1508:
1509: public String getPublishedAssessmentOwner(
1510: String publishedAssessmentId) {
1511: // HashMap h = new HashMap();
1512: String query = "select a from AuthorizationData a where "
1513: + " a.functionId='OWN_PUBLISHED_ASSESSMENT' and a.qualifierId="
1514: + publishedAssessmentId;
1515: List l = getHibernateTemplate().find(query);
1516: if (l.size() > 0) {
1517: AuthorizationData a = (AuthorizationData) l.get(0);
1518: return a.getAgentIdString();
1519: } else
1520: return null;
1521: }
1522:
1523: public boolean publishedAssessmentTitleIsUnique(
1524: final Long assessmentBaseId, final String title) {
1525: final String currentSiteId = AgentFacade.getCurrentSiteId();
1526: // String agentString = AgentFacade.getAgentString();
1527: // List list;
1528: boolean isUnique = true;
1529: final String query = "select new PublishedAssessmentData(a.publishedAssessmentId, a.title, a.lastModifiedDate)"
1530: + " from PublishedAssessmentData a, AuthorizationData z where "
1531: + " a.title=? and a.publishedAssessmentId!=? and a.status!=2 and "
1532: + " z.functionId='OWN_PUBLISHED_ASSESSMENT' and "
1533: + " a.publishedAssessmentId=z.qualifierId and z.agentIdString=?";
1534:
1535: final HibernateCallback hcb = new HibernateCallback() {
1536: public Object doInHibernate(Session session)
1537: throws HibernateException, SQLException {
1538: Query q = session.createQuery(query);
1539: q.setString(0, title);
1540: q.setLong(1, assessmentBaseId.longValue());
1541: q.setString(2, currentSiteId);
1542: return q.list();
1543: };
1544: };
1545: List list = getHibernateTemplate().executeFind(hcb);
1546:
1547: // List list = getHibernateTemplate().find(query,
1548: // new Object[]{title,assessmentBaseId,currentSiteId},
1549: // new org.hibernate.type.Type[] {Hibernate.STRING, Hibernate.LONG,
1550: // Hibernate.STRING});
1551: if (list.size() > 0)
1552: isUnique = false;
1553: return isUnique;
1554: }
1555:
1556: public boolean hasRandomPart(final Long publishedAssessmentId) {
1557: boolean hasRandomPart = false;
1558: final String key = SectionDataIfc.AUTHOR_TYPE;
1559: final String value = SectionDataIfc.RANDOM_DRAW_FROM_QUESTIONPOOL
1560: .toString();
1561: final String query = "select s from PublishedSectionData s, PublishedSectionMetaData m where "
1562: + " s = m.section and s.assessment.publishedAssessmentId=? and "
1563: + " m.label=? and m.entry=?";
1564:
1565: final HibernateCallback hcb = new HibernateCallback() {
1566: public Object doInHibernate(Session session)
1567: throws HibernateException, SQLException {
1568: Query q = session.createQuery(query);
1569: q.setLong(0, publishedAssessmentId.longValue());
1570: q.setString(1, key);
1571: q.setString(2, value);
1572: return q.list();
1573: };
1574: };
1575: List l = getHibernateTemplate().executeFind(hcb);
1576:
1577: // List l = getHibernateTemplate().find(query,
1578: // new Object[]{ publishedAssessmentId, key, value},
1579: // new org.hibernate.type.Type[] {Hibernate.LONG, Hibernate.STRING,
1580: // Hibernate.STRING});
1581: if (l.size() > 0)
1582: hasRandomPart = true;
1583: return hasRandomPart;
1584: }
1585:
1586: public PublishedItemData getFirstPublishedItem(
1587: final Long publishedAssessmentId) {
1588: final String query = "select i from PublishedAssessmentData p, PublishedSectionData s, "
1589: + " PublishedItemData i where p.publishedAssessmentId=? and"
1590: + " p.publishedAssessmentId=s.assessment.publishedAssessmentId and "
1591: + " s=i.section";
1592:
1593: final HibernateCallback hcb = new HibernateCallback() {
1594: public Object doInHibernate(Session session)
1595: throws HibernateException, SQLException {
1596: Query q = session.createQuery(query);
1597: q.setLong(0, publishedAssessmentId.longValue());
1598: return q.list();
1599: };
1600: };
1601: List l = getHibernateTemplate().executeFind(hcb);
1602:
1603: //final String key = SectionDataIfc.AUTHOR_TYPE;
1604: //final String value = SectionDataIfc.QUESTIONS_AUTHORED_ONE_BY_ONE.toString();
1605: final String query2 = "select s from PublishedAssessmentData p, PublishedSectionData s, PublishedSectionMetaData m "
1606: + " where p.publishedAssessmentId=? and s = m.section and "
1607: + " p.publishedAssessmentId=s.assessment.publishedAssessmentId ";
1608:
1609: final HibernateCallback hcb2 = new HibernateCallback() {
1610: public Object doInHibernate(Session session)
1611: throws HibernateException, SQLException {
1612: Query q = session.createQuery(query2);
1613: q.setLong(0, publishedAssessmentId.longValue());
1614: //q.setString(1, key);
1615: //q.setString(2, value);
1616: return q.list();
1617: };
1618: };
1619: List sec = getHibernateTemplate().executeFind(hcb2);
1620:
1621: PublishedItemData returnItem = null;
1622: if (sec.size() > 0 && l.size() > 0) {
1623: Collections.sort(sec, new SecComparator());
1624: for (int i = 0; i < sec.size(); i++) {
1625: PublishedSectionData this Sec = (PublishedSectionData) sec
1626: .get(i);
1627: ArrayList itemList = new ArrayList();
1628: for (int j = 0; j < l.size(); j++) {
1629: PublishedItemData compItem = (PublishedItemData) l
1630: .get(j);
1631: if (compItem.getSection().getSectionId().equals(
1632: this Sec.getSectionId())) {
1633: itemList.add(compItem);
1634: }
1635: }
1636: if (itemList.size() > 0) {
1637: Collections.sort(itemList, new ItemComparator());
1638: returnItem = (PublishedItemData) itemList.get(0);
1639: break;
1640: }
1641: }
1642: }
1643: return returnItem;
1644: }
1645:
1646: public List getPublishedItemIds(final Long publishedAssessmentId) {
1647: final HibernateCallback hcb = new HibernateCallback() {
1648: public Object doInHibernate(Session session)
1649: throws HibernateException, SQLException {
1650: Query q = session
1651: .createQuery("select i.itemId from PublishedItemData i, PublishedSectionData s, "
1652: + " PublishedAssessmentData p where p.publishedAssessmentId=? and "
1653: + " p = s.assessment and i.section = s");
1654: q.setLong(0, publishedAssessmentId.longValue());
1655: return q.list();
1656: };
1657: };
1658: return getHibernateTemplate().executeFind(hcb);
1659:
1660: // return getHibernateTemplate().find(
1661: // "select i.itemId from PublishedItemData i, PublishedSectionData s, "+
1662: // " PublishedAssessmentData p where p.publishedAssessmentId=? and "+
1663: // " p = s.assessment and i.section = s",
1664: // new Object[] { publishedAssessmentId },
1665: // new org.hibernate.type.Type[] { Hibernate.LONG });
1666: }
1667:
1668: public HashSet getPublishedItemSet(
1669: final Long publishedAssessmentId, final Long sectionId) {
1670: HashSet itemSet = new HashSet();
1671: final HibernateCallback hcb = new HibernateCallback() {
1672: public Object doInHibernate(Session session)
1673: throws HibernateException, SQLException {
1674: Query q = session
1675: .createQuery("select i from PublishedItemData i, PublishedSectionData s, "
1676: + " PublishedAssessmentData p where p.publishedAssessmentId=? "
1677: + " and i.section.id=? and p = s.assessment and i.section = s");
1678: q.setLong(0, publishedAssessmentId.longValue());
1679: q.setLong(1, sectionId.longValue());
1680: return q.list();
1681: };
1682: };
1683: List assessmentGradings = getHibernateTemplate().executeFind(
1684: hcb);
1685:
1686: Iterator iter = assessmentGradings.iterator();
1687: PublishedItemData publishedItemData;
1688: while (iter.hasNext()) {
1689: publishedItemData = (PublishedItemData) iter.next();
1690: log.debug("itemId = " + publishedItemData.getItemId());
1691: itemSet.add(publishedItemData);
1692: }
1693: return itemSet;
1694:
1695: }
1696:
1697: public Long getItemType(final Long publishedItemId) {
1698: final String query = "select p.typeId "
1699: + " from PublishedItemData p " + " where p.itemId=?";
1700:
1701: final HibernateCallback hcb = new HibernateCallback() {
1702: public Object doInHibernate(Session session)
1703: throws HibernateException, SQLException {
1704: Query q = session.createQuery(query);
1705: q.setLong(0, publishedItemId.longValue());
1706: return q.list();
1707: };
1708: };
1709: List list = getHibernateTemplate().executeFind(hcb);
1710:
1711: // List list = getHibernateTemplate().find(query,
1712: // new Object[] { publishedItemId },
1713: // new org.hibernate.type.Type[] { Hibernate.LONG });
1714: if (list.size() > 0)
1715: return (Long) list.get(0);
1716: else
1717: return null;
1718: }
1719:
1720: class SecComparator implements Comparator {
1721: public int compare(Object arg0, Object arg1) {
1722: return ((PublishedSectionData) arg0)
1723: .getSequence()
1724: .compareTo(
1725: ((PublishedSectionData) arg1).getSequence());
1726: }
1727: }
1728:
1729: class ItemComparator implements Comparator {
1730: public int compare(Object arg0, Object arg1) {
1731: return ((PublishedItemData) arg0).getSequence().compareTo(
1732: ((PublishedItemData) arg1).getSequence());
1733: }
1734: }
1735:
1736: public void delete(PublishedAssessmentIfc assessment) {
1737: PublishedAssessmentData data;
1738: if (assessment instanceof PublishedAssessmentFacade)
1739: data = (PublishedAssessmentData) ((PublishedAssessmentFacade) assessment)
1740: .getData();
1741: else
1742: data = (PublishedAssessmentData) assessment;
1743:
1744: int retryCount = PersistenceService.getInstance()
1745: .getRetryCount().intValue();
1746: while (retryCount > 0) {
1747: try {
1748: getHibernateTemplate().delete(data);
1749: retryCount = 0;
1750: } catch (Exception e) {
1751: log.warn("problem removing publishedAssessment: "
1752: + e.getMessage());
1753: retryCount = PersistenceService.getInstance()
1754: .retryDeadlock(e, retryCount);
1755: }
1756: }
1757: }
1758:
1759: public HashSet getSectionSetForAssessment(Long publishedAssessmentId) {
1760: List sectionList = getHibernateTemplate().find(
1761: "from PublishedSectionData s where s.assessment.publishedAssessmentId="
1762: + publishedAssessmentId);
1763: HashSet set = new HashSet();
1764: for (int j = 0; j < sectionList.size(); j++) {
1765: set.add((PublishedSectionData) sectionList.get(j));
1766: }
1767: return set;
1768: }
1769:
1770: private String replaceSpace(String tempString) {
1771: String newString = "";
1772: char[] oneChar = new char[1];
1773: for (int i = 0; i < tempString.length(); i++) {
1774: if (tempString.charAt(i) != ' ') {
1775: oneChar[0] = tempString.charAt(i);
1776: String concatString = new String(oneChar);
1777: newString = newString.concat(concatString);
1778: } else {
1779: newString = newString.concat("%20");
1780: }
1781: }
1782: return newString;
1783: }
1784:
1785: public boolean isRandomDrawPart(final Long publishedAssessmentId,
1786: final Long sectionId) {
1787: boolean isRandomDrawPart = false;
1788: final String key = SectionDataIfc.AUTHOR_TYPE;
1789: final String value = SectionDataIfc.RANDOM_DRAW_FROM_QUESTIONPOOL
1790: .toString();
1791: final String query = "select s from PublishedSectionData s, PublishedSectionMetaData m where "
1792: + " s = m.section and s.assessment.publishedAssessmentId=? and "
1793: + " s.id=? and m.label=? and m.entry=?";
1794:
1795: final HibernateCallback hcb = new HibernateCallback() {
1796: public Object doInHibernate(Session session)
1797: throws HibernateException, SQLException {
1798: Query q = session.createQuery(query);
1799: q.setLong(0, publishedAssessmentId.longValue());
1800: q.setLong(1, sectionId.longValue());
1801: q.setString(2, key);
1802: q.setString(3, value);
1803: return q.list();
1804: };
1805: };
1806: List l = getHibernateTemplate().executeFind(hcb);
1807:
1808: if (l.size() > 0)
1809: isRandomDrawPart = true;
1810: return isRandomDrawPart;
1811: }
1812:
1813: /**
1814: * return an array list of the AssessmentGradingFacade that a user has
1815: * submitted for grade. one per published assessment. If an assessment
1816: * allows multiple submissions and its grading option is to send highest,
1817: * then return only the submission with highest finalScore. If an assessment
1818: * allows multiple submissions and its grading option is to send last, then
1819: * return only the last submission.
1820: *
1821: * @param agentId
1822: * @param siteId
1823: * @return
1824: */
1825: public ArrayList getBasicInfoOfLastOrHighestSubmittedAssessmentsByScoringOption(
1826: final String agentId, final String siteId) {
1827: // Get total no. of submission per assessment by the given agent
1828: // sorted by submittedData DESC
1829: final String last_query = "select new AssessmentGradingData("
1830: + " a.assessmentGradingId, p.publishedAssessmentId, p.title, a.agentId,"
1831: + " a.submittedDate, a.isLate,"
1832: + " a.forGrade, a.totalAutoScore, a.totalOverrideScore,a.finalScore,"
1833: + " a.comments, a.status, a.gradedBy, a.gradedDate, a.attemptDate,"
1834: + " a.timeElapsed) "
1835: + " from AssessmentGradingData a, PublishedAssessmentData p, AuthorizationData az"
1836: + " where a.publishedAssessmentId = p.publishedAssessmentId"
1837: + " and a.forGrade=1 and a.agentId=? and az.agentIdString=? "
1838: + " and az.functionId='TAKE_PUBLISHED_ASSESSMENT' and az.qualifierId=p.publishedAssessmentId"
1839: + " order by p.publishedAssessmentId DESC, a.submittedDate DESC";
1840:
1841: // Get total no. of submission per assessment by the given agent
1842: // sorted by finalScore DESC
1843:
1844: final String highest_query = "select new AssessmentGradingData("
1845: + " a.assessmentGradingId, p.publishedAssessmentId, p.title, a.agentId,"
1846: + " a.submittedDate, a.isLate,"
1847: + " a.forGrade, a.totalAutoScore, a.totalOverrideScore,a.finalScore,"
1848: + " a.comments, a.status, a.gradedBy, a.gradedDate, a.attemptDate,"
1849: + " a.timeElapsed) "
1850: + " from AssessmentGradingData a, PublishedAssessmentData p, AuthorizationData az"
1851: + " where a.publishedAssessmentId = p.publishedAssessmentId"
1852: + " and a.forGrade=1 and a.agentId=? and az.agentIdString=? "
1853: + " and az.functionId='TAKE_PUBLISHED_ASSESSMENT' and az.qualifierId=p.publishedAssessmentId"
1854: + " order by p.publishedAssessmentId DESC, a.finalScore DESC, a.submittedDate DESC";
1855:
1856: final HibernateCallback hcb_last = new HibernateCallback() {
1857: public Object doInHibernate(Session session)
1858: throws HibernateException, SQLException {
1859: Query q = session.createQuery(last_query);
1860: q.setString(0, agentId);
1861: q.setString(1, siteId);
1862: return q.list();
1863: };
1864: };
1865:
1866: // this list is sorted by submittedDate desc.
1867: List last_list = getHibernateTemplate().executeFind(hcb_last);
1868:
1869: final HibernateCallback hcb_highest = new HibernateCallback() {
1870: public Object doInHibernate(Session session)
1871: throws HibernateException, SQLException {
1872: Query q = session.createQuery(highest_query);
1873: q.setString(0, agentId);
1874: q.setString(1, siteId);
1875: return q.list();
1876: };
1877: };
1878:
1879: // this list is sorted by finalScore desc.
1880:
1881: List highest_list = getHibernateTemplate().executeFind(
1882: hcb_highest);
1883:
1884: // The sorting for each column will be done in the action listener.
1885:
1886: ArrayList assessmentList = new ArrayList();
1887: Long currentid = new Long("0");
1888: Integer scoringOption = EvaluationModelIfc.LAST_SCORE; // use Last as
1889: boolean multiSubmissionAllowed = false;
1890:
1891: // now go through the last_list, and get the first entry in the list for
1892: // each publishedAssessment, if
1893: // not
1894:
1895: for (int i = 0; i < last_list.size(); i++) {
1896: AssessmentGradingData a = (AssessmentGradingData) last_list
1897: .get(i);
1898:
1899: // get the scoring option
1900: PublishedAssessmentFacade paf = getPublishedAssessment(a
1901: .getPublishedAssessmentId());
1902: multiSubmissionAllowed = false;
1903: if (paf != null) {
1904: scoringOption = paf.getEvaluationModel()
1905: .getScoringType();
1906: AssessmentAccessControlIfc ac = paf
1907: .getAssessmentAccessControl();
1908:
1909: if (ac.getSubmissionsAllowed() != null) {
1910: if (ac.getSubmissionsAllowed().intValue() == 1) {
1911: scoringOption = EvaluationModelIfc.LAST_SCORE;
1912: }
1913: }
1914: /*
1915: if (ac.getSubmissionsAllowed().intValue() > 1) {
1916: multiSubmissionAllowed = true;
1917: } else {
1918: multiSubmissionAllowed = false;
1919: // When number of submission allowed is 1, always display the last one, that is, order by last
1920: scoringOption = EvaluationModelIfc.LAST_SCORE;
1921: }
1922: } else {
1923: multiSubmissionAllowed = true;
1924: }
1925: */
1926:
1927: }
1928:
1929: if (EvaluationModelIfc.LAST_SCORE.equals(scoringOption)
1930: && !a.getPublishedAssessmentId().equals(currentid)) {
1931: currentid = a.getPublishedAssessmentId();
1932: AssessmentGradingFacade f = new AssessmentGradingFacade(
1933: a);
1934: assessmentList.add(f);
1935: }
1936:
1937: }
1938:
1939: // now go through the highest_list ,and get the first entry in the list
1940: // for each publishedAssessment.
1941:
1942: for (int i = 0; i < highest_list.size(); i++) {
1943: AssessmentGradingData a = (AssessmentGradingData) highest_list
1944: .get(i);
1945:
1946: // get the scoring option
1947: PublishedAssessmentFacade paf = getPublishedAssessment(a
1948: .getPublishedAssessmentId());
1949: multiSubmissionAllowed = false;
1950: if (paf != null) {
1951: scoringOption = paf.getEvaluationModel()
1952: .getScoringType();
1953: AssessmentAccessControlIfc ac = paf
1954: .getAssessmentAccessControl();
1955:
1956: if (ac.getSubmissionsAllowed() != null) {
1957: if (ac.getSubmissionsAllowed().intValue() > 1) {
1958: multiSubmissionAllowed = true;
1959: } else {
1960: multiSubmissionAllowed = false;
1961: }
1962: } else {
1963: multiSubmissionAllowed = true;
1964: }
1965:
1966: }
1967: if ((multiSubmissionAllowed)
1968: && (EvaluationModelIfc.HIGHEST_SCORE
1969: .equals(scoringOption))
1970: && (!a.getPublishedAssessmentId().equals(currentid))) {
1971: currentid = a.getPublishedAssessmentId();
1972: AssessmentGradingFacade f = new AssessmentGradingFacade(
1973: a);
1974: assessmentList.add(f);
1975: }
1976:
1977: }
1978:
1979: return assessmentList;
1980: }
1981:
1982: public PublishedAssessmentData getBasicInfoOfPublishedAssessment(
1983: final Long publishedId) {
1984: String query = "select new PublishedAssessmentData(p.publishedAssessmentId, p.title, "
1985: + " c.releaseTo, c.startDate, c.dueDate, c.retractDate, "
1986: + " c.feedbackDate, f.feedbackDelivery, f.feedbackAuthoring, c.lateHandling, "
1987: + " c.unlimitedSubmissions, c.submissionsAllowed) "
1988: + " from PublishedAssessmentData as p, PublishedAccessControl as c,"
1989: + " PublishedFeedback as f"
1990: + " where c.assessment.publishedAssessmentId=p.publishedAssessmentId "
1991: + " and p.publishedAssessmentId = f.assessment.publishedAssessmentId "
1992: + " and p.publishedAssessmentId=?";
1993: final String hql = query;
1994: final HibernateCallback hcb = new HibernateCallback() {
1995: public Object doInHibernate(Session session)
1996: throws HibernateException, SQLException {
1997: Query q = session.createQuery(hql);
1998: q.setLong(0, publishedId.longValue());
1999: return q.list();
2000: };
2001: };
2002: List list = getHibernateTemplate().executeFind(hcb);
2003: return (PublishedAssessmentData) list.get(0);
2004: }
2005:
2006: public String getPublishedAssessmentSiteId(
2007: String publishedAssessmentId) {
2008: String query = "select a from AuthorizationData a "
2009: + "where a.functionId = 'TAKE_PUBLISHED_ASSESSMENT' and "
2010: + "a.qualifierId = " + publishedAssessmentId;
2011:
2012: List l = getHibernateTemplate().find(query);
2013: if (l.size() > 0) {
2014: AuthorizationData a = (AuthorizationData) l.get(0);
2015: return a.getAgentIdString();
2016: } else
2017: return null;
2018: }
2019: }
|