@param GridQuestion $question
@param array        $answer

@return CorrectedAnswer
502502
private function getCorrectAnswerForSumCellsMode(AbstractItem $question, $answer)
    {
        $corrected = new CorrectedAnswer();

        if (!is_null($answer)) {
            foreach ($answer as $gridAnswer) {
                $cell = $question->getCell($gridAnswer['cellId']);
                $choice = $cell->getChoice($gridAnswer['text']);

                if (!empty($choice)) {
                    if (0 < $choice->getScore()) {
                        $corrected->addExpected($choice);
                    } else {
                        $corrected->addUnexpected($choice);
                    }
                } else {
                    // Retrieve the best answer for the cell
                    $corrected->addMissing($this->findCellExpectedAnswer($cell));
                }
            }
        } else {
            $cells = $question->getCells();

            foreach ($cells as $cell) {
                if ($cell->isInput()) {
                    $corrected->addMissing($this->findCellExpectedAnswer($cell));
                }
            }
        }

        return $corrected;
    }
823952
public function expectAnswer(AbstractItem $question)
    {
        return array_filter($question->getRows()->toArray(), function (GridRow $row) {
            return 0 < $row->getScore();
        });
    }
469981
public function getAnswer(array $question): array
    {
        foreach ($this->resolvers as $resolver) {
            $answer = $resolver->getAnswer($question);
            if (!empty($answer)) {
                return $answer;
            }
        }

        return [];
    }
644025
public function removeAnswer(Answer $answer)
    {
        if ($this->answers->contains($answer)) {
            $this->answers->removeElement($answer);
        }
    }
1117511
public function correctAnswer(AbstractItem $question, $answer = [])
    {
        $corrected = new CorrectedAnswer();

        foreach ($question->getChoices() as $choice) {
            if (is_array($answer) && in_array($choice->getUuid(), $answer)) {
                // Choice has been selected by the user
                if (0 < $choice->getScore()) {
                    $corrected->addExpected($choice);
                } else {
                    $corrected->addUnexpected($choice);
                }
            } elseif (0 < $choice->getScore()) {
                // The choice is not selected but it's part of the correct answer
                $corrected->addMissing($choice);
            } else {
                // The choice is not selected as expected in correct answer
                $corrected->addExpectedMissing($choice);
            }
        }

        return $corrected;
    }
1043167
public function removeAnswer(\Lyon1\Bundle\PoobleBundle\Entity\SurveyAnswer $answer)
    {
        $this->answers->removeElement($answer);
    }
11378
public function process_essay($quest, &$questions) {

        $question = $this->process_common($quest);
        $question->qtype = 'essay';

        $question->feedback = array();
        // Not sure where to get the correct answer from?
        foreach ($quest->feedback as $feedback) {
            // Added this code to put the possible solution that the
            // instructor gives as the Moodle answer for an essay question.
            if ($feedback->ident == 'solution') {
                $question->graderinfo = $this->cleaned_text_field($feedback->text);
            }
        }
        // Added because essay/questiontype.php:save_question_option is expecting a
        // fraction property - CT 8/10/06.
        $question->fraction[] = 1;
        $question->defaultmark = 1;
        $question->responseformat = 'editor';
        $question->responserequired = 1;
        $question->responsefieldlines = 15;
        $question->attachments = 0;
        $question->attachmentsrequired = 0;
        $question->responsetemplate = $this->text_field('');

        $questions[] = $question;
    }
1352976
public function correctAnswer(AbstractItem $question, $answer = [])
    {
        $corrected = new CorrectedAnswer();

        if (is_array($answer)) {
            foreach ($answer as $givenAnswer) {
                $item = $question->getItem($givenAnswer['itemId']);
                if (!empty($item->getPosition()) && $item->getPosition() === $givenAnswer['position']) {
                    $corrected->addExpected($item);
                } else {
                    $penalty = new GenericPenalty($question->getPenalty());
                    $corrected->addPenalty($penalty);
                }
            }
        }

        return $corrected;
    }
740579
private function getCorrectAnswerForFixMode(AbstractItem $question, $answer)
    {
        $corrected = new CorrectedAnswer();

        if (!is_null($answer)) {
            foreach ($answer as $gridAnswer) {
                $cell = $question->getCell($gridAnswer['cellId']);
                $choice = $cell->getChoice($gridAnswer['text']);

                if (!empty($choice)) {
                    if ($choice->isExpected()) {
                        $corrected->addExpected($choice);
                    } else {
                        $corrected->addUnexpected($choice);
                    }
                } else {
                    // Retrieve the best answer for the cell
                    $corrected->addMissing($this->findCellExpectedAnswer($cell));
                }
            }
        } else {
            $cells = $question->getCells();

            foreach ($cells as $cell) {
                if (0 < count($cell->getChoices())) {
                    $corrected->addMissing($this->findCellExpectedAnswer($cell));
                }
            }
        }

        return $corrected;
    }
654253
public function expectAnswer(AbstractItem $question)
    {
        $expected = [];

        switch ($question->getSumMode()) {
            case GridQuestion::SUM_CELL:
                foreach ($question->getCells()->toArray() as $cell) {
                    if (0 < count($cell->getChoices())) {
                        $expected[] = $this->findCellExpectedAnswer($cell);
                    }
                }
                break;
            case GridQuestion::SUM_COLUMN:
                for ($i = 0; $i < $question->getColumns(); ++$i) {
                    // get cells where there is at least 1 expected answers for the current column (none possible)
                    foreach ($question->getCells() as $cell) {
                        // found a cell in this col with an expected answer
                        if ($cell->getCoordsX() === $i && 0 < count($cell->getChoices())) {
                            // all choices should have the same score
                            $scoreToApply = $cell->getChoices()[0]->getScore();
                            $expected[] = new GenericScore($scoreToApply);
                            break;
                        }
                    }
                }
                break;
            case GridQuestion::SUM_ROW:
                for ($i = 0; $i < $question->getRows(); ++$i) {
                    // get cells where there is at least 1 expected answers for the current row (none possible)
                    foreach ($question->getCells() as $cell) {
                        // found a cell in this row with an expected answer
                        if ($cell->getCoordsY() === $i && 0 < count($cell->getChoices())) {
                            // all choices should have the same score
                            $scoreToApply = $cell->getChoices()[0]->getScore();
                            $expected[] = new GenericScore($scoreToApply);
                            break;
                        }
                    }
                }
                break;
        }

        return $expected;
    }
735591
public function correctAnswer(AbstractItem $question, $answer)
    {
        $corrected = new CorrectedAnswer();

        if (is_array($answer)) {
            foreach ($question->getAssociations() as $association) {
                $found = false;

                foreach ($answer as $index => $givenAnswer) {
                    if ($association->getProposal()->getUuid() === $givenAnswer['firstId'] && $association->getLabel()->getUuid() === $givenAnswer['secondId']) {
                        $found = true;

                        if (0 < $association->getScore()) {
                            $corrected->addExpected($association);
                        } else {
                            $corrected->addUnexpected($association);
                        }

                        unset($answer[$index]);
                    }
                }

                if (!$found && 0 < $association->getScore()) {
                    $corrected->addMissing($association);
                }
            }

            if (!empty($answer) && $question->getPenalty()) {
                // there are association not defined in the exercise
                $corrected->addPenalty(
                    new GenericPenalty(count($answer) * $question->getPenalty())
                );
            }
        } else {
            foreach ($question->getAssociations() as $association) {
                if (0 < $association->getScore()) {
                    $corrected->addMissing($association);
                }
            }
        }

        return $corrected;
    }
257900
protected function askYn( $question ) {
		$answer = $this->ask( $question .' [Y/n] ');
		while ( ! in_array( strtolower( $answer ), [ 'y', 'n', 'yes', 'no' ] ) ) {
			$answer = $this->ask('Please choose either yes or no. ');
		}
		return in_array( strtolower( $answer ), [ 'y', 'yes' ] );
	}
1147209
public function evaluateQuestion(array $question, PhpStringParser $parser, GeneratorDataObject $generator)
    {
        return $this->getInstance($question)->ask($question, $parser, $generator);
    }
165700
def addAnswers(self, answers, callback=None, errback=None, **kwargs):
        
        if not self.data:
            raise RecordException('record not loaded')
        orig_answers = self.data['answers']
        new_answers = self._rest._getAnswersForBody(answers)
        orig_answers.extend(new_answers)
        return self.update(answers=orig_answers, callback=callback,
                           errback=errback, **kwargs)
702221
public function calculateScore(Item $question, Answer $answer)
    {
        // Let the question correct the answer
        $definition = $this->itemDefinitions->get($question->getMimeType());
        /** @var AnswerableItemDefinitionInterface $definition */
        $corrected = $definition->correctAnswer($question->getInteraction(), json_decode($answer->getData(), true));
        if (!$corrected instanceof CorrectedAnswer) {
            $corrected = new CorrectedAnswer();
        }

        // Add hints
        foreach ($answer->getUsedHints() as $hintId) {
            // Get hint definition from question data
            $hint = null;
            foreach ($question->getHints() as $questionHint) {
                if ($hintId === $questionHint->getUuid()) {
                    $hint = $questionHint;
                    break;
                }
            }
            if ($hint) {
                $corrected->addPenalty($hint);
            }
        }

        return $this->scoreManager->calculate(json_decode($question->getScoreRule(), true), $corrected);
    }
354977
public function find_right_answer($wrappedquestion) {
        // We only take into account *one* (the first) correct answer.
        while ($answer = array_shift($wrappedquestion->answers)) {
            if (!question_state::graded_state_for_fraction(
                    $answer->fraction)->is_incorrect()) {
                // Store this answer as a choice, only if this is a new one.
                $key = array_search($answer->answer, $this->choices);
                if ($key === false) {
                    $key = $answer->id;
                    $this->choices[$key] = $answer->answer;
                }
                return $key;
            }
        }
        // We should never get there.
        throw new coding_exception('shortanswerquestionwithoutrightanswer', $wrappedquestion->id);

    }
408103
public function validateAfterSchema($answer, array $options = [])
    {
        $errors = [];

        if (empty($options[Validation::QUESTION])) {
            $question = $this->om->getRepository('UJMExoBundle:Item\Item')->findOneBy([
                'uuid' => $answer->questionId,
            ]);

            $options[Validation::QUESTION] = $question->getInteraction();
        }

        if (empty($options[Validation::QUESTION])) {
            $errors[] = [
                'path' => '/questionId',
                'message' => 'question does not exist',
            ];
        } elseif (!empty($answer['data']) && $this->itemDefinitions->isQuestionType($options[Validation::QUESTION]->getQuestion()->getMimeType())) {
            // Forward to item type validator
            $definition = $this->itemDefinitions->get($options[Validation::QUESTION]->getQuestion()->getMimeType());
            $errors = array_merge(
                $errors,
                $definition->validateAnswer($answer['data'], $options[Validation::QUESTION], array_merge($options, [Validation::NO_SCHEMA]))
            );
        }

        return $errors;
    }