-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
FeedbackMsqQuestionDetails.java
745 lines (635 loc) · 34.9 KB
/
FeedbackMsqQuestionDetails.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
package teammates.common.datatransfer.questions;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import teammates.common.datatransfer.FeedbackParticipantType;
import teammates.common.datatransfer.FeedbackSessionResultsBundle;
import teammates.common.datatransfer.TeamDetailsBundle;
import teammates.common.datatransfer.attributes.FeedbackQuestionAttributes;
import teammates.common.datatransfer.attributes.FeedbackResponseAttributes;
import teammates.common.datatransfer.attributes.InstructorAttributes;
import teammates.common.datatransfer.attributes.StudentAttributes;
import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.util.Assumption;
import teammates.common.util.Const;
import teammates.common.util.HttpRequestHelper;
import teammates.common.util.SanitizationHelper;
import teammates.common.util.StringHelper;
import teammates.common.util.Templates;
import teammates.common.util.Templates.FeedbackQuestion.FormTemplates;
import teammates.common.util.Templates.FeedbackQuestion.Slots;
import teammates.logic.core.CoursesLogic;
import teammates.logic.core.InstructorsLogic;
import teammates.logic.core.StudentsLogic;
import teammates.ui.template.InstructorFeedbackResultsResponseRow;
public class FeedbackMsqQuestionDetails extends FeedbackQuestionDetails {
private int numOfMsqChoices;
private List<String> msqChoices;
private boolean otherEnabled;
private FeedbackParticipantType generateOptionsFor;
private int maxSelectableChoices;
private int minSelectableChoices;
private StudentAttributes studentDoingQuestion;
public FeedbackMsqQuestionDetails() {
super(FeedbackQuestionType.MSQ);
this.numOfMsqChoices = 0;
this.msqChoices = new ArrayList<>();
this.otherEnabled = false;
this.generateOptionsFor = FeedbackParticipantType.NONE;
this.maxSelectableChoices = Integer.MIN_VALUE;
this.minSelectableChoices = Integer.MIN_VALUE;
}
@Override
public boolean extractQuestionDetails(
Map<String, String[]> requestParameters,
FeedbackQuestionType questionType) {
int numOfMsqChoices = 0;
List<String> msqChoices = new LinkedList<>();
boolean msqOtherEnabled = false;
String otherOptionFlag =
HttpRequestHelper.getValueFromParamMap(requestParameters,
Const.ParamsNames.FEEDBACK_QUESTION_MSQOTHEROPTIONFLAG);
if ("on".equals(otherOptionFlag)) {
msqOtherEnabled = true;
}
int msqMaxSelectableChoices = Integer.MIN_VALUE;
int msqMinSelectableChoices = Integer.MIN_VALUE;
String maxSelectableChoicesParam = HttpRequestHelper.getValueFromParamMap(requestParameters,
Const.ParamsNames.FEEDBACK_QUESTION_MSQ_MAX_SELECTABLE_CHOICES);
String minSelectableChoicesParam = HttpRequestHelper.getValueFromParamMap(requestParameters,
Const.ParamsNames.FEEDBACK_QUESTION_MSQ_MIN_SELECTABLE_CHOICES);
if (maxSelectableChoicesParam != null) {
msqMaxSelectableChoices = Integer.parseInt(maxSelectableChoicesParam);
}
if (minSelectableChoicesParam != null) {
msqMinSelectableChoices = Integer.parseInt(minSelectableChoicesParam);
}
this.maxSelectableChoices = msqMaxSelectableChoices;
this.minSelectableChoices = msqMinSelectableChoices;
String generatedMsqOptions =
HttpRequestHelper.getValueFromParamMap(requestParameters,
Const.ParamsNames.FEEDBACK_QUESTION_MSQ_GENERATED_OPTIONS);
if (generatedMsqOptions.equals(FeedbackParticipantType.NONE.toString())) {
String numMsqChoicesCreatedString =
HttpRequestHelper.getValueFromParamMap(requestParameters,
Const.ParamsNames.FEEDBACK_QUESTION_NUMBEROFCHOICECREATED);
Assumption.assertNotNull("Null number of choice for MSQ", numMsqChoicesCreatedString);
int numMsqChoicesCreated = Integer.parseInt(numMsqChoicesCreatedString);
for (int i = 0; i < numMsqChoicesCreated; i++) {
String msqChoice =
HttpRequestHelper.getValueFromParamMap(
requestParameters, Const.ParamsNames.FEEDBACK_QUESTION_MSQCHOICE + "-" + i);
if (msqChoice != null && !msqChoice.trim().isEmpty()) {
msqChoices.add(msqChoice);
numOfMsqChoices++;
}
}
setMsqQuestionDetails(numOfMsqChoices, msqChoices, msqOtherEnabled);
} else {
String courseId = HttpRequestHelper.getValueFromParamMap(requestParameters, Const.ParamsNames.COURSE_ID);
setMsqQuestionDetails(FeedbackParticipantType.valueOf(generatedMsqOptions), courseId);
}
return true;
}
private void setMsqQuestionDetails(int numOfMsqChoices, List<String> msqChoices, boolean otherEnabled) {
this.numOfMsqChoices = numOfMsqChoices;
this.msqChoices = msqChoices;
this.otherEnabled = otherEnabled;
this.generateOptionsFor = FeedbackParticipantType.NONE;
}
private void setMsqQuestionDetails(FeedbackParticipantType generateOptionsFor, String courseId) {
this.msqChoices = new ArrayList<>();
this.otherEnabled = false;
this.generateOptionsFor = generateOptionsFor;
if (generateOptionsFor.equals(FeedbackParticipantType.STUDENTS_EXCLUDING_SELF)) {
this.numOfMsqChoices = generateNumOfChoicesForStudentsExcludingSelf(courseId);
} else {
this.numOfMsqChoices = generateOptionList(courseId).size();
}
Assumption.assertTrue("Can only generate students, students (excluding self), teams or instructors",
generateOptionsFor == FeedbackParticipantType.STUDENTS
|| generateOptionsFor == FeedbackParticipantType.STUDENTS_EXCLUDING_SELF
|| generateOptionsFor == FeedbackParticipantType.TEAMS
|| generateOptionsFor == FeedbackParticipantType.INSTRUCTORS);
}
@Override
public String getQuestionTypeDisplayName() {
return Const.FeedbackQuestionTypeNames.MSQ;
}
@Override
public List<String> getInstructions() {
return null;
}
public boolean getOtherEnabled() {
return otherEnabled;
}
@Override
public boolean isChangesRequiresResponseDeletion(FeedbackQuestionDetails newDetails) {
FeedbackMsqQuestionDetails newMsqDetails = (FeedbackMsqQuestionDetails) newDetails;
if (this.numOfMsqChoices != newMsqDetails.numOfMsqChoices
|| !this.msqChoices.containsAll(newMsqDetails.msqChoices)
|| !newMsqDetails.msqChoices.containsAll(this.msqChoices)) {
return true;
}
if (this.generateOptionsFor != newMsqDetails.generateOptionsFor) {
return true;
}
if (this.maxSelectableChoices == Integer.MIN_VALUE && newMsqDetails.maxSelectableChoices != Integer.MIN_VALUE) {
// Delete responses if max selectable restriction is newly added
return true;
}
if (this.minSelectableChoices == Integer.MIN_VALUE && newMsqDetails.minSelectableChoices != Integer.MIN_VALUE) {
// Delete responses if min selectable restriction is newly added
return true;
}
if (this.minSelectableChoices != Integer.MIN_VALUE && newMsqDetails.minSelectableChoices != Integer.MIN_VALUE
&& this.minSelectableChoices < newMsqDetails.minSelectableChoices) {
// A more strict min selectable choices restriction is placed
return true;
}
if (this.maxSelectableChoices != Integer.MIN_VALUE && newMsqDetails.maxSelectableChoices != Integer.MIN_VALUE
&& this.maxSelectableChoices > newMsqDetails.maxSelectableChoices) {
// A more strict max selectable choices restriction is placed
return true;
}
return this.otherEnabled != newMsqDetails.otherEnabled;
}
@Override
public String getQuestionWithExistingResponseSubmissionFormHtml(
boolean sessionIsOpen, int qnIdx, int responseIdx, String courseId,
int totalNumRecipients, FeedbackResponseDetails existingResponseDetails, StudentAttributes student) {
studentDoingQuestion = student;
FeedbackMsqResponseDetails existingMsqResponse = (FeedbackMsqResponseDetails) existingResponseDetails;
List<String> choices = generateOptionList(courseId);
StringBuilder optionListHtml = new StringBuilder();
String optionFragmentTemplate = FormTemplates.MSQ_SUBMISSION_FORM_OPTIONFRAGMENT;
Boolean isOtherSelected = existingMsqResponse.isOtherOptionAnswer();
for (int i = 0; i < choices.size(); i++) {
String optionFragment =
Templates.populateTemplate(optionFragmentTemplate,
Slots.QUESTION_INDEX, Integer.toString(qnIdx),
Slots.RESPONSE_INDEX, Integer.toString(responseIdx),
Slots.DISABLED, sessionIsOpen ? "" : "disabled",
Slots.CHECKED, existingMsqResponse.contains(choices.get(i)) ? "checked" : "",
Slots.FEEDBACK_RESPONSE_TEXT, Const.ParamsNames.FEEDBACK_RESPONSE_TEXT,
Slots.MSQ_CHOICE_VALUE, SanitizationHelper.sanitizeForHtml(choices.get(i)),
Slots.MSQ_CHOICE_TEXT, SanitizationHelper.sanitizeForHtml(choices.get(i)));
optionListHtml.append(optionFragment).append(System.lineSeparator());
}
if (otherEnabled) {
String otherOptionFragmentTemplate = FormTemplates.MSQ_SUBMISSION_FORM_OTHEROPTIONFRAGMENT;
String otherOptionFragment =
Templates.populateTemplate(otherOptionFragmentTemplate,
Slots.QUESTION_INDEX, Integer.toString(qnIdx),
Slots.RESPONSE_INDEX, Integer.toString(responseIdx),
Slots.DISABLED, sessionIsOpen ? "" : "disabled",
Slots.TEXT_DISABLED, sessionIsOpen && isOtherSelected ? "" : "disabled",
Slots.CHECKED, isOtherSelected ? "checked" : "",
Slots.FEEDBACK_RESPONSE_TEXT, Const.ParamsNames.FEEDBACK_RESPONSE_TEXT,
Slots.MSQ_PARAM_IS_OTHER_OPTION_ANSWER,
Const.ParamsNames.FEEDBACK_QUESTION_MSQ_ISOTHEROPTIONANSWER,
Slots.MSQ_CHOICE_VALUE,
SanitizationHelper.sanitizeForHtml(existingMsqResponse.getOtherFieldContent()),
Slots.MSQ_OTHER_OPTION_ANSWER, isOtherSelected ? "1" : "0");
optionListHtml.append(otherOptionFragment).append(System.lineSeparator());
}
boolean isMinSelectableChoicesEnabled = minSelectableChoices != Integer.MIN_VALUE;
if (!isMinSelectableChoicesEnabled) {
// additional checkbox for user to submit a blank response ("None of the above")
String optionFragment =
Templates.populateTemplate(optionFragmentTemplate,
Slots.QUESTION_INDEX, Integer.toString(qnIdx),
Slots.RESPONSE_INDEX, Integer.toString(responseIdx),
Slots.DISABLED, sessionIsOpen ? "" : "disabled",
Slots.CHECKED, existingMsqResponse.contains("") ? "checked" : "",
Slots.FEEDBACK_RESPONSE_TEXT, Const.ParamsNames.FEEDBACK_RESPONSE_TEXT,
Slots.MSQ_CHOICE_VALUE, "",
Slots.MSQ_CHOICE_TEXT, "<i>" + Const.NONE_OF_THE_ABOVE + "</i>");
optionListHtml.append(optionFragment).append(System.lineSeparator());
}
boolean isMaxSelectableChoicesEnabled = maxSelectableChoices != Integer.MIN_VALUE;
return Templates.populateTemplate(
FormTemplates.MSQ_SUBMISSION_FORM,
Slots.MSQ_SUBMISSION_FORM_OPTION_FRAGMENTS, optionListHtml.toString(),
Slots.QUESTION_INDEX, Integer.toString(qnIdx),
Slots.MSQ_IS_MAX_SELECTABLE_CHOICES_ENABLED, isMaxSelectableChoicesEnabled ? "" : "disabled",
Slots.MSQ_IS_MIN_SELECTABLE_CHOICES_ENABLED, isMinSelectableChoicesEnabled ? "" : "disabled",
Slots.MSQ_DISPLAY_MAX_SELECTABLE_CHOICES_HINT, isMaxSelectableChoicesEnabled ? "" : "hidden",
Slots.MSQ_DISPLAY_MIN_SELECTABLE_CHOICES_HINT, isMinSelectableChoicesEnabled ? "" : "hidden",
Slots.MSQ_PARAM_MAX_SELECTABLE_CHOICES, Const.ParamsNames.FEEDBACK_QUESTION_MSQ_MAX_SELECTABLE_CHOICES,
Slots.MSQ_PARAM_MIN_SELECTABLE_CHOICES, Const.ParamsNames.FEEDBACK_QUESTION_MSQ_MIN_SELECTABLE_CHOICES,
Slots.MSQ_MAX_SELECTABLE_CHOICES,
isMaxSelectableChoicesEnabled ? Integer.toString(maxSelectableChoices) : "-1",
Slots.MSQ_MIN_SELECTABLE_CHOICES,
isMinSelectableChoicesEnabled ? Integer.toString(minSelectableChoices) : "-1");
}
@Override
public String getQuestionWithoutExistingResponseSubmissionFormHtml(
boolean sessionIsOpen, int qnIdx, int responseIdx, String courseId, int totalNumRecipients,
StudentAttributes student) {
studentDoingQuestion = student;
List<String> choices = generateOptionList(courseId);
StringBuilder optionListHtml = new StringBuilder();
String optionFragmentTemplate = FormTemplates.MSQ_SUBMISSION_FORM_OPTIONFRAGMENT;
for (int i = 0; i < choices.size(); i++) {
String optionFragment =
Templates.populateTemplate(optionFragmentTemplate,
Slots.QUESTION_INDEX, Integer.toString(qnIdx),
Slots.RESPONSE_INDEX, Integer.toString(responseIdx),
Slots.DISABLED, sessionIsOpen ? "" : "disabled",
Slots.CHECKED, "",
Slots.FEEDBACK_RESPONSE_TEXT, Const.ParamsNames.FEEDBACK_RESPONSE_TEXT,
Slots.MSQ_CHOICE_VALUE, SanitizationHelper.sanitizeForHtml(choices.get(i)),
Slots.MSQ_CHOICE_TEXT, SanitizationHelper.sanitizeForHtml(choices.get(i)));
optionListHtml.append(optionFragment);
optionListHtml.append(System.lineSeparator());
}
if (otherEnabled) {
String otherOptionFragmentTemplate = FormTemplates.MSQ_SUBMISSION_FORM_OTHEROPTIONFRAGMENT;
String otherOptionFragment =
Templates.populateTemplate(otherOptionFragmentTemplate,
Slots.QUESTION_INDEX, Integer.toString(qnIdx),
Slots.RESPONSE_INDEX, Integer.toString(responseIdx),
Slots.DISABLED, sessionIsOpen ? "" : "disabled",
Slots.TEXT_DISABLED, "disabled",
Slots.CHECKED, "",
Slots.FEEDBACK_RESPONSE_TEXT, Const.ParamsNames.FEEDBACK_RESPONSE_TEXT,
Slots.MSQ_PARAM_IS_OTHER_OPTION_ANSWER,
Const.ParamsNames.FEEDBACK_QUESTION_MSQ_ISOTHEROPTIONANSWER,
Slots.MSQ_CHOICE_VALUE, "",
Slots.MSQ_OTHER_OPTION_ANSWER, "0");
optionListHtml.append(otherOptionFragment).append(System.lineSeparator());
}
boolean isMinSelectableChoicesEnabled = minSelectableChoices != Integer.MIN_VALUE;
if (!isMinSelectableChoicesEnabled) {
// additional checkbox for user to submit a blank response ("None of the above")
String optionFragment =
Templates.populateTemplate(optionFragmentTemplate,
Slots.QUESTION_INDEX, Integer.toString(qnIdx),
Slots.RESPONSE_INDEX, Integer.toString(responseIdx),
Slots.DISABLED, sessionIsOpen ? "" : "disabled",
Slots.CHECKED, "",
Slots.FEEDBACK_RESPONSE_TEXT, Const.ParamsNames.FEEDBACK_RESPONSE_TEXT,
Slots.MSQ_CHOICE_VALUE, "",
Slots.MSQ_CHOICE_TEXT, "<i>" + Const.NONE_OF_THE_ABOVE + "</i>");
optionListHtml.append(optionFragment).append(System.lineSeparator());
}
boolean isMaxSelectableChoicesEnabled = maxSelectableChoices != Integer.MIN_VALUE;
return Templates.populateTemplate(
FormTemplates.MSQ_SUBMISSION_FORM,
Slots.MSQ_SUBMISSION_FORM_OPTION_FRAGMENTS, optionListHtml.toString(),
Slots.QUESTION_INDEX, Integer.toString(qnIdx),
Slots.MSQ_IS_MAX_SELECTABLE_CHOICES_ENABLED, isMaxSelectableChoicesEnabled ? "" : "disabled",
Slots.MSQ_IS_MIN_SELECTABLE_CHOICES_ENABLED, isMinSelectableChoicesEnabled ? "" : "disabled",
Slots.MSQ_DISPLAY_MAX_SELECTABLE_CHOICES_HINT, isMaxSelectableChoicesEnabled ? "" : "hidden",
Slots.MSQ_DISPLAY_MIN_SELECTABLE_CHOICES_HINT, isMinSelectableChoicesEnabled ? "" : "hidden",
Slots.MSQ_PARAM_MAX_SELECTABLE_CHOICES, Const.ParamsNames.FEEDBACK_QUESTION_MSQ_MAX_SELECTABLE_CHOICES,
Slots.MSQ_PARAM_MIN_SELECTABLE_CHOICES, Const.ParamsNames.FEEDBACK_QUESTION_MSQ_MIN_SELECTABLE_CHOICES,
Slots.MSQ_MAX_SELECTABLE_CHOICES,
isMaxSelectableChoicesEnabled ? Integer.toString(maxSelectableChoices) : "-1",
Slots.MSQ_MIN_SELECTABLE_CHOICES,
isMinSelectableChoicesEnabled ? Integer.toString(minSelectableChoices) : "-1");
}
/**
* Get the number of MSQ choices excluding self.
* @return number of MSQ choices generated.
*/
private int generateNumOfChoicesForStudentsExcludingSelf(String courseId) {
List<StudentAttributes> studentList = StudentsLogic.inst().getStudentsForCourse(courseId);
return studentList.size() - 1;
}
private List<String> generateOptionList(String courseId) {
List<String> optionList = new ArrayList<>();
switch (generateOptionsFor) {
case NONE:
optionList = msqChoices;
break;
case STUDENTS:
//fallthrough
case STUDENTS_EXCLUDING_SELF:
List<StudentAttributes> studentList = StudentsLogic.inst().getStudentsForCourse(courseId);
if (generateOptionsFor == FeedbackParticipantType.STUDENTS_EXCLUDING_SELF) {
studentList.removeIf(studentInList -> studentInList.email.equals(studentDoingQuestion.email));
}
for (StudentAttributes student : studentList) {
optionList.add(student.name + " (" + student.team + ")");
}
optionList.sort(null);
break;
case TEAMS:
try {
List<TeamDetailsBundle> teamList =
CoursesLogic.inst().getTeamsForCourse(courseId);
for (TeamDetailsBundle team : teamList) {
optionList.add(team.name);
}
optionList.sort(null);
} catch (EntityDoesNotExistException e) {
Assumption.fail("Course disappeared");
}
break;
case INSTRUCTORS:
List<InstructorAttributes> instructorList =
InstructorsLogic.inst().getInstructorsForCourse(
courseId);
for (InstructorAttributes instructor : instructorList) {
optionList.add(instructor.name);
}
optionList.sort(null);
break;
default:
Assumption.fail("Trying to generate options for neither students, teams nor instructors");
break;
}
return optionList;
}
@Override
public String getQuestionSpecificEditFormHtml(int questionNumber) {
StringBuilder optionListHtml = new StringBuilder();
String optionFragmentTemplate = FormTemplates.MSQ_EDIT_FORM_OPTIONFRAGMENT;
for (int i = 0; i < msqChoices.size(); i++) {
String optionFragment =
Templates.populateTemplate(optionFragmentTemplate,
Slots.ITERATOR, Integer.toString(i),
Slots.MSQ_CHOICE_VALUE, SanitizationHelper.sanitizeForHtml(msqChoices.get(i)),
Slots.MSQ_PARAM_CHOICE, Const.ParamsNames.FEEDBACK_QUESTION_MSQCHOICE);
optionListHtml.append(optionFragment).append(System.lineSeparator());
}
boolean isMaxSelectableChoicesDisabled = maxSelectableChoices == Integer.MIN_VALUE;
boolean isMinSelectableChoicesDisabled = minSelectableChoices == Integer.MIN_VALUE;
return Templates.populateTemplate(
FormTemplates.MSQ_EDIT_FORM,
Slots.MSQ_EDIT_FORM_OPTION_FRAGMENTS, optionListHtml.toString(),
Slots.QUESTION_NUMBER, Integer.toString(questionNumber),
Slots.NUMBER_OF_CHOICE_CREATED, Const.ParamsNames.FEEDBACK_QUESTION_NUMBEROFCHOICECREATED,
Slots.MSQ_NUMBER_OF_CHOICES, Integer.toString(numOfMsqChoices),
Slots.CHECKED_OTHER_OPTION_ENABLED, otherEnabled ? "checked" : "",
Slots.MSQ_PARAM_OTHER_OPTION, Const.ParamsNames.FEEDBACK_QUESTION_MSQOTHEROPTION,
Slots.MSQ_PARAM_OTHER_OPTION_FLAG, Const.ParamsNames.FEEDBACK_QUESTION_MSQOTHEROPTIONFLAG,
Slots.MSQ_CHECKED_GENERATED_OPTIONS, generateOptionsFor == FeedbackParticipantType.NONE ? "" : "checked",
Slots.MSQ_GENERATED_OPTIONS, Const.ParamsNames.FEEDBACK_QUESTION_MSQ_GENERATED_OPTIONS,
Slots.GENERATE_OPTIONS_FOR_VALUE, generateOptionsFor.toString(),
Slots.STUDENT_SELECTED, generateOptionsFor == FeedbackParticipantType.STUDENTS ? "selected" : "",
Slots.STUDENTS_TO_STRING, FeedbackParticipantType.STUDENTS.toString(),
Slots.STUDENT_EXCLUDING_SELF_SELECTED,
generateOptionsFor == FeedbackParticipantType.STUDENTS_EXCLUDING_SELF ? "selected" : "",
Slots.STUDENTS_EXCLUDING_SELF_TO_STRING, FeedbackParticipantType.STUDENTS_EXCLUDING_SELF.toString(),
Slots.TEAM_SELECTED, generateOptionsFor == FeedbackParticipantType.TEAMS ? "selected" : "",
Slots.TEAMS_TO_STRING, FeedbackParticipantType.TEAMS.toString(),
Slots.INSTRUCTOR_SELECTED, generateOptionsFor == FeedbackParticipantType.INSTRUCTORS ? "selected" : "",
Slots.INSTRUCTORS_TO_STRING, FeedbackParticipantType.INSTRUCTORS.toString(),
Slots.MSQ_IS_MAX_SELECTABLE_CHOICES_ENABLED, isMaxSelectableChoicesDisabled ? "" : "checked",
Slots.MSQ_IS_MIN_SELECTABLE_CHOICES_ENABLED, isMinSelectableChoicesDisabled ? "" : "checked",
Slots.MSQ_PARAM_ENABLED_MAX_SELECTABLE_CHOICES,
Const.ParamsNames.FEEDBACK_QUESTION_MSQ_ENABLE_MAX_SELECTABLE_CHOICES,
Slots.MSQ_PARAM_ENABLED_MIN_SELECTABLE_CHOICES,
Const.ParamsNames.FEEDBACK_QUESTION_MSQ_ENABLE_MIN_SELECTABLE_CHOICES,
Slots.MSQ_PARAM_MAX_SELECTABLE_CHOICES, Const.ParamsNames.FEEDBACK_QUESTION_MSQ_MAX_SELECTABLE_CHOICES,
Slots.MSQ_PARAM_MIN_SELECTABLE_CHOICES, Const.ParamsNames.FEEDBACK_QUESTION_MSQ_MIN_SELECTABLE_CHOICES,
Slots.MSQ_MAX_SELECTABLE_CHOICES,
isMaxSelectableChoicesDisabled ? "2" : Integer.toString(maxSelectableChoices),
Slots.MSQ_MIN_SELECTABLE_CHOICES,
isMinSelectableChoicesDisabled ? "1" : Integer.toString(minSelectableChoices));
}
@Override
public String getNewQuestionSpecificEditFormHtml() {
// Add two empty options by default
numOfMsqChoices = 2;
msqChoices.add("");
msqChoices.add("");
return "<div id=\"msqForm\">"
+ getQuestionSpecificEditFormHtml(-1)
+ "</div>";
}
@Override
public String getQuestionAdditionalInfoHtml(int questionNumber, String additionalInfoId) {
StringBuilder optionListHtml = new StringBuilder(200);
String optionFragmentTemplate = FormTemplates.MSQ_ADDITIONAL_INFO_FRAGMENT;
if (generateOptionsFor != FeedbackParticipantType.NONE) {
String optionHelpText = String.format(
"<br>The options for this question is automatically generated from the list of all %s in this course.",
generateOptionsFor.toString().toLowerCase());
optionListHtml.append(optionHelpText);
}
if (numOfMsqChoices > 0) {
optionListHtml.append("<ul style=\"list-style-type: disc;margin-left: 20px;\" >");
for (int i = 0; i < numOfMsqChoices; i++) {
String optionFragment =
Templates.populateTemplate(optionFragmentTemplate,
Slots.MSQ_CHOICE_VALUE, SanitizationHelper.sanitizeForHtml(msqChoices.get(i)));
optionListHtml.append(optionFragment);
}
if (otherEnabled) {
String optionFragment =
Templates.populateTemplate(optionFragmentTemplate, Slots.MSQ_CHOICE_VALUE, "Other");
optionListHtml.append(optionFragment);
}
optionListHtml.append("</ul>");
}
String additionalInfo = Templates.populateTemplate(
FormTemplates.MSQ_ADDITIONAL_INFO,
Slots.QUESTION_TYPE_NAME, this.getQuestionTypeDisplayName(),
Slots.MSQ_ADDITIONAL_INFO_FRAGMENTS, optionListHtml.toString());
return Templates.populateTemplate(
FormTemplates.FEEDBACK_QUESTION_ADDITIONAL_INFO,
Slots.MORE, "[more]",
Slots.LESS, "[less]",
Slots.QUESTION_NUMBER, Integer.toString(questionNumber),
Slots.ADDITIONAL_INFO_ID, additionalInfoId,
Slots.QUESTION_ADDITIONAL_INFO, additionalInfo);
}
@Override
public String getQuestionResultStatisticsHtml(List<FeedbackResponseAttributes> responses,
FeedbackQuestionAttributes question,
String studentEmail,
FeedbackSessionResultsBundle bundle,
String view) {
if ("student".equals(view) || responses.isEmpty()) {
return "";
}
Map<String, Integer> answerFrequency = new LinkedHashMap<>();
int numChoicesSelected = getNumberOfResponses(responses, answerFrequency);
if (numChoicesSelected == -1) {
return "";
}
DecimalFormat df = new DecimalFormat("#.##");
StringBuilder fragments = new StringBuilder();
answerFrequency.forEach((key, value) ->
fragments.append(Templates.populateTemplate(FormTemplates.MCQ_RESULT_STATS_OPTIONFRAGMENT,
Slots.MCQ_CHOICE_VALUE, key,
Slots.COUNT, value.toString(),
Slots.PERCENTAGE,
df.format(100 * divideOrReturnZero(value, numChoicesSelected)))));
//Use same template as MCQ for now, until they need to be different.
return Templates.populateTemplate(FormTemplates.MCQ_RESULT_STATS, Slots.FRAGMENTS, fragments.toString());
}
@Override
public String getQuestionResultStatisticsCsv(
List<FeedbackResponseAttributes> responses,
FeedbackQuestionAttributes question,
FeedbackSessionResultsBundle bundle) {
if (responses.isEmpty()) {
return "";
}
Map<String, Integer> answerFrequency = new LinkedHashMap<>();
int numChoicesSelected = getNumberOfResponses(responses, answerFrequency);
if (numChoicesSelected == -1) {
return "";
}
DecimalFormat df = new DecimalFormat("#.##");
StringBuilder fragments = new StringBuilder();
answerFrequency.forEach((key, value) -> fragments.append(SanitizationHelper.sanitizeForCsv(key) + ','
+ value.toString() + ','
+ df.format(100 * divideOrReturnZero(value, numChoicesSelected))
+ System.lineSeparator()));
return "Choice, Response Count, Percentage" + System.lineSeparator()
+ fragments + System.lineSeparator();
}
@Override
public String getCsvHeader() {
List<String> sanitizedChoices = SanitizationHelper.sanitizeListForCsv(msqChoices);
return "Feedbacks:," + StringHelper.toString(sanitizedChoices, ",");
}
@Override
public String getQuestionTypeChoiceOption() {
return "<li data-questiontype = \"MSQ\"><a href=\"javascript:;\">"
+ Const.FeedbackQuestionTypeNames.MSQ + "</a></li>";
}
@Override
public List<String> validateQuestionDetails() {
List<String> errors = new ArrayList<>();
if (generateOptionsFor == FeedbackParticipantType.NONE
&& numOfMsqChoices < Const.FeedbackQuestion.MSQ_MIN_NUM_OF_CHOICES) {
errors.add(Const.FeedbackQuestion.MSQ_ERROR_NOT_ENOUGH_CHOICES
+ Const.FeedbackQuestion.MSQ_MIN_NUM_OF_CHOICES + ".");
}
//TODO: check that msq options do not repeat. needed?
boolean isMaxSelectableChoicesEnabled = maxSelectableChoices != Integer.MIN_VALUE;
boolean isMinSelectableChoicesEnabled = minSelectableChoices != Integer.MIN_VALUE;
if (isMaxSelectableChoicesEnabled) {
if (numOfMsqChoices < maxSelectableChoices) {
errors.add(Const.FeedbackQuestion.MSQ_ERROR_MAX_SELECTABLE_EXCEEDED_TOTAL);
} else if (maxSelectableChoices < 2) {
errors.add(Const.FeedbackQuestion.MSQ_ERROR_MIN_FOR_MAX_SELECTABLE_CHOICES);
}
}
if (isMinSelectableChoicesEnabled && minSelectableChoices < 1) {
errors.add(Const.FeedbackQuestion.MSQ_ERROR_MIN_FOR_MIN_SELECTABLE_CHOICES);
}
if (isMaxSelectableChoicesEnabled && isMinSelectableChoicesEnabled
&& minSelectableChoices > maxSelectableChoices) {
errors.add(Const.FeedbackQuestion.MSQ_ERROR_MIN_SELECTABLE_EXCEEDED_MAX_SELECTABLE);
}
return errors;
}
@Override
public List<String> validateResponseAttributes(
List<FeedbackResponseAttributes> responses,
int numRecipients) {
List<String> errors = new ArrayList<>();
for (FeedbackResponseAttributes response : responses) {
FeedbackMsqResponseDetails frd = (FeedbackMsqResponseDetails) response.getResponseDetails();
if (!otherEnabled) {
List<String> validChoices = msqChoices;
validChoices.add("");
if (!validChoices.containsAll(frd.answers) && generateOptionsFor == FeedbackParticipantType.NONE) {
errors.add(frd.getAnswerString() + Const.FeedbackQuestion.MSQ_ERROR_INVALID_OPTION);
}
}
if (maxSelectableChoices != Integer.MIN_VALUE && frd.answers.size() > maxSelectableChoices) {
errors.add(Const.FeedbackQuestion.MSQ_ERROR_MAX_SELECTABLE_EXCEEDED_TOTAL
+ maxSelectableChoices);
}
}
return errors;
}
/**
* Checks if the question has been skipped.
* MSQ allows a blank response, as that represents "None of the above"
*/
@Override
public boolean isQuestionSkipped(String[] answer) {
return answer == null;
}
@Override
public Comparator<InstructorFeedbackResultsResponseRow> getResponseRowsSortOrder() {
return null;
}
@Override
public String validateGiverRecipientVisibility(FeedbackQuestionAttributes feedbackQuestionAttributes) {
return "";
}
public int getNumOfMsqChoices() {
return numOfMsqChoices;
}
public List<String> getMsqChoices() {
return msqChoices;
}
/**
* Returns maximum selectable choices for this MSQ question.
* @return Integer.MIN_VALUE if not set by instructor.
*/
public int getMaxSelectableChoices() {
return maxSelectableChoices;
}
/**
* Getting number of responses.
* @return -1 if there is no empty response else number of response.
*/
private int getNumberOfResponses(
List<FeedbackResponseAttributes> responses, Map<String, Integer> answerFrequency) {
boolean isContainsNonEmptyResponse = false; // we will only show stats if there is at least one nonempty response
for (String option : msqChoices) {
answerFrequency.put(option, 0);
}
if (otherEnabled) {
answerFrequency.put("Other", 0);
}
int numChoicesSelected = 0;
for (FeedbackResponseAttributes response : responses) {
List<String> answerStrings =
((FeedbackMsqResponseDetails) response.getResponseDetails()).getAnswerStrings();
boolean isOtherOptionAnswer =
((FeedbackMsqResponseDetails) response.getResponseDetails()).isOtherOptionAnswer();
String otherAnswer = "";
if (isOtherOptionAnswer) {
answerFrequency.put("Other", answerFrequency.getOrDefault("Other", 0) + 1);
numChoicesSelected++;
// remove other answer temporarily to calculate stats for other options
otherAnswer = answerStrings.get(answerStrings.size() - 1);
answerStrings.remove(otherAnswer);
}
int numNonEmptyChoicesSelected = getNumberOfNonEmptyResponsesOfQuestion(answerStrings, answerFrequency);
if (numNonEmptyChoicesSelected > 0) {
isContainsNonEmptyResponse = true;
numChoicesSelected += numNonEmptyChoicesSelected;
}
// restore other answer if any
if (isOtherOptionAnswer) {
answerStrings.add(otherAnswer);
}
}
if (!isContainsNonEmptyResponse) {
return -1;
}
return numChoicesSelected;
}
private int getNumberOfNonEmptyResponsesOfQuestion(List<String> answerStrings, Map<String, Integer> answerFrequency) {
int numChoices = 0;
for (String answerString : answerStrings) {
if (answerString.isEmpty()) {
continue;
}
numChoices++;
answerFrequency.put(answerString, answerFrequency.getOrDefault(answerString, 0) + 1);
}
return numChoices;
}
private double divideOrReturnZero(double numerator, int denominator) {
return (denominator == 0) ? 0 : numerator / denominator;
}
}