/** * @param string $projectId * @param string $userId * @return array - the DTO array */ public static function encode($projectId, $userId) { $data = array(); $user = new UserProfileModel($userId); $project = new LexiconProjectModel($projectId); $config = JsonEncoder::encode($project->config); $config['inputSystems'] = JsonEncoder::encode($project->inputSystems); $data['config'] = $config; // comment out at the moment until a refactor can be done that is more efficient (language data in the database?) /* $interfaceLanguageCode = $project->interfaceLanguageCode; if ($user->interfaceLanguageCode) { $interfaceLanguageCode = $user->interfaceLanguageCode; } $options = self::getInterfaceLanguages(APPPATH . 'angular-app/languageforge/lexicon/lang'); asort($options); // sort by language name $selectInterfaceLanguages = array( 'optionsOrder' => array_keys($options), 'options' => $options ); $data['interfaceConfig'] = array('userLanguageCode' => $interfaceLanguageCode); $data['interfaceConfig']['selectLanguages'] = $selectInterfaceLanguages; */ // a stand in for the code above $data['interfaceConfig'] = array('userLanguageCode' => 'en', 'selectLanguages' => array('options' => array('en' => 'English'), 'optionsOrder' => array('en'))); $optionlistListModel = new LexOptionListListModel($project); $optionlistListModel->read(); $data['optionlists'] = $optionlistListModel->entries; return $data; }
/** * Updates the given LexEntry in $projectId * @param string $projectId * @param array $params * @param string $userId * @return LexEntryModel */ public static function updateEntry($projectId, $params, $userId) { CodeGuard::checkTypeAndThrow($params, 'array'); $project = new LexiconProjectModel($projectId); if (array_key_exists('id', $params) && $params['id'] != '') { $entry = new LexEntryModel($project, $params['id']); $action = 'update'; } else { $entry = new LexEntryModel($project); $entry->authorInfo->createdByUserRef->id = $userId; $entry->authorInfo->createdDate = new \DateTime(); $action = 'create'; // TODO: Consider adding more specific activity entry: which fields were modified? 2014-09-03 RM // E.g., "User _____ updated entry _____ by adding a new sense with definition ______" } // set authorInfo $entry->authorInfo->modifiedDate = new \DateTime(); $entry->authorInfo->modifiedByUserRef->id = $userId; $params = self::recursiveRemoveEmptyFieldValues($params); //$params = self::recursiveAlignCustomFieldsWithModel($params); JsonDecoder::decode($entry, $params); $entry->write(); ActivityCommands::writeEntry($project, $userId, $entry, $action); return JsonEncoder::encode($entry); }
/** * @param string $projectId * @param string $textId * @param string $userId * @returns array - the DTO array */ public static function encode($projectId, $textId, $userId) { $user = new UserModel($userId); $project = new SfchecksProjectModel($projectId); $text = new TextModel($project, $textId); $questionList = new QuestionAnswersListModel($project, $textId); $questionList->read(); $data = array(); $data['text'] = JsonEncoder::encode($text); $data['archivedQuestions'] = array(); foreach ($questionList->entries as $questionData) { $question = new QuestionModel($project, $questionData['id']); if ($question->isArchived) { // Just want answer count, not whole list $questionData['answerCount'] = count($questionData['answers']); $responseCount = 0; // "Reponses" = answers + comments foreach ($questionData['answers'] as $a) { $commentCount = count($a['comments']); $responseCount += $commentCount + 1; // +1 for this answer } $questionData['responseCount'] = $responseCount; unset($questionData['answers']); $questionData['dateModified'] = $question->dateModified->format(\DateTime::RFC2822); $data['archivedQuestions'][] = $questionData; } } $data['rights'] = RightsHelper::encode($user, $project); $data['bcs'] = BreadCrumbHelper::encode('settings', $project, $text, null); return $data; }
public function testEncodeDecode_Same() { $model = new TestJsonDateModel(); $encoded = JsonEncoder::encode($model); $this->assertIsA($encoded['date'], 'string'); // var_dump($encoded); $otherModel = new TestJsonDateModel(); JsonDecoder::decode($otherModel, $encoded); $iso8601 = $otherModel->date->format(DateTime::ISO8601); $this->assertEqual($encoded['date'], $iso8601); // var_dump($iso8601); }
public function testLexEntryCrud_CreateUpdateDeleteListOk() { $e = new LexiconMongoTestEnvironment(); $e->clean(); $project = $e->createProject(SF_TESTPROJECT, SF_TESTPROJECTCODE); $projectId = $project->id->asString(); // create an user and add to the project $userId = $e->getProjectMember($projectId, 'user1'); $entry = new LexEntryModel($project); $entry->lexeme->form('th', 'SomeEntry'); $sense = new Sense(); $sense->definition->form('en', 'red fruit'); $sense->partOfSpeech->value = 'noun'; $example = new Example(); $example->sentence->form('th', 'example1'); $example->translation->form('en', 'trans1'); $sense->examples[] = $example; $entry->senses[] = $sense; // List $dto = LexEntryCommands::listEntries($projectId); $this->assertEqual($dto->count, 0); // Create $params = JsonEncoder::encode($entry); $result1 = LexEntryCommands::updateEntry($projectId, $params, $userId); $entryId = $result1['id']; $this->assertNotNull($entryId); $this->assertEqual(24, strlen($entryId)); // Read $result2 = LexEntryCommands::readEntry($projectId, $entryId); $this->assertNotNull($result2['id']); $this->assertEqual('SomeEntry', $result2['lexeme']['th']['value']); // Update $result2['lexeme']['th']['value'] = 'OtherEntry'; $result3 = LexEntryCommands::updateEntry($projectId, $result2, $userId); $this->assertNotNull($result3); $this->assertEqual($result3['id'], $entryId); // Read back $result4 = LexEntryCommands::readEntry($projectId, $entryId); $this->assertNotNull($result4['id']); $this->assertEqual('OtherEntry', $result4['lexeme']['th']['value']); // List $dto = LexEntryCommands::listEntries($projectId); $this->assertEqual($dto->count, 1); // Delete $result5 = LexEntryCommands::removeEntry($projectId, $entryId, $userId); $this->assertTrue($result5); // List to confirm delete $dto = LexEntryCommands::listEntries($projectId); $this->assertEqual($dto->count, 0); // Clean up after ourselves ProjectCommands::deleteProjects(array($projectId)); }
/** * * @param string $projectId * @param string $textId * @param string $userId * @return array - the DTO array */ public static function encode($projectId, $textId, $userId) { $project = new SfchecksProjectModel($projectId); $text = new TextModel($project, $textId); $user = new UserModel($userId); if (($project->isArchived || $text->isArchived) && $project->users[$userId]->role != ProjectRoles::MANAGER) { throw new ResourceNotAvailableException("This Text is no longer available. If this is incorrect contact your project manager."); } $questionList = new QuestionAnswersListModel($project, $textId); $questionList->read(); $data = array(); $data['rights'] = RightsHelper::encode($user, $project); $data['entries'] = array(); $data['project'] = array('id' => $projectId, 'name' => $project->projectName, 'slug' => $project->databaseName(), 'allowAudioDownload' => $project->allowAudioDownload); $data['text'] = JsonEncoder::encode($text); $usxHelper = new UsxHelper($text->content); $data['text']['content'] = $usxHelper->toHtml(); foreach ($questionList->entries as $questionData) { $question = new QuestionModel($project, $questionData['id']); if (!$question->isArchived) { // Just want answer count, not whole list $questionData['answerCount'] = count($questionData['answers']); $responseCount = 0; // "Reponses" = answers + comments foreach ($questionData['answers'] as $a) { $commentCount = count($a['comments']); $responseCount += $commentCount + 1; // +1 for this answer } $questionData['responseCount'] = $responseCount; unset($questionData['answers']); $questionData['dateCreated'] = $question->dateCreated->format(\DateTime::RFC2822); $data['entries'][] = $questionData; } } // sort Questions with newest at the top usort($data['entries'], 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; } }); $data['count'] = count($data['entries']); return $data; }
public function testDecode_readOnlyProperties_propertiesNotChanged() { $test = new PropertiesTest(); $test->name = 'test'; $test->shouldBeReadOnly = 'cannot change this'; $data = json_decode(json_encode(JsonEncoder::encode($test)), true); $this->assertTrue(array_key_exists('name', $data)); $this->assertTrue(array_key_exists('shouldBeReadOnly', $data)); $this->assertEqual($test->shouldBeReadOnly, $data['shouldBeReadOnly']); // change some data $data['name'] = 'different'; // this can be changed $data['shouldBeReadOnly'] = 'changed'; // this prop is read-only $test2 = new PropertiesTest(); JsonDecoder::decode($test2, $data); $this->assertEqual($test2->name, 'different'); $this->assertEqual($test2->shouldBeReadOnly, 'default'); }
/** * 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 testSemdomItemCommand_UpdateSemDomItem_AddItemUpdateItem() { $e = new SemDomMongoTestEnvironment(); $lang = 'en2'; $languageName = "English"; $e->cleanPreviousProject($lang); $e->getEnglishProjectAndCreateIfNecessary(); $user1Id = $e->createUser('u', 'u', 'u'); $targetProject = $e->createSemDomProject($lang, $languageName, $user1Id); // insert dummy models $targetItemModel = new SemDomTransItemModel($targetProject); $targetItemModel->xmlGuid = "asdf123"; $targetItemModel->key = "1"; $targetItemModel->name = new SemDomTransTranslatedForm("universe"); $targetItemModel->description = new SemDomTransTranslatedForm("Universe description"); $sq = new SemDomTransQuestion("A universe question", "A universe question term"); $targetItemModel->questions[] = $sq; $data = JsonEncoder::encode($targetItemModel); $itemId = SemDomTransItemCommands::update($data, $targetProject->id->asString()); $readItem = new SemDomTransItemModel($targetProject, $itemId); $this->assertNotEqual($readItem->key, null); $this->assertEqual($readItem->key, "1"); $this->assertNotEqual($readItem->name, null); $this->assertEqual($readItem->name->translation, "universe"); $this->assertEqual($readItem->name->status, SemDomTransStatus::Draft); $this->assertNotEqual($readItem->description->translation, null); $this->assertEqual($readItem->description->translation, "Universe description"); $this->assertEqual($readItem->description->status, SemDomTransStatus::Draft); $readItem->name = new SemDomTransTranslatedForm("universe-edited"); $data = JsonEncoder::encode($readItem); SemDomTransItemCommands::update($data, $targetProject->id->asString()); $readItemAgain = new SemDomTransItemModel($targetProject, $itemId); $this->assertNotEqual($readItemAgain->key, null); $this->assertEqual($readItemAgain->key, "1"); $this->assertNotEqual($readItemAgain->name, null); $this->assertEqual($readItemAgain->name->translation, "universe-edited"); $this->assertEqual($readItemAgain->name->status, SemDomTransStatus::Draft); $this->assertNotEqual($readItemAgain->description->translation, null); $this->assertEqual($readItemAgain->description->translation, "Universe description"); $this->assertEqual($readItemAgain->description->status, SemDomTransStatus::Draft); }
public static function readTemplate($projectId, $id) { $projectModel = new ProjectModel($projectId); $questionTemplate = new QuestionTemplateModel($projectModel, $id); return JsonEncoder::encode($questionTemplate); }
/** * * @param string $projectId * @param string $textId */ public static function readText($projectId, $textId) { $projectModel = new \Api\Model\ProjectModel($projectId); $textModel = new \Api\Model\TextModel($projectModel, $textId); return JsonEncoder::encode($textModel); }
public static function readQuestion($projectId, $questionId) { $projectModel = new \Api\Model\ProjectModel($projectId); $questionModel = new \Api\Model\QuestionModel($projectModel, $questionId); return JsonEncoder::encode($questionModel); }
/** * * @param string $validationKey * @return array */ public static function readForRegistration($validationKey) { $user = new \Api\Model\UserModelBase(); if (!$user->readByProperty('validationKey', $validationKey)) { return array(); } if (!$user->validate(false)) { throw new \Exception("Sorry, your registration link has expired."); } return JsonEncoder::encode($user); }
public function lex_upload_importLift($mediaType, $tmpFilePath) { $response = LexUploadCommands::importLiftFile($this->_projectId, $mediaType, $tmpFilePath); return JsonEncoder::encode($response); }
/** * @param string $id * @param string $authUserId - the admin user's id performing the update (for auth purposes) */ public static function readProject($id) { $project = new LexiconProjectModel($id); return JsonEncoder::encode($project); }
public static function readProjectSettings($projectId) { $project = new ProjectSettingsModel($projectId); return array('sms' => JsonEncoder::encode($project->smsSettings), 'email' => JsonEncoder::encode($project->emailSettings)); }
public function encode() { return JsonEncoder::encode($this); }