Skip to content

Commit 50fff21

Browse files
authored
[#12065] Student viewing rubric results: average doesn't match (#12066)
* Fix rubric stats calculation * Fix tests
1 parent 25f6327 commit 50fff21

File tree

3 files changed

+22
-34
lines changed

3 files changed

+22
-34
lines changed

src/web/app/components/question-types/question-statistics/question-statistics-calculation/rubric-question-statistics-calculation.ts

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,19 @@ export class RubricQuestionStatisticsCalculation
124124
for (const recipient of Object.keys(this.perRecipientStatsMap)) {
125125
const perRecipientStats: PerRecipientStats = this.perRecipientStatsMap[recipient];
126126

127+
// Answers sum = number of answers in each column
127128
perRecipientStats.answersSum = this.calculateAnswersSum(perRecipientStats.answers);
128129
perRecipientStats.percentages = this.calculatePercentages(perRecipientStats.answers);
129-
perRecipientStats.percentagesAverage =
130-
this.calculatePercentagesAverage(perRecipientStats.percentages,
131-
this.calculateNumResponses(perRecipientStats.answersSum));
130+
perRecipientStats.percentagesAverage = this.calculatePercentagesAverage(perRecipientStats.answersSum);
132131
perRecipientStats.subQuestionWeightAverage =
133132
this.calculateSubQuestionWeightAverage(perRecipientStats.answers);
134133
perRecipientStats.weightsAverage = this.calculateWeightsAverage(this.weights);
135-
perRecipientStats.overallWeightedSum = this.calculateWeightedSum(perRecipientStats.percentagesAverage,
136-
perRecipientStats.answersSum, perRecipientStats.weightsAverage);
134+
// Overall weighted sum = sum of total chosen weight for all sub questions
135+
perRecipientStats.overallWeightedSum =
136+
+(perRecipientStats.subQuestionTotalChosenWeight.reduce((a, b) => a + b)).toFixed(2);
137+
// Overall weighted average = overall weighted sum / total number of responses
137138
perRecipientStats.overallWeightAverage = +(perRecipientStats.overallWeightedSum
138-
/ (this.calculateNumResponses(perRecipientStats.answersSum))).toFixed(2);
139+
/ this.calculateNumResponses(perRecipientStats.answersSum)).toFixed(2);
139140
}
140141
}
141142

@@ -195,32 +196,19 @@ export class RubricQuestionStatisticsCalculation
195196
}
196197

197198
// Calculate percentage average for each column
198-
private calculatePercentagesAverage(percentages: number[][], numAnswers: number): number[] {
199-
// Calculate sum of percentages for each column
200-
const sums: number[] = this.calculateAnswersSum(percentages);
199+
private calculatePercentagesAverage(answersSum: number[]): number[] {
200+
// Calculate total number of responses
201+
const numResponses = this.calculateNumResponses(answersSum);
201202
const averages: number[] = [];
202-
// Divide each percentage by the number of answers
203-
for (let i: number = 0; i < sums.length; i += 1) {
204-
averages[i] = numAnswers === 0 ? 0 : +(sums[i] / numAnswers).toFixed(2);
203+
// Divide each column sum by total number of responses, then convert to percentage
204+
for (let i: number = 0; i < answersSum.length; i += 1) {
205+
averages[i] = numResponses === 0 ? 0 : +(answersSum[i] * 100 / numResponses).toFixed(2);
205206
}
206207
return averages;
207208
}
208209

209-
// Calculate weighted sum of responses
210-
private calculateWeightedSum(percentages: number[], answers: number[], weights: number[]): number {
211-
let sum: number = 0;
212-
for (let i: number = 0; i < answers.length; i += 1) {
213-
sum += (percentages[i] / 100) * answers[i] * weights[i];
214-
}
215-
return +sum.toFixed(2);
216-
}
217-
218210
// Calculate total number of responses
219211
private calculateNumResponses(answersSum: number[]): number {
220-
let num: number = 0;
221-
for (let i = 0; i < answersSum.length; i += 1) {
222-
num += answersSum[i];
223-
}
224-
return num;
212+
return answersSum.reduce((a, b) => a + b);
225213
}
226214
}

src/web/app/components/question-types/question-statistics/test-data/rubricQuestionResponses.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@
6666
"percentages": [
6767
[50, 50], [50, 50], [100, 0]
6868
],
69-
"percentagesAverage": [33.33, 16.67],
69+
"percentagesAverage": [66.67, 33.33],
7070
"recipientEmail": "[email protected]",
7171
"recipientName": "Alice",
7272
"recipientTeam": "Team 1",
7373
"subQuestionTotalChosenWeight": [1, 1, 0.8],
7474
"subQuestionWeightAverage": [0.5, 0.5, 0.4],
7575
"weightsAverage": [0.23, 0.77],
76-
"overallWeightedSum": 0.56,
77-
"overallWeightAverage": 0.09
76+
"overallWeightedSum": 2.8,
77+
"overallWeightAverage": 0.47
7878
},
7979
"Bob": {
8080
"answers": [
@@ -84,15 +84,15 @@
8484
"percentages": [
8585
[100, 0], [50, 50], [100, 0]
8686
],
87-
"percentagesAverage": [41.67, 8.33],
87+
"percentagesAverage": [83.33, 16.67],
8888
"recipientEmail": "[email protected]",
8989
"recipientName": "Bob",
9090
"recipientTeam": "Team 2",
9191
"subQuestionTotalChosenWeight": [0.4, 1, 0.8],
9292
"subQuestionWeightAverage": [0.2, 0.5, 0.4],
9393
"weightsAverage": [0.23, 0.77],
94-
"overallWeightedSum": 0.54,
95-
"overallWeightAverage": 0.09
94+
"overallWeightedSum": 2.2,
95+
"overallWeightAverage": 0.37
9696
}
9797
}
9898
}

src/web/services/__snapshots__/session-result-csv.service.spec.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,8 @@ Team,Recipient Name,Recipient Email,Sub Question,Yes,No,Total,Average
392392
393393
Per Recipient Statistics (Overall)
394394
Team,Recipient Name,Recipient Email,Yes,No,Total,Average
395-
\\"Team 1.1</td></div>'\\"\\"\\",\\"student1 In Course1</td></div>'\\"\\"\\",[email protected],50% (1) [1.25],50% (1) [-1.7],-0.22,-0.11
396-
\\"Team 1.1</td></div>'\\"\\"\\",student2 In Course1,[email protected],50% (1) [1.25],50% (1) [-1.7],-0.22,-0.11
395+
\\"Team 1.1</td></div>'\\"\\"\\",\\"student1 In Course1</td></div>'\\"\\"\\",[email protected],50% (1) [1.25],50% (1) [-1.7],-0.45,-0.23
396+
\\"Team 1.1</td></div>'\\"\\"\\",student2 In Course1,[email protected],50% (1) [1.25],50% (1) [-1.7],-0.45,-0.23
397397
\\"Team 1.1</td></div>'\\"\\"\\",student3 In Course1,[email protected],100% (2) [1.25],0% (0) [-1.7],2.5,1.25
398398
\\"Team 1.1</td></div>'\\"\\"\\",student4 In Course1,[email protected],100% (1) [1.25],0% (0) [-1.7],1.25,1.25
399399

0 commit comments

Comments
 (0)