/** * 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); } }
/** * Add a question in a step. * * * @param Question $question * @param Step $step * @param int $order * * @deprecated Use StepManager::addQuestion(Step $step, Question $question, $order = -1) instead */ public function addQuestionInStep($question, $step, $order) { if ($step != null) { $sq = new StepQuestion(); if ($order == -1) { $order = $step->getStepQuestions()->count() + 1; } $sq->setOrdre($order); $sq->setStep($step); $sq->setQuestion($question); $this->om->persist($sq); $this->om->flush(); } }
/** * Exports a step in a JSON-encodable format. * * @param Step $step * @param bool $withSolutions * * @return array */ public function exportStep(Step $step, $withSolutions = true) { $stepQuestions = $step->getStepQuestions(); $items = []; /** @var StepQuestion $stepQuestion */ foreach ($stepQuestions as $stepQuestion) { $question = $stepQuestion->getQuestion(); $items[] = $this->questionManager->exportQuestion($question, $withSolutions); } return ['id' => $step->getId(), 'meta' => ['description' => $step->getText(), 'maxAttempts' => $step->getMaxAttempts(), 'title' => $step->getTitle()], 'items' => $items]; }