Example #1
0
 /**
  * @DI\Observe("copy_claroline_survey")
  *
  * @param CopyResourceEvent $event
  */
 public function onCopy(CopyResourceEvent $event)
 {
     $workspace = $event->getParent()->getWorkspace();
     $survey = $event->getResource();
     $copy = new Survey();
     $copy->setPublished($survey->isPublished());
     $copy->setClosed($survey->isClosed());
     $copy->setHasPublicResult($survey->getHasPublicResult());
     $copy->setAllowAnswerEdition($survey->getAllowAnswerEdition());
     $copy->setStartDate($survey->getStartDate());
     $copy->setEndDate($survey->getEndDate());
     $this->om->persist($copy);
     $relations = $survey->getQuestionRelations();
     foreach ($relations as $relation) {
         $question = $relation->getQuestion();
         $type = $question->getType();
         $copyQuestion = new Question();
         $copyQuestion->setTitle($question->getTitle());
         $copyQuestion->setQuestion($question->getQuestion());
         $copyQuestion->setWorkspace($workspace);
         $copyQuestion->setType($type);
         $copyQuestion->setCommentAllowed($question->isCommentAllowed());
         $copyQuestion->setCommentLabel($question->getCommentLabel());
         $this->om->persist($copyQuestion);
         switch ($type) {
             case 'multiple_choice_single':
             case 'multiple_choice_multiple':
                 $multiChoiceQuestion = $this->surveyManager->getMultipleChoiceQuestionByQuestion($question);
                 $choices = $multiChoiceQuestion->getChoices();
                 $copyMultiQuestion = new MultipleChoiceQuestion();
                 $copyMultiQuestion->setHorizontal($multiChoiceQuestion->getHorizontal());
                 $copyMultiQuestion->setQuestion($copyQuestion);
                 foreach ($choices as $choice) {
                     $copyChoice = new Choice();
                     $copyChoice->setContent($choice->getContent());
                     $copyChoice->setOther($choice->isOther());
                     $copyChoice->setChoiceQuestion($copyMultiQuestion);
                     $this->om->persist($copyChoice);
                 }
                 $this->om->persist($copyMultiQuestion);
                 break;
             case 'open-ended':
             default:
                 break;
         }
         $copyRelation = new SurveyQuestionRelation();
         $copyRelation->setSurvey($copy);
         $copyRelation->setQuestion($copyQuestion);
         $copyRelation->setQuestionOrder($relation->getQuestionOrder());
         $this->om->persist($copyRelation);
     }
     $event->setCopy($copy);
     $event->stopPropagation();
 }
 private function updateMultipleChoiceQuestion(Question $question, array $datas)
 {
     $horizontal = isset($datas['choice-display']) && $datas['choice-display'] === 'horizontal';
     $choices = isset($datas['choice']) ? $datas['choice'] : [];
     $hasChoiceOther = isset($datas['choice-other']['other']) && $datas['choice-other']['other'] === 'other';
     $multipleChoiceQuestion = $this->surveyManager->getMultipleChoiceQuestionByQuestion($question);
     if (is_null($multipleChoiceQuestion)) {
         $multipleChoiceQuestion = $this->surveyManager->createMultipleChoiceQuestion($question, $horizontal, $choices);
     } else {
         $this->surveyManager->updateQuestionChoices($multipleChoiceQuestion, $horizontal, $choices);
     }
     if ($hasChoiceOther && isset($datas['choice-other']['content']) && !empty($datas['choice-other']['content'])) {
         $otherChoice = new Choice();
         $otherChoice->setChoiceQuestion($multipleChoiceQuestion);
         $otherChoice->setOther(true);
         $otherChoice->setContent($datas['choice-other']['content']);
         $this->surveyManager->persistChoice($otherChoice);
     }
 }
Example #3
0
 public function updateQuestionChoices(MultipleChoiceQuestion $multipleChoiceQuestion, $horizontal, array $newChoices)
 {
     $multipleChoiceQuestion->setHorizontal($horizontal);
     $this->om->persist($multipleChoiceQuestion);
     $oldChoices = $multipleChoiceQuestion->getChoices();
     foreach ($oldChoices as $oldChoice) {
         $this->om->remove($oldChoice);
     }
     foreach ($newChoices as $newChoice) {
         if (!empty($newChoice)) {
             $choice = new Choice();
             $choice->setChoiceQuestion($multipleChoiceQuestion);
             $choice->setContent($newChoice);
             $this->om->persist($choice);
         }
     }
     $this->om->flush();
 }