/** * 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); $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); }
public static function addAnswer($projectModel, $questionId, $answerModel) { return ActivityCommands::updateAnswer($projectModel, $questionId, $answerModel, 'add'); }
public function testGetActivityForProject_ProjectWithTextQuestionAnswerAndComments_DtoAsExpected() { $e = new MongoTestEnvironment(); $e->clean(); $project = $e->createProject(SF_TESTPROJECT, SF_TESTPROJECTCODE); $text = new TextModel($project); $text->title = "Text 1"; $text->content = "text content"; $textId = $text->write(); $a1 = ActivityCommands::addText($project, $textId, $text); $user1Id = $e->createUser("user1", "user1", "*****@*****.**"); $user2Id = $e->createUser("user2", "user2", "*****@*****.**"); $user3Id = $e->createUser("user3", "user3", "*****@*****.**"); $a2 = ActivityCommands::addUserToProject($project, $user1Id); $a3 = ActivityCommands::addUserToProject($project, $user2Id); $a4 = ActivityCommands::addUserToProject($project, $user3Id); // Workflow is first to create a question $question = new QuestionModel($project); $question->title = "the question"; $question->description = "question description"; $question->textRef->id = $textId; $questionId = $question->write(); $a5 = ActivityCommands::addQuestion($project, $questionId, $question); // Then to add an answer to a question $answer = new AnswerModel(); $answer->content = "first answer"; $answer->score = 10; $answer->userRef->id = $user3Id; $answer->textHightlight = "text highlight"; $answerId = $question->writeAnswer($answer); $a6 = ActivityCommands::addAnswer($project, $questionId, $answer); // Followed by comments $comment1 = new CommentModel(); $comment1->content = "first comment"; $comment1->userRef->id = $user1Id; $comment1Id = QuestionModel::writeComment($project->databaseName(), $questionId, $answerId, $comment1); $a7 = ActivityCommands::addComment($project, $questionId, $answerId, $comment1); $comment2 = new CommentModel(); $comment2->content = "second comment"; $comment2->userRef->id = $user2Id; $comment2Id = QuestionModel::writeComment($project->databaseName(), $questionId, $answerId, $comment2); $a8 = ActivityCommands::addComment($project, $questionId, $answerId, $comment2); // updated answer $question->read($questionId); $answer_updated = $question->readAnswer($answerId); $answer_updated->content = "first answer revised"; $question->writeAnswer($answer_updated); $a9 = ActivityCommands::updateAnswer($project, $questionId, $answer_updated); // updated comment1 $question->read($questionId); $comment1_updated = $question->readComment($answerId, $comment1Id); $comment1_updated->content = "first comment revised"; QuestionModel::writeComment($project->databaseName(), $questionId, $answerId, $comment1_updated); $a10 = ActivityCommands::updateComment($project, $questionId, $answerId, $comment1_updated); $dto = ActivityListDto::getActivityForProject($project); $this->assertEqual($dto[$a1]['action'], 'add_text'); $this->assertEqual($dto[$a1]['projectRef'], array('id' => $project->id->asString(), 'type' => 'sfchecks')); $this->assertEqual($dto[$a1]['content']['project'], $project->projectName); $this->assertEqual($dto[$a1]['textRef'], $textId); $this->assertEqual($dto[$a1]['content']['text'], $text->title); $this->assertEqual($dto[$a2]['action'], 'add_user_to_project'); $this->assertEqual($dto[$a2]['projectRef'], array('id' => $project->id->asString(), 'type' => 'sfchecks')); $this->assertEqual($dto[$a2]['content']['project'], $project->projectName); $this->assertEqual($dto[$a2]['userRef']['id'], $user1Id); $this->assertEqual($dto[$a2]['userRef']['username'], 'user1'); $this->assertEqual($dto[$a2]['userRef']['avatar_ref'], 'user1.png'); $this->assertEqual($dto[$a2]['content']['user'], 'user1'); $this->assertEqual($dto[$a3]['action'], 'add_user_to_project'); $this->assertEqual($dto[$a3]['projectRef'], array('id' => $project->id->asString(), 'type' => 'sfchecks')); $this->assertEqual($dto[$a3]['content']['project'], $project->projectName); $this->assertEqual($dto[$a3]['userRef']['id'], $user2Id); $this->assertEqual($dto[$a3]['userRef']['username'], 'user2'); $this->assertEqual($dto[$a3]['userRef']['avatar_ref'], 'user2.png'); $this->assertEqual($dto[$a3]['content']['user'], 'user2'); $this->assertEqual($dto[$a4]['action'], 'add_user_to_project'); $this->assertEqual($dto[$a4]['projectRef'], array('id' => $project->id->asString(), 'type' => 'sfchecks')); $this->assertEqual($dto[$a4]['content']['project'], $project->projectName); $this->assertEqual($dto[$a4]['userRef']['id'], $user3Id); $this->assertEqual($dto[$a4]['userRef']['username'], 'user3'); $this->assertEqual($dto[$a4]['userRef']['avatar_ref'], 'user3.png'); $this->assertEqual($dto[$a4]['content']['user'], 'user3'); $this->assertEqual($dto[$a5]['action'], 'add_question'); $this->assertEqual($dto[$a5]['projectRef'], array('id' => $project->id->asString(), 'type' => 'sfchecks')); $this->assertEqual($dto[$a5]['content']['project'], $project->projectName); $this->assertEqual($dto[$a5]['textRef'], $textId); $this->assertEqual($dto[$a5]['content']['text'], $text->title); $this->assertEqual($dto[$a5]['questionRef'], $questionId); $this->assertEqual($dto[$a5]['content']['question'], $question->title); $this->assertEqual($dto[$a6]['action'], 'add_answer'); $this->assertEqual($dto[$a6]['projectRef'], array('id' => $project->id->asString(), 'type' => 'sfchecks')); $this->assertEqual($dto[$a6]['content']['project'], $project->projectName); $this->assertEqual($dto[$a6]['textRef'], $textId); $this->assertEqual($dto[$a6]['content']['text'], $text->title); $this->assertEqual($dto[$a6]['questionRef'], $questionId); $this->assertEqual($dto[$a6]['content']['question'], $question->title); $this->assertEqual($dto[$a6]['userRef']['id'], $user3Id); $this->assertEqual($dto[$a6]['userRef']['username'], 'user3'); $this->assertEqual($dto[$a6]['userRef']['avatar_ref'], 'user3.png'); $this->assertEqual($dto[$a6]['content']['answer'], $answer->content); $this->assertEqual($dto[$a6]['content']['user'], 'user3'); $this->assertEqual($dto[$a7]['action'], 'add_comment'); $this->assertEqual($dto[$a7]['projectRef'], array('id' => $project->id->asString(), 'type' => 'sfchecks')); $this->assertEqual($dto[$a7]['content']['project'], $project->projectName); $this->assertEqual($dto[$a7]['textRef'], $textId); $this->assertEqual($dto[$a7]['content']['text'], $text->title); $this->assertEqual($dto[$a7]['questionRef'], $questionId); $this->assertEqual($dto[$a7]['content']['question'], $question->title); $this->assertEqual($dto[$a7]['userRef']['id'], $user1Id); $this->assertEqual($dto[$a7]['userRef']['username'], 'user1'); $this->assertEqual($dto[$a7]['userRef']['avatar_ref'], 'user1.png'); $this->assertEqual($dto[$a7]['content']['user'], 'user1'); $this->assertEqual($dto[$a7]['userRef2']['id'], $user3Id); $this->assertEqual($dto[$a7]['userRef2']['username'], 'user3'); $this->assertEqual($dto[$a7]['userRef2']['avatar_ref'], 'user3.png'); $this->assertEqual($dto[$a7]['content']['user2'], 'user3'); $this->assertEqual($dto[$a7]['content']['answer'], $answer->content); $this->assertEqual($dto[$a7]['content']['comment'], $comment1->content); $this->assertEqual($dto[$a8]['action'], 'add_comment'); $this->assertEqual($dto[$a8]['projectRef'], array('id' => $project->id->asString(), 'type' => 'sfchecks')); $this->assertEqual($dto[$a8]['content']['project'], $project->projectName); $this->assertEqual($dto[$a8]['textRef'], $textId); $this->assertEqual($dto[$a8]['content']['text'], $text->title); $this->assertEqual($dto[$a8]['questionRef'], $questionId); $this->assertEqual($dto[$a8]['content']['question'], $question->title); $this->assertEqual($dto[$a8]['userRef']['id'], $user2Id); $this->assertEqual($dto[$a8]['userRef']['username'], 'user2'); $this->assertEqual($dto[$a8]['userRef']['avatar_ref'], 'user2.png'); $this->assertEqual($dto[$a8]['content']['user'], 'user2'); $this->assertEqual($dto[$a8]['userRef2']['id'], $user3Id); $this->assertEqual($dto[$a8]['userRef2']['username'], 'user3'); $this->assertEqual($dto[$a8]['userRef2']['avatar_ref'], 'user3.png'); $this->assertEqual($dto[$a8]['content']['user2'], 'user3'); $this->assertEqual($dto[$a8]['content']['answer'], $answer->content); $this->assertEqual($dto[$a8]['content']['comment'], $comment2->content); $this->assertEqual($dto[$a9]['action'], 'update_answer'); $this->assertEqual($dto[$a9]['projectRef'], array('id' => $project->id->asString(), 'type' => 'sfchecks')); $this->assertEqual($dto[$a9]['content']['project'], $project->projectName); $this->assertEqual($dto[$a9]['textRef'], $textId); $this->assertEqual($dto[$a9]['content']['text'], $text->title); $this->assertEqual($dto[$a9]['questionRef'], $questionId); $this->assertEqual($dto[$a9]['content']['question'], $question->title); $this->assertEqual($dto[$a9]['userRef']['id'], $user3Id); $this->assertEqual($dto[$a9]['userRef']['username'], 'user3'); $this->assertEqual($dto[$a9]['userRef']['avatar_ref'], 'user3.png'); $this->assertEqual($dto[$a9]['content']['user'], 'user3'); $this->assertEqual($dto[$a9]['content']['answer'], $answer_updated->content); $this->assertEqual($dto[$a10]['action'], 'update_comment'); $this->assertEqual($dto[$a10]['projectRef'], array('id' => $project->id->asString(), 'type' => 'sfchecks')); $this->assertEqual($dto[$a10]['content']['project'], $project->projectName); $this->assertEqual($dto[$a10]['textRef'], $textId); $this->assertEqual($dto[$a10]['content']['text'], $text->title); $this->assertEqual($dto[$a10]['questionRef'], $questionId); $this->assertEqual($dto[$a10]['content']['question'], $question->title); $this->assertEqual($dto[$a10]['userRef']['id'], $user1Id); $this->assertEqual($dto[$a10]['userRef']['username'], 'user1'); $this->assertEqual($dto[$a10]['userRef']['avatar_ref'], 'user1.png'); $this->assertEqual($dto[$a10]['content']['user'], 'user1'); $this->assertEqual($dto[$a10]['userRef2']['id'], $user3Id); $this->assertEqual($dto[$a10]['userRef2']['username'], 'user3'); $this->assertEqual($dto[$a10]['userRef2']['avatar_ref'], 'user3.png'); $this->assertEqual($dto[$a10]['content']['user2'], 'user3'); $this->assertEqual($dto[$a10]['content']['answer'], $answer_updated->content); $this->assertEqual($dto[$a10]['content']['comment'], $comment1_updated->content); }