001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/tags/sakai_2-4-1/samigo-hibernate/src/java/org/sakaiproject/tool/assessment/data/dao/assessment/PublishedItemData.java $
003: * $Id: PublishedItemData.java 16922 2006-10-09 21:56:09Z ktsao@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.data.dao.assessment;
021:
022: import java.io.IOException;
023: import java.util.ArrayList;
024: import java.util.Collections;
025: import java.util.Date;
026: import java.util.HashMap;
027: import java.util.HashSet;
028: import java.util.Iterator;
029: import java.util.List;
030: import java.util.Set;
031:
032: import org.apache.log4j.Category;
033: import org.sakaiproject.tool.assessment.data.dao.grading.ItemGradingData;
034: import org.sakaiproject.tool.assessment.data.dao.shared.TypeD;
035: import org.sakaiproject.tool.assessment.data.ifc.assessment.AnswerIfc;
036: import org.sakaiproject.tool.assessment.data.ifc.assessment.ItemAttachmentIfc;
037: import org.sakaiproject.tool.assessment.data.ifc.assessment.ItemDataIfc;
038: import org.sakaiproject.tool.assessment.data.ifc.assessment.ItemTextIfc;
039: import org.sakaiproject.tool.assessment.data.ifc.assessment.SectionDataIfc;
040: import org.sakaiproject.tool.assessment.data.ifc.shared.TypeIfc;
041:
042: //import org.sakaiproject.tool.assessment.facade.AgentFacade;
043: //import org.sakaiproject.tool.assessment.facade.TypeFacadeQueriesAPI;
044: //import org.sakaiproject.tool.assessment.services.GradingService;
045: //import org.sakaiproject.tool.assessment.services.PersistenceService;
046:
047: public class PublishedItemData implements java.io.Serializable,
048: ItemDataIfc, Comparable {
049: static Category errorLogger = Category.getInstance("errorLogger");
050:
051: private static final long serialVersionUID = 7526471155622776147L;
052:
053: private Long itemId;
054: private String itemIdString;
055: private SectionDataIfc section;
056: private Integer sequence;
057: private Integer duration;
058: private Integer triesAllowed;
059: private String instruction;
060: private String description;
061: private Long typeId;
062: private String grade;
063: private Float score;
064: private String hint;
065: private Boolean hasRationale;
066: private Integer status;
067: private String createdBy;
068: private Date createdDate;
069: private String lastModifiedBy;
070: private Date lastModifiedDate;
071: private Set itemTextSet;
072: private Set itemMetaDataSet;
073: private Set itemFeedbackSet;
074: private HashMap itemMetaDataMap = new HashMap();
075: private HashMap itemFeedbackMap = new HashMap();
076: private ItemGradingData lastItemGradingDataByAgent;
077: private Set itemAttachmentSet;
078:
079: public PublishedItemData() {
080: }
081:
082: // this constructor should be deprecated, it is missing triesAllowed
083: public PublishedItemData(SectionDataIfc section, Integer sequence,
084: Integer duration, String instruction, String description,
085: Long typeId, String grade, Float score, String hint,
086: Boolean hasRationale, Integer status, String createdBy,
087: Date createdDate, String lastModifiedBy,
088: Date lastModifiedDate, Set itemTextSet,
089: Set itemMetaDataSet, Set itemFeedbackSet) {
090: this .section = section;
091: this .sequence = sequence;
092: this .duration = duration;
093: this .instruction = instruction;
094: this .description = description;
095: this .typeId = typeId;
096: this .grade = grade;
097: this .score = score;
098: this .hint = hint;
099: this .hasRationale = hasRationale;
100: this .status = status;
101: this .createdBy = createdBy;
102: this .createdDate = createdDate;
103: this .lastModifiedBy = lastModifiedBy;
104: this .lastModifiedDate = lastModifiedDate;
105: this .itemTextSet = itemTextSet;
106: this .itemMetaDataSet = itemMetaDataSet;
107: this .itemFeedbackSet = itemFeedbackSet;
108: }
109:
110: public PublishedItemData(SectionDataIfc section, Integer sequence,
111: Integer duration, String instruction, String description,
112: Long typeId, String grade, Float score, String hint,
113: Boolean hasRationale, Integer status, String createdBy,
114: Date createdDate, String lastModifiedBy,
115: Date lastModifiedDate, Set itemTextSet,
116: Set itemMetaDataSet, Set itemFeedbackSet,
117: Integer triesAllowed) {
118: this .section = section;
119: this .sequence = sequence;
120: this .duration = duration;
121: this .instruction = instruction;
122: this .description = description;
123: this .typeId = typeId;
124: this .grade = grade;
125: this .score = score;
126: this .hint = hint;
127: this .hasRationale = hasRationale;
128: this .status = status;
129: this .createdBy = createdBy;
130: this .createdDate = createdDate;
131: this .lastModifiedBy = lastModifiedBy;
132: this .lastModifiedDate = lastModifiedDate;
133: this .itemTextSet = itemTextSet;
134: this .itemMetaDataSet = itemMetaDataSet;
135: this .itemFeedbackSet = itemFeedbackSet;
136: this .triesAllowed = triesAllowed;
137: }
138:
139: public Long getItemId() {
140: return this .itemId;
141: }
142:
143: public void setItemId(Long itemId) {
144: this .itemId = itemId;
145: setItemIdString(itemId.toString());
146: }
147:
148: public String getItemIdString() {
149: return this .itemIdString;
150: }
151:
152: public void setItemIdString(String itemIdString) {
153: this .itemIdString = itemIdString;
154: }
155:
156: public SectionDataIfc getSection() {
157: return this .section;
158: }
159:
160: public void setSection(SectionDataIfc section) {
161: this .section = section;
162: }
163:
164: public Integer getDuration() {
165: return this .duration;
166: }
167:
168: public void setDuration(Integer duration) {
169: this .duration = duration;
170: }
171:
172: public Integer getSequence() {
173: return this .sequence;
174: }
175:
176: public void setSequence(Integer sequence) {
177: this .sequence = sequence;
178: }
179:
180: public String getInstruction() {
181: return this .instruction;
182: }
183:
184: public void setInstruction(String instruction) {
185: this .instruction = instruction;
186: }
187:
188: public String getDescription() {
189: return this .description;
190: }
191:
192: public void setDescription(String description) {
193: this .description = description;
194: }
195:
196: public Long getTypeId() {
197: return this .typeId;
198: }
199:
200: public void setTypeId(Long typeId) {
201: this .typeId = typeId;
202: }
203:
204: public String getGrade() {
205: return this .grade;
206: }
207:
208: public void setGrade(String grade) {
209: this .grade = grade;
210: }
211:
212: public Float getScore() {
213: return this .score;
214: }
215:
216: public void setScore(Float score) {
217: this .score = score;
218: }
219:
220: public String getHint() {
221: return this .hint;
222: }
223:
224: public void setHint(String hint) {
225: this .hint = hint;
226: }
227:
228: public Boolean getHasRationale() {
229: return this .hasRationale;
230: }
231:
232: public void setHasRationale(Boolean hasRationale) {
233: this .hasRationale = hasRationale;
234: }
235:
236: public Integer getStatus() {
237: return this .status;
238: }
239:
240: public void setStatus(Integer status) {
241: this .status = status;
242: }
243:
244: public String getCreatedBy() {
245: return this .createdBy;
246: }
247:
248: public void setCreatedBy(String createdBy) {
249: this .createdBy = createdBy;
250: }
251:
252: public Date getCreatedDate() {
253: return this .createdDate;
254: }
255:
256: public void setCreatedDate(Date createdDate) {
257: this .createdDate = createdDate;
258: }
259:
260: public String getLastModifiedBy() {
261: return this .lastModifiedBy;
262: }
263:
264: public void setLastModifiedBy(String lastModifiedBy) {
265: this .lastModifiedBy = lastModifiedBy;
266: }
267:
268: public Date getLastModifiedDate() {
269: return this .lastModifiedDate;
270: }
271:
272: public void setLastModifiedDate(Date lastModifiedDate) {
273: this .lastModifiedDate = lastModifiedDate;
274: }
275:
276: public Set getItemTextSet() {
277: return itemTextSet;
278: }
279:
280: public void setItemTextSet(Set itemTextSet) {
281: this .itemTextSet = itemTextSet;
282: }
283:
284: public Set getItemMetaDataSet() {
285: return itemMetaDataSet;
286: }
287:
288: public void setItemMetaDataSet(Set itemMetaDataSet) {
289: this .itemMetaDataSet = itemMetaDataSet;
290: this .itemMetaDataMap = getItemMetaDataMap(itemMetaDataSet);
291: }
292:
293: public HashMap getItemMetaDataMap(Set itemMetaDataSet) {
294: HashMap itemMetaDataMap = new HashMap();
295: if (itemMetaDataSet != null) {
296: for (Iterator i = itemMetaDataSet.iterator(); i.hasNext();) {
297: PublishedItemMetaData itemMetaData = (PublishedItemMetaData) i
298: .next();
299: itemMetaDataMap.put(itemMetaData.getLabel(),
300: itemMetaData.getEntry());
301: }
302: }
303: return itemMetaDataMap;
304: }
305:
306: public Set getItemFeedbackSet() {
307: return itemFeedbackSet;
308: }
309:
310: public void setItemFeedbackSet(Set itemFeedbackSet) {
311: this .itemFeedbackSet = itemFeedbackSet;
312: this .itemFeedbackMap = getItemFeedbackMap(itemFeedbackSet);
313: }
314:
315: public HashMap getItemFeedbackMap(Set itemFeedbackSet) {
316: HashMap itemFeedbackMap = new HashMap();
317: if (itemFeedbackSet != null) {
318: for (Iterator i = itemFeedbackSet.iterator(); i.hasNext();) {
319: PublishedItemFeedback itemFeedback = (PublishedItemFeedback) i
320: .next();
321: itemFeedbackMap.put(itemFeedback.getTypeId(),
322: itemFeedback.getText());
323: }
324: }
325: return itemFeedbackMap;
326: }
327:
328: private void writeObject(java.io.ObjectOutputStream out)
329: throws IOException {
330: out.defaultWriteObject();
331: }
332:
333: private void readObject(java.io.ObjectInputStream in)
334: throws IOException, ClassNotFoundException {
335: in.defaultReadObject();
336: }
337:
338: public void addItemText(String text, Set answerSet) {
339: if (this .itemTextSet == null) {
340: this .itemTextSet = new HashSet();
341: }
342: Long sequence = new Long(this .itemTextSet.size() + 1);
343: PublishedItemText itemText = new PublishedItemText(this ,
344: sequence, text, answerSet);
345: this .itemTextSet.add(itemText);
346: }
347:
348: public String getItemMetaDataByLabel(String label) {
349: return (String) this .itemMetaDataMap.get(label);
350: }
351:
352: public void addItemMetaData(String label, String entry) {
353: if (this .itemMetaDataSet == null) {
354: setItemMetaDataSet(new HashSet());
355: this .itemMetaDataMap = new HashMap();
356: }
357: this .itemMetaDataMap.put(label, entry);
358: this .itemMetaDataSet.add(new PublishedItemMetaData(this , label,
359: entry));
360: }
361:
362: public String getCorrectItemFeedback() {
363: return getItemFeedback(PublishedItemFeedback.CORRECT_FEEDBACK);
364: }
365:
366: public void setCorrectItemFeedback(String text) {
367: removeFeedbackByType(PublishedItemFeedback.CORRECT_FEEDBACK);
368: addItemFeedback(PublishedItemFeedback.CORRECT_FEEDBACK, text);
369: }
370:
371: public String getInCorrectItemFeedback() {
372: return getItemFeedback(PublishedItemFeedback.INCORRECT_FEEDBACK);
373: }
374:
375: public void setInCorrectItemFeedback(String text) {
376: removeFeedbackByType(PublishedItemFeedback.INCORRECT_FEEDBACK);
377: addItemFeedback(PublishedItemFeedback.INCORRECT_FEEDBACK, text);
378: }
379:
380: /**
381: * Get General Feedback
382: * @return
383: */
384: public String getGeneralItemFeedback() {
385: return getItemFeedback(ItemFeedback.GENERAL_FEEDBACK);
386: }
387:
388: /**
389: * Set General Feedback
390: * @param text
391: */
392: public void setGeneralItemFeedback(String text) {
393: removeFeedbackByType(ItemFeedback.GENERAL_FEEDBACK);
394: addItemFeedback(ItemFeedback.GENERAL_FEEDBACK, text);
395: }
396:
397: public String getItemFeedback(String typeId) {
398: return (String) this .itemFeedbackMap.get(typeId);
399: }
400:
401: public void addItemFeedback(String typeId, String text) {
402: if (this .itemFeedbackSet == null) {
403: setItemFeedbackSet(new HashSet());
404: this .itemFeedbackMap = new HashMap();
405: }
406: this .itemFeedbackMap.put(typeId, text);
407: this .itemFeedbackSet.add(new PublishedItemFeedback(this ,
408: typeId, text));
409: }
410:
411: public void removeFeedbackByType(String typeId) {
412: if (itemFeedbackSet != null) {
413: for (Iterator i = this .itemFeedbackSet.iterator(); i
414: .hasNext();) {
415: PublishedItemFeedback itemFeedback = (PublishedItemFeedback) i
416: .next();
417: if (itemFeedback.getTypeId().equals(typeId)) {
418: this .itemFeedbackSet.remove(itemFeedback);
419: }
420: }
421: }
422: }
423:
424: public void removeMetaDataByType(String label) {
425: try {
426: if (itemMetaDataSet != null) {
427: for (Iterator i = this .itemMetaDataSet.iterator(); i
428: .hasNext();) {
429: PublishedItemMetaData itemMetaData = (PublishedItemMetaData) i
430: .next();
431: if (itemMetaData.getLabel().equals(label)) {
432: //this.itemMetaDataSet.remove(itemMetaData);
433: i.remove();
434: }
435: }
436: }
437: } catch (Exception e) {
438: e.printStackTrace();
439: }
440: }
441:
442: /**
443: * If this is a true-false question return true if it is true, else false.
444: * If it is not a true-false question return false.
445: * @return true if this is a true true-false question
446: */
447: public Boolean getIsTrue() {
448: // if not true false, done.
449: if (!this .getTypeId().equals(TypeD.TRUE_FALSE)) {
450: return Boolean.FALSE;
451: }
452:
453: Set answerSet = null;
454:
455: Set set = this .getItemTextSet();
456: Iterator iter = set.iterator();
457: if (iter.hasNext()) {
458: answerSet = ((ItemTextIfc) iter.next()).getAnswerSet();
459: }
460:
461: // if the FIRST answer is CORRECT, the true false question is TRUE
462: // Note that this is implementation dependent
463: if (answerSet != null) {
464: Iterator aiter = answerSet.iterator();
465: if (aiter.hasNext()) {
466: AnswerIfc answer = (AnswerIfc) aiter.next();
467: return answer.getIsCorrect();
468: }
469: }
470:
471: return Boolean.FALSE;
472:
473: }
474:
475: /**
476: * In the case of an ordinary question, this will obtain the a set of text with
477: * one element and return it; in FIB return multiple elements separated by underscores.
478: * @return text of question
479: */
480: public String getText() {
481: String text = "";
482: if (getTypeId().equals(TypeIfc.MATCHING))
483: return instruction;
484: Set set = this .getItemTextSet();
485: Iterator iter = set.iterator();
486:
487: while (iter.hasNext()) {
488: ItemTextIfc itemText = (ItemTextIfc) iter.next();
489: text += "" + itemText.getText(); //each text add it in
490:
491: if (this .getTypeId().equals(TypeIfc.FILL_IN_BLANK)) { //e.g. Roses are {}. Violets are {}. replace as
492: // Roses are ____. Violets are ____.
493: text = text.replaceAll("\\{", "__");
494: text = text.replaceAll("\\}", "__");
495: }
496: if (this .getTypeId().equals(TypeIfc.FILL_IN_NUMERIC)) { //e.g. Roses are {}. Violets are {}. replace as
497: // Roses are ____. Violets are ____.
498: text = text.replaceAll("\\{", "__");
499: text = text.replaceAll("\\}", "__");
500: }
501: }
502: return text;
503: }
504:
505: public TypeIfc getType() {
506: /*
507: TypeFacadeQueriesAPI typeFacadeQueries = PersistenceService.getInstance().getTypeFacadeQueries();
508: TypeIfc type = typeFacadeQueries.getTypeFacadeById(this.typeId);
509: TypeD typeD = new TypeD(type.getAuthority(), type.getDomain(),
510: type.getKeyword(), type.getDescription());
511: typeD.setTypeId(this.typeId);
512: return typeD;
513: */
514: return null;
515: }
516:
517: public ArrayList getItemTextArray() {
518: ArrayList list = new ArrayList();
519: Iterator iter = itemTextSet.iterator();
520: while (iter.hasNext()) {
521: list.add(iter.next());
522: }
523: return list;
524: }
525:
526: public ArrayList getItemTextArraySorted() {
527: ArrayList list = getItemTextArray();
528: Collections.sort(list);
529: return list;
530: }
531:
532: public void setTriesAllowed(Integer triesAllowed) {
533: this .triesAllowed = triesAllowed;
534: }
535:
536: public Integer getTriesAllowed() {
537: return this .triesAllowed;
538: }
539:
540: /**
541: * This method return the answerKey for a matching question
542: * e.g. A:2, B:3, C:1, D:4 (where A, B & C is the answer label and 1,2 &3
543: * are the itemText sequence
544: */
545: public String getAnswerKey() {
546: String answerKey = "";
547: ArrayList itemTextArray = getItemTextArraySorted();
548: if (itemTextArray.size() == 0)
549: return answerKey;
550:
551: ArrayList answerArray = ((ItemTextIfc) itemTextArray.get(0))
552: .getAnswerArraySorted();
553: HashMap h = new HashMap();
554: for (int i = 0; i < itemTextArray.size(); i++) {
555: ItemTextIfc text = (ItemTextIfc) itemTextArray.get(i);
556: ArrayList answers = text.getAnswerArraySorted();
557: for (int j = 0; j < answers.size(); j++) {
558: AnswerIfc a = (AnswerIfc) answers.get(j);
559: if ((Boolean.TRUE).equals(a.getIsCorrect())) {
560: String pair = (String) h.get(a.getLabel());
561: if (pair == null) {
562: String s = a.getLabel() + ":"
563: + text.getSequence();
564: h.put(a.getLabel(), s);
565: } else
566: h.put(a.getLabel(), pair + " "
567: + text.getSequence());
568: }
569: }
570: }
571: for (int k = 0; k < answerArray.size(); k++) {
572: AnswerIfc a = (AnswerIfc) answerArray.get(k);
573: String pair = (String) h.get(a.getLabel());
574: //if answer is not a match to any text, just print answer label
575: if (pair == null)
576: pair = a.getLabel() + ": ";
577:
578: if (k != 0)
579: answerKey = answerKey + ", " + pair;
580: else
581: answerKey = pair;
582: }
583: return answerKey;
584: }
585:
586: public int compareTo(Object o) {
587: PublishedItemData a = (PublishedItemData) o;
588: return sequence.compareTo(a.sequence);
589: }
590:
591: /*
592: public ItemGradingData getLastItemGradingDataByAgent(){
593: GradingService service = new GradingService();
594: ItemGradingData i= service.getLastItemGradingDataByAgent(
595: this.itemId.toString(), AgentFacade.getAgentString());
596: return i;
597: }
598:
599: public ItemGradingData getLastItemGradingDataByGivenAgent(String agentId){
600: GradingService service = new GradingService();
601: ItemGradingData i= service.getLastItemGradingDataByAgent(
602: this.itemId.toString(), "jon");
603: return i;
604: }
605: */
606:
607: public Set getItemAttachmentSet() {
608: return itemAttachmentSet;
609: }
610:
611: public void setItemAttachmentSet(Set itemAttachmentSet) {
612: this .itemAttachmentSet = itemAttachmentSet;
613: }
614:
615: public List getItemAttachmentList() {
616: ArrayList list = new ArrayList();
617: if (itemAttachmentSet != null) {
618: Iterator iter = itemAttachmentSet.iterator();
619: while (iter.hasNext()) {
620: ItemAttachmentIfc a = (ItemAttachmentIfc) iter.next();
621: list.add(a);
622: }
623: }
624: return list;
625: }
626:
627: }
|