public function run($mode = 'test')
 {
     $testMode = $mode == 'test';
     $message = "";
     $projectlist = new ProjectListModel();
     $projectlist->read();
     $projectIds = array_map(function ($e) {
         return $e['id'];
     }, $projectlist->entries);
     $projectsChanged = 0;
     foreach ($projectlist->entries as $projectParams) {
         // foreach existing project
         $projectId = $projectParams['id'];
         $project = new ProjectModel($projectId);
         if ($project->siteName == '') {
             $sfproject = new SfchecksProjectModel($projectId);
             $sfproject->themeName = "jamaicanpsalms";
             $message .= "Changed the " . $sfproject->projectName . " to be a sfchecks/scriptureforge project with jamaican psalms theme\n";
             if (!$testMode) {
                 $sfproject->write();
             }
             $projectsChanged++;
         }
     }
     if ($projectsChanged > 0) {
         $message .= "\n\nChanged {$projectsChanged} projects to be of sfchecks type under scriptureforge and jamaicanpsalms theme \n\n";
     } else {
         $message .= "\n\nNo projects were found/changed \n\n";
     }
     return $message;
 }
 public function testHasRight_Ok()
 {
     $userId = MongoTestEnvironment::mockId();
     $project = new SfchecksProjectModel();
     $project->addUser($userId, ProjectRoles::MANAGER);
     $result = $project->hasRight($userId, Domain::QUESTIONS + Operation::CREATE);
     $this->assertTrue($result);
 }
 public function run($mode = 'test')
 {
     $testMode = $mode != 'run';
     $message = "";
     $templateList = new GlobalQuestionTemplateListModel();
     $templateList->read();
     if (count($templateList->entries) > 0) {
     } else {
         $message .= "There are no global templates to migrate\n\n";
     }
     $templatesMoved = 0;
     $projectlist = new ProjectListModel();
     $projectlist->read();
     $project = null;
     foreach ($projectlist->entries as $projectParams) {
         // foreach existing project
         $projectId = $projectParams['id'];
         $p = new SfchecksProjectModel($projectId);
         if ($p->databaseName() == 'sf_jamaican_psalms') {
             $project = $p;
         }
     }
     if (!is_null($project)) {
         $message .= "Migrating global templates to the '" . $project->projectName . "' project\n\n";
         foreach ($templateList->entries as $globalTemplate) {
             $template = new QuestionTemplateModel($project);
             $template->title = $globalTemplate['title'];
             $template->description = $globalTemplate['description'];
             $message .= "Moving '" . $template->title . "'\n";
             if (!$testMode) {
                 $template->write();
                 $gt = new GlobalQuestionTemplateModel($globalTemplate['id']);
                 $gt->remove();
             }
             $templatesMoved++;
         }
         $message .= "\n\nMoved {$templatesMoved} global templates to the specified project\n\n";
     } else {
         $message .= "Error: cound not find the Jamaican Psalms project on this server\n\n";
     }
     return $message;
 }
 public function run($mode = 'test')
 {
     $testMode = $mode != 'run';
     $message = "";
     $projectlist = new ProjectListModel();
     $projectlist->read();
     $contribRoleUpdated = 0;
     $managerRoleUpdated = 0;
     foreach ($projectlist->entries as $projectParams) {
         // foreach existing project
         $projectId = $projectParams['id'];
         $project = new SfchecksProjectModel($projectId);
         $users = $project->users;
         foreach ($users as $userId => $rm) {
             $role = $rm->role;
             if ($role == 'user') {
                 $roleModel = new ProjectRoleModel();
                 $roleModel->role = 'contributor';
                 $project->users[$userId] = $roleModel;
                 $contribRoleUpdated++;
                 $message .= "Updated user role for user {$userId}\n";
             } elseif ($role == 'project_admin') {
                 $roleModel = new ProjectRoleModel();
                 $roleModel->role = 'project_manager';
                 $project->users[$userId] = $roleModel;
                 $message .= "Updated manager role for user {$userId}\n";
                 $managerRoleUpdated++;
             }
         }
         if (!$testMode) {
             $message .= "saving project {$projectId}\n";
             $project->write();
         }
     }
     if ($contribRoleUpdated > 0 || $managerRoleUpdated) {
         $message .= "\n\nChanged {$contribRoleUpdated} user roles to be 'contributor' and {$managerRoleUpdated} project_admin roles to be 'project_manager'\n\n";
     } else {
         $message .= "\n\nNo old roles were found/changed \n\n";
     }
     return $message;
 }
 /**
  * @param string $projectId
  * @param string $userId
  * @returns array - the DTO array
  */
 public static function encode($projectId, $userId)
 {
     $userModel = new UserModel($userId);
     $projectModel = new SfchecksProjectModel($projectId);
     $textList = new TextListModel($projectModel);
     $textList->read();
     $list = $projectModel->listUsers();
     $data = array();
     $data['count'] = count($list->entries);
     $data['entries'] = array_values($list->entries);
     // re-index array
     $data['project'] = ProjectSettingsDtoEncoder::encode($projectModel);
     unset($data['project']['users']);
     $data['archivedTexts'] = array();
     foreach ($textList->entries as $entry) {
         $textModel = new TextModel($projectModel, $entry['id']);
         if ($textModel->isArchived) {
             $questionList = $textModel->listQuestionsWithAnswers();
             // Just want count of questions and responses, not whole list
             $entry['questionCount'] = $questionList->count;
             $responseCount = 0;
             // "Responses" = answers + comments
             foreach ($questionList->entries as $q) {
                 foreach ($q['answers'] as $a) {
                     $commentCount = count($a['comments']);
                     $responseCount += $commentCount + 1;
                     // +1 for this answer
                 }
             }
             $entry['responseCount'] = $responseCount;
             $entry['dateModified'] = $textModel->dateModified->asDateTimeInterface()->format(\DateTime::RFC2822);
             $data['archivedTexts'][] = $entry;
         }
     }
     $data['rights'] = RightsHelper::encode($userModel, $projectModel);
     $data['bcs'] = BreadCrumbHelper::encode('settings', $projectModel, null, null);
     return $data;
 }
 public function testCreateProject_SfChecksProject_IndexesCreated()
 {
     self::$environ->clean();
     $user1Id = self::$environ->createUser("user1name", "User1 Name", "*****@*****.**");
     $user1 = new UserModel($user1Id);
     $projectId = ProjectCommands::createProject(SF_TESTPROJECT, SF_TESTPROJECTCODE, SfProjectModel::SFCHECKS_APP, $user1->id->asString(), self::$environ->website);
     $project = new SfchecksProjectModel($projectId);
     $collectionName = 'activity';
     $databaseName = $project->databaseName();
     $indexCount = iterator_count(MongoStore::getCollectionIndexes($databaseName, $collectionName));
     $this->assertTrue($indexCount >= 1);
     $index = ['key' => ['_id' => 1]];
     $this->assertTrue(MongoStore::isIndexFieldNameInCollection($index, $databaseName, $collectionName));
 }
 public function run($mode = 'test')
 {
     $testMode = $mode != 'run';
     $message = "";
     $projectlist = new ProjectListModel();
     $projectlist->read();
     $textsExamined = 0;
     $textsUpdated = 0;
     // foreach existing project
     foreach ($projectlist->entries as $projectParams) {
         $projectId = $projectParams['id'];
         if ($projectParams['projectName'] == 'Jamaican Psalms') {
             $project = new SfchecksProjectModel($projectId);
             $textlist = new TextListModel($project);
             $textlist->read();
             // foreach text in project
             foreach ($textlist->entries as $textParams) {
                 $textsExamined++;
                 $textId = $textParams['id'];
                 $legacyText = new TextModel_sf_v0_9_18($project, $textId);
                 $fileName = '';
                 if ($legacyText->audioUrl) {
                     $text = new TextModel($project, $textId);
                     if (!$testMode) {
                         if (!$text->audioFileName) {
                             // legacy audioUrl format "assets/<projectId>/<textId>_<fileName>"
                             $fileNamePrefix = $textId . '_';
                             $pos = strpos($legacyText->audioUrl, $fileNamePrefix);
                             $text->audioFileName = substr($legacyText->audioUrl, $pos + strlen($fileNamePrefix));
                         }
                         $text->write();
                     }
                     $message .= "Changed text: {$text->title}\n";
                     $textsUpdated++;
                 }
             }
             if (!$testMode) {
                 TextModel_sf_v0_9_18::removeAudioProperty($project->databaseName());
                 $message .= "Removed 'audioUrl' property from project: {$project->projectName}\n";
             }
         }
     }
     if ($textsUpdated > 0) {
         $message .= "\n\nChanged {$textsUpdated} legacy texts to only store audio filename\n\n";
     } else {
         $message .= "\n\nNo legacy text audio were found/changed. {$textsExamined} texts examined.\n\n";
     }
     // re-arrange assets folder
     // - remove <siteName> from path for LF
     // - add <appName> to path for SF and change <projectId> in path to <projectSlug (databaseName)>
     $message .= "\n\nRe-arrange assets folder\n~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
     $project = new SfchecksProjectModel();
     $assetsFolderPath = APPPATH . "assets";
     $assetsSubfolders = glob($assetsFolderPath . '/*');
     @mkdir($assetsFolderPath . '/lexicon');
     @mkdir($assetsFolderPath . '/sfchecks');
     foreach ($assetsSubfolders as $assetsSubfolder) {
         if (file_exists($assetsSubfolder) && is_dir($assetsSubfolder)) {
             $assetsSubfolderName = basename($assetsSubfolder);
             if (strpos($assetsSubfolderName, 'languageforge') !== false) {
                 $message .= "Move into lexicon: {$assetsSubfolderName}\n";
                 $oldFolderPath = $assetsSubfolder . '/lexicon';
                 $newFolderPath = $assetsFolderPath . '/lexicon';
             } elseif ($assetsSubfolderName == 'lexicon' || $assetsSubfolderName == 'sfchecks') {
                 $message .= "No change: {$assetsSubfolderName}\n";
                 $oldFolderPath = '';
                 $newFolderPath = '';
                 $assetsSubfolder = '';
             } elseif ($project->exists($assetsSubfolderName)) {
                 $message .= "Move into sfchecks: {$assetsSubfolderName}\n";
                 $oldFolderPath = $assetsSubfolder;
                 $project->read($assetsSubfolderName);
                 $projectSlug = $project->databaseName();
                 $newFolderPath = $assetsFolderPath . "/sfchecks/{$projectSlug}";
                 if (!$testMode) {
                 }
             } else {
                 $message .= "Delete: {$assetsSubfolderName}\n";
                 $oldFolderPath = '';
                 $newFolderPath = '';
             }
             if (!$testMode) {
                 if (file_exists($oldFolderPath) && is_dir($oldFolderPath)) {
                     if (!@rename($oldFolderPath, $newFolderPath)) {
                         $oldFiles = glob($oldFolderPath . '/*');
                         foreach ($oldFiles as $oldFile) {
                             $newFile = $newFolderPath . '/' . basename($oldFile);
                             rename($oldFile, $newFile);
                         }
                     }
                 }
                 if (file_exists($assetsSubfolder) && is_dir($assetsSubfolder)) {
                     $this->recursiveRemoveFolder($assetsSubfolder);
                 }
             }
         }
     }
     return $message;
 }