/** * needed to create a functional simupoll resource. */ public function simupoll($title, User $creator) { $simupoll = new Simupoll(); $simupoll->setTitle($title); if (!$this->simupollType) { $this->simupollType = new ResourceType(); $this->simupollType->setName('claroline_result'); $this->om->persist($this->simupollType); } $node = new ResourceNode(); $node->setName($title); $node->setCreator($creator); $node->setResourceType($this->simupollType); $node->setWorkspace($creator->getPersonalWorkspace()); $node->setClass('CPASimUSante\\SimupollBundle\\Entity\\Simupoll'); $node->setGuid(time()); $simupoll->setResourceNode($node); $this->om->persist($simupoll); $this->om->persist($node); return $simupoll; }
/** * 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; }