Skip to content

Commit 31373bf

Browse files
authored
Merge pull request #712 from isaacphysics/feature/concepts-related-attempts
Augment concept pages with related content attempts
2 parents 72e5661 + 7aeb759 commit 31373bf

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

src/main/java/uk/ac/cam/cl/dtg/isaac/api/PagesFacade.java

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -245,34 +245,50 @@ public final Response getConcept(@Context final Request request, @Context final
245245
if (null == conceptId || conceptId.isEmpty()) {
246246
return new SegueErrorResponse(Status.BAD_REQUEST, "You must provide a valid concept id.").toResponse();
247247
}
248-
249-
// Calculate the ETag on current live version of the content:
250-
EntityTag etag = new EntityTag(
251-
String.valueOf(this.contentManager.getCurrentContentSHA().hashCode() + conceptId.hashCode())
252-
);
253-
Response cachedResponse = generateCachedResponse(request, etag);
254-
if (cachedResponse != null) {
255-
return cachedResponse;
256-
}
257-
258248
try {
249+
AbstractSegueUserDTO user = userManager.getCurrentUser(servletRequest);
250+
259251
ContentDTO contentDTO = contentManager.getContentById(conceptId, true);
260252
if (contentDTO instanceof IsaacConceptPageDTO) {
261253
SeguePageDTO content = (SeguePageDTO) contentDTO;
262-
// Do we want to use the user's actual question attempts here? We did not previously.
263-
augmentContentWithRelatedContent(content, Collections.emptyMap());
264254

255+
// Load attempts at related questions:
256+
Map<String, ? extends Map<String, ? extends List<? extends LightweightQuestionValidationResponse>>> relatedQuestionAttempts;
257+
List<String> relatedQuestionIds = new ArrayList<>(getRelatedContentIds(content));
258+
if (user instanceof AnonymousUserDTO) {
259+
// For anon users, must load all attempts:
260+
relatedQuestionAttempts = questionManager.getQuestionAttemptsByUser(user);
261+
} else {
262+
// For registered users, can load only relevant lightweight attempts:
263+
RegisteredUserDTO registeredUser = (RegisteredUserDTO) user;
264+
relatedQuestionAttempts = questionManager.getMatchingLightweightQuestionAttempts(
265+
registeredUser, relatedQuestionIds
266+
);
267+
}
268+
269+
// Check the cache status:
270+
EntityTag etag = new EntityTag(String.valueOf(
271+
this.contentManager.getCurrentContentSHA().hashCode()
272+
+ conceptId.hashCode()
273+
+ relatedQuestionAttempts.hashCode()
274+
));
275+
Response cachedResponse = generateCachedResponse(request, etag);
276+
if (cachedResponse != null) {
277+
return cachedResponse;
278+
}
279+
280+
// Augment related content with question attempts:
281+
augmentContentWithRelatedContent(content, relatedQuestionAttempts);
282+
283+
// Log the request:
265284
ImmutableMap<String, String> logEntry = new ImmutableMap.Builder<String, String>()
266285
.put(CONCEPT_ID_LOG_FIELDNAME, conceptId)
267286
.put(CONTENT_VERSION_FIELDNAME, this.contentManager.getCurrentContentSHA())
268287
.build();
269-
270-
// the request log
271-
getLogManager().logEvent(userManager.getCurrentUser(servletRequest), servletRequest,
272-
IsaacServerLogType.VIEW_CONCEPT, logEntry);
288+
getLogManager().logEvent(user, servletRequest, IsaacServerLogType.VIEW_CONCEPT, logEntry);
273289

274290
return Response.ok(content)
275-
.cacheControl(getCacheControl(NUMBER_SECONDS_IN_ONE_HOUR, true))
291+
.cacheControl(getCacheControl(NEVER_CACHE_WITHOUT_ETAG_CHECK, false))
276292
.tag(etag)
277293
.build();
278294
} else {

0 commit comments

Comments
 (0)