src/Controller/Admin/CourseIndexController.php line 673

Open in your IDE?
  1. <?php
  2. namespace EADPlataforma\Controller\Admin;
  3. use EADPlataforma\Error\ActionInvalidException;
  4. use EADPlataforma\Error\FieldException;
  5. use EADPlataforma\Error\NotFoundException;
  6. use EADPlataforma\Error\PermissionException;
  7. use EADPlataforma\Error\AuthInvalidException;
  8. use EADPlataforma\Response\HttpCreated;
  9. use EADPlataforma\Response\HttpNoContent;
  10. use EADPlataforma\Response\HttpOk;
  11. use EADPlataforma\Services\EntityServices\CourseIndexService;
  12. use EADPlataforma\Services\EntityServices\ExamUserAnswerService;
  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\Course;
  22. use EADPlataforma\Entity\CourseTeam;
  23. use EADPlataforma\Entity\Category;
  24. use EADPlataforma\Entity\Product;
  25. use EADPlataforma\Entity\ProductOffer;
  26. use EADPlataforma\Entity\ProductTeam;
  27. use EADPlataforma\Entity\Lesson;
  28. use EADPlataforma\Entity\LessonSupport;
  29. use EADPlataforma\Entity\LessonLog;
  30. use EADPlataforma\Entity\LessonLogOrigin;
  31. use EADPlataforma\Entity\LessonModule;
  32. use EADPlataforma\Entity\LessonAnnotation;
  33. use EADPlataforma\Entity\Library;
  34. use EADPlataforma\Entity\LessonXLibrary;
  35. use EADPlataforma\Entity\LikeControl;
  36. use EADPlataforma\Entity\CourseCertificateTemplate;
  37. use EADPlataforma\Entity\Enrollment;
  38. use EADPlataforma\Entity\Exam;
  39. use EADPlataforma\Entity\ExamUser;
  40. use EADPlataforma\Entity\ExamUserAnswer;
  41. use EADPlataforma\Entity\ExamUserAnswerOption;
  42. use EADPlataforma\Entity\Question;
  43. use EADPlataforma\Entity\QuestionOption;
  44. use EADPlataforma\Entity\CourseTestimonial;
  45. use EADPlataforma\Entity\User;
  46. use EADPlataforma\Entity\UserSubscription;
  47. use EADPlataforma\Enum\CourseEnum;
  48. use EADPlataforma\Enum\CourseTeamEnum;
  49. use EADPlataforma\Enum\ProductEnum;
  50. use EADPlataforma\Enum\EnrollmentEnum;
  51. use EADPlataforma\Enum\ExamEnum;
  52. use EADPlataforma\Enum\ExamUserEnum;
  53. use EADPlataforma\Enum\LessonEnum;
  54. use EADPlataforma\Enum\LessonSupportEnum;
  55. use EADPlataforma\Enum\LibraryEnum;
  56. use EADPlataforma\Enum\LikeControlEnum;
  57. use EADPlataforma\Enum\LessonXLibraryEnum;
  58. use EADPlataforma\Enum\LessonAnnotationEnum;
  59. use EADPlataforma\Enum\LessonModuleEnum;
  60. use EADPlataforma\Enum\ExamUserAnswerEnum;
  61. use EADPlataforma\Enum\QuestionEnum;
  62. use EADPlataforma\Enum\QuestionOptionEnum;
  63. use EADPlataforma\Enum\CourseTestimonialEnum;
  64. use EADPlataforma\Enum\NotificationEnum;
  65. use EADPlataforma\Enum\WebhookEnum;
  66. use EADPlataforma\Enum\TrashEnum;
  67. use EADPlataforma\Enum\ErrorEnum;
  68. /**
  69.  * @Route(
  70.  *      path          = "",
  71.  *      schemes         = {"http|https"}
  72.  * )
  73.  * @Cache(
  74.  *      maxage          = "0",
  75.  *      smaxage         = "0",
  76.  *      expires         = "now",
  77.  *      public          = false
  78.  * )
  79.  */
  80. class CourseIndexController extends AbstractController {
  81.     public function getEntityClass(){
  82.         return Course::class;
  83.     }
  84.     /**
  85.      * @Route(
  86.      *      path          = "/admin/v2/general/{id}",
  87.      *      methods       = {"GET"},
  88.      *      requirements  = { "id" = "\d+" }
  89.      * )
  90.      * 
  91.      * @throws NotFoundException
  92.      * @throws ActionInvalidException
  93.      */
  94.     public function getCourseNewGeneral(
  95.         Request $request
  96.         CourseIndexService $courseIndexService
  97.     ): JsonResponse
  98.     {
  99.         $courseId $request->get('id');
  100.         $course $courseIndexService->searchCourse($courseId);
  101.         if(!$course){
  102.             throw new NotFoundException(
  103.                 $this->configuration->getLanguage('error_course_not_found''lesson_view_error')
  104.             );
  105.         }
  106.         $enrollment $courseIndexService->searchEnrollment($course);
  107.         if(!$enrollment){
  108.             throw new ActionInvalidException(
  109.                 $this->configuration->getLanguage(
  110.                     'error_enrollment_not_found'
  111.                     'lesson_view_error'
  112.                 )
  113.             );
  114.         }
  115.         $isStudent $courseIndexService->isStudent($course);
  116.         if($isStudent && $course->getStatus() == CourseEnum::DRAFT){
  117.             throw new ActionInvalidException(
  118.                 $this->configuration->getLanguage(
  119.                     'error_course_not_published'
  120.                     'lesson_view_error'
  121.                 )
  122.             );
  123.         }
  124.         $data $courseIndexService->getCourseNewGeneral($course$enrollment);
  125.         return new HttpOk($data);
  126.     }
  127.     /**
  128.      * @Route(
  129.      *      path          = "/admin/v2/course/{id}",
  130.      *      methods       = {"GET"},
  131.      *      requirements  = { "id" = "\d+" }
  132.      * )
  133.      * 
  134.      * @throws NotFoundException
  135.      * @throws ActionInvalidException
  136.      */
  137.     public function getCourseNewIndex(
  138.         Request $request,
  139.         CourseIndexService $courseIndexService
  140.     ): JsonResponse
  141.     {
  142.         
  143.         $courseId $request->get('id');
  144.         $course $courseIndexService->searchCourse($courseId);
  145.         if(!$course){
  146.             throw new NotFoundException(
  147.                 $this->configuration->getLanguage('error_course_not_found''lesson_view_error')
  148.             );
  149.         }
  150.         $enrollment $courseIndexService->searchEnrollment($course);
  151.         if(!$enrollment){
  152.             throw new ActionInvalidException(
  153.                 $this->configuration->getLanguage(
  154.                     'error_enrollment_not_found'
  155.                     'lesson_view_error'
  156.                 )
  157.             );
  158.         }
  159.         $isStudent $courseIndexService->isStudent($course);
  160.         if($isStudent && $course->getStatus() == CourseEnum::DRAFT){
  161.             throw new ActionInvalidException(
  162.                 $this->configuration->getLanguage(
  163.                     'error_course_not_published'
  164.                     'lesson_view_error'
  165.                 )
  166.             );
  167.         }
  168.         $data $courseIndexService->getCourseIndex($enrollment);
  169.         return new HttpOk($data);
  170.     }
  171.     /**
  172.      * @Route(
  173.      *      path          = "/admin/v2/module/{id}",
  174.      *      methods       = {"GET"},
  175.      *      requirements  = { "id" = "\d+" }
  176.      * )
  177.      * 
  178.      * @throws NotFoundException
  179.      * @throws ActionInvalidException
  180.      */
  181.     public function getModuleNewIndex(
  182.         Request $request,
  183.         CourseIndexService $courseIndexService,
  184.         LessonModuleService $lessonModuleService
  185.     ): JsonResponse
  186.     {
  187.         $moduleId $request->get('id');
  188.         $lessonModule $lessonModuleService->searchModule($moduleId);
  189.         if(!$lessonModule){
  190.             throw new NotFoundException(
  191.                 $this->configuration->getLanguage('error_module_not_found''lesson_view_error')
  192.             );
  193.         }
  194.         $course $lessonModule->getCourse();
  195.         $enrollment $courseIndexService->searchEnrollment($course);
  196.         if(!$enrollment){
  197.             throw new ActionInvalidException($this->configuration->getLanguage('error_enrollment_not_found''lesson_view_error'));
  198.         }
  199.         $isStudent $courseIndexService->isStudent($course);
  200.         if($isStudent && $course->getStatus() == CourseEnum::DRAFT){
  201.             throw new ActionInvalidException(
  202.                 $this->configuration->getLanguage(
  203.                     'error_course_not_published'
  204.                     'lesson_view_error'
  205.                 )
  206.             );
  207.         }
  208.         $data $lessonModuleService->getModuleIndex(
  209.             $enrollment,
  210.             $lessonModule,
  211.             $isStudent
  212.         );
  213.         return new HttpOk($data);
  214.     }
  215.     /**
  216.      * @Route(
  217.      *      path          = "/admin/v2/course/{id}/modules",
  218.      *      methods       = {"GET"},
  219.      *      requirements  = { "id" = "\d+" }
  220.      * )
  221.      *
  222.      * @throws NotFoundException
  223.      * @throws ActionInvalidException
  224.      */
  225.     public function getModules(
  226.         Request $request,
  227.         CourseIndexService $courseIndexService,
  228.         LessonModuleService $lessonModuleService
  229.     ): JsonResponse
  230.     {
  231.         
  232.         $courseId $request->get('id');
  233.         $course $courseIndexService->searchCourse($courseId);
  234.         if(!$course){
  235.             throw new NotFoundException(
  236.                 $this->configuration->getLanguage('error_course_not_found''lesson_view_error')
  237.             );
  238.         }
  239.         $enrollment $courseIndexService->searchEnrollment($course);
  240.         if(!$enrollment){
  241.             throw new ActionInvalidException(
  242.                 $this->configuration->getLanguage(
  243.                     'error_enrollment_not_found'
  244.                     'lesson_view_error'
  245.                 )
  246.             );
  247.         }
  248.         $isStudent $courseIndexService->isStudent($course);
  249.         if($isStudent && $course->getStatus() == CourseEnum::DRAFT){
  250.             throw new ActionInvalidException(
  251.                 $this->configuration->getLanguage(
  252.                     'error_course_not_published'
  253.                     'lesson_view_error'
  254.                 )
  255.             );
  256.         }
  257.         $searchText $request->get('search');
  258.         $data $lessonModuleService->getModulesIndex(
  259.             $enrollment,
  260.             $isStudent,
  261.             $searchText
  262.         );
  263.         return new HttpOk($data);
  264.     }
  265.     /**
  266.      * @Route(
  267.      *      path          = "/admin/v2/lesson/{id}",
  268.      *      methods       = {"GET"},
  269.      *      name          = "getViewLessonNew",
  270.      *      requirements  = { "id" = "\d+" }
  271.      * )
  272.      * 
  273.      * @throws NotFoundException
  274.      * @throws ActionInvalidException
  275.      */
  276.     public function getViewLessonNew(
  277.         Request $request,
  278.         CourseIndexService $courseIndexService,
  279.         LessonService $lessonService,
  280.         LibraryChapterService $chapterService
  281.     ): JsonResponse
  282.     {
  283.         $lessonId $request->get('id');
  284.         $chatVersion = (int)$request->get('chatVersion');
  285.         $lesson $lessonService->searchLesson($lessonId);
  286.         if (!$lesson) {
  287.             throw new NotFoundException(
  288.                 $this->configuration->getLanguage('error_lesson_not_found''lesson_view_error')
  289.             );
  290.         }
  291.         $course $lesson->getCourse();
  292.         if(
  293.             $courseIndexService->isEnrollment($course) && 
  294.             !$courseIndexService->isValidEnrollment($course)
  295.         ){
  296.             throw new ActionInvalidException(
  297.                 $this->configuration->getLanguage('error_enrollment_expired''lesson_view_error'), 
  298.                 [
  299.                     "expired" => true,
  300.                 ]
  301.             );
  302.         }
  303.         //check user can access lesson
  304.         $isStudent $courseIndexService->isStudent($course);
  305.         if(!$lessonService->isLessonVisibleToStudent($lesson$isStudent)){
  306.             throw new ActionInvalidException(
  307.                 $this->configuration->getLanguage('error_lesson_unavailable''lesson_view_error')
  308.             );
  309.         }
  310.         
  311.         $enrollment $courseIndexService->searchEnrollment($course);
  312.         if(!$enrollment){
  313.             throw new ActionInvalidException(
  314.                 $this->configuration->getLanguage(
  315.                     'error_enrollment_not_found'
  316.                     'lesson_view_error'
  317.                 )
  318.             );
  319.         }
  320.         
  321.         $lessonIsAccessible $lessonService->checkLessonIsAccessibleToUser(
  322.             $lesson,
  323.             $enrollment,
  324.             $isStudent
  325.         );
  326.         if(!$lessonIsAccessible->isAccessible){
  327.             throw new ActionInvalidException($lessonIsAccessible->message);
  328.         }
  329.         $data $lessonService->getLessonIndex(
  330.             $lesson,
  331.             $enrollment,
  332.             $isStudent,
  333.             true
  334.         );
  335.         return new HttpOk($data);
  336.     }
  337.     /**
  338.      * @Route(
  339.      *      path          = "/admin/v2/{module}/{id}/{type}",
  340.      *      methods       = {"GET"},
  341.      *      name          = "getExamNewIndex",
  342.      *      requirements  = {"type"="exam|quiz"}
  343.      * )
  344.      */
  345.     public function getExamNewIndex(Request $request)
  346.     {
  347.         
  348.         $idModule = (int)$request->get('id');
  349.         $module $request->get('module');
  350.         $type $request->get('type');
  351.         
  352.         if($type != 'quiz'){
  353.             if(!$this->configuration->isModuleActive("exam_module")){
  354.                 throw new ActionInvalidException(
  355.                     $this->configuration->getLanguage('error_exam_unavailable''lesson_view_error')
  356.                 );
  357.             }
  358.         }
  359.         $class = [
  360.             "lesson" => Lesson::class,
  361.             "module" => LessonModule::class,
  362.             "course" => Course::class,
  363.         ];
  364.         if(!isset($class[$module])){
  365.             throw new NotFoundException(
  366.                 $this->configuration->getLanguage('error_exam_not_found''lesson_view_error')
  367.             );
  368.         }
  369.         $itemRepository $this->em->getRepository($class[$module]);
  370.         $item $itemRepository->findOneBy([
  371.             "id" => $idModule,
  372.             "deleted" => CourseEnum::ITEM_NO_DELETED
  373.         ]);
  374.         //check item exist
  375.         if(!$item){
  376.             //redirect to index or home
  377.             throw new NotFoundException(
  378.                 $this->configuration->getLanguage('error_exam_not_found''lesson_view_error')
  379.             );
  380.         }
  381.         $course $item;
  382.         if(!($course instanceof Course)){
  383.             $course $item->getCourse();
  384.         }
  385.         
  386.         $enrollmentRepository $this->em->getRepository(Enrollment::class);
  387.         $enrollment $enrollmentRepository->findOneBy([
  388.             "user" => $this->user->getId(),
  389.             "course" => $course->getId(),
  390.             "deleted" => LessonEnum::ITEM_NO_DELETED
  391.         ], [ "id" => "DESC" ]);
  392.         if(!$enrollment){
  393.             throw new ActionInvalidException(
  394.                 $this->configuration->getLanguage('error_enrollment_not_found''lesson_view_error')
  395.             );
  396.         }
  397.         $isStudent $this->repository->isStudent($course);
  398.         $filterExam = [
  399.             "course" => $course->getId(),
  400.             "deleted" => ExamEnum::ITEM_NO_DELETED
  401.         ];
  402.         if($item instanceof Course){
  403.             $filterExam["type"] = ExamEnum::COURSE;
  404.         }else if($item instanceof LessonModule){
  405.             $filterExam["lessonModule"] = $item->getId();
  406.             $filterExam["type"] = ExamEnum::MODULE;
  407.         }else if($item instanceof Lesson){
  408.             //check user can access lesson
  409.             if(!$itemRepository->isLessonVisibleToStudent($itemfalse$isStudent)){
  410.                 if($type == 'quiz'){
  411.                     throw new ActionInvalidException(
  412.                         $this->configuration->getLanguage(
  413.                             'error_quiz_unavailable'
  414.                             'lesson_view_error'
  415.                         )
  416.                     );
  417.                 }
  418.                 throw new ActionInvalidException(
  419.                     $this->configuration->getLanguage('error_lesson_unavailable''lesson_view_error')
  420.                 );
  421.             }
  422.             $lessonModule $item->getLessonModule();
  423.             $filterExam["lesson"] = $item->getId();
  424.             $filterExam["lessonModule"] = $lessonModule->getId();
  425.             $filterExam["type"] = ($type == "quiz" ExamEnum::QUIZ ExamEnum::LESSON );
  426.         }
  427.         if($isStudent){
  428.             $filterExam["status"] = ExamEnum::PUBLISHED;
  429.         }
  430.         $examRepository $this->em->getRepository(Exam::class);
  431.         $examUserRepository $this->em->getRepository(ExamUser::class);
  432.         $exam $examRepository->findOneBy($filterExam);
  433.         if(!$exam){
  434.             throw new NotFoundException(
  435.                 $this->configuration->getLanguage('error_exam_not_found''lesson_view_error')
  436.             );
  437.         }
  438.         $examUser $examUserRepository->findOneBy([
  439.             "user" => $this->user->getId(),
  440.             "exam" => $exam->getId(),
  441.             "inactive" => ExamUserEnum::NO,
  442.             "deleted" => ExamUserEnum::ITEM_NO_DELETED
  443.         ]);
  444.         $infoAccess $examRepository->checkExamIsAccessible($exam$examUser);
  445.         if(!$infoAccess->isAccessible){
  446.             throw new ActionInvalidException($infoAccess->message);
  447.         }
  448.         $data $examUserRepository->getDataIndexNew($exam$enrollment);
  449.         
  450.         return $this->eadResponseNew($data);
  451.     }
  452.     /**
  453.      * @Route(
  454.      *      path          = "/admin/v2/{module}/{id}/exam/start",
  455.      *      methods       = {"PATCH"},
  456.      *      name          = "startExamNewIndex",
  457.      *      requirements  = { "id" = "\d+" }
  458.      * )
  459.      */
  460.     public function startExamNewIndex(Request $request)
  461.     {
  462.         if(!$this->configuration->isModuleActive("exam_module")){
  463.             throw new ActionInvalidException(
  464.                 $this->configuration->getLanguage('error_exam_unavailable''lesson_view_error')
  465.             );
  466.         }
  467.         $idModule = (int)$request->get('id');
  468.         $module $request->get('module');
  469.         $class = [
  470.             "lesson" => Lesson::class,
  471.             "module" => LessonModule::class,
  472.             "course" => Course::class,
  473.         ];
  474.         if(!isset($class[$module])){
  475.             throw new NotFoundException(
  476.                 $this->configuration->getLanguage('error_exam_not_found''lesson_view_error')
  477.             );
  478.         }
  479.         $itemRepository $this->em->getRepository($class[$module]);
  480.         $item $itemRepository->findOneBy([
  481.             "id" => $idModule,
  482.             "deleted" => CourseEnum::ITEM_NO_DELETED
  483.         ]);
  484.         //check item exist
  485.         if(!$item){
  486.             //redirect to index or home
  487.             throw new NotFoundException(
  488.                 $this->configuration->getLanguage('error_exam_not_found''lesson_view_error')
  489.             );
  490.         }
  491.         $course $item;
  492.         if(!($course instanceof Course)){
  493.             $course $item->getCourse();
  494.         }
  495.         $user $this->user;
  496.         $enrollmentRepository $this->em->getRepository(Enrollment::class);
  497.         $enrollment $enrollmentRepository->findOneBy([
  498.             "user" => $user->getId(),
  499.             "course" => $course->getId(),
  500.             "deleted" => LessonEnum::ITEM_NO_DELETED
  501.         ], [ "id" => "DESC" ]);
  502.         if(!$enrollment){
  503.             throw new ActionInvalidException($this->configuration->getLanguage('error_enrollment_not_found''lesson_view_error'));
  504.         }
  505.         $filterExam = [
  506.             "course" => $course->getId(),
  507.             "deleted" => ExamEnum::ITEM_NO_DELETED
  508.         ];
  509.         $isStudent $this->repository->isStudent($course);
  510.         if($item instanceof Course){
  511.             $filterExam["type"] = ExamEnum::COURSE;
  512.         }else if($item instanceof LessonModule){
  513.             $filterExam["lessonModule"] = $item->getId();
  514.             $filterExam["type"] = ExamEnum::MODULE;
  515.         }else if($item instanceof Lesson){
  516.             //check user can access lesson
  517.             if(!$itemRepository->isLessonVisibleToStudent($itemfalse$isStudent)){
  518.                 throw new ActionInvalidException($this->configuration->getLanguage('error_lesson_unavailable''lesson_view_error'));
  519.             }
  520.             $lessonModule $item->getLessonModule();
  521.             $filterExam["lesson"] = $item->getId();
  522.             $filterExam["lessonModule"] = $lessonModule->getId();
  523.             $filterExam["type"] = ExamEnum::LESSON;
  524.         }
  525.         if($isStudent){
  526.             $filterExam["status"] = ExamEnum::PUBLISHED;
  527.         }
  528.         $examRepository $this->em->getRepository(Exam::class);
  529.         $exam $examRepository->findOneBy($filterExam);
  530.         if(!$exam){
  531.             throw new NotFoundException(
  532.                 $this->configuration->getLanguage('error_exam_not_found''lesson_view_error')
  533.             );
  534.         }
  535.         $examUserRepository $this->em->getRepository(ExamUser::class);
  536.         
  537.         $examUser $examUserRepository->findOneBy([
  538.             "user" => $user->getId(),
  539.             "exam" => $exam->getId(),
  540.             "inactive" => ExamUserEnum::NO,
  541.             "deleted" => ExamUserEnum::ITEM_NO_DELETED
  542.         ]);
  543.         $infoAccess $examRepository->checkExamIsAccessible($exam$examUser);
  544.         if(!$infoAccess->isAccessible){
  545.             throw new ActionInvalidException($infoAccess->message);
  546.         }
  547.         if($examUser){
  548.             if(
  549.                 $examUser->getStatus() == ExamUserEnum::DISAPPROVED || 
  550.                 $examUser->getStatus() == ExamUserEnum::TIMEOUT
  551.             ){
  552.                 $examUser $examUserRepository->getValidExamUserById(
  553.                     $examUser->getId(), 
  554.                     true
  555.                 );
  556.                 
  557.                 $attemptsInfo $examUserRepository->getAttemptsInfo($exam$examUser);
  558.                 
  559.                 if($attemptsInfo->attempts == ExamEnum::YES){
  560.                     $examUser->setInactive(ExamUserEnum::YES);
  561.                     $this->em->flush();
  562.                     $examUser null;
  563.                 }
  564.             }
  565.         }
  566.         if(!$examUser){
  567.             $examUser = new ExamUser();
  568.             $examUser->setExam($exam);
  569.             $examUser->setCourse($exam->getCourse());
  570.             $examUser->setUser($user);
  571.             $this->em->persist($examUser);
  572.             $this->em->flush();
  573.         }else{
  574.             throw new NotFoundException(
  575.                 $this->configuration->getLanguage('error_exam_not_found''lesson_view_error')
  576.             );
  577.         }
  578.         $data $examUserRepository->getDataIndexNew($exam$enrollment);
  579.         return $this->eadResponseNew($data);
  580.     }
  581.     /**
  582.      * @Route(
  583.      *      path          = "/admin/v2/{module}/{id}/exam/{questionId}",
  584.      *      methods       = {"POST"},
  585.      * )
  586.      */
  587.     public function registerAnswerExamQuestion(
  588.         Request $request,
  589.         ExamUserAnswerService $examUserAnswerService
  590.     ) {
  591.         if(!$this->configuration->isModuleActive("exam_module")){
  592.             throw new ActionInvalidException(
  593.                 $this->configuration->getLanguage('error_exam_unavailable''lesson_view_error')
  594.             );
  595.         }
  596.         $this->requestUtil->setRequest($request)->setData();
  597.         $idModule = (int)$request->get('id');
  598.         $module $request->get('module');
  599.         $questionId = (int)$request->get('questionId');
  600.         $class = [
  601.             "lesson" => Lesson::class,
  602.             "module" => LessonModule::class,
  603.             "course" => Course::class,
  604.         ];
  605.         if(!isset($class[$module])){
  606.             throw new NotFoundException(
  607.                 $this->configuration->getLanguage('error_exam_not_found''lesson_view_error')
  608.             );
  609.         }
  610.         $examUserAnswerRepository $this->em->getRepository(ExamUserAnswer::class);
  611.         $examUserRepository $this->em->getRepository(ExamUser::class);
  612.         $enrollmentRepository $this->em->getRepository(Enrollment::class);
  613.         $examRepository $this->em->getRepository(Exam::class);
  614.         $questionRepository $this->em->getRepository(Question::class);
  615.         $questionOptionRepository $this->em->getRepository(QuestionOption::class);
  616.         $itemRepository $this->em->getRepository($class[$module]);
  617.         $item $itemRepository->findOneBy([
  618.             "id" => $idModule,
  619.             "deleted" => CourseEnum::ITEM_NO_DELETED
  620.         ]);
  621.         //check item exist
  622.         if(!$item){
  623.             //redirect to index or home
  624.             throw new NotFoundException(
  625.                 $this->configuration->getLanguage('error_exam_not_found''lesson_view_error')
  626.             );
  627.         }
  628.         $course $item;
  629.         if(!($course instanceof Course)){
  630.             $course $item->getCourse();
  631.         }
  632.         $user $this->user;
  633.         $enrollment $enrollmentRepository->findOneBy([
  634.             "user" => $user->getId(),
  635.             "course" => $course->getId(),
  636.             "deleted" => LessonEnum::ITEM_NO_DELETED
  637.         ], [ "id" => "DESC" ]);
  638.         if(!$enrollment){
  639.             throw new ActionInvalidException($this->configuration->getLanguage('error_enrollment_not_found''lesson_view_error'));
  640.         }
  641.         $filterExam = [
  642.             "course" => $course->getId(),
  643.             "deleted" => ExamEnum::ITEM_NO_DELETED
  644.         ];
  645.         $isStudent $this->repository->isStudent($course);
  646.         if($item instanceof Course){
  647.             $filterExam["type"] = ExamEnum::COURSE;
  648.         }else if($item instanceof LessonModule){
  649.             $filterExam["lessonModule"] = $item->getId();
  650.             $filterExam["type"] = ExamEnum::MODULE;
  651.         }else if($item instanceof Lesson){
  652.             //check user can access lesson
  653.             if(!$itemRepository->isLessonVisibleToStudent($itemfalse$isStudent)){
  654.                 throw new ActionInvalidException(
  655.                     $this->configuration->getLanguage(
  656.                         'error_lesson_unavailable'
  657.                         'lesson_view_error'
  658.                     )
  659.                 );
  660.             }
  661.             $lessonModule $item->getLessonModule();
  662.             $filterExam["lesson"] = $item->getId();
  663.             $filterExam["lessonModule"] = $lessonModule->getId();
  664.             $filterExam["type"] = ExamEnum::LESSON;
  665.         }
  666.         if($isStudent){
  667.             $filterExam["status"] = ExamEnum::PUBLISHED;
  668.         }
  669.         $exam $examRepository->findOneBy($filterExam);
  670.         if(!$exam){
  671.             throw new NotFoundException(
  672.                 $this->configuration->getLanguage('error_exam_not_found''lesson_view_error')
  673.             );
  674.         }
  675.         
  676.         $question $questionRepository->findOneBy([
  677.             "id" => $questionId,
  678.             "deleted" => ExamUserAnswerEnum::ITEM_NO_DELETED
  679.         ]);
  680.         if(!$question){
  681.             throw new NotFoundException(
  682.                 $this->configuration->getLanguage('error_question_not_found''lesson_view_error')
  683.             );
  684.         }
  685.         $examUser $examUserRepository->findOneBy([
  686.             "user" => $user->getId(),
  687.             "exam" => $exam->getId(),
  688.             "inactive" => ExamUserEnum::NO,
  689.             "deleted" => ExamUserEnum::ITEM_NO_DELETED
  690.         ]);
  691.         $examUser $examUserRepository->getValidExamUserById(nulltrue$examUser);
  692.         if (!$examUser) {
  693.             throw new NotFoundException(
  694.                 $this->configuration->getLanguage('error_exam_not_found''lesson_view_error')
  695.             );
  696.         }
  697.         $expired $examUserRepository->examUserIsExpired($examUser);
  698.         if($expired){
  699.             throw new NotFoundException(
  700.                 $this->configuration->getLanguage('error_exam_expired''lesson_view_error')
  701.             );
  702.         }
  703.         //check exist answer to this question
  704.         $questionAnswer $examUserAnswerRepository->findOneBy([
  705.             "deleted" => ExamUserAnswerEnum::ITEM_NO_DELETED,
  706.             "examUser" => $examUser->getId(),
  707.             "question" => $questionId,
  708.         ]);
  709.         if($questionAnswer){
  710.             throw new NotFoundException(
  711.                 $this->configuration->getLanguage(
  712.                     'error_answer_question_not_found'
  713.                     'lesson_view_error'
  714.                 )
  715.             );
  716.         }
  717.         $answer $this->requestUtil->getField('answer');
  718.         if(empty($answer)){
  719.             throw new FieldException(
  720.                 "FieldException",
  721.                 [ 
  722.                     "answer" => "Value not found"
  723.                 ]
  724.             );
  725.         }
  726.         $questionAnswer = new ExamUserAnswer();
  727.         $questionAnswer->setExamUser($examUser);
  728.         $questionAnswer->setAnswered(QuestionEnum::YES);
  729.         $questionAnswer->setQuestion($question);
  730.         if($question->getType() == QuestionEnum::DISSERTATION){
  731.             $questionAnswer->setAnswer($answer);
  732.         }
  733.         $errors $this->validateEntity($questionAnswer);
  734.         if($errors){
  735.             throw new FieldException("FieldException"$errors);
  736.         }
  737.         $this->em->persist($questionAnswer);
  738.         $grade ExamUserAnswerEnum::GRADE_INCORRECT;
  739.         if($question->getType() != QuestionEnum::DISSERTATION){
  740.             $questionAnswer->setEvaluated((int)$question->getAnswered());
  741.             $optionsCorrect $questionOptionRepository->findBy([
  742.                 "question" => $question->getId(),
  743.                 "correct" => QuestionEnum::YES,
  744.                 "deleted" => ExamUserAnswerEnum::ITEM_NO_DELETED,
  745.             ]);
  746.             
  747.             $options json_decode($this->requestUtil->getField('answer'));
  748.             
  749.             foreach ($options as $key => $option) {
  750.                 $questionOption $questionOptionRepository->findOneBy([
  751.                     "id" => $option,
  752.                     "question" => $question->getId(),
  753.                     "deleted" => ExamUserAnswerEnum::ITEM_NO_DELETED,
  754.                 ]);
  755.                 if($questionOption){
  756.                     if($questionOption->getCorrect() == QuestionEnum::YES){
  757.                         $questionAnswer->setEvaluated(QuestionEnum::YES);
  758.                         $question->setAnswered(QuestionEnum::YES);
  759.                         if($question->getType() == QuestionEnum::ALTERNATIVE){
  760.                             $grade ExamUserAnswerEnum::GRADE_CORRECT;
  761.                         }
  762.                     }
  763.                     $questionAnswerOption = new ExamUserAnswerOption();
  764.                     $questionAnswerOption->setMarked(QuestionEnum::YES);
  765.                     $questionAnswerOption->setExamUserAnswer($questionAnswer);
  766.                     $questionAnswerOption->setQuestionOption($questionOption);
  767.                     $this->em->persist($questionAnswerOption);
  768.                 }
  769.             }
  770.             if($question->getType() == QuestionEnum::MULTIPLE_ALTERNATIVE){
  771.                 $grade $examUserAnswerService->correctMultipleAlternatives(
  772.                     $options
  773.                     $optionsCorrect
  774.                 );
  775.             }
  776.         }else if(
  777.             $this->configuration->checkModuleIsAbleOnPlan('iadFunction') &&
  778.             $exam->getAutoCorrectTextQuestion() == ExamEnum::YES
  779.         ){
  780.             $gpt $this->generalService->getService('ChatGPTService');
  781.             $category $course->getCategory();
  782.             $categoryTitle = ($category $category->getCategory() : null);
  783.             $respGPT $gpt->answerExamQuestion(
  784.                 $question->getQuestion(), 
  785.                 $answer
  786.                 $course->getTitle(), 
  787.                 $categoryTitle
  788.             );
  789.             if(isset($respGPT->grade) && isset($respGPT->description)){
  790.                 $grade $respGPT->grade;
  791.                 $questionAnswer->setNote($respGPT->description);
  792.                 $questionAnswer->setEvaluated(QuestionEnum::YES);
  793.             }
  794.         }
  795.         $questionAnswer->setGrade($grade);
  796.         $this->em->flush();
  797.         sleep(0.5);
  798.         $questionNumberAnswer $examUserAnswerRepository->count([
  799.             "examUser" => $examUser->getId(),
  800.             "deleted" => ExamUserAnswerEnum::ITEM_NO_DELETED,
  801.             "answered" => QuestionEnum::YES
  802.         ]);
  803.         $questionNumber $exam->getQuestionNumber();
  804.         $questionNumberReal $exam->getQuestionNumberReal();
  805.         if($questionNumber $questionNumberReal){
  806.             $questionNumber $questionNumberReal;
  807.         }
  808.         if($exam->getQuestionOrder() == ExamEnum::SEQUENTIAL){
  809.             $questionNumber count($exam->getLiveQuestion());
  810.         }
  811.         if($questionNumber <= $questionNumberAnswer){
  812.             $examUserRepository->updateExamUser($examUser);
  813.         }
  814.         $data $questionAnswer->toReturn();
  815.         $this->userLogService->logInsert("exam_user_answer"$questionAnswer->getId(), $data);
  816.         $data $examUserRepository->getDataIndexNew($exam$enrollment);
  817.         return $this->eadResponseNew($data);
  818.     }
  819.     /**
  820.      * @Route(
  821.      *      path          = "/admin/v2/{module}/{id}/exam/outside",
  822.      *      methods       = {"PATCH"},
  823.      *      name          = "outsideExam",
  824.      *      requirements  = { "id" = "\d+" }
  825.      * )
  826.      */
  827.     public function outsideExam(Request $request)
  828.     {
  829.         if(!$this->configuration->isModuleActive("exam_module")){
  830.             throw new ActionInvalidException("ActionInvalidException");
  831.         }
  832.         $idModule = (int)$request->get('id');
  833.         $module $request->get('module');
  834.         $class = [
  835.             "lesson" => Lesson::class,
  836.             "module" => LessonModule::class,
  837.             "course" => Course::class,
  838.         ];
  839.         if(!isset($class[$module])){
  840.             throw new NotFoundException("NotFoundException");
  841.         }
  842.         $itemRepository $this->em->getRepository($class[$module]);
  843.         $item $itemRepository->findOneBy([
  844.             "id" => $idModule,
  845.             "deleted" => CourseEnum::ITEM_NO_DELETED
  846.         ]);
  847.         //check item exist
  848.         if(!$item){
  849.             //redirect to index or home
  850.             throw new NotFoundException("NotFoundException");
  851.         }
  852.         $course $item;
  853.         if(!($course instanceof Course)){
  854.             $course $item->getCourse();
  855.         }
  856.         $user $this->user;
  857.         $enrollmentRepository $this->em->getRepository(Enrollment::class);
  858.         $enrollment $enrollmentRepository->findOneBy([
  859.             "user" => $user->getId(),
  860.             "course" => $course->getId(),
  861.             "deleted" => LessonEnum::ITEM_NO_DELETED
  862.         ], [ "id" => "DESC" ]);
  863.         if(!$enrollment){
  864.             throw new ActionInvalidException("ActionInvalidException");
  865.         }
  866.         $filterExam = [
  867.             "course" => $course->getId(),
  868.             "deleted" => ExamEnum::ITEM_NO_DELETED
  869.         ];
  870.         $isStudent $this->repository->isStudent($course);
  871.         if($item instanceof Course){
  872.             $filterExam["type"] = ExamEnum::COURSE;
  873.         }else if($item instanceof LessonModule){
  874.             $filterExam["lessonModule"] = $item->getId();
  875.             $filterExam["type"] = ExamEnum::MODULE;
  876.         }else if($item instanceof Lesson){
  877.             //check user can access lesson
  878.             if(!$itemRepository->isLessonVisibleToStudent($itemfalse$isStudent)){
  879.                 throw new ActionInvalidException("ActionInvalidException");
  880.             }
  881.             $lessonModule $item->getLessonModule();
  882.             $filterExam["lesson"] = $item->getId();
  883.             $filterExam["lessonModule"] = $lessonModule->getId();
  884.             $filterExam["type"] = ExamEnum::LESSON;
  885.         }
  886.         $exam $this->em->getRepository(Exam::class)->findOneBy($filterExam);
  887.         if(!$exam){
  888.             throw new NotFoundException("NotFoundException");
  889.         }
  890.         if($exam->getControlTime() == ExamEnum::NO){
  891.             throw new NotFoundException("ActionInvalidException");
  892.         }
  893.         $examUserRepository $this->em->getRepository(ExamUser::class);
  894.         
  895.         $examUser $examUserRepository->findOneBy([
  896.             "user" => $user->getId(),
  897.             "exam" => $exam->getId(),
  898.             "inactive" => ExamUserEnum::NO,
  899.             "deleted" => ExamUserEnum::ITEM_NO_DELETED
  900.         ]);
  901.         if(!$examUser){
  902.             throw new ActionInvalidException("ActionInvalidException");
  903.         }
  904.         if($examUser->getStatus() !== ExamUserEnum::IN_PROGRESS){
  905.             throw new ActionInvalidException("ActionInvalidException");
  906.         }
  907.         $timeUtil $this->generalService->getUtil('DateTimeUtil');
  908.         $examUser $examUserRepository->getValidExamUserById(
  909.             $examUser->getId(), 
  910.             true
  911.         );
  912.         $this->requestUtil->setRequest($request)->setData();
  913.         $outsideNumber $examUser->getOutsideNumber();
  914.         $newOutsideNumber $outsideNumber 1;
  915.         $examUser->setOutsideNumber($newOutsideNumber);
  916.         $outsideTime '00:00:00';
  917.         if(!empty($examUser->getOutsideTime())){
  918.             $outsideTime $examUser->getOutsideTime();
  919.         }
  920.         if($this->requestUtil->issetField('outsideTime')){
  921.             $newTime $this->requestUtil->getField('outsideTime');
  922.             $newTime $timeUtil->timeToSec($newTime);
  923.             $oldTime $timeUtil->timeToSec($outsideTime);
  924.             $sumTime $oldTime $newTime;
  925.             $time $timeUtil->secToTime($sumTime);
  926.             $examUser->setOutsideTime($time);
  927.         }
  928.         $errors $this->validateEntity($examUser);
  929.         if($errors){
  930.             throw new FieldException("FieldException"$errors);
  931.         }
  932.         
  933.         $this->em->flush();
  934.         return $this->eadResponseNew([ "message" => "Success" ]);
  935.     }
  936.     /**
  937.      * @Route(
  938.      *      path          = "/admin/v2/lesson/{id}/quiz/{questionId}",
  939.      *      methods       = {"POST"}
  940.      * )
  941.      */
  942.     public function getQuestionQuizAnswer(Request $request) {
  943.         $questionRepository $this->em->getRepository(Question::class);
  944.         $questionOptionRepository $this->em->getRepository(QuestionOption::class);
  945.         $libraryRepository $this->em->getRepository(Library::class);
  946.         $this->requestUtil->setRequest($request)->setData();
  947.         $questionId = (int)$request->get('questionId');
  948.         $lessonId = (int)$request->get('id');
  949.         
  950.         $lessonRepository $this->em->getRepository(Lesson::class);
  951.         $lesson $lessonRepository->findOneBy([
  952.             "id" => $lessonId,
  953.             "deleted" => LessonEnum::ITEM_NO_DELETED
  954.         ]);
  955.         //check item exist
  956.         if(!$lesson){
  957.             throw new NotFoundException(
  958.                 $this->configuration->getLanguage('error_lesson_not_found''lesson_view_error')
  959.             );
  960.         }
  961.         $question $questionRepository->findOneBy([
  962.             "id" => $questionId,
  963.             "deleted" => QuestionEnum::ITEM_NO_DELETED
  964.         ]);
  965.         if(!$question){
  966.             throw new NotFoundException(
  967.                 $this->configuration->getLanguage('error_question_not_found''lesson_view_error')
  968.             );
  969.         }
  970.         $optionCorrect $questionOptionRepository->findOneBy([
  971.             "deleted" => QuestionOptionEnum::ITEM_NO_DELETED,
  972.             "question" => $questionId,
  973.             "correct" => QuestionOptionEnum::YES,
  974.         ]);
  975.         $libraryReturn null;
  976.         $library $question->getLibrary();
  977.         if($library){
  978.             $libraryInfo $libraryRepository->getContentInfo($libraryfalse$lesson);
  979.             $libraryReturn = (object)[
  980.                 "id" => $library->getId(),
  981.                 "title" => $library->getTitle(),
  982.                 "type" => $library->getType(),
  983.                 "link" => $libraryInfo->url,
  984.                 "text" => $library->getText(),
  985.                 "file" => $library->getId(),
  986.                 "fileExtension" => $library->getFileExtension(),
  987.                 "liveStart" => $library->getLiveStart(),
  988.                 "liveEmbedId" => $libraryInfo->embedId,
  989.                 "pagesNumber" => $library->getPagesNumber(),
  990.                 "duration" => $library->getDuration(),
  991.             ];
  992.             $showDocument = ($lesson->getControlShowDocument() == LibraryEnum::YES);
  993.             $drmVideo $this->configuration->get("drm_video");
  994.             $hasLessonControl $this->configuration->checkModuleIsAbleOnPlan(
  995.                 'lessonControlFunction'
  996.             );
  997.             $annotate = [];
  998.             if($hasLessonControl && $showDocument && $drmVideo == LibraryEnum::YES){
  999.                 $annotate $libraryRepository->getVideoDRM($this->user);
  1000.             }
  1001.             $credentials $libraryRepository->getVideoCredentials(
  1002.                 $library,
  1003.                 $annotate
  1004.             );
  1005.             if($credentials){
  1006.                 $libraryReturn->credentials $credentials;
  1007.             }
  1008.         }
  1009.         $data = [
  1010.             "library" => $libraryReturn,
  1011.             "optionCorrect" => ( $optionCorrect $optionCorrect->getId() : null ),
  1012.         ];
  1013.         return $this->eadResponseNew($data);
  1014.     }
  1015.     /**
  1016.      * @Route(
  1017.      *      path          = "/admin/v2/lesson/{lessonId}/end/live",
  1018.      *      methods       = {"PATCH"}
  1019.      * )
  1020.      */
  1021.     public function liveLessonEnd(Request $request) {
  1022.         $lessonRepository $this->em->getRepository(Lesson::class);
  1023.         $lessonId $request->get('lessonId');
  1024.         $lesson $lessonRepository->findOneBy([
  1025.             "id" => $lessonId,
  1026.             "deleted" => LessonEnum::ITEM_NO_DELETED
  1027.         ]);
  1028.         //check lesson exist
  1029.         if (!$lesson) {
  1030.             //redirect to index or home
  1031.             throw new NotFoundException(
  1032.                 $this->configuration->getLanguage('error_lesson_not_found''lesson_view_error')
  1033.             );
  1034.         }
  1035.         $isStudent $this->repository->isStudent($lesson->getCourse());
  1036.         //check user can access lesson
  1037.         if($isStudent){
  1038.             throw new ActionInvalidException('ActionInvalidException');
  1039.         }
  1040.         $library $lesson->getLibrary();
  1041.         if(!$library){
  1042.             //redirect to index or home
  1043.             throw new NotFoundException(
  1044.                 $this->configuration->getLanguage('error_lesson_not_found''lesson_view_error')
  1045.             );
  1046.         }
  1047.         if(
  1048.             $library->getType() != LibraryEnum::CONTENT_NEW_LIVE || 
  1049.             empty($library->getLiveIdEvent())
  1050.         ){
  1051.             throw new ActionInvalidException('ActionInvalidException');
  1052.         }
  1053.         $vdocipherService $this->generalService->getService('VdocipherService');
  1054.         $vdocipherService->endLive($library->getLiveIdEvent());
  1055.         $library->setType(LibraryEnum::CONTENT_VIDEO_FILE);
  1056.         $library->setLiveEnd(date('Y-m-d H:i:s'));
  1057.         $library->setLiveEndUser($this->user);
  1058.         $this->em->flush();
  1059.         return new HttpNoContent;
  1060.     }
  1061.     /**
  1062.      * @Route(
  1063.      *      path          = "/admin/v2/lesson/{lessonId}/start/record",
  1064.      *      methods       = {"PATCH"}
  1065.      * )
  1066.      */
  1067.     public function meetLessonStart(Request $request) {
  1068.         
  1069.         $lessonRepository $this->em->getRepository(Lesson::class);
  1070.         $lessonId $request->get('lessonId');
  1071.         $lesson $lessonRepository->findOneBy([
  1072.             "id" => $lessonId,
  1073.             "deleted" => LessonEnum::ITEM_NO_DELETED
  1074.         ]);
  1075.         //check lesson exist
  1076.         if (!$lesson) {
  1077.             //redirect to index or home
  1078.             throw new NotFoundException(
  1079.                 $this->configuration->getLanguage('error_lesson_not_found''lesson_view_error')
  1080.             );
  1081.         }
  1082.         $isStudent $this->repository->isStudent($lesson->getCourse());
  1083.         //check user can access lesson
  1084.         if($isStudent){
  1085.             throw new ActionInvalidException('ActionInvalidException');
  1086.         }
  1087.         $library $lesson->getLibrary();
  1088.         if(!$library){
  1089.             //redirect to index or home
  1090.             throw new NotFoundException(
  1091.                 $this->configuration->getLanguage('error_lesson_not_found''lesson_view_error')
  1092.             );
  1093.         }
  1094.         if($library->getRecord() == LibraryEnum::YES){
  1095.             throw new ActionInvalidException('ActionInvalidException');
  1096.         }
  1097.         $library->setRecord(LibraryEnum::YES);
  1098.         $library->setRecordUser($this->user);
  1099.         $this->em->flush();
  1100.         return new HttpNoContent;
  1101.     }
  1102.     /**
  1103.      * @Route(
  1104.      *      path          = "/admin/v2/lesson/{lessonId}/end/record",
  1105.      *      methods       = {"PATCH"}
  1106.      * )
  1107.      */
  1108.     public function meetLessonEnd(Request $request) {
  1109.         
  1110.         $lessonRepository $this->em->getRepository(Lesson::class);
  1111.         $lessonId $request->get('lessonId');
  1112.         $lesson $lessonRepository->findOneBy([
  1113.             "id" => $lessonId,
  1114.             "deleted" => LessonEnum::ITEM_NO_DELETED
  1115.         ]);
  1116.         //check lesson exist
  1117.         if (!$lesson) {
  1118.             //redirect to index or home
  1119.             throw new NotFoundException(
  1120.                 $this->configuration->getLanguage('error_lesson_not_found''lesson_view_error')
  1121.             );
  1122.         }
  1123.         $isStudent $this->repository->isStudent($lesson->getCourse());
  1124.         //check user can access lesson
  1125.         if($isStudent){
  1126.             throw new ActionInvalidException('ActionInvalidException');
  1127.         }
  1128.         $library $lesson->getLibrary();
  1129.         if(!$library){
  1130.             //redirect to index or home
  1131.             throw new NotFoundException(
  1132.                 $this->configuration->getLanguage('error_lesson_not_found''lesson_view_error')
  1133.             );
  1134.         }
  1135.         if($library->getRecord() == LibraryEnum::NO){
  1136.             throw new ActionInvalidException('ActionInvalidException');
  1137.         }
  1138.         $userRecord $library->getRecordUser();
  1139.         if(!$userRecord){
  1140.             throw new ActionInvalidException('ActionInvalidException');
  1141.         }
  1142.         if($this->user->getId() != $userRecord->getId()){
  1143.             throw new ActionInvalidException('ActionInvalidException');
  1144.         }
  1145.         $library->setRecord(LibraryEnum::NO);
  1146.         $library->setRecordUser(null);
  1147.         $this->em->flush();
  1148.         return new HttpNoContent;
  1149.     }
  1150.     /**
  1151.      * @Route(
  1152.      *      path          = "/admin/v2/lesson/{lessonId}/files",
  1153.      *      methods       = {"GET"}
  1154.      * )
  1155.      */
  1156.     public function getLessonFiles(Request $request) {
  1157.         
  1158.         $lessonRepository $this->em->getRepository(Lesson::class);
  1159.         $lessonXLibraryRepository $this->em->getRepository(LessonXLibrary::class);
  1160.         $lessonId $request->get('lessonId');
  1161.         $lesson $lessonRepository->findOneBy([
  1162.             "id" => $lessonId,
  1163.             "deleted" => LessonEnum::ITEM_NO_DELETED
  1164.         ]);
  1165.         //check lesson exist
  1166.         if (!$lesson) {
  1167.             //redirect to index or home
  1168.             throw new NotFoundException(
  1169.                 $this->configuration->getLanguage('error_lesson_not_found''lesson_view_error')
  1170.             );
  1171.         }
  1172.         //check user can access lesson
  1173.         $isStudent $this->repository->isStudent($lesson->getCourse());
  1174.         if(!$lessonRepository->isLessonVisibleToStudent($lessonfalse$isStudent)){
  1175.             throw new ActionInvalidException(
  1176.                 $this->configuration->getLanguage(
  1177.                     'error_lesson_unavailable'
  1178.                     'lesson_view_error'
  1179.                 )
  1180.             );
  1181.         }
  1182.         $files $lessonXLibraryRepository->getLessonXLibraryFiles($lesson);
  1183.         
  1184.         $data = [];
  1185.         foreach ($files as $key => $file) {
  1186.             $file $file->toReturn();
  1187.             
  1188.             $data[] = (object)[
  1189.                 "name" => $file->libraryTitle,
  1190.                 "size" => $file->libraryFileSize,
  1191.                 "type" => $file->libraryType,
  1192.                 "filename" => $file->libraryFile,
  1193.                 "id" => $file->id,
  1194.             ];
  1195.         }
  1196.         return $this->eadResponseNew($data);
  1197.     }
  1198.     /**
  1199.      * @Route(
  1200.      *      path          = "/admin/v2/lesson/{lessonId}/files/{id}",
  1201.      *      methods       = {"GET"},
  1202.      *      name          = "downloadLessonLibraryNew",
  1203.      *      requirements  = { "id" = "\d+" }
  1204.      * )
  1205.      */
  1206.     public function downloadLessonLibraryNew(Request $request) {
  1207.         $lessonXLibraryId $request->get('id');
  1208.         $lessonXLibrary $this->em->getRepository(LessonXLibrary::class)->findOneBy([
  1209.             "id" => $lessonXLibraryId,
  1210.             "type" => LessonXLibraryEnum::DOWNLOAD,
  1211.             "deleted" => LessonXLibraryEnum::ITEM_NO_DELETED,
  1212.         ]);
  1213.         if (!$lessonXLibrary) {
  1214.             throw new NotFoundException($this->configuration->getLanguage('error_library_lesson_not_found''lesson_view_error'));
  1215.         }
  1216.         
  1217.         $library $lessonXLibrary->getLibrary();
  1218.         $lesson $lessonXLibrary->getLesson();
  1219.         $lessonRepository $this->em->getRepository(Lesson::class);
  1220.         
  1221.         //check user can access lesson
  1222.         $isStudent $this->repository->isStudent($lesson->getCourse());
  1223.         if(!$lessonRepository->isLessonVisibleToStudent($lessonfalse$isStudent)){
  1224.             throw new ActionInvalidException($this->configuration->getLanguage('error_lesson_unavailable''lesson_view_error'));
  1225.         }
  1226.         if($library){
  1227.             $url null;
  1228.             $type $library->getType();
  1229.             if($type == LibraryEnum::CONTENT_VIDEO_FILE ||
  1230.                 $type == LibraryEnum::CONTENT_AUDIO){
  1231.                 $vdocipherService $this->generalService->getService('VdocipherService');
  1232.                 $url $vdocipherService->getVideoDownloadLink(
  1233.                     $library->getVdocipherVideoId()
  1234.                 );
  1235.                 
  1236.             }else if($type == LibraryEnum::CONTENT_FILES){
  1237.                 
  1238.                 $isPdf = ($library->getFileExtension() == "pdf");
  1239.                 $hasFunction $this->configuration->checkModuleIsAbleOnPlan(
  1240.                     'lessonControlFunction'
  1241.                 );
  1242.                 $showDocument = (
  1243.                     $lessonXLibrary->getControlShowDocument() == LibraryEnum::YES
  1244.                 );
  1245.                 
  1246.                 if($isPdf && $hasFunction && $showDocument){
  1247.                     
  1248.                     $stringUtil $this->generalService->getUtil('StringUtil');
  1249.                     $sessionHash $this->user->getSession()->getToken();
  1250.                     $sessionHash $stringUtil->encodeHex($sessionHashfalsefalse);
  1251.                     $path $this->generalService->generateUrl(
  1252.                         "downloadPdfLessonLibrary"
  1253.                         [ 
  1254.                             "id" => $lessonXLibraryId,
  1255.                             "sessionHash" => $sessionHash
  1256.                         ]
  1257.                     );
  1258.                     $domain = (
  1259.                         $request $request->getHost() : $this->client->getDomainPrimary()
  1260.                     );
  1261.                     $url "https://{$domain}{$path}";
  1262.                 }else{
  1263.                     $fileName LibraryEnum::PATH_LESSON_FILE "/{$library->getFileName()}";
  1264.                     $this->fileService->setFile($fileName);
  1265.                     $url $this->fileService->getFileUrlTemp();
  1266.                 }
  1267.             }
  1268.             if($url){
  1269.                 $oldDownload $library->getDownloadNumber();
  1270.                 $newDownload $oldDownload 1;
  1271.                 $library->setDownloadNumber($newDownload);
  1272.                 $this->em->flush();
  1273.                 return $this->eadResponseNew([ "url" => $url ]);
  1274.             }
  1275.         }
  1276.         throw new NotFoundException(
  1277.             $this->configuration->getLanguage('error_library_lesson_not_found''lesson_view_error')
  1278.         );
  1279.     }
  1280.     /**
  1281.      * @Route(
  1282.      *      path          = "/admin/v2/course/{courseId}/notes",
  1283.      *      name          = "getCourseLessonNotesNew",
  1284.      *      methods       = {"GET"},
  1285.      * )
  1286.      */
  1287.     public function getCourseLessonNotesNew(Request $request) {
  1288.         $this->requestUtil->setRequest($request)->setData();
  1289.         $courseId $request->get('courseId');
  1290.         $course $this->repository->findOneBy([
  1291.             "id" => $courseId,
  1292.             "deleted" => CourseEnum::ITEM_NO_DELETED
  1293.         ]);
  1294.         
  1295.         if(!$course){
  1296.             throw new NotFoundException(
  1297.                 $this->configuration->getLanguage('error_course_not_found''lesson_view_error')
  1298.             );
  1299.         }
  1300.         $isInTeam $this->em->getRepository(CourseTeam::class)->userExistInCourseTeam(
  1301.             $course
  1302.             $this->user
  1303.         );
  1304.         $enrollmentRepository $this->em->getRepository(Enrollment::class);
  1305.         $enrollment $enrollmentRepository->findOneBy([
  1306.             "user" => $this->user->getId(),
  1307.             "course" => $courseId,
  1308.             "deleted" => EnrollmentEnum::ITEM_NO_DELETED,
  1309.         ], [ "id" => "DESC" ]);
  1310.         $permission $this->userPermissionUtil->getPermission("course""see");
  1311.         
  1312.         if(!$enrollment){
  1313.             if($course->getFree() == CourseEnum::NO){
  1314.                 if($this->userPermissionUtil->isLow($permission)){
  1315.                     throw new PermissionException($this->configuration->getLanguage('error_user_not_permission''lesson_view_error'));
  1316.                 }
  1317.                 if(!$isInTeam && $this->userPermissionUtil->isMiddle($permission)){
  1318.                     throw new PermissionException($this->configuration->getLanguage('error_user_not_permission''lesson_view_error'));
  1319.                 }
  1320.             }
  1321.             $enrollmentService $this->generalService->getService('EnrollmentService');
  1322.             $info $enrollmentService->enrollUser($this->user$course);
  1323.             
  1324.             if(!$info->errors){
  1325.                 $enrollment $info->enrollment;
  1326.             }
  1327.         }
  1328.         if($this->userPermissionUtil->isLow($permission) && !$isInTeam){
  1329.             if(!$enrollment){
  1330.                 throw new ActionInvalidException($this->configuration->getLanguage('error_enrollment_not_found''lesson_view_error'));
  1331.             }
  1332.             if($course->getStatus() == CourseEnum::DRAFT){
  1333.                 throw new ActionInvalidException($this->configuration->getLanguage('error_course_not_published''lesson_view_error'));
  1334.             }
  1335.         }
  1336.         $searchText $this->requestUtil->getField('search');
  1337.         $lessonAnnotationRepository $this->em->getRepository(LessonAnnotation::class);
  1338.         $notes $lessonAnnotationRepository->getLessonAnnotationsByCourse(
  1339.             $course,
  1340.             $this->user
  1341.             $searchText
  1342.         );
  1343.         $aux = [];
  1344.         $auxLessons = [];
  1345.         foreach ($notes as $key => $note) {
  1346.             $lesson $note->getLesson();
  1347.             $lessonModule $lesson->getLessonModule();
  1348.             if(!isset($aux[$lessonModule->getId()])){
  1349.                 $aux[$lessonModule->getId()] = (object)[
  1350.                     "id" => $lessonModule->getId(),
  1351.                     "status" => $lessonModule->getStatus(),
  1352.                     "title" => $lessonModule->getTitle(),
  1353.                     "description" => $lessonModule->getDescription(),
  1354.                     "exam" => null,
  1355.                     "lessons" => [],
  1356.                 ];
  1357.             }
  1358.             $library $lesson->getLibrary();
  1359.             $libraryRepository $this->em->getRepository(Library::class);
  1360.             $timeUtil $this->generalService->getUtil('DateTimeUtil');
  1361.             $duration $library->getDuration();
  1362.             if(!isset($aux[$lesson->getId()])){
  1363.                 $auxLessons[$lesson->getId()] = (object)[
  1364.                     "id" => $lesson->getId(),
  1365.                     "lessonModule" => $lessonModule->getId(),
  1366.                     "title" => $lesson->getTitle(),
  1367.                     "status" => $lesson->getStatus(),
  1368.                     "lessonIsAccessible" => true,
  1369.                     "acessMessage" => null,
  1370.                     "contentPagesNumber" => null,
  1371.                     "contentDuration" => $duration $timeUtil->timeToSec($duration) : null,
  1372.                     "contentType" => null,
  1373.                     "contentThumb" => $libraryRepository->getCover($library),
  1374.                     "notes" => [],
  1375.                     "exam" => null,
  1376.                     "quiz" => null,
  1377.                     "allowCheck" => EnrollmentEnum::YES,
  1378.                 ];
  1379.             }
  1380.             $auxLessons[$lesson->getId()]->notes[] = (object)[
  1381.                 "id" => $note->getId(),
  1382.                 "annotation" => $note->getAnnotation(),
  1383.                 "options" => $note->getOptions(),
  1384.                 "time" => $note->getTime(),
  1385.                 "date" => $note->getDate(),
  1386.             ];
  1387.         }
  1388.         $data = [];
  1389.         foreach ($auxLessons as $key => $value) {
  1390.             if(isset($aux[$value->lessonModule])){
  1391.                 $aux[$value->lessonModule]->lessons[] = $value;
  1392.                 unset($value->lessonModule);
  1393.             }
  1394.         }
  1395.         foreach ($aux as $key => $value) {
  1396.             $data[] = $value;
  1397.         }
  1398.         return $this->eadResponseNew($data);
  1399.     }
  1400.     /**
  1401.      * @Route(
  1402.      *      path          = "/admin/v2/lesson/{lessonId}/notes",
  1403.      *      name          = "lessonAnnotationListNew",
  1404.      *      methods       = {"GET"},
  1405.      * )
  1406.      */
  1407.     public function lessonAnnotationListNew(Request $request) {
  1408.         $lessonId $request->get('lessonId');
  1409.         $lessonRepository $this->em->getRepository(Lesson::class);
  1410.         $lesson $lessonRepository->findOneBy([
  1411.             "id" => $lessonId,
  1412.             "deleted" => LessonEnum::ITEM_NO_DELETED
  1413.         ]);
  1414.         //check lesson exist
  1415.         if (!$lesson) {
  1416.             //redirect to index or home
  1417.              throw new ActionInvalidException(
  1418.                 $this->configuration->getLanguage('error_lesson_not_found''lesson_view_error')
  1419.             );
  1420.         }
  1421.         //check user can access lesson
  1422.         $isStudent $this->repository->isStudent($lesson->getCourse());
  1423.         if(!$lessonRepository->isLessonVisibleToStudent($lessonfalse$isStudent)){
  1424.             throw new ActionInvalidException(
  1425.                 $this->configuration->getLanguage(
  1426.                     'error_lesson_unavailable'
  1427.                     'lesson_view_error'
  1428.                 )
  1429.             );
  1430.         }
  1431.         $lessonAnnotationRepository $this->em->getRepository(LessonAnnotation::class);
  1432.         
  1433.         $annotations $lessonAnnotationRepository->findBy([
  1434.             "deleted" => LessonAnnotationEnum::ITEM_NO_DELETED,
  1435.             "lesson" => $lessonId,
  1436.             "user" => $this->user->getId(),
  1437.         ], [ "id" => "DESC" ]);
  1438.         $data = [];
  1439.         foreach ($annotations as $key => $annotation) {
  1440.             $data[] = (object)[
  1441.                 "id" => $annotation->getId(),
  1442.                 "annotation" => $annotation->getAnnotation(),
  1443.                 "options" => $annotation->getOptions(),
  1444.                 "time" => $annotation->getTime(),
  1445.                 "date" => $annotation->getDate(),
  1446.             ];
  1447.         }
  1448.         return $this->eadResponseNew($data);
  1449.     }
  1450.     /**
  1451.      * @Route(
  1452.      *      path          = "/admin/v2/lesson/{lessonId}/notes",
  1453.      *      name          = "registerLessonAnnotationNew",
  1454.      *      methods       = {"POST"},
  1455.      * )
  1456.      */
  1457.     public function registerLessonAnnotationNew(Request $request) {
  1458.         $lessonId $request->get('lessonId');
  1459.         $lessonRepository $this->em->getRepository(Lesson::class);
  1460.         $lesson $lessonRepository->findOneBy([
  1461.             "id" => $lessonId,
  1462.             "deleted" => LessonEnum::ITEM_NO_DELETED
  1463.         ]);
  1464.         //check lesson exist
  1465.         if (!$lesson) {
  1466.             //redirect to index or home
  1467.             throw new ActionInvalidException(
  1468.                 $this->configuration->getLanguage('error_lesson_not_found''lesson_view_error')
  1469.             );
  1470.         }
  1471.         //check user can access lesson
  1472.         $isStudent $this->repository->isStudent($lesson->getCourse());
  1473.         if(!$lessonRepository->isLessonVisibleToStudent($lessonfalse$isStudent)){
  1474.             throw new ActionInvalidException(
  1475.                 $this->configuration->getLanguage(
  1476.                     'error_lesson_unavailable'
  1477.                     'lesson_view_error'
  1478.                 )
  1479.             );
  1480.         }
  1481.         $this->requestUtil->setRequest($request)->setData();
  1482.         $lessonAnnotation = new LessonAnnotation();
  1483.         if($this->requestUtil->issetField('annotation')){
  1484.             $lessonAnnotation->setAnnotation($this->requestUtil->getField('annotation'));
  1485.         }
  1486.         if($this->requestUtil->issetField('options')){
  1487.             $lessonAnnotation->setOptions($this->requestUtil->getField('options'));
  1488.         }
  1489.         if($this->requestUtil->issetField('time')){
  1490.             $lessonAnnotation->setTime($this->requestUtil->getField('time'));
  1491.         }
  1492.         $lessonAnnotation->setLesson($lesson);
  1493.         $lessonAnnotation->setUser($this->user);
  1494.         $errors $this->validateEntity($lessonAnnotation);
  1495.         if($errors){
  1496.             throw new FieldException("FieldException"$errors);
  1497.         }
  1498.         $this->em->persist($lessonAnnotation);
  1499.         $this->em->flush();
  1500.         return $this->eadResponseNew((object)[
  1501.             "id" => $lessonAnnotation->getId(),
  1502.             "annotation" => $lessonAnnotation->getAnnotation(),
  1503.             "options" => $lessonAnnotation->getOptions(),
  1504.             "time" => $lessonAnnotation->getTime(),
  1505.             "date" => $lessonAnnotation->getDate(),
  1506.         ]);
  1507.     }
  1508.     /**
  1509.      * @Route(
  1510.      *      path          = "/admin/v2/lesson/{lessonId}/notes/{id}",
  1511.      *      name          = "lessonAnnotationEditNew",
  1512.      *      methods       = {"PUT"},
  1513.      *      requirements  = { "id" = "\d+" }
  1514.      * )
  1515.      */
  1516.     public function editLessonAnnotationNew(Request $request) {
  1517.         $this->requestUtil->setRequest($request)->setData();
  1518.         $lessonId $request->get('lessonId');
  1519.         $lessonAnnotationId $request->get('id');
  1520.         $lessonRepository $this->em->getRepository(Lesson::class);
  1521.         $lesson $lessonRepository->findOneBy([
  1522.             "id" => $lessonId,
  1523.             "deleted" => LessonEnum::ITEM_NO_DELETED
  1524.         ]);
  1525.         //check lesson exist
  1526.         if (!$lesson) {
  1527.             //redirect to index or home
  1528.              throw new ActionInvalidException(
  1529.                 $this->configuration->getLanguage('error_lesson_not_found''lesson_view_error')
  1530.             );
  1531.         }
  1532.         //check user can access lesson
  1533.         $isStudent $this->repository->isStudent($lesson->getCourse());
  1534.         if(!$lessonRepository->isLessonVisibleToStudent($lessonfalse$isStudent)){
  1535.             throw new ActionInvalidException(
  1536.                 $this->configuration->getLanguage(
  1537.                     'error_lesson_unavailable'
  1538.                     'lesson_view_error'
  1539.                 )
  1540.             );
  1541.         }
  1542.         
  1543.         $lessonAnnotationRepository $this->em->getRepository(lessonAnnotation::class);
  1544.         $lessonAnnotation $lessonAnnotationRepository->findOneBy([
  1545.             "id" => $lessonAnnotationId,
  1546.             "deleted" => LessonAnnotationEnum::ITEM_NO_DELETED
  1547.         ]);
  1548.         if (!$lessonAnnotation) {
  1549.             throw new NotFoundException(
  1550.                 $this->configuration->getLanguage('error_annotation_not_found''lesson_view_error')
  1551.             );
  1552.         }
  1553.         if($this->requestUtil->issetField('annotation')){
  1554.             $lessonAnnotation->setAnnotation($this->requestUtil->getField('annotation'));
  1555.         }
  1556.         if($this->requestUtil->issetField('time')){
  1557.             $lessonAnnotation->setTime($this->requestUtil->getField('time'));
  1558.         }
  1559.         if($this->requestUtil->issetField('options')){
  1560.             $lessonAnnotation->setOptions($this->requestUtil->getField('options'));
  1561.         }
  1562.         $errors $this->validateEntity($lessonAnnotation);
  1563.         if($errors){
  1564.             throw new FieldException("FieldException"$errors);
  1565.         }
  1566.         
  1567.         $this->em->flush();
  1568.         return $this->eadResponseNew((object)[
  1569.             "id" => $lessonAnnotation->getId(),
  1570.             "annotation" => $lessonAnnotation->getAnnotation(),
  1571.             "options" => $lessonAnnotation->getOptions(),
  1572.             "time" => $lessonAnnotation->getTime(),
  1573.             "date" => $lessonAnnotation->getDate(),
  1574.         ]);
  1575.     }
  1576.     /**
  1577.      * @Route(
  1578.      *      path          = "/admin/v2/lesson/{lessonId}/notes/{id}",
  1579.      *      name          = "lessonAnnotationDeleteNew",
  1580.      *      methods       = {"DELETE"},
  1581.      *      requirements  = { "id" = "\d+" }
  1582.      * )
  1583.      */
  1584.     public function deleteLessonAnnotationNew(Request $request) {
  1585.         $lessonId $request->get('lessonId');
  1586.         $lessonAnnotationId $request->get('id');
  1587.         $lessonRepository $this->em->getRepository(Lesson::class);
  1588.         $lesson $lessonRepository->findOneBy([
  1589.             "id" => $lessonId,
  1590.             "deleted" => LessonEnum::ITEM_NO_DELETED
  1591.         ]);
  1592.         //check lesson exist
  1593.         if (!$lesson) {
  1594.             //redirect to index or home
  1595.              throw new ActionInvalidException(
  1596.                 $this->configuration->getLanguage('error_lesson_not_found''lesson_view_error')
  1597.             );
  1598.         }
  1599.         //check user can access lesson
  1600.         $isStudent $this->repository->isStudent($lesson->getCourse());
  1601.         if(!$lessonRepository->isLessonVisibleToStudent($lessonfalse$isStudent)){
  1602.             throw new ActionInvalidException($this->configuration->getLanguage('error_lesson_unavailable''lesson_view_error'));
  1603.         }
  1604.         
  1605.         $lessonAnnotationRepository $this->em->getRepository(lessonAnnotation::class);
  1606.         $lessonAnnotation $lessonAnnotationRepository->findOneBy([
  1607.             "id" => $lessonAnnotationId,
  1608.             "deleted" => LessonAnnotationEnum::ITEM_NO_DELETED
  1609.         ]);
  1610.         if (!$lessonAnnotation) {
  1611.             throw new NotFoundException(
  1612.                 $this->configuration->getLanguage('error_annotation_not_found''lesson_view_error')
  1613.             );
  1614.         }
  1615.         $lessonAnnotation->delete();
  1616.         $this->em->flush();
  1617.         return $this->eadResponseNew([ "message" => "Success" ]);
  1618.     }
  1619.     /**
  1620.      * @Route(
  1621.      *      path          = "/admin/v2/lesson/{id}",
  1622.      *      methods       = {"PATCH"},
  1623.      *      name          = "updateLessonLogNew",
  1624.      *      requirements  = { "id" = "\d+" }
  1625.      * )
  1626.      */
  1627.     public function updateLessonLogNew(Request $request) {
  1628.         $this->requestUtil->setRequest($request)->setData();
  1629.         $lessonId $request->get('id');
  1630.         $lessonRepository $this->em->getRepository(Lesson::class);
  1631.         $lesson $lessonRepository->findOneBy([
  1632.             "id" => $lessonId,
  1633.             "deleted" => LessonEnum::ITEM_NO_DELETED
  1634.         ]);
  1635.         //check lesson exist
  1636.         if (!$lesson) {
  1637.             //redirect to index or home
  1638.              throw new ActionInvalidException(
  1639.                 $this->configuration->getLanguage('error_lesson_not_found''lesson_view_error')
  1640.             );
  1641.         }
  1642.         //check user can access lesson
  1643.         $course $lesson->getCourse();
  1644.         $isStudent $this->repository->isStudent($course);
  1645.         if(!$lessonRepository->isLessonVisibleToStudent($lessonfalse$isStudent)){
  1646.             throw new ActionInvalidException($this->configuration->getLanguage('error_lesson_unavailable''lesson_view_error'));
  1647.         }
  1648.         
  1649.         $lessonModule $lesson->getLessonModule();
  1650.         $enrollmentRepository $this->em->getRepository(Enrollment::class);
  1651.         $enrollment $enrollmentRepository->findOneBy([
  1652.             "user" => $this->user->getId(),
  1653.             "course" => $course->getId(),
  1654.             "deleted" => LessonEnum::ITEM_NO_DELETED
  1655.         ], [ "id" => "DESC" ]);
  1656.         if(!$enrollment){
  1657.             throw new ActionInvalidException(
  1658.                 $this->configuration->getLanguage('error_enrollment_not_found''lesson_view_error')
  1659.             );
  1660.         }
  1661.         $lessonLogRepository $this->emEadmin->getRepository(LessonLog::class);
  1662.         $logId "{$course->getId()}#{$this->user->getId()}#{$lesson->getId()}";
  1663.         $lessonLog $lessonLogRepository->find($logId);
  1664.         $new false;
  1665.         if(!$lessonLog){
  1666.             $new true;
  1667.             $lessonLog = new LessonLog();
  1668.             $lessonLog->setId($logId);
  1669.             $lessonLog->setCourse($course);
  1670.             $lessonLog->setLesson($lesson);
  1671.             $lessonLog->setUser($this->user);
  1672.         }
  1673.         if($this->requestUtil->issetField('timeWatch')){
  1674.             $timeWatch $this->requestUtil->getField('timeWatch');
  1675.             $lessonLog $lessonLogRepository->updateLessonLogNew(
  1676.                 $lessonLog,
  1677.                 $timeWatch
  1678.             );
  1679.         }
  1680.         if($this->requestUtil->issetField('rate')){
  1681.             $rate = (int)$this->requestUtil->getField('rate');
  1682.             $lessonLog->setRate($rate);
  1683.         }
  1684.         if($this->requestUtil->issetField('favorite')){
  1685.             $favorite = (int)$this->requestUtil->getField('favorite');
  1686.             $lessonLog->setFavorite($favorite);
  1687.         }
  1688.         if($this->requestUtil->issetField('completed')){
  1689.             $complete = (int)$this->requestUtil->getField('completed');
  1690.             $today date('Y-m-d H:i:s');
  1691.             $lessonLog->setDateAccess($today);
  1692.             $lessonLog->setViewed($complete);
  1693.             
  1694.             if(empty($lessonLog->getDateConclusion()) && $complete == LessonEnum::YES){
  1695.                 $lessonLog->setComplete($complete);
  1696.                 $lessonLog->setDateConclusion($today);
  1697.             }
  1698.         }
  1699.         if($new){
  1700.             $this->emEadmin->persist($lessonLog);
  1701.         }
  1702.         $this->em->getRepository(LessonLogOrigin::class)->copyFromDynamo($lessonLog);
  1703.         $enrollmentRepository->updateDataAccessLog($enrollment);
  1704.         $this->emEadmin->flush();
  1705.         $this->em->flush();
  1706.         $infoLog $lessonLogRepository->getTimeInfo($lesson$lessonLog);   
  1707.         $hasLessonControl $this->configuration->checkModuleIsAbleOnPlan(
  1708.             'lessonControlFunction'
  1709.         );
  1710.         $data = [
  1711.             "nextContent" => $lessonRepository->getLessonNextContent($lesson$lessonLog),
  1712.             "lastContent" => $lessonRepository->getLessonLastContent($lesson),
  1713.             "id" => $lesson->getId(),
  1714.             "moduleId" => $lessonModule->getId(),
  1715.             "courseId" => $course->getId(),
  1716.             "title" => $lesson->getTitle(),
  1717.             "description" => $lesson->getDescription(),
  1718.             "controlTime" => $hasLessonControl $lesson->getControlTime() : LessonEnum::NO,
  1719.             "controlViewLimit" => (
  1720.                 $hasLessonControl 
  1721.                 $lesson->getControlViewLimit() : 
  1722.                 LessonEnum::NO
  1723.             ),
  1724.             "controlViewNumber" => (
  1725.                 $hasLessonControl 
  1726.                 $lesson->getControlViewNumber() : 
  1727.                 LessonEnum::NO
  1728.             ),
  1729.             "controlPauseNumber" => (
  1730.                 $hasLessonControl 
  1731.                 $lesson->getControlPauseNumber() : 
  1732.                 LessonEnum::NO
  1733.             ),
  1734.             "controlShowDocument" => (
  1735.                 $hasLessonControl 
  1736.                 $lesson->getControlShowDocument() : 
  1737.                 LessonEnum::NO
  1738.             ),
  1739.             "showLiveChat" => $lessonRepository->getShowLiveChat($lesson$isStudent),
  1740.             "teacher" => $lessonRepository->getLessonTeacher($lesson),
  1741.             "rates" => $lessonLogRepository->countLessonLogRate(
  1742.                 $course->getId(),
  1743.                 $lesson->getId()
  1744.             ),
  1745.             "required" => $lesson->getControlRequirement(),
  1746.             "rate" => ($lessonLog $lessonLog->getRate() : LessonEnum::NO),
  1747.             "completed" => ($lessonLog $lessonLog->getViewed() : LessonEnum::NO),
  1748.             "numberAccess" => ($lessonLog $lessonLog->getNumberAccess() : null),
  1749.             "timeToStaySeconds" => $infoLog->timeToStaySeconds,
  1750.             "timeRestToComplete" => $infoLog->timeRestToComplete,
  1751.             "blockContent" => $lessonLogRepository->checkBlockView($lessonLog),
  1752.             "chat" => null,
  1753.             "library" => null,
  1754.             "productOffers" => null,
  1755.             "chapters" => null,
  1756.         ];
  1757.         return $this->eadResponseNew($data);
  1758.     }
  1759.     /**
  1760.      * @Route(
  1761.      *      path          = "/chat/v2/lesson/{id}/{fileName}",
  1762.      *      methods       = {"POST"},
  1763.      *      name          = "getSignedUploadUrlLessonChat",
  1764.      *      requirements  = { "id" = "\d+" }
  1765.      * )
  1766.      */
  1767.     public function getSignedUploadUrlLessonChat(Request $request) {
  1768.         $token $request->headers->get('X-AUTH-TOKEN');
  1769.         if($token != $this->generalService->getTokenCron()){
  1770.             throw new AuthInvalidException("Token Invalid");
  1771.         }
  1772.         $this->requestUtil->setRequest($request)->setData();
  1773.         $lessonId $request->get('id');
  1774.         $fileName $request->get('fileName');
  1775.         if(empty($fileName)){
  1776.             throw new FieldException("Empty Fields", [ "fileName" => "Empty "]);
  1777.         }
  1778.         $pathDefault LessonEnum::PATH_UPLOAD;
  1779.         $clientConnection $this->configuration->getClientConnection();
  1780.         $serverUser $clientConnection->getServerUser();
  1781.         $basekey "client/{$serverUser}{$pathDefault}chat/{$lessonId}/{$fileName}";
  1782.         $s3 $this->generalService->getService('Aws\\AwsS3');
  1783.         $url $s3->uploadItemSignedRequest($basekey$fileName);
  1784.         if(empty($url)){
  1785.         }
  1786.         $data = [
  1787.             "urlUpload" => $url,
  1788.         ];
  1789.         return new HttpOk($data);
  1790.     }
  1791.     /**
  1792.      * @Route(
  1793.      *      path          = "/chat/v2/lesson/{id}/{fileName}",
  1794.      *      methods       = {"DELETE"},
  1795.      *      name          = "deleteFileLessonChat",
  1796.      *      requirements  = { "id" = "\d+" }
  1797.      * )
  1798.      */
  1799.     public function deleteFileLessonChat(Request $request) {
  1800.         $token $request->headers->get('X-AUTH-TOKEN');
  1801.         if($token != $this->generalService->getTokenCron()){
  1802.             throw new AuthInvalidException("Token Invalid");
  1803.         }
  1804.         $this->requestUtil->setRequest($request)->setData();
  1805.         $lessonId $request->get('id');
  1806.         $fileName $request->get('fileName');
  1807.         if(empty($fileName)){
  1808.             throw new FieldException("Empty Fields", [ "fileName" => "Empty "]);
  1809.         }
  1810.         $objectKey "chat/{$lessonId}/{$fileName}";
  1811.         $s3 $this->generalService->getService('Aws\\AwsS3');
  1812.         $s3->deleteObjectS3($objectKey);
  1813.         return new HttpNoContent;
  1814.     }
  1815.     /**
  1816.      * @Route(
  1817.      *      path          = "/admin/v2/course/{courseId}/favorites",
  1818.      *      name          = "getCourseLessonFavoritesNew",
  1819.      *      methods       = {"GET"},
  1820.      * )
  1821.      */
  1822.     public function getCourseLessonFavoritesNew(Request $request) {
  1823.         $this->requestUtil->setRequest($request)->setData();
  1824.         $courseId $request->get('courseId');
  1825.         $course $this->repository->findOneBy([
  1826.             "id" => $courseId,
  1827.             "deleted" => CourseEnum::ITEM_NO_DELETED
  1828.         ]);
  1829.         
  1830.         if(!$course){
  1831.             throw new NotFoundException(
  1832.                 $this->configuration->getLanguage('error_course_not_found''lesson_view_error')
  1833.             );
  1834.         }
  1835.         $isInTeam $this->em->getRepository(CourseTeam::class)->userExistInCourseTeam(
  1836.             $course
  1837.             $this->user
  1838.         );
  1839.         $enrollmentRepository $this->em->getRepository(Enrollment::class);
  1840.         $enrollment $enrollmentRepository->findOneBy([
  1841.             "user" => $this->user->getId(),
  1842.             "course" => $courseId,
  1843.             "deleted" => EnrollmentEnum::ITEM_NO_DELETED,
  1844.         ], [ "id" => "DESC" ]);
  1845.         $permission $this->userPermissionUtil->getPermission("course""see");
  1846.         
  1847.         if(!$enrollment){
  1848.             if($course->getFree() == CourseEnum::NO){
  1849.                 if($this->userPermissionUtil->isLow($permission)){
  1850.                     throw new \EADPlataforma\Error\PermissionException("PermissionException");
  1851.                 }
  1852.                 if(!$isInTeam && $this->userPermissionUtil->isMiddle($permission)){
  1853.                     throw new \EADPlataforma\Error\PermissionException("PermissionException");
  1854.                 }
  1855.             }
  1856.             $enrollmentService $this->generalService->getService('EnrollmentService');
  1857.             $info $enrollmentService->enrollUser($this->user$course);
  1858.             
  1859.             if(!$info->errors){
  1860.                 $enrollment $info->enrollment;
  1861.             }
  1862.         }
  1863.         if($this->userPermissionUtil->isLow($permission) && !$isInTeam){
  1864.             if(!$enrollment){
  1865.                 throw new ActionInvalidException(
  1866.                     $this->configuration->getLanguage(
  1867.                         'error_enrollment_not_found''lesson_view_error'
  1868.                     )
  1869.                 );
  1870.             }
  1871.             if($course->getStatus() == CourseEnum::DRAFT){
  1872.                 throw new ActionInvalidException(
  1873.                     $this->configuration->getLanguage(
  1874.                         'error_course_not_published''lesson_view_error'
  1875.                     )
  1876.                 );
  1877.             }
  1878.         }
  1879.         $searchText $this->requestUtil->getField('search');
  1880.         $lessonLogRepository $this->emEadmin->getRepository(LessonLog::class);
  1881.         $lessonIds $lessonLogRepository->findFavoriteLessonIds(
  1882.             $this->user->getId(), 
  1883.             $course->getId()
  1884.         );
  1885.         $lessonRepository $this->em->getRepository(Lesson::class);
  1886.         $lessons = [];
  1887.         if(!empty($lessonIds) && is_array($lessonIds)){
  1888.             $lessons $lessonRepository->getCourseLessons(
  1889.                 $course
  1890.                 null
  1891.                 $lessonIds
  1892.                 $searchText
  1893.             );
  1894.         }
  1895.         $aux = [];
  1896.         foreach ($lessons as $key => $lesson) {
  1897.             $lessonModule $lesson->getLessonModule();
  1898.             if(!isset($aux[$lessonModule->getId()])){
  1899.                 $aux[$lessonModule->getId()] = (object)[
  1900.                     "id" => $lessonModule->getId(),
  1901.                     "status" => $lessonModule->getStatus(),
  1902.                     "title" => $lessonModule->getTitle(),
  1903.                     "description" => $lessonModule->getDescription(),
  1904.                     "exam" => null,
  1905.                     "lessons" => [],
  1906.                 ];
  1907.             }
  1908.             $library $lesson->getLibrary();
  1909.             $libraryRepository $this->em->getRepository(Library::class);
  1910.             $timeUtil $this->generalService->getUtil('DateTimeUtil');
  1911.             $duration $library->getDuration();
  1912.             $aux[$lessonModule->getId()]->lessons[] = (object)[
  1913.                 "id" => $lesson->getId(),
  1914.                 "title" => $lesson->getTitle(),
  1915.                 "status" => $lesson->getStatus(),
  1916.                 "lessonIsAccessible" => true,
  1917.                 "acessMessage" => null,
  1918.                 "contentPagesNumber" => null,
  1919.                 "contentDuration" => $duration $timeUtil->timeToSec($duration) : null,
  1920.                 "contentType" => null,
  1921.                 "contentThumb" => $libraryRepository->getCover($library) ?? null,
  1922.                 "exam" => null,
  1923.                 "quiz" => null,
  1924.                 "allowCheck" => EnrollmentEnum::YES,
  1925.             ];
  1926.         }
  1927.         $data = [];
  1928.         foreach ($aux as $key => $value) {
  1929.             $data[] = $value;
  1930.         }
  1931.         return $this->eadResponseNew($data);
  1932.     }
  1933.     /**
  1934.      * @Route(
  1935.      *      path          = "/admin/v2/lesson/{id}/supports",
  1936.      *      methods       = {"GET"}
  1937.      * )
  1938.      */
  1939.     public function getLessonSupportNew(Request $request) {
  1940.         $this->requestUtil->setRequest($request)->setData();
  1941.         $lessonId $request->get('id');
  1942.         $lessonRepository $this->em->getRepository(Lesson::class);
  1943.         $lesson $lessonRepository->findOneBy([
  1944.             "id" => $lessonId,
  1945.             "deleted" => LessonEnum::ITEM_NO_DELETED
  1946.         ]);
  1947.         if(!$lesson){
  1948.             throw new NotFoundException(
  1949.                 $this->configuration->getLanguage('error_lesson_not_found''lesson_view_error')
  1950.             );
  1951.         }
  1952.         //check user can access lesson
  1953.         $course $lesson->getCourse();
  1954.         $isStudent $this->repository->isStudent($course);
  1955.         if(!$lessonRepository->isLessonVisibleToStudent($lessonfalse$isStudent)){
  1956.             throw new ActionInvalidException($this->configuration->getLanguage('error_lesson_unavailable''lesson_view_error'));
  1957.         }
  1958.         $enrollmentRepository $this->em->getRepository(Enrollment::class);
  1959.         $enrollment $enrollmentRepository->findOneBy([
  1960.             "user" => $this->user->getId(),
  1961.             "course" => $course->getId(),
  1962.             "deleted" => LessonEnum::ITEM_NO_DELETED
  1963.         ], [ "id" => "DESC" ]);
  1964.         if(!$enrollment){
  1965.             throw new ActionInvalidException(
  1966.                 $this->configuration->getLanguage(
  1967.                     'error_enrollment_not_found''lesson_view_error'
  1968.                 )
  1969.             );
  1970.         }
  1971.         $orderType = (int)$this->requestUtil->getField('orderType');
  1972.         $searchText $this->requestUtil->getField('searchText');
  1973.         $limit = (int)$this->requestUtil->getField('limit');
  1974.         $offset = (int)$this->requestUtil->getField('offset');
  1975.         $lessonSupportRepository $this->em->getRepository(LessonSupport::class);
  1976.         $rows $lessonSupportRepository->getLessonSupportOrderTypeNew(
  1977.             $lesson->getId(), 
  1978.             $orderType,
  1979.             $searchText,
  1980.             $limit,
  1981.             $offset
  1982.         );
  1983.         $total $lessonSupportRepository->countLessonSupportOrderType(
  1984.             $lessonId
  1985.             $orderType
  1986.             $searchText
  1987.         );
  1988.         $permission $this->userPermissionUtil->getPermission(
  1989.             "course""support""delete"
  1990.         );
  1991.         $isLessonTeacher $lessonRepository->isLessonTeacher($lesson$this->user);
  1992.         $data = [
  1993.             "allowDeleteSupport" => (
  1994.                 $isLessonTeacher || $this->userPermissionUtil->isHigh($permission)
  1995.             ),
  1996.             "rowsTotal" => $total,
  1997.             "rowsTotalDisplay" => count($rows),
  1998.             "searchText" => $searchText,
  1999.             "limit" => (!empty($limit) ? $limit 10),
  2000.             "offset" => $offset,
  2001.             "orderType" => $orderType,
  2002.             "rows" => $rows
  2003.         ];
  2004.         $likeControlRepository $this->em->getRepository(LikeControl::class);
  2005.         $typesText = [
  2006.             LessonSupportEnum::LESSON_STUDENT => $this->configuration->getLanguage(
  2007.                 'student''lesson_view_error'
  2008.             ),
  2009.             LessonSupportEnum::COURSE_TEACHER => $this->configuration->getLanguage(
  2010.                 'teacher''lesson_view_error'
  2011.             ),
  2012.             LessonSupportEnum::MODULE_TEACHER => $this->configuration->getLanguage(
  2013.                 'teacher_module''lesson_view_error'
  2014.             ),
  2015.             LessonSupportEnum::LESSON_TEACHER => $this->configuration->getLanguage(
  2016.                 'coordinator''lesson_view_error'
  2017.             ),
  2018.             LessonSupportEnum::LESSON_TUTOR => $this->configuration->getLanguage(
  2019.                 'tutor''lesson_view_error'
  2020.             ),
  2021.         ];
  2022.         foreach ($data['rows'] as $key => $item) {
  2023.             $item = (object)$item;
  2024.             $dataImg = [
  2025.                 "fileName" => $item->photo,
  2026.                 "pathConst" => LessonSupportEnum::PATH_PROFILES,
  2027.                 "option" => "l-user-profile-support",
  2028.                 "addUpload" => true,
  2029.                 "addStream" => true,
  2030.             ];
  2031.             $item->photo $this->fileService->getFilePathObj($dataImg);
  2032.             $item->support html_entity_decode($item->support);
  2033.             $item->answers $lessonSupportRepository->getLessonSupportAnswers($item->id);
  2034.             $item->lessonUserType = (int)$item->lessonUserType;
  2035.             $item->lessonUserTypeText $typesText[$item->lessonUserType];
  2036.             
  2037.             $likeControl $likeControlRepository->findOneByEAD([
  2038.                 "userFrom" => $this->user->getId(),
  2039.                 "element" => $item->id,
  2040.                 "type" => LikeControlEnum::LESSON_SUPPORT,
  2041.             ]);
  2042.             $item->allowLike true;
  2043.             if($likeControl){
  2044.                 $item->allowLike false;
  2045.             }
  2046.             $data['rows'][$key] = $item;
  2047.         }
  2048.         return $this->eadResponseNew($data);
  2049.     }
  2050.     /**
  2051.      * @Route(
  2052.      *      path          = "/admin/v2/lesson/{id}/supports/{lessonSupportId}/pin",
  2053.      *      methods       = {"PUT"}
  2054.      * )
  2055.      */
  2056.     public function pinLessonSupport(Request $request) {
  2057.         $permission $this->userPermissionUtil->getPermission("course""support""finalize");
  2058.         if($this->userPermissionUtil->isLow($permission)){
  2059.             throw new PermissionException(
  2060.                 $this->configuration->getLanguage('error_user_not_permission''lesson_view_error')
  2061.             );
  2062.         }
  2063.         
  2064.         $this->requestUtil->setRequest($request)->setData();
  2065.         $lessonSupportId = (int)$request->get('lessonSupportId');
  2066.         $lessonSupportRepository $this->em->getRepository(LessonSupport::class);
  2067.         $lessonSupport $lessonSupportRepository->findOneBy([
  2068.             "id" => $lessonSupportId,
  2069.             "deleted" => LessonSupportEnum::ITEM_NO_DELETED
  2070.         ]);
  2071.         
  2072.         if (!$lessonSupport) {
  2073.             throw new NotFoundException(
  2074.                 $this->configuration->getLanguage('error_support_not_found''lesson_view_error')
  2075.             );
  2076.         }
  2077.         $lesson $lessonSupport->getLesson();
  2078.         $isLessonTeacher $this->em->getRepository(Lesson::class)->isLessonTeacher(
  2079.             $lesson
  2080.             $this->user
  2081.         );
  2082.         
  2083.         if(!$isLessonTeacher && $this->userPermissionUtil->isMiddle($permission)){
  2084.             throw new PermissionException(
  2085.                 $this->configuration->getLanguage('error_user_not_permission''lesson_view_error')
  2086.             );
  2087.         }
  2088.         $lessonSupport->setLessonFixed(LessonSupportEnum::YES);
  2089.         $this->em->flush();
  2090.         $data $lessonSupport->toReturn();
  2091.         return $this->eadResponseNew([ "message" => "Success" ]);
  2092.     }
  2093.     /**
  2094.      * @Route(
  2095.      *      path          = "/admin/v2/lesson/{id}/supports/{lessonSupportId}/unpin",
  2096.      *      methods       = {"DELETE"}
  2097.      * )
  2098.      */
  2099.     public function unpinLessonSupport(Request $request) {
  2100.         $permission $this->userPermissionUtil->getPermission("course""support""finalize");
  2101.         if($this->userPermissionUtil->isLow($permission)){
  2102.             throw new PermissionException(
  2103.                 $this->configuration->getLanguage('error_user_not_permission''lesson_view_error')
  2104.             );
  2105.         }
  2106.         
  2107.         $this->requestUtil->setRequest($request)->setData();
  2108.         $lessonSupportId = (int)$request->get('lessonSupportId');
  2109.         $lessonSupportRepository $this->em->getRepository(LessonSupport::class);
  2110.         $lessonSupport $lessonSupportRepository->findOneBy([
  2111.             "id" => $lessonSupportId,
  2112.             "deleted" => LessonSupportEnum::ITEM_NO_DELETED
  2113.         ]);
  2114.         
  2115.         if (!$lessonSupport) {
  2116.             throw new NotFoundException(
  2117.                 $this->configuration->getLanguage('error_support_not_found''lesson_view_error')
  2118.             );
  2119.         }
  2120.         $lesson $lessonSupport->getLesson();
  2121.         $isLessonTeacher $this->em->getRepository(Lesson::class)->isLessonTeacher(
  2122.             $lesson
  2123.             $this->user
  2124.         );
  2125.         
  2126.         if(!$isLessonTeacher && $this->userPermissionUtil->isMiddle($permission)){
  2127.             throw new PermissionException(
  2128.                 $this->configuration->getLanguage('error_user_not_permission''lesson_view_error')
  2129.             );
  2130.         }
  2131.         $lessonSupport->setLessonFixed(LessonSupportEnum::NO);
  2132.         $this->em->flush();
  2133.         $data $lessonSupport->toReturn();
  2134.         return $this->eadResponseNew([ "message" => "Success" ]);
  2135.     }
  2136.     /**
  2137.      * @Route(
  2138.      *      path          = "/admin/v2/lesson/{id}/supports/{lessonSupportId}",
  2139.      *      methods       = {"PATCH"}
  2140.      * )
  2141.      */
  2142.     public function likeLessonSupport(Request $request) {
  2143.         $this->requestUtil->setRequest($request)->setData();
  2144.         $lessonSupportId = (int)$request->get('lessonSupportId');
  2145.         $lessonSupportRepository $this->em->getRepository(LessonSupport::class);
  2146.         $lessonSupport $lessonSupportRepository->findOneBy([
  2147.             "id" => $lessonSupportId,
  2148.             "deleted" => LessonSupportEnum::ITEM_NO_DELETED
  2149.         ]);
  2150.         
  2151.         if (!$lessonSupport) {
  2152.             throw new NotFoundException(
  2153.                 $this->configuration->getLanguage('error_support_not_found''lesson_view_error')
  2154.             );
  2155.         }
  2156.         $lesson $lessonSupport->getLesson();
  2157.         
  2158.         $likeControl = new LikeControl();
  2159.         $type LikeControlEnum::LESSON_SUPPORT;
  2160.         if($lessonSupport->getLessonSupport()){
  2161.             $type LikeControlEnum::LESSON_SUPPORT_ANSWER;
  2162.         }
  2163.         $likeControl->setElement($lessonSupportId);
  2164.         $likeControl->setType($type);
  2165.         $likeControl->setUserTo($lessonSupport->getUser());
  2166.         $likeControl->setUserFrom($this->user);
  2167.         
  2168.         $errors $this->validateEntity($likeControl);
  2169.         if($errors){
  2170.             throw new FieldException("FieldException"$errors);
  2171.         }
  2172.         $lessonSupport->setLikeNumber($lessonSupport->getLikeNumber() + 1);
  2173.         
  2174.         $this->em->persist($likeControl);
  2175.         $this->em->flush();
  2176.         return $this->eadResponseNew([ "message" => "Success" ]);
  2177.     }
  2178.     /**
  2179.      * @Route(
  2180.      *      path          = "/admin/v2/lesson/{id}/supports",
  2181.      *      methods       = {"POST"},
  2182.      * )
  2183.      */
  2184.     public function registerLessonSupport(Request $request) {
  2185.         
  2186.         $this->requestUtil->setRequest($request)->setData();
  2187.         $lessonRepository $this->em->getRepository(Lesson::class);
  2188.         $lessonSupportRepository $this->em->getRepository(LessonSupport::class);
  2189.         $enrollmentRepository $this->em->getRepository(Enrollment::class);
  2190.         $lessonId $request->get('id');
  2191.         $lesson $lessonRepository->findOneBy([
  2192.             "id" => $lessonId,
  2193.             "deleted" => LessonEnum::ITEM_NO_DELETED
  2194.         ]);
  2195.         if(!$lesson){
  2196.             throw new NotFoundException(
  2197.                 $this->configuration->getLanguage('error_lesson_not_found''lesson_view_error')
  2198.             );
  2199.         }
  2200.         //check user can access lesson
  2201.         $isStudent $this->repository->isStudent($lesson->getCourse());
  2202.         if(!$lessonRepository->isLessonVisibleToStudent($lessonfalse$isStudent)){
  2203.             throw new ActionInvalidException(
  2204.                 $this->configuration->getLanguage(
  2205.                     'error_lesson_unavailable''lesson_view_error'
  2206.                 )
  2207.             );
  2208.         }
  2209.         $lessonSupport = new LessonSupport();
  2210.         
  2211.         $lessonSupport->setUser($this->user);
  2212.         $lessonSupport->setLesson($lesson);
  2213.         //set LessonSupport in LessonSupport
  2214.         if($this->requestUtil->issetField('lessonSupport')){
  2215.             $lessonSupportId = (int)$this->requestUtil->getField('lessonSupport');
  2216.             $lessonSupportParent $lessonSupportRepository->findOneBy([
  2217.                 "id" => $lessonSupportId,
  2218.                 "deleted" => LessonSupportEnum::ITEM_NO_DELETED
  2219.             ]);
  2220.             $lessonSupport->setLessonSupport($lessonSupportParent);
  2221.         }
  2222.         
  2223.         if($this->requestUtil->issetField('support')){
  2224.             $lessonSupport->setSupport($this->requestUtil->getField('support'));
  2225.         }
  2226.         $errors $this->validateEntity($lessonSupport);
  2227.         if($errors){
  2228.             throw new FieldException("FieldException"$errors);
  2229.         }
  2230.         
  2231.         $lessonSupportParent $lessonSupport->getLessonSupport();
  2232.         $lesson $lessonSupport->getLesson();
  2233.         $course $lesson->getCourse();
  2234.         
  2235.         $isLessonTeacher $lessonRepository->isLessonTeacher(
  2236.             $lesson
  2237.             $this->user
  2238.         );
  2239.         $isEnrolled $enrollmentRepository->isValidEnrollmentByUser(
  2240.             $this->user->getId(), 
  2241.             $course->getId()
  2242.         );
  2243.         $permission $this->userPermissionUtil->getPermission("course""support""answer");
  2244.         if($lessonSupportParent){
  2245.             if(!$isEnrolled){
  2246.                 if($this->userPermissionUtil->isLow($permission)){
  2247.                     throw new PermissionException($this->configuration->getLanguage('error_user_not_permission''lesson_view_error'));
  2248.                 }
  2249.                 if(!$isLessonTeacher && $this->userPermissionUtil->isMiddle($permission)){
  2250.                     throw new PermissionException($this->configuration->getLanguage('error_user_not_permission''lesson_view_error'));
  2251.                 }
  2252.             }
  2253.         }else{
  2254.             if($course->getSupport() == LessonSupportEnum::NO){
  2255.                 throw new ActionInvalidException($this->configuration->getLanguage('error_course_not_support''lesson_view_error'));
  2256.             }
  2257.             if(!$isEnrolled && !$isLessonTeacher){
  2258.                 throw new ActionInvalidException(
  2259.                     $this->configuration->getLanguage('error_unavailable''lesson_view_error')
  2260.                 );
  2261.             }
  2262.         }
  2263.         $this->em->persist($lessonSupport);
  2264.         $this->em->flush();
  2265.         $lessonSupportRepository->processNewLessonSupport($lessonSupport);
  2266.          
  2267.         return $this->eadResponseNew([ "message" => "Success" ]);
  2268.     }
  2269.     /**
  2270.      * @Route(
  2271.      *      path          = "/admin/v2/lesson/{id}/supports/{lessonSupportId}",
  2272.      *      methods       = {"PUT"}
  2273.      * )
  2274.      */
  2275.     public function editLessonSupport(Request $request) {
  2276.         $this->requestUtil->setRequest($request)->setData();
  2277.         $lessonSupportId = (int)$request->get('lessonSupportId');
  2278.         $lessonSupportRepository $this->em->getRepository(LessonSupport::class);
  2279.         $lessonSupport $lessonSupportRepository->findOneBy([
  2280.             "id" => $lessonSupportId,
  2281.             "deleted" => LessonSupportEnum::ITEM_NO_DELETED
  2282.         ]);
  2283.         
  2284.         if (!$lessonSupport) {
  2285.             throw new NotFoundException(
  2286.                 $this->configuration->getLanguage('error_support_not_found''lesson_view_error')
  2287.             );
  2288.         }
  2289.         $lesson $lessonSupport->getLesson();
  2290.         
  2291.         if($this->requestUtil->issetField('support')){
  2292.             $lessonSupport->setSupport($this->requestUtil->getField('support'));
  2293.         }
  2294.         $lessonSupport->setDateUpdate(date('Y-m-d H:i:s'));
  2295.         
  2296.         $errors $this->validateEntity($lessonSupport);
  2297.         if($errors){
  2298.             throw new FieldException("FieldException"$errors);
  2299.         }
  2300.         $this->em->flush();
  2301.         return $this->eadResponseNew([ "message" => "Success" ]);
  2302.     }
  2303.     /**
  2304.      * @Route(
  2305.      *      path          = "/admin/v2/lesson/{id}/supports/{lessonSupportId}",
  2306.      *      methods       = {"DELETE"}
  2307.      * )
  2308.      */
  2309.     public function deleteLessonSupport(Request $request) {
  2310.         $this->requestUtil->setRequest($request)->setData();
  2311.         $lessonSupportId = (int)$request->get('lessonSupportId');
  2312.         $lessonSupportRepository $this->em->getRepository(LessonSupport::class);
  2313.         $lessonSupport $lessonSupportRepository->findOneBy([
  2314.             "id" => $lessonSupportId,
  2315.             "deleted" => LessonSupportEnum::ITEM_NO_DELETED
  2316.         ]);
  2317.         
  2318.         if (!$lessonSupport) {
  2319.             throw new NotFoundException(
  2320.                 $this->configuration->getLanguage('error_support_not_found''lesson_view_error')
  2321.             );
  2322.         }
  2323.         
  2324.         $lessonSupportRepository->delete(
  2325.             $lessonSupport
  2326.             TrashEnum::LESSON_SUPPORT
  2327.             $this->userPermissionUtil->getPermission("course""support""delete"), 
  2328.             TrashEnum::NO
  2329.         );
  2330.         $this->em->flush();
  2331.         return $this->eadResponseNew([ "message" => "Success" ]);
  2332.     }
  2333.     /**
  2334.      * @Route(
  2335.      *      path          = "/admin/v2/course/{id}/testimonial",
  2336.      *      methods       = {"POST"},
  2337.      * )
  2338.      */
  2339.     public function registerCourseTestimonial(Request $request){
  2340.         if($this->configuration->get("allow_testimonial") == CourseTestimonialEnum::NO){
  2341.             throw new ActionInvalidException('ActionInvalidException');
  2342.         }
  2343.         $this->requestUtil->setRequest($request)->setData();
  2344.         $courseId $request->get('id');
  2345.         $course $this->repository->findOneBy([
  2346.             "id" => $courseId,
  2347.             "deleted" => CourseEnum::ITEM_NO_DELETED
  2348.         ]);
  2349.         
  2350.         if(!$course){
  2351.             throw new NotFoundException(
  2352.                 $this->configuration->getLanguage('error_course_not_found''lesson_view_error')
  2353.             );
  2354.         }
  2355.         $isInTeam $this->em->getRepository(CourseTeam::class)->userExistInCourseTeam(
  2356.             $course
  2357.             $this->user
  2358.         );
  2359.         $enrollmentRepository $this->em->getRepository(Enrollment::class);
  2360.         $enrollment $enrollmentRepository->findOneBy([
  2361.             "user" => $this->user->getId(),
  2362.             "course" => $courseId,
  2363.             "deleted" => EnrollmentEnum::ITEM_NO_DELETED,
  2364.         ], [ "id" => "DESC" ]);
  2365.         $permission $this->userPermissionUtil->getPermission("course""see");
  2366.         
  2367.         if(!$enrollment){
  2368.             if($course->getFree() == CourseEnum::NO){
  2369.                 if($this->userPermissionUtil->isLow($permission)){
  2370.                     throw new PermissionException($this->configuration->getLanguage('error_user_not_permission''lesson_view_error'));
  2371.                 }
  2372.                 if(!$isInTeam && $this->userPermissionUtil->isMiddle($permission)){
  2373.                     throw new PermissionException(
  2374.                         $this->configuration->getLanguage(
  2375.                             'error_user_not_permission'
  2376.                             'lesson_view_error'
  2377.                         )
  2378.                     );
  2379.                 }
  2380.             }
  2381.             $enrollmentService $this->generalService->getService('EnrollmentService');
  2382.             $info $enrollmentService->enrollUser($this->user$course);
  2383.             
  2384.             if(!$info->errors){
  2385.                 $enrollment $info->enrollment;
  2386.             }
  2387.         }
  2388.         if($this->userPermissionUtil->isLow($permission) && !$isInTeam){
  2389.             if(!$enrollment){
  2390.                 throw new ActionInvalidException(
  2391.                     $this->configuration->getLanguage(
  2392.                         'error_enrollment_not_found'
  2393.                         'lesson_view_error'
  2394.                     )
  2395.                 );
  2396.             }
  2397.             if($course->getStatus() == CourseEnum::DRAFT){
  2398.                 throw new ActionInvalidException(
  2399.                     $this->configuration->getLanguage(
  2400.                         'error_course_not_published'
  2401.                         'lesson_view_error'
  2402.                     )
  2403.                 );
  2404.             }
  2405.         }
  2406.         $courseTestimonial = new CourseTestimonial();
  2407.         $courseTestimonial->setDate(date('Y-m-d H:i:s'));
  2408.         $courseTestimonial->setStatus(CourseTestimonialEnum::WAITING);
  2409.         if($this->requestUtil->issetField('score')){
  2410.             $courseTestimonial->setScore((int)$this->requestUtil->getField("score"));
  2411.         }
  2412.         if($this->requestUtil->issetField('testimonial')){
  2413.             $courseTestimonial->setTestimonial($this->requestUtil->getField('testimonial'));
  2414.         }
  2415.         if(
  2416.             $this->configuration->get("required_text_testimonial") == CourseEnum::YES &&
  2417.             empty($courseTestimonial->getTestimonial())
  2418.         ){
  2419.             throw new FieldException("FieldException", [ "testimonial" ]);
  2420.         }
  2421.         $courseTestimonial->setUser($this->user);
  2422.         $courseTestimonial->setCourse($course);
  2423.         $errors $this->validateEntity($courseTestimonial);
  2424.         if($errors){
  2425.             throw new FieldException("FieldException"$errors);
  2426.         }
  2427.         $this->em->persist($courseTestimonial);
  2428.         $this->em->flush();
  2429.         $courseTestimonialRepository $this->em->getRepository(CourseTestimonial::class);
  2430.         $courseTestimonialRepository->autoEvaluateTestimonial($courseTestimonial);
  2431.         $userWebhook $this->em->getRepository(User::class)->getToWebhook(
  2432.             $courseTestimonial->getUser()
  2433.         );
  2434.       
  2435.         $dataObj= (object)[
  2436.             "user" => $userWebhook,
  2437.             "course"=> (object)[        
  2438.                 "id" => (string)$courseTestimonial->getCourse()->getId(),
  2439.                 "name" => $courseTestimonial->getCourse()->getTitle(),
  2440.             ],
  2441.             "testimonial"=> (object)[
  2442.                 "stars" => $courseTestimonial->getScore(),
  2443.                 "comment" => $courseTestimonial->getTestimonial(),
  2444.                 "dates" => (object)[
  2445.                     "created" => $courseTestimonial->getDate(),
  2446.                 ],
  2447.             ],
  2448.             "enrollment"=> $enrollment->toWebhook(),
  2449.         ];
  2450.         $webhookService $this->generalService->getService('WebhookService');
  2451.         $webhookService->addItemList(WebhookEnum::TESTIMONIAL$dataObj);
  2452.         $notificationService $this->generalService->getService(
  2453.             'NotificationService'
  2454.         );
  2455.         
  2456.         if($this->user != $course->getUser()){
  2457.             $notificationService->create(
  2458.                 $this->user
  2459.                 $course->getUser(),
  2460.                 NotificationEnum::ORIGIN_COURSE_TESTIMONIAL_NEW,
  2461.                 $courseTestimonial->getId()
  2462.             );
  2463.         }
  2464.         $userCourse $course->getUser();
  2465.         if($userCourse->getAllowNotifyNewSupportMessage() == CourseTestimonialEnum::YES){
  2466.             $emailService $this->generalService->getService('EmailService');
  2467.             if($emailService->checkUserToSend($userCourse)){
  2468.                 $emailService->setToEmail($userCourse->getEmail());
  2469.                 $emailService->setToName($userCourse->getName());
  2470.     
  2471.                 $subText $this->configuration->getLanguage(
  2472.                     'new_course_testimonial.subject''email'
  2473.                 );
  2474.                 
  2475.                 $id $courseTestimonial->getId();
  2476.                 $subject "{$subText} - {$course->getTitle()}";
  2477.                 $emailService->setSubject($subject);
  2478.     
  2479.                 $emailService->setData([
  2480.                     "userName" => $userCourse->getName(),
  2481.                     "btnLink" => "{$this->adminLink}courses/testimonials/{$id}",
  2482.                 ]);
  2483.     
  2484.                 $emailService->setTemplateBody("new_course_testimonial");
  2485.                 $emailService->send();
  2486.             }
  2487.         }
  2488.         $data $courseTestimonial->toReturn();
  2489.         $this->userLogService->logInsert(
  2490.             "course_testimonial"
  2491.             $courseTestimonial->getId(), 
  2492.             $data
  2493.         );
  2494.         $testimonial = (object)[
  2495.             "id" => $courseTestimonial->getId(),
  2496.             "score" => $courseTestimonial->getScore(),
  2497.             "testimonial" => $courseTestimonial->getTestimonial(),
  2498.         ];
  2499.         return $this->eadResponseNew($testimonial);
  2500.     }
  2501.     /**
  2502.      * @Route(
  2503.      *      path          = "/admin/v2/course/{courseId}/testimonial/{id}",
  2504.      *      methods       = {"PUT"}
  2505.      * )
  2506.      */
  2507.     public function editCourseTestimonial(Request $request){
  2508.         if($this->configuration->get("allow_testimonial") == CourseTestimonialEnum::NO){
  2509.             throw new ActionInvalidException('ActionInvalidException');
  2510.         }
  2511.         $courseId $request->get('courseId');
  2512.         $course $this->repository->findOneBy([
  2513.             "id" => $courseId,
  2514.             "deleted" => CourseEnum::ITEM_NO_DELETED
  2515.         ]);
  2516.         
  2517.         if(!$course){
  2518.             throw new NotFoundException(
  2519.                 $this->configuration->getLanguage('error_course_not_found''lesson_view_error')
  2520.             );
  2521.         }
  2522.         $isInTeam $this->em->getRepository(CourseTeam::class)->userExistInCourseTeam(
  2523.             $course
  2524.             $this->user
  2525.         );
  2526.         $enrollmentRepository $this->em->getRepository(Enrollment::class);
  2527.         $enrollment $enrollmentRepository->findOneBy([
  2528.             "user" => $this->user->getId(),
  2529.             "course" => $courseId,
  2530.             "deleted" => EnrollmentEnum::ITEM_NO_DELETED,
  2531.         ], [ "id" => "DESC" ]);
  2532.         $permission $this->userPermissionUtil->getPermission("course""see");
  2533.         
  2534.         if(!$enrollment){
  2535.             if($course->getFree() == CourseEnum::NO){
  2536.                 if($this->userPermissionUtil->isLow($permission)){
  2537.                     throw new PermissionException(
  2538.                         $this->configuration->getLanguage(
  2539.                             'error_user_not_permission'
  2540.                             'lesson_view_error'
  2541.                         )
  2542.                     );
  2543.                 }
  2544.                 if(!$isInTeam && $this->userPermissionUtil->isMiddle($permission)){
  2545.                     throw new PermissionException(
  2546.                         $this->configuration->getLanguage(
  2547.                             'error_user_not_permission'
  2548.                             'lesson_view_error'
  2549.                         )
  2550.                     );
  2551.                 }
  2552.             }
  2553.             $enrollmentService $this->generalService->getService('EnrollmentService');
  2554.             $info $enrollmentService->enrollUser($this->user$course);
  2555.             
  2556.             if(!$info->errors){
  2557.                 $enrollment $info->enrollment;
  2558.             }
  2559.         }
  2560.         if($this->userPermissionUtil->isLow($permission) && !$isInTeam){
  2561.             if(!$enrollment){
  2562.                 throw new ActionInvalidException(
  2563.                     $this->configuration->getLanguage(
  2564.                         'error_enrollment_not_found',
  2565.                         'lesson_view_error'
  2566.                     )
  2567.                 );
  2568.             }
  2569.             if($course->getStatus() == CourseEnum::DRAFT){
  2570.                 throw new ActionInvalidException(
  2571.                     $this->configuration->getLanguage(
  2572.                         'error_course_not_published',
  2573.                         'lesson_view_error'
  2574.                     )
  2575.                 );
  2576.             }
  2577.         }
  2578.         $testimonialId $request->get('id');
  2579.         $courseTestimonialRepository $this->em->getRepository(CourseTestimonial::class);
  2580.         $courseTestimonial $courseTestimonialRepository->findOneBy([
  2581.             "id" => $testimonialId,
  2582.             "deleted" => CourseTestimonialEnum::ITEM_NO_DELETED
  2583.         ]);
  2584.         if (!$courseTestimonial || empty($testimonialId)) {
  2585.             throw new NotFoundException(
  2586.                 $this->configuration->getLanguage('error_testimonial_not_found''lesson_view_error')
  2587.             );
  2588.         }
  2589.         $this->requestUtil->setRequest($request)->setData();
  2590.         if($this->requestUtil->issetField('score')){
  2591.             $courseTestimonial->setScore((int)$this->requestUtil->getField("score"));
  2592.         }
  2593.         if($this->requestUtil->issetField('testimonial')){
  2594.             $courseTestimonial->setTestimonial($this->requestUtil->getField('testimonial'));
  2595.         }
  2596.         if(
  2597.             $this->configuration->get("required_text_testimonial") == CourseEnum::YES &&
  2598.             empty($courseTestimonial->getTestimonial())
  2599.         ){
  2600.             throw new FieldException("FieldException", [ "testimonial" ]);
  2601.         }
  2602.         
  2603.         $courseTestimonial->setDate(date('Y-m-d H:i:s'));
  2604.         $courseTestimonial->setStatus(CourseTestimonialEnum::WAITING);
  2605.         $errors $this->validateEntity($courseTestimonial);
  2606.         
  2607.         if($errors){
  2608.             throw new FieldException("FieldException"$errors);
  2609.         }
  2610.         $courseTestimonialRepository->autoEvaluateTestimonial($courseTestimonial);
  2611.         $this->em->flush();
  2612.         $notificationService $this->generalService->getService('NotificationService');
  2613.         $emailService $this->generalService->getService('EmailService');
  2614.         if($this->user != $course->getUser()){
  2615.             $notificationService->create(
  2616.                 $this->user
  2617.                 $course->getUser(),
  2618.                 NotificationEnum::ORIGIN_COURSE_TESTIMONIAL_CHANGE,
  2619.                 $courseTestimonial->getId()
  2620.             );
  2621.         }
  2622.         $user $course->getUser();
  2623.         if($user->getAllowNotifyNewSupportMessage() == CourseTestimonialEnum::YES){
  2624.             if($emailService->checkUserToSend($user)){
  2625.                 $emailService->setToEmail($user->getEmail());
  2626.                 $emailService->setToName($user->getName());
  2627.                 $subText $this->configuration->getLanguage(
  2628.                     'new_course_testimonial.subject''email'
  2629.                 );
  2630.                 $subject "{$subText} - {$course->getTitle()}";
  2631.                 $emailService->setSubject($subject);
  2632.                 
  2633.                 $emailService->setData([
  2634.                     "userName" => $course->getUser()->getName(),
  2635.                     "btnLink" => "{$this->adminLink}courses/testimonials/{$testimonialId}",
  2636.                 ]);
  2637.                 $emailService->setTemplateBody("new_course_testimonial");
  2638.                 $emailService->send();
  2639.             }
  2640.         }
  2641.         $data $courseTestimonial->toReturn();
  2642.         $this->userLogService->logUpdate(
  2643.             "course_testimonial"
  2644.             $courseTestimonial->getId(),
  2645.             $data
  2646.         );
  2647.         $testimonial = (object)[
  2648.             "id" => $courseTestimonial->getId(),
  2649.             "score" => $courseTestimonial->getScore(),
  2650.             "testimonial" => $courseTestimonial->getTestimonial(),
  2651.         ];
  2652.         return $this->eadResponseNew($testimonial);
  2653.     }
  2654. }