protected function execute(InputInterface $input, OutputInterface $output) { $isVerbose = $output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE; $sourceProjectCode = $input->getOption('source-project-code'); $targetProjectCode = $input->getOption('target-project-code'); $targetSurveyCode = $input->getOption('target-survey-code'); $sourceProjectResource = new ProjectResource($this->sourceClient); $projectResource = new ProjectResource($this->client); $sourceProject = $sourceProjectResource->getByCode($sourceProjectCode); $targetProject = $projectResource->getByCode($targetProjectCode); if (!$sourceProject) { $output->writeln("<error>Project '{$sourceProjectCode}' not found</error>"); exit; } if (!$targetProject) { $output->writeln("<error>Project '{$targetProjectCode}' not found</error>"); exit; } $sourceSurveyId = $input->getOption('source-survey-id'); /** @type SurveyResource $fromSurveyResource */ $fromSurveyResource = new SurveyResource($this->sourceClient); $toSurveyResource = new SurveyResource($this->client); $survey = $fromSurveyResource->getById($sourceSurveyId); $surveyJson = $fromSurveyResource->getExportJson($survey['id']); if ($targetSurveyCode) { $surveyJson['code'] = $targetSurveyCode; } $result = $toSurveyResource->importSurvey(['import_data' => $surveyJson, 'name' => $survey['name'], 'code' => $survey['code'], 'project_id' => $targetProject['id'], 'category_code' => $survey['category_code'], 'description' => $survey['description']]); $output->writeln("Survey {$result['code']} #{$result['id']} transferred"); }
protected function execute(InputInterface $input, OutputInterface $output) { $isVerbose = $output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE; $sourceProjectCode = $input->getOption('source-project-code'); $targetProjectCode = $input->getOption('target-project-code'); $sourceProjectResource = new ProjectResource($this->sourceClient); $projectResource = new ProjectResource($this->client); $sourceProject = $sourceProjectResource->getByCode($sourceProjectCode); $targetProject = $projectResource->getByCode($targetProjectCode); if (!$sourceProject) { $output->writeln("<error>Project '{$sourceProjectCode}' not found</error>"); exit; } if (!$targetProject) { $output->writeln("<error>Project '{$targetProjectCode}' not found</error>"); exit; } if (!$targetProjectCode) { $targetProjectCode = $sourceProjectCode; } $sourceWaveId = $input->getOption('source-wave-id'); $targetJobCode = $input->getOption('target-job-code'); $jobResource = new JobResource($this->client); /** @type WaveResource $fromWaveResource */ $fromWaveResource = new WaveResource($this->sourceClient); /** @type WaveResource $toWaveResource */ $toWaveResource = new WaveResource($this->client); try { $wave = $fromWaveResource->getById($sourceWaveId); } catch (\Exception $e) { if (!isset($wave)) { $output->writeln("<error>Wave #{$sourceWaveId} not found</error>"); exit; } } // if no target job code set - use current job if (!$targetJobCode) { $targetJobCode = $wave['job_code']; } $job = $jobResource->getOneBy(['code' => $targetJobCode], ['project_code' => $targetProject['code']]); if (!$job) { throw new \Exception("job '{$targetJobCode}' not found in project '{$targetProject['code']}'"); } // update target job ID // find job ID, then update wave data $this->processWaveFields($wave, $job); $response = $toWaveResource->save($wave); $output->writeln("Wave {$response['code']} #{$response['id']} transferred"); }
protected function execute(InputInterface $input, OutputInterface $output) { $name = $input->getArgument('filename'); $projectResource = new ProjectResource($this->client); $userResource = new UserResource($this->client); $memberResource = new MemberResource($this->client); $reader = new \EasyCSV\Reader('new_members.csv'); while ($row = $reader->getRow()) { $project = $projectResource->getByCode($row['project_code']); // we ned that user for admins, maybe it should be separate field $user = $userResource->getOneBy(['username' => $row['username']]); if (!$user) { print "user '{$row['username']}' not found\n"; } try { $res = $memberResource->save(['code' => $row['code'], 'project_id' => $project['id'], 'user_id' => $user['id'], 'permission_type_code' => $row['permission_type_code']]); } catch (Exception $e) { print "Error importing member {$row['code']}:" . $e->getMessage() . "\n"; } } $output->writeln($text); }
protected function execute(InputInterface $input, OutputInterface $output) { // $output->writeln("<error>Command not implemented yet</error>"); // die(); $isVerbose = $output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE; $targetProject = $input->getOption('target-project-code'); $waveId = $input->getOption('import-wave-id'); $projectResource = new ProjectResource($this->client); $project = $projectResource->getByCode($targetProject); $sourceCsv = $input->getOption('source-csv-file'); $csvData = str_getcsv(file_get_contents($sourceCsv), "\n"); //parse the rows $columns = str_getcsv(array_shift($csvData), ','); $i = 0; foreach ($csvData as &$row) { $dataResource = new DataResource($this->client); $dataArray = str_getcsv($row, ","); $dataArray = array_combine($columns, $dataArray); $dataTypeResource = new DataTypeResource($this->client); $dataType = $dataTypeResource->getByCode($dataArray['DataTypeCode']); $data = ['code' => $dataArray['Code'], 'name' => $dataArray['Name'], 'project' => $project['@id'], 'dataTypeCode' => $dataType['@id'], 'isActive' => $dataArray['IsActive'], 'createdAt' => $dataArray['CreatedAt']]; if ($dataArray['DataTypeCode'] == 'place') { $data = array_merge($data, ['latitude' => $dataArray['Latitude'], 'longitude' => $dataArray['Longitude'], 'zip' => $dataArray['Zip'], 'address' => $dataArray['Address']]); } elseif ($dataArray['DataTypeCode'] == 'image') { $data = array_merge($data, ['questionCode' => $dataArray['QuestionCode'], 'height' => intval($dataArray['Height']), 'width' => intval($dataArray['Width']), 'orientation' => intval($dataArray['Orientation']), 'tag' => $dataArray['Tag']]); } try { $dataResource->addForImportWave($waveId, $data); $i++; } catch (\Exception $e) { $output->writeln('<err>' . $e->getMessage() . '</err>'); } if ($i > 10) { break; } } }
protected function execute(InputInterface $input, OutputInterface $output) { $filename = $input->getArgument('filename'); $serverCode = $input->getOption('server-code'); $timezoneId = $input->getOption('timezone-id'); $projectCode = $input->getOption('project-code'); $userResource = new UserResource($this->client); /** @type MemberResource $memberResource */ $memberResource = new MemberResource($this->client); $projectResource = new ProjectResource($this->client); /** @type ProjectResource $resource */ $resource = new ProjectResource($this->client); $reader = new \EasyCSV\Reader($filename, 'r', true); while ($row = $reader->getRow()) { //code,name,description,timezone_id $code = $row['code']; if ($projectCode && $code != $projectCode) { continue; } $params = []; if ($project = $resource->getOneBy(['code' => $code])) { $params['id'] = $project['id']; } try { /* $res = $resource->save( array_merge( $params, [ 'title' => $name = $row['name'], 'code' => $code, 'timezone_id' => $timezoneId, 'description' => $name." Project", 'background_server_code' => $serverCode, ] ) ); */ } catch (\Exception $e) { printf("Error saving project: %s\n", $e->getMessage()); printf("Project {$code} already exists\n"); } $project = $projectResource->getByCode($code); $res = $resource->addModule($code, 'turk'); $staffAdmins = ['tac', 'ho449']; try { foreach (array_merge([$code], $staffAdmins) as $idx => $username) { $user = $userResource->getOneBy(['username' => $username]); if (!$user) { print "user '{$username}' not found\n"; continue; } $params = ['code' => $username, 'project_id' => $project['id'], 'user_id' => $user['id'], 'permission_type_code' => 'owner', 'enrollment_status_code' => 'notenrolled']; $member = $memberResource->getOneBy(['code' => $username, 'project_id' => $project['id']]); if ($member) { $output->writeln("<error>Member '{$username}' already exists for project " . $project['code'] . "</error>"); continue; } print "Saving member '{$username}' for project " . $project['code'] . "\n"; $res = $memberResource->save($params); } } catch (Exception $e) { var_dump($params); $output->writeln("<error>Error importing member: {$e->getMessage()}</error>"); } } }
<?php require __DIR__ . '/../vendor/autoload.php'; use Survos\Client\SurvosClient; use Survos\Client\Resource\MemberResource; use Survos\Client\Resource\ProjectResource; $config = json_decode(file_get_contents(__DIR__ . '/config.json'), true); $client = new SurvosClient($config['endpoint']); if (!$client->authorize($config['username'], $config['password'])) { throw new \Exception('Wrong credentials!'); } // get all projects $userResource = new \Survos\Client\Resource\UserResource($client); //$user= $userResource->getOneBy('username', 'tac'); $pResource = new ProjectResource($client); $project = $pResource->getByCode('demo'); $resource = new MemberResource($client); $resource->save(['code' => "new_project_code5", 'phone_within_project' => '+447834274472', 'email_within_project' => '*****@*****.**', 'project_id' => $project['id']]); $res = $pResource->addModule('demo', 'field'); echo $res;