/** * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output */ protected function execute(InputInterface $input, OutputInterface $output) { $this->services = []; $isVerbose = $output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE; $projectCode = $input->getOption('project-code'); /** @type AssignmentResource $assignmentResource */ $assignmentResource = new AssignmentResource($this->sourceClient); $page = 0; $perPage = 10; $maxPages = 1; $criteria = []; $data = []; // need much better filter! Maybe the survey or wave needs to associate itself with an external service? $assignments = $assignmentResource->getList(1, 1, ['survey_response_status_code' => 'initiated'], null, null, ['project_code' => 'behattest']); // if no items, return if (!count($assignments['items']) || !$assignments['total']) { return; } $tasksIds = array_map(function ($item) { return $item['task_id']; }, $assignments['items']); $taskResource = new TaskResource($this->sourceClient); $tasks = $taskResource->getList(null, null, ['id' => array_unique($tasksIds)], ['id' => SurvosCriteria::IN]); $surveyByTask = []; foreach ($tasks['items'] as $task) { $surveyByTask[$task['id']] = isset($task['survey_json']) ? json_decode($task['survey_json'], true) : null; } foreach ($assignments['items'] as $key => $assignment) { $taskId = $assignment['task_id']; $survey = $surveyByTask[$taskId]; // $answers = []; foreach ($survey['questions'] as $question) { if ($isVerbose) { $output->writeln("Checking question \\'{$question['text']}\\' "); } switch ($question['code']) { case 'temp': $weatherData = $this->getWeatherData(); // needs persisting to responses $answers[$question['code']] = $weatherData['main']['temp']; break; case 'wind_speed': $weatherData = $this->getWeatherData(); $answers[$question['code']] = $weatherData['wind']['speed']; break; default: throw new Exception("Unhandled field '{$question['code']}' in survey"); } } if (!empty($answers)) { $assignment['flat_data'] = array_merge($assignment['flat_data'] ?: [], $answers); $assignmentResource->save($assignment); } } }
function saveAssignment($client, $data) { $resource = new \Survos\Client\Resource\AssignmentResource($client); $response = $resource->save($data); }