/** * 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); } }
/** * 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]; }
/** * Export the Questions linked to the Paper. * * @param Paper $paper * @param bool $withSolution * @param bool $forPaperList * * @return array */ public function exportPaperQuestions(Paper $paper, $withSolution = false, $forPaperList = false) { $solutionAvailable = $withSolution || $this->isSolutionAvailable($paper->getExercise(), $paper); $export = []; $questions = $this->getPaperQuestions($paper); foreach ($questions as $question) { $exportedQuestion = $this->questionManager->exportQuestion($question, $solutionAvailable, $forPaperList); $exportedQuestion->stats = null; if ($paper->getExercise()->hasStatistics()) { $exportedQuestion->stats = $this->questionManager->generateQuestionStats($question, $paper->getExercise()); } $export[] = $exportedQuestion; } return $export; }