Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support anonymous users in question finder #634

Merged
merged 4 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/java/uk/ac/cam/cl/dtg/isaac/api/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ public enum GameboardItemState {
}

public enum CompletionState {
ALL_CORRECT, IN_PROGRESS, NOT_ATTEMPTED
ALL_CORRECT, IN_PROGRESS, NOT_ATTEMPTED;

public static final Set<CompletionState> ALL_STATES = Set.of(CompletionState.values());
jsharkey13 marked this conversation as resolved.
Show resolved Hide resolved
}

public enum QuestionPartState {
Expand Down
37 changes: 20 additions & 17 deletions src/main/java/uk/ac/cam/cl/dtg/isaac/api/PagesFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -388,28 +388,29 @@ public final Response getQuestionList(@Context final Request request,
}

try {
if (null == statuses || Objects.equals(statuses, "")) {
// If no statuses apply assume all statuses
filterByStatuses = Set.of(CompletionState.values());
if (null == statuses) {
filterByStatuses = null;
} else if (statuses.isEmpty()) {
filterByStatuses = CompletionState.ALL_STATES;
jsharkey13 marked this conversation as resolved.
Show resolved Hide resolved
} else {
filterByStatuses = Arrays.stream(statuses.split(","))
.map(CompletionState::valueOf)
.collect(Collectors.toSet());
}
} catch (IllegalArgumentException e) {
return new SegueErrorResponse(
Status.BAD_REQUEST, "Invalid question statuses to filter by provided.", e
).toResponse();
return new SegueErrorResponse(Status.BAD_REQUEST, "Invalid question status filter provided.").toResponse();
}

String validatedSearchString = searchString.isBlank() ? null : searchString;

// Show content tagged as "nofilter" if the user is staff
boolean showNoFilterContent;
// Show content tagged as "nofilter" if the user is staff:
boolean showNoFilterContent = false;
try {
showNoFilterContent = isUserStaff(userManager, httpServletRequest);
if (user instanceof RegisteredUserDTO) {
showNoFilterContent = isUserStaff(userManager, (RegisteredUserDTO) user);
}
} catch (NoUserLoggedInException e) {
showNoFilterContent = false;
// This cannot happen!
}

List<ContentSummaryDTO> combinedResults = new ArrayList<>();
Expand Down Expand Up @@ -440,13 +441,15 @@ public final Response getQuestionList(@Context final Request request,

List<ContentSummaryDTO> unfilteredSummarizedResults = new ArrayList<>(summarizedResults);

if (user instanceof RegisteredUserDTO) {
summarizedResults = userAttemptManager.augmentContentSummaryListWithAttemptInformation(
(RegisteredUserDTO) user, summarizedResults
);
summarizedResults = summarizedResults.stream()
.filter(q -> filterByStatuses.contains(q.getState()))
.collect(Collectors.toList());
if (null != filterByStatuses) {
// Only augment when filtering by statuses:
summarizedResults = userAttemptManager.augmentContentSummaryListWithAttemptInformation(user, summarizedResults);
// Optimise out unnecessary filtering:
if (!filterByStatuses.equals(CompletionState.ALL_STATES)) {
summarizedResults = summarizedResults.stream()
.filter(q -> filterByStatuses.contains(q.getState()))
.collect(Collectors.toList());
}
}

if (limit < 0 || combinedResults.size() + summarizedResults.size() <= limit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import uk.ac.cam.cl.dtg.isaac.dto.content.ContentBaseDTO;
import uk.ac.cam.cl.dtg.isaac.dto.content.ContentDTO;
import uk.ac.cam.cl.dtg.isaac.dto.content.ContentSummaryDTO;
import uk.ac.cam.cl.dtg.isaac.dto.users.AbstractSegueUserDTO;
import uk.ac.cam.cl.dtg.isaac.dto.users.RegisteredUserDTO;
import uk.ac.cam.cl.dtg.segue.api.managers.QuestionManager;
import uk.ac.cam.cl.dtg.segue.dao.SegueDatabaseException;
Expand Down Expand Up @@ -99,12 +100,20 @@ private void augmentContentSummaryWithAttemptInformation(
* @return the augmented content summary list.
* @throws SegueDatabaseException if there is an error retrieving question attempts.
*/
public List<ContentSummaryDTO> augmentContentSummaryListWithAttemptInformation(RegisteredUserDTO user, List<ContentSummaryDTO> summarizedResults) throws SegueDatabaseException {
List<String> questionPageIds = summarizedResults.stream().map(ContentSummaryDTO::getId).collect(Collectors.toList());
public List<ContentSummaryDTO> augmentContentSummaryListWithAttemptInformation(AbstractSegueUserDTO user, List<ContentSummaryDTO> summarizedResults) throws SegueDatabaseException {

Map<String, Map<String, List<LightweightQuestionValidationResponse>>> questionAttempts =
questionManager.getMatchingLightweightQuestionAttempts(Collections.singletonList(user), questionPageIds)
.getOrDefault(user.getId(), Collections.emptyMap());
Map<String, ? extends Map<String, ? extends List<? extends LightweightQuestionValidationResponse>>> questionAttempts;

if (user instanceof RegisteredUserDTO) {
// Load only relevant attempts:
RegisteredUserDTO registeredUser = (RegisteredUserDTO) user;
List<String> questionPageIds = summarizedResults.stream().map(ContentSummaryDTO::getId).collect(Collectors.toList());
questionAttempts = questionManager.getMatchingLightweightQuestionAttempts(Collections.singletonList(registeredUser), questionPageIds)
.getOrDefault(registeredUser.getId(), Collections.emptyMap());
} else {
// For anon users, all attempts are in one place so just load all:
questionAttempts = questionManager.getQuestionAttemptsByUser(user);
}

for (ContentSummaryDTO result : summarizedResults) {
augmentContentSummaryWithAttemptInformation(result, questionAttempts);
Expand Down
Loading