/**
  * Encodes a QuestionModel and related data for $questionId
  * @param string $projectId
  * @param string $questionId
  * @param string $userId
  * @return array - The DTO.
  * @throws ResourceNotAvailableException
  */
 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;
 }
 /**
  *
  * @param ProjectModel $projectModel
  * @param string $questionId
  * @param AnswerModel $answerModel
  * @param string $mode
  * @return string activity id
  */
 public static function updateAnswer($projectModel, $questionId, $answerModel, $mode = "update")
 {
     $activity = new ActivityModel($projectModel);
     $question = new QuestionModel($projectModel, $questionId);
     $text = new TextModel($projectModel, $question->textRef->asString());
     $user = new UserModel($answerModel->userRef->asString());
     $activity->action = $mode == "update" ? ActivityModel::UPDATE_ANSWER : ActivityModel::ADD_ANSWER;
     $activity->userRef->id = $answerModel->userRef->asString();
     $activity->textRef->id = $text->id->asString();
     $activity->questionRef->id = $questionId;
     $activity->addContent(ActivityModel::TEXT, $text->title);
     $activity->addContent(ActivityModel::QUESTION, $question->getTitleForDisplay());
     $activity->addContent(ActivityModel::ANSWER, $answerModel->content);
     $activity->addContent(ActivityModel::USER, $user->username);
     $activityId = $activity->write();
     UnreadActivityModel::markUnreadForProjectMembers($activityId, $projectModel);
     UnreadAnswerModel::markUnreadForProjectMembers($answerModel->id->asString(), $projectModel, $questionId, $answerModel->userRef->asString());
     return $activityId;
 }
 public function testMultipleUnreadModels_multipleUsers_multipleUpdates_multipleVisitsToQuestionPage_usersHaveDifferentUnreadStates()
 {
     $project = self::$environ->createProject("unread_test", "unreadCode");
     $projectId = $project->id->asString();
     $userId1 = self::$environ->createUser('user1', 'user1', 'user1');
     $user1 = new UserModel($userId1);
     $user1->addProject($project->id->asString());
     $user1->write();
     $userId2 = self::$environ->createUser('user2', 'user2', 'user2');
     $user2 = new UserModel($userId2);
     $user2->addProject($project->id->asString());
     $user2->write();
     $userId3 = self::$environ->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();
     QuestionCommands::updateAnswer($projectId, $questionId, $answer1, $userId1);
     QuestionCommands::updateAnswer($projectId, $questionId, $answer2, $userId2);
     // the answer author does NOT get their answer marked as unread
     $unreadModel = new UnreadAnswerModel($userId1, $projectId, $questionId);
     $this->assertCount(1, $unreadModel->unreadItems());
     // the answer author does NOT get their answer marked as unread
     $unreadModel = new UnreadAnswerModel($userId2, $projectId, $questionId);
     $this->assertCount(1, $unreadModel->unreadItems());
     $unreadModel = new UnreadAnswerModel($userId3, $projectId, $questionId);
     $this->assertCount(2, $unreadModel->unreadItems());
     // user1 visits question page
     QuestionCommentDto::encode($projectId, $questionId, $userId1);
     $unreadModel = new UnreadAnswerModel($userId1, $projectId, $questionId);
     $this->assertCount(0, $unreadModel->unreadItems());
     $unreadModel = new UnreadAnswerModel($userId2, $projectId, $questionId);
     $this->assertCount(1, $unreadModel->unreadItems());
     $unreadModel = new UnreadAnswerModel($userId3, $projectId, $questionId);
     $this->assertCount(2, $unreadModel->unreadItems());
     // user2 visits question page
     QuestionCommentDto::encode($projectId, $questionId, $userId2);
     $unreadModel = new UnreadAnswerModel($userId1, $projectId, $questionId);
     $this->assertCount(0, $unreadModel->unreadItems());
     $unreadModel = new UnreadAnswerModel($userId2, $projectId, $questionId);
     $this->assertCount(0, $unreadModel->unreadItems());
     $unreadModel = new UnreadAnswerModel($userId3, $projectId, $questionId);
     $this->assertCount(2, $unreadModel->unreadItems());
     // user2 visits question page
     QuestionCommentDto::encode($projectId, $questionId, $userId3);
     $unreadModel = new UnreadAnswerModel($userId1, $projectId, $questionId);
     $this->assertCount(0, $unreadModel->unreadItems());
     $unreadModel = new UnreadAnswerModel($userId2, $projectId, $questionId);
     $this->assertCount(0, $unreadModel->unreadItems());
     $unreadModel = new UnreadAnswerModel($userId3, $projectId, $questionId);
     $this->assertCount(0, $unreadModel->unreadItems());
 }