/** * @param string $projectId * @param JSON $object * @return ID of text updated/added */ public static function updateText($projectId, $object) { $projectModel = new \Api\Model\ProjectModel($projectId); $textModel = new \Api\Model\TextModel($projectModel); $isNewText = $object['id'] == ''; if (!$isNewText) { $textModel->read($object['id']); } JsonDecoder::decode($textModel, $object); TextCommands::makeValidRange($object); if (TextCommands::hasRange($object)) { $usxTrimHelper = new UsxTrimHelper($textModel->content, $object['startCh'] || 0, $object['startVs'] || 0, $object['endCh'] || 0, $object['endVs'] || 0); $textModel->content = $usxTrimHelper->trimUsx(); } $textId = $textModel->write(); if ($isNewText) { ActivityCommands::addText($projectModel, $textId, $textModel); } return $textId; }
public static function addAnswer($projectModel, $questionId, $answerModel) { return ActivityCommands::updateAnswer($projectModel, $questionId, $answerModel, 'add'); }
public function testUnreadActivityModel_unreadItems_itemsAreUnread_listsUnreadItems() { $e = new MongoTestEnvironment(); $e->clean(); $project = $e->createProject("unread_test", "unreadCode"); $userId1 = $e->createUser('user1', 'user1', 'user1'); $userId2 = $e->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->assertEqual(count($unreadItems), 2); $otherUnreadModel->markRead($activityId1); $otherUnreadModel->write(); $unreadModel->read(); $unreadItems = $unreadModel->unreadItems(); $this->assertEqual(count($unreadItems), 1); }
/** * Update the user project role in the project * @param string $projectId * @param string $userId * @param string $projectRole * @return string - userId */ public static function updateUserRole($projectId, $userId, $projectRole = ProjectRoles::CONTRIBUTOR) { CodeGuard::checkNotFalseAndThrow($projectId, '$projectId'); CodeGuard::checkNotFalseAndThrow($userId, 'userId'); //CodeGuard::assertInArrayOrThrow($role, array(ProjectRoles::CONTRIBUTOR, ProjectRoles::MANAGER)); // Add the user to the project $user = new UserModel($userId); $project = ProjectModel::getById($projectId); if ($userId == $project->ownerRef->asString()) { throw new \Exception("Cannot update role for project owner"); } // TODO: Only trigger activity if this is the first time they have been added to project $usersDto = ProjectCommands::usersDto($projectId); if (!$project->users->offsetExists($userId)) { ActivityCommands::addUserToProject($project, $userId); } $project->addUser($userId, $projectRole); $user->addProject($projectId); $project->write(); $user->write(); return $userId; }
/** * Up votes the given answer, if permitted for the given $userId * @param string $userId * @param string $projectId * @param string $questionId * @param string $answerId */ public static function voteUp($userId, $projectId, $questionId, $answerId) { $projectModel = new ProjectModel($projectId); $questionModel = new QuestionModel($projectModel, $questionId); // Check the vote lock. $vote = new UserVoteModel($userId, $projectId, $questionId); if ($vote->hasVote($answerId)) { // Don't throw. There's no harm in this, just don't increment the vote. return self::encodeAnswer($questionModel->readAnswer($answerId)); } // If ok up vote the question and add the lock. $answerModel = $questionModel->readAnswer($answerId); $answerModel->score++; $questionModel->writeAnswer($answerModel); $vote->addVote($answerId); $vote->write(); ActivityCommands::updateScore($projectModel, $questionId, $answerId, $userId); // Return the answer dto. return self::encodeAnswer($answerModel); }
public static function removeEntry($projectId, $entryId, $userId) { $project = new ProjectModel($projectId); $entry = new LexEntryModel($project, $entryId); $entry->isDeleted = true; $entry->write(); ActivityCommands::deleteEntry($project, $userId, $entryId); return true; }
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); }