/** * Creates or updates an answer for the given $questionId. * @param string $projectId * @param string $questionId * @param array $answerJson is decoded into an AnswerModel * @param string $userId * @return array Returns an encoded QuestionDTO fragment for the Answer * @see AnswerModel */ public static function updateAnswer($projectId, $questionId, $answerJson, $userId) { CodeGuard::assertKeyExistsOrThrow('id', $answerJson, "answerJson"); CodeGuard::checkNotFalseAndThrow($answerJson['content'], "answerJson['content']"); $project = new ProjectModel($projectId); ProjectCommands::checkIfArchivedAndThrow($project); $question = new QuestionModel($project, $questionId); // whitelist updatable items if ($answerJson['id'] != '') { // update existing answer $answer = $question->readAnswer($answerJson['id']); $answer->content = $answerJson['content']; } else { // create new answer $answer = new AnswerModel(); JsonDecoder::decode($answer, array('id' => '', 'content' => $answerJson['content'])); $answer->userRef->id = $userId; } if (array_key_exists('textHighlight', $answerJson)) { $answer->textHighlight = $answerJson['textHighlight']; } $answerId = $question->writeAnswer($answer); // Re-read question model to pick up new answer $question->read($questionId); $newAnswer = $question->readAnswer($answerId); if ($answerJson['id'] != '') { // TODO log the activity after we confirm that the comment was successfully updated ; cjh 2013-08 ActivityCommands::updateAnswer($project, $questionId, $newAnswer); } else { ActivityCommands::addAnswer($project, $questionId, $newAnswer); } return self::encodeAnswer($newAnswer); }