src/Controller/COMUNICACIONES/Announcement.php line 100

Open in your IDE?
  1. <?php
  2. namespace App\Controller\COMUNICACIONES;
  3. use App\Controller\_USER\Nuser;
  4. use App\Controller\RequestTransform;
  5. use App\Entity\Msg;
  6. use App\Entity\MsgEntry;
  7. use App\Entity\Usuario;
  8. use App\Repository\BaseRepository;
  9. use App\Repository\Sql\CursosRepository;
  10. use App\Repository\Sql\MsgRepository;
  11. use App\Repository\Sql\TemaRepository;
  12. use App\Services\Files\FileUploader;
  13. use App\Services\MsgService\MsgService;
  14. use App\Services\Password\EncoderService;
  15. use App\Services\SqlService\SqlService;
  16. use App\Services\TemaService\TemaService;
  17. use App\Services\UserService\UserService;
  18. use Doctrine\ORM\EntityManagerInterface;
  19. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  20. use Symfony\Component\HttpFoundation\JsonResponse;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\Mailer\MailerInterface;
  23. use Symfony\Component\Routing\Annotation\Route;
  24. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  25. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  26. use Symfony\Component\Serializer\Serializer;
  27. class Announcement extends AbstractController
  28. {
  29.     private $cursosRepository;
  30.     private $temaService;
  31.     private $msgRepository;
  32.     private $temaRepository;
  33.     private $encoderService;
  34.     private $em;
  35.     private $nuser;
  36.     private $msgService;
  37.     private $sqlService;
  38.     private $userService;
  39.     private $mailer;
  40.     public function __construct(
  41.         EntityManagerInterface $em,
  42.         EncoderService $encoderService,
  43.         CursosRepository $cursosRepository,
  44.         MsgRepository $msgRepository,
  45.         TemaRepository $temaRepository,
  46.         Nuser $nuser,
  47.         TemaService $temaService,
  48.         MsgService $msgService,
  49.         SqlService $sqlService,
  50.         UserService $userService,
  51.         MailerInterface $mailer
  52.     ) {
  53.         $this->nuser $nuser;
  54.         $this->em $em;
  55.         $this->encoderService $encoderService;
  56.         $this->msgRepository $msgRepository;
  57.         $this->temaService $temaService;
  58.         $this->msgService $msgService;
  59.         $this->cursosRepository $cursosRepository;
  60.         $this->temaRepository $temaRepository;
  61.         $this->sqlService $sqlService;
  62.         $this->userService $userService;
  63.         $this->mailer $mailer;
  64.         $encoders = [new JsonEncoder()];
  65.         $normalizers = [new ObjectNormalizer()];
  66.         $this->serializer = new Serializer($normalizers$encoders);
  67.     }
  68.     /**
  69.      * Show a particular user announcements with no update.
  70.      *
  71.      * @Route("/api/msg/user/announcement/list", name="get_msg_user_announcement_list", methods={"POST"})
  72.      */
  73.     public function getUserAnnouncementList(Request $request): JsonResponse
  74.     {
  75.         $userInformation $this->userService->decodeToken($request);
  76.         $pageNumber RequestTransform::getRequiredField($request'page');
  77.         $category RequestTransform::getRequiredField($request'category'falsetrue) ?: null;
  78.         $course RequestTransform::getRequiredField($request'course'falsetrue) ?: null;
  79.         $msgs $this->msgService->getUserAnnouncementList($userInformation['uuid'], $course$category$pageNumber);
  80.         return new JsonResponse([
  81.             'code' => $msgs['code'],
  82.             'message' => 'get_msg_user_announcement_list',
  83.             'id' => 200 == $msgs['code'] ? $this->serializer->serialize($msgs['id'], 'json') : $msgs['id'],
  84.         ]);
  85.     }
  86.     /**
  87.      * Get the number of new announcements for a specific user with no update to read.
  88.      *
  89.      * @Route("/api/anuncio/new", name="anuncio_new_announcement", methods={"POST"})
  90.      */
  91.     public function userAnnouncementsList(Request $request): JsonResponse
  92.     {
  93.         $userInformation $this->userService->decodeToken($request);
  94.         $course RequestTransform::getRequiredField($request'course'falsetrue) ?: null;
  95.         $announcementsAndNews $this->msgService->getAnnouncementsWithNews($userInformation['uuid'], $coursenulltrue);
  96.         if (!is_null($announcementsAndNews)) {
  97.             return new JsonResponse([
  98.                 'code' => 200,
  99.                 'message' => 'anuncio_new_announcement',
  100.                 'result' => $announcementsAndNews,
  101.             ]);
  102.         }
  103.         return new JsonResponse([
  104.             'code' => 400,
  105.             'message' => 'anuncio_new_announcement',
  106.             'result' => [],
  107.         ]);
  108.     }
  109.     /**
  110.      * Show a list of announcements.
  111.      *
  112.      * @Route("/api/msg/admin/announcement/list", name="get_msg_admin_announcement_list", methods={"POST"})
  113.      */
  114.     public function getAdminAnnouncementList(Request $request): JsonResponse
  115.     {
  116.         $userInformation $this->userService->decodeToken($request);
  117.         $pageNumber RequestTransform::getRequiredField($request'page');
  118.         $category RequestTransform::getRequiredField($request'category'falsetrue) ?: null;
  119.         $search RequestTransform::getRequiredField($request'search'falsetrue) ?: null;
  120.         $msgs $this->msgService->getAdminAnnouncementList($userInformation['uuid'], $category$pageNumber$search);
  121.         return new JsonResponse([
  122.             'code' => $msgs['code'],
  123.             'message' => 'get_msg_user_announcement_list',
  124.             'id' => 200 == $msgs['code'] ? $this->serializer->serialize($msgs['id'], 'json') : $msgs['id'],
  125.         ]);
  126.     }
  127.     /**
  128.      * Show communications with teachers, secretaries and admins.
  129.      *
  130.      * @param Request $request when consulting a message
  131.      *
  132.      * @Route("/api/msg/admin/doubt/list", name="get_msg_admin_doubt_list", methods={"POST"})
  133.      */
  134.     public function getMsgAdminDoubtList(Request $request): JsonResponse
  135.     {
  136.         $userInformation $this->userService->decodeToken($request);
  137.         $pageNumber RequestTransform::getRequiredField($request'page');
  138.         $msgType RequestTransform::getRequiredField($request'type');
  139.         $msgStatus RequestTransform::getRequiredField($request'status');
  140.         $area RequestTransform::getRequiredField($request'area'falsetrue) ?: null;
  141.         $sede RequestTransform::getRequiredField($request'sede'falsetrue) ?: null;
  142.         $seeAsTutor RequestTransform::getRequiredField($request'seeAsTutor'falsetrue) ?: null;
  143.         $msgs $this->msgService->getAdminDoubtsList($userInformation['uuid'], $pageNumber$msgType$msgStatus$area$sede$seeAsTutor);
  144.         return new JsonResponse([
  145.             'code' => $msgs['code'],
  146.             'message' => 'get_msg_admin_doubt_list',
  147.             'id' => 200 == $msgs['code'] ? $this->serializer->serialize($msgs['id'], 'json') : $msgs['id'],
  148.         ]);
  149.     }
  150.     /**
  151.      * Show communications created.
  152.      *
  153.      * @Route("/api/msg/user/doubt/list", name="get_msg_user_doubt_list", methods={"POST"})
  154.      */
  155.     public function getMsgUserDoubtList(Request $request): JsonResponse
  156.     {
  157.         $userInformation $this->userService->decodeToken($request);
  158.         $pageNumber RequestTransform::getRequiredField($request'page');
  159.         $msgStatus RequestTransform::getRequiredField($request'status');
  160.         $msgs $this->msgService->getUserDoubtsList($userInformation['uuid'], $pageNumber$msgStatus);
  161.         return new JsonResponse([
  162.             'code' => $msgs['code'],
  163.             'message' => 'get_msg_user_doubt_list',
  164.             'id' => 200 == $msgs['code'] ? $this->serializer->serialize($msgs['id'], 'json') : $msgs['id'],
  165.         ]);
  166.     }
  167.     /**
  168.      * Details about an announcement.
  169.      *
  170.      * @param string $msg_id ID of the message
  171.      *
  172.      * @Route("/api/details/announcement/{msg_id}", name="announcement_get_details", methods={"GET"})
  173.      */
  174.     public function showDetailsAnnouncement(string $msg_id): JsonResponse
  175.     {
  176.         $announcement $this->msgService->getAnnouncementDetails($msg_id);
  177.         if ($announcement) {
  178.             return new JsonResponse([
  179.                 'code' => 200,
  180.                 'message' => 'announcement_get_details',
  181.                 'result' => $announcement,
  182.             ]);
  183.         }
  184.         return new JsonResponse([
  185.             'code' => 404,
  186.             'message' => 'announcement_get_details',
  187.             'result' => $announcement,
  188.         ]);
  189.     }
  190.     /**
  191.      * Details about an announcement.
  192.      *
  193.      * @param string $msg_id ID of the message
  194.      *
  195.      * @Route("/api/msg/user/comunication/{course_id}", name="comunication_get_access", methods={"GET"})
  196.      */
  197.     public function hasCourseAccessComunication(string $course_id): JsonResponse
  198.     {
  199.         $courses explode(','$course_id);
  200.         $access $this->msgService->hasCourseAccessComunication($courses);
  201.         if ($access) {
  202.             return new JsonResponse([
  203.                 'code' => 200,
  204.                 'message' => 'comunication_get_access',
  205.                 'result' => $access,
  206.             ]);
  207.         }
  208.         return new JsonResponse([
  209.             'code' => 404,
  210.             'message' => 'comunication_get_access',
  211.             'result' => $access,
  212.         ]);
  213.     }
  214.     /**
  215.      * Details about an announcement.
  216.      *
  217.      * @param string $msg_id ID of the message
  218.      *
  219.      * @Route("/api/msg/user/all/comunication", name="comunication_get_user_access", methods={"GET"})
  220.      */
  221.     public function getUserAccessComunication(Request $request): JsonResponse
  222.     {
  223.         $userInformation $this->userService->decodeToken($request);
  224.         $access $this->msgService->getUserCoursesAccess($userInformation['uuid']);
  225.         if ($access) {
  226.             return new JsonResponse([
  227.                 'code' => 200,
  228.                 'message' => 'comunication_get_user_access',
  229.                 'result' => $access,
  230.             ]);
  231.         }
  232.         return new JsonResponse([
  233.             'code' => 404,
  234.             'message' => 'comunication_get_user_access',
  235.             'result' => $access,
  236.         ]);
  237.     }
  238.     /**
  239.      * Details about a conversation (doubt).
  240.      *
  241.      * @param string  $msgId   ID of the message
  242.      * @param Request $request when consulting a message
  243.      *
  244.      * @Route("/api/msg/doubt/details/{msgId}", name="get_msg_doubt_details", methods={"GET"})
  245.      */
  246.     public function getMsgDoubtDetails(string $msgIdRequest $request): JsonResponse
  247.     {
  248.         $userInformation $this->userService->decodeToken($request);
  249.         $msgs $this->msgService->getDoubtDetails($msgId$userInformation['uuid']);
  250.         return new JsonResponse([
  251.             'code' => $msgs['code'],
  252.             'message' => 'get_msg_doubt_details',
  253.             'result' => $msgs['result'],
  254.         ]);
  255.     }
  256.     /**
  257.      * Auxiliary functions for creating messages.
  258.      *
  259.      * @param $msg Msg msg object to creat or edit
  260.      * @param $sentBy Usuario user that created the message
  261.      * @param $type String type of message: announcement, doubt or feedback
  262.      * @param $title String title of the message
  263.      * @param $message String content of the message
  264.      * @param $keyValueArray array for msg KV
  265.      * @param $request Request when a new announcement is submitted
  266.      * @param $repository BaseRepository to make SQL calls using ORM
  267.      * @param $sqlService SqlService to make SQL calls
  268.      *
  269.      * @return array
  270.      */
  271.     private function createNewMessage(Msg $msgUsuario $sentBystring $typestring $titlestring $message, array $keyValueArrayRequest $requestBaseRepository $repositorySqlService $sqlService)
  272.     {
  273.         $exception = [];
  274.         $today = new \DateTime();
  275.         $createdBy null == $msg->getCreatedBy() ? $sentBy $msg->getCreatedBy();
  276.         $createdAt null == $msg->getCreatedAt() ? $today $msg->getCreatedAt();
  277.         // Message information
  278.         $msg->setCreatedBy($createdBy);
  279.         $msg->setCreatedAt($createdAt);
  280.         $msg->setModifiedAt($today);
  281.         $msg->setTitle($title);
  282.         $msg->setType($type);
  283.         $exception_msg $repository->add($msg);
  284.         if (200 == $exception_msg['code']) {
  285.             // Message Entry information
  286.             $msgEntry $repository->isId($requestMsgEntry::class);
  287.             $msgEntry->setIdMsg($msg);
  288.             $msgEntry->setCreatedBy($createdBy);
  289.             $msgEntry->setModifiedBy($sentBy);
  290.             $msgEntry->setCreatedAt($createdAt);
  291.             $msgEntry->setModifiedAt($today);
  292.             $msgEntry->setText($message);
  293.             $exception_msgEntry $repository->add($msgEntry);
  294.             if (200 == $exception_msgEntry['code']) {
  295.                 // Para cada clave valor crear un insert en Msg_Entry (con su clave, valor).
  296.                 $exception_msgKv $sqlService->generateMsgKvSql($msg$keyValueArray);
  297.                 if (200 == $exception_msgKv[0]['code']) {
  298.                     // Exception message if exit
  299.                     $exception = [
  300.                         'code' => 200,
  301.                         'message' => 'success',
  302.                         'id' => [
  303.                             'msg' => $exception_msg['id'],
  304.                             'msgEntry' => $exception_msgEntry['id'],
  305.                         ],
  306.                     ];
  307.                     $msgKv_ids = [];
  308.                     foreach ($exception_msgKv as $insert) {
  309.                         if (200 != $insert['code']) {
  310.                             return new JsonResponse([
  311.                                 'code' => $insert['code'],
  312.                                 'message' => $insert['message'],
  313.                                 'id' => $insert['id'],
  314.                             ]);
  315.                         } else {
  316.                             array_push($msgKv_ids$insert['id']);
  317.                         }
  318.                     }
  319.                     $exception['id']['msgKv'] = $msgKv_ids;
  320.                 }
  321.             }
  322.         }
  323.         return $exception;
  324.     }
  325.     /**
  326.      * Create a new Announcement.
  327.      *
  328.      * @param $request Request when a new announcement is submitted
  329.      *
  330.      * @Route("/api/anuncio/add", name="anuncio_add", methods={"POST"})
  331.      * @Route("/api/anuncio/edit/{id}", name="anuncio_edit", methods={"POST"})
  332.      */
  333.     public function newAnnouncement(Request $request): JsonResponse
  334.     {
  335.         $title RequestTransform::getRequiredField($request'title');
  336.         $message RequestTransform::getRequiredField($request'message');
  337.         $category RequestTransform::getRequiredField($request'category');
  338.         $dateStart RequestTransform::getRequiredField($request'dateStart');
  339.         $dateEnd RequestTransform::getRequiredField($request'dateEnd'falsetrue) ?: null;
  340.         $showOnPanel RequestTransform::getRequiredField($request'showOnPanel');
  341.         $sendEmail RequestTransform::getRequiredField($request'sendEmail');
  342.         $isEnabled RequestTransform::getRequiredField($request'isEnabled');
  343.         $sedes RequestTransform::getRequiredField($request'sedes'truetrue) ?: null;
  344.         $areas RequestTransform::getRequiredField($request'areas'truetrue) ?: null;
  345.         $currentCursos RequestTransform::getRequiredField($request'currentCursos'truetrue);
  346.         $previousCursos RequestTransform::getRequiredField($request'previousCursos'truetrue);
  347.         $nextCursos RequestTransform::getRequiredField($request'nextCursos'truetrue);
  348.         $superPromotions RequestTransform::getRequiredField($request'superPromotions'truetrue);
  349.         $users RequestTransform::getRequiredField($request'users') ?: null;
  350.         $msgId RequestTransform::getRequiredField($request'id'falsetrue) ?: null;
  351.         $filesToServer RequestTransform::getRequiredField($request'fichero'falsetrue);
  352.         $filesFromServer RequestTransform::getRequiredField($request'filesFromServer'truetrue);
  353.         $userInformation $this->userService->decodeToken($request);
  354.         $cursos array_merge($currentCursos$previousCursos$nextCursos) ?: null;
  355.         $keyValueArray = [
  356.             'inicio' => date('d-m-Y'strtotime($dateStart)),
  357.             'fin' => $dateEnd date('d-m-Y'strtotime($dateEnd)) : null,
  358.             'habilitado' => $isEnabled 0,
  359.             'visualizar' => $showOnPanel 0,
  360.             'email' => $sendEmail 0,
  361.             'user' => $users,
  362.             'category' => $category,
  363.             'curso' => $cursos,
  364.             'area' => $areas,
  365.             'sede' => $sedes,
  366.             'super_promocion' => $superPromotions,
  367.             'filesToServer' => $filesToServer ?: null,
  368.             'fichero' => $filesFromServer,
  369.         ];
  370.         $announcement $this->msgService->addNewAnnouncement($userInformation['uuid'], 0$title$message$keyValueArray$msgId$filesToServer);
  371.         if ($announcement) {
  372.             return new JsonResponse([
  373.                 'code' => 200,
  374.                 'message' => 'anuncio_add',
  375.                 'id' => $announcement,
  376.             ]);
  377.         }
  378.         return new JsonResponse([
  379.             'code' => 400,
  380.             'message' => 'anuncio_add',
  381.             'id' => $announcement,
  382.         ]);
  383.     }
  384.     /**
  385.      * Upload a file to a announcement.
  386.      *
  387.      * @param Request $request from the multi-file-upload.server
  388.      *
  389.      * @Route("/api/anuncio/add/image", name="fichero_anuncio", methods={"POST"})
  390.      */
  391.     public function announcementFiles(Request $request): JsonResponse
  392.     {
  393.         $title $request->request->get('title');
  394.         $message $request->request->get('message');
  395.         $array_key_values $request->request->get('key_value_array');
  396.         $uploadedFile $request->files->get('picture');
  397.         $me_id $request->request->get('id');
  398.         if ($uploadedFile) {
  399.             foreach ($uploadedFile as $key => $value) {
  400.                 $e[] = $this->msgService->uploadFileToAnnouncement($value$me_id);
  401.             }
  402.             $array = (array) json_decode($array_key_values);
  403.             if ($title && $message && $array && $e) {
  404.                 $this->msgService->sendEmail($title$message$array$e);
  405.             }
  406.             if ($e) {
  407.                 return new JsonResponse([
  408.                     'code' => 200,
  409.                     'message' => 'fichero_anuncio',
  410.                     'id' => $me_id,
  411.                 ]);
  412.             }
  413.             return new JsonResponse([
  414.                 'code' => 500,
  415.                 'message' => $e->getMessage(),
  416.                 'id' => $me_id,
  417.             ]);
  418.         }
  419.         return new JsonResponse([
  420.             'code' => 404,
  421.             'message' => 'File not found',
  422.             'id' => $me_id,
  423.         ]);
  424.     }
  425.     /**
  426.      * Create a new Message.
  427.      *
  428.      * @param Request        $request    when a new doubt is submitted
  429.      * @param BaseRepository $repository to make SQL calls using ORM
  430.      * @param SqlService     $sqlService to make SQL calls
  431.      *
  432.      * @Route("/api/duda/add", name="duda_add", methods={"POST"})
  433.      */
  434.     public function newDoubt(Request $requestBaseRepository $repositorySqlService $sqlService): JsonResponse
  435.     {
  436.         // Request
  437.         $user_information $this->userService->decodeToken($request);
  438.         $title RequestTransform::getRequiredField($request'title');
  439.         $message RequestTransform::getRequiredField($request'message');
  440.         $addressee RequestTransform::getRequiredField($request'destino');
  441.         $campus RequestTransform::getRequiredField($request'sede');
  442.         $grade RequestTransform::getRequiredField($request'curso');
  443.         $unit RequestTransform::getRequiredField($request'tema');
  444.         $msg $repository->isId($requestMsg::class);
  445.         $sentBy $this->em->getRepository(Usuario::class)->find($user_information['uuid']);
  446.         $keyValueArray = [
  447.             'estado' => 0,
  448.         ];
  449.         switch ($addressee) {
  450.             case 1// Unit doubts
  451.                 $keyValueArray['tema'] = $unit;
  452.                 $keyValueArray['curso'] = $grade;
  453.                 break;
  454.             case 2// Secretary
  455.                 $keyValueArray['sede'] = $campus;
  456.                 break;
  457.         }
  458.         $exception $this->createNewMessage($msg$sentBy$addressee$title$message$keyValueArray$request$repository$sqlService);
  459.         return new JsonResponse($exception);
  460.     }
  461.     /**
  462.      * Create a new feedback message from test.
  463.      *
  464.      * @param Request $request when a new doubt is submitted
  465.      *
  466.      * @Route("/api/retroalimentacion/add", name="feedback_add", methods={"POST"})
  467.      */
  468.     public function newFeedback(Request $request): JsonResponse
  469.     {
  470.         $userInformation $this->userService->decodeToken($request);
  471.         $course RequestTransform::getRequiredField($request'course');
  472.         $questionId RequestTransform::getRequiredField($request'question');
  473.         $testId RequestTransform::getRequiredField($request'test');
  474.         $message RequestTransform::getRequiredField($request'message');
  475.         $feedback_id $this->msgService->addNewFeedback($userInformation['uuid'], $course$questionId$testId$message);
  476.         return new JsonResponse([
  477.             'code' => 200,
  478.             'message' => 'feedback_add',
  479.             'result' => $feedback_id,
  480.         ]);
  481.     }
  482.     /**
  483.      * Upload a file to a msg.
  484.      *
  485.      * @param Request      $request      from the multi-file-upload.server
  486.      * @param FileUploader $fileUploader to upload the image to the server
  487.      *
  488.      * @Route("/api/details/msg/add/image", name="image_msg", methods={"POST"})
  489.      */
  490.     public function msgFiles(Request $requestFileUploader $fileUploader): JsonResponse
  491.     {
  492.         $uploadedFile $request->files->get('picture');
  493.         $me_id $request->request->get('id');
  494.         if ($uploadedFile) {
  495.             try {
  496.                 $urlFileName $fileUploader->upload($uploadedFile'/msg');
  497.                 $me $this->em->getRepository(MsgEntry::class)->find($me_id);
  498.                 $msg_id $me->getIdMsg()->getId();
  499.                 $exception $this->sqlService->genericInsert('msg_kv', [
  500.                     'id_msg_id' => $msg_id,
  501.                     'field' => 'fichero',
  502.                     'value' => $urlFileName,
  503.                 ]);
  504.                 return new JsonResponse([
  505.                     'code' => 200,
  506.                     'message' => $exception,
  507.                 ]);
  508.             } catch (\Exception $e) {
  509.                 return new JsonResponse(['code' => 500'message' => $e->getMessage()]);
  510.             }
  511.         }
  512.         return new JsonResponse(['code' => 404'message' => 'File not found']);
  513.     }
  514.     /**
  515.      * Answer a conversation.
  516.      *
  517.      * @param $request Request when a new reply is submitted
  518.      *
  519.      * @Route("/api/details/msg/add", name="msg_answer", methods={"POST"})
  520.      */
  521.     public function newAnswer(Request $request): JsonResponse
  522.     {
  523.         $user_information $this->userService->decodeToken($request);
  524.         $msg RequestTransform::getRequiredField($request'msg');
  525.         $message RequestTransform::getRequiredField($request'message');
  526.         $msg_entry_id $this->msgService->addMsgEntry($user_information['uuid'], $message$msg);
  527.         return new JsonResponse([
  528.             'code' => 200,
  529.             'message' => 'msg_answer',
  530.             'result' => $msg_entry_id,
  531.         ]);
  532.     }
  533.     /**
  534.      * Answer a conversation and change status to published.
  535.      *
  536.      * @param $request Request when a new reply is submitted
  537.      *
  538.      * @Route("/api/details/msg/add/publish", name="msg_answer_and_publish", methods={"POST"})
  539.      */
  540.     public function addAnswerAndPublish(Request $request): JsonResponse
  541.     {
  542.         $user_information $this->userService->decodeToken($request);
  543.         $msg RequestTransform::getRequiredField($request'msg');
  544.         $message RequestTransform::getRequiredField($request'message');
  545.         $msg_entry_id $this->msgService->addMsgEntry($user_information['uuid'], $message$msg);
  546.         $msg_id $msg['id'];
  547.         $this->msgService->switchMsgPublishStatus($msg_id1);
  548.         return new JsonResponse([
  549.             'code' => 200,
  550.             'message' => 'msg_answer_and_publish',
  551.             'result' => $msg_entry_id,
  552.         ]);
  553.     }
  554.     /**
  555.      * Modify an existing message.
  556.      *
  557.      * @Route("/api/details/msg/edit", name="msg_answer_edit", methods={"PUT"})
  558.      */
  559.     public function editAnswer(Request $request): JsonResponse
  560.     {
  561.         $user_information $this->userService->decodeToken($request);
  562.         $msg_entry_id RequestTransform::getRequiredField($request'id');
  563.         $text RequestTransform::getRequiredField($request'message');
  564.         $updated $this->msgService->editMsgEntry($user_information['uuid'], $text$msg_entry_id);
  565.         if ($updated) {
  566.             return new JsonResponse([
  567.                 'code' => 200,
  568.                 'message' => 'msg_answer_edit',
  569.                 'result' => $msg_entry_id,
  570.             ]);
  571.         }
  572.         return new JsonResponse([
  573.             'code' => 400,
  574.             'message' => 'msg_answer_edit',
  575.             'result' => $msg_entry_id,
  576.         ]);
  577.     }
  578.     /**
  579.      * Publish existing messages.
  580.      *
  581.      * @Route("/api/details/msg/switchPublish", name="msg_switch_publish", methods={"PUT"})
  582.      */
  583.     public function switchPublishAnswers(Request $request): JsonResponse
  584.     {
  585.         $msg_id RequestTransform::getRequiredField($request'msgId');
  586.         $published RequestTransform::getRequiredField($request'estadoPublicacion');
  587.         $updated $this->msgService->switchMsgPublishStatus($msg_id$published);
  588.         if ($updated) {
  589.             return new JsonResponse([
  590.                 'code' => 200,
  591.                 'message' => 'msg_switch_publish',
  592.                 'result' => $msg_id,
  593.             ]);
  594.         }
  595.         return new JsonResponse([
  596.             'code' => 400,
  597.             'message' => 'msg_switch_publish',
  598.             'result' => [],
  599.         ]);
  600.     }
  601.     /**
  602.      * Change message type.
  603.      *
  604.      * @Route("/api/details/msg/changeType", name="msg_change_type", methods={"PUT"})
  605.      */
  606.     public function changeMsgType(Request $request): JsonResponse
  607.     {
  608.         $msg_id RequestTransform::getRequiredField($request'msgId');
  609.         $msg_type RequestTransform::getRequiredField($request'msgType');
  610.         $curso RequestTransform::getRequiredField($request'curso'truetrue);
  611.         $tema RequestTransform::getRequiredField($request'tema'truetrue);
  612.         $sede RequestTransform::getRequiredField($request'sede'truetrue);
  613.         $values = [
  614.             'curso' => $curso,
  615.             'tema' => $tema,
  616.             'sede' => $sede,
  617.         ];
  618.         $type_changed $this->msgService->changeMsgType($msg_id$msg_type$values);
  619.         if ($type_changed) {
  620.             return new JsonResponse([
  621.                 'code' => 200,
  622.                 'message' => 'msg_change_type',
  623.                 'result' => $type_changed,
  624.             ]);
  625.         }
  626.         return new JsonResponse([
  627.             'code' => 400,
  628.             'message' => 'msg_change_type',
  629.             'result' => $type_changed,
  630.         ]);
  631.     }
  632.     /**
  633.      * Change message status.
  634.      *
  635.      * @Route("/api/details/msg/changeStatus", name="msg_change_status", methods={"PUT"})
  636.      */
  637.     public function changeMsgStatus(Request $request): JsonResponse
  638.     {
  639.         $msg_id RequestTransform::getRequiredField($request'msgId');
  640.         $msg_status RequestTransform::getRequiredField($request'msgStatus');
  641.         $status_changed $this->msgService->changeMsgStatus($msg_id$msg_status);
  642.         if ($status_changed) {
  643.             return new JsonResponse([
  644.                 'code' => 200,
  645.                 'message' => 'msg_change_status',
  646.                 'result' => $status_changed,
  647.             ]);
  648.         }
  649.         return new JsonResponse([
  650.             'code' => 400,
  651.             'message' => 'msg_change_status',
  652.             'result' => $status_changed,
  653.         ]);
  654.     }
  655.     /**
  656.      * Switches a Msg (announcement) to be enabled or disabled.
  657.      *
  658.      * @Route("/api/msg/announcement/enable", name="msg_announcement_enable", methods={"PUT"})
  659.      */
  660.     public function switchEnableMsg(Request $request): JsonResponse
  661.     {
  662.         $userInformation $this->userService->decodeToken($request);
  663.         $msgId RequestTransform::getRequiredField($request'msgId');
  664.         $isMsgEnabled $this->msgService->switchMsgEnableStatus($msgId$userInformation['uuid']);
  665.         if (is_null($isMsgEnabled)) {
  666.             return new JsonResponse([
  667.                 'code' => 403,
  668.                 'message' => 'msg_announcement_enable',
  669.                 'result' => [],
  670.             ]);
  671.         }
  672.         return new JsonResponse([
  673.             'code' => 200,
  674.             'message' => 'msg_announcement_enable',
  675.             'result' => $msgId,
  676.         ]);
  677.     }
  678.     /**
  679.      * Deletes a messages with all its entries, reads and kvs.
  680.      *
  681.      * @param string $msg_id Message id
  682.      *
  683.      * @Route("/api/msg/{msg_id}", name="msg_delete" , methods={"DELETE"})
  684.      */
  685.     public function deleteMsg(string $msg_id): JsonResponse
  686.     {
  687.         if ($msg_id) {
  688.             $em $this->getDoctrine()->getManager();
  689.             $msg $em->getRepository(Msg::class)->find($msg_id);
  690.             if ($msg) {
  691.                 $em->remove($msg);
  692.                 $em->flush();
  693.                 $exception = [
  694.                     'code' => 200,
  695.                     'message' => 'success',
  696.                     'id' => $msg_id,
  697.                 ];
  698.             } else {
  699.                 $exception = [
  700.                     'code' => 404,
  701.                     'message' => 'Not found',
  702.                     'id' => $msg_id,
  703.                 ];
  704.             }
  705.             return new JsonResponse($exception);
  706.         }
  707.         return new JsonResponse([
  708.             'code' => 200,
  709.             'message' => 'msg_announcement_enable',
  710.             'result' => 'error',
  711.         ]);
  712.     }
  713.     /**
  714.      * Deletes a Msg.
  715.      *
  716.      * @Route("/api/details/msg/delete", name="msg_delete_bd", methods={"PUT"})
  717.      */
  718.     public function deleteMsgBd(Request $request): JsonResponse
  719.     {
  720.         $userInformation $this->userService->decodeToken($request);
  721.         $msgId RequestTransform::getRequiredField($request'msgId');
  722.         $isMsgEnabled $this->msgService->deleteMsg($msgId$userInformation['uuid']);
  723.         if (is_null($isMsgEnabled)) {
  724.             return new JsonResponse([
  725.                 'code' => 403,
  726.                 'message' => 'msg_announcement_enable',
  727.                 'result' => [],
  728.             ]);
  729.         }
  730.         return new JsonResponse([
  731.             'code' => 200,
  732.             'message' => 'msg_announcement_enable',
  733.             'result' => $msgId,
  734.         ]);
  735.     }
  736.     /**
  737.      * Show feedback list.
  738.      *
  739.      * @Route("/api/msg/feedback/list", name="get_msg_feedback_list", methods={"POST"})
  740.      */
  741.     public function getFeedbackList(Request $request): JsonResponse
  742.     {
  743.         $userInformation $this->userService->decodeToken($request);
  744.         $pageNumber RequestTransform::getRequiredField($request'page');
  745.         $msgStatus RequestTransform::getRequiredField($request'status');
  746.         $area RequestTransform::getRequiredField($request'area'falsetrue) ?: null;
  747.         $sede RequestTransform::getRequiredField($request'sede'falsetrue) ?: null;
  748.         $seeAsTutor RequestTransform::getRequiredField($request'seeAsTutor'falsetrue) ?: null;
  749.         $msgs $this->msgService->getFeedbackList($userInformation['uuid'], $pageNumber$msgStatus$area$sede$seeAsTutor);
  750.         return new JsonResponse([
  751.             'code' => $msgs['code'],
  752.             'message' => 'get_msg_feedback_list',
  753.             'id' => 200 == $msgs['code'] ? $this->serializer->serialize($msgs['id'], 'json') : $msgs['id'],
  754.         ]);
  755.     }
  756.     /**
  757.      * Details about a conversation (feedback).
  758.      *
  759.      * @param string  $msgId   ID of the message
  760.      * @param Request $request when consulting a message
  761.      *
  762.      * @Route("/api/msg/feedback/details/{msgId}", name="get_msg_feedback_details", methods={"GET"})
  763.      */
  764.     public function getFeedbackDetails(string $msgIdRequest $request): JsonResponse
  765.     {
  766.         $userInformation $this->userService->decodeToken($request);
  767.         $msgs $this->msgService->getFeedbackDetails($msgId$userInformation['uuid']);
  768.         return new JsonResponse([
  769.             'code' => $msgs['code'],
  770.             'message' => 'get_msg_feedback_details',
  771.             'result' => $msgs['result'],
  772.         ]);
  773.     }
  774. }