'Implements the answer-pool subsetting operation in-place on the tree.
Allows for problem questions with a pool of answers, from which answer options shown to the student
and randomly selected so that there is always 1 correct answer and n-1 incorrect answers,
where the author specifies n as the value of the attribute "answer-pool" within <choicegroup>
The <choicegroup> tag must have an attribute \'answer-pool\' giving the desired
pool size. If that attribute is zero or not present, no operation is performed.
Calling this a second time does nothing.
Raises LoncapaProblemError if the answer-pool value is not an integer,
or if the number of correct or incorrect choices available is zero.'

def do_answer_pool(self, tree, problem):

    choicegroups = tree.xpath('choicegroup[@answer-pool]')
    if choicegroups:
        choicegroup = choicegroups[0]
        num_str = choicegroup.get('answer-pool')
        if (num_str == '0'):
            return
        try:
            num_choices = int(num_str)
        except ValueError:
            _ = self.capa_system.i18n.ugettext
            msg = _('answer-pool    value    should    be    an    integer')
            raise LoncapaProblemError(msg)
        if self.has_answerpool():
            return
        self._has_answerpool = True
        choices_list = list(choicegroup.getchildren())
        for choice in choices_list:
            choicegroup.remove(choice)
        rng = self.get_rng(problem)
        (solution_id, subset_choices) = self.sample_from_answer_pool(choices_list, rng, num_choices)
        for choice in subset_choices:
            choicegroup.append(choice)
        solutionset = choicegroup.xpath('../following-sibling::solutionset')
        if (len(solutionset) != 0):
            solutionset = solutionset[0]
            solutions = solutionset.xpath('./solution')
            for solution in solutions:
                if (solution.get('explanation-id') != solution_id):
                    solutionset.remove(solution)
