001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/tags/sakai_2-4-1/samigo-app/src/java/org/sakaiproject/tool/assessment/ui/bean/author/SectionBean.java $
003: * $Id: SectionBean.java 27669 2007-03-22 20:58:46Z 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.ui.bean.author;
021:
022: import java.io.Serializable;
023: import java.util.ArrayList;
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.List;
027: import org.sakaiproject.util.ResourceLoader;
028:
029: import javax.faces.context.ExternalContext;
030: import javax.faces.context.FacesContext;
031: import javax.faces.event.ValueChangeEvent;
032: import javax.faces.model.SelectItem;
033:
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036: import org.sakaiproject.content.api.ContentResource;
037: import org.sakaiproject.content.api.FilePickerHelper;
038: import org.sakaiproject.content.cover.ContentHostingService;
039: import org.sakaiproject.entity.impl.ReferenceComponent;
040: import org.sakaiproject.exception.IdUnusedException;
041: import org.sakaiproject.exception.PermissionException;
042: import org.sakaiproject.exception.TypeException;
043: import org.sakaiproject.tool.api.ToolSession;
044: import org.sakaiproject.tool.assessment.data.ifc.assessment.AssessmentIfc;
045: import org.sakaiproject.tool.assessment.data.ifc.assessment.AttachmentIfc;
046: import org.sakaiproject.tool.assessment.data.ifc.assessment.SectionDataIfc;
047: import org.sakaiproject.tool.assessment.data.model.Tree;
048: import org.sakaiproject.tool.assessment.facade.AgentFacade;
049: import org.sakaiproject.tool.assessment.facade.QuestionPoolFacade;
050: import org.sakaiproject.tool.assessment.facade.SectionFacade;
051: import org.sakaiproject.tool.assessment.services.QuestionPoolService;
052: import org.sakaiproject.tool.assessment.services.assessment.AssessmentService;
053: import org.sakaiproject.tool.assessment.ui.listener.author.SavePartAttachmentListener;
054: import org.sakaiproject.tool.assessment.ui.listener.util.ContextUtil;
055: import org.sakaiproject.tool.cover.SessionManager;
056:
057: /**
058: * Used to be org.navigoproject.ui.web.asi.author.section.SectionActionForm.java
059: */
060:
061: public class SectionBean implements Serializable {
062: private static Log log = LogFactory.getLog(SectionBean.class);
063:
064: /** Use serialVersionUID for interoperability. */
065: private final static long serialVersionUID = 4216587136245498157L;
066: private String assessmentTitle;
067: private String assessmentId;
068: private String showMetadata;
069: private String sectionId;
070: private String noOfItems;
071: private String sectionTitle;
072: private String sectionDescription;
073: private ArrayList assessmentSectionIdents;
074: private ArrayList poolsAvailable; // selectItems for pools
075: private ArrayList items;
076: private boolean random;
077: private String removeAllQuestions; // 1=Yes, 0=No
078: private SectionFacade section;
079: private AssessmentIfc assessment;
080: private String destSectionId; //destinated section where questions will be moved to
081: private String randomizationType;
082:
083: private String numberSelected;
084: private String selectedPool; // pool id for the item to be added to
085:
086: private String objective;
087: private String keyword;
088: private String rubric;
089: private String type;
090: private String questionOrdering;
091:
092: private boolean hideRandom = false;
093: private boolean hideOneByOne = false;
094:
095: private String outcome;
096: private Tree tree;
097:
098: private List attachmentList;
099:
100: public void setSection(SectionFacade section) {
101: try {
102: if (section == null) {
103: this .section = null;
104: } else {
105: this .section = section;
106: this .assessment = section.getAssessment();
107: this .assessmentId = assessment.getAssessmentId()
108: .toString();
109: this .assessmentTitle = assessment.getTitle();
110: this .sectionId = section.getSectionId().toString();
111: this .sectionTitle = section.getTitle();
112: this .sectionDescription = section.getDescription();
113: this .attachmentList = section
114: .getSectionAttachmentList();
115: if (this .attachmentList != null
116: && this .attachmentList.size() > 0)
117: this .hasAttachment = true;
118: }
119: } catch (Exception ex) {
120: log
121: .warn("Unable to set section. Exception thrown from setSection(): "
122: + ex.getMessage());
123: }
124: }
125:
126: public SectionFacade getSection() {
127: return section;
128: }
129:
130: public boolean getHideRandom() {
131: return hideRandom;
132: }
133:
134: public void setHideRandom(boolean param) {
135: hideRandom = param;
136: }
137:
138: public boolean getHideOneByOne() {
139: return hideOneByOne;
140: }
141:
142: public void setHideOneByOne(boolean param) {
143: hideOneByOne = param;
144: }
145:
146: /**
147: * @return
148: */
149: public String getAssessmentId() {
150: return assessmentId;
151: }
152:
153: /**
154: * @return
155: */
156: public String getAssessmentTitle() {
157: return assessmentTitle;
158: }
159:
160: /**
161: * @param string
162: */
163: public void setAssessmentId(String string) {
164: assessmentId = string;
165: }
166:
167: /**
168: * @param string
169: */
170: public void setAssessmentTitle(String string) {
171: assessmentTitle = string;
172: }
173:
174: /**
175: * @return
176: */
177: public String getShowMetadata() {
178: return showMetadata;
179: }
180:
181: /**
182: * @param string
183: */
184: public void setShowMetadata(String string) {
185: showMetadata = string;
186: }
187:
188: /**
189: * @return
190: */
191: public String getSectionId() {
192: return sectionId;
193: }
194:
195: public String getSectionIdent() {
196: return getSectionId();
197: }
198:
199: /**
200: * @param string
201: */
202: public void setSectionId(String string) {
203: sectionId = string;
204: }
205:
206: public void setSectionIdent(String string) {
207: setSectionId(string);
208: }
209:
210: /**
211: * @return
212: */
213: public ArrayList getAssessmentSectionIdents() {
214: return assessmentSectionIdents;
215: }
216:
217: /**
218: * @param list
219: */
220: public void setAssessmentSectionIdents(ArrayList list) {
221: assessmentSectionIdents = list;
222: }
223:
224: /**
225: * Get a numerical sequence for all parts.
226: * Derived property.
227: * @return String[] in format "1", "2", "3"... up to the number of parts
228: */
229: public ArrayList getSectionNumberList() {
230: ArrayList list = new ArrayList();
231:
232: if (assessmentSectionIdents == null)
233: return list;
234:
235: for (int i = 0; i < assessmentSectionIdents.toArray().length; i++) {
236: SelectItem selection = new SelectItem();
237: selection.setLabel("" + i);
238: selection.setValue("" + i);
239: list.add(selection);
240: }
241:
242: return list;
243: }
244:
245: public ArrayList getAuthorTypeList() {
246:
247: ArrayList list = new ArrayList();
248: // cannot disable only one radio button in a list, so am generating the list again
249:
250: FacesContext context = FacesContext.getCurrentInstance();
251: ResourceLoader rb = new ResourceLoader(
252: "org.sakaiproject.tool.assessment.bundle.AuthorMessages");
253:
254: if (hideRandom) {
255: SelectItem selection = new SelectItem();
256: selection.setLabel(rb.getString("type_onebyone"));
257: selection.setValue("1");
258: list.add(selection);
259: } else {
260: SelectItem selection = new SelectItem();
261: selection.setLabel(rb.getString("type_onebyone"));
262: selection.setValue("1");
263: list.add(selection);
264: SelectItem selection1 = new SelectItem();
265: selection1.setLabel(rb.getString("random_draw_from_que"));
266: selection1.setValue("2");
267: list.add(selection1);
268: }
269:
270: return list;
271: }
272:
273: /**
274: * Ordinal number of current section.
275: * Derived property.
276: * @return String the number as a String, e.g. "3"
277: */
278: public String getSelectedSection() {
279: return "" + assessmentSectionIdents.indexOf(sectionId);
280: }
281:
282: public int getTotalSections() {
283: return assessmentSectionIdents.size();
284: }
285:
286: /**List of available question pools for random draw.
287: * returns a list of pools that have not been used by other random drawn parts
288: * @return ArrayList of QuestionPoolFacade objects
289: */
290: public ArrayList getPoolsAvailable() {
291:
292: ArrayList resultPoolList = new ArrayList();
293: AssessmentBean assessmentBean = (AssessmentBean) ContextUtil
294: .lookupBean("assessmentBean");
295: //ItemAuthorBean itemauthorBean = (ItemAuthorBean) ContextUtil.lookupBean("itemauthor");
296:
297: QuestionPoolService delegate = new QuestionPoolService();
298:
299: ArrayList allpoollist = delegate
300: .getBasicInfoOfAllPools(AgentFacade.getAgentString());
301:
302: HashMap allPoolsMap = new HashMap();
303: for (int i = 0; i < allpoollist.size(); i++) {
304: QuestionPoolFacade apool = (QuestionPoolFacade) allpoollist
305: .get(i);
306: allPoolsMap
307: .put(apool.getQuestionPoolId().toString(), apool);
308: }
309:
310: AssessmentService assessdelegate = new AssessmentService();
311: List sectionList = assessmentBean.getSectionList();
312: for (int i = 0; i < sectionList.size(); i++) {
313: SelectItem s = (SelectItem) sectionList.get(i);
314:
315: // need to remove the pools already used by random draw parts
316:
317: SectionDataIfc section = assessdelegate.getSection(s
318: .getValue().toString());
319: if ((section != null)
320: && (section
321: .getSectionMetaDataByLabel(SectionDataIfc.AUTHOR_TYPE) != null)
322: && (section
323: .getSectionMetaDataByLabel(SectionDataIfc.AUTHOR_TYPE)
324: .equals(SectionDataIfc.RANDOM_DRAW_FROM_QUESTIONPOOL
325: .toString()))) {
326: String poolid = section
327: .getSectionMetaDataByLabel(SectionDataIfc.POOLID_FOR_RANDOM_DRAW);
328: if (allPoolsMap.containsKey(poolid)) {
329: allPoolsMap.remove(poolid);
330: }
331: }
332: }
333:
334: Iterator pooliter = allPoolsMap.keySet().iterator();
335: while (pooliter.hasNext()) {
336: QuestionPoolFacade pool = (QuestionPoolFacade) allPoolsMap
337: .get(pooliter.next());
338: //Huong's new
339: ArrayList itemlist = delegate.getAllItems(pool
340: .getQuestionPoolId());
341: if (itemlist.size() > 0) {
342: String resultListName = pool.getDisplayName() + "("
343: + itemlist.size() + ")";
344: resultPoolList.add(new SelectItem((pool
345: .getQuestionPoolId().toString()),
346: resultListName));
347: }
348: }
349: // add pool which is currently used in current Part for modify part
350: if (!("".equals(this .getSelectedPool()))
351: && (this .getSelectedPool() != null)) {
352:
353: //now we need to get the poolid and displayName
354:
355: QuestionPoolFacade currPool = delegate.getPool(new Long(
356: this .getSelectedPool()), AgentFacade
357: .getAgentString());
358: // now add the current pool used to the list, so it's available in the pulldown
359: if (currPool != null) {
360: // if the pool still exists, it's possible that the pool has been deleted
361: ArrayList currItemlist = delegate.getAllItems(currPool
362: .getQuestionPoolId());
363: if (currItemlist.size() > 0) {
364: String currPoolName = currPool.getDisplayName()
365: + "(" + currItemlist.size() + ")";
366: resultPoolList.add(new SelectItem((currPool
367: .getQuestionPoolId().toString()),
368: currPoolName));
369:
370: }
371: } else {
372: // the pool has been deleted,
373: }
374: }
375:
376: return resultPoolList;
377: }
378:
379: /**List of available question pools.
380: * @param list ArrayList of selectItems
381: */
382: public void setPoolsAvailable(ArrayList list) {
383: poolsAvailable = list;
384: }
385:
386: /**
387: * @return
388: */
389: public String getNoOfItems() {
390: return noOfItems;
391: }
392:
393: /**
394: * @param string
395: */
396: public void setNoOfItems(String string) {
397: noOfItems = string;
398: }
399:
400: /**
401: * Get a numerical sequence for all questions.
402: * Derived property.
403: * @return String[] in format "1", "2", "3"... up to the number of questions
404: */
405:
406: public ArrayList getItemNumberList() {
407: ArrayList list = new ArrayList();
408:
409: for (int i = 0; i < items.toArray().length; i++) {
410: SelectItem selection = new SelectItem();
411: selection.setLabel("" + i);
412: selection.setValue("" + i);
413: list.add(selection);
414: }
415:
416: return list;
417: }
418:
419: /**
420: * @return the title
421: */
422: public String getSectionTitle() {
423: return this .sectionTitle;
424: }
425:
426: /**
427: * @param string the title
428: */
429: public void setSectionTitle(String string) {
430: this .sectionTitle = string;
431: }
432:
433: /**
434: * @return the info
435: */
436: public String getSectionDescription() {
437: return sectionDescription;
438: }
439:
440: /**
441: * @param string the info
442: */
443: public void setSectionDescription(String string) {
444: sectionDescription = string;
445: }
446:
447: /**
448: * @return the number selected
449: */
450: public String getNumberSelected() {
451: return numberSelected;
452: }
453:
454: /**
455: * @param string the number selected
456: */
457: public void setNumberSelected(String string) {
458: numberSelected = string;
459: }
460:
461: /**
462: * randomize?
463: * @return boolean
464: */
465: public boolean getRandom() {
466: return random;
467: }
468:
469: public ArrayList getItems() {
470: return items;
471: }
472:
473: /**
474: * randomize?
475: * @param bool boolean
476: */
477: public void setRandom(boolean bool) {
478: random = bool;
479: }
480:
481: public void setItems(ArrayList items) {
482: this .items = items;
483: }
484:
485: /**
486: * If removing part, do questions go with it?
487: * @return true if questions are deleted too.
488: */
489: public String getRemoveAllQuestions() {
490: return removeAllQuestions;
491: }
492:
493: /**
494: * If removing part, do questions go with it?
495: * @param removeAllQuestions
496: */
497: public void setRemoveAllQuestions(String removeAllQuestions) {
498: this .removeAllQuestions = removeAllQuestions;
499: }
500:
501: public String getDestSectionId() {
502: return destSectionId;
503: }
504:
505: /**
506: * @param string the title
507: */
508: public void setDestSectionId(String destSectionId) {
509: this .destSectionId = destSectionId;
510: }
511:
512: /**
513: * String value of selected pool id
514: * @return String value of selected pool id
515: */
516: public String getSelectedPool() {
517: return selectedPool;
518: }
519:
520: /**
521: * set the String value of selected pool id
522: * @param selectedPool String value of selected pool id
523: */
524: public void setSelectedPool(String selectedPool) {
525: this .selectedPool = selectedPool;
526: }
527:
528: /**
529: * get keyword metadata
530: */
531: public String getKeyword() {
532: return keyword;
533: }
534:
535: /**
536: * set metadata
537: * @param param
538: */
539: public void setKeyword(String param) {
540: this .keyword = param;
541: }
542:
543: /**
544: * get objective metadata
545: */
546: public String getObjective() {
547: return objective;
548: }
549:
550: /**
551: * set metadata
552: * @param param
553: */
554: public void setObjective(String param) {
555: this .objective = param;
556: }
557:
558: /**
559: * get rubric metadata
560: */
561: public String getRubric() {
562: return rubric;
563: }
564:
565: /**
566: * set metadata
567: * @param param
568: */
569: public void setRubric(String param) {
570: this .rubric = param;
571: }
572:
573: /**
574: * get type
575: */
576: public String getType() {
577: return type;
578: }
579:
580: /**
581: * set type
582: * @param param
583: */
584: public void setType(String param) {
585: this .type = param;
586: }
587:
588: /**
589: * get questionOrdering
590: */
591: public String getQuestionOrdering() {
592: return questionOrdering;
593: }
594:
595: /**
596: * set questionOrdering
597: * @param param
598: */
599: public void setQuestionOrdering(String param) {
600: this .questionOrdering = param;
601: }
602:
603: public void toggleAuthorType(ValueChangeEvent event) {
604:
605: // need to update metadata in db.
606: //FacesContext context = FacesContext.getCurrentInstance();
607: String type = (String) event.getNewValue();
608:
609: if ((type == null)
610: || type
611: .equals(SectionDataIfc.QUESTIONS_AUTHORED_ONE_BY_ONE
612: .toString())) {
613: setType(SectionDataIfc.QUESTIONS_AUTHORED_ONE_BY_ONE
614: .toString());
615: } else if (type
616: .equals(SectionDataIfc.RANDOM_DRAW_FROM_QUESTIONPOOL
617: .toString())) {
618: setType(SectionDataIfc.RANDOM_DRAW_FROM_QUESTIONPOOL
619: .toString());
620: } else {
621: // shouldn't go here.
622: }
623:
624: }
625:
626: /**
627: * get outcome
628: */
629: public String getOutcome() {
630: return outcome;
631: }
632:
633: /**
634: * set outcome
635: * @param param
636: */
637: public void setOutcome(String param) {
638: this .outcome = param;
639: }
640:
641: public List getAttachmentList() {
642: return attachmentList;
643: }
644:
645: public void setAttachmentList(List attachmentList) {
646: this .attachmentList = attachmentList;
647: }
648:
649: private boolean hasAttachment = false;
650:
651: public boolean getHasAttachment() {
652: return this .hasAttachment;
653: }
654:
655: public void setHasAttachment(boolean hasAttachment) {
656: this .hasAttachment = hasAttachment;
657: }
658:
659: public String addAttachmentsRedirect() {
660: // 1. then redirect to add attachment
661: try {
662: List filePickerList = new ArrayList();
663: if (attachmentList != null) {
664: filePickerList = prepareReferenceList(attachmentList);
665: }
666: log.debug("**filePicker list=" + filePickerList.size());
667: ToolSession currentToolSession = SessionManager
668: .getCurrentToolSession();
669: currentToolSession.setAttribute(
670: FilePickerHelper.FILE_PICKER_ATTACHMENTS,
671: filePickerList);
672: ExternalContext context = FacesContext.getCurrentInstance()
673: .getExternalContext();
674: context.redirect("sakai.filepicker.helper/tool");
675: } catch (Exception e) {
676: log.error("fail to redirect to attachment page: "
677: + e.getMessage());
678: }
679: return getOutcome();
680: }
681:
682: public void setPartAttachment() {
683: SavePartAttachmentListener lis = new SavePartAttachmentListener();
684: lis.processAction(null);
685: }
686:
687: private List prepareReferenceList(List attachmentList) {
688: List list = new ArrayList();
689: for (int i = 0; i < attachmentList.size(); i++) {
690: ContentResource cr = null;
691: AttachmentIfc attach = (AttachmentIfc) attachmentList
692: .get(i);
693: try {
694: cr = ContentHostingService.getResource(attach
695: .getResourceId());
696: } catch (PermissionException e) {
697: log
698: .warn("PermissionException from ContentHostingService:"
699: + e.getMessage());
700: } catch (IdUnusedException e) {
701: log
702: .warn("IdUnusedException from ContentHostingService:"
703: + e.getMessage());
704: // <-- bad sign, some left over association of part and resource,
705: // use case: user remove resource in file picker, then exit modification without
706: // proper cancellation by clicking at the left nav instead of "cancel".
707: // Also in this use case, any added resource would be left orphan.
708: AssessmentService assessmentService = new AssessmentService();
709: assessmentService.removeSectionAttachment(attach
710: .getAttachmentId().toString());
711: } catch (TypeException e) {
712: log.warn("TypeException from ContentHostingService:"
713: + e.getMessage());
714: }
715: if (cr != null) {
716: if (this .resourceHash == null)
717: this .resourceHash = new HashMap();
718: this .resourceHash.put(attach.getResourceId(), cr);
719: ReferenceComponent ref = new ReferenceComponent(cr
720: .getReference());
721: if (ref != null)
722: list.add(ref);
723: }
724: }
725: return list;
726: }
727:
728: private HashMap resourceHash = new HashMap();
729:
730: public HashMap getResourceHash() {
731: return resourceHash;
732: }
733:
734: public void setResourceHash(HashMap resourceHash) {
735: this .resourceHash = resourceHash;
736: }
737:
738: public ArrayList getRandomizationTypeList() {
739:
740: ArrayList list = new ArrayList();
741:
742: FacesContext context = FacesContext.getCurrentInstance();
743: ResourceLoader rb = new ResourceLoader(
744: "org.sakaiproject.tool.assessment.bundle.AuthorMessages");
745:
746: SelectItem selection = new SelectItem();
747: selection.setLabel(rb.getString("randomized_per_submission"));
748: selection.setValue("1");
749: list.add(selection);
750:
751: selection = new SelectItem();
752: selection.setLabel(rb.getString("randomized_per_student"));
753: selection.setValue("2");
754: list.add(selection);
755:
756: return list;
757: }
758:
759: public String getRandomizationType() {
760: return randomizationType;
761: }
762:
763: public void setRandomizationType(String randomizationType) {
764: this.randomizationType = randomizationType;
765: }
766:
767: }
|