configFactory = $config_factory; $this->limesurvey = $lime_survey; } /** * {@inheritdoc} */ public static function create(ContainerInterface $cont) { return new static( $cont->get('config.factory'), $cont->get('limesurvey.client') ); } /** * {@inheritdoc} */ public function getFormId() { return 'nps_survey_form'; } /** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, $arg = NULL) { $data = []; $service = \Drupal::service('limesurvey.client'); $SurveyData = $this->configFactory->get('limesurvey.lime_survey.surveylist')->get('surveydata'); foreach ($SurveyData as $SurveyId => $value) { if( $arg ==$SurveyId ){ $request = $service->requestData($SurveyId); $data = $service->BuildSurveyForm($request); } } $service->ReleaseSessionKey($service->sessionKey); return($data); } /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { //this form submit is not necessary due to ajax form submit } /** * Implements the submit handler for the modal dialog AJAX call. * * @param array $form * Render array representing from. * @param \Drupal\Core\Form\FormStateInterface $form_state * Current form state. * * @return \Drupal\Core\Ajax\AjaxResponse * Array of AJAX commands to execute on submit of the modal form. */ public function ajaxSubmitForm(array &$form, FormStateInterface $form_state) { $ajax_response = new AjaxResponse(); if ($form_state->getErrors()) { $ajax_response->addCommand( new HtmlCommand( '.error_message', $this->t('
') ) ); } // If there are no errors, show the output dialog. else { $values = $form_state->getValues(); $SurveyID = $values['survey_id']; $groupID = $values['survey_gid']; foreach ($values as $key => $value) { if (strpos($key, 'question') !== FALSE) { $quest = explode('_', $key); $questionID = $quest[1]; $answer = $value; $sgqa = $SurveyID . 'X' . $groupID . 'X' . $questionID; $response[$sgqa] = $answer; } } $this->limesurvey->AddResponse($SurveyID, $response); $ajax_response->addCommand( new HtmlCommand( '.success_message', $this->t('Thank you for your feedback.