'Implements targeted-feedback in-place on  <multiplechoiceresponse> --
choice-level explanations shown to a student after submission.
Does nothing if there is no targeted-feedback attribute.'

def do_targeted_feedback(self, tree):

    _ = self.capa_system.i18n.ugettext
    if hasattr(self, 'has_targeted'):
        return
    self.has_targeted = True
    for mult_choice_response in tree.xpath('//multiplechoiceresponse[@targeted-feedback]'):
        show_explanation = (mult_choice_response.get('targeted-feedback') == 'alwaysShowCorrectChoiceExplanation')
        choicegroup = mult_choice_response.xpath('./choicegroup[@type="MultipleChoice"]')[0]
        choices_list = list(choicegroup.iter('choice'))
        student_answer = self.student_answers.get(choicegroup.get('id'))
        expl_id_for_student_answer = None
        solution_id = None
        choice_correctness_for_student_answer = _('Incorrect')
        for choice in choices_list:
            if (choice.get('name') == student_answer):
                expl_id_for_student_answer = choice.get('explanation-id')
                if (choice.get('correct') == 'true'):
                    choice_correctness_for_student_answer = _('Correct')
            if (choice.get('correct') == 'true'):
                solution_id = choice.get('explanation-id')
        targetedfeedbackset = mult_choice_response.xpath('./following-sibling::targetedfeedbackset')
        if (len(targetedfeedbackset) != 0):
            targetedfeedbackset = targetedfeedbackset[0]
            targetedfeedbacks = targetedfeedbackset.xpath('./targetedfeedback')
            problem_legend_id = (str(choicegroup.get('id')) + '-legend')
            for targetedfeedback in targetedfeedbacks:
                screenreadertext = etree.Element('span')
                targetedfeedback.insert(0, screenreadertext)
                screenreadertext.set('class', 'sr')
                screenreadertext.text = choice_correctness_for_student_answer
                targetedfeedback.set('role', 'group')
                targetedfeedback.set('aria-describedby', problem_legend_id)
                if ((not self.done) or (targetedfeedback.get('explanation-id') != expl_id_for_student_answer)):
                    targetedfeedbackset.remove(targetedfeedback)
        if ((not show_explanation) or (not self.done)):
            continue
        next_element = targetedfeedbackset.getnext()
        parent_element = tree
        solution_element = None
        if ((next_element is not None) and (next_element.tag == 'solution')):
            solution_element = next_element
        elif ((next_element is not None) and (next_element.tag == 'solutionset')):
            solutions = next_element.xpath('./solution')
            for solution in solutions:
                if (solution.get('explanation-id') == solution_id):
                    parent_element = next_element
                    solution_element = solution
        if (solution_element is None):
            continue
        parent_element.remove(solution_element)
        solution_element.tag = 'targetedfeedback'
        targetedfeedbackset.append(solution_element)
