public static function getUnreadActivityForUserInProject($userId, $projectId)
 {
     $unreadActivity = new UnreadActivityModel($userId, $projectId);
     $items = $unreadActivity->unreadItems();
     $unreadActivity->markAllRead();
     $unreadActivity->write();
     return $items;
 }
 /**
  * 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 string $projectId
  * @param string $userId
  * @returns array - the DTO array
  * @throws ResourceNotAvailableException
  */
 public static function encode($projectId, $userId)
 {
     $user = new UserModel($userId);
     $project = new SfchecksProjectModel($projectId);
     if ($project->isArchived && $project->users[$userId]->role != ProjectRoles::MANAGER) {
         throw new ResourceNotAvailableException("This Project is no longer available. If this is incorrect contact your project manager.");
     }
     $textList = new TextListModel($project);
     $textList->read();
     $data = array();
     $data['rights'] = RightsHelper::encode($user, $project);
     $data['project'] = array('name' => $project->projectName, 'id' => $projectId);
     if ($project->isArchived) {
         $data['project']['name'] .= " [ARCHIVED]";
     }
     $data['texts'] = array();
     foreach ($textList->entries as $entry) {
         $text = new TextModel($project, $entry['id']);
         if (!$text->isArchived) {
             $questionList = $text->listQuestionsWithAnswers();
             // Just want count of questions and responses, not whole list
             $entry['questionCount'] = 0;
             $responseCount = 0;
             // "Responses" = answers + comments
             foreach ($questionList->entries as $q) {
                 $question = new QuestionModel($project, $q['id']);
                 if (!$question->isArchived) {
                     $entry['questionCount']++;
                     foreach ($q['answers'] as $a) {
                         $commentCount = count($a['comments']);
                         $responseCount += $commentCount + 1;
                         // +1 for this answer
                     }
                 }
             }
             $entry['responseCount'] = $responseCount;
             $entry['dateCreated'] = $text->dateCreated->asDateTimeInterface()->format(\DateTime::RFC2822);
             $data['texts'][] = $entry;
         }
     }
     // sort Texts with newest at the top
     usort($data['texts'], function ($a, $b) {
         $sortOn = 'dateCreated';
         if (array_key_exists($sortOn, $a) && array_key_exists($sortOn, $b)) {
             return strtotime($a[$sortOn]) < strtotime($b[$sortOn]) ? 1 : -1;
         } else {
             return 0;
         }
     });
     // future support for members
     $data['members'] = array();
     // unread activity count
     $unreadActivity = new UnreadActivityModel($userId, $projectId);
     $unreadItems = $unreadActivity->unreadItems();
     $data['activityUnreadCount'] = count($unreadItems);
     // unread broadcast messages
     $unreadMessages = new UnreadMessageModel($userId, $projectId);
     $messageIds = $unreadMessages->unreadItems();
     $messages = array();
     foreach ($messageIds as $messageId) {
         $message = new MessageModel($project, $messageId);
         $messages[] = array('id' => $message->id->asString(), 'subject' => $message->subject, 'content' => $message->content);
     }
     $data['broadcastMessages'] = $messages;
     return $data;
 }
 /**
  *
  * @param ProjectModel $projectModel
  * @param string $questionId
  * @param string $answerId
  * @param string $userId
  * @param string $mode
  * @return string activity id
  */
 public static function updateScore($projectModel, $questionId, $answerId, $userId, $mode = 'increase')
 {
     $question = new QuestionModel($projectModel, $questionId);
     $text = new TextModel($projectModel, $question->textRef->asString());
     $answer = $question->answers[$answerId];
     $user = new UserModel($userId);
     $user2 = new UserModel($answer->userRef->asString());
     $activity = new ActivityModel($projectModel);
     $activity->action = $mode == 'increase' ? ActivityModel::INCREASE_SCORE : ActivityModel::DECREASE_SCORE;
     $activity->userRef->id = $userId;
     $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, $answer->content);
     $activity->addContent(ActivityModel::USER, $user->username);
     $activity->addContent(ActivityModel::USER, $user2->username);
     $activityId = $activity->write();
     UnreadActivityModel::markUnreadForProjectMembers($activityId, $projectModel);
     return $activityId;
 }
 public function testUnreadActivityModel_unreadItems_itemsAreUnread_listsUnreadItems()
 {
     $project = self::$environ->createProject("unread_test", "unreadCode");
     $userId1 = self::$environ->createUser('user1', 'user1', 'user1');
     $userId2 = self::$environ->createUser('user2', 'user2', 'user2');
     $activityId1 = ActivityCommands::addUserToProject($project, $userId1);
     $activityId2 = ActivityCommands::addUserToProject($project, $userId2);
     $unreadModel = new UnreadActivityModel($userId1, $project->id->asString());
     $unreadModel->markUnread($activityId1);
     $unreadModel->markUnread($activityId2);
     $unreadModel->write();
     $otherUnreadModel = new UnreadActivityModel($userId1, $project->id->asString());
     $unreadItems = $otherUnreadModel->unreadItems();
     $this->assertCount(2, $unreadItems);
     $otherUnreadModel->markRead($activityId1);
     $otherUnreadModel->write();
     $unreadModel->read();
     $unreadItems = $unreadModel->unreadItems();
     $this->assertCount(1, $unreadItems);
 }