1 /***
2 *
3 */
4 package org.telscenter.pas.steps;
5
6 import java.awt.BorderLayout;
7 import java.awt.Color;
8 import java.awt.Cursor;
9 import java.awt.Dimension;
10 import java.awt.Font;
11 import java.awt.GridLayout;
12 import java.awt.event.ActionEvent;
13 import java.awt.event.ActionListener;
14 import java.io.Serializable;
15 import java.util.List;
16 import java.util.logging.Logger;
17
18 import javax.swing.AbstractAction;
19 import javax.swing.BorderFactory;
20 import javax.swing.BoxLayout;
21 import javax.swing.JButton;
22 import javax.swing.JComponent;
23 import javax.swing.JDialog;
24 import javax.swing.JFrame;
25 import javax.swing.JLabel;
26 import javax.swing.JPanel;
27 import javax.swing.JScrollPane;
28 import javax.xml.bind.JAXBElement;
29
30 import net.sf.sail.core.entity.ISock;
31 import net.sf.sail.jaxb.extension.BlockInteractionType;
32
33 import org.imsglobal.xsd.imsqti_v2p0.ChoiceInteractionType;
34 import org.imsglobal.xsd.imsqti_v2p0.FeedbackInlineType;
35 import org.imsglobal.xsd.imsqti_v2p0.ResponseDeclarationType;
36 import org.imsglobal.xsd.imsqti_v2p0.SimpleChoiceType;
37 import org.telscenter.pas.common.ui.CommonUI;
38 import org.telscenter.pas.common.ui.panel.SimpleGradientPanel;
39 import org.telscenter.pas.steps.actions.LeaveStudentAssessmentAction;
40 import org.telscenter.pas.ui.dialog.PasDialogManager;
41 import org.telscenter.pas.ui.dialog.PasMessageDialogUI;
42 import org.telscenter.pas.ui.dialog.PasSaveStudentAssesmentDialogUI;
43 import org.telscenter.pas.ui.dialog.PasStudentAssessmentConfirmDialogUI;
44 import org.telscenter.pas.ui.util.PasColors;
45 import org.telscenter.pas.util.JaxbQtiCreationUtils;
46 import org.xito.dialog.CustomDialog;
47
48 /***
49 * Creates the self assessment ui for self assessment jaxb version
50 *
51 * @author aperritano
52 *
53 */
54 public class JaxbQtiSelfAssessmentUI extends JaxbQtiStudentAssessmentUI {
55
56 /***
57 * Logger for this class
58 */
59 static final Logger logger = Logger
60 .getLogger(JaxbQtiSelfAssessmentUI.class.getName());
61
62
63 private static final long serialVersionUID = 1L;
64
65 protected JButton submitQuestionsButton;
66
67 protected String QUESTION_CARD = "QUESTION_CARD";
68
69 protected String RESULT_CARD = "RESULT_CARD";
70
71
72 public boolean toLeaveStep = false;
73
74 protected JPanel submitButtonPanel;
75
76 protected LeaveStudentAssessmentAction leaveAction;
77
78 public JaxbQtiSelfAssessmentUI(JaxbQtiStep assmt) {
79 super(assmt);
80 }
81
82 protected JPanel createButtonPanel() {
83 submitButtonPanel = new JPanel(new BorderLayout(0, 0));
84
85 submitButtonPanel.setBackground(Color.white);
86 submitQuestionsButton = new JButton("Submit Answers");
87 submitQuestionsButton = CommonUI
88 .makeEnhancedButton(submitQuestionsButton);
89 submitQuestionsButton.setCursor(Cursor
90 .getPredefinedCursor(Cursor.HAND_CURSOR));
91
92 submitQuestionsButton.addActionListener(new ActionListener() {
93
94 public void actionPerformed(ActionEvent e) {
95
96 cardPanel.showNextCard();
97
98 doSave();
99
100 }
101 });
102
103 submitButtonPanel.add(new JLabel(" "), BorderLayout.WEST);
104 submitButtonPanel.add(new JLabel(" "), BorderLayout.EAST);
105
106 JPanel buttonFillerPanel = new JPanel();
107 buttonFillerPanel.setOpaque(false);
108 buttonFillerPanel.add(submitQuestionsButton);
109 submitButtonPanel.add(buttonFillerPanel, BorderLayout.CENTER);
110
111 JPanel t = new JPanel(new GridLayout(1, 1));
112 t.setOpaque(false);
113 t.add(submitButtonPanel);
114 return t;
115 }
116
117
118 /***
119 * Switches to the results panel
120 */
121 protected void showResultsCard() {
122 submitButtonPanel.setVisible(false);
123 if( cardPanel.getComponentCount() > 1 ) {
124 cardPanel.remove(1);
125 }
126 JPanel resultsCard = createResultsCard();
127 cardPanel.add(resultsCard, RESULT_CARD);
128
129 logger.info("COUNT " + cardPanel.getComponentCount() );
130 cardPanel.showNextCard();
131 }
132
133
134 /***
135 * Creates the results of the assessment
136 *
137 * @return results card
138 */
139 protected JPanel createResultsCard() {
140
141 SimpleGradientPanel resultsCard = new SimpleGradientPanel(new BorderLayout(0, 0));
142 resultsCard.setDirection(SimpleGradientPanel.VERTICAL);
143 resultsCard.setStartColor(PasColors.compactNavBackgroundStartColor);
144 resultsCard.setEndColor(PasColors.compactNavBackgroundEndColor);
145
146
147 JLabel finishedLabel = new JLabel(Messages
148 .getString("StudentAssessment.11"));
149 Font font = finishedLabel.getFont().deriveFont(Font.BOLD, 16f);
150 finishedLabel.setFont(font);
151 finishedLabel.setForeground(Color.white);
152 finishedLabel.setBorder(BorderFactory.createEmptyBorder(15, 2, 15, 2));
153
154 JPanel itemsPanel = new JPanel();
155
156 itemsPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
157 itemsPanel.setLayout(new BoxLayout(itemsPanel, BoxLayout.PAGE_AXIS));
158 itemsPanel.setOpaque(false);
159 List<ResponseDeclarationType> reponseDeclarations = assessment.getAssessmentItem().getResponseDeclaration();
160
161 for (ResponseDeclarationType rd : reponseDeclarations) {
162 String answerId = null;
163 String humanReadableAnswer = null;
164 String inlineFeedback = null;
165 boolean isCorrect = false;
166 ISock<String> sock = assessment.getResponseDeclarationToSocks().get(rd);
167 if (sock != null && !sock.isEmpty()) {
168 answerId = sock.peek();
169
170 humanReadableAnswer = JaxbQtiStep.lastAnswer(assessment.getResponseDeclarationToSocks(), rd);
171
172 BlockInteractionType bi = JaxbQtiStep.getInteractionByResponseDeclaration(assessment.getAssessmentItem().getItemBody().getBlockElementGroup(),rd);
173
174 if (bi instanceof ChoiceInteractionType) {
175 ChoiceInteractionType ci = (ChoiceInteractionType) bi;
176
177 List<SimpleChoiceType> simpleChoices = ci.getSimpleChoice();
178 for (SimpleChoiceType simpleChoice : simpleChoices) {
179 if( simpleChoice.getIdentifier().equals(answerId)) {
180
181 List<Serializable> choiceContent = simpleChoice.getContent();
182 for (Serializable serializable : choiceContent) {
183 if (serializable instanceof String) {
184 humanReadableAnswer = (String) serializable;
185 } else if( serializable instanceof JAXBElement) {
186 JAXBElement<FeedbackInlineType> jaxbElement = (JAXBElement<FeedbackInlineType>) serializable;
187 Object contentItem = jaxbElement.getValue();
188 if (contentItem instanceof FeedbackInlineType) {
189 FeedbackInlineType fb = (FeedbackInlineType) contentItem;
190 inlineFeedback = (String) fb.getContent().get(0);
191 }
192 }
193
194 }
195 isCorrect = JaxbQtiCreationUtils.isCorrectAnswer(rd, simpleChoice.getIdentifier());
196 }
197 }
198
199 } else {
200 humanReadableAnswer = answerId;
201 }
202 } else {
203 humanReadableAnswer = Messages
204 .getString("StudentAssessment.12");
205 }
206
207 JPanel aItemPanel = new JPanel(new BorderLayout(0, 0));
208 aItemPanel.setBackground(Color.BLACK);
209 aItemPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 1, 2));
210
211 JPanel promptPanel = new JPanel(new BorderLayout(0, 0));
212
213 JComponent editorPane = JaxbQtiStepUI.createPromptPanel( JaxbQtiStep.getInteractionByResponseDeclaration(assessment.getAssessmentItem().getItemBody().getBlockElementGroup(), rd));
214 editorPane.setBackground(PasColors.studentnAssessmentResultBackgroundColor);
215 promptPanel.add(editorPane, BorderLayout.CENTER);
216 promptPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
217 promptPanel.setBackground(PasColors.studentnAssessmentResultBackgroundColor);
218 aItemPanel.add(promptPanel, BorderLayout.NORTH);
219
220 JPanel answerPanel = new JPanel(new BorderLayout(0, 0));
221
222 humanReadableAnswer = wrapText(humanReadableAnswer,100.0);
223 JLabel answerLabel = getFeedbackLbl(humanReadableAnswer,100.0);
224 answerPanel.add(answerLabel,BorderLayout.CENTER);
225 JLabel feedbackLabel = new JLabel(inlineFeedback);
226
227
228 if( isCorrect ) {
229 feedbackLabel.setIcon(greenCheck);
230 } else {
231 feedbackLabel.setIcon(redX);
232 }
233
234 answerPanel.add(feedbackLabel, BorderLayout.SOUTH);
235
236
237 System.out.println(humanReadableAnswer);
238 answerPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
239 answerPanel.setBackground(Color.WHITE);
240 aItemPanel.add(answerPanel, BorderLayout.CENTER);
241
242
243
244
245
246 itemsPanel.add(aItemPanel);
247
248 }
249
250 JPanel innerPanel = new JPanel(new BorderLayout(0, 0));
251 innerPanel.setOpaque(false);
252 innerPanel.add(finishedLabel, BorderLayout.NORTH);
253
254 JScrollPane answerScroller = new JScrollPane();
255 answerScroller
256 .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
257 answerScroller
258 .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
259
260 JPanel temp = new JPanel(new BorderLayout(0, 0));
261 temp.add(itemsPanel, BorderLayout.NORTH);
262
263 temp.setOpaque(false);
264 temp.setBorder(BorderFactory.createLineBorder(Color.black, 2));
265 answerScroller.setViewportView(temp);
266 answerScroller.setBorder(BorderFactory.createEmptyBorder());
267 answerScroller.getViewport().setViewPosition(new java.awt.Point(0, 0));
268 answerScroller.getVerticalScrollBar().setValue(
269 answerScroller.getVerticalScrollBar().getMaximum());
270 answerScroller.setBackground(Color.RED);
271
272 resultsCard.add(answerScroller, BorderLayout.CENTER);
273
274 resultsCard.add(innerPanel, BorderLayout.NORTH);
275
276
277 JButton backButton = new JButton(Messages
278 .getString("StudentAssessment.13"));
279 CommonUI.makeEnhancedButton(backButton);
280 backButton.addActionListener(new ActionListener() {
281
282 public void actionPerformed(ActionEvent e) {
283
284
285 submitButtonPanel.setVisible(true);
286 cardPanel.showCard(cardPanel.getComponent(0));
287 }
288 });
289
290 JPanel buttonFillerPanel = new JPanel();
291 buttonFillerPanel.setOpaque(false);
292 buttonFillerPanel.add(backButton);
293
294 resultsCard.add(buttonFillerPanel, BorderLayout.SOUTH);
295
296 submitButtonPanel.setVisible(false);
297
298 return resultsCard;
299 }
300
301 /***
302 * @param textLen
303 * @param adjustedWidth
304 * @param inlineFeedbackText
305 * @return
306 */
307 private JLabel getFeedbackLbl(String inlineFeedbackText, double adjustedWidth) {
308 double factor;
309 int textlen = inlineFeedbackText.length();
310 JLabel fiLabel = new JLabel();
311 fiLabel.setLayout(new BoxLayout(fiLabel,BoxLayout.Y_AXIS));
312 fiLabel.setBorder(BorderFactory.createLineBorder(PasColors.blackColor,1));
313 Dimension thisDim = fiLabel.getPreferredSize();
314 factor = Math.ceil((double)textlen/adjustedWidth);
315 thisDim.setSize(adjustedWidth,15*factor);
316 fiLabel.setPreferredSize(thisDim);
317 fiLabel.setMaximumSize(fiLabel.getPreferredSize());
318 fiLabel.setMinimumSize(fiLabel.getPreferredSize());
319 fiLabel.setText(inlineFeedbackText);
320 fiLabel.setVisible(true);
321
322 fiLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
323 return fiLabel;
324 }
325
326 private String wrapText(String comments, double textWidth){
327 String initHtml = "<html><body>";
328 String finalHtml = "</body></html>";
329 String finalStr = "";
330 int lastIndex = 0;
331
332 comments.trim();
333
334 while(comments.length() > textWidth){
335 String substri = comments.substring(0,(int)textWidth);
336 lastIndex = substri.lastIndexOf(" ");
337 finalStr = finalStr + substri.substring(0,lastIndex) + "\n";
338 comments = comments.substring((int)lastIndex, comments.length());
339
340 }
341
342 return initHtml + finalStr + comments.substring(0,comments.length()) + finalHtml;
343 }
344
345 /***
346 * Saves all the responses to rims
347 *
348 * @param clientProperty
349 */
350 protected void doSave() {
351 JaxbQtiStep.saveAssessmentItem(assessment.getAssessmentItem(), assessment.getResponses(),
352 assessment.getResponseDeclarationToSocks());
353 ((JaxbQtiSelfAssessment)assessment).setHasSubmitted(true);
354 showResultsCard();
355 }
356
357 public JDialog showAssessmentPartsCheckDialog() {
358 final PasMessageDialogUI ui = new PasMessageDialogUI(true);
359
360
361 ui
362 .setMessage("You need to complete all assessment questions before saving");
363 return PasDialogManager.showPasMessageDialog("Warning", ui
364 .createDialogPanel(), null, true);
365 }
366
367 public JDialog showAreYouSureCheckDialog(AbstractAction action) {
368 final PasStudentAssessmentConfirmDialogUI ui = new PasStudentAssessmentConfirmDialogUI(action);
369
370 ui
371 .setMessage("Are all the questions answered? Once submitted all answers are final.");
372 return PasDialogManager.showPasMessageDialog("Warning", ui
373 .createDialogPanel(), null, true);
374 }
375
376
377 /***
378 * @return the leaveAction
379 */
380 public LeaveStudentAssessmentAction getLeaveAction() {
381 return leaveAction;
382 }
383
384 /***
385 * @param projectFrame
386 * @return
387 */
388 public JDialog showSubmitDialog(JFrame projectFrame) {
389
390 leaveAction = new LeaveStudentAssessmentAction(
391 Messages.getString("StudentAssessment.51"));
392
393 PasSaveStudentAssesmentDialogUI ui = new PasSaveStudentAssesmentDialogUI(
394 leaveAction);
395 ui.setMessage(Messages.getString("StudentAssessment.52"));
396
397
398
399
400
401 CustomDialog dialog = PasDialogManager
402 .showPasMessageDialog(
403 Messages.getString("StudentAssessment.53"), ui.createDialogPanel(), projectFrame, true);
404
405
406
407
408
409
410
411
412
413
414
415 return dialog;
416
417
418
419
420 }
421
422
423
424 }