From 0a62c580838ca8dc137c9e1a32c49cf9b4ea0a1c Mon Sep 17 00:00:00 2001 From: Taras Lytvynenko <69678257+Inferato@users.noreply.github.com> Date: Fri, 10 Nov 2023 01:49:05 +0200 Subject: [PATCH] fix: Adaptation for the Course Authoring problems structure --- xmodule/capa/capa_problem.py | 13 +++++++++++-- xmodule/capa/inputtypes.py | 4 +++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/xmodule/capa/capa_problem.py b/xmodule/capa/capa_problem.py index 797e95e1d5bb..fc06e4dc1d5e 100644 --- a/xmodule/capa/capa_problem.py +++ b/xmodule/capa/capa_problem.py @@ -548,7 +548,14 @@ def find_correct_answer_text(self, answer_id): return answer_id[0] if xml_element.tag == 'optioninput': return xml_element.xpath('@correct')[0] - return ', '.join(xml_element.xpath('*[@correct="true"]/text()')) + + answers_list = xml_element.xpath('*[@correct="true"]//text()') + cleaned_answers = [answer for answer in answers_list if re.sub(r'[^a-zA-Z0-9\s]', '', answer).strip()] + + if not cleaned_answers: + cleaned_answers = xml_element.getparent().xpath('@answer') + + return ', '.join(cleaned_answers) def find_question_label(self, answer_id): """ @@ -625,7 +632,9 @@ def generate_default_question_label(): if questiontext_elem is not None and questiontext_elem.tag in LABEL_ELEMS: question_text = questiontext_elem.text else: - question_text = generate_default_question_label() + # Course authoring store a question inside

tag within the problem + problem_title = xml_elem.xpath('p') or xml_elem.getparent().xpath('p') + question_text = problem_title[0].text if len(problem_title) == 1 else generate_default_question_label() return question_text diff --git a/xmodule/capa/inputtypes.py b/xmodule/capa/inputtypes.py index 5decad4a2ca8..69acc83854f1 100644 --- a/xmodule/capa/inputtypes.py +++ b/xmodule/capa/inputtypes.py @@ -542,7 +542,9 @@ def extract_choices(element, i18n, text_only=False): if not text_only: text = stringify_children(choice) else: - text = choice.text + text_string = choice.xpath('string()') + cleaned_text = re.sub(r'[^a-zA-Z0-9\s]', '', text_string).strip() + text = cleaned_text or text_string choices.append((choice.get("name"), text)) else: if choice.tag != 'compoundhint':