/**
  * @param ISurvey $parent
  * @param IEntitySurveyTemplate $template
  * @param $owner
  * @return EntitySurvey
  */
 public function buildEntitySurvey(ISurvey $parent, IEntitySurveyTemplate $template, $owner)
 {
     $survey = new EntitySurvey();
     $survey->TemplateID = $template->getIdentifier();
     $survey->CreatedByID = $owner->getIdentifier();
     $survey->ParentID = $parent->getIdentifier();
     $i = 0;
     foreach ($template->getSteps() as $step_template) {
         ++$i;
         $new_step = $this->buildStep($step_template);
         $survey->addStep($new_step);
         if ($i == 1) {
             $survey->registerCurrentStep($new_step);
             $survey->registerAllowedMaxStep($new_step);
         }
     }
     return $survey;
 }
 /**
  * @param ISurvey $survey
  * @param ISurveyBuilder $survey_builder
  * @param ISurveyManager $manager
  */
 public function autoPopulate(ISurvey $survey, ISurveyBuilder $survey_builder, ISurveyManager $manager)
 {
     $template = $survey->template();
     $owner = $survey->createdBy();
     $mappings = $template->getAutopopulationMappings();
     // get former survey ...
     $old_survey = DeploymentSurvey::get()->filter('MemberID', $owner->getIdentifier())->sort('Created', 'DESC')->first();
     if (is_null($old_survey)) {
         return;
     }
     foreach ($mappings as $mapping) {
         if (!$mapping instanceof IOldSurveyMigrationMapping) {
             continue;
         }
         $origin_table_name = $mapping->getOriginTableName();
         $origin_field_name = $mapping->getOriginFieldName();
         $question = $mapping->getTargetQuestion();
         if (is_null($question)) {
             continue;
         }
         $step_template = $question->step();
         if (is_null($step_template)) {
             continue;
         }
         $step = $survey->getStep($step_template->title());
         if (is_null($step)) {
             continue;
         }
         if (!$step instanceof ISurveyRegularStep) {
             continue;
         }
         if ($origin_table_name === 'DeploymentSurvey') {
             $old_data = $old_survey->{$origin_field_name};
             // old data is only a label, we need to find out the value
             $data = $old_data;
             if ($question instanceof IMultiValueQuestionTemplate) {
                 $data = '';
                 if ($question instanceof IDropDownQuestionTemplate && $question->isCountrySelector()) {
                     $data = $old_data;
                 } else {
                     $old_data = explode(',', $old_data);
                     foreach ($old_data as $od) {
                         $v = $question->getValueByValue($od);
                         if (is_null($v)) {
                             continue;
                         }
                         if (!empty($data)) {
                             $data .= ',';
                         }
                         $data .= $v->getIdentifier();
                     }
                 }
             }
             if (empty($data)) {
                 continue;
             }
             $step->addAnswer($survey_builder->buildAnswer($question, $data));
         }
         if ($origin_table_name === 'AppDevSurvey') {
             $app_dev_survey = $old_survey->AppDevSurveys()->first();
             if (!$app_dev_survey) {
                 continue;
             }
             $old_data = $app_dev_survey->{$origin_field_name};
             $data = $old_data;
             if ($question instanceof IMultiValueQuestionTemplate) {
                 $data = '';
                 $old_data = explode(',', $old_data);
                 foreach ($old_data as $od) {
                     $v = $question->getValueByValue($od);
                     if (is_null($v)) {
                         continue;
                     }
                     if (!empty($data)) {
                         $data .= ',';
                     }
                     $data .= $v->getIdentifier();
                 }
             }
             if (empty($data)) {
                 continue;
             }
             $step->addAnswer($survey_builder->buildAnswer($question, $data));
         }
     }
     //check if we need to auto-populate entities
     foreach ($template->getEntities() as $entity_survey) {
         if ($entity_survey->belongsToDynamicStep() && $entity_survey->shouldPrepopulateWithFormerData()) {
             $mappings = $entity_survey->getAutopopulationMappings();
             $dyn_step = $survey->getStep($entity_survey->getDynamicStepTemplate()->title());
             if (is_null($dyn_step)) {
                 continue;
             }
             foreach ($old_survey->Deployments() as $old_deployment) {
                 $entity_survey = $manager->buildEntitySurvey($dyn_step, $owner->getIdentifier());
                 foreach ($mappings as $mapping) {
                     if (!$mapping instanceof IOldSurveyMigrationMapping) {
                         continue;
                     }
                     $origin_table_name = $mapping->getOriginTableName();
                     $origin_field_name = $mapping->getOriginFieldName();
                     $question = $mapping->getTargetQuestion();
                     $step_template = $question->step();
                     $step = $entity_survey->getStep($step_template->title());
                     if (!$step instanceof ISurveyRegularStep) {
                         continue;
                     }
                     if ($origin_table_name === 'Deployment') {
                         $old_data = $old_deployment->{$origin_field_name};
                         $data = $old_data;
                         if ($question instanceof IMultiValueQuestionTemplate) {
                             $data = '';
                             $old_data = explode(',', $old_data);
                             foreach ($old_data as $od) {
                                 $v = $question->getValueByValue($od);
                                 if (is_null($v)) {
                                     continue;
                                 }
                                 if (!empty($data)) {
                                     $data .= ',';
                                 }
                                 $data .= $v->getIdentifier();
                             }
                         }
                         if (empty($data)) {
                             continue;
                         }
                         $step->addAnswer($survey_builder->buildAnswer($question, $data));
                     }
                 }
             }
         }
     }
 }
Example #3
0
 /**
  * @param IEntitySurvey $entity
  * @param ISurveyStep $next_step
  * @return SS_HTTPResponse
  */
 private function go2DynEntityStep(IEntitySurvey $entity, ISurveyStep $next_step)
 {
     $dyn_step_holder_title = $this->current_survey->currentStep()->template()->title();
     $next_step_title = $next_step->template()->title();
     return $this->redirect(sprintf('%s%s/edit/%s/%s', $this->Link(), $dyn_step_holder_title, $entity->getIdentifier(), $next_step_title));
 }
 /**
  * @param ISurvey $survey
  * @return void
  */
 public function resetSteps(ISurvey $survey)
 {
     return $this->tx_manager->transaction(function () use($survey) {
         if ($survey->isLastStep()) {
             $steps = $survey->getSteps();
             // reset to first step
             $survey->registerCurrentStep($steps[0]);
         }
     });
 }