1 package org.telscenter.pas.steps.actions;
2
3
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
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
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
27
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;
46
47
48
49 private static final int INITIAL_Y = 150;
50
51
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
84 cq.getCqUI().clearFeedbackInline();
85
86
87 cq.getCqUI().clearRadioButtonIcons();
88
89
90 cq.getCqUI().updateScoreLabelStatus();
91
92
93 if(attemptNum >= cq.getMaxAttempts()) {
94 cq.getCqUI().disableRadioButtons(studentAnswer,false);
95 }
96
97
98 FeedbackModal fm = null;
99 if (correctAnswer.equals(studentAnswer)) {
100
101 cq.getCqUI().displayCorrectFeedbackInline();
102
103
104 cq.getCqUI().selectRadioButton(correctAnswer, true);
105
106
107 cq.getCqUI().disableRadioButtons(correctAnswer,true);
108
109 cq.getCqUI().disableProcessAttemptButton();
110 cq.getCqUI().getGotoStepButton().setVisible(false);
111
112
113
114
115
116
117
118
119
120
121
122 } else {
123
124
125
126 cq.getCqUI().disableRadioButtons(studentAnswer,false);
127 cq.getCqUI().displayLastFeedbackInline();
128 cq.getCqUI().selectRadioButton(studentAnswer, false);
129
130
131 if(attemptNum >= cq.getMaxAttempts()) {
132
133 cq.getCqUI().selectRadioButton(correctAnswer, true);
134 cq.getCqUI().getGotoStepButton().setVisible(false);
135 }else{
136 cq.getCqUI().getGotoStepButton().setVisible(true);
137 }
138
139
140
141
142
143
144
145 cq.getCqUI().disableProcessAttemptButton();
146
147
148
149
150
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
166
167 evidencePanel.setSize(500, 600);
168 evidencePanel.add(gotoStep.getComponent());
169
170
171 JDialog evidenceDialog = new JDialog();
172
173
174 evidenceDialog.setTitle("Revisit step");
175
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
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
197 feedbackString = cq.getCqUI().wrapInlineText(feedbackString,(int)width);
198 if(feedbackString.endsWith("</body></html>")){
199 feedbackString = feedbackString.replaceAll("</body></html>", "");
200 }
201
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
219
220
221
222
223
224
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249 }