-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
FeedbackQuestionsLogic.java
650 lines (574 loc) · 28.3 KB
/
FeedbackQuestionsLogic.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
package teammates.sqllogic.core;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import teammates.common.datatransfer.FeedbackParticipantType;
import teammates.common.datatransfer.FeedbackQuestionRecipient;
import teammates.common.datatransfer.SqlCourseRoster;
import teammates.common.datatransfer.questions.FeedbackMcqQuestionDetails;
import teammates.common.datatransfer.questions.FeedbackMsqQuestionDetails;
import teammates.common.datatransfer.questions.FeedbackQuestionDetails;
import teammates.common.datatransfer.questions.FeedbackQuestionType;
import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.exception.InvalidParametersException;
import teammates.common.util.Const;
import teammates.common.util.Logger;
import teammates.storage.sqlapi.FeedbackQuestionsDb;
import teammates.storage.sqlentity.FeedbackQuestion;
import teammates.storage.sqlentity.FeedbackSession;
import teammates.storage.sqlentity.Instructor;
import teammates.storage.sqlentity.Student;
import teammates.storage.sqlentity.questions.FeedbackMcqQuestion;
import teammates.storage.sqlentity.questions.FeedbackMsqQuestion;
import teammates.ui.request.FeedbackQuestionUpdateRequest;
/**
* Handles operations related to feedback questions.
*
* @see FeedbackQuestion
* @see FeedbackQuestionsDb
*/
public final class FeedbackQuestionsLogic {
static final String USER_NAME_FOR_SELF = "Myself";
private static final Logger log = Logger.getLogger();
private static final FeedbackQuestionsLogic instance = new FeedbackQuestionsLogic();
private FeedbackQuestionsDb fqDb;
private CoursesLogic coursesLogic;
private FeedbackResponsesLogic frLogic;
private UsersLogic usersLogic;
private FeedbackSessionsLogic feedbackSessionsLogic;
private FeedbackQuestionsLogic() {
// prevent initialization
}
public static FeedbackQuestionsLogic inst() {
return instance;
}
void initLogicDependencies(FeedbackQuestionsDb fqDb, CoursesLogic coursesLogic, FeedbackResponsesLogic frLogic,
UsersLogic usersLogic, FeedbackSessionsLogic feedbackSessionsLogic) {
this.fqDb = fqDb;
this.coursesLogic = coursesLogic;
this.frLogic = frLogic;
this.usersLogic = usersLogic;
this.feedbackSessionsLogic = feedbackSessionsLogic;
}
/**
* Creates a new feedback question.
*
* @return the created question
* @throws InvalidParametersException if the question is invalid
*/
public FeedbackQuestion createFeedbackQuestion(FeedbackQuestion feedbackQuestion) throws InvalidParametersException {
assert feedbackQuestion != null;
if (!feedbackQuestion.isValid()) {
throw new InvalidParametersException(feedbackQuestion.getInvalidityInfo());
}
List<FeedbackQuestion> questionsBefore = getFeedbackQuestionsForSession(feedbackQuestion.getFeedbackSession());
FeedbackQuestion createdQuestion = fqDb.createFeedbackQuestion(feedbackQuestion);
adjustQuestionNumbers(questionsBefore.size() + 1, createdQuestion.getQuestionNumber(), questionsBefore);
return createdQuestion;
}
/**
* Gets an feedback question by feedback question id.
* @param id of feedback question.
* @return the specified feedback question.
*/
public FeedbackQuestion getFeedbackQuestion(UUID id) {
return fqDb.getFeedbackQuestion(id);
}
/**
* Gets a {@link List} of every FeedbackQuestion in the given session.
*/
public List<FeedbackQuestion> getFeedbackQuestionsForSession(FeedbackSession feedbackSession) {
List<FeedbackQuestion> questions = fqDb.getFeedbackQuestionsForSession(feedbackSession.getId());
questions.sort(null);
// check whether the question numbers are consistent
if (questions.size() > 1 && !areQuestionNumbersConsistent(questions)) {
log.severe(feedbackSession.getCourse().getId() + ": " + feedbackSession.getName()
+ " has invalid question numbers");
}
return questions;
}
/**
* Checks if there are any questions for the given session that instructors can view/submit.
*/
public boolean hasFeedbackQuestionsForInstructors(List<FeedbackQuestion> fqs, boolean isCreator) {
boolean hasQuestions = hasFeedbackQuestionsForGiverType(fqs, FeedbackParticipantType.INSTRUCTORS);
if (hasQuestions) {
return true;
}
if (isCreator) {
hasQuestions = hasFeedbackQuestionsForGiverType(fqs, FeedbackParticipantType.SELF);
}
return hasQuestions;
}
/**
* Gets a {@code List} of all questions for the given session that instructors can view/submit.
*/
public List<FeedbackQuestion> getFeedbackQuestionsForInstructors(
FeedbackSession feedbackSession, String userEmail) {
List<FeedbackQuestion> questions = new ArrayList<>();
questions.addAll(
fqDb.getFeedbackQuestionsForGiverType(
feedbackSession, FeedbackParticipantType.INSTRUCTORS));
if (feedbackSession.getCreatorEmail().equals(userEmail)) {
questions.addAll(
fqDb.getFeedbackQuestionsForGiverType(
feedbackSession, FeedbackParticipantType.SELF));
}
return questions;
}
/**
* Gets a {@code List} of all questions for the given session that students can view/submit.
*/
public List<FeedbackQuestion> getFeedbackQuestionsForStudents(FeedbackSession feedbackSession) {
List<FeedbackQuestion> questions = new ArrayList<>();
questions.addAll(fqDb.getFeedbackQuestionsForGiverType(feedbackSession, FeedbackParticipantType.STUDENTS));
questions.addAll(fqDb.getFeedbackQuestionsForGiverType(feedbackSession, FeedbackParticipantType.SELF));
return questions;
}
/**
* Updates a feedback question.
*
* <p>Cascade adjust the question number of questions in the same session.
*
* <p>Cascade adjust the existing response of the question.
*
* @return updated feedback question
* @throws InvalidParametersException if attributes to update are not valid
* @throws EntityDoesNotExistException if the feedback question cannot be found
*/
public FeedbackQuestion updateFeedbackQuestionCascade(UUID questionId, FeedbackQuestionUpdateRequest updateRequest)
throws InvalidParametersException, EntityDoesNotExistException {
FeedbackQuestion question = fqDb.getFeedbackQuestion(questionId);
if (question == null) {
throw new EntityDoesNotExistException("Trying to update a feedback question that does not exist.");
}
int oldQuestionNumber = question.getQuestionNumber();
int newQuestionNumber = updateRequest.getQuestionNumber();
List<FeedbackQuestion> previousQuestionsInSession = new ArrayList<>();
if (oldQuestionNumber != newQuestionNumber) {
// get questions in session before update
previousQuestionsInSession = getFeedbackQuestionsForSession(question.getFeedbackSession());
}
// update question
question.setQuestionNumber(updateRequest.getQuestionNumber());
question.setDescription(updateRequest.getQuestionDescription());
question.setQuestionDetails(updateRequest.getQuestionDetails());
question.setGiverType(updateRequest.getGiverType());
question.setRecipientType(updateRequest.getRecipientType());
question.setNumOfEntitiesToGiveFeedbackTo(updateRequest.getNumberOfEntitiesToGiveFeedbackTo());
question.setShowResponsesTo(updateRequest.getShowResponsesTo());
question.setShowGiverNameTo(updateRequest.getShowGiverNameTo());
question.setShowRecipientNameTo(updateRequest.getShowRecipientNameTo());
// validate questions (giver & recipient)
String err = question.getQuestionDetailsCopy().validateGiverRecipientVisibility(question);
if (!err.isEmpty()) {
throw new InvalidParametersException(err);
}
// validate questions (question details)
FeedbackQuestionDetails questionDetails = question.getQuestionDetailsCopy();
List<String> questionDetailsErrors = questionDetails.validateQuestionDetails();
if (!questionDetailsErrors.isEmpty()) {
throw new InvalidParametersException(questionDetailsErrors.toString());
}
if (oldQuestionNumber != newQuestionNumber) {
// shift other feedback questions (generate an empty "slot")
adjustQuestionNumbers(oldQuestionNumber, newQuestionNumber, previousQuestionsInSession);
}
// adjust responses
if (question.areResponseDeletionsRequiredForChanges(updateRequest.getGiverType(),
updateRequest.getRecipientType(), updateRequest.getQuestionDetails())) {
frLogic.deleteFeedbackResponsesForQuestionCascade(question.getId());
}
return question;
}
/**
* Checks if there are any questions for the given session that students can view/submit.
*/
public boolean hasFeedbackQuestionsForStudents(List<FeedbackQuestion> fqs) {
return hasFeedbackQuestionsForGiverType(fqs, FeedbackParticipantType.STUDENTS)
|| hasFeedbackQuestionsForGiverType(fqs, FeedbackParticipantType.TEAMS);
}
/**
* Checks if there is any feedback questions in a session in a course for the given giver type.
*/
public boolean hasFeedbackQuestionsForGiverType(
List<FeedbackQuestion> feedbackQuestions, FeedbackParticipantType giverType) {
assert feedbackQuestions != null;
assert giverType != null;
for (FeedbackQuestion fq : feedbackQuestions) {
if (fq.getGiverType() == giverType) {
return true;
}
}
return false;
}
// TODO can be removed once we are sure that question numbers will be consistent
private boolean areQuestionNumbersConsistent(List<FeedbackQuestion> questions) {
Set<Integer> questionNumbersInSession = new HashSet<>();
for (FeedbackQuestion question : questions) {
if (!questionNumbersInSession.add(question.getQuestionNumber())) {
return false;
}
}
for (int i = 1; i <= questions.size(); i++) {
if (!questionNumbersInSession.contains(i)) {
return false;
}
}
return true;
}
/**
* Adjust questions between the old and new number,
* if the new number is smaller, then shift up (increase qn#) all questions in between.
* if the new number is bigger, then shift down(decrease qn#) all questions in between.
*/
private void adjustQuestionNumbers(int oldQuestionNumber, int newQuestionNumber, List<FeedbackQuestion> questions) {
if (oldQuestionNumber > newQuestionNumber && oldQuestionNumber >= 1) {
for (int i = oldQuestionNumber - 1; i >= newQuestionNumber; i--) {
FeedbackQuestion question = questions.get(i - 1);
question.setQuestionNumber(question.getQuestionNumber() + 1);
}
} else if (oldQuestionNumber < newQuestionNumber && oldQuestionNumber < questions.size()) {
for (int i = oldQuestionNumber + 1; i <= newQuestionNumber; i++) {
FeedbackQuestion question = questions.get(i - 1);
question.setQuestionNumber(question.getQuestionNumber() - 1);
}
}
}
/**
* Populates fields that need dynamic generation in a question.
*
* <p>Currently, only MCQ/MSQ needs to generate choices dynamically.</p>
*
* @param feedbackQuestion the question to populate
* @param courseId the ID of the course
* @param emailOfEntityDoingQuestion the email of the entity doing the question
* @param teamOfEntityDoingQuestion the team of the entity doing the question. If the entity is an instructor,
* it can be {@code null}.
*/
public void populateFieldsToGenerateInQuestion(FeedbackQuestion feedbackQuestion,
String courseId, String emailOfEntityDoingQuestion, String teamOfEntityDoingQuestion) {
List<String> optionList;
FeedbackParticipantType generateOptionsFor;
FeedbackQuestionType questionType = feedbackQuestion.getQuestionDetailsCopy().getQuestionType();
if (questionType == FeedbackQuestionType.MCQ) {
FeedbackMcqQuestionDetails feedbackMcqQuestionDetails =
(FeedbackMcqQuestionDetails) feedbackQuestion.getQuestionDetailsCopy();
optionList = feedbackMcqQuestionDetails.getMcqChoices();
generateOptionsFor = feedbackMcqQuestionDetails.getGenerateOptionsFor();
} else if (questionType == FeedbackQuestionType.MSQ) {
FeedbackMsqQuestionDetails feedbackMsqQuestionDetails =
(FeedbackMsqQuestionDetails) feedbackQuestion.getQuestionDetailsCopy();
optionList = feedbackMsqQuestionDetails.getMsqChoices();
generateOptionsFor = feedbackMsqQuestionDetails.getGenerateOptionsFor();
} else {
// other question types
return;
}
switch (generateOptionsFor) {
case NONE:
break;
case STUDENTS:
case STUDENTS_IN_SAME_SECTION:
case STUDENTS_EXCLUDING_SELF:
List<Student> studentList;
if (generateOptionsFor == FeedbackParticipantType.STUDENTS_IN_SAME_SECTION) {
Student student =
usersLogic.getStudentForEmail(courseId, emailOfEntityDoingQuestion);
studentList = usersLogic.getStudentsForSection(student.getSectionName(), courseId);
} else {
studentList = usersLogic.getStudentsForCourse(courseId);
}
if (generateOptionsFor == FeedbackParticipantType.STUDENTS_EXCLUDING_SELF) {
studentList.removeIf(studentInList -> studentInList.getEmail().equals(emailOfEntityDoingQuestion));
}
for (Student student : studentList) {
optionList.add(student.getName() + " (" + student.getTeam() + ")");
}
optionList.sort(null);
break;
case TEAMS:
case TEAMS_IN_SAME_SECTION:
case TEAMS_EXCLUDING_SELF:
List<String> teams;
if (generateOptionsFor == FeedbackParticipantType.TEAMS_IN_SAME_SECTION) {
Student student =
usersLogic.getStudentForEmail(courseId, emailOfEntityDoingQuestion);
teams = coursesLogic.getTeamsForSection(student.getSection())
.stream()
.map(team -> { return team.getName(); })
.collect(Collectors.toList());
} else {
teams = coursesLogic.getTeamsForCourse(courseId)
.stream()
.map(team -> { return team.getName(); })
.collect(Collectors.toList());
}
if (generateOptionsFor == FeedbackParticipantType.TEAMS_EXCLUDING_SELF) {
teams.removeIf(team -> team.equals(teamOfEntityDoingQuestion));
}
for (String team : teams) {
optionList.add(team);
}
optionList.sort(null);
break;
case OWN_TEAM_MEMBERS_INCLUDING_SELF:
case OWN_TEAM_MEMBERS:
if (teamOfEntityDoingQuestion != null) {
List<Student> teamMembers = usersLogic.getStudentsForTeam(teamOfEntityDoingQuestion,
courseId);
if (generateOptionsFor == FeedbackParticipantType.OWN_TEAM_MEMBERS) {
teamMembers.removeIf(teamMember -> teamMember.getEmail().equals(emailOfEntityDoingQuestion));
}
teamMembers.forEach(teamMember -> optionList.add(teamMember.getName()));
optionList.sort(null);
}
break;
case INSTRUCTORS:
List<Instructor> instructorList =
usersLogic.getInstructorsForCourse(courseId);
for (Instructor instructor : instructorList) {
optionList.add(instructor.getName());
}
optionList.sort(null);
break;
default:
assert false : "Trying to generate options for neither students, teams nor instructors";
break;
}
if (questionType == FeedbackQuestionType.MCQ) {
FeedbackMcqQuestionDetails feedbackMcqQuestionDetails =
(FeedbackMcqQuestionDetails) feedbackQuestion.getQuestionDetailsCopy();
feedbackMcqQuestionDetails.setMcqChoices(optionList);
((FeedbackMcqQuestion) feedbackQuestion).setFeedBackQuestionDetails(feedbackMcqQuestionDetails);
} else if (questionType == FeedbackQuestionType.MSQ) {
FeedbackMsqQuestionDetails feedbackMsqQuestionDetails =
(FeedbackMsqQuestionDetails) feedbackQuestion.getQuestionDetailsCopy();
feedbackMsqQuestionDetails.setMsqChoices(optionList);
((FeedbackMsqQuestion) feedbackQuestion).setFeedBackQuestionDetails(feedbackMsqQuestionDetails);
}
}
/**
* Gets the recipients of a feedback question including recipient section and team.
*
* @param question the feedback question
* @param instructorGiver can be null for student giver
* @param studentGiver can be null for instructor giver
* @param courseRoster if provided, the function can be completed without touching database
* @return a Map of {@code FeedbackQuestionRecipient} as the value and identifier as the key.
*/
public Map<String, FeedbackQuestionRecipient> getRecipientsOfQuestion(
FeedbackQuestion question,
@Nullable Instructor instructorGiver, @Nullable Student studentGiver,
@Nullable SqlCourseRoster courseRoster) {
assert instructorGiver != null || studentGiver != null;
String courseId = question.getCourseId();
Map<String, FeedbackQuestionRecipient> recipients = new HashMap<>();
boolean isStudentGiver = studentGiver != null;
boolean isInstructorGiver = instructorGiver != null;
String giverEmail = "";
String giverTeam = "";
String giverSection = "";
if (isStudentGiver) {
giverEmail = studentGiver.getEmail();
giverTeam = studentGiver.getTeamName();
giverSection = studentGiver.getSectionName();
} else if (isInstructorGiver) {
giverEmail = instructorGiver.getEmail();
giverTeam = Const.USER_TEAM_FOR_INSTRUCTOR;
giverSection = Const.DEFAULT_SECTION;
}
FeedbackParticipantType recipientType = question.getRecipientType();
FeedbackParticipantType generateOptionsFor = recipientType;
switch (recipientType) {
case SELF:
if (question.getGiverType() == FeedbackParticipantType.TEAMS) {
recipients.put(giverTeam,
new FeedbackQuestionRecipient(giverTeam, giverTeam));
} else {
recipients.put(giverEmail,
new FeedbackQuestionRecipient(USER_NAME_FOR_SELF, giverEmail));
}
break;
case STUDENTS:
case STUDENTS_EXCLUDING_SELF:
case STUDENTS_IN_SAME_SECTION:
List<Student> studentList;
if (courseRoster == null) {
if (generateOptionsFor == FeedbackParticipantType.STUDENTS_IN_SAME_SECTION) {
studentList = usersLogic.getStudentsForSection(giverSection, courseId);
} else {
studentList = usersLogic.getStudentsForCourse(courseId);
}
} else {
if (generateOptionsFor == FeedbackParticipantType.STUDENTS_IN_SAME_SECTION) {
final String finalGiverSection = giverSection;
studentList = courseRoster.getStudents().stream()
.filter(studentAttributes -> studentAttributes.getSectionName()
.equals(finalGiverSection)).collect(Collectors.toList());
} else {
studentList = courseRoster.getStudents();
}
}
for (Student student : studentList) {
if (isInstructorGiver && !instructorGiver.isAllowedForPrivilege(
student.getSectionName(), question.getFeedbackSession().getName(),
Const.InstructorPermissions.CAN_SUBMIT_SESSION_IN_SECTIONS)) {
// instructor can only see students in allowed sections for him/her
continue;
}
// Ensure student does not evaluate him/herself if it's STUDENTS_EXCLUDING_SELF or
// STUDENTS_IN_SAME_SECTION
if (giverEmail.equals(student.getEmail()) && generateOptionsFor != FeedbackParticipantType.STUDENTS) {
continue;
}
recipients.put(student.getEmail(), new FeedbackQuestionRecipient(student.getName(), student.getEmail(),
student.getSectionName(), student.getTeamName()));
}
break;
case INSTRUCTORS:
List<Instructor> instructorsInCourse;
if (courseRoster == null) {
instructorsInCourse = usersLogic.getInstructorsForCourse(courseId);
} else {
instructorsInCourse = courseRoster.getInstructors();
}
for (Instructor instr : instructorsInCourse) {
// remove hidden instructors for students
if (isStudentGiver && !instr.isDisplayedToStudents()) {
continue;
}
// Ensure instructor does not evaluate himself
if (!giverEmail.equals(instr.getEmail())) {
recipients.put(instr.getEmail(),
new FeedbackQuestionRecipient(instr.getName(), instr.getEmail()));
}
}
break;
case TEAMS:
case TEAMS_EXCLUDING_SELF:
case TEAMS_IN_SAME_SECTION:
Map<String, List<Student>> teamToTeamMembersTable;
List<Student> teamStudents;
if (courseRoster == null) {
if (generateOptionsFor == FeedbackParticipantType.TEAMS_IN_SAME_SECTION) {
teamStudents = usersLogic.getStudentsForSection(giverSection, courseId);
} else {
teamStudents = usersLogic.getStudentsForCourse(courseId);
}
teamToTeamMembersTable = SqlCourseRoster.buildTeamToMembersTable(teamStudents);
} else {
if (generateOptionsFor == FeedbackParticipantType.TEAMS_IN_SAME_SECTION) {
final String finalGiverSection = giverSection;
teamStudents = courseRoster.getStudents().stream()
.filter(student -> student.getSectionName().equals(finalGiverSection))
.collect(Collectors.toList());
teamToTeamMembersTable = SqlCourseRoster.buildTeamToMembersTable(teamStudents);
} else {
teamToTeamMembersTable = courseRoster.getTeamToMembersTable();
}
}
for (Map.Entry<String, List<Student>> team : teamToTeamMembersTable.entrySet()) {
if (isInstructorGiver && !instructorGiver.isAllowedForPrivilege(
team.getValue().iterator().next().getSectionName(),
question.getFeedbackSession().getName(),
Const.InstructorPermissions.CAN_SUBMIT_SESSION_IN_SECTIONS)) {
// instructor can only see teams in allowed sections for him/her
continue;
}
// Ensure student('s team) does not evaluate own team if it's TEAMS_EXCLUDING_SELF or
// TEAMS_IN_SAME_SECTION
if (giverTeam.equals(team.getKey()) && generateOptionsFor != FeedbackParticipantType.TEAMS) {
continue;
}
// recipientEmail doubles as team name in this case.
recipients.put(team.getKey(), new FeedbackQuestionRecipient(team.getKey(), team.getKey()));
}
break;
case OWN_TEAM:
recipients.put(giverTeam, new FeedbackQuestionRecipient(giverTeam, giverTeam));
break;
case OWN_TEAM_MEMBERS:
List<Student> students;
if (courseRoster == null) {
students = usersLogic.getStudentsForTeam(giverTeam, courseId);
} else {
students = courseRoster.getTeamToMembersTable().getOrDefault(giverTeam, Collections.emptyList());
}
for (Student student : students) {
if (!student.getEmail().equals(giverEmail)) {
recipients.put(student.getEmail(), new FeedbackQuestionRecipient(student.getName(), student.getEmail(),
student.getSectionName(), student.getTeamName()));
}
}
break;
case OWN_TEAM_MEMBERS_INCLUDING_SELF:
List<Student> teamMembers;
if (courseRoster == null) {
teamMembers = usersLogic.getStudentsForTeam(giverTeam, courseId);
} else {
teamMembers = courseRoster.getTeamToMembersTable().getOrDefault(giverTeam, Collections.emptyList());
}
for (Student student : teamMembers) {
// accepts self feedback too
recipients.put(student.getEmail(), new FeedbackQuestionRecipient(student.getName(), student.getEmail(),
student.getSectionName(), student.getTeamName()));
}
break;
case NONE:
recipients.put(Const.GENERAL_QUESTION,
new FeedbackQuestionRecipient(Const.GENERAL_QUESTION, Const.GENERAL_QUESTION));
break;
default:
break;
}
return recipients;
}
/**
* Returns true if a session has question in a specific giverType.
*/
public boolean sessionHasQuestionsForGiverType(
String feedbackSessionName, String courseId, FeedbackParticipantType giverType) {
return fqDb.hasFeedbackQuestionsForGiverType(feedbackSessionName, courseId, giverType);
}
/**
* Returns true if a session has question in either STUDENTS type or TEAMS type.
*/
public boolean sessionHasQuestionsForStudent(String feedbackSessionName, String courseId) {
return fqDb.hasFeedbackQuestionsForGiverType(feedbackSessionName, courseId, FeedbackParticipantType.STUDENTS)
|| fqDb.hasFeedbackQuestionsForGiverType(feedbackSessionName, courseId, FeedbackParticipantType.TEAMS);
}
/**
* Deletes a feedback question cascade its responses and comments.
*
* <p>Silently fail if question does not exist.
*/
public void deleteFeedbackQuestionCascade(UUID feedbackQuestionId) {
fqDb.deleteFeedbackQuestion(feedbackQuestionId);
}
/**
* Filters the feedback questions in a course, with specified question type.
* @param courseId the course to search from
* @param questionType the question type to search on
* @return a list of filtered questions
*/
public List<FeedbackQuestion> getFeedbackQuestionForCourseWithType(
String courseId, FeedbackQuestionType questionType) {
List<FeedbackSession> feedbackSessions = feedbackSessionsLogic.getFeedbackSessionsForCourse(courseId);
List<FeedbackQuestion> feedbackQuestions = new ArrayList<>();
for (FeedbackSession session : feedbackSessions) {
feedbackQuestions.addAll(getFeedbackQuestionsForSession(session));
}
return feedbackQuestions
.stream()
.filter(q -> q.getQuestionDetailsCopy().getQuestionType().equals(questionType))
.collect(Collectors.toList());
}
}