src/Controller/Admin/CourseIndexController.php line 240

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