/** * Add question. * * @param \CPASimUSante\SimupollBundle\Entity\Question $question * * @return Simupoll */ public function addQuestion(\CPASimUSante\SimupollBundle\Entity\Question $question) { $this->questions[] = $question; $question->setSimupoll($this); return $this; /* $question->setSimupoll($this); $this->questions->add($question); */ }
/** * Copy Simupoll - called by onCopy listener. * * @param $simupoll Simupoll * @param $loggedUser User * * @return $newSimupoll Simupoll */ public function copySimupoll(Simupoll $simupoll, $loggedUser) { $newSimupoll = new Simupoll(); $newSimupoll->setTitle($simupoll->getTitle()); $this->om->persist($newSimupoll); $questions = $simupoll->getQuestions(); $periods = $this->om->getRepository('CPASimUSanteSimupollBundle:Period')->findBySimupoll($simupoll); $categories = $this->om->getRepository('CPASimUSanteSimupollBundle:Category')->findBySimupoll($simupoll); //copy period foreach ($periods as $period) { $newPeriod = new Period(); $newPeriod->setSimupoll($newSimupoll); $newPeriod->setStart($period->getStart()); $newPeriod->setStop($period->getStop()); $newPeriod->setTitle($period->getTitle()); $this->om->persist($newPeriod); } $nc = []; $pc = []; foreach ($categories as $category) { $newCategory = new Category(); $newCategory->setName($category->getName()); $newCategory->setUser($category->getUser()); $newCategory->setSimupoll($newSimupoll); $newCategory->setParent(null); $this->om->persist($newCategory); if (null !== $category->getParent()) { $pc[$category->getId()] = array('nc' => $newCategory, 'pid' => $category->getParent()->getId()); } else { $pc[$category->getId()] = array('nc' => $newCategory, 'pid' => null); } } foreach ($pc as $oldid => $cats) { if (isset($pc[$cats['pid']])) { $cats['nc']->setParent($pc[$cats['pid']]['nc']); } else { $cats['nc']->setParent(null); } $this->om->persist($cats['nc']); } //copy questions foreach ($questions as $question) { $newQuestion = new Question(); $newQuestion->setSimupoll($newSimupoll); $newQuestion->setTitle($question->getTitle()); $newQuestion->setOrderq($question->getOrderq()); $newQuestion->setCategory($question->getCategory()); $this->om->persist($newQuestion); //copy propositions $propositions = $question->getPropositions(); foreach ($propositions as $proposition) { $newProposition = new Proposition(); $newProposition->setQuestion($newQuestion); $newProposition->setChoice($proposition->getChoice()); $newProposition->setMark($proposition->getMark()); $this->om->persist($newProposition); } } return $newSimupoll; }