/** * Records an answer for an exercise Step. * * @EXT\Route("/papers/{paperId}/steps/{stepId}", name="exercise_submit_step") * @EXT\Method("PUT") * * @EXT\ParamConverter("user", converter="current_user", options={"allowAnonymous"=true}) * @EXT\ParamConverter("paper", class="UJMExoBundle:Paper", options={"mapping": {"paperId": "id"}}) * @EXT\ParamConverter("step", class="UJMExoBundle:Step", options={"mapping": {"stepId": "id"}}) * * @param Paper $paper * @param Step $step * @param User $user * @param Request $request * * @return JsonResponse */ public function submitStepAction(Paper $paper, Step $step, User $user = null, Request $request) { $this->assertHasPaperAccess($paper, $user); // Get submitted answers from Request $data = $request->request->get('data'); /** @var StepQuestion $stepQuestion */ foreach ($step->getStepQuestions() as $stepQuestion) { /** @var Question $question */ $question = $stepQuestion->getQuestion(); // Get question data from Request $questionData = !isset($data[$question->getId()]) ? null : $data[$question->getId()]; $errors = $this->questionManager->validateAnswerFormat($question, $questionData); if (count($errors) !== 0) { return new JsonResponse($errors, 422); } $this->paperManager->recordAnswer($paper, $question, $questionData, $request->getClientIp()); } if (Exercise::TYPE_FORMATIVE === $paper->getExercise()->getType()) { // For formative, export solution and score for immediate feedback $answers = []; /** @var StepQuestion $stepQuestion */ foreach ($step->getStepQuestions() as $stepQuestion) { $answers[] = ['question' => $this->questionManager->exportQuestionAnswers($stepQuestion->getQuestion()), 'answer' => $this->paperManager->exportPaperAnswer($stepQuestion->getQuestion(), $paper, true)]; } return new JsonResponse($answers, 200); } else { return new JsonResponse('', 204); } }