diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 551ae2e7b9..584b062169 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -5,6 +5,8 @@ These are notable changes in edx-platform. This is a rolling list of changes, in roughly chronological order, most recent first. Add your entries at or near the top. Include a label indicating the component affected. +Blades: Allow multiple strings as the correct answer to a string response question. BLD-474. + Blades: a11y - Videos will alert screenreaders when the video is over. LMS: Trap focus on the loading element when a user loads more threads diff --git a/cms/templates/widgets/problem-edit.html b/cms/templates/widgets/problem-edit.html index 94c6a578e1..855939743a 100644 --- a/cms/templates/widgets/problem-edit.html +++ b/cms/templates/widgets/problem-edit.html @@ -75,7 +75,9 @@
-
= dog
+
= dog
+or= cat
+or= mouse
diff --git a/common/lib/capa/capa/responsetypes.py b/common/lib/capa/capa/responsetypes.py index 6e9dda13f8..c71c1a7632 100644 --- a/common/lib/capa/capa/responsetypes.py +++ b/common/lib/capa/capa/responsetypes.py @@ -946,17 +946,34 @@ class NumericalResponse(LoncapaResponse): class StringResponse(LoncapaResponse): + ''' + This response type allows one or more answers. Use `_or_` separator to set + more than 1 answer. + Example: + + # One answer + + + + + # Multiple answers + + + + + ''' response_tag = 'stringresponse' hint_tag = 'stringhint' allowed_inputfields = ['textline'] required_attributes = ['answer'] max_inputfields = 1 - correct_answer = None + correct_answer = [] + SEPARATOR = '_or_' def setup_response(self): - self.correct_answer = contextualize_text( - self.xml.get('answer'), self.context).strip() + self.correct_answer = [contextualize_text(answer, self.context).strip() + for answer in self.xml.get('answer').split(self.SEPARATOR)] def get_score(self, student_answers): '''Grade a string response ''' @@ -966,23 +983,25 @@ class StringResponse(LoncapaResponse): def check_string(self, expected, given): if self.xml.get('type') == 'ci': - return given.lower() == expected.lower() - return given == expected + return given.lower() in [i.lower() for i in expected] + return given in expected def check_hint_condition(self, hxml_set, student_answers): given = student_answers[self.answer_id].strip() hints_to_show = [] for hxml in hxml_set: name = hxml.get('name') - correct_answer = contextualize_text( - hxml.get('answer'), self.context).strip() + + correct_answer = [contextualize_text(answer, self.context).strip() + for answer in hxml.get('answer').split(self.SEPARATOR)] + if self.check_string(correct_answer, given): hints_to_show.append(name) log.debug('hints_to_show = %s', hints_to_show) return hints_to_show def get_answers(self): - return {self.answer_id: self.correct_answer} + return {self.answer_id: ' or '.join(self.correct_answer)} #----------------------------------------------------------------------------- diff --git a/common/lib/capa/capa/tests/test_responsetypes.py b/common/lib/capa/capa/tests/test_responsetypes.py index ba20bcbe0d..99b3f1d52c 100644 --- a/common/lib/capa/capa/tests/test_responsetypes.py +++ b/common/lib/capa/capa/tests/test_responsetypes.py @@ -500,6 +500,7 @@ class StringResponseTest(ResponseTest): xml_factory_class = StringResponseXMLFactory def test_case_sensitive(self): + # Test single answer problem = self.build_problem(answer="Second", case_sensitive=True) # Exact string should be correct @@ -509,7 +510,20 @@ class StringResponseTest(ResponseTest): self.assert_grade(problem, "Other String", "incorrect") self.assert_grade(problem, "second", "incorrect") + # Test multiple answers + answers = ["Second", "Third", "Fourth"] + problem = self.build_problem(answer="_or_".join(answers), case_sensitive=True) + + for answer in answers: + # Exact string should be correct + self.assert_grade(problem, answer, "correct") + + # Other strings and the lowercase version of the string are incorrect + self.assert_grade(problem, "Other String", "incorrect") + self.assert_grade(problem, "second", "incorrect") + def test_case_insensitive(self): + # Test single answer problem = self.build_problem(answer="Second", case_sensitive=False) # Both versions of the string should be allowed, regardless @@ -520,9 +534,28 @@ class StringResponseTest(ResponseTest): # Other strings are not allowed self.assert_grade(problem, "Other String", "incorrect") + # Test multiple answers + answers = ["Second", "Third", "Fourth"] + problem = self.build_problem(answer="_or_".join(answers), case_sensitive=False) + + for answer in answers: + # Exact string should be correct + self.assert_grade(problem, answer, "correct") + self.assert_grade(problem, answer.lower(), "correct") + + # Other strings and the lowercase version of the string are incorrect + self.assert_grade(problem, "Other String", "incorrect") + def test_hints(self): + multiple_answers = [ + "Martin Luther King Junior", + "Doctor Martin Luther King Junior", + "Dr. Martin Luther King Jr.", + "Martin Luther King" + ] hints = [("wisconsin", "wisc", "The state capital of Wisconsin is Madison"), - ("minnesota", "minn", "The state capital of Minnesota is St. Paul")] + ("minnesota", "minn", "The state capital of Minnesota is St. Paul"), + ("_or_".join(multiple_answers), "mlk", "He lead the civil right movement in the United States of America.")] problem = self.build_problem(answer="Michigan", case_sensitive=False, @@ -550,6 +583,14 @@ class StringResponseTest(ResponseTest): correct_map = problem.grade_answers(input_dict) self.assertEquals(correct_map.get_hint('1_2_1'), "") + # We should get the same hint for each answer + for answer in multiple_answers: + input_dict = {'1_2_1': answer} + correct_map = problem.grade_answers(input_dict) + self.assertEquals(correct_map.get_hint('1_2_1'), + "He lead the civil right movement in the United States of America.") + + def test_computed_hints(self): problem = self.build_problem( answer="Michigan", diff --git a/common/lib/xmodule/xmodule/js/spec/problem/edit_spec.coffee b/common/lib/xmodule/xmodule/js/spec/problem/edit_spec.coffee index c0c9b88441..ea1341cc23 100644 --- a/common/lib/xmodule/xmodule/js/spec/problem/edit_spec.coffee +++ b/common/lib/xmodule/xmodule/js/spec/problem/edit_spec.coffee @@ -104,45 +104,45 @@ describe 'MarkdownEditingDescriptor', -> Enter the number of fingers on a human hand: = 5 - + [Explanation] Pi, or the the ratio between a circle's circumference to its diameter, is an irrational number known to extreme precision. It is value is approximately equal to 3.14. - + Although you can get an exact value by typing 502*9 into a calculator, the result will be close to 500*10, or 5,000. The grader accepts any response within 15% of the true value, 4518, so that you can use any estimation technique that you like. - + If you look at your hand, you can count that you have five fingers. [Explanation] """) expect(data).toEqual("""

A numerical response problem accepts a line of text input from the student, and evaluates the input for correctness based on its numerical value.

- +

The answer is correct if it is within a specified numerical tolerance of the expected answer.

- +

Enter the numerical value of Pi:

- +

Enter the approximate value of 502*9:

- +

Enter the number of fingers on a human hand:

- +

Explanation

- +

Pi, or the the ratio between a circle's circumference to its diameter, is an irrational number known to extreme precision. It is value is approximately equal to 3.14.

- +

Although you can get an exact value by typing 502*9 into a calculator, the result will be close to 500*10, or 5,000. The grader accepts any response within 15% of the true value, 4518, so that you can use any estimation technique that you like.

- +

If you look at your hand, you can count that you have five fingers.

@@ -161,12 +161,27 @@ describe 'MarkdownEditingDescriptor', -> +
""") + it 'markup with multiple answers doesn\'t break numerical response', -> + data = MarkdownEditingDescriptor.markdownToXml(""" + Enter 1 with a tolerance: + = 1 +- .02 + or= 2 +- 5% + """) + expect(data).toEqual(""" +

Enter 1 with a tolerance:

+ + + + + +
""") it 'converts multiple choice to xml', -> data = MarkdownEditingDescriptor.markdownToXml("""A multiple choice problem presents radio buttons for student input. Students can only select a single option presented. Multiple Choice questions have been the subject of many areas of research due to the early invention and adoption of bubble sheets. - + One of the main elements that goes into a good multiple choice question is the existence of good distractors. That is, each of the alternate responses presented to the student should be the result of a plausible mistake that a student might make. - + What Apple device competed with the portable CD player? ( ) The iPad ( ) Napster @@ -174,16 +189,16 @@ describe 'MarkdownEditingDescriptor', -> ( ) The vegetable peeler ( ) Android ( ) The Beatles - + [Explanation] The release of the iPod allowed consumers to carry their entire music library with them in a format that did not rely on fragile and energy-intensive spinning disks. [Explanation] """) expect(data).toEqual("""

A multiple choice problem presents radio buttons for student input. Students can only select a single option presented. Multiple Choice questions have been the subject of many areas of research due to the early invention and adoption of bubble sheets.

- +

One of the main elements that goes into a good multiple choice question is the existence of good distractors. That is, each of the alternate responses presented to the student should be the result of a plausible mistake that a student might make.

- +

What Apple device competed with the portable CD player?

@@ -195,76 +210,102 @@ describe 'MarkdownEditingDescriptor', -> The Beatles - +

Explanation

- +

The release of the iPod allowed consumers to carry their entire music library with them in a format that did not rely on fragile and energy-intensive spinning disks.

-
""") + """) it 'converts OptionResponse to xml', -> data = MarkdownEditingDescriptor.markdownToXml("""OptionResponse gives a limited set of options for students to respond with, and presents those options in a format that encourages them to search for a specific answer rather than being immediately presented with options from which to recognize the correct answer. - + The answer options and the identification of the correct answer is defined in the optioninput tag. - + Translation between Option Response and __________ is extremely straightforward: [[(Multiple Choice), String Response, Numerical Response, External Response, Image Response]] - + [Explanation] Multiple Choice also allows students to select from a variety of pre-written responses, although the format makes it easier for students to read very long response options. Optionresponse also differs slightly because students are more likely to think of an answer and then search for it rather than relying purely on recognition to answer the question. [Explanation] """) expect(data).toEqual("""

OptionResponse gives a limited set of options for students to respond with, and presents those options in a format that encourages them to search for a specific answer rather than being immediately presented with options from which to recognize the correct answer.

- +

The answer options and the identification of the correct answer is defined in the optioninput tag.

- +

Translation between Option Response and __________ is extremely straightforward:

- + - +

Explanation

- +

Multiple Choice also allows students to select from a variety of pre-written responses, although the format makes it easier for students to read very long response options. Optionresponse also differs slightly because students are more likely to think of an answer and then search for it rather than relying purely on recognition to answer the question.

-
""") + """) it 'converts StringResponse to xml', -> data = MarkdownEditingDescriptor.markdownToXml("""A string response problem accepts a line of text input from the student, and evaluates the input for correctness based on an expected answer within each input box. - + The answer is correct if it matches every character of the expected answer. This can be a problem with international spelling, dates, or anything where the format of the answer is not clear. - + Which US state has Lansing as its capital? = Michigan - + [Explanation] Lansing is the capital of Michigan, although it is not Michgan's largest city, or even the seat of the county in which it resides. [Explanation] """) expect(data).toEqual("""

A string response problem accepts a line of text input from the student, and evaluates the input for correctness based on an expected answer within each input box.

- +

The answer is correct if it matches every character of the expected answer. This can be a problem with international spelling, dates, or anything where the format of the answer is not clear.

- +

Which US state has Lansing as its capital?

- +

Explanation

- +

Lansing is the capital of Michigan, although it is not Michgan's largest city, or even the seat of the county in which it resides.

+
+
+
""") + it 'converts StringResponse with multiple answers to xml', -> + data = MarkdownEditingDescriptor.markdownToXml("""Who lead the civil right movement in the United States of America? + = Dr. Martin Luther King Jr. + or= Doctor Martin Luther King Junior + or= Martin Luther King + or= Martin Luther King Junior + + [Explanation] + Test Explanation. + [Explanation] + """) + expect(data).toEqual(""" +

Who lead the civil right movement in the United States of America?

+ + + + + +
+

Explanation

+ +

Test Explanation.

+
""") @@ -273,26 +314,26 @@ describe 'MarkdownEditingDescriptor', -> data = MarkdownEditingDescriptor.markdownToXml("""Not a header A header ============== - + Multiple choice w/ parentheticals ( ) option (with parens) ( ) xd option (x) ()) parentheses inside () no space b4 close paren - + Choice checks [ ] option1 [x] [x] correct [x] redundant [(] distractor [] no space - + Option with multiple correct ones [[one option, (correct one), (should not be correct)]] - + Option with embedded parens [[My (heart), another, (correct)]] - + What happens w/ empty correct options? [[()]] @@ -300,21 +341,21 @@ describe 'MarkdownEditingDescriptor', -> [explanation] orphaned start - + No p tags in the below - + But in this there should be
Great ideas require offsetting. - + bad tests require drivel
- + [code] Code should be nicely monospaced. [/code] @@ -322,7 +363,7 @@ describe 'MarkdownEditingDescriptor', -> expect(data).toEqual("""

Not a header

A header

- +

Multiple choice w/ parentheticals

@@ -332,7 +373,7 @@ describe 'MarkdownEditingDescriptor', -> no space b4 close paren - +

Choice checks

@@ -343,25 +384,25 @@ describe 'MarkdownEditingDescriptor', -> no space - +

Option with multiple correct ones

- + - +

Option with embedded parens

- + - +

What happens w/ empty correct options?

- + - +

Explanation

@@ -379,14 +420,14 @@ describe 'MarkdownEditingDescriptor', -> console.log(two * 2); - +

But in this there should be

Great ideas require offsetting.

- +

bad tests require drivel

- +

         Code should be nicely monospaced.
         
diff --git a/common/lib/xmodule/xmodule/js/src/problem/edit.coffee b/common/lib/xmodule/xmodule/js/src/problem/edit.coffee index ba85db574b..9f5d361009 100644 --- a/common/lib/xmodule/xmodule/js/src/problem/edit.coffee +++ b/common/lib/xmodule/xmodule/js/src/problem/edit.coffee @@ -228,11 +228,13 @@ class @MarkdownEditingDescriptor extends XModule.Descriptor }); // replace string and numerical - xml = xml.replace(/^\=\s*(.*?$)/gm, function(match, p) { - var string; - var floatValue = parseFloat(p); + xml = xml.replace(/(^\=\s*(.*?$)(\n*or\=\s*(.*?$))*)+/gm, function(match, p) { + var string, + answersList = p.replace(/^(or)?=\s*/gm, '').split('\n'), + floatValue = parseFloat(answersList[0]); + if(!isNaN(floatValue)) { - var params = /(.*?)\+\-\s*(.*?$)/.exec(p); + var params = /(.*?)\+\-\s*(.*?$)/.exec(answersList[0]); if(params) { string = '\n'; string += ' \n'; @@ -242,10 +244,16 @@ class @MarkdownEditingDescriptor extends XModule.Descriptor string += ' \n'; string += '\n\n'; } else { - string = '\n \n\n\n'; + var answers = []; + + for(var i = 0; i < answersList.length; i++) { + answers.push(answersList[i]) + } + + string = '\n \n\n\n'; } return string; - }); + }); // replace selects xml = xml.replace(/\[\[(.+?)\]\]/g, function(match, p) { @@ -262,13 +270,13 @@ class @MarkdownEditingDescriptor extends XModule.Descriptor selectString += '\n\n'; return selectString; }); - + // replace explanations xml = xml.replace(/\[explanation\]\n?([^\]]*)\[\/?explanation\]/gmi, function(match, p1) { var selectString = '\n
\nExplanation\n\n' + p1 + '\n
\n
'; return selectString; }); - + // replace code blocks xml = xml.replace(/\[code\]\n?([^\]]*)\[\/?code\]/gmi, function(match, p1) { var selectString = '
\n' + p1 + '
'; @@ -293,7 +301,7 @@ class @MarkdownEditingDescriptor extends XModule.Descriptor // rid white space xml = xml.replace(/\n\n\n/g, '\n'); - + // surround w/ problem tag xml = '\n' + xml + '\n';