/** * @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; }