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

enhance(packages/graphql): store correctness along open answers in live quiz results #4360

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Changes from all 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
107 changes: 78 additions & 29 deletions packages/graphql/src/services/liveQuizzes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import {
gradeQuestionFreeText,
gradeQuestionNumerical,
} from '@klicker-uzh/grading'
import {
AccessMode,
ConfusionTimestep,
Expand Down Expand Up @@ -59,6 +63,7 @@ async function getCachedBlockResults({
redisMulti.hgetall(`s:${quizId}:lb`)
redisMulti.hgetall(`s:${quizId}:b:${blockId}:lb`)
activeInstanceIds.forEach((instanceId) => {
redisMulti.hgetall(`s:${quizId}:i:${instanceId}:info`)
redisMulti.hgetall(`s:${quizId}:i:${instanceId}:responseHashes`)
redisMulti.hgetall(`s:${quizId}:i:${instanceId}:responses`)
redisMulti.hgetall(`s:${quizId}:i:${instanceId}:results`)
Expand All @@ -81,6 +86,7 @@ async function processCachedData({
const instanceResults: Record<
string,
{
info: Record<string, string>
responseHashes: Record<string, string>
responses: Record<string, string>
anonymousResults:
Expand All @@ -90,14 +96,45 @@ async function processCachedData({
| ElementResultsContent
}
> = mappedResults.slice(2).reduce((acc, cacheObj, ix) => {
const ixMod = ix % 3
const ixMod = ix % 4
const instance = activeBlock.elements[Math.floor((ix - ixMod) / 3)]

if (!instance) return acc

switch (ixMod) {
// compute element instance results from cache entries
case 2: {

// instance info / solutions
case 0:
return {
...acc,
[instance.id]: {
...acc[instance.id],
info: cacheObj,
},
}

// response hashes
case 1:
return {
...acc,
[instance.id]: {
...acc[instance.id],
responseHashes: cacheObj,
},
}

// responses
case 2:
return {
...acc,
[instance.id]: {
...acc[instance.id],
responses: cacheObj,
},
}

case 3: {
// TODO: if possible, split up results and anonymous results here (potentially the cache content needs to augmented)
let anonymousResults:
| ElementResultsChoices
Expand Down Expand Up @@ -132,15 +169,47 @@ async function processCachedData({
omitBy(cacheObj, (_, key) => key === 'participants')
).reduce<Record<string, { value: string; count: number }>>(
(responses_acc, [responseHash, count]) => {
const solutions = JSON.parse(
acc[instance.id]?.['info']?.solutions
)
const response =
acc[instance.id]?.['responseHashes'][responseHash] ??
responseHash

let grading: number | undefined
if (solutions && solutions.length > 0) {
if (instance.elementType === ElementType.NUMERICAL) {
grading =
gradeQuestionNumerical({
response,
solutionRanges: solutions,
}) ?? undefined
} else if (instance.elementType === ElementType.FREE_TEXT) {
grading =
gradeQuestionFreeText({
response,
solutions,
}) ?? undefined
}
}

const updatedResponse = {
value:
acc[instance.id]?.['responseHashes'][responseHash] ??
responseHash,
count:
(responses_acc[responseHash]?.count ?? 0) + parseInt(count),
}

return {
...responses_acc,
[responseHash]: {
value:
acc[instance.id]?.['responseHashes'][responseHash] ??
responseHash,
count:
(responses_acc[responseHash]?.count ?? 0) + parseInt(count),
},
[responseHash]:
typeof grading !== 'undefined'
? {
...updatedResponse,
correct: grading === 1 ? true : false,
}
: updatedResponse,
}
},
{}
Expand All @@ -161,26 +230,6 @@ async function processCachedData({
}
}

// responses
case 1:
return {
...acc,
[instance.id]: {
...acc[instance.id],
responses: cacheObj,
},
}

// response hashes
case 0:
return {
...acc,
[instance.id]: {
...acc[instance.id],
responseHashes: cacheObj,
},
}

default:
return acc
}
Expand Down
Loading