source/View/Controller/StudyController.php line 451

Open in your IDE?
  1. <?php
  2. namespace App\View\Controller;
  3. use App\Crud\Crudable;
  4. use App\Domain\Model\Study\CreatorMetaDataGroup;
  5. use App\Domain\Model\Study\Experiment;
  6. use App\Questionnaire\Questionnairable;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Psr\Log\LoggerInterface;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\Security\Core\Security;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. /**
  17.  * @Route("/studies", name="Study-")
  18.  * @IsGranted("ROLE_USER")
  19.  *
  20.  * Class StudyController
  21.  * @package App\View\Controller
  22.  */
  23. class StudyController extends AbstractController
  24. {
  25.     private Security $security;
  26.     private Questionnairable $questionnaire;
  27.     private EntityManagerInterface $em;
  28.     private LoggerInterface $logger;
  29.     private Crudable $crud;
  30.     /**
  31.      * @param Security $security
  32.      * @param Questionnairable $questionnaire
  33.      * @param EntityManagerInterface $em
  34.      * @param LoggerInterface $logger
  35.      * @param Crudable $crud
  36.      */
  37.     public function __construct(Security $securityQuestionnairable $questionnaireEntityManagerInterface $emLoggerInterface $loggerCrudable $crud)
  38.     {
  39.         $this->security $security;
  40.         $this->questionnaire $questionnaire;
  41.         $this->em $em;
  42.         $this->logger $logger;
  43.         $this->crud $crud;
  44.     }
  45.     /**
  46.      * @Route("/", name="overview")
  47.      *
  48.      * @return Response
  49.      */
  50.     public function overviewAction(): Response
  51.     {
  52.         $this->logger->debug("Enter StudyController::overviewAction");
  53.         return $this->render('Pages/Study/overview.html.twig', [
  54.             'all_experiments' => $this->em->getRepository(Experiment::class)->findAll(),
  55.         ]);
  56.     }
  57.     /**
  58.      * @Route("/new", name="new")
  59.      *
  60.      * @param Questionnairable $questionnaire
  61.      * @param Crudable $crud
  62.      * @param Request $request
  63.      * @return Response
  64.      */
  65.     public function newAction(Questionnairable $questionnaireCrudable $crudRequest $request): Response
  66.     {
  67.         $this->logger->debug("Enter StudyController::newAction");
  68.         $newExperiment Experiment::createNewExperiment($this->security->getUser());
  69.         $form $questionnaire->askAndHandle($newExperiment->getSettingsMetaDataGroup(), 'create'$request);
  70.         if ($this->questionnaire->isSubmittedAndValid($form)) {
  71.             $this->em->persist($newExperiment);
  72.             $this->em->flush();
  73.             return $this->redirectToRoute('Study-introduction', ['uuid' => $newExperiment->getId()]);
  74.         }
  75.         return $this->render('Pages/Study/new.html.twig', [
  76.             'form' => $form->createView(),
  77.             'experiment' => $newExperiment,
  78.         ]);
  79.     }
  80.     /**
  81.      * @Route("/{uuid}/settings", name="settings")
  82.      *
  83.      * @param string $uuid
  84.      * @param Request $request
  85.      * @return Response
  86.      */
  87.     public function settingsAction(string $uuidRequest $request): Response
  88.     {
  89.         $this->logger->debug("Enter StudyController::settingsAction with [UUID: $uuid]");
  90.         $entityAtChange $this->getEntityAtChange($uuid);
  91.         $form $this->questionnaire->askAndHandle($entityAtChange->getSettingsMetaDataGroup(), 'save'$request);
  92.         if ($this->questionnaire->isSubmittedAndValid($form)) {
  93.             $this->em->persist($entityAtChange);
  94.             $this->em->flush();
  95.         }
  96.         return $this->render('Pages/Study/settings.html.twig', [
  97.             'form' => $form->createView(),
  98.             'experiment' => $entityAtChange,
  99.         ]);
  100.     }
  101.     /**
  102.      * @Route("/{uuid}/documentation", name="documentation")
  103.      *
  104.      * @param string $uuid
  105.      * @param Request $request
  106.      * @return Response
  107.      */
  108.     public function documentationAction(string $uuidRequest $request): Response
  109.     {
  110.         $this->logger->debug("Enter StudyController::documentationAction with [UUID: $uuid]");
  111.         $entityAtChange $this->getEntityAtChange($uuid);
  112.         $basicInformation $entityAtChange->getBasicInformationMetaDataGroup();
  113.         if (sizeof($basicInformation->getCreators()) == 0) {
  114.             $basicInformation->getCreators()->add(new CreatorMetaDataGroup());
  115.         }
  116.         $basicInformation->setRelatedPublications($this->prepareEmptyArray($basicInformation->getRelatedPublications()));
  117.         $form $this->questionnaire->askAndHandle($basicInformation'save'$request);
  118.         if ($this->questionnaire->isSubmittedAndValid($form)) {
  119.             $formData $form->getData();
  120.             $currentCreators $this->em->getRepository(CreatorMetaDataGroup::class)->findBy(['basicInformation' => $basicInformation]);
  121.             if (is_iterable($currentCreators)) {
  122.                 foreach ($currentCreators as $currentCreator) {
  123.                     $this->em->remove($currentCreator);
  124.                 }
  125.             }
  126.             if ($form->getData()->getCreators() !== null && is_iterable($form->getData()->getCreators())) {
  127.                 foreach ($formData->getCreators() as $creator) {
  128.                     if (!$creator->isEmpty()) {
  129.                         $creator->setCreditRoles(array_values(array_unique($creator->getCreditRoles())));
  130.                         $creator->setBasicInformation($basicInformation);
  131.                         $this->em->persist($creator);
  132.                     } else {
  133.                         $form->getData()->getCreators()->removeElement($creator);
  134.                     }
  135.                 }
  136.             }
  137.             $formData->setRelatedPublications(array_filter($formData->getRelatedPublications()));
  138.             $this->em->persist($formData);
  139.             $this->em->flush();
  140.             switch (true) {
  141.                 case $form->get('saveAndNext')->isClicked():
  142.                     return $this->redirectToRoute('Study-theory', ['uuid' => $uuid]);
  143.                 case $form->get('saveAndIntroduction')->isClicked():
  144.                     return $this->redirectToRoute('Study-introduction', ['uuid' => $uuid]);
  145.                 case $form->get('saveAndDocumentation')->isClicked():
  146.                     return $this->redirectToRoute('Study-documentation', ['uuid' => $uuid]);
  147.                 case $form->get('saveAndTheory')->isClicked():
  148.                     return $this->redirectToRoute('Study-theory', ['uuid' => $uuid]);
  149.                 case $form->get('saveAndMethod')->isClicked():
  150.                     return $this->redirectToRoute('Study-method', ['uuid' => $uuid]);
  151.                 case $form->get('saveAndMeasure')->isClicked():
  152.                     return $this->redirectToRoute('Study-measure', ['uuid' => $uuid]);
  153.                 case $form->get('saveAndSample')->isClicked():
  154.                     return $this->redirectToRoute('Study-sample', ['uuid' => $uuid]);
  155.                 case $form->get('saveAndDatasets')->isClicked():
  156.                     return $this->redirectToRoute('Study-datasets', ['uuid' => $uuid]);
  157.                 case $form->get('saveAndMaterials')->isClicked():
  158.                     return $this->redirectToRoute('Study-materials', ['uuid' => $uuid]);
  159.                 case $form->get('saveAndReview')->isClicked():
  160.                     return $this->redirectToRoute('Study-review', ['uuid' => $uuid]);
  161.                 case $form->get('saveAndExport')->isClicked():
  162.                     return $this->redirectToRoute('export_index', ['uuid' => $uuid]);
  163.                 case $form->get('saveAndSettings')->isClicked():
  164.                     return $this->redirectToRoute('Study-settings', ['uuid' => $uuid]);
  165.             }
  166.             return $this->redirectToRoute('Study-documentation', ['uuid' => $uuid]);
  167.         }
  168.         return $this->render('Pages/Study/documentation.html.twig', [
  169.             'form' => $form->createView(),
  170.             'experiment' => $entityAtChange,
  171.         ]);
  172.     }
  173.     /**
  174.      * @Route("/{uuid}/theory", name="theory")
  175.      *
  176.      * @param string $uuid
  177.      * @param Request $request
  178.      * @return Response
  179.      */
  180.     public function theoryAction(string $uuidRequest $request): Response
  181.     {
  182.         $this->logger->debug("Enter StudyController::theoryAction with [UUID: $uuid]");
  183.         $entityAtChange $this->getEntityAtChange($uuid);
  184.         $form $this->questionnaire->askAndHandle($entityAtChange->getTheoryMetaDataGroup(), 'save'$request);
  185.         if ($this->questionnaire->isSubmittedAndValid($form)) {
  186.             $this->em->persist($entityAtChange);
  187.             $this->em->flush();
  188.             switch (true) {
  189.                 case $form->get('saveAndPrevious')->isClicked():
  190.                     return $this->redirectToRoute('Study-documentation', ['uuid' => $uuid]);
  191.                 case $form->get('saveAndNext')->isClicked():
  192.                     return $this->redirectToRoute('Study-method', ['uuid' => $uuid]);
  193.                 case $form->get('saveAndIntroduction')->isClicked():
  194.                     return $this->redirectToRoute('Study-introduction', ['uuid' => $uuid]);
  195.                 case $form->get('saveAndDocumentation')->isClicked():
  196.                     return $this->redirectToRoute('Study-documentation', ['uuid' => $uuid]);
  197.                 case $form->get('saveAndTheory')->isClicked():
  198.                     return $this->redirectToRoute('Study-theory', ['uuid' => $uuid]);
  199.                 case $form->get('saveAndMethod')->isClicked():
  200.                     return $this->redirectToRoute('Study-method', ['uuid' => $uuid]);
  201.                 case $form->get('saveAndMeasure')->isClicked():
  202.                     return $this->redirectToRoute('Study-measure', ['uuid' => $uuid]);
  203.                 case $form->get('saveAndSample')->isClicked():
  204.                     return $this->redirectToRoute('Study-sample', ['uuid' => $uuid]);
  205.                 case $form->get('saveAndDatasets')->isClicked():
  206.                     return $this->redirectToRoute('Study-datasets', ['uuid' => $uuid]);
  207.                 case $form->get('saveAndMaterials')->isClicked():
  208.                     return $this->redirectToRoute('Study-materials', ['uuid' => $uuid]);
  209.                 case $form->get('saveAndReview')->isClicked():
  210.                     return $this->redirectToRoute('Study-review', ['uuid' => $uuid]);
  211.                 case $form->get('saveAndExport')->isClicked():
  212.                     return $this->redirectToRoute('export_index', ['uuid' => $uuid]);
  213.                 case $form->get('saveAndSettings')->isClicked():
  214.                     return $this->redirectToRoute('Study-settings', ['uuid' => $uuid]);
  215.             }
  216.         }
  217.         return $this->render('Pages/Study/theory.html.twig', [
  218.             'form' => $form->createView(),
  219.             'experiment' => $entityAtChange,
  220.         ]);
  221.     }
  222.     /**
  223.      * @Route("/{uuid}/sample", name="sample")
  224.      *
  225.      * @param string $uuid
  226.      * @param Request $request
  227.      * @return Response
  228.      */
  229.     public function sampleAction(string $uuidRequest $request): Response
  230.     {
  231.         $this->logger->debug("Enter StudyController::sampleAction with [UUID: $uuid]");
  232.         $entityAtChange $this->getEntityAtChange($uuid);
  233.         $sampleGroup $entityAtChange->getSampleMetaDataGroup();
  234.         $sampleGroup->setPopulation($this->prepareEmptyArray($sampleGroup->getPopulation()));
  235.         $sampleGroup->setInclusionCriteria($this->prepareEmptyArray($sampleGroup->getInclusionCriteria()));
  236.         $sampleGroup->setExclusionCriteria($this->prepareEmptyArray($sampleGroup->getExclusionCriteria()));
  237.         $form $this->questionnaire->askAndHandle($entityAtChange->getSampleMetaDataGroup(), 'save'$request);
  238.         if ($this->questionnaire->isSubmittedAndValid($form)) {
  239.             $formData $form->getData();
  240.             $formData->setPopulation(array_values($formData->getPopulation()));
  241.             $formData->setInclusionCriteria(array_values($formData->getInclusionCriteria()));
  242.             $formData->setExclusionCriteria(array_values($formData->getExclusionCriteria()));
  243.             $this->em->persist($formData);
  244.             $this->em->flush();
  245.             switch (true) {
  246.                 case $form->get('saveAndPrevious')->isClicked():
  247.                     return $this->redirectToRoute('Study-measure', ['uuid' => $uuid]);
  248.                 case $form->get('saveAndIntroduction')->isClicked():
  249.                     return $this->redirectToRoute('Study-introduction', ['uuid' => $uuid]);
  250.                 case $form->get('saveAndDocumentation')->isClicked():
  251.                     return $this->redirectToRoute('Study-documentation', ['uuid' => $uuid]);
  252.                 case $form->get('saveAndTheory')->isClicked():
  253.                     return $this->redirectToRoute('Study-theory', ['uuid' => $uuid]);
  254.                 case $form->get('saveAndMethod')->isClicked():
  255.                     return $this->redirectToRoute('Study-method', ['uuid' => $uuid]);
  256.                 case $form->get('saveAndMeasure')->isClicked():
  257.                     return $this->redirectToRoute('Study-measure', ['uuid' => $uuid]);
  258.                 case $form->get('saveAndSample')->isClicked():
  259.                     return $this->redirectToRoute('Study-sample', ['uuid' => $uuid]);
  260.                 case $form->get('saveAndDatasets')->isClicked():
  261.                     return $this->redirectToRoute('Study-datasets', ['uuid' => $uuid]);
  262.                 case $form->get('saveAndMaterials')->isClicked():
  263.                     return $this->redirectToRoute('Study-materials', ['uuid' => $uuid]);
  264.                 case $form->get('saveAndReview')->isClicked():
  265.                     return $this->redirectToRoute('Study-review', ['uuid' => $uuid]);
  266.                 case $form->get('saveAndExport')->isClicked():
  267.                     return $this->redirectToRoute('export_index', ['uuid' => $uuid]);
  268.                 case $form->get('saveAndSettings')->isClicked():
  269.                     return $this->redirectToRoute('Study-settings', ['uuid' => $uuid]);
  270.             }
  271.         }
  272.         return $this->render('Pages/Study/sample.html.twig', [
  273.             'form' => $form->createView(),
  274.             'experiment' => $entityAtChange,
  275.         ]);
  276.     }
  277.     /**
  278.      * @Route("/{uuid}/measure", name="measure")
  279.      *
  280.      * @param string $uuid
  281.      * @param Request $request
  282.      * @return Response
  283.      */
  284.     public function measureAction(string $uuidRequest $request): Response
  285.     {
  286.         $this->logger->debug("Enter StudyController::measureAction with [UUID: $uuid]");
  287.         $entityAtChange $this->getEntityAtChange($uuid);
  288.         $entityAtChange->getMeasureMetaDataGroup()->setMeasures($this->prepareEmptyArray($entityAtChange->getMeasureMetaDataGroup()->getMeasures()));
  289.         $entityAtChange->getMeasureMetaDataGroup()->setApparatus($this->prepareEmptyArray($entityAtChange->getMeasureMetaDataGroup()->getApparatus()));
  290.         $form $this->questionnaire->askAndHandle($entityAtChange->getMeasureMetaDataGroup(), 'save'$request);
  291.         if ($this->questionnaire->isSubmittedAndValid($form)) {
  292.             $formData $form->getData();
  293.             $formData->setApparatus(array_filter($formData->getApparatus()));
  294.             $formData->setMeasures(array_filter($formData->getMeasures()));
  295.             $this->em->persist($formData);
  296.             $this->em->flush();
  297.             switch (true) {
  298.                 case $form->get('saveAndPrevious')->isClicked():
  299.                     return $this->redirectToRoute('Study-method', ['uuid' => $uuid]);
  300.                 case $form->get('saveAndNext')->isClicked():
  301.                     return $this->redirectToRoute('Study-sample', ['uuid' => $uuid]);
  302.                 case $form->get('saveAndIntroduction')->isClicked():
  303.                     return $this->redirectToRoute('Study-introduction', ['uuid' => $uuid]);
  304.                 case $form->get('saveAndDocumentation')->isClicked():
  305.                     return $this->redirectToRoute('Study-documentation', ['uuid' => $uuid]);
  306.                 case $form->get('saveAndTheory')->isClicked():
  307.                     return $this->redirectToRoute('Study-theory', ['uuid' => $uuid]);
  308.                 case $form->get('saveAndMethod')->isClicked():
  309.                     return $this->redirectToRoute('Study-method', ['uuid' => $uuid]);
  310.                 case $form->get('saveAndMeasure')->isClicked():
  311.                     return $this->redirectToRoute('Study-measure', ['uuid' => $uuid]);
  312.                 case $form->get('saveAndSample')->isClicked():
  313.                     return $this->redirectToRoute('Study-sample', ['uuid' => $uuid]);
  314.                 case $form->get('saveAndDatasets')->isClicked():
  315.                     return $this->redirectToRoute('Study-datasets', ['uuid' => $uuid]);
  316.                 case $form->get('saveAndMaterials')->isClicked():
  317.                     return $this->redirectToRoute('Study-materials', ['uuid' => $uuid]);
  318.                 case $form->get('saveAndReview')->isClicked():
  319.                     return $this->redirectToRoute('Study-review', ['uuid' => $uuid]);
  320.                 case $form->get('saveAndExport')->isClicked():
  321.                     return $this->redirectToRoute('export_index', ['uuid' => $uuid]);
  322.                 case $form->get('saveAndSettings')->isClicked():
  323.                     return $this->redirectToRoute('Study-settings', ['uuid' => $uuid]);
  324.             }
  325.         }
  326.         return $this->render('Pages/Study/measure.html.twig', [
  327.             'form' => $form->createView(),
  328.             'experiment' => $entityAtChange,
  329.         ]);
  330.     }
  331.     /**
  332.      * @Route("/{uuid}/method", name="method")
  333.      *
  334.      * @param string $uuid
  335.      * @param Request $request
  336.      * @return Response
  337.      */
  338.     public function methodAction(string $uuidRequest $request): Response
  339.     {
  340.         $this->logger->debug("Enter StudyController::methodAction with [UUID: $uuid]");
  341.         $entityAtChange $this->getEntityAtChange($uuid);
  342.         $form $this->questionnaire->askAndHandle($entityAtChange->getMethodMetaDataGroup(), 'save'$request);
  343.         if ($this->questionnaire->isSubmittedAndValid($form)) {
  344.             $this->em->persist($entityAtChange);
  345.             $this->em->flush();
  346.             switch (true) {
  347.                 case $form->get('saveAndPrevious')->isClicked():
  348.                     return $this->redirectToRoute('Study-theory', ['uuid' => $uuid]);
  349.                 case $form->get('saveAndNext')->isClicked():
  350.                     return $this->redirectToRoute('Study-measure', ['uuid' => $uuid]);
  351.                 case $form->get('saveAndIntroduction')->isClicked():
  352.                     return $this->redirectToRoute('Study-introduction', ['uuid' => $uuid]);
  353.                 case $form->get('saveAndDocumentation')->isClicked():
  354.                     return $this->redirectToRoute('Study-documentation', ['uuid' => $uuid]);
  355.                 case $form->get('saveAndTheory')->isClicked():
  356.                     return $this->redirectToRoute('Study-theory', ['uuid' => $uuid]);
  357.                 case $form->get('saveAndMethod')->isClicked():
  358.                     return $this->redirectToRoute('Study-method', ['uuid' => $uuid]);
  359.                 case $form->get('saveAndMeasure')->isClicked():
  360.                     return $this->redirectToRoute('Study-measure', ['uuid' => $uuid]);
  361.                 case $form->get('saveAndSample')->isClicked():
  362.                     return $this->redirectToRoute('Study-sample', ['uuid' => $uuid]);
  363.                 case $form->get('saveAndDatasets')->isClicked():
  364.                     return $this->redirectToRoute('Study-datasets', ['uuid' => $uuid]);
  365.                 case $form->get('saveAndMaterials')->isClicked():
  366.                     return $this->redirectToRoute('Study-materials', ['uuid' => $uuid]);
  367.                 case $form->get('saveAndReview')->isClicked():
  368.                     return $this->redirectToRoute('Study-review', ['uuid' => $uuid]);
  369.                 case $form->get('saveAndExport')->isClicked():
  370.                     return $this->redirectToRoute('export_index', ['uuid' => $uuid]);
  371.                 case $form->get('saveAndSettings')->isClicked():
  372.                     return $this->redirectToRoute('Study-settings', ['uuid' => $uuid]);
  373.             }
  374.         }
  375.         return $this->render('Pages/Study/method.html.twig', [
  376.             'form' => $form->createView(),
  377.             'experiment' => $entityAtChange,
  378.         ]);
  379.     }
  380.     /**
  381.      * @Route("/{uuid}/materials", name="materials")
  382.      *
  383.      * @param string $uuid
  384.      * @return Response
  385.      */
  386.     public function materialsAction(string $uuid): Response
  387.     {
  388.         $this->logger->debug("Enter StudyController::materialsAction with [UUID: $uuid]");
  389.         $entityAtChange $this->getEntityAtChange($uuid);
  390.         return $this->render('Pages/Study/materials.html.twig', [
  391.             'experiment' => $entityAtChange,
  392.         ]);
  393.     }
  394.     /**
  395.      * @Route("/{uuid}/datasets", name="datasets")
  396.      *
  397.      * @param string $uuid
  398.      * @return Response
  399.      */
  400.     public function datasetsAction(string $uuid): Response
  401.     {
  402.         $this->logger->debug("Enter StudyController::datasetsAction with [UUID: $uuid]");
  403.         $entityAtChange $this->getEntityAtChange($uuid);
  404.         return $this->render('Pages/Study/datasets.html.twig', [
  405.             'experiment' => $entityAtChange,
  406.         ]);
  407.     }
  408.     /**
  409.      * @Route("/{uuid}/introduction", name="introduction")
  410.      *
  411.      * @param string $uuid
  412.      * @return Response
  413.      */
  414.     public function introductionAction(string $uuid): Response
  415.     {
  416.         $this->logger->debug("Enter StudyController::introductionAction with [UUID: $uuid]");
  417.         $entityAtChange $this->getEntityAtChange($uuid);
  418.         return $this->render('Pages/Study/introduction.html.twig', [
  419.             'experiment' => $entityAtChange,
  420.         ]);
  421.     }
  422.     /**
  423.      * @Route("/{uuid}/delete", name="delete")
  424.      *
  425.      * @param string $uuid
  426.      * @return Response
  427.      */
  428.     public function deleteAction(string $uuid): Response
  429.     {
  430.         $this->logger->debug("Enter StudyController::deleteAction with [UUID: $uuid]");
  431.         $entityAtChange $this->getEntityAtChange($uuid);
  432.         $result $this->crud->deleteStudy($entityAtChange);
  433.         return $this->redirectToRoute('Study-overview');
  434.     }
  435.     protected function getEntityAtChange(string $uuidstring $className Experiment::class)
  436.     {
  437.         return $this->em->getRepository($className)->find($uuid);
  438.     }
  439.     /**
  440.      * @param array|null $array
  441.      * @return array|string[]
  442.      */
  443.     private function prepareEmptyArray(?array $array): array
  444.     {
  445.         if (null === $array || >= sizeof($array)) {
  446.             $array = array("");
  447.         }
  448.         return $array;
  449.     }
  450. }