src/Controller/Admin/CourseIndexController.php line 231

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.         $supportId = (int)$this->requestUtil->getField('supportId');
  1976.         $lessonSupportRepository $this->em->getRepository(LessonSupport::class);
  1977.         $rows $lessonSupportRepository->getLessonSupportOrderTypeNew(
  1978.             $lesson->getId(), 
  1979.             $orderType,
  1980.             $searchText,
  1981.             $limit,
  1982.             $offset
  1983.         );
  1984.         $total $lessonSupportRepository->countLessonSupportOrderType(
  1985.             $lessonId
  1986.             $orderType
  1987.             $searchText
  1988.         );
  1989.         $permission $this->userPermissionUtil->getPermission(
  1990.             "course""support""delete"
  1991.         );
  1992.         $isLessonTeacher $lessonRepository->isLessonTeacher($lesson$this->user);
  1993.         $data = [
  1994.             "allowDeleteSupport" => (
  1995.                 $isLessonTeacher || $this->userPermissionUtil->isHigh($permission)
  1996.             ),
  1997.             "rowsTotal" => $total,
  1998.             "rowsTotalDisplay" => count($rows),
  1999.             "searchText" => $searchText,
  2000.             "limit" => (!empty($limit) ? $limit 10),
  2001.             "offset" => $offset,
  2002.             "orderType" => $orderType,
  2003.             "rows" => $rows
  2004.         ];
  2005.         $likeControlRepository $this->em->getRepository(LikeControl::class);
  2006.         $typesText = [
  2007.             LessonSupportEnum::LESSON_STUDENT => $this->configuration->getLanguage(
  2008.                 'student''lesson_view_error'
  2009.             ),
  2010.             LessonSupportEnum::COURSE_TEACHER => $this->configuration->getLanguage(
  2011.                 'teacher''lesson_view_error'
  2012.             ),
  2013.             LessonSupportEnum::MODULE_TEACHER => $this->configuration->getLanguage(
  2014.                 'teacher_module''lesson_view_error'
  2015.             ),
  2016.             LessonSupportEnum::LESSON_TEACHER => $this->configuration->getLanguage(
  2017.                 'coordinator''lesson_view_error'
  2018.             ),
  2019.             LessonSupportEnum::LESSON_TUTOR => $this->configuration->getLanguage(
  2020.                 'tutor''lesson_view_error'
  2021.             ),
  2022.         ];
  2023.         $firstRow null;
  2024.         foreach ($data['rows'] as $key => $item) {
  2025.             $item = (object)$item;
  2026.             $dataImg = [
  2027.                 "fileName" => $item->photo,
  2028.                 "pathConst" => LessonSupportEnum::PATH_PROFILES,
  2029.                 "option" => "l-user-profile-support",
  2030.                 "addUpload" => true,
  2031.                 "addStream" => true,
  2032.             ];
  2033.             $item->photo $this->fileService->getFilePathObj($dataImg);
  2034.             $item->support html_entity_decode($item->support);
  2035.             $item->answers $lessonSupportRepository->getLessonSupportAnswers($item->id);
  2036.             $item->lessonUserType = (int)$item->lessonUserType;
  2037.             $item->lessonUserTypeText $typesText[$item->lessonUserType];
  2038.             
  2039.             $likeControl $likeControlRepository->findOneByEAD([
  2040.                 "userFrom" => $this->user->getId(),
  2041.                 "element" => $item->id,
  2042.                 "type" => LikeControlEnum::LESSON_SUPPORT,
  2043.             ]);
  2044.             $item->allowLike true;
  2045.             if($likeControl){
  2046.                 $item->allowLike false;
  2047.             }
  2048.             if($supportId == $item->id){
  2049.                 $firstRow $item;
  2050.             }else{
  2051.                 $data['rows'][$key] = $item;
  2052.             }
  2053.         }
  2054.         if($firstRow){
  2055.             array_unshift($data['rows'], $firstRow);
  2056.         }
  2057.         return $this->eadResponseNew($data);
  2058.     }
  2059.     /**
  2060.      * @Route(
  2061.      *      path          = "/admin/v2/lesson/{id}/supports/{lessonSupportId}/pin",
  2062.      *      methods       = {"PUT"}
  2063.      * )
  2064.      */
  2065.     public function pinLessonSupport(Request $request) {
  2066.         $permission $this->userPermissionUtil->getPermission("course""support""finalize");
  2067.         if($this->userPermissionUtil->isLow($permission)){
  2068.             throw new PermissionException(
  2069.                 $this->configuration->getLanguage('error_user_not_permission''lesson_view_error')
  2070.             );
  2071.         }
  2072.         
  2073.         $this->requestUtil->setRequest($request)->setData();
  2074.         $lessonSupportId = (int)$request->get('lessonSupportId');
  2075.         $lessonSupportRepository $this->em->getRepository(LessonSupport::class);
  2076.         $lessonSupport $lessonSupportRepository->findOneBy([
  2077.             "id" => $lessonSupportId,
  2078.             "deleted" => LessonSupportEnum::ITEM_NO_DELETED
  2079.         ]);
  2080.         
  2081.         if (!$lessonSupport) {
  2082.             throw new NotFoundException(
  2083.                 $this->configuration->getLanguage('error_support_not_found''lesson_view_error')
  2084.             );
  2085.         }
  2086.         $lesson $lessonSupport->getLesson();
  2087.         $isLessonTeacher $this->em->getRepository(Lesson::class)->isLessonTeacher(
  2088.             $lesson
  2089.             $this->user
  2090.         );
  2091.         
  2092.         if(!$isLessonTeacher && $this->userPermissionUtil->isMiddle($permission)){
  2093.             throw new PermissionException(
  2094.                 $this->configuration->getLanguage('error_user_not_permission''lesson_view_error')
  2095.             );
  2096.         }
  2097.         $lessonSupport->setLessonFixed(LessonSupportEnum::YES);
  2098.         $this->em->flush();
  2099.         $data $lessonSupport->toReturn();
  2100.         return $this->eadResponseNew([ "message" => "Success" ]);
  2101.     }
  2102.     /**
  2103.      * @Route(
  2104.      *      path          = "/admin/v2/lesson/{id}/supports/{lessonSupportId}/unpin",
  2105.      *      methods       = {"DELETE"}
  2106.      * )
  2107.      */
  2108.     public function unpinLessonSupport(Request $request) {
  2109.         $permission $this->userPermissionUtil->getPermission("course""support""finalize");
  2110.         if($this->userPermissionUtil->isLow($permission)){
  2111.             throw new PermissionException(
  2112.                 $this->configuration->getLanguage('error_user_not_permission''lesson_view_error')
  2113.             );
  2114.         }
  2115.         
  2116.         $this->requestUtil->setRequest($request)->setData();
  2117.         $lessonSupportId = (int)$request->get('lessonSupportId');
  2118.         $lessonSupportRepository $this->em->getRepository(LessonSupport::class);
  2119.         $lessonSupport $lessonSupportRepository->findOneBy([
  2120.             "id" => $lessonSupportId,
  2121.             "deleted" => LessonSupportEnum::ITEM_NO_DELETED
  2122.         ]);
  2123.         
  2124.         if (!$lessonSupport) {
  2125.             throw new NotFoundException(
  2126.                 $this->configuration->getLanguage('error_support_not_found''lesson_view_error')
  2127.             );
  2128.         }
  2129.         $lesson $lessonSupport->getLesson();
  2130.         $isLessonTeacher $this->em->getRepository(Lesson::class)->isLessonTeacher(
  2131.             $lesson
  2132.             $this->user
  2133.         );
  2134.         
  2135.         if(!$isLessonTeacher && $this->userPermissionUtil->isMiddle($permission)){
  2136.             throw new PermissionException(
  2137.                 $this->configuration->getLanguage('error_user_not_permission''lesson_view_error')
  2138.             );
  2139.         }
  2140.         $lessonSupport->setLessonFixed(LessonSupportEnum::NO);
  2141.         $this->em->flush();
  2142.         $data $lessonSupport->toReturn();
  2143.         return $this->eadResponseNew([ "message" => "Success" ]);
  2144.     }
  2145.     /**
  2146.      * @Route(
  2147.      *      path          = "/admin/v2/lesson/{id}/supports/{lessonSupportId}",
  2148.      *      methods       = {"PATCH"}
  2149.      * )
  2150.      */
  2151.     public function likeLessonSupport(Request $request) {
  2152.         $this->requestUtil->setRequest($request)->setData();
  2153.         $lessonSupportId = (int)$request->get('lessonSupportId');
  2154.         $lessonSupportRepository $this->em->getRepository(LessonSupport::class);
  2155.         $lessonSupport $lessonSupportRepository->findOneBy([
  2156.             "id" => $lessonSupportId,
  2157.             "deleted" => LessonSupportEnum::ITEM_NO_DELETED
  2158.         ]);
  2159.         
  2160.         if (!$lessonSupport) {
  2161.             throw new NotFoundException(
  2162.                 $this->configuration->getLanguage('error_support_not_found''lesson_view_error')
  2163.             );
  2164.         }
  2165.         $lesson $lessonSupport->getLesson();
  2166.         
  2167.         $likeControl = new LikeControl();
  2168.         $type LikeControlEnum::LESSON_SUPPORT;
  2169.         if($lessonSupport->getLessonSupport()){
  2170.             $type LikeControlEnum::LESSON_SUPPORT_ANSWER;
  2171.         }
  2172.         $likeControl->setElement($lessonSupportId);
  2173.         $likeControl->setType($type);
  2174.         $likeControl->setUserTo($lessonSupport->getUser());
  2175.         $likeControl->setUserFrom($this->user);
  2176.         
  2177.         $errors $this->validateEntity($likeControl);
  2178.         if($errors){
  2179.             throw new FieldException("FieldException"$errors);
  2180.         }
  2181.         $lessonSupport->setLikeNumber($lessonSupport->getLikeNumber() + 1);
  2182.         
  2183.         $this->em->persist($likeControl);
  2184.         $this->em->flush();
  2185.         return $this->eadResponseNew([ "message" => "Success" ]);
  2186.     }
  2187.     /**
  2188.      * @Route(
  2189.      *      path          = "/admin/v2/lesson/{id}/supports",
  2190.      *      methods       = {"POST"},
  2191.      * )
  2192.      */
  2193.     public function registerLessonSupport(Request $request) {
  2194.         
  2195.         $this->requestUtil->setRequest($request)->setData();
  2196.         $lessonRepository $this->em->getRepository(Lesson::class);
  2197.         $lessonSupportRepository $this->em->getRepository(LessonSupport::class);
  2198.         $enrollmentRepository $this->em->getRepository(Enrollment::class);
  2199.         $lessonId $request->get('id');
  2200.         $lesson $lessonRepository->findOneBy([
  2201.             "id" => $lessonId,
  2202.             "deleted" => LessonEnum::ITEM_NO_DELETED
  2203.         ]);
  2204.         if(!$lesson){
  2205.             throw new NotFoundException(
  2206.                 $this->configuration->getLanguage('error_lesson_not_found''lesson_view_error')
  2207.             );
  2208.         }
  2209.         //check user can access lesson
  2210.         $isStudent $this->repository->isStudent($lesson->getCourse());
  2211.         if(!$lessonRepository->isLessonVisibleToStudent($lessonfalse$isStudent)){
  2212.             throw new ActionInvalidException(
  2213.                 $this->configuration->getLanguage(
  2214.                     'error_lesson_unavailable''lesson_view_error'
  2215.                 )
  2216.             );
  2217.         }
  2218.         $lessonSupport = new LessonSupport();
  2219.         
  2220.         $lessonSupport->setUser($this->user);
  2221.         $lessonSupport->setLesson($lesson);
  2222.         //set LessonSupport in LessonSupport
  2223.         if($this->requestUtil->issetField('lessonSupport')){
  2224.             $lessonSupportId = (int)$this->requestUtil->getField('lessonSupport');
  2225.             $lessonSupportParent $lessonSupportRepository->findOneBy([
  2226.                 "id" => $lessonSupportId,
  2227.                 "deleted" => LessonSupportEnum::ITEM_NO_DELETED
  2228.             ]);
  2229.             $lessonSupport->setLessonSupport($lessonSupportParent);
  2230.         }
  2231.         
  2232.         if($this->requestUtil->issetField('support')){
  2233.             $lessonSupport->setSupport($this->requestUtil->getField('support'));
  2234.         }
  2235.         $errors $this->validateEntity($lessonSupport);
  2236.         if($errors){
  2237.             throw new FieldException("FieldException"$errors);
  2238.         }
  2239.         
  2240.         $lessonSupportParent $lessonSupport->getLessonSupport();
  2241.         $lesson $lessonSupport->getLesson();
  2242.         $course $lesson->getCourse();
  2243.         
  2244.         $isLessonTeacher $lessonRepository->isLessonTeacher(
  2245.             $lesson
  2246.             $this->user
  2247.         );
  2248.         $isEnrolled $enrollmentRepository->isValidEnrollmentByUser(
  2249.             $this->user->getId(), 
  2250.             $course->getId()
  2251.         );
  2252.         $permission $this->userPermissionUtil->getPermission("course""support""answer");
  2253.         if($lessonSupportParent){
  2254.             if(!$isEnrolled){
  2255.                 if($this->userPermissionUtil->isLow($permission)){
  2256.                     throw new PermissionException($this->configuration->getLanguage('error_user_not_permission''lesson_view_error'));
  2257.                 }
  2258.                 if(!$isLessonTeacher && $this->userPermissionUtil->isMiddle($permission)){
  2259.                     throw new PermissionException($this->configuration->getLanguage('error_user_not_permission''lesson_view_error'));
  2260.                 }
  2261.             }
  2262.         }else{
  2263.             if($course->getSupport() == LessonSupportEnum::NO){
  2264.                 throw new ActionInvalidException($this->configuration->getLanguage('error_course_not_support''lesson_view_error'));
  2265.             }
  2266.             if(!$isEnrolled && !$isLessonTeacher){
  2267.                 throw new ActionInvalidException(
  2268.                     $this->configuration->getLanguage('error_unavailable''lesson_view_error')
  2269.                 );
  2270.             }
  2271.         }
  2272.         $this->em->persist($lessonSupport);
  2273.         $this->em->flush();
  2274.         $lessonSupportRepository->processNewLessonSupport($lessonSupport);
  2275.          
  2276.         return $this->eadResponseNew([ "message" => "Success" ]);
  2277.     }
  2278.     /**
  2279.      * @Route(
  2280.      *      path          = "/admin/v2/lesson/{id}/supports/{lessonSupportId}",
  2281.      *      methods       = {"PUT"}
  2282.      * )
  2283.      */
  2284.     public function editLessonSupport(Request $request) {
  2285.         $this->requestUtil->setRequest($request)->setData();
  2286.         $lessonSupportId = (int)$request->get('lessonSupportId');
  2287.         $lessonSupportRepository $this->em->getRepository(LessonSupport::class);
  2288.         $lessonSupport $lessonSupportRepository->findOneBy([
  2289.             "id" => $lessonSupportId,
  2290.             "deleted" => LessonSupportEnum::ITEM_NO_DELETED
  2291.         ]);
  2292.         
  2293.         if (!$lessonSupport) {
  2294.             throw new NotFoundException(
  2295.                 $this->configuration->getLanguage('error_support_not_found''lesson_view_error')
  2296.             );
  2297.         }
  2298.         $lesson $lessonSupport->getLesson();
  2299.         
  2300.         if($this->requestUtil->issetField('support')){
  2301.             $lessonSupport->setSupport($this->requestUtil->getField('support'));
  2302.         }
  2303.         $lessonSupport->setDateUpdate(date('Y-m-d H:i:s'));
  2304.         
  2305.         $errors $this->validateEntity($lessonSupport);
  2306.         if($errors){
  2307.             throw new FieldException("FieldException"$errors);
  2308.         }
  2309.         $this->em->flush();
  2310.         return $this->eadResponseNew([ "message" => "Success" ]);
  2311.     }
  2312.     /**
  2313.      * @Route(
  2314.      *      path          = "/admin/v2/lesson/{id}/supports/{lessonSupportId}",
  2315.      *      methods       = {"DELETE"}
  2316.      * )
  2317.      */
  2318.     public function deleteLessonSupport(Request $request) {
  2319.         $this->requestUtil->setRequest($request)->setData();
  2320.         $lessonSupportId = (int)$request->get('lessonSupportId');
  2321.         $lessonSupportRepository $this->em->getRepository(LessonSupport::class);
  2322.         $lessonSupport $lessonSupportRepository->findOneBy([
  2323.             "id" => $lessonSupportId,
  2324.             "deleted" => LessonSupportEnum::ITEM_NO_DELETED
  2325.         ]);
  2326.         
  2327.         if (!$lessonSupport) {
  2328.             throw new NotFoundException(
  2329.                 $this->configuration->getLanguage('error_support_not_found''lesson_view_error')
  2330.             );
  2331.         }
  2332.         
  2333.         $lessonSupportRepository->delete(
  2334.             $lessonSupport
  2335.             TrashEnum::LESSON_SUPPORT
  2336.             $this->userPermissionUtil->getPermission("course""support""delete"), 
  2337.             TrashEnum::NO
  2338.         );
  2339.         $this->em->flush();
  2340.         return $this->eadResponseNew([ "message" => "Success" ]);
  2341.     }
  2342.     /**
  2343.      * @Route(
  2344.      *      path          = "/admin/v2/course/{id}/testimonial",
  2345.      *      methods       = {"POST"},
  2346.      * )
  2347.      */
  2348.     public function registerCourseTestimonial(Request $request){
  2349.         if($this->configuration->get("allow_testimonial") == CourseTestimonialEnum::NO){
  2350.             throw new ActionInvalidException('ActionInvalidException');
  2351.         }
  2352.         $this->requestUtil->setRequest($request)->setData();
  2353.         $courseId $request->get('id');
  2354.         $course $this->repository->findOneBy([
  2355.             "id" => $courseId,
  2356.             "deleted" => CourseEnum::ITEM_NO_DELETED
  2357.         ]);
  2358.         
  2359.         if(!$course){
  2360.             throw new NotFoundException(
  2361.                 $this->configuration->getLanguage('error_course_not_found''lesson_view_error')
  2362.             );
  2363.         }
  2364.         $isInTeam $this->em->getRepository(CourseTeam::class)->userExistInCourseTeam(
  2365.             $course
  2366.             $this->user
  2367.         );
  2368.         $enrollmentRepository $this->em->getRepository(Enrollment::class);
  2369.         $enrollment $enrollmentRepository->findOneBy([
  2370.             "user" => $this->user->getId(),
  2371.             "course" => $courseId,
  2372.             "deleted" => EnrollmentEnum::ITEM_NO_DELETED,
  2373.         ], [ "id" => "DESC" ]);
  2374.         $permission $this->userPermissionUtil->getPermission("course""see");
  2375.         
  2376.         if(!$enrollment){
  2377.             if($course->getFree() == CourseEnum::NO){
  2378.                 if($this->userPermissionUtil->isLow($permission)){
  2379.                     throw new PermissionException($this->configuration->getLanguage('error_user_not_permission''lesson_view_error'));
  2380.                 }
  2381.                 if(!$isInTeam && $this->userPermissionUtil->isMiddle($permission)){
  2382.                     throw new PermissionException(
  2383.                         $this->configuration->getLanguage(
  2384.                             'error_user_not_permission'
  2385.                             'lesson_view_error'
  2386.                         )
  2387.                     );
  2388.                 }
  2389.             }
  2390.             $enrollmentService $this->generalService->getService('EnrollmentService');
  2391.             $info $enrollmentService->enrollUser($this->user$course);
  2392.             
  2393.             if(!$info->errors){
  2394.                 $enrollment $info->enrollment;
  2395.             }
  2396.         }
  2397.         if($this->userPermissionUtil->isLow($permission) && !$isInTeam){
  2398.             if(!$enrollment){
  2399.                 throw new ActionInvalidException(
  2400.                     $this->configuration->getLanguage(
  2401.                         'error_enrollment_not_found'
  2402.                         'lesson_view_error'
  2403.                     )
  2404.                 );
  2405.             }
  2406.             if($course->getStatus() == CourseEnum::DRAFT){
  2407.                 throw new ActionInvalidException(
  2408.                     $this->configuration->getLanguage(
  2409.                         'error_course_not_published'
  2410.                         'lesson_view_error'
  2411.                     )
  2412.                 );
  2413.             }
  2414.         }
  2415.         $courseTestimonial = new CourseTestimonial();
  2416.         $courseTestimonial->setDate(date('Y-m-d H:i:s'));
  2417.         $courseTestimonial->setStatus(CourseTestimonialEnum::WAITING);
  2418.         if($this->requestUtil->issetField('score')){
  2419.             $courseTestimonial->setScore((int)$this->requestUtil->getField("score"));
  2420.         }
  2421.         if($this->requestUtil->issetField('testimonial')){
  2422.             $courseTestimonial->setTestimonial($this->requestUtil->getField('testimonial'));
  2423.         }
  2424.         if(
  2425.             $this->configuration->get("required_text_testimonial") == CourseEnum::YES &&
  2426.             empty($courseTestimonial->getTestimonial())
  2427.         ){
  2428.             throw new FieldException("FieldException", [ "testimonial" ]);
  2429.         }
  2430.         $courseTestimonial->setUser($this->user);
  2431.         $courseTestimonial->setCourse($course);
  2432.         $errors $this->validateEntity($courseTestimonial);
  2433.         if($errors){
  2434.             throw new FieldException("FieldException"$errors);
  2435.         }
  2436.         $this->em->persist($courseTestimonial);
  2437.         $this->em->flush();
  2438.         $courseTestimonialRepository $this->em->getRepository(CourseTestimonial::class);
  2439.         $courseTestimonialRepository->autoEvaluateTestimonial($courseTestimonial);
  2440.         $userWebhook $this->em->getRepository(User::class)->getToWebhook(
  2441.             $courseTestimonial->getUser()
  2442.         );
  2443.       
  2444.         $dataObj= (object)[
  2445.             "user" => $userWebhook,
  2446.             "course"=> (object)[        
  2447.                 "id" => (string)$courseTestimonial->getCourse()->getId(),
  2448.                 "name" => $courseTestimonial->getCourse()->getTitle(),
  2449.             ],
  2450.             "testimonial"=> (object)[
  2451.                 "stars" => $courseTestimonial->getScore(),
  2452.                 "comment" => $courseTestimonial->getTestimonial(),
  2453.                 "dates" => (object)[
  2454.                     "created" => $courseTestimonial->getDate(),
  2455.                 ],
  2456.             ],
  2457.             "enrollment"=> $enrollment->toWebhook(),
  2458.         ];
  2459.         $webhookService $this->generalService->getService('WebhookService');
  2460.         $webhookService->addItemList(WebhookEnum::TESTIMONIAL$dataObj);
  2461.         $notificationService $this->generalService->getService(
  2462.             'NotificationService'
  2463.         );
  2464.         
  2465.         if($this->user != $course->getUser()){
  2466.             $notificationService->create(
  2467.                 $this->user
  2468.                 $course->getUser(),
  2469.                 NotificationEnum::ORIGIN_COURSE_TESTIMONIAL_NEW,
  2470.                 $courseTestimonial->getId()
  2471.             );
  2472.         }
  2473.         $userCourse $course->getUser();
  2474.         if($userCourse->getAllowNotifyNewSupportMessage() == CourseTestimonialEnum::YES){
  2475.             $emailService $this->generalService->getService('EmailService');
  2476.             if($emailService->checkUserToSend($userCourse)){
  2477.                 $emailService->setToEmail($userCourse->getEmail());
  2478.                 $emailService->setToName($userCourse->getName());
  2479.     
  2480.                 $subText $this->configuration->getLanguage(
  2481.                     'new_course_testimonial.subject''email'
  2482.                 );
  2483.                 
  2484.                 $id $courseTestimonial->getId();
  2485.                 $subject "{$subText} - {$course->getTitle()}";
  2486.                 $emailService->setSubject($subject);
  2487.     
  2488.                 $emailService->setData([
  2489.                     "userName" => $userCourse->getName(),
  2490.                     "btnLink" => "{$this->adminLink}courses/testimonials/{$id}",
  2491.                 ]);
  2492.     
  2493.                 $emailService->setTemplateBody("new_course_testimonial");
  2494.                 $emailService->send();
  2495.             }
  2496.         }
  2497.         $data $courseTestimonial->toReturn();
  2498.         $this->userLogService->logInsert(
  2499.             "course_testimonial"
  2500.             $courseTestimonial->getId(), 
  2501.             $data
  2502.         );
  2503.         $testimonial = (object)[
  2504.             "id" => $courseTestimonial->getId(),
  2505.             "score" => $courseTestimonial->getScore(),
  2506.             "testimonial" => $courseTestimonial->getTestimonial(),
  2507.         ];
  2508.         return $this->eadResponseNew($testimonial);
  2509.     }
  2510.     /**
  2511.      * @Route(
  2512.      *      path          = "/admin/v2/course/{courseId}/testimonial/{id}",
  2513.      *      methods       = {"PUT"}
  2514.      * )
  2515.      */
  2516.     public function editCourseTestimonial(Request $request){
  2517.         if($this->configuration->get("allow_testimonial") == CourseTestimonialEnum::NO){
  2518.             throw new ActionInvalidException('ActionInvalidException');
  2519.         }
  2520.         $courseId $request->get('courseId');
  2521.         $course $this->repository->findOneBy([
  2522.             "id" => $courseId,
  2523.             "deleted" => CourseEnum::ITEM_NO_DELETED
  2524.         ]);
  2525.         
  2526.         if(!$course){
  2527.             throw new NotFoundException(
  2528.                 $this->configuration->getLanguage('error_course_not_found''lesson_view_error')
  2529.             );
  2530.         }
  2531.         $isInTeam $this->em->getRepository(CourseTeam::class)->userExistInCourseTeam(
  2532.             $course
  2533.             $this->user
  2534.         );
  2535.         $enrollmentRepository $this->em->getRepository(Enrollment::class);
  2536.         $enrollment $enrollmentRepository->findOneBy([
  2537.             "user" => $this->user->getId(),
  2538.             "course" => $courseId,
  2539.             "deleted" => EnrollmentEnum::ITEM_NO_DELETED,
  2540.         ], [ "id" => "DESC" ]);
  2541.         $permission $this->userPermissionUtil->getPermission("course""see");
  2542.         
  2543.         if(!$enrollment){
  2544.             if($course->getFree() == CourseEnum::NO){
  2545.                 if($this->userPermissionUtil->isLow($permission)){
  2546.                     throw new PermissionException(
  2547.                         $this->configuration->getLanguage(
  2548.                             'error_user_not_permission'
  2549.                             'lesson_view_error'
  2550.                         )
  2551.                     );
  2552.                 }
  2553.                 if(!$isInTeam && $this->userPermissionUtil->isMiddle($permission)){
  2554.                     throw new PermissionException(
  2555.                         $this->configuration->getLanguage(
  2556.                             'error_user_not_permission'
  2557.                             'lesson_view_error'
  2558.                         )
  2559.                     );
  2560.                 }
  2561.             }
  2562.             $enrollmentService $this->generalService->getService('EnrollmentService');
  2563.             $info $enrollmentService->enrollUser($this->user$course);
  2564.             
  2565.             if(!$info->errors){
  2566.                 $enrollment $info->enrollment;
  2567.             }
  2568.         }
  2569.         if($this->userPermissionUtil->isLow($permission) && !$isInTeam){
  2570.             if(!$enrollment){
  2571.                 throw new ActionInvalidException(
  2572.                     $this->configuration->getLanguage(
  2573.                         'error_enrollment_not_found',
  2574.                         'lesson_view_error'
  2575.                     )
  2576.                 );
  2577.             }
  2578.             if($course->getStatus() == CourseEnum::DRAFT){
  2579.                 throw new ActionInvalidException(
  2580.                     $this->configuration->getLanguage(
  2581.                         'error_course_not_published',
  2582.                         'lesson_view_error'
  2583.                     )
  2584.                 );
  2585.             }
  2586.         }
  2587.         $testimonialId $request->get('id');
  2588.         $courseTestimonialRepository $this->em->getRepository(CourseTestimonial::class);
  2589.         $courseTestimonial $courseTestimonialRepository->findOneBy([
  2590.             "id" => $testimonialId,
  2591.             "deleted" => CourseTestimonialEnum::ITEM_NO_DELETED
  2592.         ]);
  2593.         if (!$courseTestimonial || empty($testimonialId)) {
  2594.             throw new NotFoundException(
  2595.                 $this->configuration->getLanguage('error_testimonial_not_found''lesson_view_error')
  2596.             );
  2597.         }
  2598.         $this->requestUtil->setRequest($request)->setData();
  2599.         if($this->requestUtil->issetField('score')){
  2600.             $courseTestimonial->setScore((int)$this->requestUtil->getField("score"));
  2601.         }
  2602.         if($this->requestUtil->issetField('testimonial')){
  2603.             $courseTestimonial->setTestimonial($this->requestUtil->getField('testimonial'));
  2604.         }
  2605.         if(
  2606.             $this->configuration->get("required_text_testimonial") == CourseEnum::YES &&
  2607.             empty($courseTestimonial->getTestimonial())
  2608.         ){
  2609.             throw new FieldException("FieldException", [ "testimonial" ]);
  2610.         }
  2611.         
  2612.         $courseTestimonial->setDate(date('Y-m-d H:i:s'));
  2613.         $courseTestimonial->setStatus(CourseTestimonialEnum::WAITING);
  2614.         $errors $this->validateEntity($courseTestimonial);
  2615.         
  2616.         if($errors){
  2617.             throw new FieldException("FieldException"$errors);
  2618.         }
  2619.         $courseTestimonialRepository->autoEvaluateTestimonial($courseTestimonial);
  2620.         $this->em->flush();
  2621.         $notificationService $this->generalService->getService('NotificationService');
  2622.         $emailService $this->generalService->getService('EmailService');
  2623.         if($this->user != $course->getUser()){
  2624.             $notificationService->create(
  2625.                 $this->user
  2626.                 $course->getUser(),
  2627.                 NotificationEnum::ORIGIN_COURSE_TESTIMONIAL_CHANGE,
  2628.                 $courseTestimonial->getId()
  2629.             );
  2630.         }
  2631.         $user $course->getUser();
  2632.         if($user->getAllowNotifyNewSupportMessage() == CourseTestimonialEnum::YES){
  2633.             if($emailService->checkUserToSend($user)){
  2634.                 $emailService->setToEmail($user->getEmail());
  2635.                 $emailService->setToName($user->getName());
  2636.                 $subText $this->configuration->getLanguage(
  2637.                     'new_course_testimonial.subject''email'
  2638.                 );
  2639.                 $subject "{$subText} - {$course->getTitle()}";
  2640.                 $emailService->setSubject($subject);
  2641.                 
  2642.                 $emailService->setData([
  2643.                     "userName" => $course->getUser()->getName(),
  2644.                     "btnLink" => "{$this->adminLink}courses/testimonials/{$testimonialId}",
  2645.                 ]);
  2646.                 $emailService->setTemplateBody("new_course_testimonial");
  2647.                 $emailService->send();
  2648.             }
  2649.         }
  2650.         $data $courseTestimonial->toReturn();
  2651.         $this->userLogService->logUpdate(
  2652.             "course_testimonial"
  2653.             $courseTestimonial->getId(),
  2654.             $data
  2655.         );
  2656.         $testimonial = (object)[
  2657.             "id" => $courseTestimonial->getId(),
  2658.             "score" => $courseTestimonial->getScore(),
  2659.             "testimonial" => $courseTestimonial->getTestimonial(),
  2660.         ];
  2661.         return $this->eadResponseNew($testimonial);
  2662.     }
  2663. }