@khanacademy/perseus-score 4.0.1 → 4.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/es/index.js CHANGED
@@ -1,2296 +1,89 @@
1
- import _extends from '@babel/runtime/helpers/extends';
2
1
  import * as KAS from '@khanacademy/kas';
3
2
  import { KhanMath, geometry, angles, coefficients, number } from '@khanacademy/kmath';
4
3
  import { PerseusError, Errors, getDecimalSeparator, GrapherUtil, approximateDeepEqual, approximateEqual, deepClone, getMatrixSize, getWidgetIdsFromContent, getUpgradedWidgetOptions } from '@khanacademy/perseus-core';
5
4
  import _ from 'underscore';
6
5
 
7
- const MISSING_PERCENT_ERROR = "MISSING_PERCENT_ERROR";
8
- const NEEDS_TO_BE_SIMPLIFIED_ERROR = "NEEDS_TO_BE_SIMPLIFIED_ERROR";
9
- const APPROXIMATED_PI_ERROR = "APPROXIMATED_PI_ERROR";
10
- const EXTRA_SYMBOLS_ERROR = "EXTRA_SYMBOLS_ERROR";
11
- const WRONG_CASE_ERROR = "WRONG_CASE_ERROR";
12
- const WRONG_LETTER_ERROR = "WRONG_LETTER_ERROR";
13
- const MULTIPLICATION_SIGN_ERROR = "MULTIPLICATION_SIGN_ERROR";
14
- const INVALID_SELECTION_ERROR = "INVALID_SELECTION_ERROR";
15
- const CHOOSE_CORRECT_NUM_ERROR = "CHOOSE_CORRECT_NUM_ERROR";
16
- const NOT_NONE_ABOVE_ERROR = "NOT_NONE_ABOVE_ERROR";
17
- const FILL_ALL_CELLS_ERROR = "FILL_ALL_CELLS_ERROR";
18
- const ErrorCodes = {
19
- MISSING_PERCENT_ERROR,
20
- NEEDS_TO_BE_SIMPLIFIED_ERROR,
21
- APPROXIMATED_PI_ERROR,
22
- EXTRA_SYMBOLS_ERROR,
23
- WRONG_CASE_ERROR,
24
- WRONG_LETTER_ERROR,
25
- MULTIPLICATION_SIGN_ERROR,
26
- INVALID_SELECTION_ERROR,
27
- CHOOSE_CORRECT_NUM_ERROR,
28
- NOT_NONE_ABOVE_ERROR,
29
- FILL_ALL_CELLS_ERROR
30
- };
6
+ const MISSING_PERCENT_ERROR="MISSING_PERCENT_ERROR";const NEEDS_TO_BE_SIMPLIFIED_ERROR="NEEDS_TO_BE_SIMPLIFIED_ERROR";const APPROXIMATED_PI_ERROR="APPROXIMATED_PI_ERROR";const EXTRA_SYMBOLS_ERROR="EXTRA_SYMBOLS_ERROR";const WRONG_CASE_ERROR="WRONG_CASE_ERROR";const WRONG_LETTER_ERROR="WRONG_LETTER_ERROR";const MULTIPLICATION_SIGN_ERROR="MULTIPLICATION_SIGN_ERROR";const INVALID_SELECTION_ERROR="INVALID_SELECTION_ERROR";const CHOOSE_CORRECT_NUM_ERROR="CHOOSE_CORRECT_NUM_ERROR";const NOT_NONE_ABOVE_ERROR="NOT_NONE_ABOVE_ERROR";const FILL_ALL_CELLS_ERROR="FILL_ALL_CELLS_ERROR";const ErrorCodes={MISSING_PERCENT_ERROR,NEEDS_TO_BE_SIMPLIFIED_ERROR,APPROXIMATED_PI_ERROR,EXTRA_SYMBOLS_ERROR,WRONG_CASE_ERROR,WRONG_LETTER_ERROR,MULTIPLICATION_SIGN_ERROR,INVALID_SELECTION_ERROR,CHOOSE_CORRECT_NUM_ERROR,NOT_NONE_ABOVE_ERROR,FILL_ALL_CELLS_ERROR};
31
7
 
32
- const MAXERROR_EPSILON = Math.pow(2, -42);
8
+ const MAXERROR_EPSILON=Math.pow(2,-42);const KhanAnswerTypes={predicate:{defaultForms:"integer, proper, improper, mixed, decimal",createValidatorFunctional:function(predicate,options){options=_.extend({simplify:"required",ratio:false,forms:KhanAnswerTypes.predicate.defaultForms},options);let acceptableForms;if(!_.isArray(options.forms)){acceptableForms=options.forms.split(/\s*,\s*/);}else {acceptableForms=options.forms;}if(options.inexact===undefined){options.maxError=0;}options.maxError=+options.maxError+MAXERROR_EPSILON;if(_.contains(acceptableForms,"percent")){acceptableForms=_.without(acceptableForms,"percent");acceptableForms.push("percent");}const fractionTransformer=function(text){text=text.replace(/\u2212/,"-").replace(/([+-])\s+/g,"$1").replace(/(^\s*)|(\s*$)/gi,"");const match=text.match(/^([+-]?\d+)\s*\/\s*([+-]?\d+)$/);const mobileDeviceMatch=text.match(/^([+-]?)\\frac\{([+-]?\d+)\}\{([+-]?\d+)\}$/);const parsedInt=parseInt(text,10);if(match||mobileDeviceMatch){let num;let denom;let simplified=true;if(match){num=parseFloat(match[1]);denom=parseFloat(match[2]);}else {num=parseFloat(mobileDeviceMatch[2]);if(mobileDeviceMatch[1]==="-"){if(num<0){simplified=false;}num=-num;}denom=parseFloat(mobileDeviceMatch[3]);}simplified=simplified&&denom>0&&(options.ratio||denom!==1)&&KhanMath.getGCD(num,denom)===1;return [{value:num/denom,exact:simplified}]}if(!isNaN(parsedInt)&&""+parsedInt===text){return [{value:parsedInt,exact:true}]}return []};const forms={integer:function(text){const decimal=forms.decimal(text);const rounded=forms.decimal(text,1);if(decimal[0].value!=null&&decimal[0].value===rounded[0].value||decimal[1].value!=null&&decimal[1].value===rounded[1].value){return decimal}return []},proper:function(text){const transformed=fractionTransformer(text);return transformed.flatMap(o=>{if(Math.abs(o.value)<1){return [o]}return []})},improper:function(text){const fractionExists=text.includes("/")||text.match(/\\(d?frac)/);if(!fractionExists){return []}const transformed=fractionTransformer(text);return transformed.flatMap(o=>{if(Math.abs(o.value)>=1){return [o]}return []})},pi:function(text){let match;let possibilities=[];text=text.replace(/\u2212/,"-");if(match=text.match(/^([+-]?)\s*(\\?pi|p|\u03c0|\\?tau|t|\u03c4|pau)$/i)){possibilities=[{value:parseFloat(match[1]+"1"),exact:true}];}else if(match=text.match(/^([+-]?\s*\d+\s*(?:\/\s*[+-]?\s*\d+)?)\s*\*?\s*(\\?pi|p|\u03c0|\\?tau|t|\u03c4|pau)$/i)){possibilities=fractionTransformer(match[1]);}else if(match=text.match(/^([+-]?)\s*(\d+)\s*([+-]?\d+)\s*\/\s*([+-]?\d+)\s*\*?\s*(\\?pi|p|\u03c0|\\?tau|t|\u03c4|pau)$/i)){const sign=parseFloat(match[1]+"1");const integ=parseFloat(match[2]);const num=parseFloat(match[3]);const denom=parseFloat(match[4]);const simplified=num<denom&&KhanMath.getGCD(num,denom)===1;possibilities=[{value:sign*(integ+num/denom),exact:simplified}];}else if(match=text.match(/^([+-]?\s*\d+)\s*\*?\s*(\\?pi|p|\u03c0|\\?tau|t|\u03c4|pau)\s*(?:\/\s*([+-]?\s*\d+))?$/i)){possibilities=fractionTransformer(match[1]+"/"+match[3]);}else if(match=text.match(/^([+-]?)\s*\*?\s*(\\?pi|p|\u03c0|\\?tau|t|\u03c4|pau)\s*(?:\/\s*([+-]?\d+))?$/i)){possibilities=fractionTransformer(match[1]+"1/"+match[3]);}else if(text==="0"){possibilities=[{value:0,exact:true}];}else if(match=text.match(/^(.+)\s*\*?\s*(\\?pi|p|\u03c0|\\?tau|t|\u03c4|pau)$/i)){possibilities=forms.decimal(match[1]);}else {possibilities=_.reduce(KhanAnswerTypes.predicate.defaultForms.split(/\s*,\s*/),function(memo,form){return memo.concat(forms[form](text))},[]);let approximatesPi=false;const number=parseFloat(text);if(!isNaN(number)&&number!==parseInt(text)){const piMult=Math.PI/12;const roundedNumber=piMult*Math.round(number/piMult);if(Math.abs(number-roundedNumber)<.01){approximatesPi=true;}}else if(text.match(/\/\s*7/)){approximatesPi=true;}if(approximatesPi){_.each(possibilities,function(possibility){possibility.piApprox=true;});}return possibilities}let multiplier=Math.PI;if(text.match(/\\?tau|t|\u03c4/)){multiplier=Math.PI*2;}if(text.match(/pau/)){multiplier=Math.PI*1.5;}possibilities.forEach(possibility=>{possibility.value*=multiplier;});return possibilities},coefficient:function(text){let possibilities=[];text=text.replace(/\u2212/,"-");if(text===""){possibilities=[{value:1,exact:true}];}else if(text==="-"){possibilities=[{value:-1,exact:true}];}return possibilities},log:function(text){let match;let possibilities=[];text=text.replace(/\u2212/,"-");text=text.replace(/[ \(\)]/g,"");if(match=text.match(/^log\s*(\S+)\s*$/i)){possibilities=forms.decimal(match[1]);}else if(text==="0"){possibilities=[{value:0,exact:true}];}return possibilities},percent:function(text){text=String(text).trim();let hasPercentSign=false;if(text.indexOf("%")===text.length-1){text=text.substring(0,text.length-1).trim();hasPercentSign=true;}const transformed=forms.decimal(text);transformed.forEach(t=>{t.exact=hasPercentSign;t.value=t.value/100;});return transformed},mixed:function(text){const match=text.replace(/\u2212/,"-").replace(/([+-])\s+/g,"$1").match(/^([+-]?)(\d+)\s+(\d+)\s*\/\s*(\d+)$/);if(match){const sign=parseFloat(match[1]+"1");const integ=parseFloat(match[2]);const num=parseFloat(match[3]);const denom=parseFloat(match[4]);const simplified=num<denom&&KhanMath.getGCD(num,denom)===1;return [{value:sign*(integ+num/denom),exact:simplified}]}return []},decimal:function(text,precision=1e10){const normal=function(text){text=String(text).trim();const match=text.replace(/\u2212/,"-").replace(/([+-])\s+/g,"$1").match(/^([+-]?(?:\d{1,3}(?:[, ]?\d{3})*\.?|\d{0,3}(?:[, ]?\d{3})*\.(?:\d{3}[, ]?)*\d{1,3}))$/);const badLeadingZero=text.match(/^0[0,]*,/);if(match&&!badLeadingZero){let x=parseFloat(match[1].replace(/[, ]/g,""));if(options.inexact===undefined){x=Math.round(x*precision)/precision;}return x}};const commas=function(text){text=text.replace(/([\.,])/g,function(_,c){return c==="."?",":"."});return normal(text)};return [{value:normal(text),exact:true},{value:commas(text),exact:true}]}};return function(guess){const fallback=options.fallback!=null?""+options.fallback:"";guess=String(guess).trim()||fallback;const score={empty:guess==="",correct:false,message:null,guess:guess};const findCorrectAnswer=()=>{for(const form of acceptableForms){const transformed=forms[form](guess);for(let j=0,l=transformed.length;j<l;j++){const val=transformed[j].value;const exact=transformed[j].exact;const piApprox=transformed[j].piApprox;if(predicate(val,options.maxError)){if(exact||options.simplify==="optional"){score.correct=true;score.message=options.message||null;score.empty=false;}else if(form==="percent"){score.empty=true;score.message=ErrorCodes.MISSING_PERCENT_ERROR;}else {if(options.simplify!=="enforced"){score.empty=true;}score.message=ErrorCodes.NEEDS_TO_BE_SIMPLIFIED_ERROR;}return false}if(piApprox&&predicate(val,Math.abs(val*.001))){score.empty=true;score.message=ErrorCodes.APPROXIMATED_PI_ERROR;}}}};findCorrectAnswer();if(score.correct===false){let interpretedGuess=false;_.each(forms,function(form){const anyAreNaN=_.any(form(guess),function(t){return t.value!=null&&!_.isNaN(t.value)});if(anyAreNaN){interpretedGuess=true;}});if(!interpretedGuess){score.empty=true;score.message=ErrorCodes.EXTRA_SYMBOLS_ERROR;return score}}return score}}},number:{convertToPredicate:function(correctAnswer,options){const correctFloat=parseFloat(correctAnswer);return [function(guess,maxError){return Math.abs(guess-correctFloat)<maxError},{...options,type:"predicate"}]},createValidatorFunctional:function(correctAnswer,options){return KhanAnswerTypes.predicate.createValidatorFunctional(...KhanAnswerTypes.number.convertToPredicate(correctAnswer,options))}},expression:{parseSolution:function(solutionString,options){let solution=KAS.parse(solutionString,options);if(!solution.parsed){throw new PerseusError("The provided solution ("+solutionString+") didn't parse.",Errors.InvalidInput)}else if(options.simplified&&!solution.expr.isSimplified()){throw new PerseusError("The provided solution ("+solutionString+") isn't fully expanded and simplified.",Errors.InvalidInput)}else {solution=solution.expr;}return solution},createValidatorFunctional:function(solution,options){return function(guess){const score={empty:false,correct:false,message:null,guess:guess,ungraded:false};if(!guess){score.empty=true;return score}const answer=KAS.parse(guess,options);if(!answer.parsed){score.empty=true;return score}if(typeof solution==="string"){solution=KhanAnswerTypes.expression.parseSolution(solution,options);}const result=KAS.compare(answer.expr,solution,options);if(result.equal){score.correct=true;}else if(result.wrongVariableNames||result.wrongVariableCase){score.ungraded=true;score.message=result.wrongVariableCase?ErrorCodes.WRONG_CASE_ERROR:ErrorCodes.WRONG_LETTER_ERROR;score.suppressAlmostThere=true;}else if(result.message){score.message=result.message;}else {const answerX=KAS.parse(guess.replace(/[xX]/g,"*"),options);if(answerX.parsed){const resultX=KAS.compare(answerX.expr,solution,options);if(resultX.equal){score.ungraded=true;score.message=ErrorCodes.MULTIPLICATION_SIGN_ERROR;}else if(resultX.message){score.message=resultX.message+" Also, I'm a computer. I only understand "+"multiplication if you use an "+"asterisk (*) as the multiplication "+"sign.";}}}return score}}}};
33
9
 
34
- // TOOD(kevinb): Figure out how this relates to KEScore in
35
- // perseus-all-package/types.js and see if there's a way to
36
- // unify these types.
10
+ function scoreCategorizer(userInput,rubric){let allCorrect=true;rubric.values.forEach((value,i)=>{if(userInput.values[i]!==value){allCorrect=false;}});return {type:"points",earned:allCorrect?1:0,total:1,message:null}}
37
11
 
38
- /**
39
- * Answer types
40
- *
41
- * Utility for creating answerable questions displayed in exercises
42
- *
43
- * Different answer types produce different kinds of input displays, and do
44
- * different kinds of checking on the solutions.
45
- *
46
- * Each of the objects contain two functions, setup and createValidator.
47
- *
48
- * The setup function takes a solutionarea and solution, and performs setup
49
- * within the solutionarea, and then returns an object which contains:
50
- *
51
- * answer: a function which, when called, will retrieve the current answer from
52
- * the solutionarea, which can then be validated using the validator
53
- * function
54
- * validator: a function returned from the createValidator function (defined
55
- * below)
56
- * solution: the correct answer to the problem
57
- * showGuess: a function which, when given a guess, shows the guess within the
58
- * provided solutionarea
59
- * showGuessCustom: a function which displays parts of a guess that are not
60
- * within the solutionarea; currently only used for custom
61
- * answers
62
- *
63
- * The createValidator function only takes a solution, and it returns a
64
- * function which can be used to validate an answer.
65
- *
66
- * The resulting validator function returns:
67
- * - true: if the answer is fully correct
68
- * - false: if the answer is incorrect
69
- * - "" (the empty string): if no answer has been provided (e.g. the answer box
70
- * is left unfilled)
71
- * - a string: if there is some slight error
72
- *
73
- * In most cases, setup and createValidator don't really need the solution DOM
74
- * element so we have setupFunctional and createValidatorFunctional for them
75
- * which take only $solution.text() and $solution.data(). This makes it easier
76
- * to reuse specific answer types.
77
- *
78
- * TODO(alpert): Think of a less-absurd name for createValidatorFunctional.
79
- *
80
- */
81
- const KhanAnswerTypes = {
82
- /*
83
- * predicate answer type
84
- *
85
- * performs simple predicate-based checking of a numeric solution, with
86
- * different kinds of number formats
87
- *
88
- * Uses the data-forms option on the solution to choose which number formats
89
- * are acceptable. Available data-forms:
90
- *
91
- * - integer: 3
92
- * - proper: 3/5
93
- * - improper: 5/3
94
- * - pi: 3 pi
95
- * - log: log(5)
96
- * - percent: 15%
97
- * - mixed: 1 1/3
98
- * - decimal: 1.7
99
- *
100
- * The solution should be a predicate of the form:
101
- *
102
- * function(guess, maxError) {
103
- * return abs(guess - 3) < maxError;
104
- * }
105
- *
106
- */
107
- predicate: {
108
- defaultForms: "integer, proper, improper, mixed, decimal",
109
- createValidatorFunctional: function (predicate, options) {
110
- // Extract the options from the given solution object
111
- options = _.extend({
112
- simplify: "required",
113
- ratio: false,
114
- forms: KhanAnswerTypes.predicate.defaultForms
115
- }, options);
116
- let acceptableForms;
117
- // this is maintaining backwards compatibility
118
- // TODO(merlob) fix all places that depend on this, then delete
119
- if (!_.isArray(options.forms)) {
120
- acceptableForms = options.forms.split(/\s*,\s*/);
121
- } else {
122
- acceptableForms = options.forms;
123
- }
12
+ function validateCategorizer(userInput,validationData){const incomplete=validationData.items.some((_,i)=>userInput.values[i]==null);if(incomplete){return {type:"invalid",message:ErrorCodes.INVALID_SELECTION_ERROR}}return null}
124
13
 
125
- // TODO(jack): remove options.inexact in favor of options.maxError
126
- if (options.inexact === undefined) {
127
- // If we aren't allowing inexact, ensure that we don't have a
128
- // large maxError as well.
129
- options.maxError = 0;
130
- }
131
- // Allow a small tolerance on maxError, to avoid numerical
132
- // representation issues (2.3 should be correct for a solution of
133
- // 2.45 with maxError=0.15).
134
- options.maxError = +options.maxError + MAXERROR_EPSILON;
14
+ function scoreCSProgram(userInput){if(userInput.status==="correct"){return {type:"points",earned:1,total:1,message:userInput.message||null}}if(userInput.status==="incorrect"){return {type:"points",earned:0,total:1,message:userInput.message||null}}return {type:"invalid",message:"Keep going, you're not there yet!"}}
135
15
 
136
- // If percent is an acceptable form, make sure it's the last one
137
- // in the list so we don't prematurely complain about not having
138
- // a percent sign when the user entered the correct answer in a
139
- // different form (such as a decimal or fraction)
140
- if (_.contains(acceptableForms, "percent")) {
141
- acceptableForms = _.without(acceptableForms, "percent");
142
- acceptableForms.push("percent");
143
- }
16
+ function scoreDropdown(userInput,rubric){const correct=rubric.choices[userInput.value-1].correct;return {type:"points",earned:correct?1:0,total:1,message:null}}
144
17
 
145
- // Take text looking like a fraction, and turn it into a number
146
- const fractionTransformer = function fractionTransformer(text) {
147
- text = text
148
- // Replace unicode minus sign with hyphen
149
- .replace(/\u2212/, "-")
150
- // Remove space after +, -
151
- .replace(/([+-])\s+/g, "$1")
152
- // Remove leading/trailing whitespace
153
- .replace(/(^\s*)|(\s*$)/gi, "");
18
+ function validateDropdown(userInput){if(userInput.value===0){return {type:"invalid",message:null}}return null}
154
19
 
155
- // Extract numerator and denominator
156
- const match = text.match(/^([+-]?\d+)\s*\/\s*([+-]?\d+)$/);
157
- // Fractions are represented as "-\frac{numerator}{denominator}"
158
- // in Mobile device input instead of "numerator/denominator" as
159
- // in web-browser.
160
- const mobileDeviceMatch = text.match(/^([+-]?)\\frac\{([+-]?\d+)\}\{([+-]?\d+)\}$/);
161
- const parsedInt = parseInt(text, 10);
162
- if (match || mobileDeviceMatch) {
163
- let num;
164
- let denom;
165
- let simplified = true;
166
- if (match) {
167
- num = parseFloat(match[1]);
168
- denom = parseFloat(match[2]);
169
- } else {
170
- num = parseFloat(mobileDeviceMatch[2]);
171
- if (mobileDeviceMatch[1] === "-") {
172
- if (num < 0) {
173
- simplified = false;
174
- }
175
- num = -num;
176
- }
177
- denom = parseFloat(mobileDeviceMatch[3]);
178
- }
179
- simplified = simplified && denom > 0 && (options.ratio || denom !== 1) && KhanMath.getGCD(num, denom) === 1;
180
- return [{
181
- value: num / denom,
182
- exact: simplified
183
- }];
184
- }
185
- if (!isNaN(parsedInt) && "" + parsedInt === text) {
186
- return [{
187
- value: parsedInt,
188
- exact: true
189
- }];
190
- }
191
- return [];
192
- };
20
+ function scoreExpression(userInput,rubric,locale){const options=_.clone(rubric);_.extend(options,{decimal_separator:getDecimalSeparator(locale)});const createValidator=answer=>{const expression=KAS.parse(answer.value,rubric);if(!expression.parsed){throw new PerseusError("Unable to parse solution answer for expression",Errors.InvalidInput,{metadata:{rubric:JSON.stringify(rubric)}})}return KhanAnswerTypes.expression.createValidatorFunctional(expression.expr,_({}).extend(options,{simplify:answer.simplify,form:answer.form}))};let matchingAnswerForm;let matchMessage;let allEmpty=true;let firstUngradedResult;for(const answerForm of rubric.answerForms||[]){const validator=createValidator(answerForm);if(!validator){continue}const result=validator(userInput);if(result.correct){matchingAnswerForm=answerForm;matchMessage=result.message||"";break}allEmpty=allEmpty&&result.empty;if(answerForm.considered==="correct"&&result.ungraded&&!firstUngradedResult){firstUngradedResult=result;}}if(!matchingAnswerForm){if(firstUngradedResult){return {type:"invalid",message:firstUngradedResult.message,suppressAlmostThere:firstUngradedResult.suppressAlmostThere}}if(allEmpty){return {type:"invalid",message:null}}return {type:"points",earned:0,total:1}}if(matchingAnswerForm.considered==="ungraded"){return {type:"invalid",message:matchMessage}}return {type:"points",earned:matchingAnswerForm.considered==="correct"?1:0,total:1,message:matchMessage}}
193
21
 
194
- /*
195
- * Different forms of numbers
196
- *
197
- * Each function returns a list of objects of the form:
198
- *
199
- * {
200
- * value: numerical value,
201
- * exact: true/false
202
- * }
203
- */
204
- const forms = {
205
- // integer, which is encompassed by decimal
206
- integer: function (text) {
207
- // Compare the decimal form to the decimal form rounded to
208
- // an integer. Only accept if the user actually entered an
209
- // integer.
210
- const decimal = forms.decimal(text);
211
- const rounded = forms.decimal(text, 1);
212
- if (decimal[0].value != null && decimal[0].value === rounded[0].value || decimal[1].value != null && decimal[1].value === rounded[1].value) {
213
- return decimal;
214
- }
215
- return [];
216
- },
217
- // A proper fraction
218
- proper: function (text) {
219
- const transformed = fractionTransformer(text);
220
- return transformed.flatMap(o => {
221
- // All fractions that are less than 1
222
- if (Math.abs(o.value) < 1) {
223
- return [o];
224
- }
225
- return [];
226
- });
227
- },
228
- // an improper fraction
229
- improper: function (text) {
230
- // As our answer keys are always in simplest form, we need
231
- // to check for the existence of a fraction in the input before
232
- // validating the answer. If no fraction is found, we consider
233
- // the answer to be incorrect.
234
- const fractionExists = text.includes("/") || text.match(/\\(d?frac)/);
235
- if (!fractionExists) {
236
- return [];
237
- }
238
- const transformed = fractionTransformer(text);
239
- return transformed.flatMap(o => {
240
- // All fractions that are greater than 1
241
- if (Math.abs(o.value) >= 1) {
242
- return [o];
243
- }
244
- return [];
245
- });
246
- },
247
- // pi-like numbers
248
- pi: function (text) {
249
- let match;
250
- let possibilities = [];
22
+ function validateExpression(userInput){if(userInput===""){return {type:"invalid",message:null}}return null}
251
23
 
252
- // Replace unicode minus sign with hyphen
253
- text = text.replace(/\u2212/, "-");
24
+ function getCoefficientsByType(data){if(data.coords==null){return undefined}if(data.type==="exponential"||data.type==="logarithm"){const grader=GrapherUtil.functionForType(data.type);return grader.getCoefficients(data.coords,data.asymptote)}else if(data.type==="linear"||data.type==="quadratic"||data.type==="absolute_value"||data.type==="sinusoid"||data.type==="tangent"){const grader=GrapherUtil.functionForType(data.type);return grader.getCoefficients(data.coords)}else {throw new PerseusError("Invalid grapher type",Errors.InvalidInput)}}function scoreGrapher(userInput,rubric){if(userInput.type!==rubric.correct.type){return {type:"points",earned:0,total:1,message:null}}if(userInput.coords==null){return {type:"invalid",message:null}}const grader=GrapherUtil.functionForType(userInput.type);const guessCoeffs=getCoefficientsByType(userInput);const correctCoeffs=getCoefficientsByType(rubric.correct);if(guessCoeffs==null||correctCoeffs==null){return {type:"invalid",message:null}}if(grader.areEqual(guessCoeffs,correctCoeffs)){return {type:"points",earned:1,total:1,message:null}}return {type:"points",earned:0,total:1,message:null}}
254
25
 
255
- // - pi
256
- // (Note: we also support \pi (for TeX), p, tau (and \tau,
257
- // and t), pau.)
258
- if (match = text.match(/^([+-]?)\s*(\\?pi|p|\u03c0|\\?tau|t|\u03c4|pau)$/i)) {
259
- possibilities = [{
260
- value: parseFloat(match[1] + "1"),
261
- exact: true
262
- }];
26
+ function scoreIframe(userInput){if(userInput.status==="correct"){return {type:"points",earned:1,total:1,message:userInput.message||null}}if(userInput.status==="incorrect"){return {type:"points",earned:0,total:1,message:userInput.message||null}}return {type:"invalid",message:"Keep going, you're not there yet!"}}
263
27
 
264
- // 5 / 6 pi
265
- } else if (match = text.match(/^([+-]?\s*\d+\s*(?:\/\s*[+-]?\s*\d+)?)\s*\*?\s*(\\?pi|p|\u03c0|\\?tau|t|\u03c4|pau)$/i)) {
266
- possibilities = fractionTransformer(match[1]);
28
+ const{collinear,canonicalSineCoefficients,similar,clockwise}=geometry;const{getClockwiseAngle}=angles;const{getSinusoidCoefficients,getQuadraticCoefficients}=coefficients;function scoreInteractiveGraph(userInput,rubric){if(userInput.type==="none"&&rubric.correct.type==="none"){return {type:"points",earned:0,total:0,message:null}}const hasValue=Boolean(userInput.coords||userInput.center&&userInput.radius);if(userInput.type===rubric.correct.type&&hasValue){if(userInput.type==="linear"&&rubric.correct.type==="linear"&&userInput.coords!=null){const guess=userInput.coords;const correct=rubric.correct.coords;if(collinear(correct[0],correct[1],guess[0])&&collinear(correct[0],correct[1],guess[1])){return {type:"points",earned:1,total:1,message:null}}}else if(userInput.type==="linear-system"&&rubric.correct.type==="linear-system"&&userInput.coords!=null){const guess=userInput.coords;const correct=rubric.correct.coords;if(collinear(correct[0][0],correct[0][1],guess[0][0])&&collinear(correct[0][0],correct[0][1],guess[0][1])&&collinear(correct[1][0],correct[1][1],guess[1][0])&&collinear(correct[1][0],correct[1][1],guess[1][1])||collinear(correct[0][0],correct[0][1],guess[1][0])&&collinear(correct[0][0],correct[0][1],guess[1][1])&&collinear(correct[1][0],correct[1][1],guess[0][0])&&collinear(correct[1][0],correct[1][1],guess[0][1])){return {type:"points",earned:1,total:1,message:null}}}else if(userInput.type==="quadratic"&&rubric.correct.type==="quadratic"&&userInput.coords!=null){const guessCoeffs=getQuadraticCoefficients(userInput.coords);const correctCoeffs=getQuadraticCoefficients(rubric.correct.coords);if(approximateDeepEqual(guessCoeffs,correctCoeffs)){return {type:"points",earned:1,total:1,message:null}}}else if(userInput.type==="sinusoid"&&rubric.correct.type==="sinusoid"&&userInput.coords!=null){const guessCoeffs=getSinusoidCoefficients(userInput.coords);const correctCoeffs=getSinusoidCoefficients(rubric.correct.coords);const canonicalGuessCoeffs=canonicalSineCoefficients(guessCoeffs);const canonicalCorrectCoeffs=canonicalSineCoefficients(correctCoeffs);if(approximateDeepEqual(canonicalGuessCoeffs,canonicalCorrectCoeffs)){return {type:"points",earned:1,total:1,message:null}}}else if(userInput.type==="circle"&&rubric.correct.type==="circle"){if(approximateDeepEqual(userInput.center,rubric.correct.center)&&approximateEqual(userInput.radius,rubric.correct.radius)){return {type:"points",earned:1,total:1,message:null}}}else if(userInput.type==="point"&&rubric.correct.type==="point"&&userInput.coords!=null){let correct=rubric.correct.coords;if(correct==null){throw new Error("Point graph rubric has null coords")}const guess=userInput.coords.slice();correct=correct.slice();guess?.sort();correct.sort();if(approximateDeepEqual(guess,correct)){return {type:"points",earned:1,total:1,message:null}}}else if(userInput.type==="polygon"&&rubric.correct.type==="polygon"&&userInput.coords!=null){const guess=userInput.coords.slice();const correct=rubric.correct.coords.slice();let match;if(rubric.correct.match==="similar"){match=similar(guess,correct,Number.POSITIVE_INFINITY);}else if(rubric.correct.match==="congruent"){match=similar(guess,correct,number.DEFAULT_TOLERANCE);}else if(rubric.correct.match==="approx"){match=similar(guess,correct,.1);}else {guess.sort();correct.sort();match=approximateDeepEqual(guess,correct);}if(match){return {type:"points",earned:1,total:1,message:null}}}else if(userInput.type==="segment"&&rubric.correct.type==="segment"&&userInput.coords!=null){let guess=deepClone(userInput.coords);let correct=deepClone(rubric.correct.coords);guess=_.invoke(guess,"sort").sort();correct=_.invoke(correct,"sort").sort();if(approximateDeepEqual(guess,correct)){return {type:"points",earned:1,total:1,message:null}}}else if(userInput.type==="ray"&&rubric.correct.type==="ray"&&userInput.coords!=null){const guess=userInput.coords;const correct=rubric.correct.coords;if(approximateDeepEqual(guess[0],correct[0])&&collinear(correct[0],correct[1],guess[1])){return {type:"points",earned:1,total:1,message:null}}}else if(userInput.type==="angle"&&rubric.correct.type==="angle"){const coords=userInput.coords;const correct=rubric.correct.coords;const allowReflexAngles=rubric.correct.allowReflexAngles;if(!coords){return {type:"invalid",message:null}}const areClockwise=clockwise([coords[0],coords[2],coords[1]]);const shouldReverseCoords=areClockwise&&!allowReflexAngles;const guess=shouldReverseCoords?coords.slice().reverse():coords;let match;if(rubric.correct.match==="congruent"){const angles=_.map([guess,correct],function(coords){if(!coords){return false}const angle=getClockwiseAngle(coords,allowReflexAngles);return angle});match=approximateEqual(...angles);}else {match=approximateDeepEqual(guess[1],correct[1])&&collinear(correct[1],correct[0],guess[0])&&collinear(correct[1],correct[2],guess[2]);}if(match){return {type:"points",earned:1,total:1,message:null}}}}if(!hasValue||_.isEqual(userInput,rubric.graph)){return {type:"invalid",message:null}}return {type:"points",earned:0,total:1,message:null}}
267
29
 
268
- // 4 5 / 6 pi
269
- } else if (match = text.match(/^([+-]?)\s*(\d+)\s*([+-]?\d+)\s*\/\s*([+-]?\d+)\s*\*?\s*(\\?pi|p|\u03c0|\\?tau|t|\u03c4|pau)$/i)) {
270
- const sign = parseFloat(match[1] + "1");
271
- const integ = parseFloat(match[2]);
272
- const num = parseFloat(match[3]);
273
- const denom = parseFloat(match[4]);
274
- const simplified = num < denom && KhanMath.getGCD(num, denom) === 1;
275
- possibilities = [{
276
- value: sign * (integ + num / denom),
277
- exact: simplified
278
- }];
30
+ function scoreLabelImageMarker(userInput,rubric){const score={hasAnswers:false,isCorrect:false};if(userInput&&userInput.length>0){score.hasAnswers=true;}if(rubric.length>0){if(userInput&&userInput.length===rubric.length){score.isCorrect=userInput.every(choice=>rubric.includes(choice));}}else if(!userInput||userInput.length===0){score.isCorrect=true;}return score}function scoreLabelImage(userInput,rubric){let numCorrect=0;for(let i=0;i<userInput.markers.length;i++){const score=scoreLabelImageMarker(userInput.markers[i].selected,rubric.markers[i].answers);if(score.isCorrect){numCorrect++;}}return {type:"points",earned:numCorrect===userInput.markers.length?1:0,total:1,message:null}}
279
31
 
280
- // 5 pi / 6
281
- } else if (match = text.match(/^([+-]?\s*\d+)\s*\*?\s*(\\?pi|p|\u03c0|\\?tau|t|\u03c4|pau)\s*(?:\/\s*([+-]?\s*\d+))?$/i)) {
282
- possibilities = fractionTransformer(match[1] + "/" + match[3]);
32
+ function scoreMatcher(userInput,rubric){const correct=_.isEqual(userInput.left,rubric.left)&&_.isEqual(userInput.right,rubric.right);return {type:"points",earned:correct?1:0,total:1,message:null}}
283
33
 
284
- // - pi / 4
285
- } else if (match = text.match(/^([+-]?)\s*\*?\s*(\\?pi|p|\u03c0|\\?tau|t|\u03c4|pau)\s*(?:\/\s*([+-]?\d+))?$/i)) {
286
- possibilities = fractionTransformer(match[1] + "1/" + match[3]);
34
+ function scoreMatrix(userInput,rubric){const solution=rubric.answers;const supplied=userInput.answers;const solutionSize=getMatrixSize(solution);const suppliedSize=getMatrixSize(supplied);const incorrectSize=solutionSize[0]!==suppliedSize[0]||solutionSize[1]!==suppliedSize[1];const createValidator=KhanAnswerTypes.number.createValidatorFunctional;let message=null;let incorrect=false;_(suppliedSize[0]).times(row=>{_(suppliedSize[1]).times(col=>{if(!incorrectSize){const validator=createValidator(solution[row][col],{simplify:true});const result=validator(supplied[row][col]);if(result.message){message=result.message;}if(!result.correct){incorrect=true;}}});});if(incorrectSize){return {type:"points",earned:0,total:1,message:null}}return {type:"points",earned:incorrect?0:1,total:1,message:message}}
287
35
 
288
- // 0
289
- } else if (text === "0") {
290
- possibilities = [{
291
- value: 0,
292
- exact: true
293
- }];
36
+ function validateMatrix(userInput){const supplied=userInput.answers;const suppliedSize=getMatrixSize(supplied);for(let row=0;row<suppliedSize[0];row++){for(let col=0;col<suppliedSize[1];col++){if(supplied[row][col]==null||supplied[row][col].toString().length===0){return {type:"invalid",message:ErrorCodes.FILL_ALL_CELLS_ERROR}}}}return null}
294
37
 
295
- // 0.5 pi (fallback)
296
- } else if (match = text.match(/^(.+)\s*\*?\s*(\\?pi|p|\u03c0|\\?tau|t|\u03c4|pau)$/i)) {
297
- possibilities = forms.decimal(match[1]);
298
- } else {
299
- possibilities = _.reduce(KhanAnswerTypes.predicate.defaultForms.split(/\s*,\s*/), function (memo, form) {
300
- return memo.concat(forms[form](text));
301
- }, []);
38
+ function scoreNumberLine(userInput,rubric){const range=rubric.range;const start=rubric.initialX!=null?rubric.initialX:range[0];const startRel=rubric.isInequality?"ge":"eq";const correctRel=rubric.correctRel||"eq";const correctPos=number.equal(userInput.numLinePosition,rubric.correctX||0);if(correctPos&&correctRel===userInput.rel){return {type:"points",earned:1,total:1,message:null}}if(userInput.numLinePosition===start&&userInput.rel===startRel){return {type:"invalid",message:null}}return {type:"points",earned:0,total:1,message:null}}
302
39
 
303
- // If the answer is a floating point number that's
304
- // near a multiple of pi, mark is as being possibly
305
- // an approximation of pi. We actually check if
306
- // it's a plausible approximation of pi/12, since
307
- // sometimes the correct answer is like pi/3 or pi/4.
308
- // We also say it's a pi-approximation if it involves
309
- // x/7 (since 22/7 is an approximation of pi.)
310
- // Never mark an integer as being an approximation
311
- // of pi.
312
- let approximatesPi = false;
313
- const number = parseFloat(text);
314
- if (!isNaN(number) && number !== parseInt(text)) {
315
- const piMult = Math.PI / 12;
316
- const roundedNumber = piMult * Math.round(number / piMult);
317
- if (Math.abs(number - roundedNumber) < 0.01) {
318
- approximatesPi = true;
319
- }
320
- } else if (text.match(/\/\s*7/)) {
321
- approximatesPi = true;
322
- }
323
- if (approximatesPi) {
324
- _.each(possibilities, function (possibility) {
325
- possibility.piApprox = true;
326
- });
327
- }
328
- return possibilities;
329
- }
330
- let multiplier = Math.PI;
331
- if (text.match(/\\?tau|t|\u03c4/)) {
332
- multiplier = Math.PI * 2;
333
- }
40
+ function validateNumberLine(userInput){const divisionRange=userInput.divisionRange;const outsideAllowedRange=userInput.numDivisions>divisionRange[1]||userInput.numDivisions<divisionRange[0];if(userInput.isTickCrtl&&outsideAllowedRange){return {type:"invalid",message:"Number of divisions is outside the allowed range."}}return null}
334
41
 
335
- // We're taking an early stand along side xkcd in the
336
- // inevitable ti vs. pau debate... http://xkcd.com/1292
337
- if (text.match(/pau/)) {
338
- multiplier = Math.PI * 1.5;
339
- }
340
- possibilities.forEach(possibility => {
341
- possibility.value *= multiplier;
342
- });
343
- return possibilities;
344
- },
345
- // Converts '' to 1 and '-' to -1 so you can write "[___] x"
346
- // and accept sane things
347
- coefficient: function (text) {
348
- let possibilities = [];
42
+ function findEndpoint(tex,currentIndex){let bracketDepth=0;for(let i=currentIndex,len=tex.length;i<len;i++){const c=tex[i];if(c==="{"){bracketDepth++;}else if(c==="}"){bracketDepth--;}if(bracketDepth<0){return i}}return tex.length}function parseNextExpression(tex,currentIndex,handler){const openBracketIndex=tex.indexOf("{",currentIndex);if(openBracketIndex===-1){return {endpoint:tex.length,expression:""}}const nextExpIndex=openBracketIndex+1;const endpoint=findEndpoint(tex,nextExpIndex);const expressionTeX=tex.substring(nextExpIndex,endpoint);const parsedExp=walkTex(expressionTeX,handler);return {endpoint:endpoint,expression:parsedExp}}function getNextFracIndex(tex,currentIndex){const dfrac="\\dfrac";const frac="\\frac";const nextFrac=tex.indexOf(frac,currentIndex);const nextDFrac=tex.indexOf(dfrac,currentIndex);if(nextFrac>-1&&nextDFrac>-1){return Math.min(nextFrac,nextDFrac)}if(nextFrac>-1){return nextFrac}if(nextDFrac>-1){return nextDFrac}return -1}function walkTex(tex,handler){if(!tex){return ""}let parsedString="";let currentIndex=0;let nextFrac=getNextFracIndex(tex,currentIndex);while(nextFrac>-1){parsedString+=tex.substring(currentIndex,nextFrac);currentIndex=nextFrac;const firstParsedExpression=parseNextExpression(tex,currentIndex,handler);currentIndex=firstParsedExpression.endpoint+1;const secondParsedExpression=parseNextExpression(tex,currentIndex,handler);currentIndex=secondParsedExpression.endpoint+1;if(parsedString.length){parsedString+=" ";}parsedString+=handler(firstParsedExpression.expression,secondParsedExpression.expression);nextFrac=getNextFracIndex(tex,currentIndex);}parsedString+=tex.slice(currentIndex);return parsedString}function parseTex(tex){const handler=function(exp1,exp2){return exp1+"/"+exp2};const texWithoutFracs=walkTex(tex,handler);return texWithoutFracs.replace("\\%","%")}
349
43
 
350
- // Replace unicode minus sign with hyphen
351
- text = text.replace(/\u2212/, "-");
352
- if (text === "") {
353
- possibilities = [{
354
- value: 1,
355
- exact: true
356
- }];
357
- } else if (text === "-") {
358
- possibilities = [{
359
- value: -1,
360
- exact: true
361
- }];
362
- }
363
- return possibilities;
364
- },
365
- // simple log(c) form
366
- log: function (text) {
367
- let match;
368
- let possibilities = [];
44
+ const answerFormButtons=[{title:"Integers",value:"integer",content:"6"},{title:"Decimals",value:"decimal",content:"0.75"},{title:"Proper fractions",value:"proper",content:"⅗"},{title:"Improper fractions",value:"improper",content:"⁷⁄₄"},{title:"Mixed numbers",value:"mixed",content:"1¾"},{title:"Numbers with π",value:"pi",content:"π"}];function maybeParsePercentInput(inputValue,normalizedAnswerExpected){if(!(typeof inputValue==="string"&&inputValue.endsWith("%"))){return inputValue}const value=parseFloat(inputValue.slice(0,-1));if(isNaN(value)){return inputValue}if(normalizedAnswerExpected){return value/100}return value}function scoreNumericInput(userInput,rubric){const defaultAnswerForms=answerFormButtons.map(e=>e["value"]).filter(e=>e!=="pi");const createValidator=answer=>{const stringAnswer=`${answer.value}`;const validatorForms=[...answer.answerForms??[]];if(!answer.strict||validatorForms.length===0){validatorForms.push(...defaultAnswerForms);}return KhanAnswerTypes.number.createValidatorFunctional(stringAnswer,{message:answer.message,simplify:answer.status==="correct"?answer.simplify:"optional",inexact:true,maxError:answer.maxError,forms:validatorForms})};const currentValue=parseTex(userInput.currentValue);const normalizedAnswerExpected=rubric.answers.filter(answer=>answer.status==="correct").every(answer=>answer.value!=null&&Math.abs(answer.value)<=1);let localValue=currentValue;if(rubric.coefficient){if(!localValue){localValue=1;}else if(localValue==="-"){localValue=-1;}}const matchedAnswer=rubric.answers.map(answer=>{const validateFn=createValidator(answer);const score=validateFn(maybeParsePercentInput(localValue,normalizedAnswerExpected));return {...answer,score}}).find(answer=>{return answer.score.correct||answer.status==="correct"&&answer.score.empty});const result=matchedAnswer?.status==="correct"?matchedAnswer.score:{empty:matchedAnswer?.status==="ungraded",correct:matchedAnswer?.status==="correct",message:matchedAnswer?.message??null};if(result.empty){return {type:"invalid",message:result.message}}return {type:"points",earned:result.correct?1:0,total:1,message:result.message}}
369
45
 
370
- // Replace unicode minus sign with hyphen
371
- text = text.replace(/\u2212/, "-");
372
- text = text.replace(/[ \(\)]/g, "");
373
- if (match = text.match(/^log\s*(\S+)\s*$/i)) {
374
- // @ts-expect-error - TS2322 - Type '{ value: number | undefined; exact: boolean; }[]' is not assignable to type 'never[]'.
375
- possibilities = forms.decimal(match[1]);
376
- } else if (text === "0") {
377
- // @ts-expect-error - TS2322 - Type 'number' is not assignable to type 'never'. | TS2322 - Type 'boolean' is not assignable to type 'never'.
378
- possibilities = [{
379
- value: 0,
380
- exact: true
381
- }];
382
- }
383
- return possibilities;
384
- },
385
- // Numbers with percent signs
386
- percent: function (text) {
387
- text = String(text).trim();
388
- // store whether or not there is a percent sign
389
- let hasPercentSign = false;
390
- if (text.indexOf("%") === text.length - 1) {
391
- text = text.substring(0, text.length - 1).trim();
392
- hasPercentSign = true;
393
- }
394
- const transformed = forms.decimal(text);
395
- transformed.forEach(t => {
396
- t.exact = hasPercentSign;
397
- // @ts-expect-error - TS2532 - Object is possibly 'undefined'.
398
- t.value = t.value / 100;
399
- });
400
- return transformed;
401
- },
402
- // Mixed numbers, like 1 3/4
403
- mixed: function (text) {
404
- const match = text
405
- // Replace unicode minus sign with hyphen
406
- .replace(/\u2212/, "-")
407
- // Remove space after +, -
408
- .replace(/([+-])\s+/g, "$1")
409
- // Extract integer, numerator and denominator
410
- .match(/^([+-]?)(\d+)\s+(\d+)\s*\/\s*(\d+)$/);
411
- if (match) {
412
- const sign = parseFloat(match[1] + "1");
413
- const integ = parseFloat(match[2]);
414
- const num = parseFloat(match[3]);
415
- const denom = parseFloat(match[4]);
416
- const simplified = num < denom && KhanMath.getGCD(num, denom) === 1;
417
- return [{
418
- value: sign * (integ + num / denom),
419
- exact: simplified
420
- }];
421
- }
422
- return [];
423
- },
424
- // Decimal numbers -- compare entered text rounded to
425
- // 'precision' Reciprical of the precision against the correct
426
- // answer. We round to 1/1e10 by default, which is healthily
427
- // less than machine epsilon but should be more than any real
428
- // decimal answer would use. (The 'integer' answer type uses
429
- // precision == 1.)
430
- decimal: function (text, precision = 1e10) {
431
- const normal = function normal(text) {
432
- text = String(text).trim();
433
- const match = text
434
- // Replace unicode minus sign with hyphen
435
- .replace(/\u2212/, "-")
436
- // Remove space after +, -
437
- .replace(/([+-])\s+/g, "$1")
438
- // Extract integer, numerator and denominator. If
439
- // commas or spaces are used, they must be in the
440
- // "correct" places
441
- .match(/^([+-]?(?:\d{1,3}(?:[, ]?\d{3})*\.?|\d{0,3}(?:[, ]?\d{3})*\.(?:\d{3}[, ]?)*\d{1,3}))$/);
46
+ function scoreOrderer(userInput,rubric){const correct=_.isEqual(userInput.current,rubric.correctOptions.map(option=>option.content));return {type:"points",earned:correct?1:0,total:1,message:null}}
442
47
 
443
- // You can't start a number with `0,`, to prevent us
444
- // interpeting '0.342' as correct for '342'
445
- const badLeadingZero = text.match(/^0[0,]*,/);
446
- if (match && !badLeadingZero) {
447
- let x = parseFloat(match[1].replace(/[, ]/g, ""));
448
- if (options.inexact === undefined) {
449
- x = Math.round(x * precision) / precision;
450
- }
451
- return x;
452
- }
453
- };
454
- const commas = function commas(text) {
455
- text = text.replace(/([\.,])/g, function (_, c) {
456
- return c === "." ? "," : ".";
457
- });
458
- return normal(text);
459
- };
460
- return [{
461
- value: normal(text),
462
- exact: true
463
- }, {
464
- value: commas(text),
465
- exact: true
466
- }];
467
- }
468
- };
48
+ function validateOrderer(userInput){if(userInput.current.length===0){return {type:"invalid",message:null}}return null}
469
49
 
470
- // validator function
471
- return function (guess) {
472
- // The fallback variable is used in place of the answer, if no
473
- // answer is provided (i.e. the field is left blank)
474
- const fallback = options.fallback != null ? "" + options.fallback : "";
475
- guess = String(guess).trim() || fallback;
476
- const score = {
477
- empty: guess === "",
478
- correct: false,
479
- message: null,
480
- guess: guess
481
- };
50
+ function scorePlotter(userInput,rubric){return {type:"points",earned:approximateDeepEqual(userInput,rubric.correct)?1:0,total:1,message:null}}
482
51
 
483
- // Iterate over all the acceptable forms
484
- // and exit if one of the answers is correct.
485
- //
486
- // HACK: This function is a bug fix from LEMS-2962;
487
- // after a transition from jQuery's `each` to JS's `forEach`
488
- // we realized this code was banking on the ability to:
489
- // 1. exit early from nested loops (can be tricky outside of functions)
490
- // 2. mutate external variables (score)
491
- // Could probably be refactored to be a pure function that
492
- // returns a score, but this code is poorly tested and prone to break.
493
- const findCorrectAnswer = () => {
494
- // WARNING: Don't use `forEach` without additional refactoring
495
- // because code needs to be able to exit early
496
- for (const form of acceptableForms) {
497
- const transformed = forms[form](guess);
498
- for (let j = 0, l = transformed.length; j < l; j++) {
499
- const val = transformed[j].value;
500
- const exact = transformed[j].exact;
501
- const piApprox = transformed[j].piApprox;
502
- // If a string was returned, and it exactly matches,
503
- // return true
504
- if (predicate(val, options.maxError)) {
505
- // If the exact correct number was returned,
506
- // return true
507
- if (exact || options.simplify === "optional") {
508
- score.correct = true;
509
- score.message = options.message || null;
510
- // If the answer is correct, don't say it's
511
- // empty. This happens, for example, with the
512
- // coefficient type where guess === "" but is
513
- // interpreted as "1" which is correct.
514
- score.empty = false;
515
- } else if (form === "percent") {
516
- // Otherwise, an error was returned
517
- score.empty = true;
518
- score.message = ErrorCodes.MISSING_PERCENT_ERROR;
519
- } else {
520
- if (options.simplify !== "enforced") {
521
- score.empty = true;
522
- }
523
- score.message = ErrorCodes.NEEDS_TO_BE_SIMPLIFIED_ERROR;
524
- }
525
- // HACK: The return false below stops the looping of the
526
- // callback since predicate check succeeded.
527
- // No more forms to look to verify the user guess.
528
- return false;
529
- }
530
- if (piApprox && predicate(val, Math.abs(val * 0.001))) {
531
- score.empty = true;
532
- score.message = ErrorCodes.APPROXIMATED_PI_ERROR;
533
- }
534
- }
535
- }
536
- };
52
+ function validatePlotter(userInput,validationData){if(approximateDeepEqual(userInput,validationData.starting)){return {type:"invalid",message:null}}return null}
537
53
 
538
- // mutates `score`
539
- findCorrectAnswer();
540
- if (score.correct === false) {
541
- let interpretedGuess = false;
542
- _.each(forms, function (form) {
543
- const anyAreNaN = _.any(form(guess), function (t) {
544
- return t.value != null && !_.isNaN(t.value);
545
- });
546
- if (anyAreNaN) {
547
- interpretedGuess = true;
548
- }
549
- });
550
- if (!interpretedGuess) {
551
- score.empty = true;
552
- score.message = ErrorCodes.EXTRA_SYMBOLS_ERROR;
553
- return score;
554
- }
555
- }
556
- return score;
557
- };
558
- }
559
- },
560
- /*
561
- * number answer type
562
- *
563
- * wraps the predicate answer type to performs simple number-based checking
564
- * of a solution
565
- */
566
- number: {
567
- convertToPredicate: function (correctAnswer, options) {
568
- const correctFloat = parseFloat(correctAnswer);
569
- return [function (guess, maxError) {
570
- return Math.abs(guess - correctFloat) < maxError;
571
- }, _extends({}, options, {
572
- type: "predicate"
573
- })];
574
- },
575
- createValidatorFunctional: function (correctAnswer, options) {
576
- return KhanAnswerTypes.predicate.createValidatorFunctional(...KhanAnswerTypes.number.convertToPredicate(correctAnswer, options));
577
- }
578
- },
579
- /*
580
- * The expression answer type parses a given expression or equation
581
- * and semantically compares it to the solution. In addition, instant
582
- * feedback is provided by rendering the last answer that fully parsed.
583
- *
584
- * Parsing options:
585
- * functions (e.g. data-functions="f g h")
586
- * A space or comma separated list of single-letter variables that
587
- * should be interpreted as functions. Case sensitive. "e" and "i"
588
- * are reserved.
589
- *
590
- * no functions specified: f(x+y) == fx + fy
591
- * with "f" as a function: f(x+y) != fx + fy
592
- *
593
- * Comparison options:
594
- * same-form (e.g. data-same-form)
595
- * If present, the answer must match the solution's structure in
596
- * addition to evaluating the same. Commutativity and excess negation
597
- * are ignored, but all other changes will trigger a rejection. Useful
598
- * for requiring a particular form of an equation, or if the answer
599
- * must be factored.
600
- *
601
- * example question: Factor x^2 + x - 2
602
- * example solution: (x-1)(x+2)
603
- * accepted answers: (x-1)(x+2), (x+2)(x-1), ---(-x-2)(-1+x), etc.
604
- * rejected answers: x^2+x-2, x*x+x-2, x(x+1)-2, (x-1)(x+2)^1, etc.
605
- * rejection message: Your answer is not in the correct form
606
- *
607
- * simplify (e.g. data-simplify)
608
- * If present, the answer must be fully expanded and simplified. Use
609
- * carefully - simplification is hard and there may be bugs, or you
610
- * might not agree on the definition of "simplified" used. You will
611
- * get an error if the provided solution is not itself fully expanded
612
- * and simplified.
613
- *
614
- * example question: Simplify ((n*x^5)^5) / (n^(-2)*x^2)^-3
615
- * example solution: x^31 / n
616
- * accepted answers: x^31 / n, x^31 / n^1, x^31 * n^(-1), etc.
617
- * rejected answers: (x^25 * n^5) / (x^(-6) * n^6), etc.
618
- * rejection message: Your answer is not fully expanded and simplified
619
- *
620
- * Rendering options:
621
- * times (e.g. data-times)
622
- * If present, explicit multiplication (such as between numbers) will
623
- * be rendered with a cross/x symbol (TeX: \times) instead of the usual
624
- * center dot (TeX: \cdot).
625
- *
626
- * normal rendering: 2 * 3^x -> 2 \cdot 3^{x}
627
- * but with "times": 2 * 3^x -> 2 \times 3^{x}
628
- */
629
- expression: {
630
- parseSolution: function (solutionString, options) {
631
- let solution = KAS.parse(solutionString, options);
632
- if (!solution.parsed) {
633
- throw new PerseusError("The provided solution (" + solutionString + ") didn't parse.", Errors.InvalidInput);
634
- } else if (options.simplified && !solution.expr.isSimplified()) {
635
- throw new PerseusError("The provided solution (" + solutionString + ") isn't fully expanded and simplified.", Errors.InvalidInput);
636
- } else {
637
- solution = solution.expr;
638
- }
639
- return solution;
640
- },
641
- createValidatorFunctional: function (solution, options) {
642
- return function (guess) {
643
- const score = {
644
- empty: false,
645
- correct: false,
646
- message: null,
647
- guess: guess,
648
- // Setting `ungraded` to true indicates that if the
649
- // guess doesn't match any of the solutions, the guess
650
- // shouldn't be marked as incorrect; instead, `message`
651
- // should be shown to the user. This is different from
652
- // setting `empty` to true, since the behavior of `empty`
653
- // is that `message` only will be shown if the guess is
654
- // graded as empty for every solution.
655
- ungraded: false
656
- };
54
+ function scoreRadio(userInput,rubric){const numSelected=userInput.choicesSelected.reduce((sum,selected)=>{return sum+(selected?1:0)},0);const numCorrect=rubric.choices.reduce((sum,currentChoice)=>{return currentChoice.correct?sum+1:sum},0);if(numCorrect>1&&numSelected!==numCorrect){return {type:"invalid",message:ErrorCodes.CHOOSE_CORRECT_NUM_ERROR}}const noneOfTheAboveSelected=rubric.choices.some((choice,index)=>choice.isNoneOfTheAbove&&userInput.choicesSelected[index]);if(noneOfTheAboveSelected&&numSelected>1){return {type:"invalid",message:ErrorCodes.NOT_NONE_ABOVE_ERROR}}const correct=userInput.choicesSelected.every((selected,i)=>{let isCorrect;if(rubric.choices[i].isNoneOfTheAbove){isCorrect=rubric.choices.every((choice,j)=>{return i===j||!choice.correct});}else {isCorrect=!!rubric.choices[i].correct;}return isCorrect===selected});return {type:"points",earned:correct?1:0,total:1,message:null}}
657
55
 
658
- // Don't bother parsing an empty input
659
- if (!guess) {
660
- // @ts-expect-error - TS2540 - Cannot assign to 'empty' because it is a read-only property.
661
- score.empty = true;
662
- return score;
663
- }
664
- const answer = KAS.parse(guess, options);
56
+ function validateRadio(userInput){const numSelected=userInput.choicesSelected.reduce((sum,selected)=>{return sum+(selected?1:0)},0);if(numSelected===0){return {type:"invalid",message:null}}return null}
665
57
 
666
- // An unsuccessful parse doesn't count as wrong
667
- if (!answer.parsed) {
668
- // @ts-expect-error - TS2540 - Cannot assign to 'empty' because it is a read-only property.
669
- score.empty = true;
670
- return score;
671
- }
58
+ function scoreSorter(userInput,rubric){const correct=approximateDeepEqual(userInput.options,rubric.correct);return {type:"points",earned:correct?1:0,total:1,message:null}}
672
59
 
673
- // Solution will need to be parsed again if we're creating
674
- // this from a multiple question type
675
- if (typeof solution === "string") {
676
- solution = KhanAnswerTypes.expression.parseSolution(solution, options);
677
- }
678
- const result = KAS.compare(answer.expr, solution, options);
679
- if (result.equal) {
680
- // Correct answer
681
- // @ts-expect-error - TS2540 - Cannot assign to 'correct' because it is a read-only property.
682
- score.correct = true;
683
- } else if (result.wrongVariableNames || result.wrongVariableCase) {
684
- // We don't want to give people an error for getting the
685
- // variable names or the variable case wrong.
686
- // TODO(aasmund): This should ideally have been handled
687
- // under the `result.message` condition, but the
688
- // KAS messages currently aren't translatable.
689
- // @ts-expect-error - TS2540 - Cannot assign to 'ungraded' because it is a read-only property.
690
- score.ungraded = true;
691
- // @ts-expect-error - TS2540 - Cannot assign to 'message' because it is a read-only property.
692
- score.message = result.wrongVariableCase ? ErrorCodes.WRONG_CASE_ERROR : ErrorCodes.WRONG_LETTER_ERROR;
693
- // Don't tell the use they're "almost there" in this case, that may not be true and isn't helpful.
694
- // @ts-expect-error - TS2339 - Property 'suppressAlmostThere' does not exist on type '{ readonly empty: false; readonly correct: false; readonly message: string | null | undefined; readonly guess: any; readonly ungraded: false; }'.
695
- score.suppressAlmostThere = true;
696
- } else if (result.message) {
697
- // Nearly correct answer
698
- // TODO(aasmund): This message also isn't translatable;
699
- // need to fix that in KAS
700
- // @ts-expect-error - TS2540 - Cannot assign to 'message' because it is a read-only property.
701
- score.message = result.message;
702
- } else {
703
- // Replace x with * and see if it would have been correct
704
- // TODO(aasmund): I think this branch is effectively dead,
705
- // because the replacement will only work in situations
706
- // where the variables are wrong (except if the variable
707
- // is x, in which case the replacement won't work either),
708
- // which is handled by another branch. When we implement a
709
- // more sophisticated variable check, revive this or
710
- // remove it completely if it will never come into play.
711
- const answerX = KAS.parse(guess.replace(/[xX]/g, "*"), options);
712
- if (answerX.parsed) {
713
- const resultX = KAS.compare(answerX.expr, solution, options);
714
- if (resultX.equal) {
715
- // @ts-expect-error - TS2540 - Cannot assign to 'ungraded' because it is a read-only property.
716
- score.ungraded = true;
717
- // @ts-expect-error - TS2540 - Cannot assign to 'message' because it is a read-only property.
718
- score.message = ErrorCodes.MULTIPLICATION_SIGN_ERROR;
719
- } else if (resultX.message) {
720
- // TODO(aasmund): I18nize `score.message`
721
- // @ts-expect-error - TS2540 - Cannot assign to 'message' because it is a read-only property.
722
- score.message = resultX.message + " Also, I'm a computer. I only understand " + "multiplication if you use an " + "asterisk (*) as the multiplication " + "sign.";
723
- }
724
- }
725
- }
726
- return score;
727
- };
728
- }
729
- }
730
- };
60
+ function validateSorter(userInput){if(!userInput.changed){return {type:"invalid",message:null}}return null}
731
61
 
732
- function scoreCategorizer(userInput, rubric) {
733
- let allCorrect = true;
734
- rubric.values.forEach((value, i) => {
735
- if (userInput.values[i] !== value) {
736
- allCorrect = false;
737
- }
738
- });
739
- return {
740
- type: "points",
741
- earned: allCorrect ? 1 : 0,
742
- total: 1,
743
- message: null
744
- };
745
- }
62
+ const filterNonEmpty=function(table){return table.filter(function(row){return row.some(cell=>cell)})};
746
63
 
747
- /**
748
- * Checks userInput from the categorizer widget to see if the user has selected
749
- * a category for each item.
750
- * @param userInput - The user's input corresponding to an array of indices that
751
- * represent the selected category for each row/item.
752
- * @param validationData - An array of strings corresponding to each row/item
753
- * @param strings - Used to provide a validation message
754
- */
755
- function validateCategorizer(userInput, validationData) {
756
- const incomplete = validationData.items.some((_, i) => userInput.values[i] == null);
757
- if (incomplete) {
758
- return {
759
- type: "invalid",
760
- message: ErrorCodes.INVALID_SELECTION_ERROR
761
- };
762
- }
763
- return null;
764
- }
64
+ function validateTable(userInput){const supplied=filterNonEmpty(userInput);const hasEmptyCell=supplied.some(function(row){return row.some(function(cell){return cell===""})});if(hasEmptyCell||!supplied.length){return {type:"invalid",message:null}}return null}
765
65
 
766
- function scoreCSProgram(userInput) {
767
- // The CS program can tell us whether it's correct or incorrect,
768
- // and pass an optional message
769
- if (userInput.status === "correct") {
770
- return {
771
- type: "points",
772
- earned: 1,
773
- total: 1,
774
- message: userInput.message || null
775
- };
776
- }
777
- if (userInput.status === "incorrect") {
778
- return {
779
- type: "points",
780
- earned: 0,
781
- total: 1,
782
- message: userInput.message || null
783
- };
784
- }
785
- return {
786
- type: "invalid",
787
- message: "Keep going, you're not there yet!"
788
- };
789
- }
66
+ function scoreTable(userInput,rubric){const validationResult=validateTable(userInput);if(validationResult!=null){return validationResult}const supplied=filterNonEmpty(userInput);const solution=filterNonEmpty(rubric.answers);if(supplied.length!==solution.length){return {type:"points",earned:0,total:1,message:null}}const createValidator=KhanAnswerTypes.number.createValidatorFunctional;let message=null;const allCorrect=solution.every(function(rowSolution){for(let i=0;i<supplied.length;i++){const rowSupplied=supplied[i];const correct=rowSupplied.every(function(cellSupplied,i){const cellSolution=rowSolution[i];const validator=createValidator(cellSolution,{simplify:true});const result=validator(cellSupplied);if(result.message){message=result.message;}return result.correct});if(correct){supplied.splice(i,1);return true}}return false});return {type:"points",earned:allCorrect?1:0,total:1,message}}
790
67
 
791
- function scoreDropdown(userInput, rubric) {
792
- const correct = rubric.choices[userInput.value - 1].correct;
793
- return {
794
- type: "points",
795
- earned: correct ? 1 : 0,
796
- total: 1,
797
- message: null
798
- };
799
- }
68
+ const inputNumberAnswerTypes={number:{name:"Numbers",forms:"integer, decimal, proper, improper, mixed"},decimal:{name:"Decimals",forms:"decimal"},integer:{name:"Integers",forms:"integer"},rational:{name:"Fractions and mixed numbers",forms:"integer, proper, improper, mixed"},improper:{name:"Improper numbers (no mixed)",forms:"integer, proper, improper"},mixed:{name:"Mixed numbers (no improper)",forms:"integer, proper, mixed"},percent:{name:"Numbers or percents",forms:"integer, decimal, proper, improper, mixed, percent"},pi:{name:"Numbers with pi",forms:"pi"}};function scoreInputNumber(userInput,rubric){if(rubric.answerType==null){rubric.answerType="number";}const stringValue=`${rubric.value}`;const val=KhanAnswerTypes.number.createValidatorFunctional(stringValue,{simplify:rubric.simplify,inexact:rubric.inexact||undefined,maxError:rubric.maxError,forms:inputNumberAnswerTypes[rubric.answerType].forms});const currentValue=parseTex(userInput.currentValue);const result=val(currentValue);if(result.empty){return {type:"invalid",message:result.message}}return {type:"points",earned:result.correct?1:0,total:1,message:result.message}}
800
69
 
801
- /**
802
- * Checks if the user has selected an item from the dropdown before scoring.
803
- * This is shown with a userInput value / index other than 0.
804
- */
805
- function validateDropdown(userInput) {
806
- if (userInput.value === 0) {
807
- return {
808
- type: "invalid",
809
- message: null
810
- };
811
- }
812
- return null;
813
- }
70
+ function scoreNoop(points=0){return {type:"points",earned:points,total:points,message:null}}
814
71
 
815
- /* Content creators input a list of answers which are matched from top to
816
- * bottom. The intent is that they can include spcific solutions which should
817
- * be graded as correct or incorrect (or ungraded!) first, then get more
818
- * general.
819
- *
820
- * We iterate through each answer, trying to match it with the user's input
821
- * using the following angorithm:
822
- * - Try to parse the user's input. If it doesn't parse then return "not
823
- * graded".
824
- * - For each answer:
825
- * ~ Try to validate the user's input against the answer. The answer is
826
- * expected to parse.
827
- * ~ If the user's input validates (the validator judges it "correct"), we've
828
- * matched and can stop considering answers.
829
- * - If there were no matches or the matching answer is considered "ungraded",
830
- * show the user an error. TODO(joel) - what error?
831
- * - Otherwise, pass through the resulting points and message.
832
- */
833
- function scoreExpression(userInput, rubric, locale) {
834
- const options = _.clone(rubric);
835
- _.extend(options, {
836
- decimal_separator: getDecimalSeparator(locale)
837
- });
838
- const createValidator = answer => {
839
- // We give options to KAS.parse here because it is parsing the
840
- // solution answer, not the student answer, and we don't want a
841
- // solution to work if the student is using a different language
842
- // (different from the content creation language, ie. English).
843
- const expression = KAS.parse(answer.value, rubric);
844
- // An answer may not be parsed if the expression was defined
845
- // incorrectly. For example if the answer is using a symbol defined
846
- // in the function variables list for the expression.
847
- if (!expression.parsed) {
848
- /* c8 ignore next */
849
- throw new PerseusError("Unable to parse solution answer for expression", Errors.InvalidInput, {
850
- metadata: {
851
- rubric: JSON.stringify(rubric)
852
- }
853
- });
854
- }
855
- return KhanAnswerTypes.expression.createValidatorFunctional(expression.expr, _({}).extend(options, {
856
- simplify: answer.simplify,
857
- form: answer.form
858
- }));
859
- };
72
+ function scoreGroup(userInput,rubric,locale){const scores=scoreWidgetsFunctional(rubric.widgets,Object.keys(rubric.widgets),userInput,locale);return flattenScores(scores)}
860
73
 
861
- // Find the first answer form that matches the user's input and that
862
- // is considered correct. Also, track whether the input is
863
- // considered "empty" for all answer forms, and keep the validation
864
- // result for the first answer form for which the user's input was
865
- // considered "ungraded".
866
- // (Terminology reminder: the answer forms are provided by the
867
- // assessment items; they are not the user's input. Each one might
868
- // represent a correct answer, an incorrect one (if the exercise
869
- // creator has predicted certain common wrong answers and wants to
870
- // provide guidance via a message), or an ungraded one (same idea,
871
- // but without giving the user an incorrect mark for the question).
872
- let matchingAnswerForm;
873
- let matchMessage;
874
- let allEmpty = true;
875
- let firstUngradedResult;
876
- // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
877
- for (const answerForm of rubric.answerForms || []) {
878
- const validator = createValidator(answerForm);
879
- // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
880
- if (!validator) {
881
- continue;
882
- }
883
- const result = validator(userInput);
74
+ function emptyWidgetsFunctional(widgets,widgetIds,userInputMap,locale){return widgetIds.filter(id=>{const widget=widgets[id];if(!widget||widget.static===true){return false}const validator=getWidgetValidator(widget.type);const userInput=userInputMap[id];const validationData=widget.options;const score=validator?.(userInput,validationData,locale);if(score){return scoreIsEmpty(score)}})}
884
75
 
885
- // Short-circuit as soon as the user's input matches some answer
886
- // (independently of whether the answer is correct)
887
- if (result.correct) {
888
- matchingAnswerForm = answerForm;
889
- matchMessage = result.message || "";
890
- break;
891
- }
892
- allEmpty = allEmpty && result.empty;
893
- // If this answer form is correct and the user's input is considered
894
- // "ungraded" for it, we'll want to keep the evaluation result for
895
- // later. If the user's input doesn't match any answer forms, we'll
896
- // show the message from this validation.
897
- if (answerForm.considered === "correct" && result.ungraded && !firstUngradedResult) {
898
- firstUngradedResult = result;
899
- }
900
- }
76
+ function validateGroup(userInput,validationData,locale){const emptyWidgets=emptyWidgetsFunctional(validationData.widgets,Object.keys(validationData.widgets),userInput,locale);if(emptyWidgets.length===0){return null}return {type:"invalid",message:null}}
901
77
 
902
- // Now check to see if we matched any answer form at all, and if
903
- // we did, whether it's considered correct, incorrect, or ungraded
904
- if (!matchingAnswerForm) {
905
- if (firstUngradedResult) {
906
- // While we didn't directly match with any answer form, we
907
- // did at some point get an "ungraded" validation result,
908
- // which might indicate e.g. a mismatch in variable casing.
909
- // We'll return "invalid", which will let the user try again
910
- // with no penalty, and the hopefully helpful validation
911
- // message.
912
- return {
913
- type: "invalid",
914
- message: firstUngradedResult.message,
915
- suppressAlmostThere: firstUngradedResult.suppressAlmostThere
916
- };
917
- }
918
- if (allEmpty) {
919
- // If everything graded as empty, it's invalid.
920
- return {
921
- type: "invalid",
922
- message: null
923
- };
924
- }
925
- // We fell through all the possibilities and we're not empty,
926
- // so the answer is considered incorrect.
927
- return {
928
- type: "points",
929
- earned: 0,
930
- total: 1
931
- };
932
- }
933
- if (matchingAnswerForm.considered === "ungraded") {
934
- return {
935
- type: "invalid",
936
- message: matchMessage
937
- };
938
- }
939
- // We matched a graded answer form, so we can now tell the user
940
- // whether their input was correct or incorrect, and hand out
941
- // points accordingly
942
- return {
943
- type: "points",
944
- earned: matchingAnswerForm.considered === "correct" ? 1 : 0,
945
- total: 1,
946
- message: matchMessage
947
- };
948
- }
78
+ function validateLabelImage(userInput){let numAnswered=0;for(let i=0;i<userInput.markers.length;i++){const userSelection=userInput.markers[i].selected;if(userSelection&&userSelection.length>0){numAnswered++;}}if(numAnswered!==userInput.markers.length){return {type:"invalid",message:null}}return null}
949
79
 
950
- /**
951
- * Checks user input from the expression widget to see if it is scorable.
952
- *
953
- * Note: Most of the expression widget's validation requires the Rubric because
954
- * of its use of KhanAnswerTypes as a core part of scoring.
955
- *
956
- * @see `scoreExpression()` for more details.
957
- */
958
- function validateExpression(userInput) {
959
- if (userInput === "") {
960
- return {
961
- type: "invalid",
962
- message: null
963
- };
964
- }
965
- return null;
966
- }
80
+ function validateMockWidget(userInput){if(userInput.currentValue==null||userInput.currentValue===""){return {type:"invalid",message:""}}return null}
967
81
 
968
- function getCoefficientsByType(data) {
969
- if (data.coords == null) {
970
- return undefined;
971
- }
972
- if (data.type === "exponential" || data.type === "logarithm") {
973
- const grader = GrapherUtil.functionForType(data.type);
974
- return grader.getCoefficients(data.coords, data.asymptote);
975
- } else if (data.type === "linear" || data.type === "quadratic" || data.type === "absolute_value" || data.type === "sinusoid" || data.type === "tangent") {
976
- const grader = GrapherUtil.functionForType(data.type);
977
- return grader.getCoefficients(data.coords);
978
- } else {
979
- throw new PerseusError("Invalid grapher type", Errors.InvalidInput);
980
- }
981
- }
982
- function scoreGrapher(userInput, rubric) {
983
- if (userInput.type !== rubric.correct.type) {
984
- return {
985
- type: "points",
986
- earned: 0,
987
- total: 1,
988
- message: null
989
- };
990
- }
82
+ function scoreMockWidget(userInput,rubric){const validationResult=validateMockWidget(userInput);if(validationResult!=null){return validationResult}return {type:"points",earned:userInput.currentValue===rubric.value?1:0,total:1,message:""}}
991
83
 
992
- // We haven't moved the coords
993
- if (userInput.coords == null) {
994
- return {
995
- type: "invalid",
996
- message: null
997
- };
998
- }
84
+ const widgets={};function registerWidget(type,scorer,validator){widgets[type]={scorer,validator};}const getWidgetValidator=name=>{return widgets[name]?.validator??null};const getWidgetScorer=name=>{return widgets[name]?.scorer??null};registerWidget("categorizer",scoreCategorizer,validateCategorizer);registerWidget("cs-program",scoreCSProgram);registerWidget("dropdown",scoreDropdown,validateDropdown);registerWidget("expression",scoreExpression,validateExpression);registerWidget("grapher",scoreGrapher);registerWidget("group",scoreGroup,validateGroup);registerWidget("iframe",scoreIframe);registerWidget("input-number",scoreInputNumber);registerWidget("interactive-graph",scoreInteractiveGraph);registerWidget("label-image",scoreLabelImage,validateLabelImage);registerWidget("matcher",scoreMatcher);registerWidget("matrix",scoreMatrix,validateMatrix);registerWidget("mock-widget",scoreMockWidget,scoreMockWidget);registerWidget("number-line",scoreNumberLine,validateNumberLine);registerWidget("numeric-input",scoreNumericInput);registerWidget("orderer",scoreOrderer,validateOrderer);registerWidget("plotter",scorePlotter,validatePlotter);registerWidget("radio",scoreRadio,validateRadio);registerWidget("sorter",scoreSorter,validateSorter);registerWidget("table",scoreTable,validateTable);registerWidget("deprecated-standin",()=>scoreNoop(1));registerWidget("measurer",()=>scoreNoop(1));registerWidget("definition",scoreNoop);registerWidget("explanation",scoreNoop);registerWidget("image",scoreNoop);registerWidget("interaction",scoreNoop);registerWidget("molecule",scoreNoop);registerWidget("passage",scoreNoop);registerWidget("passage-ref",scoreNoop);registerWidget("passage-ref-target",scoreNoop);registerWidget("video",scoreNoop);
999
85
 
1000
- // Get new function handler for grading
1001
- const grader = GrapherUtil.functionForType(userInput.type);
1002
- const guessCoeffs = getCoefficientsByType(userInput);
1003
- const correctCoeffs = getCoefficientsByType(rubric.correct);
1004
- if (guessCoeffs == null || correctCoeffs == null) {
1005
- return {
1006
- type: "invalid",
1007
- message: null
1008
- };
1009
- }
1010
- if (grader.areEqual(guessCoeffs, correctCoeffs)) {
1011
- return {
1012
- type: "points",
1013
- earned: 1,
1014
- total: 1,
1015
- message: null
1016
- };
1017
- }
1018
- return {
1019
- type: "points",
1020
- earned: 0,
1021
- total: 1,
1022
- message: null
1023
- };
1024
- }
1025
-
1026
- // TODO: merge this with scoreCSProgram, it's the same code
1027
- function scoreIframe(userInput) {
1028
- // The iframe can tell us whether it's correct or incorrect,
1029
- // and pass an optional message
1030
- if (userInput.status === "correct") {
1031
- return {
1032
- type: "points",
1033
- earned: 1,
1034
- total: 1,
1035
- message: userInput.message || null
1036
- };
1037
- }
1038
- if (userInput.status === "incorrect") {
1039
- return {
1040
- type: "points",
1041
- earned: 0,
1042
- total: 1,
1043
- message: userInput.message || null
1044
- };
1045
- }
1046
- return {
1047
- type: "invalid",
1048
- message: "Keep going, you're not there yet!"
1049
- };
1050
- }
1051
-
1052
- const {
1053
- collinear,
1054
- canonicalSineCoefficients,
1055
- similar,
1056
- clockwise
1057
- } = geometry;
1058
- const {
1059
- getClockwiseAngle
1060
- } = angles;
1061
- const {
1062
- getSinusoidCoefficients,
1063
- getQuadraticCoefficients
1064
- } = coefficients;
1065
- function scoreInteractiveGraph(userInput, rubric) {
1066
- // None-type graphs are not graded
1067
- if (userInput.type === "none" && rubric.correct.type === "none") {
1068
- return {
1069
- type: "points",
1070
- earned: 0,
1071
- total: 0,
1072
- message: null
1073
- };
1074
- }
1075
-
1076
- // When nothing has moved, there will neither be coords nor the
1077
- // circle's center/radius fields. When those fields are absent, skip
1078
- // all these checks; just go mark the answer as empty.
1079
- const hasValue = Boolean(
1080
- // @ts-expect-error - TS2339 - Property 'coords' does not exist on type 'PerseusGraphType'.
1081
- userInput.coords ||
1082
- // @ts-expect-error - TS2339 - Property 'center' does not exist on type 'PerseusGraphType'. | TS2339 - Property 'radius' does not exist on type 'PerseusGraphType'.
1083
- userInput.center && userInput.radius);
1084
- if (userInput.type === rubric.correct.type && hasValue) {
1085
- if (userInput.type === "linear" && rubric.correct.type === "linear" && userInput.coords != null) {
1086
- const guess = userInput.coords;
1087
- const correct = rubric.correct.coords;
1088
-
1089
- // If both of the guess points are on the correct line, it's
1090
- // correct.
1091
- if (collinear(correct[0], correct[1], guess[0]) && collinear(correct[0], correct[1], guess[1])) {
1092
- return {
1093
- type: "points",
1094
- earned: 1,
1095
- total: 1,
1096
- message: null
1097
- };
1098
- }
1099
- } else if (userInput.type === "linear-system" && rubric.correct.type === "linear-system" && userInput.coords != null) {
1100
- const guess = userInput.coords;
1101
- const correct = rubric.correct.coords;
1102
- if (collinear(correct[0][0], correct[0][1], guess[0][0]) && collinear(correct[0][0], correct[0][1], guess[0][1]) && collinear(correct[1][0], correct[1][1], guess[1][0]) && collinear(correct[1][0], correct[1][1], guess[1][1]) || collinear(correct[0][0], correct[0][1], guess[1][0]) && collinear(correct[0][0], correct[0][1], guess[1][1]) && collinear(correct[1][0], correct[1][1], guess[0][0]) && collinear(correct[1][0], correct[1][1], guess[0][1])) {
1103
- return {
1104
- type: "points",
1105
- earned: 1,
1106
- total: 1,
1107
- message: null
1108
- };
1109
- }
1110
- } else if (userInput.type === "quadratic" && rubric.correct.type === "quadratic" && userInput.coords != null) {
1111
- // If the parabola coefficients match, it's correct.
1112
- const guessCoeffs = getQuadraticCoefficients(userInput.coords);
1113
- const correctCoeffs = getQuadraticCoefficients(rubric.correct.coords);
1114
- if (approximateDeepEqual(guessCoeffs, correctCoeffs)) {
1115
- return {
1116
- type: "points",
1117
- earned: 1,
1118
- total: 1,
1119
- message: null
1120
- };
1121
- }
1122
- } else if (userInput.type === "sinusoid" && rubric.correct.type === "sinusoid" && userInput.coords != null) {
1123
- const guessCoeffs = getSinusoidCoefficients(userInput.coords);
1124
- const correctCoeffs = getSinusoidCoefficients(rubric.correct.coords);
1125
- const canonicalGuessCoeffs = canonicalSineCoefficients(guessCoeffs);
1126
- const canonicalCorrectCoeffs = canonicalSineCoefficients(correctCoeffs);
1127
- // If the canonical coefficients match, it's correct.
1128
- if (approximateDeepEqual(canonicalGuessCoeffs, canonicalCorrectCoeffs)) {
1129
- return {
1130
- type: "points",
1131
- earned: 1,
1132
- total: 1,
1133
- message: null
1134
- };
1135
- }
1136
- } else if (userInput.type === "circle" && rubric.correct.type === "circle") {
1137
- if (approximateDeepEqual(userInput.center, rubric.correct.center) && approximateEqual(userInput.radius, rubric.correct.radius)) {
1138
- return {
1139
- type: "points",
1140
- earned: 1,
1141
- total: 1,
1142
- message: null
1143
- };
1144
- }
1145
- } else if (userInput.type === "point" && rubric.correct.type === "point" && userInput.coords != null) {
1146
- let correct = rubric.correct.coords;
1147
- if (correct == null) {
1148
- throw new Error("Point graph rubric has null coords");
1149
- }
1150
- const guess = userInput.coords.slice();
1151
- correct = correct.slice();
1152
- // Everything's already rounded so we shouldn't need to do an
1153
- // eq() comparison but _.isEqual(0, -0) is false, so we'll use
1154
- // eq() anyway. The sort should be fine because it'll stringify
1155
- // it and -0 converted to a string is "0"
1156
- guess == null || guess.sort();
1157
- // @ts-expect-error - TS2339 - Property 'sort' does not exist on type 'readonly Coord[]'.
1158
- correct.sort();
1159
- if (approximateDeepEqual(guess, correct)) {
1160
- return {
1161
- type: "points",
1162
- earned: 1,
1163
- total: 1,
1164
- message: null
1165
- };
1166
- }
1167
- } else if (userInput.type === "polygon" && rubric.correct.type === "polygon" && userInput.coords != null) {
1168
- const guess = userInput.coords.slice();
1169
- const correct = rubric.correct.coords.slice();
1170
- let match;
1171
- if (rubric.correct.match === "similar") {
1172
- match = similar(guess, correct, Number.POSITIVE_INFINITY);
1173
- } else if (rubric.correct.match === "congruent") {
1174
- match = similar(guess, correct, number.DEFAULT_TOLERANCE);
1175
- } else if (rubric.correct.match === "approx") {
1176
- match = similar(guess, correct, 0.1);
1177
- } else {
1178
- /* exact */
1179
- guess.sort();
1180
- correct.sort();
1181
- match = approximateDeepEqual(guess, correct);
1182
- }
1183
- if (match) {
1184
- return {
1185
- type: "points",
1186
- earned: 1,
1187
- total: 1,
1188
- message: null
1189
- };
1190
- }
1191
- } else if (userInput.type === "segment" && rubric.correct.type === "segment" && userInput.coords != null) {
1192
- let guess = deepClone(userInput.coords);
1193
- let correct = deepClone(rubric.correct.coords);
1194
- guess = _.invoke(guess, "sort").sort();
1195
- correct = _.invoke(correct, "sort").sort();
1196
- if (approximateDeepEqual(guess, correct)) {
1197
- return {
1198
- type: "points",
1199
- earned: 1,
1200
- total: 1,
1201
- message: null
1202
- };
1203
- }
1204
- } else if (userInput.type === "ray" && rubric.correct.type === "ray" && userInput.coords != null) {
1205
- const guess = userInput.coords;
1206
- const correct = rubric.correct.coords;
1207
- if (approximateDeepEqual(guess[0], correct[0]) && collinear(correct[0], correct[1], guess[1])) {
1208
- return {
1209
- type: "points",
1210
- earned: 1,
1211
- total: 1,
1212
- message: null
1213
- };
1214
- }
1215
- } else if (userInput.type === "angle" && rubric.correct.type === "angle") {
1216
- const coords = userInput.coords;
1217
- const correct = rubric.correct.coords;
1218
- const allowReflexAngles = rubric.correct.allowReflexAngles;
1219
-
1220
- // While the angle graph should always have 3 points, our types
1221
- // technically allow for null values. We'll check for that here.
1222
- // TODO: (LEMS-2857) We would like to update the type of coords
1223
- // to be non-nullable, as the graph should always have 3 points.
1224
- if (!coords) {
1225
- return {
1226
- type: "invalid",
1227
- message: null
1228
- };
1229
- }
1230
-
1231
- // We need to check both the direction of the angle and the
1232
- // whether the graph allows for reflexive angles in order to
1233
- // to determine if we need to reverse the coords for scoring.
1234
- const areClockwise = clockwise([coords[0], coords[2], coords[1]]);
1235
- const shouldReverseCoords = areClockwise && !allowReflexAngles;
1236
- const guess = shouldReverseCoords ? coords.slice().reverse() : coords;
1237
- let match;
1238
- if (rubric.correct.match === "congruent") {
1239
- const angles = _.map([guess, correct], function (coords) {
1240
- // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
1241
- if (!coords) {
1242
- return false;
1243
- }
1244
- const angle = getClockwiseAngle(coords, allowReflexAngles);
1245
- return angle;
1246
- });
1247
- // @ts-expect-error - TS2556 - A spread argument must either have a tuple type or be passed to a rest parameter.
1248
- match = approximateEqual(...angles);
1249
- } else {
1250
- /* exact */
1251
- match = approximateDeepEqual(guess[1], correct[1]) && collinear(correct[1], correct[0], guess[0]) && collinear(correct[1], correct[2], guess[2]);
1252
- }
1253
- if (match) {
1254
- return {
1255
- type: "points",
1256
- earned: 1,
1257
- total: 1,
1258
- message: null
1259
- };
1260
- }
1261
- }
1262
- }
1263
-
1264
- // The input wasn't correct, so check if it's a blank input or if it's
1265
- // actually just wrong
1266
- if (!hasValue || _.isEqual(userInput, rubric.graph)) {
1267
- // We're where we started.
1268
- return {
1269
- type: "invalid",
1270
- message: null
1271
- };
1272
- }
1273
- return {
1274
- type: "points",
1275
- earned: 0,
1276
- total: 1,
1277
- message: null
1278
- };
1279
- }
1280
-
1281
- // Question state for marker as result of user selected answers.
1282
-
1283
- function scoreLabelImageMarker(userInput, rubric) {
1284
- const score = {
1285
- hasAnswers: false,
1286
- isCorrect: false
1287
- };
1288
- if (userInput && userInput.length > 0) {
1289
- score.hasAnswers = true;
1290
- }
1291
- if (rubric.length > 0) {
1292
- if (userInput && userInput.length === rubric.length) {
1293
- // All correct answers are selected by the user.
1294
- score.isCorrect = userInput.every(choice => rubric.includes(choice));
1295
- }
1296
- } else if (!userInput || userInput.length === 0) {
1297
- // Correct as no answers should be selected by the user.
1298
- score.isCorrect = true;
1299
- }
1300
- return score;
1301
- }
1302
- function scoreLabelImage(userInput, rubric) {
1303
- let numCorrect = 0;
1304
- for (let i = 0; i < userInput.markers.length; i++) {
1305
- const score = scoreLabelImageMarker(userInput.markers[i].selected, rubric.markers[i].answers);
1306
- if (score.isCorrect) {
1307
- numCorrect++;
1308
- }
1309
- }
1310
- return {
1311
- type: "points",
1312
- // Markers with no expected answers are graded as correct if user
1313
- // makes no answer selection.
1314
- earned: numCorrect === userInput.markers.length ? 1 : 0,
1315
- total: 1,
1316
- message: null
1317
- };
1318
- }
1319
-
1320
- function scoreMatcher(userInput, rubric) {
1321
- const correct = _.isEqual(userInput.left, rubric.left) && _.isEqual(userInput.right, rubric.right);
1322
- return {
1323
- type: "points",
1324
- earned: correct ? 1 : 0,
1325
- total: 1,
1326
- message: null
1327
- };
1328
- }
1329
-
1330
- function scoreMatrix(userInput, rubric) {
1331
- const solution = rubric.answers;
1332
- const supplied = userInput.answers;
1333
- const solutionSize = getMatrixSize(solution);
1334
- const suppliedSize = getMatrixSize(supplied);
1335
- const incorrectSize = solutionSize[0] !== suppliedSize[0] || solutionSize[1] !== suppliedSize[1];
1336
- const createValidator = KhanAnswerTypes.number.createValidatorFunctional;
1337
- let message = null;
1338
- let incorrect = false;
1339
- _(suppliedSize[0]).times(row => {
1340
- _(suppliedSize[1]).times(col => {
1341
- if (!incorrectSize) {
1342
- const validator = createValidator(
1343
- // @ts-expect-error - TS2345 - Argument of type 'number' is not assignable to parameter of type 'string'.
1344
- solution[row][col], {
1345
- simplify: true
1346
- });
1347
- const result = validator(supplied[row][col]);
1348
- if (result.message) {
1349
- // @ts-expect-error - TS2322 - Type 'string' is not assignable to type 'null'.
1350
- message = result.message;
1351
- }
1352
- if (!result.correct) {
1353
- incorrect = true;
1354
- }
1355
- }
1356
- });
1357
- });
1358
- if (incorrectSize) {
1359
- return {
1360
- type: "points",
1361
- earned: 0,
1362
- total: 1,
1363
- message: null
1364
- };
1365
- }
1366
- return {
1367
- type: "points",
1368
- earned: incorrect ? 0 : 1,
1369
- total: 1,
1370
- message: message
1371
- };
1372
- }
1373
-
1374
- /**
1375
- * Checks user input from the matrix widget to see if it is scorable.
1376
- *
1377
- * Note: The matrix widget cannot do much validation without the Scoring
1378
- * Data because of its use of KhanAnswerTypes as a core part of scoring.
1379
- *
1380
- * @see `scoreMatrix()` for more details.
1381
- */
1382
- function validateMatrix(userInput) {
1383
- const supplied = userInput.answers;
1384
- const suppliedSize = getMatrixSize(supplied);
1385
- for (let row = 0; row < suppliedSize[0]; row++) {
1386
- for (let col = 0; col < suppliedSize[1]; col++) {
1387
- if (supplied[row][col] == null || supplied[row][col].toString().length === 0) {
1388
- return {
1389
- type: "invalid",
1390
- message: ErrorCodes.FILL_ALL_CELLS_ERROR
1391
- };
1392
- }
1393
- }
1394
- }
1395
- return null;
1396
- }
1397
-
1398
- function scoreNumberLine(userInput, rubric) {
1399
- const range = rubric.range;
1400
- const start = rubric.initialX != null ? rubric.initialX : range[0];
1401
- const startRel = rubric.isInequality ? "ge" : "eq";
1402
- const correctRel = rubric.correctRel || "eq";
1403
- const correctPos = number.equal(userInput.numLinePosition,
1404
- // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
1405
- rubric.correctX || 0);
1406
- if (correctPos && correctRel === userInput.rel) {
1407
- return {
1408
- type: "points",
1409
- earned: 1,
1410
- total: 1,
1411
- message: null
1412
- };
1413
- }
1414
- if (userInput.numLinePosition === start && userInput.rel === startRel) {
1415
- // We're where we started.
1416
- return {
1417
- type: "invalid",
1418
- message: null
1419
- };
1420
- }
1421
- return {
1422
- type: "points",
1423
- earned: 0,
1424
- total: 1,
1425
- message: null
1426
- };
1427
- }
1428
-
1429
- /**
1430
- * Checks user input is within the allowed range and not the same as the initial
1431
- * state.
1432
- * @param userInput
1433
- * @see 'scoreNumberLine' for the scoring logic.
1434
- */
1435
- function validateNumberLine(userInput) {
1436
- const divisionRange = userInput.divisionRange;
1437
- const outsideAllowedRange = userInput.numDivisions > divisionRange[1] || userInput.numDivisions < divisionRange[0];
1438
-
1439
- // TODO: I don't think isTickCrtl is a thing anymore
1440
- if (userInput.isTickCrtl && outsideAllowedRange) {
1441
- return {
1442
- type: "invalid",
1443
- message: "Number of divisions is outside the allowed range."
1444
- };
1445
- }
1446
- return null;
1447
- }
1448
-
1449
- /*
1450
- * In this file, an `expression` is some portion of valid TeX enclosed in
1451
- * curly brackets.
1452
- */
1453
-
1454
- /*
1455
- * Find the index at which an expression ends, i.e., has an unmatched
1456
- * closing curly bracket. This method assumes that we start with a non-open
1457
- * bracket character and end when we've seen more left than right brackets
1458
- * (rather than assuming that we start with a bracket character and wait for
1459
- * bracket equality).
1460
- */
1461
- function findEndpoint(tex, currentIndex) {
1462
- let bracketDepth = 0;
1463
- for (let i = currentIndex, len = tex.length; i < len; i++) {
1464
- const c = tex[i];
1465
- if (c === "{") {
1466
- bracketDepth++;
1467
- } else if (c === "}") {
1468
- bracketDepth--;
1469
- }
1470
- if (bracketDepth < 0) {
1471
- return i;
1472
- }
1473
- }
1474
- // If we never see unbalanced curly brackets, default to the
1475
- // entire string
1476
- return tex.length;
1477
- }
1478
-
1479
- /*
1480
- * Parses an individual set of curly brackets into TeX.
1481
- */
1482
- function parseNextExpression(tex, currentIndex, handler) {
1483
- // Find the first '{' and grab subsequent TeX
1484
- // Ex) tex: '{3}{7}', and we want the '3'
1485
- const openBracketIndex = tex.indexOf("{", currentIndex);
1486
-
1487
- // If there is no open bracket, set the endpoint to the end of the string
1488
- // and the expression to an empty string. This helps ensure we don't
1489
- // get stuck in an infinite loop when users handtype TeX.
1490
- if (openBracketIndex === -1) {
1491
- return {
1492
- endpoint: tex.length,
1493
- expression: ""
1494
- };
1495
- }
1496
- const nextExpIndex = openBracketIndex + 1;
1497
-
1498
- // Truncate to only contain remaining TeX
1499
- const endpoint = findEndpoint(tex, nextExpIndex);
1500
- const expressionTeX = tex.substring(nextExpIndex, endpoint);
1501
- const parsedExp = walkTex(expressionTeX, handler);
1502
- return {
1503
- endpoint: endpoint,
1504
- expression: parsedExp
1505
- };
1506
- }
1507
- function getNextFracIndex(tex, currentIndex) {
1508
- const dfrac = "\\dfrac";
1509
- const frac = "\\frac";
1510
- const nextFrac = tex.indexOf(frac, currentIndex);
1511
- const nextDFrac = tex.indexOf(dfrac, currentIndex);
1512
- if (nextFrac > -1 && nextDFrac > -1) {
1513
- return Math.min(nextFrac, nextDFrac);
1514
- }
1515
- if (nextFrac > -1) {
1516
- return nextFrac;
1517
- }
1518
- if (nextDFrac > -1) {
1519
- return nextDFrac;
1520
- }
1521
- return -1;
1522
- }
1523
- function walkTex(tex, handler) {
1524
- if (!tex) {
1525
- return "";
1526
- }
1527
-
1528
- // Ex) tex: '2 \dfrac {3}{7}'
1529
- let parsedString = "";
1530
- let currentIndex = 0;
1531
- let nextFrac = getNextFracIndex(tex, currentIndex);
1532
-
1533
- // For each \dfrac, find the two expressions (wrapped in {}) and recur
1534
- while (nextFrac > -1) {
1535
- // Gather first fragment, preceding \dfrac
1536
- // Ex) parsedString: '2 '
1537
- parsedString += tex.substring(currentIndex, nextFrac);
1538
-
1539
- // Remove everything preceding \dfrac, which has been parsed
1540
- currentIndex = nextFrac;
1541
-
1542
- // Parse first expression and move index past it
1543
- // Ex) firstParsedExpression.expression: '3'
1544
- const firstParsedExpression = parseNextExpression(tex, currentIndex, handler);
1545
- currentIndex = firstParsedExpression.endpoint + 1;
1546
-
1547
- // Parse second expression
1548
- // Ex) secondParsedExpression.expression: '7'
1549
- const secondParsedExpression = parseNextExpression(tex, currentIndex, handler);
1550
- currentIndex = secondParsedExpression.endpoint + 1;
1551
-
1552
- // Add expressions to running total of parsed expressions
1553
- // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
1554
- if (parsedString.length) {
1555
- parsedString += " ";
1556
- }
1557
-
1558
- // Apply a custom handler based on the parsed subexpressions
1559
- parsedString += handler(firstParsedExpression.expression, secondParsedExpression.expression);
1560
-
1561
- // Find next DFrac, relative to currentIndex
1562
- nextFrac = getNextFracIndex(tex, currentIndex);
1563
- }
1564
-
1565
- // Add remaining TeX, which is \dfrac-free
1566
- parsedString += tex.slice(currentIndex);
1567
- return parsedString;
1568
- }
1569
-
1570
- /*
1571
- * Parse a TeX expression into something interpretable by input-number.
1572
- * The process is concerned with: (1) parsing fractions, i.e., \dfracs; and
1573
- * (2) removing backslash-escaping from certain characters (right now, only
1574
- * percent signs).
1575
- *
1576
- * The basic algorithm for handling \dfracs splits on \dfracs and then recurs
1577
- * on the subsequent "expressions", i.e., the {} pairs that follow \dfrac. The
1578
- * recursion is to allow for nested \dfrac elements.
1579
- *
1580
- * Backslash-escapes are removed with a simple search-and-replace.
1581
- */
1582
- function parseTex(tex) {
1583
- const handler = function handler(exp1, exp2) {
1584
- return exp1 + "/" + exp2;
1585
- };
1586
- const texWithoutFracs = walkTex(tex, handler);
1587
- return texWithoutFracs.replace("\\%", "%");
1588
- }
1589
-
1590
- const answerFormButtons = [{
1591
- title: "Integers",
1592
- value: "integer",
1593
- content: "6"
1594
- }, {
1595
- title: "Decimals",
1596
- value: "decimal",
1597
- content: "0.75"
1598
- }, {
1599
- title: "Proper fractions",
1600
- value: "proper",
1601
- content: "\u2157"
1602
- }, {
1603
- title: "Improper fractions",
1604
- value: "improper",
1605
- content: "\u2077\u2044\u2084"
1606
- }, {
1607
- title: "Mixed numbers",
1608
- value: "mixed",
1609
- content: "1\u00BE"
1610
- }, {
1611
- title: "Numbers with \u03C0",
1612
- value: "pi",
1613
- content: "\u03C0"
1614
- }];
1615
-
1616
- // This function checks if the user inputted a percent value, parsing
1617
- // it as a number (and maybe scaling) so that it can be graded.
1618
- // NOTE(michaelpolyak): Unlike `KhanAnswerTypes.number.percent()` which
1619
- // can accept several input forms with or without "%", the decision
1620
- // to parse based on the presence of "%" in the input, is so that we
1621
- // don't accidently scale the user typed value before grading, CP-930.
1622
- function maybeParsePercentInput(inputValue, normalizedAnswerExpected) {
1623
- // If the input value is not a string ending with "%", then there's
1624
- // nothing more to do. The value will be graded as inputted by user.
1625
- if (!(typeof inputValue === "string" && inputValue.endsWith("%"))) {
1626
- return inputValue;
1627
- }
1628
- const value = parseFloat(inputValue.slice(0, -1));
1629
- // If the input value stripped of the "%" cannot be parsed as a
1630
- // number (the slice is not really necessary for parseFloat to work
1631
- // if the string starts with a number) then return the original
1632
- // input for grading.
1633
- if (isNaN(value)) {
1634
- return inputValue;
1635
- }
1636
-
1637
- // Next, if all correct answers are in the range of |0,1| then we
1638
- // scale the user typed value. We assume this is the correct thing
1639
- // to do since the input value ends with "%".
1640
- if (normalizedAnswerExpected) {
1641
- return value / 100;
1642
- }
1643
-
1644
- // Otherwise, we return input value (number) stripped of the "%".
1645
- return value;
1646
- }
1647
- function scoreNumericInput(userInput, rubric) {
1648
- var _matchedAnswer$messag;
1649
- const defaultAnswerForms = answerFormButtons.map(e => e["value"])
1650
- // Don't default to validating the answer as a pi answer
1651
- // if answerForm isn't set on the answer
1652
- // https://khanacademy.atlassian.net/browse/LC-691
1653
- .filter(e => e !== "pi");
1654
- const createValidator = answer => {
1655
- var _answer$answerForms;
1656
- const stringAnswer = `${answer.value}`;
1657
-
1658
- // Always validate against the provided answer forms (pi, decimal, etc.)
1659
- const validatorForms = [...((_answer$answerForms = answer.answerForms) != null ? _answer$answerForms : [])];
1660
-
1661
- // When an answer is set to strict, we validate using ONLY
1662
- // the provided answerForms. If strict is false, or if there
1663
- // were no provided answer forms, we will include all
1664
- // of the default answer forms in our validator.
1665
- if (!answer.strict || validatorForms.length === 0) {
1666
- validatorForms.push(...defaultAnswerForms);
1667
- }
1668
- return KhanAnswerTypes.number.createValidatorFunctional(stringAnswer, {
1669
- message: answer.message,
1670
- simplify: answer.status === "correct" ? answer.simplify : "optional",
1671
- inexact: true,
1672
- // TODO(merlob) backfill / delete
1673
- maxError: answer.maxError,
1674
- forms: validatorForms
1675
- });
1676
- };
1677
-
1678
- // We may have received TeX; try to parse it before grading.
1679
- // If `currentValue` is not TeX, this should be a no-op.
1680
- const currentValue = parseTex(userInput.currentValue);
1681
- const normalizedAnswerExpected = rubric.answers.filter(answer => answer.status === "correct").every(answer => answer.value != null && Math.abs(answer.value) <= 1);
1682
-
1683
- // The coefficient is an attribute of the widget
1684
- let localValue = currentValue;
1685
- if (rubric.coefficient) {
1686
- if (!localValue) {
1687
- localValue = 1;
1688
- } else if (localValue === "-") {
1689
- localValue = -1;
1690
- }
1691
- }
1692
- const matchedAnswer = rubric.answers.map(answer => {
1693
- const validateFn = createValidator(answer);
1694
- const score = validateFn(maybeParsePercentInput(localValue, normalizedAnswerExpected));
1695
- return _extends({}, answer, {
1696
- score
1697
- });
1698
- }).find(answer => {
1699
- // NOTE: "answer.score.correct" indicates a match via the validate function.
1700
- // It does NOT indicate that the answer itself is correct.
1701
- return answer.score.correct || answer.status === "correct" && answer.score.empty;
1702
- });
1703
- const result = (matchedAnswer == null ? void 0 : matchedAnswer.status) === "correct" ? matchedAnswer.score : {
1704
- empty: (matchedAnswer == null ? void 0 : matchedAnswer.status) === "ungraded",
1705
- correct: (matchedAnswer == null ? void 0 : matchedAnswer.status) === "correct",
1706
- message: (_matchedAnswer$messag = matchedAnswer == null ? void 0 : matchedAnswer.message) != null ? _matchedAnswer$messag : null};
1707
- if (result.empty) {
1708
- return {
1709
- type: "invalid",
1710
- message: result.message
1711
- };
1712
- }
1713
- return {
1714
- type: "points",
1715
- earned: result.correct ? 1 : 0,
1716
- total: 1,
1717
- message: result.message
1718
- };
1719
- }
1720
-
1721
- function scoreOrderer(userInput, rubric) {
1722
- const correct = _.isEqual(userInput.current, rubric.correctOptions.map(option => option.content));
1723
- return {
1724
- type: "points",
1725
- earned: correct ? 1 : 0,
1726
- total: 1,
1727
- message: null
1728
- };
1729
- }
1730
-
1731
- /**
1732
- * Checks user input from the orderer widget to see if the user has started
1733
- * ordering the options, making the widget scorable.
1734
- * @param userInput
1735
- * @see `scoreOrderer` for more details.
1736
- */
1737
- function validateOrderer(userInput) {
1738
- if (userInput.current.length === 0) {
1739
- return {
1740
- type: "invalid",
1741
- message: null
1742
- };
1743
- }
1744
- return null;
1745
- }
1746
-
1747
- function scorePlotter(userInput, rubric) {
1748
- return {
1749
- type: "points",
1750
- earned: approximateDeepEqual(userInput, rubric.correct) ? 1 : 0,
1751
- total: 1,
1752
- message: null
1753
- };
1754
- }
1755
-
1756
- /**
1757
- * Checks user input to confirm it is not the same as the starting values for the graph.
1758
- * This means the user has modified the graph, and the question can be scored.
1759
- *
1760
- * @see 'scorePlotter' for more details on scoring.
1761
- */
1762
- function validatePlotter(userInput, validationData) {
1763
- if (approximateDeepEqual(userInput, validationData.starting)) {
1764
- return {
1765
- type: "invalid",
1766
- message: null
1767
- };
1768
- }
1769
- return null;
1770
- }
1771
-
1772
- function scoreRadio(userInput, rubric) {
1773
- const numSelected = userInput.choicesSelected.reduce((sum, selected) => {
1774
- return sum + (selected ? 1 : 0);
1775
- }, 0);
1776
- const numCorrect = rubric.choices.reduce((sum, currentChoice) => {
1777
- return currentChoice.correct ? sum + 1 : sum;
1778
- }, 0);
1779
- if (numCorrect > 1 && numSelected !== numCorrect) {
1780
- return {
1781
- type: "invalid",
1782
- message: ErrorCodes.CHOOSE_CORRECT_NUM_ERROR
1783
- };
1784
- // If NOTA and some other answer are checked, ...
1785
- }
1786
- const noneOfTheAboveSelected = rubric.choices.some((choice, index) => choice.isNoneOfTheAbove && userInput.choicesSelected[index]);
1787
- if (noneOfTheAboveSelected && numSelected > 1) {
1788
- return {
1789
- type: "invalid",
1790
- message: ErrorCodes.NOT_NONE_ABOVE_ERROR
1791
- };
1792
- }
1793
- const correct = userInput.choicesSelected.every((selected, i) => {
1794
- let isCorrect;
1795
- if (rubric.choices[i].isNoneOfTheAbove) {
1796
- isCorrect = rubric.choices.every((choice, j) => {
1797
- return i === j || !choice.correct;
1798
- });
1799
- } else {
1800
- isCorrect = !!rubric.choices[i].correct;
1801
- }
1802
- return isCorrect === selected;
1803
- });
1804
- return {
1805
- type: "points",
1806
- earned: correct ? 1 : 0,
1807
- total: 1,
1808
- message: null
1809
- };
1810
- }
1811
-
1812
- /**
1813
- * Checks if the user has selected at least one option. Additional validation
1814
- * is done in scoreRadio to check if the number of selected options is correct
1815
- * and if the user has selected both a correct option and the "none of the above"
1816
- * option.
1817
- * @param userInput
1818
- * @see `scoreRadio` for the additional validation logic and the scoring logic.
1819
- */
1820
- function validateRadio(userInput) {
1821
- const numSelected = userInput.choicesSelected.reduce((sum, selected) => {
1822
- return sum + (selected ? 1 : 0);
1823
- }, 0);
1824
- if (numSelected === 0) {
1825
- return {
1826
- type: "invalid",
1827
- message: null
1828
- };
1829
- }
1830
- return null;
1831
- }
1832
-
1833
- function scoreSorter(userInput, rubric) {
1834
- const correct = approximateDeepEqual(userInput.options, rubric.correct);
1835
- return {
1836
- type: "points",
1837
- earned: correct ? 1 : 0,
1838
- total: 1,
1839
- message: null
1840
- };
1841
- }
1842
-
1843
- /**
1844
- * Checks user input for the sorter widget to ensure that the user has made
1845
- * changes before attempting to score the widget.
1846
- * @param userInput
1847
- * @see 'scoreSorter' in 'packages/perseus/src/widgets/sorter/score-sorter.ts'
1848
- * for more details on how the sorter widget is scored.
1849
- */
1850
- function validateSorter(userInput) {
1851
- // If the sorter widget hasn't been changed yet, we treat it as "empty" which
1852
- // prevents the "Check" button from becoming active. We want the user
1853
- // to make a change before trying to move forward. This makes an
1854
- // assumption that the initial order isn't the correct order! However,
1855
- // this should be rare if it happens, and interacting with the list
1856
- // will enable the button, so they won't be locked out of progressing.
1857
- if (!userInput.changed) {
1858
- return {
1859
- type: "invalid",
1860
- message: null
1861
- };
1862
- }
1863
- return null;
1864
- }
1865
-
1866
- /**
1867
- * Filters the given table (modelled as a 2D array) to remove any rows that are
1868
- * completely empty.
1869
- *
1870
- * @returns A new table with only non-empty rows.
1871
- */
1872
- const filterNonEmpty = function filterNonEmpty(table) {
1873
- return table.filter(function (row) {
1874
- // Return only rows that are non-empty.
1875
- return row.some(cell => cell);
1876
- });
1877
- };
1878
-
1879
- function validateTable(userInput) {
1880
- const supplied = filterNonEmpty(userInput);
1881
- const hasEmptyCell = supplied.some(function (row) {
1882
- return row.some(function (cell) {
1883
- return cell === "";
1884
- });
1885
- });
1886
-
1887
- // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
1888
- if (hasEmptyCell || !supplied.length) {
1889
- return {
1890
- type: "invalid",
1891
- message: null
1892
- };
1893
- }
1894
- return null;
1895
- }
1896
-
1897
- function scoreTable(userInput, rubric) {
1898
- const validationResult = validateTable(userInput);
1899
- if (validationResult != null) {
1900
- return validationResult;
1901
- }
1902
- const supplied = filterNonEmpty(userInput);
1903
- const solution = filterNonEmpty(rubric.answers);
1904
- if (supplied.length !== solution.length) {
1905
- return {
1906
- type: "points",
1907
- earned: 0,
1908
- total: 1,
1909
- message: null
1910
- };
1911
- }
1912
- const createValidator = KhanAnswerTypes.number.createValidatorFunctional;
1913
- let message = null;
1914
- const allCorrect = solution.every(function (rowSolution) {
1915
- for (let i = 0; i < supplied.length; i++) {
1916
- const rowSupplied = supplied[i];
1917
- const correct = rowSupplied.every(function (cellSupplied, i) {
1918
- const cellSolution = rowSolution[i];
1919
- const validator = createValidator(cellSolution, {
1920
- simplify: true
1921
- });
1922
- const result = validator(cellSupplied);
1923
- if (result.message) {
1924
- message = result.message;
1925
- }
1926
- return result.correct;
1927
- });
1928
- if (correct) {
1929
- supplied.splice(i, 1);
1930
- return true;
1931
- }
1932
- }
1933
- return false;
1934
- });
1935
- return {
1936
- type: "points",
1937
- earned: allCorrect ? 1 : 0,
1938
- total: 1,
1939
- message
1940
- };
1941
- }
1942
-
1943
- const inputNumberAnswerTypes = {
1944
- number: {
1945
- name: "Numbers",
1946
- forms: "integer, decimal, proper, improper, mixed"
1947
- },
1948
- decimal: {
1949
- name: "Decimals",
1950
- forms: "decimal"
1951
- },
1952
- integer: {
1953
- name: "Integers",
1954
- forms: "integer"
1955
- },
1956
- rational: {
1957
- name: "Fractions and mixed numbers",
1958
- forms: "integer, proper, improper, mixed"
1959
- },
1960
- improper: {
1961
- name: "Improper numbers (no mixed)",
1962
- forms: "integer, proper, improper"
1963
- },
1964
- mixed: {
1965
- name: "Mixed numbers (no improper)",
1966
- forms: "integer, proper, mixed"
1967
- },
1968
- percent: {
1969
- name: "Numbers or percents",
1970
- forms: "integer, decimal, proper, improper, mixed, percent"
1971
- },
1972
- pi: {
1973
- name: "Numbers with pi",
1974
- forms: "pi"
1975
- }
1976
- };
1977
- function scoreInputNumber(userInput, rubric) {
1978
- if (rubric.answerType == null) {
1979
- rubric.answerType = "number";
1980
- }
1981
-
1982
- // note(matthewc): this will get immediately parsed again by
1983
- // `KhanAnswerTypes.number.convertToPredicate`, but a string is
1984
- // expected here
1985
- const stringValue = `${rubric.value}`;
1986
- const val = KhanAnswerTypes.number.createValidatorFunctional(stringValue, {
1987
- simplify: rubric.simplify,
1988
- inexact: rubric.inexact || undefined,
1989
- maxError: rubric.maxError,
1990
- forms: inputNumberAnswerTypes[rubric.answerType].forms
1991
- });
1992
-
1993
- // We may have received TeX; try to parse it before grading.
1994
- // If `currentValue` is not TeX, this should be a no-op.
1995
- const currentValue = parseTex(userInput.currentValue);
1996
- const result = val(currentValue);
1997
- if (result.empty) {
1998
- return {
1999
- type: "invalid",
2000
- message: result.message
2001
- };
2002
- }
2003
- return {
2004
- type: "points",
2005
- earned: result.correct ? 1 : 0,
2006
- total: 1,
2007
- message: result.message
2008
- };
2009
- }
2010
-
2011
- /**
2012
- * Several widgets don't have "right"/"wrong" scoring logic,
2013
- * so this just says to move on past those widgets
2014
- *
2015
- * TODO(LEMS-2543) widgets that use this probably shouldn't have any
2016
- * scoring logic and the thing scoring an exercise
2017
- * should just know to skip these
2018
- */
2019
- function scoreNoop(points = 0) {
2020
- return {
2021
- type: "points",
2022
- earned: points,
2023
- total: points,
2024
- message: null
2025
- };
2026
- }
2027
-
2028
- // The `group` widget is basically a widget hosting a full Perseus system in
2029
-
2030
- // it. As such, scoring a group means scoring all widgets it contains.
2031
- function scoreGroup(userInput, rubric, locale) {
2032
- const scores = scoreWidgetsFunctional(rubric.widgets, Object.keys(rubric.widgets), userInput, locale);
2033
- return flattenScores(scores);
2034
- }
2035
-
2036
- /**
2037
- * Checks the given user input to see if any answerable widgets have not been
2038
- * "filled in" (ie. if they're empty). Another way to think about this
2039
- * function is that its a check to see if we can score the provided input.
2040
- */
2041
- function emptyWidgetsFunctional(widgets,
2042
- // This is a port of old code, I'm not sure why
2043
- // we need widgetIds vs the keys of the widgets object
2044
- widgetIds, userInputMap, locale) {
2045
- return widgetIds.filter(id => {
2046
- const widget = widgets[id];
2047
- if (!widget || widget.static === true) {
2048
- // Static widgets shouldn't count as empty
2049
- return false;
2050
- }
2051
- const validator = getWidgetValidator(widget.type);
2052
- const userInput = userInputMap[id];
2053
- const validationData = widget.options;
2054
- const score = validator == null ? void 0 : validator(userInput, validationData, locale);
2055
- if (score) {
2056
- return scoreIsEmpty(score);
2057
- }
2058
- });
2059
- }
2060
-
2061
- function validateGroup(userInput, validationData, locale) {
2062
- const emptyWidgets = emptyWidgetsFunctional(validationData.widgets, Object.keys(validationData.widgets), userInput, locale);
2063
- if (emptyWidgets.length === 0) {
2064
- return null;
2065
- }
2066
- return {
2067
- type: "invalid",
2068
- message: null
2069
- };
2070
- }
2071
-
2072
- function validateLabelImage(userInput) {
2073
- let numAnswered = 0;
2074
- for (let i = 0; i < userInput.markers.length; i++) {
2075
- const userSelection = userInput.markers[i].selected;
2076
- if (userSelection && userSelection.length > 0) {
2077
- numAnswered++;
2078
- }
2079
- }
2080
- // We expect all question markers to be answered before grading.
2081
- if (numAnswered !== userInput.markers.length) {
2082
- return {
2083
- type: "invalid",
2084
- message: null
2085
- };
2086
- }
2087
- return null;
2088
- }
2089
-
2090
- function validateMockWidget(userInput) {
2091
- if (userInput.currentValue == null || userInput.currentValue === "") {
2092
- return {
2093
- type: "invalid",
2094
- message: ""
2095
- };
2096
- }
2097
- return null;
2098
- }
2099
-
2100
- function scoreMockWidget(userInput, rubric) {
2101
- const validationResult = validateMockWidget(userInput);
2102
- if (validationResult != null) {
2103
- return validationResult;
2104
- }
2105
- return {
2106
- type: "points",
2107
- earned: userInput.currentValue === rubric.value ? 1 : 0,
2108
- total: 1,
2109
- message: ""
2110
- };
2111
- }
2112
-
2113
- const widgets = {};
2114
- function registerWidget(type, scorer, validator) {
2115
- widgets[type] = {
2116
- scorer,
2117
- validator
2118
- };
2119
- }
2120
- const getWidgetValidator = name => {
2121
- var _widgets$name$validat, _widgets$name;
2122
- return (_widgets$name$validat = (_widgets$name = widgets[name]) == null ? void 0 : _widgets$name.validator) != null ? _widgets$name$validat : null;
2123
- };
2124
- const getWidgetScorer = name => {
2125
- var _widgets$name$scorer, _widgets$name2;
2126
- return (_widgets$name$scorer = (_widgets$name2 = widgets[name]) == null ? void 0 : _widgets$name2.scorer) != null ? _widgets$name$scorer : null;
2127
- };
2128
- registerWidget("categorizer", scoreCategorizer, validateCategorizer);
2129
- registerWidget("cs-program", scoreCSProgram);
2130
- registerWidget("dropdown", scoreDropdown, validateDropdown);
2131
- registerWidget("expression", scoreExpression, validateExpression);
2132
- registerWidget("grapher", scoreGrapher);
2133
- registerWidget("group", scoreGroup, validateGroup);
2134
- registerWidget("iframe", scoreIframe);
2135
- registerWidget("input-number", scoreInputNumber);
2136
- registerWidget("interactive-graph", scoreInteractiveGraph);
2137
- registerWidget("label-image", scoreLabelImage, validateLabelImage);
2138
- registerWidget("matcher", scoreMatcher);
2139
- registerWidget("matrix", scoreMatrix, validateMatrix);
2140
- registerWidget("mock-widget", scoreMockWidget, scoreMockWidget);
2141
- registerWidget("number-line", scoreNumberLine, validateNumberLine);
2142
- registerWidget("numeric-input", scoreNumericInput);
2143
- registerWidget("orderer", scoreOrderer, validateOrderer);
2144
- registerWidget("plotter", scorePlotter, validatePlotter);
2145
- registerWidget("radio", scoreRadio, validateRadio);
2146
- registerWidget("sorter", scoreSorter, validateSorter);
2147
- registerWidget("table", scoreTable, validateTable);
2148
- registerWidget("deprecated-standin", () => scoreNoop(1));
2149
- registerWidget("measurer", () => scoreNoop(1));
2150
- registerWidget("definition", scoreNoop);
2151
- registerWidget("explanation", scoreNoop);
2152
- registerWidget("image", scoreNoop);
2153
- registerWidget("interaction", scoreNoop);
2154
- registerWidget("molecule", scoreNoop);
2155
- registerWidget("passage", scoreNoop);
2156
- registerWidget("passage-ref", scoreNoop);
2157
- registerWidget("passage-ref-target", scoreNoop);
2158
- registerWidget("video", scoreNoop);
2159
-
2160
- const noScore = {
2161
- type: "points",
2162
- earned: 0,
2163
- total: 0,
2164
- message: null
2165
- };
2166
-
2167
- /**
2168
- * If a widget says that it is empty once it is graded.
2169
- * Trying to encapsulate references to the score format.
2170
- */
2171
- function scoreIsEmpty(score) {
2172
- // HACK(benkomalo): ugh. this isn't great; the Perseus score objects
2173
- // overload the type "invalid" for what should probably be three
2174
- // distinct cases:
2175
- // - truly empty or not fully filled out
2176
- // - invalid or malformed inputs
2177
- // - "almost correct" like inputs where the widget wants to give
2178
- // feedback (e.g. a fraction needs to be reduced, or `pi` should
2179
- // be used instead of 3.14)
2180
- //
2181
- // Unfortunately the coercion happens all over the place, as these
2182
- // Perseus style score objects are created *everywhere* (basically
2183
- // in every widget), so it's hard to change now. We assume that
2184
- // anything with a "message" is not truly empty, and one of the
2185
- // latter two cases for now.
2186
- return score.type === "invalid" && (!score.message || score.message.length === 0);
2187
- }
2188
-
2189
- /**
2190
- * Combine two score objects.
2191
- *
2192
- * Given two score objects for two different widgets, combine them so that
2193
- * if one is wrong, the total score is wrong, etc.
2194
- */
2195
- function combineScores(scoreA, scoreB) {
2196
- let message;
2197
- if (scoreA.type === "points" && scoreB.type === "points") {
2198
- if (scoreA.message && scoreB.message && scoreA.message !== scoreB.message) {
2199
- // TODO(alpert): Figure out how to combine messages usefully
2200
- message = null;
2201
- } else {
2202
- message = scoreA.message || scoreB.message;
2203
- }
2204
- return {
2205
- type: "points",
2206
- earned: scoreA.earned + scoreB.earned,
2207
- total: scoreA.total + scoreB.total,
2208
- message: message
2209
- };
2210
- }
2211
- if (scoreA.type === "points" && scoreB.type === "invalid") {
2212
- return scoreB;
2213
- }
2214
- if (scoreA.type === "invalid" && scoreB.type === "points") {
2215
- return scoreA;
2216
- }
2217
- if (scoreA.type === "invalid" && scoreB.type === "invalid") {
2218
- if (scoreA.message && scoreB.message && scoreA.message !== scoreB.message) {
2219
- // TODO(alpert): Figure out how to combine messages usefully
2220
- message = null;
2221
- } else {
2222
- message = scoreA.message || scoreB.message;
2223
- }
2224
- return {
2225
- type: "invalid",
2226
- message: message
2227
- };
2228
- }
2229
-
2230
- /**
2231
- * The above checks cover all combinations of score type, so if we get here
2232
- * then something is amiss with our inputs.
2233
- */
2234
- throw new PerseusError("PerseusScore with unknown type encountered", Errors.InvalidInput, {
2235
- metadata: {
2236
- scoreA: JSON.stringify(scoreA),
2237
- scoreB: JSON.stringify(scoreB)
2238
- }
2239
- });
2240
- }
2241
- function flattenScores(widgetScoreMap) {
2242
- return Object.values(widgetScoreMap).reduce(combineScores, noScore);
2243
- }
2244
-
2245
- /**
2246
- * score a Perseus item
2247
- *
2248
- * @param perseusRenderData - the full answer data, includes the correct answer
2249
- * @param userInputMap - the user's input for each widget, mapped by ID
2250
- * @param locale - string locale for math parsing ("de" 1.000,00 vs "en" 1,000.00)
2251
- */
2252
- function scorePerseusItem(perseusRenderData, userInputMap, locale) {
2253
- // There seems to be a chance that PerseusRenderer.widgets might include
2254
- // widget data for widgets that are not in PerseusRenderer.content,
2255
- // so this checks that the widgets are being used before scoring them
2256
- const usedWidgetIds = getWidgetIdsFromContent(perseusRenderData.content);
2257
- const scores = scoreWidgetsFunctional(perseusRenderData.widgets, usedWidgetIds, userInputMap, locale);
2258
- return flattenScores(scores);
2259
- }
2260
-
2261
- // TODO: combine scorePerseusItem with scoreWidgetsFunctional
2262
- function scoreWidgetsFunctional(widgets,
2263
- // This is a port of old code, I'm not sure why
2264
- // we need widgetIds vs the keys of the widgets object
2265
- widgetIds, userInputMap, locale) {
2266
- const upgradedWidgets = getUpgradedWidgetOptions(widgets);
2267
- const gradedWidgetIds = widgetIds.filter(id => {
2268
- const props = upgradedWidgets[id];
2269
- const widgetIsGraded = (props == null ? void 0 : props.graded) == null || props.graded;
2270
- const widgetIsStatic = !!(props != null && props.static);
2271
- // Ungraded widgets or widgets set to static shouldn't be graded.
2272
- return widgetIsGraded && !widgetIsStatic;
2273
- });
2274
- const widgetScores = {};
2275
- gradedWidgetIds.forEach(id => {
2276
- var _validator;
2277
- const widget = upgradedWidgets[id];
2278
- if (!widget) {
2279
- return;
2280
- }
2281
- const userInput = userInputMap[id];
2282
- const validator = getWidgetValidator(widget.type);
2283
- const scorer = getWidgetScorer(widget.type);
2284
-
2285
- // We do validation (empty checks) first and then scoring. If
2286
- // validation fails, it's result is itself a PerseusScore.
2287
- const score = (_validator = validator == null ? void 0 : validator(userInput, widget.options, locale)) != null ? _validator : scorer == null ? void 0 : scorer(userInput, widget.options, locale);
2288
- if (score != null) {
2289
- widgetScores[id] = score;
2290
- }
2291
- });
2292
- return widgetScores;
2293
- }
86
+ const noScore={type:"points",earned:0,total:0,message:null};function scoreIsEmpty(score){return score.type==="invalid"&&(!score.message||score.message.length===0)}function combineScores(scoreA,scoreB){let message;if(scoreA.type==="points"&&scoreB.type==="points"){if(scoreA.message&&scoreB.message&&scoreA.message!==scoreB.message){message=null;}else {message=scoreA.message||scoreB.message;}return {type:"points",earned:scoreA.earned+scoreB.earned,total:scoreA.total+scoreB.total,message:message}}if(scoreA.type==="points"&&scoreB.type==="invalid"){return scoreB}if(scoreA.type==="invalid"&&scoreB.type==="points"){return scoreA}if(scoreA.type==="invalid"&&scoreB.type==="invalid"){if(scoreA.message&&scoreB.message&&scoreA.message!==scoreB.message){message=null;}else {message=scoreA.message||scoreB.message;}return {type:"invalid",message:message}}throw new PerseusError("PerseusScore with unknown type encountered",Errors.InvalidInput,{metadata:{scoreA:JSON.stringify(scoreA),scoreB:JSON.stringify(scoreB)}})}function flattenScores(widgetScoreMap){return Object.values(widgetScoreMap).reduce(combineScores,noScore)}function scorePerseusItem(perseusRenderData,userInputMap,locale){const usedWidgetIds=getWidgetIdsFromContent(perseusRenderData.content);const scores=scoreWidgetsFunctional(perseusRenderData.widgets,usedWidgetIds,userInputMap,locale);return flattenScores(scores)}function scoreWidgetsFunctional(widgets,widgetIds,userInputMap,locale){const upgradedWidgets=getUpgradedWidgetOptions(widgets);const gradedWidgetIds=widgetIds.filter(id=>{const props=upgradedWidgets[id];const widgetIsGraded=props?.graded==null||props.graded;const widgetIsStatic=!!props?.static;return widgetIsGraded&&!widgetIsStatic});const widgetScores={};gradedWidgetIds.forEach(id=>{const widget=upgradedWidgets[id];if(!widget){return}const userInput=userInputMap[id];const validator=getWidgetValidator(widget.type);const scorer=getWidgetScorer(widget.type);const score=validator?.(userInput,widget.options,locale)??scorer?.(userInput,widget.options,locale);if(score!=null){widgetScores[id]=score;}});return widgetScores}
2294
87
 
2295
88
  export { ErrorCodes, KhanAnswerTypes, emptyWidgetsFunctional, flattenScores, getWidgetScorer, getWidgetValidator, inputNumberAnswerTypes, registerWidget, scoreCSProgram, scoreCategorizer, scoreDropdown, scoreExpression, scoreGrapher, scoreIframe, scoreInputNumber, scoreInteractiveGraph, scoreLabelImage, scoreLabelImageMarker, scoreMatcher, scoreMatrix, scoreNumberLine, scoreNumericInput, scoreOrderer, scorePerseusItem, scorePlotter, scoreRadio, scoreSorter, scoreTable, scoreWidgetsFunctional, validateCategorizer, validateDropdown, validateExpression, validateMatrix, validateNumberLine, validateOrderer, validatePlotter, validateRadio, validateSorter, validateTable };
2296
89
  //# sourceMappingURL=index.js.map