From 50fff210a3d29c6df00b2d1412b6c9dfcba448a3 Mon Sep 17 00:00:00 2001 From: Zhao Jingjing <54243224+zhaojj2209@users.noreply.github.com> Date: Fri, 10 Feb 2023 11:54:20 +0800 Subject: [PATCH] [#12065] Student viewing rubric results: average doesn't match (#12066) * Fix rubric stats calculation * Fix tests --- .../rubric-question-statistics-calculation.ts | 40 +++++++------------ .../test-data/rubricQuestionResponses.json | 12 +++--- .../session-result-csv.service.spec.ts.snap | 4 +- 3 files changed, 22 insertions(+), 34 deletions(-) diff --git a/src/web/app/components/question-types/question-statistics/question-statistics-calculation/rubric-question-statistics-calculation.ts b/src/web/app/components/question-types/question-statistics/question-statistics-calculation/rubric-question-statistics-calculation.ts index 7109405d74f..7612d1a4bfb 100644 --- a/src/web/app/components/question-types/question-statistics/question-statistics-calculation/rubric-question-statistics-calculation.ts +++ b/src/web/app/components/question-types/question-statistics/question-statistics-calculation/rubric-question-statistics-calculation.ts @@ -124,18 +124,19 @@ export class RubricQuestionStatisticsCalculation for (const recipient of Object.keys(this.perRecipientStatsMap)) { const perRecipientStats: PerRecipientStats = this.perRecipientStatsMap[recipient]; + // Answers sum = number of answers in each column perRecipientStats.answersSum = this.calculateAnswersSum(perRecipientStats.answers); perRecipientStats.percentages = this.calculatePercentages(perRecipientStats.answers); - perRecipientStats.percentagesAverage = - this.calculatePercentagesAverage(perRecipientStats.percentages, - this.calculateNumResponses(perRecipientStats.answersSum)); + perRecipientStats.percentagesAverage = this.calculatePercentagesAverage(perRecipientStats.answersSum); perRecipientStats.subQuestionWeightAverage = this.calculateSubQuestionWeightAverage(perRecipientStats.answers); perRecipientStats.weightsAverage = this.calculateWeightsAverage(this.weights); - perRecipientStats.overallWeightedSum = this.calculateWeightedSum(perRecipientStats.percentagesAverage, - perRecipientStats.answersSum, perRecipientStats.weightsAverage); + // Overall weighted sum = sum of total chosen weight for all sub questions + perRecipientStats.overallWeightedSum = + +(perRecipientStats.subQuestionTotalChosenWeight.reduce((a, b) => a + b)).toFixed(2); + // Overall weighted average = overall weighted sum / total number of responses perRecipientStats.overallWeightAverage = +(perRecipientStats.overallWeightedSum - / (this.calculateNumResponses(perRecipientStats.answersSum))).toFixed(2); + / this.calculateNumResponses(perRecipientStats.answersSum)).toFixed(2); } } @@ -195,32 +196,19 @@ export class RubricQuestionStatisticsCalculation } // Calculate percentage average for each column - private calculatePercentagesAverage(percentages: number[][], numAnswers: number): number[] { - // Calculate sum of percentages for each column - const sums: number[] = this.calculateAnswersSum(percentages); + private calculatePercentagesAverage(answersSum: number[]): number[] { + // Calculate total number of responses + const numResponses = this.calculateNumResponses(answersSum); const averages: number[] = []; - // Divide each percentage by the number of answers - for (let i: number = 0; i < sums.length; i += 1) { - averages[i] = numAnswers === 0 ? 0 : +(sums[i] / numAnswers).toFixed(2); + // Divide each column sum by total number of responses, then convert to percentage + for (let i: number = 0; i < answersSum.length; i += 1) { + averages[i] = numResponses === 0 ? 0 : +(answersSum[i] * 100 / numResponses).toFixed(2); } return averages; } - // Calculate weighted sum of responses - private calculateWeightedSum(percentages: number[], answers: number[], weights: number[]): number { - let sum: number = 0; - for (let i: number = 0; i < answers.length; i += 1) { - sum += (percentages[i] / 100) * answers[i] * weights[i]; - } - return +sum.toFixed(2); - } - // Calculate total number of responses private calculateNumResponses(answersSum: number[]): number { - let num: number = 0; - for (let i = 0; i < answersSum.length; i += 1) { - num += answersSum[i]; - } - return num; + return answersSum.reduce((a, b) => a + b); } } diff --git a/src/web/app/components/question-types/question-statistics/test-data/rubricQuestionResponses.json b/src/web/app/components/question-types/question-statistics/test-data/rubricQuestionResponses.json index f9a40ead32b..c829f05d5b5 100644 --- a/src/web/app/components/question-types/question-statistics/test-data/rubricQuestionResponses.json +++ b/src/web/app/components/question-types/question-statistics/test-data/rubricQuestionResponses.json @@ -66,15 +66,15 @@ "percentages": [ [50, 50], [50, 50], [100, 0] ], - "percentagesAverage": [33.33, 16.67], + "percentagesAverage": [66.67, 33.33], "recipientEmail": "alice@gmail.com", "recipientName": "Alice", "recipientTeam": "Team 1", "subQuestionTotalChosenWeight": [1, 1, 0.8], "subQuestionWeightAverage": [0.5, 0.5, 0.4], "weightsAverage": [0.23, 0.77], - "overallWeightedSum": 0.56, - "overallWeightAverage": 0.09 + "overallWeightedSum": 2.8, + "overallWeightAverage": 0.47 }, "Bob": { "answers": [ @@ -84,15 +84,15 @@ "percentages": [ [100, 0], [50, 50], [100, 0] ], - "percentagesAverage": [41.67, 8.33], + "percentagesAverage": [83.33, 16.67], "recipientEmail": "bob@gmail.com", "recipientName": "Bob", "recipientTeam": "Team 2", "subQuestionTotalChosenWeight": [0.4, 1, 0.8], "subQuestionWeightAverage": [0.2, 0.5, 0.4], "weightsAverage": [0.23, 0.77], - "overallWeightedSum": 0.54, - "overallWeightAverage": 0.09 + "overallWeightedSum": 2.2, + "overallWeightAverage": 0.37 } } } diff --git a/src/web/services/__snapshots__/session-result-csv.service.spec.ts.snap b/src/web/services/__snapshots__/session-result-csv.service.spec.ts.snap index 3b1eb280bb9..d9868f4194b 100644 --- a/src/web/services/__snapshots__/session-result-csv.service.spec.ts.snap +++ b/src/web/services/__snapshots__/session-result-csv.service.spec.ts.snap @@ -392,8 +392,8 @@ Team,Recipient Name,Recipient Email,Sub Question,Yes,No,Total,Average Per Recipient Statistics (Overall) Team,Recipient Name,Recipient Email,Yes,No,Total,Average -\\"Team 1.1'\\"\\"\\",\\"student1 In Course1'\\"\\"\\",student1InCourse1@gmail.tmt,50% (1) [1.25],50% (1) [-1.7],-0.22,-0.11 -\\"Team 1.1'\\"\\"\\",student2 In Course1,student2InCourse1@gmail.tmt,50% (1) [1.25],50% (1) [-1.7],-0.22,-0.11 +\\"Team 1.1'\\"\\"\\",\\"student1 In Course1'\\"\\"\\",student1InCourse1@gmail.tmt,50% (1) [1.25],50% (1) [-1.7],-0.45,-0.23 +\\"Team 1.1'\\"\\"\\",student2 In Course1,student2InCourse1@gmail.tmt,50% (1) [1.25],50% (1) [-1.7],-0.45,-0.23 \\"Team 1.1'\\"\\"\\",student3 In Course1,student3InCourse1@gmail.tmt,100% (2) [1.25],0% (0) [-1.7],2.5,1.25 \\"Team 1.1'\\"\\"\\",student4 In Course1,student4InCourse1@gmail.tmt,100% (1) [1.25],0% (0) [-1.7],1.25,1.25