/** * @param ISurveyStep $step * @param string $action * @param string $form_name * @return Form */ public function build(ISurveyStep $step, $action, $form_name = 'SurveyStepForm') { Requirements::customScript('jQuery(document).ready(function($) { var form = $(".survey_step_form"); form.validate(); });'); $fields = new FieldList(); $content = $step->template()->content(); if (!empty($content)) { $fields->add(new LiteralField('content', $content)); } if ($step->template()->canSkip() && !$step->survey()->isLastStep()) { $next_step_url = sprintf("%s%s/skip-step", Controller::curr()->Link(), $step->template()->title()); if ($step->survey() instanceof EntitySurvey) { $dyn_step_holder = $step->survey()->owner()->template()->title(); $id = $step->survey()->getIdentifier(); $next_step_url = sprintf("%s%s/edit/%s/skip-step", Controller::curr()->Link(), $dyn_step_holder, $id); } $fields->add(new LiteralField('skip', sprintf('<p><strong>If you do not wish to answer these questions, you may <a href="%s">skip to the next section</a>.</strong></p>', $next_step_url))); } if (!empty($content) || $step->template()->canSkip()) { $fields->add(new LiteralField('hr', '<hr/>')); } foreach ($step->template()->getQuestions() as $q) { if ($q->isHidden()) { continue; } $type = $q->Type(); $builder_class = $type . 'UIBuilder'; // @ISurveyQuestionTemplateUIBuilder $builder = Injector::inst()->create($builder_class); $field = $builder->build($step, $q, $step->getAnswerByTemplateId($q->getIdentifier())); $fields->add($field); } $validator = null; $fields->add(new HiddenField('survey_id', 'survey_id', $step->survey()->getIdentifier())); $fields->add(new HiddenField('step_id', 'step_id', $step->getIdentifier())); $survey = $step->survey(); $next_btn_title = 'Next'; if ($survey->isLastStep()) { $next_btn_title = 'Done'; if ($survey->template() instanceof IEntitySurveyTemplate) { $next_btn_title = 'Save ' . $survey->template()->getEntityName(); } } $actions = new FieldList($default_action = FormAction::create($action)->setTitle($next_btn_title)); $form = new RegularStepForm(Controller::curr(), $form_name, $fields, $actions, $step, $validator); $form->setDefaultAction($default_action); $form->setAttribute('class', 'survey_step_form'); return $form; }
/** * @param ISurveyStep $step * @return null|ISurveyStepUIBuilder */ public function build(ISurveyStep $step) { if ($step->template() instanceof ISurveyRegularStepTemplate) { if ($step->survey() instanceof IEntitySurvey) { return new EntitySurveyRegularStepTemplateUIBuilder(); } return new SurveyRegularStepTemplateUIBuilder(); } if ($step->template() instanceof ISurveyDynamicEntityStepTemplate) { return new SurveyDynamicEntityStepTemplateUIBuilder(); } if ($step->template() instanceof ISurveyThankYouStepTemplate) { return new SurveySurveyThankYouStepTemplateUIBuilder(); } return null; }
/** * @param ISurveyStep $step * @param string $action * @param string $form_name * @return Form */ public function build(ISurveyStep $step, $action, $form_name = 'SurveyStepForm') { $fields = new FieldList(); $fields->add(new HiddenField('survey_id', 'survey_id', $step->survey()->getIdentifier())); $fields->add(new HiddenField('step_id', 'step_id', $step->getIdentifier())); $content = $step->template()->content(); if (!empty($content)) { $fields->add(new LiteralField('content', $content)); } if ($step->template()->canSkip()) { $fields->add(new LiteralField('skip', sprintf('<p><strong>If you do not wish to answer these questions, you may <a href="%s%s/skip-step">skip to the next section</a>.</strong></p>', Controller::curr()->Link(), $step->template()->title()))); } if (!empty($content) || $step->template()->canSkip()) { $fields->add(new LiteralField('hr', '<hr/>')); } $validator = null; $actions = new FieldList(FormAction::create('AddEntity')->setTitle("Add")->setUseButtonTag(true), FormAction::create('Done')->setTitle("Done")->setUseButtonTag(true)); $form = new DynamicStepForm(Controller::curr(), $form_name, $fields, $actions, $step, $validator); $form->setTemplate('DynamicEntityStepForm'); $form->setAttribute('class', 'survey_step_form'); return $form; }
/** * @param ISurveyStep $step * @return bool */ public function canShowStep(ISurveyStep $step) { $should_show = true; // checks if we have a dependency to show it or not $static_rules = array(); foreach ($step->template()->getDependsOn() as $d) { // belongs to another step (former one) if (!isset($static_rules[$d->getIdentifier()])) { $static_rules[$d->getIdentifier()] = array('question' => $d, 'values' => array(), 'operator' => $d->Operator, 'visibility' => $d->Visibility, 'default' => $d->DependantDefaultValue, 'boolean_operator' => $d->BooleanOperatorOnValues, 'initial_condition' => $d->BooleanOperatorOnValues === 'And' ? true : false); } array_push($static_rules[$d->getIdentifier()]['values'], $d->ValueID); } foreach ($static_rules as $id => $info) { $q = $info['question']; $values = $info['values']; $operator = $info['operator']; $visibility = $info['visibility']; $boolean_operator = $info['boolean_operator']; $initial_condition = $info['initial_condition']; $answer = $this->findAnswerByQuestion($q); if (is_null($answer)) { return false; } //checks the condition switch ($operator) { case 'Equal': foreach ($values as $vid) { if ($boolean_operator === 'And') { $initial_condition &= strpos($answer->value(), $vid) !== false; } else { $initial_condition |= strpos($answer->value(), $vid) !== false; } } break; case 'Not-Equal': foreach ($values as $vid) { if ($boolean_operator === 'And') { $initial_condition &= strpos($answer->value(), $vid) === false; } else { $initial_condition |= strpos($answer->value(), $vid) === false; } } break; } //visibility switch ($visibility) { case 'Visible': if (!$initial_condition) { $should_show = false; } break; case 'Not-Visible': if ($initial_condition) { $should_show = false; } break; } } return $should_show; }
/** * @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 array $data * @param ISurveyStep $current_step * @return ISurveyStep */ public function completeStep(ISurveyStep $current_step, array $data) { $template_repository = $this->template_repository; $survey_repository = $this->survey_repository; $survey_builder = $this->survey_builder; $member_repository = $this->member_repository; return $this->tx_manager->transaction(function () use($current_step, $data, $survey_builder, $member_repository, $template_repository, $survey_repository) { $current_survey = $current_step->survey(); if ($current_step instanceof ISurveyRegularStep) { $current_step->clearAnswers(); foreach ($current_step->template()->getQuestions() as $q) { if (isset($data[$q->name()])) { // its has an answer set if ($q->name() === SurveyOrganizationQuestionTemplate::FieldName) { //publish event PublisherSubscriberManager::getInstance()->publish('survey_organization_selected', array($current_survey->createdBy(), $data[$q->name()])); } $current_step->addAnswer($survey_builder->buildAnswer($q, $data[$q->name()])); } } } return $current_survey->completeCurrentStep(); }); }