src/Controller/Admin/CourseIndexController.php line 1695

Open in your IDE?
  1. <?php
  2. namespace EADPlataforma\Controller\Admin;
  3. use EADPlataforma\Entity\Course;
  4. use EADPlataforma\Enum\CourseEnum;
  5. use EADPlataforma\Error\ActionInvalidException;
  6. use EADPlataforma\Error\FieldException;
  7. use EADPlataforma\Error\NotFoundException;
  8. use EADPlataforma\Error\PermissionException;
  9. use EADPlataforma\Response\HttpCreated;
  10. use EADPlataforma\Response\HttpNoContent;
  11. use EADPlataforma\Response\HttpOk;
  12. use EADPlataforma\Services\EntityServices\CourseIndexService;
  13. use EADPlataforma\Services\EntityServices\LibraryChapterService;
  14. use EADPlataforma\Services\EntityServices\LessonService;
  15. use EADPlataforma\Services\EntityServices\LessonModuleService;
  16. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  17. use Symfony\Component\HttpFoundation\JsonResponse;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use EADPlataforma\Http\EADResponse;
  21. use EADPlataforma\Entity\CourseTeam;
  22. use EADPlataforma\Entity\Category;
  23. use EADPlataforma\Entity\Product;
  24. use EADPlataforma\Entity\ProductOffer;
  25. use EADPlataforma\Entity\ProductTeam;
  26. use EADPlataforma\Entity\Lesson;
  27. use EADPlataforma\Entity\LessonSupport;
  28. use EADPlataforma\Entity\LessonLog;
  29. use EADPlataforma\Entity\LessonLogOrigin;
  30. use EADPlataforma\Entity\LessonModule;
  31. use EADPlataforma\Entity\LessonAnnotation;
  32. use EADPlataforma\Entity\Library;
  33. use EADPlataforma\Entity\LessonXLibrary;
  34. use EADPlataforma\Entity\LikeControl;
  35. use EADPlataforma\Entity\CourseCertificateTemplate;
  36. use EADPlataforma\Entity\Enrollment;
  37. use EADPlataforma\Entity\Exam;
  38. use EADPlataforma\Entity\ExamUser;
  39. use EADPlataforma\Entity\ExamUserAnswer;
  40. use EADPlataforma\Entity\ExamUserAnswerOption;
  41. use EADPlataforma\Entity\Question;
  42. use EADPlataforma\Entity\QuestionOption;
  43. use EADPlataforma\Entity\CourseTestimonial;
  44. use EADPlataforma\Entity\User;
  45. use EADPlataforma\Entity\UserSubscription;
  46. use EADPlataforma\Enum\CourseTeamEnum;
  47. use EADPlataforma\Enum\ProductEnum;
  48. use EADPlataforma\Enum\EnrollmentEnum;
  49. use EADPlataforma\Enum\ExamEnum;
  50. use EADPlataforma\Enum\ExamUserEnum;
  51. use EADPlataforma\Enum\LessonEnum;
  52. use EADPlataforma\Enum\LessonSupportEnum;
  53. use EADPlataforma\Enum\LibraryEnum;
  54. use EADPlataforma\Enum\LikeControlEnum;
  55. use EADPlataforma\Enum\LessonXLibraryEnum;
  56. use EADPlataforma\Enum\LessonAnnotationEnum;
  57. use EADPlataforma\Enum\LessonModuleEnum;
  58. use EADPlataforma\Enum\ExamUserAnswerEnum;
  59. use EADPlataforma\Enum\QuestionEnum;
  60. use EADPlataforma\Enum\QuestionOptionEnum;
  61. use EADPlataforma\Enum\CourseTestimonialEnum;
  62. use EADPlataforma\Enum\NotificationEnum;
  63. use EADPlataforma\Enum\WebhookEnum;
  64. use EADPlataforma\Enum\TrashEnum;
  65. use EADPlataforma\Enum\ErrorEnum;
  66. /**
  67.  * @Route(
  68.  *      path          = "",
  69.  *      schemes         = {"http|https"}
  70.  * )
  71.  * @Cache(
  72.  *      maxage          = "0",
  73.  *      smaxage         = "0",
  74.  *      expires         = "now",
  75.  *      public          = false
  76.  * )
  77.  */
  78. class CourseIndexController extends AbstractController {
  79.     public function getEntityClass(){
  80.         return Course::class;
  81.     }
  82.     /**
  83.      * @Route(
  84.      *      path          = "/admin/v2/general/{id}",
  85.      *      methods       = {"GET"},
  86.      *      requirements  = { "id" = "\d+" }
  87.      * )
  88.      * 
  89.      * @throws NotFoundException
  90.      * @throws ActionInvalidException
  91.      */
  92.     public function getCourseNewGeneral(
  93.         Request $request
  94.         CourseIndexService $courseIndexService
  95.     ): JsonResponse
  96.     {
  97.         $courseId $request->get('id');
  98.         $course $courseIndexService->searchCourse($courseId);
  99.         if(!$course){
  100.             throw new NotFoundException("NotFoundException");
  101.         }
  102.         $enrollment $courseIndexService->searchEnrollment($course);
  103.         if(!$enrollment){
  104.             throw new ActionInvalidException("ActionInvalidException");
  105.         }
  106.         $isStudent $courseIndexService->isStudent($course);
  107.         if($isStudent && $course->getStatus() == CourseEnum::DRAFT){
  108.             throw new ActionInvalidException("ActionInvalidException");
  109.         }
  110.         $data $courseIndexService->getCourseNewGeneral($course$enrollment);
  111.         return new HttpOk($data);
  112.     }
  113.     /**
  114.      * @Route(
  115.      *      path          = "/admin/v2/course/{id}",
  116.      *      methods       = {"GET"},
  117.      *      requirements  = { "id" = "\d+" }
  118.      * )
  119.      * 
  120.      * @throws NotFoundException
  121.      * @throws ActionInvalidException
  122.      */
  123.     public function getCourseNewIndex(
  124.         Request $request,
  125.         CourseIndexService $courseIndexService
  126.     ): JsonResponse
  127.     {
  128.         
  129.         $courseId $request->get('id');
  130.         $course $courseIndexService->searchCourse($courseId);
  131.         if(!$course){
  132.             throw new NotFoundException("NotFoundException");
  133.         }
  134.         $enrollment $courseIndexService->searchEnrollment($course);
  135.         if(!$enrollment){
  136.             throw new ActionInvalidException("ActionInvalidException");
  137.         }
  138.         $isStudent $courseIndexService->isStudent($course);
  139.         if($isStudent && $course->getStatus() == CourseEnum::DRAFT){
  140.             throw new ActionInvalidException("ActionInvalidException");
  141.         }
  142.         $data $courseIndexService->getCourseIndex($enrollment);
  143.         return new HttpOk($data);
  144.     }
  145.     /**
  146.      * @Route(
  147.      *      path          = "/admin/v2/module/{id}",
  148.      *      methods       = {"GET"},
  149.      *      requirements  = { "id" = "\d+" }
  150.      * )
  151.      * 
  152.      * @throws NotFoundException
  153.      * @throws ActionInvalidException
  154.      */
  155.     public function getModuleNewIndex(
  156.         Request $request,
  157.         CourseIndexService $courseIndexService,
  158.         LessonModuleService $lessonModuleService
  159.     ): JsonResponse
  160.     {
  161.         $moduleId $request->get('id');
  162.         $lessonModule $lessonModuleService->searchModule($moduleId);
  163.         if(!$lessonModule){
  164.             throw new NotFoundException("NotFoundException");
  165.         }
  166.         $course $lessonModule->getCourse();
  167.         $enrollment $courseIndexService->searchEnrollment($course);
  168.         if(!$enrollment){
  169.             throw new ActionInvalidException("ActionInvalidException");
  170.         }
  171.         $isStudent $courseIndexService->isStudent($course);
  172.         if($isStudent && $course->getStatus() == CourseEnum::DRAFT){
  173.             throw new ActionInvalidException("ActionInvalidException");
  174.         }
  175.         $data $lessonModuleService->getModuleIndex(
  176.             $enrollment,
  177.             $lessonModule,
  178.             $isStudent
  179.         );
  180.         return new HttpOk($data);
  181.     }
  182.     /**
  183.      * @Route(
  184.      *      path          = "/admin/v2/course/{id}/modules",
  185.      *      methods       = {"GET"},
  186.      *      requirements  = { "id" = "\d+" }
  187.      * )
  188.      *
  189.      * @throws NotFoundException
  190.      * @throws ActionInvalidException
  191.      */
  192.     public function getModules(
  193.         Request $request,
  194.         CourseIndexService $courseIndexService,
  195.         LessonModuleService $lessonModuleService
  196.     ): JsonResponse
  197.     {
  198.         
  199.         $courseId $request->get('id');
  200.         $course $courseIndexService->searchCourse($courseId);
  201.         if(!$course){
  202.             throw new NotFoundException("NotFoundException");
  203.         }
  204.         $enrollment $courseIndexService->searchEnrollment($course);
  205.         if(!$enrollment){
  206.             throw new ActionInvalidException("ActionInvalidException");
  207.         }
  208.         $isStudent $courseIndexService->isStudent($course);
  209.         if($isStudent && $course->getStatus() == CourseEnum::DRAFT){
  210.             throw new ActionInvalidException("ActionInvalidException");
  211.         }
  212.         $searchText $request->get('search');
  213.         $data $lessonModuleService->getModulesIndex(
  214.             $enrollment,
  215.             $isStudent,
  216.             $searchText
  217.         );
  218.         return new HttpOk($data);
  219.     }
  220.     /**
  221.      * @Route(
  222.      *      path          = "/admin/v2/lesson/{id}",
  223.      *      methods       = {"GET"},
  224.      *      name          = "getViewLessonNew",
  225.      *      requirements  = { "id" = "\d+" }
  226.      * )
  227.      * 
  228.      * @throws NotFoundException
  229.      * @throws ActionInvalidException
  230.      */
  231.     public function getViewLessonNew(
  232.         Request $request,
  233.         CourseIndexService $courseIndexService,
  234.         LessonService $lessonService,
  235.         LibraryChapterService $chapterService
  236.     ): JsonResponse
  237.     {
  238.         $lessonId $request->get('id');
  239.         $lesson $lessonService->searchLesson($lessonId);
  240.         if (!$lesson) {
  241.             throw new NotFoundException("NotFoundException");
  242.         }
  243.         $course $lesson->getCourse();
  244.         if(
  245.             $courseIndexService->isEnrollment($course) && 
  246.             !$courseIndexService->isValidEnrollment($course)
  247.         ){
  248.             throw new ActionInvalidException("ActionInvalidException", [
  249.                 "expired" => true,
  250.             ]);
  251.         }
  252.         //check user can access lesson
  253.         if(!$lessonService->isLessonVisibleToStudent($lesson)){
  254.             throw new ActionInvalidException("ActionInvalidException");
  255.         }
  256.         
  257.         $enrollment $courseIndexService->searchEnrollment($course);
  258.         if(!$enrollment){
  259.             throw new ActionInvalidException("ActionInvalidException");
  260.         }
  261.         $isStudent $courseIndexService->isStudent($course);
  262.         $lessonIsAccessible $lessonService->checkLessonIsAccessibleToUser(
  263.             $lesson,
  264.             $enrollment,
  265.             $isStudent
  266.         );
  267.         if(!$lessonIsAccessible->isAccessible){
  268.             throw new ActionInvalidException($lessonIsAccessible->message);
  269.         }
  270.         $data $lessonService->getLessonIndex(
  271.             $lesson,
  272.             $enrollment,
  273.             $isStudent
  274.         );
  275.         return new HttpOk($data);
  276.     }
  277.     /**
  278.      * @Route(
  279.      *      path          = "/admin/v2/{module}/{id}/{type}",
  280.      *      methods       = {"GET"},
  281.      *      name          = "getExamNewIndex",
  282.      *      requirements  = {"type"="exam|quiz"}
  283.      * )
  284.      */
  285.     public function getExamNewIndex(Request $request)
  286.     {
  287.         if(!$this->configuration->isModuleActive("exam_module")){
  288.             throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException");
  289.         }
  290.         $idModule = (int)$request->get('id');
  291.         $module $request->get('module');
  292.         $type $request->get('type');
  293.         $class = [
  294.             "lesson" => Lesson::class,
  295.             "module" => LessonModule::class,
  296.             "course" => Course::class,
  297.         ];
  298.         if(!isset($class[$module])){
  299.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  300.         }
  301.         $itemRepository $this->em->getRepository($class[$module]);
  302.         $item $itemRepository->findOneBy([
  303.             "id" => $idModule,
  304.             "deleted" => CourseEnum::ITEM_NO_DELETED
  305.         ]);
  306.         //check item exist
  307.         if(!$item){
  308.             //redirect to index or home
  309.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  310.         }
  311.         $course $item;
  312.         if(!($course instanceof Course)){
  313.             $course $item->getCourse();
  314.         }
  315.         
  316.         $enrollmentRepository $this->em->getRepository(Enrollment::class);
  317.         $enrollment $enrollmentRepository->findOneBy([
  318.             "user" => $this->user->getId(),
  319.             "course" => $course->getId(),
  320.             "deleted" => LessonEnum::ITEM_NO_DELETED
  321.         ], [ "id" => "DESC" ]);
  322.         if(!$enrollment){
  323.             throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException");
  324.         }
  325.         $filterExam = [
  326.             "course" => $course->getId(),
  327.             "deleted" => ExamEnum::ITEM_NO_DELETED
  328.         ];
  329.         if($item instanceof Course){
  330.             $filterExam["type"] = ExamEnum::COURSE;
  331.         }else if($item instanceof LessonModule){
  332.             $filterExam["lessonModule"] = $item->getId();
  333.             $filterExam["type"] = ExamEnum::MODULE;
  334.         }else if($item instanceof Lesson){
  335.             //check user can access lesson
  336.             if(!$itemRepository->isLessonVisibleToStudent($item)){
  337.                 throw new \EADPlataforma\Error\ActionInvalidException(
  338.                     "ActionInvalidException"
  339.                 );
  340.             }
  341.             $lessonModule $item->getLessonModule();
  342.             $filterExam["lesson"] = $item->getId();
  343.             $filterExam["lessonModule"] = $lessonModule->getId();
  344.             $filterExam["type"] = ($type == "quiz" ExamEnum::QUIZ ExamEnum::LESSON );
  345.         }
  346.         $isStudent $this->repository->isStudent($course);
  347.         if($isStudent){
  348.             $filterExam["status"] = ExamEnum::PUBLISHED;
  349.         }
  350.         $examRepository $this->em->getRepository(Exam::class);
  351.         $examUserRepository $this->em->getRepository(ExamUser::class);
  352.         $exam $examRepository->findOneBy($filterExam);
  353.         if(!$exam){
  354.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  355.         }
  356.         $examUser $examUserRepository->findOneBy([
  357.             "user" => $this->user->getId(),
  358.             "exam" => $exam->getId(),
  359.             "inactive" => ExamUserEnum::NO,
  360.             "deleted" => ExamUserEnum::ITEM_NO_DELETED
  361.         ]);
  362.         $infoAccess $examRepository->checkExamIsAccessible($exam$examUser);
  363.         if(!$infoAccess->isAccessible){
  364.             throw new \EADPlataforma\Error\ActionInvalidException($infoAccess->message);
  365.         }
  366.         $data $examUserRepository->getDataIndexNew($exam$this->user);
  367.         
  368.         return $this->eadResponseNew($data);
  369.     }
  370.     /**
  371.      * @Route(
  372.      *      path          = "/admin/v2/{module}/{id}/exam/start",
  373.      *      methods       = {"PATCH"},
  374.      *      name          = "startExamNewIndex",
  375.      *      requirements  = { "id" = "\d+" }
  376.      * )
  377.      */
  378.     public function startExamNewIndex(Request $request)
  379.     {
  380.         if(!$this->configuration->isModuleActive("exam_module")){
  381.             throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException");
  382.         }
  383.         $idModule = (int)$request->get('id');
  384.         $module $request->get('module');
  385.         $class = [
  386.             "lesson" => Lesson::class,
  387.             "module" => LessonModule::class,
  388.             "course" => Course::class,
  389.         ];
  390.         if(!isset($class[$module])){
  391.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  392.         }
  393.         $itemRepository $this->em->getRepository($class[$module]);
  394.         $item $itemRepository->findOneBy([
  395.             "id" => $idModule,
  396.             "deleted" => CourseEnum::ITEM_NO_DELETED
  397.         ]);
  398.         //check item exist
  399.         if(!$item){
  400.             //redirect to index or home
  401.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  402.         }
  403.         $course $item;
  404.         if(!($course instanceof Course)){
  405.             $course $item->getCourse();
  406.         }
  407.         $user $this->user;
  408.         $enrollmentRepository $this->em->getRepository(Enrollment::class);
  409.         $enrollment $enrollmentRepository->findOneBy([
  410.             "user" => $user->getId(),
  411.             "course" => $course->getId(),
  412.             "deleted" => LessonEnum::ITEM_NO_DELETED
  413.         ], [ "id" => "DESC" ]);
  414.         if(!$enrollment){
  415.             throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException");
  416.         }
  417.         $filterExam = [
  418.             "course" => $course->getId(),
  419.             "deleted" => ExamEnum::ITEM_NO_DELETED
  420.         ];
  421.         if($item instanceof Course){
  422.             $filterExam["type"] = ExamEnum::COURSE;
  423.         }else if($item instanceof LessonModule){
  424.             $filterExam["lessonModule"] = $item->getId();
  425.             $filterExam["type"] = ExamEnum::MODULE;
  426.         }else if($item instanceof Lesson){
  427.             //check user can access lesson
  428.             if(!$itemRepository->isLessonVisibleToStudent($item)){
  429.                 throw new \EADPlataforma\Error\ActionInvalidException(
  430.                     "ActionInvalidException"
  431.                 );
  432.             }
  433.             $lessonModule $item->getLessonModule();
  434.             $filterExam["lesson"] = $item->getId();
  435.             $filterExam["lessonModule"] = $lessonModule->getId();
  436.             $filterExam["type"] = ExamEnum::LESSON;
  437.         }
  438.         $isStudent $this->repository->isStudent($course);
  439.         if($isStudent){
  440.             $filterExam["status"] = ExamEnum::PUBLISHED;
  441.         }
  442.         $examRepository $this->em->getRepository(Exam::class);
  443.         $exam $examRepository->findOneBy($filterExam);
  444.         if(!$exam){
  445.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  446.         }
  447.         $examUserRepository $this->em->getRepository(ExamUser::class);
  448.         
  449.         $examUser $examUserRepository->findOneBy([
  450.             "user" => $user->getId(),
  451.             "exam" => $exam->getId(),
  452.             "inactive" => ExamUserEnum::NO,
  453.             "deleted" => ExamUserEnum::ITEM_NO_DELETED
  454.         ]);
  455.         $infoAccess $examRepository->checkExamIsAccessible($exam$examUser);
  456.         if(!$infoAccess->isAccessible){
  457.             throw new \EADPlataforma\Error\ActionInvalidException($infoAccess->message);
  458.         }
  459.         if($examUser){
  460.             if(
  461.                 $examUser->getStatus() == ExamUserEnum::DISAPPROVED || 
  462.                 $examUser->getStatus() == ExamUserEnum::TIMEOUT
  463.             ){
  464.                 $examUser $examUserRepository->getValidExamUserById(
  465.                     $examUser->getId(), 
  466.                     true
  467.                 );
  468.                 
  469.                 $attemptsInfo $examUserRepository->getAttemptsInfo($exam$examUser);
  470.                 
  471.                 if($attemptsInfo->attempts == ExamEnum::YES){
  472.                     $examUser->setInactive(ExamUserEnum::YES);
  473.                     $this->em->flush();
  474.                     $examUser null;
  475.                 }
  476.             }
  477.         }
  478.         if(!$examUser){
  479.             $examUser = new ExamUser();
  480.             $examUser->setExam($exam);
  481.             $examUser->setCourse($exam->getCourse());
  482.             $examUser->setUser($user);
  483.             $this->em->persist($examUser);
  484.             $this->em->flush();
  485.         }else{
  486.             throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException");
  487.         }
  488.         $data $examUserRepository->getDataIndexNew($exam$user);
  489.         return $this->eadResponseNew($data);
  490.     }
  491.     /**
  492.      * @Route(
  493.      *      path          = "/admin/v2/{module}/{id}/exam/{questionId}",
  494.      *      methods       = {"POST"},
  495.      * )
  496.      */
  497.     public function registerAnswerExamQuestion(Request $request) {
  498.         if(!$this->configuration->isModuleActive("exam_module")){
  499.             throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException");
  500.         }
  501.         $this->requestUtil->setRequest($request)->setData();
  502.         $idModule = (int)$request->get('id');
  503.         $module $request->get('module');
  504.         $questionId = (int)$request->get('questionId');
  505.         $class = [
  506.             "lesson" => Lesson::class,
  507.             "module" => LessonModule::class,
  508.             "course" => Course::class,
  509.         ];
  510.         if(!isset($class[$module])){
  511.             throw new \EADPlataforma\Error\NotFoundException("Module");
  512.         }
  513.         $examUserAnswerRepository $this->em->getRepository(ExamUserAnswer::class);
  514.         $examUserRepository $this->em->getRepository(ExamUser::class);
  515.         $enrollmentRepository $this->em->getRepository(Enrollment::class);
  516.         $examRepository $this->em->getRepository(Exam::class);
  517.         $questionRepository $this->em->getRepository(Question::class);
  518.         $questionOptionRepository $this->em->getRepository(QuestionOption::class);
  519.         $itemRepository $this->em->getRepository($class[$module]);
  520.         $item $itemRepository->findOneBy([
  521.             "id" => $idModule,
  522.             "deleted" => CourseEnum::ITEM_NO_DELETED
  523.         ]);
  524.         //check item exist
  525.         if(!$item){
  526.             //redirect to index or home
  527.             throw new \EADPlataforma\Error\NotFoundException("Item");
  528.         }
  529.         $course $item;
  530.         if(!($course instanceof Course)){
  531.             $course $item->getCourse();
  532.         }
  533.         $user $this->user;
  534.         $enrollment $enrollmentRepository->findOneBy([
  535.             "user" => $user->getId(),
  536.             "course" => $course->getId(),
  537.             "deleted" => LessonEnum::ITEM_NO_DELETED
  538.         ], [ "id" => "DESC" ]);
  539.         if(!$enrollment){
  540.             throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException");
  541.         }
  542.         $filterExam = [
  543.             "course" => $course->getId(),
  544.             "deleted" => ExamEnum::ITEM_NO_DELETED
  545.         ];
  546.         if($item instanceof Course){
  547.             $filterExam["type"] = ExamEnum::COURSE;
  548.         }else if($item instanceof LessonModule){
  549.             $filterExam["lessonModule"] = $item->getId();
  550.             $filterExam["type"] = ExamEnum::MODULE;
  551.         }else if($item instanceof Lesson){
  552.             //check user can access lesson
  553.             if(!$itemRepository->isLessonVisibleToStudent($item)){
  554.                 throw new \EADPlataforma\Error\ActionInvalidException(
  555.                     "ActionInvalidException"
  556.                 );
  557.             }
  558.             $lessonModule $item->getLessonModule();
  559.             $filterExam["lesson"] = $item->getId();
  560.             $filterExam["lessonModule"] = $lessonModule->getId();
  561.             $filterExam["type"] = ExamEnum::LESSON;
  562.         }
  563.         $isStudent $this->repository->isStudent($course);
  564.         if($isStudent){
  565.             $filterExam["status"] = ExamEnum::PUBLISHED;
  566.         }
  567.         $exam $examRepository->findOneBy($filterExam);
  568.         if(!$exam){
  569.             throw new \EADPlataforma\Error\NotFoundException("Exam");
  570.         }
  571.         
  572.         $question $questionRepository->findOneBy([
  573.             "id" => $questionId,
  574.             "deleted" => ExamUserAnswerEnum::ITEM_NO_DELETED
  575.         ]);
  576.         if(!$question){
  577.             throw new \EADPlataforma\Error\NotFoundException("Question");
  578.         }
  579.         $examUser $examUserRepository->findOneBy([
  580.             "user" => $user->getId(),
  581.             "exam" => $exam->getId(),
  582.             "inactive" => ExamUserEnum::NO,
  583.             "deleted" => ExamUserEnum::ITEM_NO_DELETED
  584.         ]);
  585.         $examUser $examUserRepository->getValidExamUserById(nulltrue$examUser);
  586.         if (!$examUser) {
  587.             throw new \EADPlataforma\Error\NotFoundException("ExamUser");
  588.         }
  589.         $expired $examUserRepository->examUserIsExpired($examUser);
  590.         if($expired){
  591.             throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException 1");
  592.         }
  593.         //check exist answer to this question
  594.         $questionAnswer $examUserAnswerRepository->findOneBy([
  595.             "deleted" => ExamUserAnswerEnum::ITEM_NO_DELETED,
  596.             "examUser" => $examUser->getId(),
  597.             "question" => $questionId,
  598.         ]);
  599.         if($questionAnswer){
  600.             throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException 2");
  601.         }
  602.         $answer $this->requestUtil->getField('answer');
  603.         if(empty($answer)){
  604.             throw new \EADPlataforma\Error\FieldException(
  605.                 "FieldException",
  606.                 [ 
  607.                     "answer" => "Value not found"
  608.                 ]
  609.             );
  610.         }
  611.         $questionAnswer = new ExamUserAnswer();
  612.         $questionAnswer->setExamUser($examUser);
  613.         $questionAnswer->setAnswered(QuestionEnum::YES);
  614.         $questionAnswer->setQuestion($question);
  615.         if($question->getType() == QuestionEnum::DISSERTATION){
  616.             $questionAnswer->setAnswer($answer);
  617.         }
  618.         $errors $this->validateEntity($questionAnswer);
  619.         if($errors){
  620.             throw new \EADPlataforma\Error\FieldException("FieldException"$errors);
  621.         }
  622.         $this->em->persist($questionAnswer);
  623.         $grade ExamUserAnswerEnum::GRADE_INCORRECT;
  624.         if($question->getType() != QuestionEnum::DISSERTATION){
  625.             $questionAnswer->setEvaluated((int)$question->getAnswered());
  626.             $auxMultiple = [];
  627.             $totalCorrect $questionOptionRepository->count([
  628.                 "question" => $question->getId(),
  629.                 "correct" => QuestionEnum::YES,
  630.                 "deleted" => ExamUserAnswerEnum::ITEM_NO_DELETED,
  631.             ]);
  632.             $options json_decode($this->requestUtil->getField('answer'));
  633.             
  634.             foreach ($options as $key => $option) {
  635.                 $questionOption $questionOptionRepository->findOneBy([
  636.                     "id" => $option,
  637.                     "question" => $question->getId(),
  638.                     "deleted" => ExamUserAnswerEnum::ITEM_NO_DELETED,
  639.                 ]);
  640.                 if($questionOption){
  641.                     if($questionOption->getCorrect() == QuestionEnum::YES){
  642.                         if($question->getType() == QuestionEnum::ALTERNATIVE){
  643.                             $grade ExamUserAnswerEnum::GRADE_CORRECT;
  644.                         }else if(
  645.                             $question->getType() == QuestionEnum::MULTIPLE_ALTERNATIVE
  646.                         ){
  647.                             $auxMultiple[] = $option;
  648.                         }
  649.                     }
  650.                     $questionAnswerOption = new ExamUserAnswerOption();
  651.                     $questionAnswerOption->setMarked(QuestionEnum::YES);
  652.                     $questionAnswerOption->setExamUserAnswer($questionAnswer);
  653.                     $questionAnswerOption->setQuestionOption($questionOption);
  654.                     $this->em->persist($questionAnswerOption);
  655.                 }
  656.             }
  657.             if($question->getType() == QuestionEnum::MULTIPLE_ALTERNATIVE){
  658.                 $numCorrectMarked count($auxMultiple);
  659.                 if($totalCorrect == $numCorrectMarked){
  660.                     $grade ExamUserAnswerEnum::GRADE_CORRECT;
  661.                 }else if($numCorrectMarked QuestionEnum::NO){
  662.                     $grade ExamUserAnswerEnum::GRADE_CORRECT_PARTIAL;
  663.                 }
  664.             }
  665.         }
  666.         $questionAnswer->setGrade($grade);
  667.         $this->em->flush();
  668.         sleep(0.5);
  669.         $questionNumberAnswer $examUserAnswerRepository->count([
  670.             "examUser" => $examUser->getId(),
  671.             "deleted" => ExamUserAnswerEnum::ITEM_NO_DELETED,
  672.             "answered" => QuestionEnum::YES
  673.         ]);
  674.         $questionNumber $exam->getQuestionNumber();
  675.         $questionNumberReal $exam->getQuestionNumberReal();
  676.         if($questionNumber $questionNumberReal){
  677.             $questionNumber $questionNumberReal;
  678.         }
  679.         if($exam->getQuestionOrder() == ExamEnum::SEQUENTIAL){
  680.             $questionNumber count($exam->getLiveQuestion());
  681.         }
  682.         if($questionNumber <= $questionNumberAnswer){
  683.             $examUserRepository->updateExamUser($examUser);
  684.         }
  685.         $data $questionAnswer->toReturn();
  686.         $this->userLogService->logInsert("exam_user_answer"$questionAnswer->getId(), $data);
  687.         $data $examUserRepository->getDataIndexNew($exam$user);
  688.         return $this->eadResponseNew($data);
  689.     }
  690.     /**
  691.      * @Route(
  692.      *      path          = "/admin/v2/lesson/{id}/quiz/{questionId}",
  693.      *      methods       = {"POST"}
  694.      * )
  695.      */
  696.     public function getQuestionQuizAnswer(Request $request) {
  697.         $questionRepository $this->em->getRepository(Question::class);
  698.         $questionOptionRepository $this->em->getRepository(QuestionOption::class);
  699.         $libraryRepository $this->em->getRepository(Library::class);
  700.         $this->requestUtil->setRequest($request)->setData();
  701.         $questionId = (int)$request->get('questionId');
  702.         $lessonId = (int)$request->get('id');
  703.         
  704.         $lessonRepository $this->em->getRepository(Lesson::class);
  705.         $lesson $lessonRepository->findOneBy([
  706.             "id" => $lessonId,
  707.             "deleted" => LessonEnum::ITEM_NO_DELETED
  708.         ]);
  709.         //check item exist
  710.         if(!$lesson){
  711.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  712.         }
  713.         $question $questionRepository->findOneBy([
  714.             "id" => $questionId,
  715.             "deleted" => QuestionEnum::ITEM_NO_DELETED
  716.         ]);
  717.         if(!$question){
  718.             throw new \EADPlataforma\Error\NotFoundException("Exam");
  719.         }
  720.         $optionCorrect $questionOptionRepository->findOneBy([
  721.             "deleted" => QuestionOptionEnum::ITEM_NO_DELETED,
  722.             "question" => $questionId,
  723.             "correct" => QuestionOptionEnum::YES,
  724.         ]);
  725.         $libraryReturn null;
  726.         $library $question->getLibrary();
  727.         if($library){
  728.             $libraryInfo $libraryRepository->getContentInfo($libraryfalse$lesson);
  729.             $libraryReturn = (object)[
  730.                 "id" => $library->getId(),
  731.                 "title" => $library->getTitle(),
  732.                 "type" => $library->getType(),
  733.                 "link" => $libraryInfo->url,
  734.                 "text" => $library->getText(),
  735.                 "file" => $library->getId(),
  736.                 "fileExtension" => $library->getFileExtension(),
  737.                 "liveStart" => $library->getLiveStart(),
  738.                 "liveEmbedId" => $libraryInfo->embedId,
  739.                 "pagesNumber" => $library->getPagesNumber(),
  740.                 "duration" => $library->getDuration(),
  741.             ];
  742.             $showDocument = ($lesson->getControlShowDocument() == LibraryEnum::YES);
  743.             $drmVideo $this->configuration->get("drm_video");
  744.             $hasLessonControl $this->configuration->checkModuleIsAbleOnPlan(
  745.                 'lessonControlFunction'
  746.             );
  747.             $annotate = [];
  748.             if($hasLessonControl && $showDocument && $drmVideo == LibraryEnum::YES){
  749.                 $annotate $libraryRepository->getVideoDRM($this->user);
  750.             }
  751.             $credentials $libraryRepository->getVideoCredentials(
  752.                 $library,
  753.                 $annotate
  754.             );
  755.             if($credentials){
  756.                 $libraryReturn->credentials $credentials;
  757.             }
  758.         }
  759.         $data = [
  760.             "library" => $libraryReturn,
  761.             "optionCorrect" => ( $optionCorrect $optionCorrect->getId() : null ),
  762.         ];
  763.         return $this->eadResponseNew($data);
  764.     }
  765.     /**
  766.      * @Route(
  767.      *      path          = "/admin/v2/lesson/{lessonId}/files",
  768.      *      methods       = {"GET"}
  769.      * )
  770.      */
  771.     public function getLessonFiles(Request $request) {
  772.         
  773.         $lessonRepository $this->em->getRepository(Lesson::class);
  774.         $lessonXLibraryRepository $this->em->getRepository(LessonXLibrary::class);
  775.         $lessonId $request->get('lessonId');
  776.         $lesson $lessonRepository->findOneBy([
  777.             "id" => $lessonId,
  778.             "deleted" => LessonEnum::ITEM_NO_DELETED
  779.         ]);
  780.         //check lesson exist
  781.         if (!$lesson) {
  782.             //redirect to index or home
  783.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  784.         }
  785.         //check user can access lesson
  786.         if(!$lessonRepository->isLessonVisibleToStudent($lesson)){
  787.             throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException");
  788.         }
  789.         $files $lessonXLibraryRepository->getLessonXLibraryFiles($lesson);
  790.         
  791.         $data = [];
  792.         foreach ($files as $key => $file) {
  793.             $file $file->toReturn();
  794.             
  795.             $data[] = (object)[
  796.                 "name" => $file->libraryTitle,
  797.                 "size" => $file->libraryFileSize,
  798.                 "type" => $file->libraryType,
  799.                 "filename" => $file->libraryFile,
  800.                 "id" => $file->id,
  801.             ];
  802.         }
  803.         return $this->eadResponseNew($data);
  804.     }
  805.     /**
  806.      * @Route(
  807.      *      path          = "/admin/v2/lesson/{lessonId}/files/{id}",
  808.      *      methods       = {"GET"},
  809.      *      name          = "downloadLessonLibraryNew",
  810.      *      requirements  = { "id" = "\d+" }
  811.      * )
  812.      */
  813.     public function downloadLessonLibraryNew(Request $request) {
  814.         $lessonXLibraryId $request->get('id');
  815.         $lessonXLibrary $this->em->getRepository(LessonXLibrary::class)->findOneBy([
  816.             "id" => $lessonXLibraryId,
  817.             "type" => LessonXLibraryEnum::DOWNLOAD,
  818.             "deleted" => LessonXLibraryEnum::ITEM_NO_DELETED,
  819.         ]);
  820.         if (!$lessonXLibrary) {
  821.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  822.         }
  823.         
  824.         $library $lessonXLibrary->getLibrary();
  825.         $lesson $lessonXLibrary->getLesson();
  826.         $lessonRepository $this->em->getRepository(Lesson::class);
  827.         
  828.         //check user can access lesson
  829.         if(!$lessonRepository->isLessonVisibleToStudent($lesson)){
  830.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  831.         }
  832.         if($library){
  833.             $url null;
  834.             $type $library->getType();
  835.             if($type == LibraryEnum::CONTENT_VIDEO_FILE ||
  836.                 $type == LibraryEnum::CONTENT_AUDIO){
  837.                 $vdocipherService $this->generalService->getService('VdocipherService');
  838.                 $url $vdocipherService->getVideoDownloadLink($library->getVdocipherVideoId());
  839.                 
  840.             }else if($type == LibraryEnum::CONTENT_FILES){
  841.                 
  842.                 $isPdf = ($library->getFileExtension() == "pdf");
  843.                 $hasFunction $this->configuration->checkModuleIsAbleOnPlan(
  844.                     'lessonControlFunction'
  845.                 );
  846.                 $showDocument = (
  847.                     $lessonXLibrary->getControlShowDocument() == LibraryEnum::YES
  848.                 );
  849.                 
  850.                 if($isPdf && $hasFunction && $showDocument){
  851.                     
  852.                     $stringUtil $this->generalService->getUtil('StringUtil');
  853.                     $sessionHash $this->user->getSession()->getToken();
  854.                     $sessionHash $stringUtil->encodeHex($sessionHashfalsefalse);
  855.                     $path $this->generalService->generateUrl(
  856.                         "downloadPdfLessonLibrary"
  857.                         [ 
  858.                             "id" => $lessonXLibraryId,
  859.                             "sessionHash" => $sessionHash
  860.                         ]
  861.                     );
  862.                     $domain = (
  863.                         $request $request->getHost() : $this->client->getDomainPrimary()
  864.                     );
  865.                     $url "https://{$domain}{$path}";
  866.                 }else{
  867.                     $fileName LibraryEnum::PATH_LESSON_FILE "/{$library->getFileName()}";
  868.                     $this->fileService->setFile($fileName);
  869.                     $url $this->fileService->getFileUrlTemp();
  870.                 }
  871.             }
  872.             if($url){
  873.                 $oldDownload $library->getDownloadNumber();
  874.                 $newDownload $oldDownload 1;
  875.                 $library->setDownloadNumber($newDownload);
  876.                 $this->em->flush();
  877.                 return $this->eadResponseNew([ "url" => $url ]);
  878.             }
  879.         }
  880.         throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  881.     }
  882.     /**
  883.      * @Route(
  884.      *      path          = "/admin/v2/course/{courseId}/notes",
  885.      *      name          = "getCourseLessonNotesNew",
  886.      *      methods       = {"GET"},
  887.      * )
  888.      */
  889.     public function getCourseLessonNotesNew(Request $request) {
  890.         $this->requestUtil->setRequest($request)->setData();
  891.         $courseId $request->get('courseId');
  892.         $course $this->repository->findOneBy([
  893.             "id" => $courseId,
  894.             "deleted" => CourseEnum::ITEM_NO_DELETED
  895.         ]);
  896.         
  897.         if(!$course){
  898.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  899.         }
  900.         $isInTeam $this->em->getRepository(CourseTeam::class)->userExistInCourseTeam(
  901.             $course
  902.             $this->user
  903.         );
  904.         $enrollmentRepository $this->em->getRepository(Enrollment::class);
  905.         $enrollment $enrollmentRepository->findOneBy([
  906.             "user" => $this->user->getId(),
  907.             "course" => $courseId,
  908.             "deleted" => EnrollmentEnum::ITEM_NO_DELETED,
  909.         ], [ "id" => "DESC" ]);
  910.         $permission $this->userPermissionUtil->getPermission("course""see");
  911.         
  912.         if(!$enrollment){
  913.             if($course->getFree() == CourseEnum::NO){
  914.                 if($this->userPermissionUtil->isLow($permission)){
  915.                     throw new \EADPlataforma\Error\PermissionException("PermissionException");
  916.                 }
  917.                 if(!$isInTeam && $this->userPermissionUtil->isMiddle($permission)){
  918.                     throw new \EADPlataforma\Error\PermissionException("PermissionException");
  919.                 }
  920.             }
  921.             $enrollmentService $this->generalService->getService('EnrollmentService');
  922.             $info $enrollmentService->enrollUser($this->user$course);
  923.             
  924.             if(!$info->errors){
  925.                 $enrollment $info->enrollment;
  926.             }
  927.         }
  928.         if($this->userPermissionUtil->isLow($permission) && !$isInTeam){
  929.             if(!$enrollment){
  930.                 throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException");
  931.             }
  932.             if($course->getStatus() == CourseEnum::DRAFT){
  933.                 throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException");
  934.             }
  935.         }
  936.         $searchText $this->requestUtil->getField('search');
  937.         $lessonAnnotationRepository $this->em->getRepository(LessonAnnotation::class);
  938.         $notes $lessonAnnotationRepository->getLessonAnnotationsByCourse(
  939.             $course,
  940.             $this->user
  941.             $searchText
  942.         );
  943.         $aux = [];
  944.         $auxLessons = [];
  945.         foreach ($notes as $key => $note) {
  946.             $lesson $note->getLesson();
  947.             $lessonModule $lesson->getLessonModule();
  948.             if(!isset($aux[$lessonModule->getId()])){
  949.                 $aux[$lessonModule->getId()] = (object)[
  950.                     "id" => $lessonModule->getId(),
  951.                     "status" => $lessonModule->getStatus(),
  952.                     "title" => $lessonModule->getTitle(),
  953.                     "description" => $lessonModule->getDescription(),
  954.                     "exam" => null,
  955.                     "lessons" => [],
  956.                 ];
  957.             }
  958.             $library $lesson->getLibrary();
  959.             $libraryRepository $this->em->getRepository(Library::class);
  960.             $timeUtil $this->generalService->getUtil('DateTimeUtil');
  961.             $duration $library->getDuration();
  962.             if(!isset($aux[$lesson->getId()])){
  963.                 $auxLessons[$lesson->getId()] = (object)[
  964.                     "id" => $lesson->getId(),
  965.                     "lessonModule" => $lessonModule->getId(),
  966.                     "title" => $lesson->getTitle(),
  967.                     "status" => $lesson->getStatus(),
  968.                     "lessonIsAccessible" => true,
  969.                     "acessMessage" => null,
  970.                     "contentPagesNumber" => null,
  971.                     "contentDuration" => $duration $timeUtil->timeToSec($duration) : null,
  972.                     "contentType" => null,
  973.                     "contentThumb" => $libraryRepository->getCover($library),
  974.                     "notes" => [],
  975.                     "exam" => null,
  976.                     "quiz" => null,
  977.                     "allowCheck" => EnrollmentEnum::YES,
  978.                 ];
  979.             }
  980.             $auxLessons[$lesson->getId()]->notes[] = (object)[
  981.                 "id" => $note->getId(),
  982.                 "annotation" => $note->getAnnotation(),
  983.                 "options" => $note->getOptions(),
  984.                 "time" => $note->getTime(),
  985.                 "date" => $note->getDate(),
  986.             ];
  987.         }
  988.         $data = [];
  989.         foreach ($auxLessons as $key => $value) {
  990.             if(isset($aux[$value->lessonModule])){
  991.                 $aux[$value->lessonModule]->lessons[] = $value;
  992.                 unset($value->lessonModule);
  993.             }
  994.         }
  995.         foreach ($aux as $key => $value) {
  996.             $data[] = $value;
  997.         }
  998.         return $this->eadResponseNew($data);
  999.     }
  1000.     /**
  1001.      * @Route(
  1002.      *      path          = "/admin/v2/lesson/{lessonId}/notes",
  1003.      *      name          = "lessonAnnotationListNew",
  1004.      *      methods       = {"GET"},
  1005.      * )
  1006.      */
  1007.     public function lessonAnnotationListNew(Request $request) {
  1008.         $lessonId $request->get('lessonId');
  1009.         $lessonRepository $this->em->getRepository(Lesson::class);
  1010.         $lesson $lessonRepository->findOneBy([
  1011.             "id" => $lessonId,
  1012.             "deleted" => LessonEnum::ITEM_NO_DELETED
  1013.         ]);
  1014.         //check lesson exist
  1015.         if (!$lesson) {
  1016.             //redirect to index or home
  1017.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  1018.         }
  1019.         //check user can access lesson
  1020.         if(!$lessonRepository->isLessonVisibleToStudent($lesson)){
  1021.             throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException");
  1022.         }
  1023.         $lessonAnnotationRepository $this->em->getRepository(LessonAnnotation::class);
  1024.         
  1025.         $annotations $lessonAnnotationRepository->findBy([
  1026.             "deleted" => LessonAnnotationEnum::ITEM_NO_DELETED,
  1027.             "lesson" => $lessonId,
  1028.             "user" => $this->user->getId(),
  1029.         ], [ "id" => "DESC" ]);
  1030.         $data = [];
  1031.         foreach ($annotations as $key => $annotation) {
  1032.             $data[] = (object)[
  1033.                 "id" => $annotation->getId(),
  1034.                 "annotation" => $annotation->getAnnotation(),
  1035.                 "options" => $annotation->getOptions(),
  1036.                 "time" => $annotation->getTime(),
  1037.                 "date" => $annotation->getDate(),
  1038.             ];
  1039.         }
  1040.         return $this->eadResponseNew($data);
  1041.     }
  1042.     /**
  1043.      * @Route(
  1044.      *      path          = "/admin/v2/lesson/{lessonId}/notes",
  1045.      *      name          = "registerLessonAnnotationNew",
  1046.      *      methods       = {"POST"},
  1047.      * )
  1048.      */
  1049.     public function registerLessonAnnotationNew(Request $request) {
  1050.         $lessonId $request->get('lessonId');
  1051.         $lessonRepository $this->em->getRepository(Lesson::class);
  1052.         $lesson $lessonRepository->findOneBy([
  1053.             "id" => $lessonId,
  1054.             "deleted" => LessonEnum::ITEM_NO_DELETED
  1055.         ]);
  1056.         //check lesson exist
  1057.         if (!$lesson) {
  1058.             //redirect to index or home
  1059.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  1060.         }
  1061.         //check user can access lesson
  1062.         if(!$lessonRepository->isLessonVisibleToStudent($lesson)){
  1063.             throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException");
  1064.         }
  1065.         $this->requestUtil->setRequest($request)->setData();
  1066.         $lessonAnnotation = new LessonAnnotation();
  1067.         if($this->requestUtil->issetField('annotation')){
  1068.             $lessonAnnotation->setAnnotation($this->requestUtil->getField('annotation'));
  1069.         }
  1070.         if($this->requestUtil->issetField('options')){
  1071.             $lessonAnnotation->setOptions($this->requestUtil->getField('options'));
  1072.         }
  1073.         if($this->requestUtil->issetField('time')){
  1074.             $lessonAnnotation->setTime($this->requestUtil->getField('time'));
  1075.         }
  1076.         $lessonAnnotation->setLesson($lesson);
  1077.         $lessonAnnotation->setUser($this->user);
  1078.         $errors $this->validateEntity($lessonAnnotation);
  1079.         if($errors){
  1080.             throw new \EADPlataforma\Error\FieldException("FieldException"$errors);
  1081.         }
  1082.         $this->em->persist($lessonAnnotation);
  1083.         $this->em->flush();
  1084.         return $this->eadResponseNew((object)[
  1085.             "id" => $lessonAnnotation->getId(),
  1086.             "annotation" => $lessonAnnotation->getAnnotation(),
  1087.             "options" => $lessonAnnotation->getOptions(),
  1088.             "time" => $lessonAnnotation->getTime(),
  1089.             "date" => $lessonAnnotation->getDate(),
  1090.         ]);
  1091.     }
  1092.     /**
  1093.      * @Route(
  1094.      *      path          = "/admin/v2/lesson/{lessonId}/notes/{id}",
  1095.      *      name          = "lessonAnnotationEditNew",
  1096.      *      methods       = {"PUT"},
  1097.      *      requirements  = { "id" = "\d+" }
  1098.      * )
  1099.      */
  1100.     public function editLessonAnnotationNew(Request $request) {
  1101.         $this->requestUtil->setRequest($request)->setData();
  1102.         $lessonId $request->get('lessonId');
  1103.         $lessonAnnotationId $request->get('id');
  1104.         $lessonRepository $this->em->getRepository(Lesson::class);
  1105.         $lesson $lessonRepository->findOneBy([
  1106.             "id" => $lessonId,
  1107.             "deleted" => LessonEnum::ITEM_NO_DELETED
  1108.         ]);
  1109.         //check lesson exist
  1110.         if (!$lesson) {
  1111.             //redirect to index or home
  1112.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  1113.         }
  1114.         //check user can access lesson
  1115.         if(!$lessonRepository->isLessonVisibleToStudent($lesson)){
  1116.             throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException");
  1117.         }
  1118.         
  1119.         $lessonAnnotationRepository $this->em->getRepository(lessonAnnotation::class);
  1120.         $lessonAnnotation $lessonAnnotationRepository->findOneBy([
  1121.             "id" => $lessonAnnotationId,
  1122.             "deleted" => LessonAnnotationEnum::ITEM_NO_DELETED
  1123.         ]);
  1124.         if (!$lessonAnnotation) {
  1125.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  1126.         }
  1127.         if($this->requestUtil->issetField('annotation')){
  1128.             $lessonAnnotation->setAnnotation($this->requestUtil->getField('annotation'));
  1129.         }
  1130.         if($this->requestUtil->issetField('time')){
  1131.             $lessonAnnotation->setTime($this->requestUtil->getField('time'));
  1132.         }
  1133.         if($this->requestUtil->issetField('options')){
  1134.             $lessonAnnotation->setOptions($this->requestUtil->getField('options'));
  1135.         }
  1136.         $errors $this->validateEntity($lessonAnnotation);
  1137.         if($errors){
  1138.             throw new \EADPlataforma\Error\FieldException("FieldException"$errors);
  1139.         }
  1140.         
  1141.         $this->em->flush();
  1142.         return $this->eadResponseNew((object)[
  1143.             "id" => $lessonAnnotation->getId(),
  1144.             "annotation" => $lessonAnnotation->getAnnotation(),
  1145.             "options" => $lessonAnnotation->getOptions(),
  1146.             "time" => $lessonAnnotation->getTime(),
  1147.             "date" => $lessonAnnotation->getDate(),
  1148.         ]);
  1149.     }
  1150.     /**
  1151.      * @Route(
  1152.      *      path          = "/admin/v2/lesson/{lessonId}/notes/{id}",
  1153.      *      name          = "lessonAnnotationDeleteNew",
  1154.      *      methods       = {"DELETE"},
  1155.      *      requirements  = { "id" = "\d+" }
  1156.      * )
  1157.      */
  1158.     public function deleteLessonAnnotationNew(Request $request) {
  1159.         $lessonId $request->get('lessonId');
  1160.         $lessonAnnotationId $request->get('id');
  1161.         $lessonRepository $this->em->getRepository(Lesson::class);
  1162.         $lesson $lessonRepository->findOneBy([
  1163.             "id" => $lessonId,
  1164.             "deleted" => LessonEnum::ITEM_NO_DELETED
  1165.         ]);
  1166.         //check lesson exist
  1167.         if (!$lesson) {
  1168.             //redirect to index or home
  1169.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  1170.         }
  1171.         //check user can access lesson
  1172.         if(!$lessonRepository->isLessonVisibleToStudent($lesson)){
  1173.             throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException");
  1174.         }
  1175.         
  1176.         $lessonAnnotationRepository $this->em->getRepository(lessonAnnotation::class);
  1177.         $lessonAnnotation $lessonAnnotationRepository->findOneBy([
  1178.             "id" => $lessonAnnotationId,
  1179.             "deleted" => LessonAnnotationEnum::ITEM_NO_DELETED
  1180.         ]);
  1181.         if (!$lessonAnnotation) {
  1182.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  1183.         }
  1184.         $lessonAnnotation->delete();
  1185.         $this->em->flush();
  1186.         return $this->eadResponseNew([ "message" => "Success" ]);
  1187.     }
  1188.     /**
  1189.      * @Route(
  1190.      *      path          = "/admin/v2/lesson/{id}",
  1191.      *      methods       = {"PATCH"},
  1192.      *      name          = "updateLessonLogNew",
  1193.      *      requirements  = { "id" = "\d+" }
  1194.      * )
  1195.      */
  1196.     public function updateLessonLogNew(Request $request) {
  1197.         $this->requestUtil->setRequest($request)->setData();
  1198.         $lessonId $request->get('id');
  1199.         $lessonRepository $this->em->getRepository(Lesson::class);
  1200.         $lesson $lessonRepository->findOneBy([
  1201.             "id" => $lessonId,
  1202.             "deleted" => LessonEnum::ITEM_NO_DELETED
  1203.         ]);
  1204.         //check lesson exist
  1205.         if (!$lesson) {
  1206.             //redirect to index or home
  1207.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  1208.         }
  1209.         //check user can access lesson
  1210.         if(!$lessonRepository->isLessonVisibleToStudent($lesson)){
  1211.             throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException");
  1212.         }
  1213.         $course $lesson->getCourse();
  1214.         $lessonModule $lesson->getLessonModule();
  1215.         $isStudent $this->repository->isStudent($course);
  1216.         $enrollmentRepository $this->em->getRepository(Enrollment::class);
  1217.         $enrollment $enrollmentRepository->findOneBy([
  1218.             "user" => $this->user->getId(),
  1219.             "course" => $course->getId(),
  1220.             "deleted" => LessonEnum::ITEM_NO_DELETED
  1221.         ], [ "id" => "DESC" ]);
  1222.         if(!$enrollment){
  1223.             throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException");
  1224.         }
  1225.         $lessonLogRepository $this->emEadmin->getRepository(LessonLog::class);
  1226.         $logId "{$course->getId()}#{$this->user->getId()}#{$lesson->getId()}";
  1227.         $lessonLog $lessonLogRepository->find($logId);
  1228.         $new false;
  1229.         if(!$lessonLog){
  1230.             $new true;
  1231.             $lessonLog = new LessonLog();
  1232.             $lessonLog->setId($logId);
  1233.             $lessonLog->setCourse($course);
  1234.             $lessonLog->setLesson($lesson);
  1235.             $lessonLog->setUser($this->user);
  1236.         }
  1237.         if($this->requestUtil->issetField('timeWatch')){
  1238.             $timeWatch $this->requestUtil->getField('timeWatch');
  1239.             $lessonLog $lessonLogRepository->updateLessonLogNew(
  1240.                 $lessonLog,
  1241.                 $timeWatch
  1242.             );
  1243.         }
  1244.         if($this->requestUtil->issetField('rate')){
  1245.             $rate = (int)$this->requestUtil->getField('rate');
  1246.             $lessonLog->setRate($rate);
  1247.         }
  1248.         if($this->requestUtil->issetField('favorite')){
  1249.             $favorite = (int)$this->requestUtil->getField('favorite');
  1250.             $lessonLog->setFavorite($favorite);
  1251.         }
  1252.         if($this->requestUtil->issetField('completed')){
  1253.             $complete = (int)$this->requestUtil->getField('completed');
  1254.             $today date('Y-m-d H:i:s');
  1255.             $lessonLog->setDateAccess($today);
  1256.             $lessonLog->setViewed($complete);
  1257.             
  1258.             if(empty($lessonLog->getDateConclusion()) && $complete == LessonEnum::YES){
  1259.                 $lessonLog->setComplete($complete);
  1260.                 $lessonLog->setDateConclusion($today);
  1261.             }
  1262.         }
  1263.         if($new){
  1264.             $this->emEadmin->persist($lessonLog);
  1265.         }
  1266.         $this->em->getRepository(LessonLogOrigin::class)->copyFromDynamo($lessonLog);
  1267.         $enrollmentRepository->updateDataAccessLog($enrollment);
  1268.         $this->emEadmin->flush();
  1269.         $this->em->flush();
  1270.         $infoLog $lessonLogRepository->getTimeInfo($lesson$lessonLog);   
  1271.         $hasLessonControl $this->configuration->checkModuleIsAbleOnPlan(
  1272.             'lessonControlFunction'
  1273.         );
  1274.         $data = [
  1275.             "nextContent" => $lessonRepository->getLessonNextContent($lesson),
  1276.             "lastContent" => $lessonRepository->getLessonLastContent($lesson),
  1277.             "id" => $lesson->getId(),
  1278.             "moduleId" => $lessonModule->getId(),
  1279.             "courseId" => $course->getId(),
  1280.             "title" => $lesson->getTitle(),
  1281.             "description" => $lesson->getDescription(),
  1282.             "controlTime" => $hasLessonControl $lesson->getControlTime() : LessonEnum::NO,
  1283.             "controlViewLimit" => $hasLessonControl $lesson->getControlViewLimit() : LessonEnum::NO,
  1284.             "controlViewNumber" => $hasLessonControl $lesson->getControlViewNumber() : LessonEnum::NO,
  1285.             "controlPauseNumber" => $hasLessonControl $lesson->getControlPauseNumber() : LessonEnum::NO,
  1286.             "controlShowDocument" => $hasLessonControl $lesson->getControlShowDocument() : LessonEnum::NO,
  1287.             "showLiveChat" => $lessonRepository->getShowLiveChat($lesson$isStudent),
  1288.             "teacher" => $lessonRepository->getLessonTeacher($lesson),
  1289.             "rates" => $lessonLogRepository->countLessonLogRate(
  1290.                 $course->getId(),
  1291.                 $lesson->getId()
  1292.             ),
  1293.             "required" => $lesson->getControlRequirement(),
  1294.             "rate" => ($lessonLog $lessonLog->getRate() : LessonEnum::NO),
  1295.             "completed" => ($lessonLog $lessonLog->getViewed() : LessonEnum::NO),
  1296.             "numberAccess" => ($lessonLog $lessonLog->getNumberAccess() : null),
  1297.             "timeToStaySeconds" => $infoLog->timeToStaySeconds,
  1298.             "timeRestToComplete" => $infoLog->timeRestToComplete,
  1299.             "blockContent" => $lessonLogRepository->checkBlockView($lessonLog),
  1300.             "chat" => null,
  1301.             "library" => null,
  1302.         ];
  1303.         return $this->eadResponseNew($data);
  1304.     }
  1305.     /**
  1306.      * @Route(
  1307.      *      path          = "/admin/v2/course/{courseId}/favorites",
  1308.      *      name          = "getCourseLessonFavoritesNew",
  1309.      *      methods       = {"GET"},
  1310.      * )
  1311.      */
  1312.     public function getCourseLessonFavoritesNew(Request $request) {
  1313.         $this->requestUtil->setRequest($request)->setData();
  1314.         $courseId $request->get('courseId');
  1315.         $course $this->repository->findOneBy([
  1316.             "id" => $courseId,
  1317.             "deleted" => CourseEnum::ITEM_NO_DELETED
  1318.         ]);
  1319.         
  1320.         if(!$course){
  1321.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  1322.         }
  1323.         $isInTeam $this->em->getRepository(CourseTeam::class)->userExistInCourseTeam(
  1324.             $course
  1325.             $this->user
  1326.         );
  1327.         $enrollmentRepository $this->em->getRepository(Enrollment::class);
  1328.         $enrollment $enrollmentRepository->findOneBy([
  1329.             "user" => $this->user->getId(),
  1330.             "course" => $courseId,
  1331.             "deleted" => EnrollmentEnum::ITEM_NO_DELETED,
  1332.         ], [ "id" => "DESC" ]);
  1333.         $permission $this->userPermissionUtil->getPermission("course""see");
  1334.         
  1335.         if(!$enrollment){
  1336.             if($course->getFree() == CourseEnum::NO){
  1337.                 if($this->userPermissionUtil->isLow($permission)){
  1338.                     throw new \EADPlataforma\Error\PermissionException("PermissionException");
  1339.                 }
  1340.                 if(!$isInTeam && $this->userPermissionUtil->isMiddle($permission)){
  1341.                     throw new \EADPlataforma\Error\PermissionException("PermissionException");
  1342.                 }
  1343.             }
  1344.             $enrollmentService $this->generalService->getService('EnrollmentService');
  1345.             $info $enrollmentService->enrollUser($this->user$course);
  1346.             
  1347.             if(!$info->errors){
  1348.                 $enrollment $info->enrollment;
  1349.             }
  1350.         }
  1351.         if($this->userPermissionUtil->isLow($permission) && !$isInTeam){
  1352.             if(!$enrollment){
  1353.                 throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException");
  1354.             }
  1355.             if($course->getStatus() == CourseEnum::DRAFT){
  1356.                 throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException");
  1357.             }
  1358.         }
  1359.         $searchText $this->requestUtil->getField('search');
  1360.         $lessonLogRepository $this->emEadmin->getRepository(LessonLog::class);
  1361.         $lessonIds $lessonLogRepository->findFavoriteLessonIds(
  1362.             $this->user->getId(), 
  1363.             $course->getId()
  1364.         );
  1365.         $lessonRepository $this->em->getRepository(Lesson::class);
  1366.         $lessons = [];
  1367.         if(!empty($lessonIds) && is_array($lessonIds)){
  1368.             $lessons $lessonRepository->getCourseLessons(
  1369.                 $course
  1370.                 null
  1371.                 $lessonIds
  1372.                 $searchText
  1373.             );
  1374.         }
  1375.         $aux = [];
  1376.         foreach ($lessons as $key => $lesson) {
  1377.             $lessonModule $lesson->getLessonModule();
  1378.             if(!isset($aux[$lessonModule->getId()])){
  1379.                 $aux[$lessonModule->getId()] = (object)[
  1380.                     "id" => $lessonModule->getId(),
  1381.                     "status" => $lessonModule->getStatus(),
  1382.                     "title" => $lessonModule->getTitle(),
  1383.                     "description" => $lessonModule->getDescription(),
  1384.                     "exam" => null,
  1385.                     "lessons" => [],
  1386.                 ];
  1387.             }
  1388.             $library $lesson->getLibrary();
  1389.             $libraryRepository $this->em->getRepository(Library::class);
  1390.             $timeUtil $this->generalService->getUtil('DateTimeUtil');
  1391.             $duration $library->getDuration();
  1392.             $aux[$lessonModule->getId()]->lessons[] = (object)[
  1393.                 "id" => $lesson->getId(),
  1394.                 "title" => $lesson->getTitle(),
  1395.                 "status" => $lesson->getStatus(),
  1396.                 "lessonIsAccessible" => true,
  1397.                 "acessMessage" => null,
  1398.                 "contentPagesNumber" => null,
  1399.                 "contentDuration" => $duration $timeUtil->timeToSec($duration) : null,
  1400.                 "contentType" => null,
  1401.                 "contentThumb" => $libraryRepository->getCover($library) ?? null,
  1402.                 "exam" => null,
  1403.                 "quiz" => null,
  1404.                 "allowCheck" => EnrollmentEnum::YES,
  1405.             ];
  1406.         }
  1407.         $data = [];
  1408.         foreach ($aux as $key => $value) {
  1409.             $data[] = $value;
  1410.         }
  1411.         return $this->eadResponseNew($data);
  1412.     }
  1413.     /**
  1414.      * @Route(
  1415.      *      path          = "/admin/v2/lesson/{id}/supports",
  1416.      *      methods       = {"GET"}
  1417.      * )
  1418.      */
  1419.     public function getLessonSupportNew(Request $request) {
  1420.         $this->requestUtil->setRequest($request)->setData();
  1421.         $lessonId $request->get('id');
  1422.         $lessonRepository $this->em->getRepository(Lesson::class);
  1423.         $lesson $lessonRepository->findOneBy([
  1424.             "id" => $lessonId,
  1425.             "deleted" => LessonEnum::ITEM_NO_DELETED
  1426.         ]);
  1427.         if(!$lesson){
  1428.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  1429.         }
  1430.         //check user can access lesson
  1431.         if(!$lessonRepository->isLessonVisibleToStudent($lesson)){
  1432.             throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException");
  1433.         }
  1434.         $course $lesson->getCourse();
  1435.         $enrollmentRepository $this->em->getRepository(Enrollment::class);
  1436.         $enrollment $enrollmentRepository->findOneBy([
  1437.             "user" => $this->user->getId(),
  1438.             "course" => $course->getId(),
  1439.             "deleted" => LessonEnum::ITEM_NO_DELETED
  1440.         ], [ "id" => "DESC" ]);
  1441.         if(!$enrollment){
  1442.             throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException");
  1443.         }
  1444.         $orderType = (int)$this->requestUtil->getField('orderType');
  1445.         $searchText $this->requestUtil->getField('searchText');
  1446.         $limit = (int)$this->requestUtil->getField('limit');
  1447.         $offset = (int)$this->requestUtil->getField('offset');
  1448.         $lessonSupportRepository $this->em->getRepository(LessonSupport::class);
  1449.         $rows $lessonSupportRepository->getLessonSupportOrderTypeNew(
  1450.             $lesson->getId(), 
  1451.             $orderType,
  1452.             $searchText,
  1453.             $limit,
  1454.             $offset
  1455.         );
  1456.         $total $lessonSupportRepository->countLessonSupportOrderType(
  1457.             $lessonId
  1458.             $orderType
  1459.             $searchText
  1460.         );
  1461.         $permission $this->userPermissionUtil->getPermission(
  1462.             "course""support""delete"
  1463.         );
  1464.         $isLessonTeacher $lessonRepository->isLessonTeacher($lesson$this->user);
  1465.         $data = [
  1466.             "allowDeleteSupport" => (
  1467.                 $isLessonTeacher || $this->userPermissionUtil->isHigh($permission)
  1468.             ),
  1469.             "rowsTotal" => $total,
  1470.             "rowsTotalDisplay" => count($rows),
  1471.             "searchText" => $searchText,
  1472.             "limit" => (!empty($limit) ? $limit 10),
  1473.             "offset" => $offset,
  1474.             "orderType" => $orderType,
  1475.             "rows" => $rows
  1476.         ];
  1477.         $likeControlRepository $this->em->getRepository(LikeControl::class);
  1478.         $typesText = [
  1479.             LessonSupportEnum::LESSON_STUDENT => $this->configuration->getLanguage(
  1480.                 'student''lesson_view'
  1481.             ),
  1482.             LessonSupportEnum::COURSE_TEACHER => $this->configuration->getLanguage(
  1483.                 'teacher''lesson_view'
  1484.             ),
  1485.             LessonSupportEnum::MODULE_TEACHER => $this->configuration->getLanguage(
  1486.                 'teacher_module''lesson_view'
  1487.             ),
  1488.             LessonSupportEnum::LESSON_TEACHER => $this->configuration->getLanguage(
  1489.                 'coordinator''lesson_view'
  1490.             ),
  1491.             LessonSupportEnum::LESSON_TUTOR => $this->configuration->getLanguage(
  1492.                 'tutor''lesson_view'
  1493.             ),
  1494.         ];
  1495.         foreach ($data['rows'] as $key => $item) {
  1496.             $item = (object)$item;
  1497.             $dataImg = [
  1498.                 "fileName" => $item->photo,
  1499.                 "pathConst" => LessonSupportEnum::PATH_PROFILES,
  1500.                 "option" => "l-user-profile-support",
  1501.                 "addUpload" => true,
  1502.                 "addStream" => true,
  1503.             ];
  1504.             $item->photo $this->fileService->getFilePathObj($dataImg);
  1505.             $item->support html_entity_decode($item->support);
  1506.             $item->answers $lessonSupportRepository->getLessonSupportAnswers($item->id);
  1507.             $item->lessonUserType = (int)$item->lessonUserType;
  1508.             $item->lessonUserTypeText $typesText[$item->lessonUserType];
  1509.             
  1510.             $likeControl $likeControlRepository->findOneByEAD([
  1511.                 "userFrom" => $this->user->getId(),
  1512.                 "element" => $item->id,
  1513.                 "type" => LikeControlEnum::LESSON_SUPPORT,
  1514.             ]);
  1515.             $item->allowLike true;
  1516.             if($likeControl){
  1517.                 $item->allowLike false;
  1518.             }
  1519.             $data['rows'][$key] = $item;
  1520.         }
  1521.         return $this->eadResponseNew($data);
  1522.     }
  1523.     /**
  1524.      * @Route(
  1525.      *      path          = "/admin/v2/lesson/{id}/supports/{lessonSupportId}/pin",
  1526.      *      methods       = {"PUT"}
  1527.      * )
  1528.      */
  1529.     public function pinLessonSupport(Request $request) {
  1530.         $permission $this->userPermissionUtil->getPermission("course""support""finalize");
  1531.         if($this->userPermissionUtil->isLow($permission)){
  1532.             throw new \EADPlataforma\Error\PermissionException("PermissionException");
  1533.         }
  1534.         
  1535.         $this->requestUtil->setRequest($request)->setData();
  1536.         $lessonSupportId = (int)$request->get('lessonSupportId');
  1537.         $lessonSupportRepository $this->em->getRepository(LessonSupport::class);
  1538.         $lessonSupport $lessonSupportRepository->findOneBy([
  1539.             "id" => $lessonSupportId,
  1540.             "deleted" => LessonSupportEnum::ITEM_NO_DELETED
  1541.         ]);
  1542.         
  1543.         if (!$lessonSupport) {
  1544.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  1545.         }
  1546.         $lesson $lessonSupport->getLesson();
  1547.         $isLessonTeacher $this->em->getRepository(Lesson::class)->isLessonTeacher(
  1548.             $lesson
  1549.             $this->user
  1550.         );
  1551.         
  1552.         if(!$isLessonTeacher && $this->userPermissionUtil->isMiddle($permission)){
  1553.             throw new \EADPlataforma\Error\PermissionException("PermissionException");
  1554.         }
  1555.         $lessonSupport->setLessonFixed(LessonSupportEnum::YES);
  1556.         $this->em->flush();
  1557.         $data $lessonSupport->toReturn();
  1558.         return $this->eadResponseNew([ "message" => "Success" ]);
  1559.     }
  1560.     /**
  1561.      * @Route(
  1562.      *      path          = "/admin/v2/lesson/{id}/supports/{lessonSupportId}/unpin",
  1563.      *      methods       = {"DELETE"}
  1564.      * )
  1565.      */
  1566.     public function unpinLessonSupport(Request $request) {
  1567.         $permission $this->userPermissionUtil->getPermission("course""support""finalize");
  1568.         if($this->userPermissionUtil->isLow($permission)){
  1569.             throw new \EADPlataforma\Error\PermissionException("PermissionException");
  1570.         }
  1571.         
  1572.         $this->requestUtil->setRequest($request)->setData();
  1573.         $lessonSupportId = (int)$request->get('lessonSupportId');
  1574.         $lessonSupportRepository $this->em->getRepository(LessonSupport::class);
  1575.         $lessonSupport $lessonSupportRepository->findOneBy([
  1576.             "id" => $lessonSupportId,
  1577.             "deleted" => LessonSupportEnum::ITEM_NO_DELETED
  1578.         ]);
  1579.         
  1580.         if (!$lessonSupport) {
  1581.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  1582.         }
  1583.         $lesson $lessonSupport->getLesson();
  1584.         $isLessonTeacher $this->em->getRepository(Lesson::class)->isLessonTeacher(
  1585.             $lesson
  1586.             $this->user
  1587.         );
  1588.         
  1589.         if(!$isLessonTeacher && $this->userPermissionUtil->isMiddle($permission)){
  1590.             throw new \EADPlataforma\Error\PermissionException("PermissionException");
  1591.         }
  1592.         $lessonSupport->setLessonFixed(LessonSupportEnum::NO);
  1593.         $this->em->flush();
  1594.         $data $lessonSupport->toReturn();
  1595.         return $this->eadResponseNew([ "message" => "Success" ]);
  1596.     }
  1597.     /**
  1598.      * @Route(
  1599.      *      path          = "/admin/v2/lesson/{id}/supports/{lessonSupportId}",
  1600.      *      methods       = {"PATCH"}
  1601.      * )
  1602.      */
  1603.     public function likeLessonSupport(Request $request) {
  1604.         $this->requestUtil->setRequest($request)->setData();
  1605.         $lessonSupportId = (int)$request->get('lessonSupportId');
  1606.         $lessonSupportRepository $this->em->getRepository(LessonSupport::class);
  1607.         $lessonSupport $lessonSupportRepository->findOneBy([
  1608.             "id" => $lessonSupportId,
  1609.             "deleted" => LessonSupportEnum::ITEM_NO_DELETED
  1610.         ]);
  1611.         
  1612.         if (!$lessonSupport) {
  1613.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  1614.         }
  1615.         $lesson $lessonSupport->getLesson();
  1616.         
  1617.         $likeControl = new LikeControl();
  1618.         $type LikeControlEnum::LESSON_SUPPORT;
  1619.         if($lessonSupport->getLessonSupport()){
  1620.             $type LikeControlEnum::LESSON_SUPPORT_ANSWER;
  1621.         }
  1622.         $likeControl->setElement($lessonSupportId);
  1623.         $likeControl->setType($type);
  1624.         $likeControl->setUserTo($lessonSupport->getUser());
  1625.         $likeControl->setUserFrom($this->user);
  1626.         
  1627.         $errors $this->validateEntity($likeControl);
  1628.         if($errors){
  1629.             throw new \EADPlataforma\Error\FieldException("FieldException"$errors);
  1630.         }
  1631.         $lessonSupport->setLikeNumber($lessonSupport->getLikeNumber() + 1);
  1632.         
  1633.         $this->em->persist($likeControl);
  1634.         $this->em->flush();
  1635.         return $this->eadResponseNew([ "message" => "Success" ]);
  1636.     }
  1637.     /**
  1638.      * @Route(
  1639.      *      path          = "/admin/v2/lesson/{id}/supports",
  1640.      *      methods       = {"POST"},
  1641.      * )
  1642.      */
  1643.     public function registerLessonSupport(Request $request) {
  1644.         
  1645.         $this->requestUtil->setRequest($request)->setData();
  1646.         $lessonRepository $this->em->getRepository(Lesson::class);
  1647.         $lessonSupportRepository $this->em->getRepository(LessonSupport::class);
  1648.         $enrollmentRepository $this->em->getRepository(Enrollment::class);
  1649.         $lessonId $request->get('id');
  1650.         $lesson $lessonRepository->findOneBy([
  1651.             "id" => $lessonId,
  1652.             "deleted" => LessonEnum::ITEM_NO_DELETED
  1653.         ]);
  1654.         if(!$lesson){
  1655.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  1656.         }
  1657.         //check user can access lesson
  1658.         if(!$lessonRepository->isLessonVisibleToStudent($lesson)){
  1659.             throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException 1");
  1660.         }
  1661.         $lessonSupport = new LessonSupport();
  1662.         
  1663.         $lessonSupport->setUser($this->user);
  1664.         $lessonSupport->setLesson($lesson);
  1665.         //set LessonSupport in LessonSupport
  1666.         if($this->requestUtil->issetField('lessonSupport')){
  1667.             $lessonSupportId = (int)$this->requestUtil->getField('lessonSupport');
  1668.             $lessonSupportParent $lessonSupportRepository->findOneBy([
  1669.                 "id" => $lessonSupportId,
  1670.                 "deleted" => LessonSupportEnum::ITEM_NO_DELETED
  1671.             ]);
  1672.             $lessonSupport->setLessonSupport($lessonSupportParent);
  1673.         }
  1674.         
  1675.         if($this->requestUtil->issetField('support')){
  1676.             $lessonSupport->setSupport($this->requestUtil->getField('support'));
  1677.         }
  1678.         $errors $this->validateEntity($lessonSupport);
  1679.         if($errors){
  1680.             throw new \EADPlataforma\Error\FieldException("FieldException"$errors);
  1681.         }
  1682.         
  1683.         $lessonSupportParent $lessonSupport->getLessonSupport();
  1684.         $lesson $lessonSupport->getLesson();
  1685.         $course $lesson->getCourse();
  1686.         
  1687.         $isLessonTeacher $lessonRepository->isLessonTeacher(
  1688.             $lesson
  1689.             $this->user
  1690.         );
  1691.         $isEnrolled $enrollmentRepository->isValidEnrollmentByUser(
  1692.             $this->user->getId(), 
  1693.             $course->getId()
  1694.         );
  1695.         $permission $this->userPermissionUtil->getPermission("course""support""answer");
  1696.         if($lessonSupportParent){
  1697.             if(!$isEnrolled){
  1698.                 if($this->userPermissionUtil->isLow($permission)){
  1699.                     throw new \EADPlataforma\Error\PermissionException("PermissionException");
  1700.                 }
  1701.                 if(!$isLessonTeacher && $this->userPermissionUtil->isMiddle($permission)){
  1702.                     throw new \EADPlataforma\Error\PermissionException("PermissionException");
  1703.                 }
  1704.             }
  1705.         }else{
  1706.             if($course->getSupport() == LessonSupportEnum::NO){
  1707.                 throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException 2");
  1708.             }
  1709.             if(!$isEnrolled && !$isLessonTeacher){
  1710.                 throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException 3");
  1711.             }
  1712.         }
  1713.         $this->em->persist($lessonSupport);
  1714.         $this->em->flush();
  1715.         $lessonSupportRepository->processNewLessonSupport($lessonSupport);
  1716.          
  1717.         return $this->eadResponseNew([ "message" => "Success" ]);
  1718.     }
  1719.     /**
  1720.      * @Route(
  1721.      *      path          = "/admin/v2/lesson/{id}/supports/{lessonSupportId}",
  1722.      *      methods       = {"PUT"}
  1723.      * )
  1724.      */
  1725.     public function editLessonSupport(Request $request) {
  1726.         $this->requestUtil->setRequest($request)->setData();
  1727.         $lessonSupportId = (int)$request->get('lessonSupportId');
  1728.         $lessonSupportRepository $this->em->getRepository(LessonSupport::class);
  1729.         $lessonSupport $lessonSupportRepository->findOneBy([
  1730.             "id" => $lessonSupportId,
  1731.             "deleted" => LessonSupportEnum::ITEM_NO_DELETED
  1732.         ]);
  1733.         
  1734.         if (!$lessonSupport) {
  1735.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  1736.         }
  1737.         $lesson $lessonSupport->getLesson();
  1738.         
  1739.         if($this->requestUtil->issetField('support')){
  1740.             $lessonSupport->setSupport($this->requestUtil->getField('support'));
  1741.         }
  1742.         $lessonSupport->setDateUpdate(date('Y-m-d H:i:s'));
  1743.         
  1744.         $errors $this->validateEntity($lessonSupport);
  1745.         if($errors){
  1746.             throw new \EADPlataforma\Error\FieldException("FieldException"$errors);
  1747.         }
  1748.         $this->em->flush();
  1749.         return $this->eadResponseNew([ "message" => "Success" ]);
  1750.     }
  1751.     /**
  1752.      * @Route(
  1753.      *      path          = "/admin/v2/lesson/{id}/supports/{lessonSupportId}",
  1754.      *      methods       = {"DELETE"}
  1755.      * )
  1756.      */
  1757.     public function deleteLessonSupport(Request $request) {
  1758.         $this->requestUtil->setRequest($request)->setData();
  1759.         $lessonSupportId = (int)$request->get('lessonSupportId');
  1760.         $lessonSupportRepository $this->em->getRepository(LessonSupport::class);
  1761.         $lessonSupport $lessonSupportRepository->findOneBy([
  1762.             "id" => $lessonSupportId,
  1763.             "deleted" => LessonSupportEnum::ITEM_NO_DELETED
  1764.         ]);
  1765.         
  1766.         if (!$lessonSupport) {
  1767.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  1768.         }
  1769.         
  1770.         $lessonSupportRepository->delete(
  1771.             $lessonSupport
  1772.             TrashEnum::LESSON_SUPPORT
  1773.             $this->userPermissionUtil->getPermission("course""support""delete"), 
  1774.             TrashEnum::NO
  1775.         );
  1776.         $this->em->flush();
  1777.         return $this->eadResponseNew([ "message" => "Success" ]);
  1778.     }
  1779.     /**
  1780.      * @Route(
  1781.      *      path          = "/admin/v2/course/{id}/testimonial",
  1782.      *      methods       = {"POST"},
  1783.      * )
  1784.      */
  1785.     public function registerCourseTestimonial(Request $request) {
  1786.         $this->requestUtil->setRequest($request)->setData();
  1787.         $courseId $request->get('id');
  1788.         $course $this->repository->findOneBy([
  1789.             "id" => $courseId,
  1790.             "deleted" => CourseEnum::ITEM_NO_DELETED
  1791.         ]);
  1792.         
  1793.         if(!$course){
  1794.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  1795.         }
  1796.         $isInTeam $this->em->getRepository(CourseTeam::class)->userExistInCourseTeam(
  1797.             $course
  1798.             $this->user
  1799.         );
  1800.         $enrollmentRepository $this->em->getRepository(Enrollment::class);
  1801.         $enrollment $enrollmentRepository->findOneBy([
  1802.             "user" => $this->user->getId(),
  1803.             "course" => $courseId,
  1804.             "deleted" => EnrollmentEnum::ITEM_NO_DELETED,
  1805.         ], [ "id" => "DESC" ]);
  1806.         $permission $this->userPermissionUtil->getPermission("course""see");
  1807.         
  1808.         if(!$enrollment){
  1809.             if($course->getFree() == CourseEnum::NO){
  1810.                 if($this->userPermissionUtil->isLow($permission)){
  1811.                     throw new \EADPlataforma\Error\PermissionException("PermissionException");
  1812.                 }
  1813.                 if(!$isInTeam && $this->userPermissionUtil->isMiddle($permission)){
  1814.                     throw new \EADPlataforma\Error\PermissionException("PermissionException");
  1815.                 }
  1816.             }
  1817.             $enrollmentService $this->generalService->getService('EnrollmentService');
  1818.             $info $enrollmentService->enrollUser($this->user$course);
  1819.             
  1820.             if(!$info->errors){
  1821.                 $enrollment $info->enrollment;
  1822.             }
  1823.         }
  1824.         if($this->userPermissionUtil->isLow($permission) && !$isInTeam){
  1825.             if(!$enrollment){
  1826.                 throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException");
  1827.             }
  1828.             if($course->getStatus() == CourseEnum::DRAFT){
  1829.                 throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException");
  1830.             }
  1831.         }
  1832.         $courseTestimonial = new CourseTestimonial();
  1833.         $courseTestimonial->setDate(date('Y-m-d H:i:s'));
  1834.         $courseTestimonial->setStatus(CourseTestimonialEnum::WAITING);
  1835.         if($this->requestUtil->issetField('score')){
  1836.             $courseTestimonial->setScore((int)$this->requestUtil->getField("score"));
  1837.         }
  1838.         if($this->requestUtil->issetField('testimonial')){
  1839.             $courseTestimonial->setTestimonial($this->requestUtil->getField('testimonial'));
  1840.         }
  1841.         $courseTestimonial->setUser($this->user);
  1842.         $courseTestimonial->setCourse($course);
  1843.         $errors $this->validateEntity($courseTestimonial);
  1844.         if($errors){
  1845.             throw new \EADPlataforma\Error\FieldException("FieldException"$errors);
  1846.         }
  1847.         $this->em->persist($courseTestimonial);
  1848.         $this->em->flush();
  1849.       
  1850.         $dataObj= (object)[
  1851.             "user" => (object)[        
  1852.                     "id" => (string)$courseTestimonial->getUser()->getId(),
  1853.                     "name" => $courseTestimonial->getUser()->getName(),
  1854.                     "email" => $courseTestimonial->getUser()->getEmail(),
  1855.                     "phone" => $courseTestimonial->getUser()->getPhone(),
  1856.                     "status" => $courseTestimonial->getUser()->getStatus(),
  1857.                     "dates" => (object)[
  1858.                         "signup" => $courseTestimonial->getUser()->getDateRegister(),
  1859.                     ],
  1860.                 "course"=> (object)[        
  1861.                     "id" => (string)$courseTestimonial->getCourse()->getId(),
  1862.                     "name" => $courseTestimonial->getCourse()->getTitle(),
  1863.                 ],
  1864.                 "testimonial"=> (object)[
  1865.                     "stars" => $courseTestimonial->getScore(),
  1866.                     "comment" => $courseTestimonial->getTestimonial(),
  1867.                     "dates" => (object)[
  1868.                         "created" => $courseTestimonial->getDate(),
  1869.                     ],
  1870.                 ],
  1871.                 "enrollment"=> (object)[
  1872.                     "id" => (string)$enrollment->getId(),
  1873.                     "status" => $enrollment->getStatus(),
  1874.                     "dates" => (object)[
  1875.                         "register" => $enrollment->getDateRegister(),
  1876.                         "expiration" => $enrollment->getDateLastAccess(),
  1877.                         "support" => $enrollment->getDateSupport(),
  1878.                         "start" => $enrollment->getDateStart(),
  1879.                         "conclusion" => $enrollment->getDateConclusion(),
  1880.                     ],
  1881.                 ],
  1882.             ],
  1883.         ];
  1884.         $webhookService $this->generalService->getService('WebhookService');
  1885.         $webhookService->addItemList(WebhookEnum::TESTIMONIAL$dataObj);
  1886.         $notificationService $this->generalService->getService(
  1887.             'NotificationService'
  1888.         );
  1889.         
  1890.         
  1891.         if($this->user != $course->getUser()){
  1892.             $notificationService->create(
  1893.                 $this->user
  1894.                 $course->getUser(),
  1895.                 NotificationEnum::ORIGIN_COURSE_TESTIMONIAL_NEW,
  1896.                 $courseTestimonial->getId()
  1897.             );
  1898.         }
  1899.         $userCourse $course->getUser();
  1900.         if($userCourse->getAllowNotifyNewSupportMessage() == CourseTestimonialEnum::YES){
  1901.             $emailService $this->generalService->getService('EmailService');
  1902.             if($emailService->checkUserToSend($userCourse)){
  1903.                 $emailService->setToEmail($userCourse->getEmail());
  1904.                 $emailService->setToName($userCourse->getName());
  1905.     
  1906.                 $subText $this->configuration->getLanguage(
  1907.                     'new_course_testimonial.subject''email'
  1908.                 );
  1909.                 
  1910.                 $id $courseTestimonial->getId();
  1911.                 $subject "{$subText} - {$course->getTitle()}";
  1912.                 $emailService->setSubject($subject);
  1913.     
  1914.                 $emailService->setData([
  1915.                     "userName" => $userCourse->getName(),
  1916.                     "btnLink" => "{$this->adminLink}courses/testimonials/{$id}",
  1917.                 ]);
  1918.     
  1919.                 $emailService->setTemplateBody("new_course_testimonial");
  1920.                 $emailService->send();
  1921.             }
  1922.         }
  1923.         $data $courseTestimonial->toReturn();
  1924.         $this->userLogService->logInsert(
  1925.             "course_testimonial"
  1926.             $courseTestimonial->getId(), 
  1927.             $data
  1928.         );
  1929.         $testimonial = (object)[
  1930.             "id" => $courseTestimonial->getId(),
  1931.             "score" => $courseTestimonial->getScore(),
  1932.             "testimonial" => $courseTestimonial->getTestimonial(),
  1933.         ];
  1934.         return $this->eadResponseNew($testimonial);
  1935.     }
  1936.     /**
  1937.      * @Route(
  1938.      *      path          = "/admin/v2/course/{courseId}/testimonial/{id}",
  1939.      *      methods       = {"PUT"}
  1940.      * )
  1941.      */
  1942.     public function editCourseTestimonial(Request $request) {
  1943.         $courseId $request->get('courseId');
  1944.         $course $this->repository->findOneBy([
  1945.             "id" => $courseId,
  1946.             "deleted" => CourseEnum::ITEM_NO_DELETED
  1947.         ]);
  1948.         
  1949.         if(!$course){
  1950.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  1951.         }
  1952.         $isInTeam $this->em->getRepository(CourseTeam::class)->userExistInCourseTeam(
  1953.             $course
  1954.             $this->user
  1955.         );
  1956.         $enrollmentRepository $this->em->getRepository(Enrollment::class);
  1957.         $enrollment $enrollmentRepository->findOneBy([
  1958.             "user" => $this->user->getId(),
  1959.             "course" => $courseId,
  1960.             "deleted" => EnrollmentEnum::ITEM_NO_DELETED,
  1961.         ], [ "id" => "DESC" ]);
  1962.         $permission $this->userPermissionUtil->getPermission("course""see");
  1963.         
  1964.         if(!$enrollment){
  1965.             if($course->getFree() == CourseEnum::NO){
  1966.                 if($this->userPermissionUtil->isLow($permission)){
  1967.                     throw new \EADPlataforma\Error\PermissionException("PermissionException");
  1968.                 }
  1969.                 if(!$isInTeam && $this->userPermissionUtil->isMiddle($permission)){
  1970.                     throw new \EADPlataforma\Error\PermissionException("PermissionException");
  1971.                 }
  1972.             }
  1973.             $enrollmentService $this->generalService->getService('EnrollmentService');
  1974.             $info $enrollmentService->enrollUser($this->user$course);
  1975.             
  1976.             if(!$info->errors){
  1977.                 $enrollment $info->enrollment;
  1978.             }
  1979.         }
  1980.         if($this->userPermissionUtil->isLow($permission) && !$isInTeam){
  1981.             if(!$enrollment){
  1982.                 throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException");
  1983.             }
  1984.             if($course->getStatus() == CourseEnum::DRAFT){
  1985.                 throw new \EADPlataforma\Error\ActionInvalidException("ActionInvalidException");
  1986.             }
  1987.         }
  1988.         $testimonialId $request->get('id');
  1989.         $courseTestimonialRepository $this->em->getRepository(CourseTestimonial::class);
  1990.         $courseTestimonial $courseTestimonialRepository->findOneBy([
  1991.             "id" => $testimonialId,
  1992.             "deleted" => CourseTestimonialEnum::ITEM_NO_DELETED
  1993.         ]);
  1994.         if (!$courseTestimonial || empty($testimonialId)) {
  1995.             throw new \EADPlataforma\Error\NotFoundException("NotFoundException");
  1996.         }
  1997.         $this->requestUtil->setRequest($request)->setData();
  1998.         if($this->requestUtil->issetField('score')){
  1999.             $courseTestimonial->setScore((int)$this->requestUtil->getField("score"));
  2000.         }
  2001.         if($this->requestUtil->issetField('testimonial')){
  2002.             $courseTestimonial->setTestimonial($this->requestUtil->getField('testimonial'));
  2003.         }
  2004.         
  2005.         $courseTestimonial->setDate(date('Y-m-d H:i:s'));
  2006.         $courseTestimonial->setStatus(CourseTestimonialEnum::WAITING);
  2007.         $errors $this->validateEntity($courseTestimonial);
  2008.         
  2009.         if($errors){
  2010.             throw new \EADPlataforma\Error\FieldException("FieldException"$errors);
  2011.         }
  2012.         $this->em->flush();
  2013.         $notificationService $this->generalService->getService('NotificationService');
  2014.         $emailService $this->generalService->getService('EmailService');
  2015.         if($this->user != $course->getUser()){
  2016.             $notificationService->create(
  2017.                 $this->user
  2018.                 $course->getUser(),
  2019.                 NotificationEnum::ORIGIN_COURSE_TESTIMONIAL_CHANGE,
  2020.                 $courseTestimonial->getId()
  2021.             );
  2022.         }
  2023.         $user $course->getUser();
  2024.         if($user->getAllowNotifyNewSupportMessage() == CourseTestimonialEnum::YES){
  2025.             if($emailService->checkUserToSend($user)){
  2026.                 $emailService->setToEmail($user->getEmail());
  2027.                 $emailService->setToName($user->getName());
  2028.                 $subText $this->configuration->getLanguage(
  2029.                     'new_course_testimonial.subject''email'
  2030.                 );
  2031.                 $subject "{$subText} - {$course->getTitle()}";
  2032.                 $emailService->setSubject($subject);
  2033.                 
  2034.                 $emailService->setData([
  2035.                     "userName" => $course->getUser()->getName(),
  2036.                     "btnLink" => "{$this->adminLink}courses/testimonials/{$testimonialId}",
  2037.                 ]);
  2038.                 $emailService->setTemplateBody("new_course_testimonial");
  2039.                 $emailService->send();
  2040.             }
  2041.         }
  2042.         $data $courseTestimonial->toReturn();
  2043.         $this->userLogService->logUpdate(
  2044.             "course_testimonial"
  2045.             $courseTestimonial->getId(),
  2046.             $data
  2047.         );
  2048.         $testimonial = (object)[
  2049.             "id" => $courseTestimonial->getId(),
  2050.             "score" => $courseTestimonial->getScore(),
  2051.             "testimonial" => $courseTestimonial->getTestimonial(),
  2052.         ];
  2053.         return $this->eadResponseNew($testimonial);
  2054.     }
  2055. }