/** * Encodes a QuestionModel and related data for $questionId * @param string $projectId * @param string $questionId * @param string $userId * @return array - The DTO. */ public static function encode($projectId, $questionId, $userId) { $user = new UserModel($userId); $project = new SfchecksProjectModel($projectId); $question = new QuestionModel($project, $questionId); $textId = $question->textRef->asString(); $text = new TextModel($project, $textId); if (($text->isArchived || $question->isArchived) && $project->users[$userId]->role != ProjectRoles::MANAGER) { throw new ResourceNotAvailableException("This Question is no longer available. If this is incorrect contact your project manager."); } $usxHelper = new UsxHelper($text->content); //echo $usxHelper->toHtml(); //echo $text->content; $votes = new UserVoteModel($userId, $projectId, $questionId); $votesDto = array(); foreach ($votes->votes as $vote) { $votesDto[$vote->answerRef->id] = true; } $unreadAnswerModel = new UnreadAnswerModel($userId, $project->id->asString(), $questionId); $unreadAnswers = $unreadAnswerModel->unreadItems(); $unreadAnswerModel->markAllRead(); $unreadAnswerModel->write(); $unreadCommentModel = new UnreadCommentModel($userId, $project->id->asString(), $questionId); $unreadComments = $unreadCommentModel->unreadItems(); $unreadCommentModel->markAllRead(); $unreadCommentModel->write(); $unreadActivityModel = new UnreadActivityModel($userId, $projectId); $unreadActivity = $unreadActivityModel->unreadItems(); $dto = array(); $dto['question'] = QuestionCommentDtoEncoder::encode($question); $dto['votes'] = $votesDto; $dto['text'] = JsonEncoder::encode($text); $dto['text']['content'] = $usxHelper->toHtml(); $dto['project'] = JsonEncoder::encode($project); $dto['project']['slug'] = $project->databaseName(); $dto['rights'] = RightsHelper::encode($user, $project); $dto['unreadAnswers'] = $unreadAnswers; $dto['unreadComments'] = $unreadComments; $dto['unreadActivityCount'] = count($unreadActivity); return $dto; }
public function testMultipleUnreadModels_multipleUsers_multipleUpdates_multipleVisitsToQuestionPage_usersHaveDifferentUnreadStates() { $e = new MongoTestEnvironment(); $e->clean(); $project = $e->createProject("unread_test", "unreadCode"); $projectId = $project->id->asString(); $userId1 = $e->createUser('user1', 'user1', 'user1'); $user1 = new UserModel($userId1); $user1->addProject($project->id->asString()); $user1->write(); $userId2 = $e->createUser('user2', 'user2', 'user2'); $user2 = new UserModel($userId2); $user2->addProject($project->id->asString()); $user2->write(); $userId3 = $e->createUser('user3', 'user3', 'user3'); $user3 = new UserModel($userId3); $user3->addProject($project->id->asString()); $user3->write(); $answer1 = array('content' => "test answer 1", 'id' => ''); $answer2 = array('content' => "test answer 2", 'id' => ''); $text = new TextModel($project); $text->title = "Text 1"; $usx = MongoTestEnvironment::usxSample(); $text->content = $usx; $textId = $text->write(); $question = new QuestionModel($project); $question->title = "test question"; $question->textRef->id = $textId; $questionId = $question->write(); $answer1Dto = QuestionCommands::updateAnswer($projectId, $questionId, $answer1, $userId1); $answer2Dto = QuestionCommands::updateAnswer($projectId, $questionId, $answer2, $userId2); $answer1 = array_pop($answer1Dto); $answer1Id = $answer1['id']; $answer2 = array_pop($answer2Dto); $answer2Id = $answer2['id']; // the answer author does NOT get their answer marked as unread $unreadModel = new UnreadAnswerModel($userId1, $projectId, $questionId); $this->assertEqual(count($unreadModel->unreadItems()), 1); // the answer author does NOT get their answer marked as unread $unreadModel = new UnreadAnswerModel($userId2, $projectId, $questionId); $this->assertEqual(count($unreadModel->unreadItems()), 1); $unreadModel = new UnreadAnswerModel($userId3, $projectId, $questionId); $this->assertEqual(count($unreadModel->unreadItems()), 2); // user1 visits question page $pageDto = QuestionCommentDto::encode($projectId, $questionId, $userId1); $unreadModel = new UnreadAnswerModel($userId1, $projectId, $questionId); $this->assertEqual(count($unreadModel->unreadItems()), 0); $unreadModel = new UnreadAnswerModel($userId2, $projectId, $questionId); $this->assertEqual(count($unreadModel->unreadItems()), 1); $unreadModel = new UnreadAnswerModel($userId3, $projectId, $questionId); $this->assertEqual(count($unreadModel->unreadItems()), 2); // user2 visits question page $pageDto = QuestionCommentDto::encode($projectId, $questionId, $userId2); $unreadModel = new UnreadAnswerModel($userId1, $projectId, $questionId); $this->assertEqual(count($unreadModel->unreadItems()), 0); $unreadModel = new UnreadAnswerModel($userId2, $projectId, $questionId); $this->assertEqual(count($unreadModel->unreadItems()), 0); $unreadModel = new UnreadAnswerModel($userId3, $projectId, $questionId); $this->assertEqual(count($unreadModel->unreadItems()), 2); // user2 visits question page $pageDto = QuestionCommentDto::encode($projectId, $questionId, $userId3); $unreadModel = new UnreadAnswerModel($userId1, $projectId, $questionId); $this->assertEqual(count($unreadModel->unreadItems()), 0); $unreadModel = new UnreadAnswerModel($userId2, $projectId, $questionId); $this->assertEqual(count($unreadModel->unreadItems()), 0); $unreadModel = new UnreadAnswerModel($userId3, $projectId, $questionId); $this->assertEqual(count($unreadModel->unreadItems()), 0); }