View Javadoc

1   package org.telscenter.pas.steps.actions;
2   
3   //import java.awt.Frame;
4   import java.awt.event.ActionEvent;
5   import java.beans.PropertyVetoException;
6   import java.util.HashMap;
7   import java.util.Map;
8   
9   import javax.swing.BoxLayout;
10  import javax.swing.JButton;
11  import javax.swing.JDialog;
12  import javax.swing.JOptionPane;
13  import javax.swing.JPanel;
14  //import javax.swing.JRadioButton;
15  
16  import net.sf.sail.core.entity.ISock;
17  
18  import org.telscenter.pas.beans.PasStep;
19  import org.telscenter.pas.common.ui.CommonUI;
20  import org.telscenter.pas.steps.Assessment;
21  import org.telscenter.pas.steps.ChallengeQuestion;
22  //import org.telscenter.pas.steps.ChallengeQuestionUI;
23  import org.telscenter.pas.steps.domain.AssessmentItem;
24  import org.telscenter.pas.steps.domain.FeedbackModal;
25  import org.telscenter.pas.steps.domain.ResponseDeclaration;
26  //import org.telscenter.pas.ui.dialog.PasDialogManager;
27  //import org.telscenter.pas.ui.dialog.PasNoteMessageDialogUI;
28  
29  
30  /***
31   * checks and saves when user attempts an answer for a Challenge Question
32   * 
33   * @author hiroki, jerry
34   */
35  public class ChallengeQuestionCheckAction extends DialogSaveAction {
36  
37  	private static final long serialVersionUID = 1L;
38  
39  	private AssessmentItem assessmentItem;
40  
41  	private Map<ResponseDeclaration, ISock<String>> responseDeclarationToSocks = new HashMap<ResponseDeclaration, ISock<String>>();
42  
43  	private Map<String, Object> responses = new HashMap<String, Object>();
44  	
45  	private static final int INITIAL_X = 200; // initial left position
46  											  // (x-coordinate) of frame, in
47  											  // pixel
48  
49  	private static final int INITIAL_Y = 150; // initial top position
50  											  // (y-coordinate) of frame, in
51  											  // pixel
52  
53  	private ChallengeQuestion cq;
54  	
55  	public ChallengeQuestionCheckAction(AssessmentItem assessmentItem, Map<String, Object> responses, Map<ResponseDeclaration, ISock<String>> responseDeclarationToSocks, ChallengeQuestion cq) {
56  		super("Check Answer");
57  		this.assessmentItem = assessmentItem;
58  		this.responses = responses;
59  		this.responseDeclarationToSocks = responseDeclarationToSocks;
60  		this.cq = cq;
61  	}
62  
63  	public void actionPerformed(ActionEvent e) {
64  		super.actionPerformed(e);
65  		doSave();
66  		doCheckAndUpdate();
67  	}
68  
69  	
70  	/***
71  	 * check student's answer
72  	 * display inline feedback
73  	 * display modal popup with message specific to the submitted answer and a link 
74  	 * to relevent step.
75  	 * @return 
76  	 */
77  	public void doCheckAndUpdate() {
78  		Object correctAnswer = cq.correctAnswer();
79  		String studentAnswer = cq.lastAnswer();
80  		
81  		int attemptNum = cq.numStudentAttempts();
82  		
83  		// refresh VLE window's inline feedback
84  		cq.getCqUI().clearFeedbackInline();
85  		
86  		// get rid of the any previous Icons 
87  		cq.getCqUI().clearRadioButtonIcons(); 
88  		
89  		// update/display score status no matter what
90  		cq.getCqUI().updateScoreLabelStatus();
91  
92  		// disable if this was last attempt
93  		if(attemptNum >= cq.getMaxAttempts()) {
94  			cq.getCqUI().disableRadioButtons(studentAnswer,false);
95  		}
96  				
97  		// Make the popup display
98  		FeedbackModal fm = null;		
99  		if (correctAnswer.equals(studentAnswer)) {
100 			// student got the correct answer
101 			cq.getCqUI().displayCorrectFeedbackInline();
102 			
103 			// set student's choice's icon			
104 			cq.getCqUI().selectRadioButton(correctAnswer, true); // green check
105 			
106 			// display feedback with ok button
107 			cq.getCqUI().disableRadioButtons(correctAnswer,true);
108 			// disable check answer button
109 			cq.getCqUI().disableProcessAttemptButton();
110 			cq.getCqUI().getGotoStepButton().setVisible(false);
111 			// get the modal feedback for correct answer
112 			//fm = assessmentItem.getModalFeedbacks().get(correctAnswer);
113 			/*for (FeedbackModal fbModal : assessmentItem.getModalFeedbacks().values()){
114 				String title = fbModal.getTitle().toString();
115 				if(title.toUpperCase().indexOf("CORRECT") > -1){
116 					fm = assessmentItem.getModalFeedbacks().get(fbModal.getIdentifier());
117 				}
118 			}*/
119 			
120 			//displayFeedbackModal(fm, true);
121 			
122 		} else {
123 			// student didn't get the correct answer
124 
125 			// show the incorrect inline feedback and check radiobutton
126 			cq.getCqUI().disableRadioButtons(studentAnswer,false);
127 			cq.getCqUI().displayLastFeedbackInline();
128 			cq.getCqUI().selectRadioButton(studentAnswer, false);
129 			
130 			// if this is the final attempt, also show the correct inline feedback and check radiobutton
131 			if(attemptNum >= cq.getMaxAttempts()) {
132 //				cq.getCqUI().displayCorrectFeedbackInline();
133 				cq.getCqUI().selectRadioButton(correctAnswer, true);
134 				cq.getCqUI().getGotoStepButton().setVisible(false);
135 			}else{
136 				cq.getCqUI().getGotoStepButton().setVisible(true);
137 			}
138 			
139 			// get the modal feedback for this attemptNum-th wrong answer
140 			//String[] attemptStrings = {"firstAttempt", "secondAttempt", "thirdAttempt", "fourthAttempt"};
141 			//fm = assessmentItem.getModalFeedbacks().get(attemptStrings[attemptNum-1]);
142 			//displayFeedbackModal(fm, false);
143 			
144 			// disable check answer button
145 			cq.getCqUI().disableProcessAttemptButton();
146 			
147 			
148 			
149 			// then display evidence step in a modal popup window
150 			//displayGotoStep_old();	
151 		}		
152 	}
153 
154 	private void displayGotoStep_old() {
155 		JPanel evidencePanel = new JPanel();
156 		evidencePanel.setLayout(new BoxLayout(evidencePanel, BoxLayout.Y_AXIS));		
157 		
158 		PasStep gotoStep = cq.getGotoStep();
159 		try {
160 			gotoStep.setBeanContext(cq.getBeanContext());
161 		} catch (PropertyVetoException e) {
162 			e.printStackTrace();
163 		}
164 
165 		//cq.getNavigationService().setCurrentStep(gotoStep);
166 
167 		evidencePanel.setSize(500, 600);
168 		evidencePanel.add(gotoStep.getComponent());
169 		
170 		// TODO: find appropriate type of pas dialog
171 		JDialog evidenceDialog = new JDialog();
172 		
173 		//evidenceDialog.setModal(true);
174 		evidenceDialog.setTitle("Revisit step");
175 		//evidenceDialog.setTitle("Correct");
176 		evidenceDialog.setContentPane(evidencePanel);
177 		evidenceDialog.setLocation(INITIAL_X, INITIAL_Y);
178 		evidenceDialog.setSize(evidencePanel.getWidth(), evidencePanel.getHeight());
179 		evidenceDialog.setVisible(true);
180 		evidenceDialog.toFront();
181 		//evidenceDialog.transferFocusUpCycle();
182 	}
183 
184 	private void displayFeedbackModal(FeedbackModal fm, boolean isCorrect) {
185 		String feedbackString;
186 		double width;
187 		if(fm!=null && (feedbackString = fm.getContent().toString()).length() != 0){
188 				int stringLen = feedbackString.length();
189 				if(stringLen > 40){
190 					width = Math.ceil(stringLen/3);
191 				}else if(stringLen < 20){
192 					width = Math.ceil(stringLen);
193 				}else{
194 					width = Math.ceil(stringLen/2);
195 				}
196 			//cq.getCqUI().wrapInlineText(feedbackString,1);
197 			feedbackString = cq.getCqUI().wrapInlineText(feedbackString,(int)width);
198 			if(feedbackString.endsWith("</body></html>")){
199 				feedbackString = feedbackString.replaceAll("</body></html>", "");
200 			}	
201 			// show a modal dialog with feedback
202 				JButton cancelButton = new JButton();
203 				cancelButton.setAction(new DialogSaveAction("Cancel"));
204 				if(isCorrect) {
205 					cancelButton.setText("OK");
206 				} else { 
207 					cancelButton.setText("Revisit Evidence Step");
208 				}
209 				CommonUI.makeEnhancedButton(cancelButton);
210 				JOptionPane.showOptionDialog(null , feedbackString, "Correct", JOptionPane.DEFAULT_OPTION, 
211 						JOptionPane.PLAIN_MESSAGE, null, 
212 						new Object[]{cancelButton}, null);
213 
214 			
215 		}
216 		
217 
218 //		PasNoteMessageDialogUI ui = new PasNoteMessageDialogUI();
219 //		ui.setMessage(feedbackString);
220 
221 //		JPanel dialogPanel = ui.createDialogPanel();
222 //		ui.getOkButton().setVisible(false);
223 //		ui.getCancelButton().setText("Dimiss");
224 //		PasDialogManager.showPasMessageDialog("Feedback", "", null, dialogPanel, null, true);
225 	}
226 
227 	/***
228 	 * Saves all the responses to rims
229 	 */
230 	private void doSave() {
231 		Assessment.saveAssessmentItem(assessmentItem, responses, responseDeclarationToSocks);
232 	}
233 
234 
235 // see PasFeedbackModalDialogUI	
236 //		PasSaveStudentAssesmentDialogUI ui = new PasSaveStudentAssesmentDialogUI(
237 //				leaveAction);
238 //		ui.setMessage(Messages.getString("StudentAssessment.52")); //$NON-NLS-1$
239 //		// ui.setMessage("Your issue has not been sent to the TELS Team, would
240 //		// you like to send it?");
241 
242 //		PasDialogManager
243 //				.showPasMessageDialog(
244 //						Messages.getString("StudentAssessment.53"), ui.createDialogPanel(), (JFrame) projectFrame, true); //$NON-NLS-1$
245 //
246 
247 	
248 
249 }