-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
FeedbackResponsesLogic.java
506 lines (435 loc) · 20.1 KB
/
FeedbackResponsesLogic.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
package teammates.sqllogic.core;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.annotation.Nullable;
import teammates.common.datatransfer.FeedbackParticipantType;
import teammates.common.datatransfer.SqlCourseRoster;
import teammates.common.datatransfer.questions.FeedbackQuestionType;
import teammates.common.datatransfer.questions.FeedbackRankRecipientsResponseDetails;
import teammates.common.exception.EntityAlreadyExistsException;
import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.exception.InvalidParametersException;
import teammates.storage.sqlapi.FeedbackResponsesDb;
import teammates.storage.sqlentity.Course;
import teammates.storage.sqlentity.FeedbackQuestion;
import teammates.storage.sqlentity.FeedbackResponse;
import teammates.storage.sqlentity.FeedbackResponseComment;
import teammates.storage.sqlentity.Instructor;
import teammates.storage.sqlentity.Section;
import teammates.storage.sqlentity.Student;
import teammates.storage.sqlentity.Team;
import teammates.storage.sqlentity.responses.FeedbackRankRecipientsResponse;
/**
* Handles operations related to feedback sessions.
*
* @see FeedbackResponse
* @see FeedbackResponsesDb
*/
public final class FeedbackResponsesLogic {
private static final FeedbackResponsesLogic instance = new FeedbackResponsesLogic();
private FeedbackResponsesDb frDb;
private UsersLogic usersLogic;
private FeedbackQuestionsLogic fqLogic;
private FeedbackResponseCommentsLogic frcLogic;
private FeedbackResponsesLogic() {
// prevent initialization
}
public static FeedbackResponsesLogic inst() {
return instance;
}
/**
* Initialize dependencies for {@code FeedbackResponsesLogic}.
*/
void initLogicDependencies(FeedbackResponsesDb frDb,
UsersLogic usersLogic, FeedbackQuestionsLogic fqLogic, FeedbackResponseCommentsLogic frcLogic) {
this.frDb = frDb;
this.usersLogic = usersLogic;
this.fqLogic = fqLogic;
this.frcLogic = frcLogic;
}
/**
* Gets a feedbackResponse or null if it does not exist.
*/
public FeedbackResponse getFeedbackResponse(UUID frId) {
return frDb.getFeedbackResponse(frId);
}
/**
* Returns true if the responses of the question are visible to students.
*/
public boolean isResponseOfFeedbackQuestionVisibleToStudent(FeedbackQuestion question) {
if (question.isResponseVisibleTo(FeedbackParticipantType.STUDENTS)) {
return true;
}
boolean isStudentRecipientType =
question.getRecipientType().equals(FeedbackParticipantType.STUDENTS)
|| question.getRecipientType().equals(FeedbackParticipantType.STUDENTS_EXCLUDING_SELF)
|| question.getRecipientType().equals(FeedbackParticipantType.STUDENTS_IN_SAME_SECTION)
|| question.getRecipientType().equals(FeedbackParticipantType.OWN_TEAM_MEMBERS)
|| question.getRecipientType().equals(FeedbackParticipantType.OWN_TEAM_MEMBERS_INCLUDING_SELF)
|| question.getRecipientType().equals(FeedbackParticipantType.GIVER)
&& question.getGiverType().equals(FeedbackParticipantType.STUDENTS);
if ((isStudentRecipientType || question.getRecipientType().isTeam())
&& question.isResponseVisibleTo(FeedbackParticipantType.RECEIVER)) {
return true;
}
if (question.getGiverType() == FeedbackParticipantType.TEAMS
|| question.isResponseVisibleTo(FeedbackParticipantType.OWN_TEAM_MEMBERS)) {
return true;
}
return question.isResponseVisibleTo(FeedbackParticipantType.RECEIVER_TEAM_MEMBERS);
}
/**
* Returns true if the responses of the question are visible to instructors.
*/
public boolean isResponseOfFeedbackQuestionVisibleToInstructor(FeedbackQuestion question) {
return question.isResponseVisibleTo(FeedbackParticipantType.INSTRUCTORS);
}
/**
* Checks whether a giver has responded a session.
*/
public boolean hasGiverRespondedForSession(String giverIdentifier, List<FeedbackQuestion> questions) {
assert questions != null;
for (FeedbackQuestion question : questions) {
boolean hasResponse = question
.getFeedbackResponses()
.stream()
.anyMatch(response -> response.getGiver().equals(giverIdentifier));
if (hasResponse) {
return true;
}
}
return false;
}
/**
* Checks whether a giver has responded a session.
*/
public boolean hasGiverRespondedForSession(String giver, String feedbackSessionName, String courseId) {
return frDb.hasResponsesFromGiverInSession(giver, feedbackSessionName, courseId);
}
/**
* Creates a feedback response.
* @return the created response
* @throws InvalidParametersException if the response is not valid
* @throws EntityAlreadyExistsException if the response already exist
*/
public FeedbackResponse createFeedbackResponse(FeedbackResponse feedbackResponse)
throws InvalidParametersException, EntityAlreadyExistsException {
return frDb.createFeedbackResponse(feedbackResponse);
}
/**
* Get existing feedback responses from instructor for the given question.
*/
public List<FeedbackResponse> getFeedbackResponsesFromInstructorForQuestion(
FeedbackQuestion question, Instructor instructor) {
return frDb.getFeedbackResponsesFromGiverForQuestion(
question.getId(), instructor.getEmail());
}
/**
* Get existing feedback responses from student or his team for the given
* question.
*/
public List<FeedbackResponse> getFeedbackResponsesFromStudentOrTeamForQuestion(
FeedbackQuestion question, Student student) {
if (question.getGiverType() == FeedbackParticipantType.TEAMS) {
return getFeedbackResponsesFromTeamForQuestion(
question.getId(), question.getCourseId(), student.getTeamName(), null);
}
return frDb.getFeedbackResponsesFromGiverForQuestion(question.getId(), student.getEmail());
}
private List<FeedbackResponse> getFeedbackResponsesFromTeamForQuestion(
UUID feedbackQuestionId, String courseId, String teamName, @Nullable SqlCourseRoster courseRoster) {
List<FeedbackResponse> responses = new ArrayList<>();
List<Student> studentsInTeam = courseRoster == null
? usersLogic.getStudentsForTeam(teamName, courseId) : courseRoster.getTeamToMembersTable().get(teamName);
for (Student student : studentsInTeam) {
responses.addAll(frDb.getFeedbackResponsesFromGiverForQuestion(
feedbackQuestionId, student.getEmail()));
}
responses.addAll(frDb.getFeedbackResponsesFromGiverForQuestion(
feedbackQuestionId, teamName));
return responses;
}
/**
* Updates a non-null feedback response by {@link FeedbackResponse}.
*
* <p>Cascade updates its associated feedback response comment
* (e.g. associated response ID, giverSection and recipientSection).
*
* <p>If the giver/recipient field is changed, the response is updated by recreating the response
* as question-giver-recipient is the primary key.
*
* @return updated feedback response
* @throws InvalidParametersException if attributes to update are not valid
* @throws EntityDoesNotExistException if the comment cannot be found
*/
public FeedbackResponse updateFeedbackResponseCascade(FeedbackResponse feedbackResponse)
throws InvalidParametersException, EntityDoesNotExistException {
FeedbackResponse oldResponse = frDb.getFeedbackResponse(feedbackResponse.getId());
FeedbackResponse newResponse = frDb.updateFeedbackResponse(feedbackResponse);
boolean isGiverSectionChanged = !oldResponse.getGiverSection().equals(newResponse.getGiverSection());
boolean isRecipientSectionChanged = !oldResponse.getRecipientSection().equals(newResponse.getRecipientSection());
if (isGiverSectionChanged || isRecipientSectionChanged) {
List<FeedbackResponseComment> oldResponseComments =
frcLogic.getFeedbackResponseCommentForResponse(oldResponse.getId());
for (FeedbackResponseComment oldResponseComment : oldResponseComments) {
if (isGiverSectionChanged) {
oldResponseComment.setGiverSection(newResponse.getGiverSection());
}
if (isRecipientSectionChanged) {
oldResponseComment.setRecipientSection(newResponse.getRecipientSection());
}
frcLogic.updateFeedbackResponseComment(oldResponseComment);
}
}
return newResponse;
}
/**
* Deletes a feedback response cascade its associated feedback response comments.
* Implicitly makes use of CascadeType.REMOVE.
*/
public void deleteFeedbackResponsesAndCommentsCascade(FeedbackResponse feedbackResponse) {
frDb.deleteFeedbackResponse(feedbackResponse);
}
/**
* Deletes all feedback responses of a question cascade its associated comments.
*/
public void deleteFeedbackResponsesForQuestionCascade(UUID feedbackQuestionId) {
// delete all responses, comments of the question
frDb.deleteFeedbackResponsesForQuestionCascade(feedbackQuestionId);
}
/**
* Checks whether there are responses for a question.
*/
public boolean areThereResponsesForQuestion(UUID questionId) {
return frDb.areThereResponsesForQuestion(questionId);
}
/**
* Checks whether there are responses for a course.
*/
public boolean hasResponsesForCourse(String courseId) {
return frDb.hasResponsesForCourse(courseId);
}
/**
* Deletes all feedback responses involved an entity, cascade its associated comments.
* Deletion will automatically be cascaded to each feedback response's comments,
* handled by Hibernate using the OnDelete annotation.
*/
public void deleteFeedbackResponsesForCourseCascade(String courseId, String entityEmail) {
// delete responses from the entity
List<FeedbackResponse> responsesFromStudent =
getFeedbackResponsesFromGiverForCourse(courseId, entityEmail);
for (FeedbackResponse response : responsesFromStudent) {
frDb.deleteFeedbackResponse(response);
}
// delete responses to the entity
List<FeedbackResponse> responsesToStudent =
getFeedbackResponsesForRecipientForCourse(courseId, entityEmail);
for (FeedbackResponse response : responsesToStudent) {
frDb.deleteFeedbackResponse(response);
}
}
/**
* Gets all responses given by a user for a course.
*/
public List<FeedbackResponse> getFeedbackResponsesFromGiverForCourse(
String courseId, String giver) {
assert courseId != null;
assert giver != null;
return frDb.getFeedbackResponsesFromGiverForCourse(courseId, giver);
}
/**
* Gets all responses received by a user for a course.
*/
public List<FeedbackResponse> getFeedbackResponsesForRecipientForCourse(
String courseId, String recipient) {
assert courseId != null;
assert recipient != null;
return frDb.getFeedbackResponsesForRecipientForCourse(courseId, recipient);
}
/**
* Gets all responses given by a user for a question.
*/
public List<FeedbackResponse> getFeedbackResponsesFromGiverForQuestion(
UUID feedbackQuestionId, String giver) {
return frDb.getFeedbackResponsesFromGiverForQuestion(feedbackQuestionId, giver);
}
/**
* Updates the relevant responses before the deletion of a student.
* This method takes care of the following:
* Making existing responses of 'rank recipient question' consistent.
*/
public void updateRankRecipientQuestionResponsesAfterDeletingStudent(String courseId) {
List<FeedbackQuestion> filteredQuestions =
fqLogic.getFeedbackQuestionForCourseWithType(courseId, FeedbackQuestionType.RANK_RECIPIENTS);
SqlCourseRoster roster = new SqlCourseRoster(
usersLogic.getStudentsForCourse(courseId),
usersLogic.getInstructorsForCourse(courseId));
for (FeedbackQuestion question : filteredQuestions) {
makeRankRecipientQuestionResponsesConsistent(question, roster);
}
}
/**
* Makes the rankings by one giver in the response to a 'rank recipient question' consistent, after deleting a
* student.
* <p>
* Fails silently if the question type is not 'rank recipient question'.
* </p>
*/
private void makeRankRecipientQuestionResponsesConsistent(
FeedbackQuestion question, SqlCourseRoster roster) {
assert !question.getQuestionDetailsCopy().getQuestionType()
.equals(FeedbackQuestionType.RANK_RECIPIENTS);
FeedbackParticipantType giverType = question.getGiverType();
List<FeedbackResponse> responses = new ArrayList<>();
int numberOfRecipients = 0;
switch (giverType) {
case INSTRUCTORS:
case SELF:
for (Instructor instructor : roster.getInstructors()) {
numberOfRecipients =
fqLogic.getRecipientsOfQuestion(question, instructor, null, roster).size();
responses = getFeedbackResponsesFromGiverForQuestion(question.getId(), instructor.getEmail());
}
break;
case TEAMS:
case TEAMS_IN_SAME_SECTION:
Student firstMemberOfTeam;
String team;
Map<String, List<Student>> teams = roster.getTeamToMembersTable();
for (Map.Entry<String, List<Student>> entry : teams.entrySet()) {
team = entry.getKey();
firstMemberOfTeam = entry.getValue().get(0);
numberOfRecipients =
fqLogic.getRecipientsOfQuestion(question, null, firstMemberOfTeam, roster).size();
responses =
getFeedbackResponsesFromTeamForQuestion(
question.getId(), question.getCourseId(), team, roster);
}
break;
default:
for (Student student : roster.getStudents()) {
numberOfRecipients =
fqLogic.getRecipientsOfQuestion(question, null, student, roster).size();
responses = getFeedbackResponsesFromGiverForQuestion(question.getId(), student.getEmail());
}
break;
}
updateFeedbackResponsesForRankRecipientQuestions(responses, numberOfRecipients);
}
/**
* Updates responses for 'rank recipient question', such that the ranks in the responses are consistent.
* @param responses responses to one feedback question, from one giver
* @param maxRank the maximum rank in each response
*/
private void updateFeedbackResponsesForRankRecipientQuestions(
List<FeedbackResponse> responses, int maxRank) {
if (maxRank <= 0) {
return;
}
FeedbackRankRecipientsResponseDetails responseDetails;
boolean[] isRankUsed;
boolean isUpdateNeeded = false;
int answer;
int maxUnusedRank = 0;
// Checks whether update is needed.
for (FeedbackResponse response : responses) {
if (!(response instanceof FeedbackRankRecipientsResponse)) {
continue;
}
responseDetails = ((FeedbackRankRecipientsResponse) response).getAnswer();
answer = responseDetails.getAnswer();
if (answer > maxRank) {
isUpdateNeeded = true;
break;
}
}
// Updates repeatedly, until all responses are consistent.
while (isUpdateNeeded) {
isUpdateNeeded = false; // will be set to true again once invalid rank appears after update
isRankUsed = new boolean[maxRank];
// Obtains the largest unused rank.
for (FeedbackResponse response : responses) {
if (!(response instanceof FeedbackRankRecipientsResponse)) {
continue;
}
responseDetails = ((FeedbackRankRecipientsResponse) response).getAnswer();
answer = responseDetails.getAnswer();
if (answer <= maxRank) {
isRankUsed[answer - 1] = true;
}
}
for (int i = maxRank - 1; i >= 0; i--) {
if (!isRankUsed[i]) {
maxUnusedRank = i + 1;
break;
}
}
assert maxUnusedRank > 0; // if update is needed, there must be at least one unused rank
for (FeedbackResponse response : responses) {
if (response instanceof FeedbackRankRecipientsResponse) {
responseDetails = ((FeedbackRankRecipientsResponse) response).getAnswer();
answer = responseDetails.getAnswer();
if (answer > maxUnusedRank) {
answer--;
responseDetails.setAnswer(answer);
}
if (answer > maxRank) {
isUpdateNeeded = true; // sets the flag to true if the updated rank is still invalid
}
}
}
}
}
/**
* Updates responses for a student when his team changes.
* <p>
* This is done by deleting responses that are no longer relevant to him in his new team.
* </p>
*/
public void updateFeedbackResponsesForChangingTeam(Course course, String newEmail, Team newTeam, Team oldTeam)
throws InvalidParametersException, EntityDoesNotExistException {
FeedbackQuestion qn;
List<FeedbackResponse> responsesFromUser =
getFeedbackResponsesFromGiverForCourse(course.getId(), newEmail);
for (FeedbackResponse response : responsesFromUser) {
qn = fqLogic.getFeedbackQuestion(response.getId());
if (qn != null && qn.getGiverType() == FeedbackParticipantType.TEAMS) {
deleteFeedbackResponsesForQuestionCascade(qn.getId());
}
}
List<FeedbackResponse> responsesToUser =
getFeedbackResponsesForRecipientForCourse(course.getId(), newEmail);
for (FeedbackResponse response : responsesToUser) {
qn = fqLogic.getFeedbackQuestion(response.getId());
if (qn != null && qn.getGiverType() == FeedbackParticipantType.TEAMS) {
deleteFeedbackResponsesForQuestionCascade(qn.getId());
}
}
boolean isOldTeamEmpty = usersLogic.getStudentsForTeam(oldTeam.getName(), course.getId()).isEmpty();
if (isOldTeamEmpty) {
deleteFeedbackResponsesForCourseCascade(course.getId(), oldTeam.getName());
}
}
/**
* Updates responses for a student when his section changes.
*/
public void updateFeedbackResponsesForChangingSection(Course course, String newEmail, Section newSection)
throws InvalidParametersException, EntityDoesNotExistException {
List<FeedbackResponse> responsesFromUser =
getFeedbackResponsesFromGiverForCourse(course.getId(), newEmail);
for (FeedbackResponse response : responsesFromUser) {
response.setGiverSection(newSection);
frDb.updateFeedbackResponse(response);
frcLogic.updateFeedbackResponseCommentsForResponse(response);
}
List<FeedbackResponse> responsesToUser =
getFeedbackResponsesForRecipientForCourse(course.getId(), newEmail);
for (FeedbackResponse response : responsesToUser) {
response.setRecipientSection(newSection);
frDb.updateFeedbackResponse(response);
frcLogic.updateFeedbackResponseCommentsForResponse(response);
}
}
}