Converts raw data into a Answer entity.

@param array  $data
@param Answer $answer
@param array  $options

@return Answer
174843
public function deserialize($data, Answer $answer = null, array $options = [])
    {
        $answer = $answer ?: new Answer();

        $this->sipe('id', 'setUuid', $data, $answer);
        $this->sipe('questionId', 'setQuestionId', $data, $answer);
        $this->sipe('tries', 'setTries', $data, $answer);
        $this->sipe('score', 'setScore', $data, $answer);
        $this->sipe('feedback', 'setFeedback', $data, $answer);

        if (isset($data['usedHints'])) {
            foreach ($data['usedHints'] as $usedHint) {
                $answer->addUsedHint($usedHint['id']);
            }
        }
        if (!empty($data['data'])) {
            $answer->setData(json_encode($data['data']));
        }

        return $answer;
    }
500391
public function process($field, EntityInterface $data, array $options = [])
    {
        $this->setContext($data);

        $value = $this->fieldValue($data, $field);
        $options += ['formatter' => null];

        if ($options['formatter'] === 'element') {
            $context = $this->getContext();

            return $this->_View->element($options['element'], compact('context', 'field', 'value', 'options'));
        }

        if ($options['formatter'] === 'relation') {
            $relation = $this->relation($field);
            if ($relation) {
                return $relation['output'];
            }
        }

        if (is_callable($options['formatter'])) {
            return $options['formatter']($field, $value, $this->getContext(), $options, $this->getView());
        }

        $value = $this->introspect($field, $value, $options);

        return $value;
    }
1040796
public function deserialize($data, ChoiceQuestion $choiceQuestion = null, array $options = [])
    {
        if (empty($choiceQuestion)) {
            $choiceQuestion = new ChoiceQuestion();
        }

        $this->sipe('multiple', 'setMultiple', $data, $choiceQuestion);
        $this->sipe('random', 'setShuffle', $data, $choiceQuestion);
        $this->sipe('numbering', 'setNumbering', $data, $choiceQuestion);

        $this->deserializeChoices($choiceQuestion, $data['choices'], $data['solutions'], $options);

        return $choiceQuestion;
    }
740140
protected function parseData($data, $options)
    {
        if (empty($data)) {
            return [];
        }

        // 获取绑定信息
        $bind = $this->query->getFieldsBind($options['table']);
        if ('*' == $options['field']) {
            $fields = array_keys($bind);
        } else {
            $fields = $options['field'];
        }

        $result = [];
        foreach ($data as $key => $val) {
            $item = $this->parseKey($key, $options);
            if (is_object($val) && method_exists($val, '__toString')) {
                // 对象数据写入
                $val = $val->__toString();
            }
            if (false === strpos($key, '.') && !in_array($key, $fields, true)) {
                if ($options['strict']) {
                    throw new Exception('fields not exists:[' . $key . ']');
                }
            } elseif (is_null($val)) {
                $result[$item] = 'NULL';
            } elseif (isset($val[0]) && 'exp' == $val[0]) {
                $result[$item] = $val[1];
            } elseif (is_scalar($val)) {
                // 过滤非标量数据
                if (0 === strpos($val, ':') && $this->query->isBind(substr($val, 1))) {
                    $result[$item] = $val;
                } else {
                    $key = str_replace('.', '_', $key);
                    $this->query->bind('__data__' . $key, $val, isset($bind[$key]) ? $bind[$key] : PDO::PARAM_STR);
                    $result[$item] = ':__data__' . $key;
                }
            }
        }
        return $result;
    }
1249843
public function provide($data = null, array $options = [])
    {
        $template = $this->getBasicTemplate('text');
        $defaultOptions = $this->getDefaultOptions($data, $options);
        $defaultOptions['input'] = ['content' => $template];

        $result[$this->config->getField()] = $defaultOptions;

        return $result;
    }
1127660
private function serializeObjects(Item $question, array $options = [])
    {
        return array_values(array_map(function (ItemObject $object) use ($options) {
            return $this->itemObjectSerializer->serialize($object, $options);
        }, $question->getObjects()->toArray()));
    }
821282
public function process_question(array $data, array $raw) {

        // Convert and write the answers first.
        if (isset($data['answers'])) {
            $this->write_answers($data['answers'], $this->pluginname);
        }

        // Convert and write the shortanswer extra fields.
        foreach ($data['shortanswer'] as $shortanswer) {
            $shortanswer['id'] = $this->converter->get_nextid();
            $this->write_xml('shortanswer', $shortanswer, array('/shortanswer/id'));
        }
    }
787340
public function insert(array $data, $options = [])
    {
        // 分析并处理数据
        $data = $this->parseData($data, $options);
        $bulk = new BulkWrite;
        if ($insertId = $bulk->insert($data)) {
            $this->insertId = $insertId;
        }
        $this->log('insert', $data, $options);
        return $bulk;
    }
249733
public function deserialize(array $data, Options $options = null)
    {
        if (empty($options)) {
            $options = $this->optionsRepo->findOneBy(['id' => $data['id']]);
        }
        if (empty($options)) {
            $options = new Options();
        }
        if (isset($data['user'])) {
            $user = isset($data['user']['id']) ? $this->userRepo->findOneBy(['uuid' => $data['user']['id']]) : null;
            $options->setUser($user);
        }
        if (isset($data['data'])) {
            $options->setOptions($data['data']);
        }

        return $options;
    }
77795
public function serialize(OpenQuestion $wordsQuestion, array $options = [])
    {
        $serialized = [];

        if (in_array(Transfer::INCLUDE_SOLUTIONS, $options)) {
            $serialized['solutions'] = $this->serializeSolutions($wordsQuestion, $options);
        }

        return $serialized;
    }
913505
public function validateAnswer($answer, AbstractItem $question, array $options = [])
    {
        $options[Validation::QUESTION] = $question;

        return $this->getAnswerValidator()->validate($answer, $options);
    }
1234478
public function provide($data = null, array $options = [])
    {
        $result = parent::provide($data, $options);
        $errorResponse = sprintf(parent::VALUE_NOT_FOUND_HTML, $data);

        //In case the result is the same with the errorResponse then return the error
        if (empty($result) || $errorResponse == $result) {
            return $result;
        } else {
            return static::getIcon($data, $result);
        }
    }
1009506
protected function parsePkWhere($data, &$options)
    {
        $pk = $this->getPk($options);
        // 获取当前数据表
        $table = is_array($options['table']) ? key($options['table']) : $options['table'];
        if (!empty($options['alias'][$table])) {
            $alias = $options['alias'][$table];
        }
        if (is_string($pk)) {
            $key = isset($alias) ? $alias . '.' . $pk : $pk;
            // 根据主键查询
            if (is_array($data)) {
                $where[$key] = isset($data[$pk]) ? $data[$pk] : ['in', $data];
            } else {
                $where[$key] = strpos($data, ',') ? ['IN', $data] : $data;
            }
        } elseif (is_array($pk) && is_array($data) && !empty($data)) {
            // 根据复合主键查询
            foreach ($pk as $key) {
                if (isset($data[$key])) {
                    $attr         = isset($alias) ? $alias . '.' . $key : $key;
                    $where[$attr] = $data[$key];
                } else {
                    throw new Exception('miss complex primary data');
                }
            }
        }

        if (!empty($where)) {
            if (isset($options['where']['AND'])) {
                $options['where']['AND'] = array_merge($options['where']['AND'], $where);
            } else {
                $options['where']['AND'] = $where;
            }
        }
        return;
    }
1289174
public function validateAfterSchema($answerData, array $options = [])
    {
        /** @var MatchQuestion $question */
        $question = !empty($options[Validation::QUESTION]) ? $options[Validation::QUESTION] : null;
        if (empty($question)) {
            throw new \LogicException('Answer validation : Cannot perform additional validation without question.');
        }

        $errors = [];

        $proposalIds = array_map(function (Proposal $proposal) {
            return $proposal->getUuid();
        }, $question->getProposals()->toArray());

        $labelIds = array_map(function (Label $label) {
            return $label->getUuid();
        }, $question->getLabels()->toArray());

        foreach ($answerData as $answer) {
            if (!in_array($answer['itemId'], $proposalIds)) {
                $errors[] = [
                    'path' => '/itemId',
                    'message' => 'Answer `itemId` must reference an item from `items`',
                ];
            }

            if (!in_array($answer['setId'], $labelIds)) {
                $errors[] = [
                    'path' => '/setId',
                    'message' => 'Answer `setId` must reference an item from `sets`',
                ];
            }
        }

        return $errors;
    }
478975
private function deserializeCells(GridQuestion $gridQuestion, array $cells, array $solutions, array $options = [])
    {
        $cellEntities = $gridQuestion->getCells()->toArray();

        foreach ($cells as $cellData) {
            $cell = null;

            // Searches for an existing cell entity.
            foreach ($cellEntities as $entityIndex => $entityCell) {
                /* @var Cell $entityCell */
                if ($entityCell->getUuid() === $cellData['id']) {
                    $cell = $entityCell;
                    unset($cellEntities[$entityIndex]);
                    break;
                }
            }

            $cell = $cell ?: new Cell();
            $cell->setUuid($cellData['id']);
            $cell->setCoordsX($cellData['coordinates'][0]);
            $cell->setCoordsY($cellData['coordinates'][1]);
            $cell->setColor($cellData['color']);
            $cell->setBackground($cellData['background']);

            if (!empty($cellData['data'])) {
                $cell->setData($cellData['data']);
            }
            if (!empty($cellData['choices'])) {
                $cell->setSelector(true);
            } else {
                $cell->setSelector(false);
            }
            $hasSolution = false;

            foreach ($solutions as $solution) {
                if ($solution['cellId'] === $cellData['id']) {
                    $this->deserializeCellChoices($cell, $solution['answers'], $options);
                    $hasSolution = true;
                    break;
                }
            }
            if (!$hasSolution) {
                $this->deserializeCellChoices($cell, [], $options);
            }
            $cell->setInput($hasSolution);

            $gridQuestion->addCell($cell);
        }

        // Remaining cells are no longer in the Question
        foreach ($cellEntities as $cellToRemove) {
            $gridQuestion->removeCell($cellToRemove);
        }
    }
838177
public function deserialize($data, $object, $options = [])
    {
        // search for the correct serializer
        if ($meta = $this->om->getClassMetaData(get_class($object))) {
            $class = $meta->name;
        }

        $serializer = $this->get($class);

        if (method_exists($serializer, 'deserialize')) {
            $serializer->deserialize($data, $object, $options);
        }

        return $object;
    }
1142675
public function process_question(array $data, array $raw) {

        // Convert and write the answers first.
        if (isset($data['answers'])) {
            $this->write_answers($data['answers'], $this->pluginname);
        }

        // Convert and write the numerical units and numerical options.
        if (isset($data['calculated'][0]['numerical_units'])) {
            $numericalunits = $data['calculated'][0]['numerical_units'];
        } else {
            $numericalunits = array();
        }
        $numericaloptions = $this->get_default_numerical_options(
                $data['oldquestiontextformat'], $numericalunits);

        $this->write_numerical_units($numericalunits);
        $this->write_numerical_options($numericaloptions);

        // Write dataset_definitions.
        if (isset($data['calculated'][0]['dataset_definitions']['dataset_definition'])) {
            $datasetdefinitions = $data['calculated'][0]['dataset_definitions']['dataset_definition'];
        } else {
            $datasetdefinitions = array();
        }
        $this->write_dataset_definitions($datasetdefinitions);

        // Write calculated_records.
        $this->xmlwriter->begin_tag('calculated_records');
        foreach ($data['calculated'] as $calculatedrecord) {
            $record = array(
                'id'                  => $this->converter->get_nextid(),
                'answer'              => $calculatedrecord['answer'],
                'tolerance'           => $calculatedrecord['tolerance'],
                'tolerancetype'       => $calculatedrecord['tolerancetype'],
                'correctanswerlength' => $calculatedrecord['correctanswerlength'],
                'correctanswerformat' => $calculatedrecord['correctanswerformat']
            );
            $this->write_xml('calculated_record', $record, array('/calculated_record/id'));
        }
        $this->xmlwriter->end_tag('calculated_records');

        // Write calculated_options.
        $options = array(
            'calculate_option' => array(
                'id'                             => $this->converter->get_nextid(),
                'synchronize'                    => 0,
                'single'                         => 0,
                'shuffleanswers'                 => 0,
                'correctfeedback'                => null,
                'correctfeedbackformat'          => FORMAT_HTML,
                'partiallycorrectfeedback'       => null,
                'partiallycorrectfeedbackformat' => FORMAT_HTML,
                'incorrectfeedback'              => null,
                'incorrectfeedbackformat'        => FORMAT_HTML,
                'answernumbering'                => 'abc'
            )
        );
        $this->write_xml('calculated_options', $options, array('/calculated_options/calculate_option/id'));
    }