JackSteller
JackSteller

Reputation: 35

calculate the number of points and save the result in the user profile database

So I have a form containing questions of type free fields (text), multiple choice (checkbox), single choice (radio).

enter image description here

I get the values ​​of the button checked and I save in database for each user but what I want to do is change this value like this:

data-point "2" = 10 points,                                                                                                                          
data-point "3" = 7 points,                                                                                                                          
data-point "4" = 4 points,                                                                                                                         
data-point "5" = 2 points,                                                                                                                         

and then with these values ​​I have to do a calculation and define a profile according to the result .. how should I do that? SQL request? in the loop? a little help would be welcome ..here my twig to loop on subquestion.

<div class=" col-6 d-flex">
     <div class="label-div1">

           <label for="subscale{{subQuestion.id}}for{{i}}>
              <img src="{{asset('images/survey/humeur/'~ i ~'.jpg')}}">
            </label>

             <input type="radio" class="radio-pict" name="{{subQuestion.id}}" id="subscale{{i}}" data-point="{{i}}"value="{{i}}">

         </div>

here my controller to save de answers

 public function saveAnswer(Request $request)
{
    /* Repository */
    $questionRepo =  $this->getDoctrine()->getRepository(Question::class);
    $answerRepo =  $this->getDoctrine()->getRepository(Answer::class);
    $choiceRepo = $this->getDoctrine()->getRepository(Choice::class);
    $userSlpRepo = $this->getDoctrine()->getRepository(UserSlp::class);

    /* Entity Manager */
    $em = $this->getDoctrine()->getManager();


    $datas = $request->request->all();

    $userSlp = $userSlpRepo->findOneByGaeaUserId($this->getUser()->getId());

    foreach ($datas as $data => $value) {

        $questionId = explode("_", $data);
        $question = $questionRepo->findOneById($questionId[0]);
       switch ($question) {
            case 'Sub_question_free':

                $answer = new Answer_free;
                $answer->setFreeAswere($value);

                break;

            case 'Sub_question_scale':

                $answer = new Answer_scale;

                $answer->setScale($value);

                break; 

        $answer->setQuestion($question);
        $answer->setUserSlp($userSlp);

        $em->persist($answer);
        $em->flush();
       }
      exit;
    }
  }

and here my twig to display on admin the results..

{% if answer.question == "Sub_question_choice" or
      answer.question == "Sub_question_scale" or
      answer.question == "Sub_question_free" %}
{% if answer == "choice" %} 
{% for choice in answer.choices %}
<p>{{choice.name}}</p>
{% endfor %}
{% elseif answer == "free" %}
<p>{{answer.freeAswere}}</p>
 {% elseif answer == "scale" %}
<p> {{answer.scale}}</p>
{% endif %}

Upvotes: 0

Views: 87

Answers (1)

A.DREY
A.DREY

Reputation: 38

So first, in Symfony a controller always need to send back a response. Even if it's just a redirect or a JsonResponse. So you shouldn't use exit in your controller.

You should use Symfony form, but if you are new to Symfony, I can understand that it's not easy.

Here to help you, you have many options:

  1. You can change value with some conditions inside your swith statement in your controller
  2. You can separate that in a service, look for service in Symfony on the docs. And use dependency injection inside your controller like this :
Service : 
    public function conversion(){
    //Some conditions
    }
controller: 
    public function saveAnswer(Request $request, YourService $service)

And use it inside as a function.

  1. (More Easy Option) You can overload your setter in your entity (Answer.php for example):
public function yourSetter(...){
//do some conditions and after set your variables depending on result
}

Hope this will help you, feel free to ask if you want !

Upvotes: 1

Related Questions