/** * Create and add a new Step to an Exercise. * * @param Exercise $exercise * * @return Step */ public function addStep(Exercise $exercise) { $step = new Step(); $step->setOrder($exercise->getSteps()->count() + 1); // Link the Step to the Exercise $exercise->addStep($step); $this->om->persist($step); $this->om->flush(); return $step; }
/** * create the exercise. * * @param array $step - properties of the step * @param Exercise $exercise * * @return Step */ private function createStep(array $step, Exercise $exercise) { $newStep = new Step(); $newStep->setText($step['text']); $newStep->setOrder($step['order']); $newStep->setShuffle($step['shuffle']); $newStep->setNbQuestion($step['nbQuestion']); $newStep->setKeepSameQuestion($step['keepSameQuestion']); $newStep->setDuration($step['duration']); $newStep->setMaxAttempts($step['maxAttempts']); $newStep->setExercise($exercise); $this->om->persist($newStep); $this->om->flush(); return $newStep; }
/** * @param Exercise $exercise * @param int $orderStep * * @return Step */ public function createStep(Exercise $exercise, $orderStep) { $em = $this->doctrine->getManager(); //Creating a step by question $step = new Step(); $step->setText(' '); $step->setExercise($exercise); $step->setNbQuestion(0); $step->setDuration(0); $step->setMaxAttempts(0); $step->setOrder($orderStep); $em->persist($step); return $step; }
/** * @param string $title * @param Question[] $questions * @param User $user * * @return Exercise */ public function exercise($title, array $questions = [], User $user = null) { $exercise = new Exercise(); if ($user) { if (!isset($this->exoType)) { $this->exoType = new ResourceType(); $this->exoType->setName('exercise'); $this->om->persist($this->exoType); } $node = new ResourceNode(); $node->setName($title); $node->setCreator($user); $node->setResourceType($this->exoType); $node->setWorkspace($user->getPersonalWorkspace()); $node->setClass('UJM\\ExoBundle\\Entity\\Exercise'); $node->setGuid(time()); $exercise->setResourceNode($node); $this->om->persist($node); } $this->om->persist($exercise); for ($i = 0, $max = count($questions); $i < $max; ++$i) { $step = new Step(); $step->setText('step'); $step->setOrder($i); // Add step to the exercise $exercise->addStep($step); $this->om->persist($step); $stepQuestion = new StepQuestion(); $stepQuestion->setStep($step); $stepQuestion->setQuestion($questions[$i]); $stepQuestion->setOrdre(0); $this->om->persist($stepQuestion); } return $exercise; }
/** * Set Step. * * @param Step $step */ public function setStep(Step $step) { $this->step = $step; $step->addStepQuestion($this); }
/** * 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]; }