src/Repository/LessonRepository.php line 584

Open in your IDE?
  1. <?php
  2. namespace EADPlataforma\Repository;
  3. use EADPlataforma\Entity\Course;
  4. use EADPlataforma\Entity\Product;
  5. use EADPlataforma\Entity\LessonModule;
  6. use EADPlataforma\Entity\Lesson;
  7. use EADPlataforma\Entity\LessonLog;
  8. use EADPlataforma\Entity\LessonLogOrigin;
  9. use EADPlataforma\Entity\CourseTeam;
  10. use EADPlataforma\Entity\Exam;
  11. use EADPlataforma\Entity\ExamUser;
  12. use EADPlataforma\Entity\Enrollment;
  13. use EADPlataforma\Entity\Library;
  14. use EADPlataforma\Entity\LessonXLibrary;
  15. use EADPlataforma\Entity\LessonSupport;
  16. use EADPlataforma\Entity\User;
  17. use EADPlataforma\Entity\Trash;
  18. use EADPlataforma\Enum\CourseEnum;
  19. use EADPlataforma\Enum\CourseTeamEnum;
  20. use EADPlataforma\Enum\LessonEnum;
  21. use EADPlataforma\Enum\LessonModuleEnum;
  22. use EADPlataforma\Enum\EnrollmentEnum;
  23. use EADPlataforma\Enum\ExamEnum;
  24. use EADPlataforma\Enum\UserEnum;
  25. use EADPlataforma\Enum\LibraryEnum;
  26. use EADPlataforma\Enum\LessonXLibraryEnum;
  27. use EADPlataforma\Enum\ServicesEnum;
  28. use EADPlataforma\Enum\NotificationEnum;
  29. use EADPlataforma\Enum\TrashEnum;
  30. use Firebase\JWT\JWT;
  31. use \PDO;
  32. /**
  33.  * @method Lesson|null find($id, $lockMode = null, $lockVersion = null)
  34.  * @method Lesson|null findOneBy(array $criteria, array $orderBy = null)
  35.  * @method Lesson[]    findAll()
  36.  * @method Lesson[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  37.  */
  38. class LessonRepository extends AbstractRepository
  39. {
  40.     private $lessonIdsCourse = [];
  41.     public function getEntityClass(){
  42.         return Lesson::class;
  43.     }
  44.     public function getConnectionName(){
  45.         return "school";
  46.     }
  47.     public function setLessonTag(
  48.         Lesson $lesson
  49.         User $user,
  50.         ?string $content null,
  51.         ?string $link null
  52.     ): object
  53.     {
  54.         $stringUtil $this->generalService->getUtil('StringUtil');
  55.         $cityName = ( $user->getCity() ? $user->getCity()->getName() : '' );
  56.         $stateUf = ( $user->getCity() ? $user->getCity()->getState()->getName() : '' );
  57.         $stateUf = ( $stateUf $stringUtil->getFirstWordAll($stateUf) : '' );
  58.         $client $this->configuration->getClient();
  59.         $schoolName $client->getBrand();
  60.         $schoolDocument $client->getDocument();
  61.         $schoolDomain $this->configuration->getActiveDomain();
  62.         $course $lesson->getCourse();
  63.         $module $lesson->getLessonModule();
  64.         $dataReplace = [
  65.             "[[aluno_id]]" => $user->getId(),
  66.             "[[aluno_nome]]" => $user->getName(),
  67.             "[[aluno_email]]" => $user->getEmail(),
  68.             "[[aluno_cpf]]" => $user->getDocument(),
  69.             "[[aluno_telefone]]" => $user->getPhone(),
  70.             "[[aluno_data_nascimento]]" => $user->getBirthdate('d/m/Y'),
  71.             "[[aluno_cidade]]" => $cityName,
  72.             "[[aluno_uf]]" => $stateUf,
  73.             
  74.             "[[curso_titulo]]" => $course->getTitle(),
  75.             "[[curso_professor]]" => mb_strtoupper($course->getUser()->getName()),
  76.             
  77.             "[[modulo_titulo]]" => $module->getTitle(),
  78.             "[[modulo_descricao]]" => $module->getDescription(),
  79.             "[[modulo_professor]]" => mb_strtoupper($module->getUser()->getName()),
  80.             
  81.             "[[aula_titulo]]" => $lesson->getTitle(),
  82.             "[[aula_descricao]]" => $lesson->getDescription(),
  83.             "[[aula_professor]]" => mb_strtoupper($lesson->getUser()->getName()),
  84.             "[[ead_nome]]" => $schoolName,
  85.             "[[ead_cnpj]]" => $schoolDocument,
  86.             "[[ead_dominio]]" => $schoolDomain,
  87.         ];
  88.         foreach($dataReplace as $key => $value){
  89.             if(!empty($link)){
  90.                 $link str_replace($key$value$link);
  91.             }
  92.             if(!empty($content)){
  93.                 $content str_replace($key$value$content);
  94.             }
  95.         }
  96.         return (object)[
  97.             "content" => $content,
  98.             "link" => $link,
  99.         ];
  100.     }
  101.     public function getMeetCredentials(lesson $lessonUser $user)
  102.     {
  103.         $library $lesson->getLibrary();
  104.         if(!$library){
  105.             return null;
  106.         }
  107.         $libraryType $library->getType();
  108.         if($libraryType != LibraryEnum::CONTENT_CONFERENCE){
  109.             return null;
  110.         }
  111.         $info $this->generalService->getServiceAccess(ServicesEnum::MEET);
  112.         $privateKey $info->privateKey;
  113.         $publicKey $info->publicKey;
  114.         $appId $info->appId;
  115.         $fileService $this->generalService->getService('FileService');
  116.         $photo $fileService->getFilePathComplete(
  117.             $user->getPhoto(), 
  118.             UserEnum::PATH_PROFILES
  119.             true
  120.             true
  121.         );
  122.         $courseRepository $this->em->getRepository(Course::class);
  123.         $isStudent $courseRepository->isStudent($lesson->getCourse());
  124.         $isAdmin = ($isStudent false true );
  125.         $client $this->getConfiguration()->getClient();
  126.         $clientId $client->getClientId();
  127.         $formatClientId = [
  128.             "1" => "00000{$clientId}",
  129.             "2" => "0000{$clientId}",
  130.             "3" => "000{$clientId}",
  131.             "4" => "00{$clientId}",
  132.             "5" => "0{$clientId}",
  133.         ];
  134.         
  135.         if(isset($formatClientId[strlen($clientId)])){
  136.             $clientId $formatClientId[strlen($clientId)];
  137.         }
  138.         
  139.         $channelId "{$clientId}-{$lesson->getId()}";
  140.         $userId "{$clientId}-{$user->getId()}";
  141.         $payload = [
  142.             'iss' => 'chat',
  143.             'aud' => 'jitsi',
  144.             'exp' => time() + 7200
  145.             'nbf' => time() - 10,
  146.             'room'=> '*',
  147.             'sub' => $appId,
  148.             'context' => [
  149.                 'user' => [
  150.                     'moderator' => $isAdmin "true" "false",
  151.                     'email' => $user->getEmail(),
  152.                     'name' => $user->getName(),
  153.                     'avatar' => $photo,
  154.                     'id' => $userId,
  155.                     "custom" => [
  156.                         "clientId" => $clientId,
  157.                     ],
  158.                 ],
  159.                 'features' => [
  160.                     'recording' => $isAdmin "true" "false",
  161.                     'livestreaming' => "false",
  162.                     'transcription' => "false",
  163.                     'outbound-call' => "false"
  164.                 ]
  165.             ]
  166.         ];
  167.         $token JWT::encode($payload$privateKey"RS256"$publicKey);
  168.         return (object)[
  169.             "token" => $token,
  170.             "room" => $channelId,
  171.             "appId" => $appId,
  172.         ];
  173.     }
  174.     public function getLessonChatCredentialsNew(Lesson $lesson)
  175.     {
  176.         $library $lesson->getLibrary();
  177.         if(!$this->getUser()){
  178.             return;
  179.         }
  180.         if(!$library){
  181.             return;
  182.         }
  183.         if(
  184.             $library->getType() != LibraryEnum::CONTENT_LIVE && 
  185.             $library->getType() != LibraryEnum::CONTENT_EXTERNAL_LIVE &&
  186.             $library->getType() != LibraryEnum::CONTENT_NEW_LIVE
  187.         ){
  188.             return;
  189.         }
  190.         $courseRepository $this->em->getRepository(Course::class);
  191.         $isStudent $courseRepository->isStudent($lesson->getCourse());
  192.         if($lesson->getShowLiveChat() == LessonEnum::NO && $isStudent){
  193.             return;
  194.         }
  195.         
  196.         $today date('Y-m-d H:i:s');
  197.         $todayDate date('Y-m-d');
  198.         $timeToday strtotime($today);
  199.         $showLiveChat LessonEnum::NO;
  200.         $liveDateStart $library->getLiveStart();
  201.         $timeLiveEnd date('Y-m-d'strtotime($liveDateStart));
  202.         $timeLiveStart strtotime($liveDateStart ' - 1 hours'); 
  203.         
  204.         if($timeToday >= $timeLiveStart && $todayDate == $timeLiveEnd || !$isStudent){
  205.             $showLiveChat LessonEnum::YES;
  206.         }
  207.         if($showLiveChat == LessonEnum::NO){
  208.             return;
  209.         }
  210.         $client $this->getConfiguration()->getClient();
  211.         $clientId $client->getClientId();
  212.         $formatClientId = [
  213.             "1" => "00000{$clientId}",
  214.             "2" => "0000{$clientId}",
  215.             "3" => "000{$clientId}",
  216.             "4" => "00{$clientId}",
  217.             "5" => "0{$clientId}",
  218.         ];
  219.         
  220.         if(isset($formatClientId[strlen($clientId)])){
  221.             $clientId $formatClientId[strlen($clientId)];
  222.         }
  223.         
  224.         $channelId md5($clientId $lesson->getId());
  225.         $userId md5($clientId $this->getUser()->getId());
  226.         $isAdmin = ($isStudent false true );
  227.         $fileService $this->generalService->getService('FileService');
  228.         $photo $fileService->getFilePathComplete(
  229.             $this->getUser()->getPhoto(), 
  230.             UserEnum::PATH_PROFILES
  231.             true
  232.             true
  233.         );
  234.         $tokenAuth $this->generalService->getTokenCron();
  235.         $url "https://chat.eadplataforma.app/api/users/token";
  236.         $ch curl_init($url);
  237.         $dataPost = [
  238.             "isAdmin" => $isAdmin,
  239.             "id" => $userId,
  240.             "name" => $this->getUser()->getName(),
  241.             "roomId" => $channelId,
  242.             "photoSrc" => $photo,
  243.             "lessonId" => $lesson->getId(),
  244.             "clientId" => $clientId,
  245.         ];
  246.         curl_setopt($chCURLOPT_CUSTOMREQUEST'POST');
  247.         curl_setopt($chCURLOPT_POSTFIELDSjson_encode($dataPost));
  248.         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  249.         curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
  250.         curl_setopt($chCURLOPT_HTTPHEADER, [ 
  251.             "Authorization: {$tokenAuth}",
  252.             "Content-Type: application/json"
  253.         ]);
  254.         $dataToken curl_exec($ch);
  255.         $error curl_error($ch);
  256.         $dataToken json_decode($dataToken);
  257.         curl_close($ch);
  258.         return (object)[
  259.             "role" => ($isAdmin "admin" "user"),
  260.             "userId" => $userId,
  261.             "userFoto" => $photo,
  262.             "userName" => $this->getUser()->getName(),
  263.             "channelId" => $channelId,
  264.             "channelLogo" => "",
  265.             "channelName" => $lesson->getTitle(),
  266.             "userToken" => (!empty($dataToken->token) ? $dataToken->token null),
  267.             "clientId" => $clientId
  268.         ];
  269.     }
  270.     public function getLessonChatCredentials(Lesson $lesson)
  271.     {
  272.         $library $lesson->getLibrary();
  273.         if(!$this->getUser()){
  274.             return;
  275.         }
  276.         if(!$library){
  277.             return;
  278.         }
  279.         if(
  280.             $library->getType() != LibraryEnum::CONTENT_LIVE && 
  281.             $library->getType() != LibraryEnum::CONTENT_EXTERNAL_LIVE &&
  282.             $library->getType() != LibraryEnum::CONTENT_NEW_LIVE
  283.         ){
  284.             return;
  285.         }
  286.         $courseRepository $this->em->getRepository(Course::class);
  287.         $isStudent $courseRepository->isStudent($lesson->getCourse());
  288.         if($lesson->getShowLiveChat() == LessonEnum::NO && $isStudent){
  289.             return;
  290.         }
  291.         
  292.         $today date('Y-m-d H:i:s');
  293.         $todayDate date('Y-m-d');
  294.         $timeToday strtotime($today);
  295.         $showLiveChat LessonEnum::NO;
  296.         $liveDateStart $library->getLiveStart();
  297.         $timeLiveEnd date('Y-m-d'strtotime($liveDateStart));
  298.         $timeLiveStart strtotime($liveDateStart ' - 1 hours'); 
  299.         
  300.         if($timeToday >= $timeLiveStart && $todayDate == $timeLiveEnd || !$isStudent){
  301.             $showLiveChat LessonEnum::YES;
  302.         }
  303.         if($showLiveChat == LessonEnum::NO){
  304.             return;
  305.         }
  306.         $header = [ 
  307.             'typ' => 'JWT'
  308.             'alg' => 'HS256'
  309.         ];
  310.         $info $this->generalService->getServiceAccess(ServicesEnum::LIVECHAT);
  311.         $client $this->getConfiguration()->getClient();
  312.         $clientId $client->getClientId();
  313.         $formatClientId = [
  314.             "1" => "00000{$clientId}",
  315.             "2" => "0000{$clientId}",
  316.             "3" => "000{$clientId}",
  317.             "4" => "00{$clientId}",
  318.             "5" => "0{$clientId}",
  319.         ];
  320.         
  321.         if(isset($formatClientId[strlen($clientId)])){
  322.             $clientId $formatClientId[strlen($clientId)];
  323.         }
  324.         
  325.         $channelId md5($clientId $lesson->getId());
  326.         $clientStream = new \GetStream\StreamChat\Client($info->client$info->token);
  327.         $channel $clientStream->Channel("livestream"$channelId);
  328.         $userId md5($clientId $this->getUser()->getId());
  329.         $role = ($isStudent 'user' 'admin' );
  330.         $stringUtil $this->generalService->getUtil('StringUtil');
  331.         $chatToken $info->token;
  332.         $segments = [];
  333.         $segments[] = $stringUtil->urlSafeB64Encode(json_encode($header));
  334.         $segments[] = $stringUtil->urlSafeB64Encode(json_encode([ 
  335.             "user_id" => $userId 
  336.         ]));
  337.         $signingInput implode('.'$segments);
  338.         $signature hash_hmac('SHA256'$signingInput$chatTokentrue);
  339.         $segments[] = $stringUtil->urlSafeB64Encode($signature);
  340.         $userToken =  implode('.'$segments);
  341.         $fileService $this->generalService->getService('FileService');
  342.         $photo $fileService->getFilePathComplete(
  343.             $this->getUser()->getPhoto(), 
  344.             UserEnum::PATH_PROFILES
  345.             true
  346.             true
  347.         );
  348.         return (object)[
  349.             "role" => $role,
  350.             "userId" => $userId,
  351.             "userFoto" => $photo,
  352.             "userName" => $this->getUser()->getName(),
  353.             "channelId" => $channelId,
  354.             "channelLogo" => "",
  355.             "channelName" => $lesson->getTitle(),
  356.             "userToken" => $userToken,
  357.             "clientId" => $info->client
  358.         ];
  359.     }
  360.     public function getLessonTeacher(Lesson $lesson)
  361.     {
  362.         $userLesson $lesson->getUser();
  363.         $userPermissionUtil $this->generalService->getUtil('UserPermissionUtil');
  364.         $fileService $this->generalService->getService('FileService');
  365.         $dataImg = [
  366.             "fileName" => $userLesson->getPhoto(),
  367.             "pathConst" => LessonEnum::PATH_PROFILES,
  368.             "option" => "l-user-profile-teacher",
  369.             "addUpload" => true,
  370.             "addStream" => true,
  371.         ];
  372.         $request $this->generalService->getRequest();
  373.         $teacher = (object)[
  374.             "id" => $userLesson->getId(),
  375.             "name" => $userLesson->getName(),
  376.             "canAccessAdm" => $userPermissionUtil->canAccessAdm(),
  377.             "username" => $userLesson->getUsername(),
  378.             "photo" => $fileService->getFilePathObj($dataImg),
  379.             "permissionName" => (
  380.                 $userLesson->getUserProfile() ? 
  381.                 $userLesson->getUserProfile()->getName() 
  382.                 : null
  383.             ),
  384.         ];
  385.         return $teacher;
  386.     }
  387.     public function getLessonNextContent(Lesson $lesson, ?LessonLog $lessonLog null)
  388.     {
  389.         $libraryRepository $this->em->getRepository(Library::class);
  390.         $examRepository $this->em->getRepository(Exam::class);
  391.         $nextExamLesson $examRepository->getNextExamIdByLesson($lessontrue);
  392.         $lessonAfter $this->getLessonIdAfterThat($lessontrue);
  393.         $course $lesson->getCourse();
  394.         $courseId $course->getId();
  395.         $isAccessible false;
  396.         $acessMessage null;
  397.         $lessonAfterClass null;
  398.         $nextContent null;
  399.         if($nextExamLesson || $lessonAfter){
  400.             $contentPagesNumber null;
  401.             $contentDuration null;
  402.             $contentType null;
  403.             $contentThumb null;
  404.             if($lessonAfter && empty($nextExamLesson)){
  405.                 $lessonAfterClass $this->find($lessonAfter->id);
  406.                 if($lessonAfterClass){
  407.                     $library $lessonAfterClass->getLibrary();
  408.                     if($library){
  409.                         $contentPagesNumber $library->getPagesNumber();
  410.                         $contentDuration $library->getDuration();
  411.                         $contentType $library->getType();
  412.                         $libraryType = [
  413.                             LibraryEnum::CONTENT_EMBED
  414.                             LibraryEnum::CONTENT_CONFERENCE
  415.                             LibraryEnum::CONTENT_TEXT
  416.                             LibraryEnum::CONTENT_FILES
  417.                         ];
  418.                         if(!in_array($contentType$libraryType)){
  419.                             $contentThumb $libraryRepository->getCover($library);
  420.                         }
  421.                     }
  422.                 }
  423.             }
  424.             if($nextExamLesson){
  425.                 $nextExamLessonClass $examRepository->find($nextExamLesson->id);
  426.                 $infoAccess $examRepository->checkExamIsAccessible($nextExamLessonClass);
  427.                 $isAccessible $infoAccess->isAccessible;
  428.                 $acessMessage $infoAccess->message;
  429.             }else{
  430.                 $enrollmentRepository $this->em->getRepository(Enrollment::class);
  431.                 $courseRepository $this->em->getRepository(Course::class);
  432.                 $enrollment $enrollmentRepository->findOneBy([
  433.                     "user" => $this->getUser()->getId(),
  434.                     "course" => $courseId,
  435.                 ]);
  436.                 if($enrollment){
  437.                     $isStudent $courseRepository->isStudent($course);
  438.                     $infoAccess $this->checkLessonIsAccessibleToUser(
  439.                         $lessonAfterClass
  440.                         $enrollment
  441.                         $lessonLog
  442.                         $isStudent,
  443.                         $lesson->getId()
  444.                     );
  445.                     $isAccessible $infoAccess->isAccessible;
  446.                     $acessMessage $infoAccess->message;
  447.                 }
  448.             }
  449.             $module null;
  450.             if($lessonAfter){
  451.                 $module = (object)[
  452.                     "id" => (int)$lessonAfter->moduleId,
  453.                     "title" => $lessonAfter->moduleTitle,
  454.                 ];
  455.             }
  456.             if($nextExamLesson){
  457.                 $module null;
  458.                 if(!empty($nextExamLesson->moduleId)){
  459.                     $module = (object)[
  460.                         "id" => (int)$nextExamLesson->moduleId,
  461.                         "title" => $nextExamLesson->moduleTitle,
  462.                     ];
  463.                 }
  464.             }
  465.             $timeUtil $this->generalService->getUtil('DateTimeUtil');
  466.             $nextContent = (object)[
  467.                 "id" => (int)($nextExamLesson $nextExamLesson->id $lessonAfter->id),
  468.                 "lessonId" => (
  469.                     $nextExamLesson 
  470.                     $nextExamLesson->lessonId 
  471.                     (int)$lessonAfter->id
  472.                 ),
  473.                 "module" => $module,
  474.                 "courseId" => (
  475.                     $nextExamLesson 
  476.                     $nextExamLesson->courseId 
  477.                     (int)$lessonAfter->courseId
  478.                 ),
  479.                 "title" => ($nextExamLesson $nextExamLesson->title $lessonAfter->title),
  480.                 "type" => (
  481.                     $nextExamLesson 
  482.                     ($nextExamLesson->type == ExamEnum::QUIZ 'quiz' 'exam') : 
  483.                     'lesson'
  484.                 ),
  485.                 "contentPagesNumber" => $contentPagesNumber,
  486.                 "contentDuration" => $contentDuration $timeUtil->timeToSec($contentDuration) : null,
  487.                 "contentType" => $contentType,
  488.                 "contentThumb" => $contentThumb,
  489.                 "isAccessible" => $isAccessible,
  490.                 "acessMessage" => $acessMessage,
  491.             ];
  492.         }
  493.         return $nextContent;
  494.     }
  495.     public function getLessonLastContent(Lesson $lesson)
  496.     {
  497.         $libraryRepository $this->em->getRepository(Library::class);
  498.         $examRepository $this->em->getRepository(Exam::class);
  499.         $lastExamLesson $examRepository->getLastExamIdByLesson($lessontrue);
  500.         $lessonBefore $this->getLessonIdBeforeThat($lessontrue);
  501.         $timeUtil $this->generalService->getUtil('DateTimeUtil');
  502.         $lastContent null;
  503.         if($lastExamLesson || $lessonBefore){
  504.             $contentPagesNumber null;
  505.             $contentDuration null;
  506.             $contentType null;
  507.             $contentThumb null;
  508.             if($lessonBefore && empty($lastExamLesson)){
  509.                 $lessonBeforeClass $this->find($lessonBefore->id);
  510.                 if($lessonBeforeClass){
  511.                     $library $lessonBeforeClass->getLibrary();
  512.                     if($library){
  513.                         $contentPagesNumber $library->getPagesNumber();
  514.                         $contentDuration $library->getDuration();
  515.                         $contentType $library->getType();
  516.                         
  517.                         $libraryType = [
  518.                             LibraryEnum::CONTENT_EMBED
  519.                             LibraryEnum::CONTENT_CONFERENCE
  520.                             LibraryEnum::CONTENT_TEXT
  521.                             LibraryEnum::CONTENT_FILES
  522.                         ];
  523.                         if(!in_array($contentType$libraryType)){
  524.                             $contentThumb $libraryRepository->getCover($library);
  525.                         }
  526.                     }
  527.                 }
  528.             }
  529.             $module = (object)[
  530.                 "id" => (int)$lessonBefore->moduleId,
  531.                 "title" => $lessonBefore->moduleTitle,
  532.             ];
  533.             if($lastExamLesson){
  534.                 $module null;
  535.                 if(!empty($lastExamLesson->moduleId)){
  536.                     $module = (object)[
  537.                         "id" => (int)$lastExamLesson->moduleId,
  538.                         "title" => $lastExamLesson->moduleTitle,
  539.                     ];
  540.                 }
  541.             }
  542.             $lastContent = (object)[
  543.                 "id" => (int)($lastExamLesson $lastExamLesson->id $lessonBefore->id),
  544.                 "lessonId" => (
  545.                     $lastExamLesson 
  546.                     $lastExamLesson->lessonId 
  547.                     (int)$lessonBefore->id
  548.                 ),
  549.                 "module" => $module,
  550.                 "courseId" => (
  551.                     $lastExamLesson 
  552.                     $lastExamLesson->courseId 
  553.                     (int)$lessonBefore->courseId
  554.                 ),
  555.                 "title" => ($lastExamLesson $lastExamLesson->title $lessonBefore->title),
  556.                 "type" => (
  557.                     $lastExamLesson 
  558.                     ($lastExamLesson->type == ExamEnum::QUIZ 'quiz' 'exam') : 
  559.                     'lesson'
  560.                 ),
  561.                 "contentPagesNumber" => $contentPagesNumber,
  562.                 "contentDuration" => $contentDuration $timeUtil->timeToSec($contentDuration) : null,
  563.                 "contentType" => $contentType,
  564.                 "contentThumb" => $contentThumb,
  565.                 "isAccessible" => true,
  566.                 "acessMessage" => null,
  567.             ];
  568.         }
  569.         return $lastContent;
  570.     }
  571.     public function getShowLiveChat(Lesson $lesson, ?bool $isStudent true)
  572.     {   
  573.         $library $lesson->getLibrary();
  574.         if(!$library){
  575.             return LessonEnum::NO;
  576.         }
  577.         if(
  578.             $library->getType() == LibraryEnum::CONTENT_LIVE ||
  579.             $library->getType() == LibraryEnum::CONTENT_NEW_LIVE
  580.         ){
  581.             if($this->configuration->checkModuleIsAbleOnPlan('liveContentFunction')){
  582.                 if($lesson->getShowLiveChat() == LessonEnum::YES){
  583.                     $liveDateStart $library->getLiveStart();
  584.                     $timeLiveEnd date('Y-m-d'strtotime($liveDateStart));
  585.                     $timeLiveStart strtotime($liveDateStart ' - 1 hours'); 
  586.                     $todayDate date('Y-m-d');
  587.                     $timeToday strtotime($todayDate);
  588.                     if(
  589.                         $timeToday >= $timeLiveStart && 
  590.                         $todayDate == $timeLiveEnd || !$isStudent
  591.                     ){
  592.                         return LessonEnum::YES;
  593.                     }
  594.                 }
  595.             }
  596.         }
  597.         return LessonEnum::NO;
  598.     }
  599.     public function findItems(?Course $course)
  600.     {
  601.         $query $this->createQueryBuilder('l');
  602.         if($course){
  603.             $query->andWhere('l.course = :courseId');
  604.             $query->setParameter('courseId'$course->getId());
  605.         }
  606.         return $query->orderBy('l.order''ASC')->getQuery()->getResult();
  607.     }
  608.     public function countLessons()
  609.     {
  610.         $query $this->createQueryBuilder('l');
  611.         $query->select("COUNT(l.id) AS total");
  612.         $query->andWhere('l.deleted != :deleted');
  613.         $query->setParameter('deleted'LessonEnum::ITEM_DELETED);
  614.         $result = (object)$query->getQuery()->getOneOrNullResult();
  615.         return $result->total;
  616.     }
  617.     public function countLessonByLibrary(int $libraryId, ?Lesson $lesson null)
  618.     {
  619.         
  620.         $query $this->createQueryBuilder('l');
  621.         $query->select("COUNT(l.id) AS total");
  622.         $query->andWhere('l.library = :library');
  623.         $query->andWhere('l.deleted != :deleted');
  624.         
  625.         $query->setParameter('deleted'LessonEnum::ITEM_DELETED);
  626.         $query->setParameter('library'$libraryId);
  627.         if(!empty($lesson)){
  628.             $query->andWhere('l.id =! :lessonId');
  629.             $query->setParameter('lessonId'$lesson->getId());
  630.         }
  631.         $result = (object)$query->getQuery()->getOneOrNullResult();
  632.         return $result->total;
  633.     }
  634.     public function countAllLessonByLibraryType(?int $status null, ?int $type null)
  635.     {
  636.         $query $this->createQueryBuilder('l');
  637.         $query->select("COUNT(l.id) AS total");
  638.         if(!is_null($type)){
  639.             $query->innerJoin('EADPlataforma:Library''lb''WITH''lb.id = l.library');
  640.             $query->andWhere('lb.type = :type');
  641.             $query->andWhere('lb.deleted = :deleted');
  642.             $query->setParameter('type'$type);
  643.             $query->setParameter('deleted'LibraryEnum::ITEM_NO_DELETED);
  644.         }
  645.         if(!empty($status)){
  646.             $query->andWhere('l.status = :status');
  647.             $query->setParameter('status'$status);
  648.         }
  649.         $query->andWhere('l.deleted = :deleted');
  650.         $query->setParameter('deleted'LessonEnum::ITEM_NO_DELETED);
  651.         $result = (object)$query->getQuery()->getOneOrNullResult();
  652.         return $result->total;
  653.     }
  654.     public function getAllLessonByLibraryLive(?int $user null)
  655.     {
  656.         $query $this->createQueryBuilder('l');
  657.         $query->innerJoin('EADPlataforma:Course''c''WITH''l.course = c.id');
  658.         $query->innerJoin('EADPlataforma:LessonModule''lm''WITH''l.lessonModule = lm.id');
  659.         $query->innerJoin('EADPlataforma:Library''lb''WITH''lb.id = l.library');
  660.         $query->andWhere('(
  661.             lb.type = :type OR 
  662.             lb.type = :typeExternalLive OR 
  663.             lb.type = :typeNewLive
  664.         )');
  665.         
  666.         $query->setParameter('type'LibraryEnum::CONTENT_LIVE);
  667.         $query->setParameter('typeExternalLive'LibraryEnum::CONTENT_EXTERNAL_LIVE);
  668.         $query->setParameter('typeNewLive'LibraryEnum::CONTENT_NEW_LIVE);
  669.         
  670.         if(!empty($user)){
  671.             
  672.             $query->andWhere('(
  673.                 l.user = :user OR 
  674.                 lm.user = :user OR 
  675.                 c.user = :user
  676.             )');
  677.             $query->setParameter('user'$user);
  678.         }
  679.         $query->andWhere('c.deleted = :deletedCourse');
  680.         $query->andWhere('lm.deleted = :deletedLessonModule');
  681.         $query->andWhere('lb.deleted = :deletedLibrary');
  682.         $query->andWhere('l.deleted = :deleted');
  683.         $query->setParameter('deletedCourse'CourseEnum::ITEM_NO_DELETED);
  684.         $query->setParameter('deletedLessonModule'LessonModuleEnum::ITEM_NO_DELETED);
  685.         $query->setParameter('deletedLibrary'LibraryEnum::ITEM_NO_DELETED);
  686.         $query->setParameter('deleted'LessonEnum::ITEM_NO_DELETED);
  687.         $data $query->getQuery()->getResult();
  688.         return $data;
  689.     }
  690.     public function getAllLessons()
  691.     {
  692.         $query $this->createQueryBuilder('l');
  693.         $query->select('
  694.             l.id AS lessonId,
  695.             c.id AS courseId,
  696.             lm.id AS lessonModuleId,
  697.             l.title AS lessonTitle,
  698.             c.title AS courseTitle,
  699.             lm.title AS lessonModuleTitle
  700.         ');
  701.         $query->innerJoin(
  702.             'EADPlataforma:Course'
  703.             'c'
  704.             'WITH'
  705.             'c.id = l.course AND c.deleted = :courseDeleted'
  706.         );
  707.         $query->innerJoin(
  708.             'EADPlataforma:LessonModule'
  709.             'lm'
  710.             'WITH'
  711.             'lm.id = l.lessonModule AND lm.deleted = :lessonModuleDeleted'
  712.         );
  713.         $query->andWhere('l.deleted = :lessonDeleted');
  714.         $query->setParameter('courseDeleted'CourseEnum::ITEM_NO_DELETED);
  715.         $query->setParameter('lessonModuleDeleted'LessonModuleEnum::ITEM_NO_DELETED);
  716.         $query->setParameter('lessonDeleted'LessonEnum::ITEM_NO_DELETED);
  717.         $query->addOrderBy('c.order''ASC');
  718.         $query->addOrderBy('lm.order''ASC');
  719.         $query->addOrderBy('l.order''ASC');
  720.         $data $query->getQuery()->getResult();
  721.         return $data;
  722.     }
  723.     public function getLessonPublicDemoById(int $lessonId)
  724.     {
  725.         $query $this->createQueryBuilder('l');
  726.         $query->innerJoin(
  727.             'EADPlataforma:LessonModule'
  728.             'lm'
  729.             'WITH'
  730.             'lm.id = l.lessonModule AND lm.deleted = 0'
  731.         );
  732.         $query->innerJoin(
  733.             'EADPlataforma:Course'
  734.             'c'
  735.             'WITH'
  736.             'c.id = l.course AND c.deleted = 0'
  737.         );
  738.         $query->andWhere('l.deleted = 0');
  739.         $query->andWhere('l.id = :lessonId');
  740.         $query->andWhere('l.demonstration = :demonstration');
  741.         $query->andWhere('c.status = :courseStatus');
  742.         $query->andWhere('l.status = :lessonStatus');
  743.         $query->andWhere('lm.status = :lessonModuleStatus');
  744.         $query->setParameter('lessonId'$lessonId);
  745.         $query->setParameter('demonstration'LessonEnum::YES);
  746.         $query->setParameter('courseStatus'CourseEnum::PUBLISHED);
  747.         $query->setParameter('lessonStatus'LessonEnum::PUBLISHED);
  748.         $query->setParameter('lessonModuleStatus'LessonModuleEnum::PUBLISHED);
  749.         $data $query->getQuery()->getOneOrNullResult();
  750.         return $data;
  751.     }
  752.     public function isLessonTeacher(Lesson $lessonUser $user)
  753.     {
  754.         if(!$lesson->getCourse()){
  755.             return false;
  756.         }
  757.         if(!$lesson->getLessonModule()){
  758.             return false;
  759.         }
  760.         $courseTeacherId $lesson->getCourse()->getUser()->getId();
  761.         $lessonModuleTeacherId $lesson->getLessonModule()->getUser()->getId();
  762.         $lessonTeacherId $lesson->getUser()->getId();
  763.         if(
  764.             $courseTeacherId == $user->getId() || 
  765.             $lessonModuleTeacherId == $user->getId() || 
  766.             $lessonTeacherId == $user->getId()
  767.         ){
  768.             return true;
  769.         }
  770.         return false;
  771.     }
  772.     public function checkLessonIsAccessibleToUser(
  773.         Lesson $lesson
  774.         Enrollment $enrollment
  775.         ?LessonLog $log null
  776.         ?bool $isStudent true,
  777.         ?int $lessonIdBefore null,
  778.         ?bool $blockIsNotAccess false
  779.     )
  780.     {
  781.         $info = (object)[
  782.             "isAccessible" => false,
  783.             "isAccessPeriodExpired" => false,
  784.             "message" => null,
  785.         ];
  786.         if(!$this->getUser()){
  787.             $info->message $this->configuration->getLanguage('error_auth''lesson_view_error');
  788.             return $info;
  789.         }
  790.         $user $enrollment->getUser();
  791.         $course $lesson->getCourse();
  792.         $lessonModule $lesson->getLessonModule();
  793.         $today strtotime('now');
  794.         if(!$isStudent){
  795.             $info->isAccessible true;
  796.             return $info;
  797.         }
  798.         if(!$enrollment){
  799.             $info->message $this->configuration->getLanguage(
  800.                 'error_enrollment_not_found',
  801.                 'lesson_view'
  802.             );
  803.             return $info;
  804.         }
  805.         if(
  806.             $lesson->getStatus() == LessonEnum::DRAFT ||
  807.             $lessonModule->getStatus() == LessonModuleEnum::DRAFT ||
  808.             $course->getStatus() == CourseEnum::DRAFT
  809.         ){
  810.             $info->message $this->configuration->getLanguage(
  811.                 'error_unavailable',
  812.                 'lesson_view'
  813.             );
  814.             return $info;
  815.         }
  816.         if($enrollment->getStatus(falsetrue) == EnrollmentEnum::STATUS_EXPIRED){
  817.         
  818.             $info->message $this->configuration->getLanguage(
  819.                 'error_enrollment_expired'
  820.                 'lesson_view_error'
  821.             );
  822.             return $info;
  823.         }
  824.         $courseDateRelease strtotime($course->getDateRelease());
  825.         if($courseDateRelease >= $today){
  826.             $info->message "{$this->configuration->getLanguage(
  827.                 'error_date_release'
  828.                 'lesson_view_error'
  829.             )} {$course->getDateRelease('d/m/Y')}";
  830.             return $info;
  831.         }
  832.         $lessonModuleRepository $this->em->getRepository(LessonModule::class);
  833.         $moduleInfo $lessonModuleRepository->checkLessonModuleIsAccessibleToUser(
  834.             $lessonModule
  835.             $enrollment
  836.             $isStudent,
  837.             $lessonIdBefore
  838.         );
  839.         
  840.         if(!$moduleInfo->isAccessible) {
  841.             return $moduleInfo;
  842.         }
  843.         $unlimitedexamFunction $this->configuration->checkModuleIsAbleOnPlan(
  844.             'unlimitedExamFunction'
  845.         );
  846.         $hasLessonControl $this->configuration->checkModuleIsAbleOnPlan(
  847.             'lessonControlFunction'
  848.         );
  849.         $examModule $this->configuration->isModuleActive("exam_module");
  850.         if(!$hasLessonControl){
  851.             $info->isAccessible true;
  852.             return $info;
  853.         }
  854.         $lessonLogRepository $this->emEadmin->getRepository(LessonLog::class);
  855.         if(empty($log)){
  856.             $logLessonId "{$course->getId()}#{$user->getId()}#{$lesson->getId()}";
  857.             $log $lessonLogRepository->find($logLessonId);
  858.         }
  859.         if($log && !empty($log->getDateConclusion())){
  860.             $info->isAccessible true;
  861.             $periodEnd $lesson->getControlClosePeriod();
  862.             if(!empty($periodEnd)){
  863.                 $dateEnd strtotime("{$log->getDateConclusion()} + {$periodEnd} days");
  864.                 if($dateEnd $today){                    
  865.                     $info->isAccessible false;
  866.                     $info->isAccessPeriodExpired true;
  867.                 }
  868.             }
  869.             return $info;
  870.         }
  871.         if($blockIsNotAccess){
  872.             $info->message $this->configuration->getLanguage(
  873.                 'error_required_incomplete'
  874.                 'lesson_view_error'
  875.             );
  876.             return $info;
  877.         }
  878.         if($examModule){
  879.             if($log && empty($log->getDateConclusion()) || !$log){
  880.                 $examUserRepository $this->em->getRepository(ExamUser::class);
  881.                 $currentExam $examUserRepository->getCurrentExamOnCourseToUserByLesson(
  882.                     $lesson
  883.                     $user
  884.                 );
  885.                 if($currentExam){
  886.                     if($unlimitedexamFunction){
  887.                         //check the exam type
  888.                         if($currentExam->getType() == ExamEnum::LESSON){
  889.                             $lessonExam $currentExam->getLesson();
  890.                             $lessonModuleExam $lessonExam->getLessonModule();
  891.                             //if exam is from this lesson redirect to exam
  892.                             if($lessonExam->getId() != $lesson->getId()){
  893.                                 //if the exam is from a module before that redirect to exam
  894.                                 if($lessonModuleExam->getOrder() < $lessonModule->getOrder()){
  895.                                     $info->message $this->configuration->getLanguage(
  896.                                         'error_required_incomplete'
  897.                                         'lesson_view_error'
  898.                                     );
  899.                                     return $info;
  900.                                 }else if(
  901.                                     $lessonExam->getOrder() < $lesson->getOrder() && 
  902.                                     $lessonModuleExam->getId() == $lessonModule->getId()
  903.                                 ){ //if is the same module but the lesson is before that rediret to exam
  904.                                     $info->message $this->configuration->getLanguage(
  905.                                         'error_required_incomplete'
  906.                                         'lesson_view_error'
  907.                                     );
  908.                                     return $info;
  909.                                 }
  910.                             }
  911.                         }else if($currentExam->getType() == ExamEnum::MODULE){
  912.                             $lessonModuleExam $currentExam->getLessonModule();
  913.                             
  914.                             if(
  915.                                 $lessonModuleExam->getId() != $lessonModule->getId() && 
  916.                                 $lessonModuleExam->getOrder() < $lessonModule->getOrder()
  917.                             ){ //if is not the same module and the module is before that rediret to exam
  918.                                 $info->message $this->configuration->getLanguage(
  919.                                     'error_required_incomplete'
  920.                                     'lesson_view_error'
  921.                                 );
  922.                                 return $info;
  923.                             }
  924.                         }
  925.                     }
  926.                     if($currentExam->getType() == ExamEnum::COURSE){
  927.                         $info->message $this->configuration->getLanguage(
  928.                             'error_required_incomplete'
  929.                             'lesson_view_error'
  930.                         );
  931.                         return $info;
  932.                     }
  933.                 }
  934.                 if($unlimitedexamFunction){
  935.                     $examRepository $this->em->getRepository(Exam::class);
  936.                     $exam $examRepository->getCurrentNotStartedExamUser(
  937.                         $lesson
  938.                         $user
  939.                     );
  940.                     if($exam){
  941.                         $lessonExam $exam->getLesson();
  942.                         if(!$lessonExam || $lessonExam->getId() != $lesson->getId()){
  943.                             $info->message $this->configuration->getLanguage(
  944.                                 'error_required_incomplete'
  945.                                 'lesson_view_error'
  946.                             );
  947.                             return $info;
  948.                         }
  949.                     }
  950.                 }
  951.             }
  952.         }
  953.         //if exist a lesson with requirement before that redirect to there
  954.         $currentLesson $this->getCurrentLesson($lesson$user);
  955.         if(!empty($currentLesson)){
  956.             if($currentLesson->getId() != $lesson->getId()){
  957.                 $logId "{$course->getId()}#{$user->getId()}#{$currentLesson->getId()}";
  958.                 $logCurrent $lessonLogRepository->find($logId);
  959.                 if(empty($logCurrent)){
  960.                     $info->message $this->configuration->getLanguage(
  961.                         'error_required_incomplete'
  962.                         'lesson_view_error'
  963.                     );
  964.                     return $info;
  965.                 }else if(empty($logCurrent->getDateConclusion())){
  966.                     $info->message $this->configuration->getLanguage(
  967.                         'error_required_incomplete'
  968.                         'lesson_view_error'
  969.                     );
  970.                     return $info;
  971.                 }
  972.             }
  973.         }
  974.         if($lesson->getControlReleaseType() == LessonEnum::RELEASED){
  975.             $info->isAccessible true;
  976.             return $info;
  977.         }else if($lesson->getControlReleaseType() == LessonEnum::FIXED_DATE){
  978.             $controlDateRelease $lesson->getControlDateRelease();
  979.             $time strtotime($controlDateRelease);
  980.             $todaySeconds strtotime(date('Y-m-d H:i:s'));
  981.            
  982.             $controlClosePeriod $lesson->getControlClosePeriod();
  983.             if(!empty($controlClosePeriod)){
  984.                 $timeLog strtotime(
  985.                     "{$controlDateRelease} + {$controlClosePeriod} day"
  986.                 );
  987.                 if($todaySeconds $timeLog){
  988.                     $timeLogText date('d/m/Y H:i:s'$timeLog);
  989.                     $info->message "{$this->configuration->getLanguage(
  990.                         'error_date_end'
  991.                         'lesson_view_error'
  992.                     )} {$timeLogText}";
  993.                     return $info;
  994.                 }
  995.             }
  996.            
  997.             if($today >= $time){
  998.                 $info->isAccessible true;
  999.                 return $info;
  1000.             }
  1001.             $timeText date('d/m/Y H:i:s'$time);
  1002.                         
  1003.             $info->message "{$this->configuration->getLanguage(
  1004.                 'error_date_release'
  1005.                 'lesson_view_error'
  1006.             )} {$timeText}";
  1007.             return $info;
  1008.         }else if($lesson->getControlReleaseType() == LessonEnum::FLEXIBLE_DATE){
  1009.             $controlReleasePeriod $lesson->getControlReleasePeriod();
  1010.             if($lesson->getControlReleaseAfterType() == LessonEnum::ENROLLMENT){
  1011.                 $time strtotime(
  1012.                     "{$enrollment->getDateRegister()} + {$controlReleasePeriod} day"
  1013.                 );
  1014.                 
  1015.                 $todaySeconds strtotime(date('Y-m-d H:i:s'));
  1016.                 $controlClosePeriod $lesson->getControlClosePeriod();
  1017.                 if(!empty($controlClosePeriod)){
  1018.                     
  1019.                     /*
  1020.                     if($log && empty($log->getDateConclusion()) || !$log){
  1021.                         return false;
  1022.                     }
  1023.                     
  1024.                     if($log->getComplete() == LessonEnum::NO){
  1025.                         return false;
  1026.                     }*/
  1027.                     if($log && !empty($log->getDateConclusion())){
  1028.                         $timeLog strtotime(
  1029.                             "{$log->getDateConclusion()} + {$controlClosePeriod} day"
  1030.                         );
  1031.                         if($todaySeconds $timeLog){
  1032.                             $timeLogText date('d/m/Y H:i:s'$timeLog);
  1033.                             $info->message "{$this->configuration->getLanguage(
  1034.                                 'error_date_end'
  1035.                                 'lesson_view_error'
  1036.                             )} {$timeLogText}";
  1037.                             return $info;
  1038.                         }
  1039.                     }
  1040.                 }
  1041.                 
  1042.                 if($today >= $time){
  1043.                     $info->isAccessible true;
  1044.                     return $info;
  1045.                 }
  1046.                 $timeText date('d/m/Y H:i:s'$time);
  1047.                         
  1048.                 $info->message "{$this->configuration->getLanguage(
  1049.                     'error_date_release'
  1050.                     'lesson_view_error'
  1051.                 )} {$timeText}";
  1052.                 return $info;
  1053.             }else if(
  1054.                 $lesson->getControlReleaseAfterType() == LessonEnum::LAST_LESSON && 
  1055.                 !empty($lessonIdBefore)
  1056.             ){
  1057.                 $logId "{$course->getId()}#{$user->getId()}#{$lessonIdBefore}";
  1058.                 $logLast $lessonLogRepository->find($logId);
  1059.                 
  1060.                 if($logLast && !empty($logLast->getDateConclusion())){
  1061.                     $time strtotime(
  1062.                         "{$logLast->getDateConclusion()} + {$controlReleasePeriod} day"
  1063.                     );
  1064.                     if($today >= $time){
  1065.                         $info->isAccessible true;
  1066.                         return $info;
  1067.                     }
  1068.                 }
  1069.             }
  1070.         }
  1071.         $info->message $this->configuration->getLanguage('error_unavailable''lesson_view_error');
  1072.         return $info;
  1073.     }
  1074.     public function getLessonDateReleaseAccess(
  1075.         Lesson $lesson
  1076.         Enrollment $enrollment
  1077.         ?bool $isStudent true
  1078.         ?int $lessonIdBefore null
  1079.     )
  1080.     {
  1081.         $date date('Y-m-d H:i:s');
  1082.         if(!$this->getUser()){
  1083.             return $date;
  1084.         }
  1085.         $user $enrollment->getUser();
  1086.         $course $lesson->getCourse();
  1087.         $today strtotime('now');
  1088.         if(!$isStudent){
  1089.             return $date;
  1090.         }
  1091.         if(!$enrollment){
  1092.             return $date;
  1093.         }
  1094.         $lessonLogRepository $this->emEadmin->getRepository(LessonLog::class);
  1095.         if($lesson->getStatus() == LessonEnum::PUBLISHED){
  1096.             if($lesson->getControlReleaseType() == LessonEnum::RELEASED){
  1097.                 return $date;
  1098.             }else if($lesson->getControlReleaseType() == LessonEnum::FIXED_DATE){
  1099.                 return $lesson->getControlDateRelease();
  1100.             }else if($lesson->getControlReleaseType() == LessonEnum::FLEXIBLE_DATE){
  1101.                 $controlReleasePeriod $lesson->getControlReleasePeriod();
  1102.                 if($lesson->getControlReleaseAfterType() == LessonEnum::ENROLLMENT){
  1103.                     $time strtotime(
  1104.                         "{$enrollment->getDateRegister()} + {$controlReleasePeriod} day"
  1105.                     );
  1106.                     return date('Y-m-d H:i:s'$time);
  1107.                 }else if(
  1108.                     $lesson->getControlReleaseAfterType() == LessonEnum::LAST_LESSON && 
  1109.                     !empty($lessonIdBefore)
  1110.                 ){
  1111.                     $logId "{$course->getId()}#{$user->getId()}#{$lessonIdBefore}";
  1112.                     $logLast $lessonLogRepository->find($logId);
  1113.                     if($logLast){
  1114.                         $time strtotime(
  1115.                             "{$logLast->getDateConclusion()} + {$controlReleasePeriod} day"
  1116.                         );
  1117.                         return date('Y-m-d H:i:s'$time);
  1118.                     }
  1119.                 }
  1120.             }
  1121.         }
  1122.         return $date;
  1123.     }
  1124.     public function getCurrentLessonLogOrigin(Lesson $lessonBase)
  1125.     {
  1126.         $course $lessonBase->getCourse();
  1127.         $lessonModule $lessonBase->getLessonModule();
  1128.         $query $this->createQueryBuilder('l');
  1129.         $query->select('l.id');
  1130.         $query->innerJoin(
  1131.             'EADPlataforma:LessonModule'
  1132.             'lm'
  1133.             'WITH'
  1134.             'lm.id = l.lessonModule'
  1135.         );
  1136.         $query->leftJoin(
  1137.             'EADPlataforma:LessonLogOrigin'
  1138.             'lg'
  1139.             'WITH'
  1140.             'lg.lesson = l.id AND lg.user = :user'
  1141.         );
  1142.         $query->andWhere('l.deleted = 0');
  1143.         $query->andWhere('lm.deleted = 0');
  1144.         $query->andWhere('l.controlRequirement = :requirement');
  1145.         $query->andWhere('l.status = :lessonStatus');
  1146.         $query->andWhere('lm.status = :lessonModuleStatus');
  1147.         $query->andWhere('l.course = :course');
  1148.         if($lessonBase->getOrder() == LessonEnum::YES){
  1149.             $query->andWhere('(
  1150.                 ( l.order <= :orderL AND lm.order = :orderLM ) OR ( lm.order < :orderLM )
  1151.             )');
  1152.         }else{
  1153.             $query->andWhere('(
  1154.                 ( l.order < :orderL AND lm.order = :orderLM ) OR ( lm.order < :orderLM )
  1155.             )');
  1156.         }
  1157.         $query->andWhere('lg.dateConclusion IS NULL');
  1158.         $query->setParameter('user'$this->getUser()->getId());
  1159.         $query->setParameter('course'$lessonBase->getCourse()->getId());
  1160.         $query->setParameter('requirement'LessonEnum::YES);
  1161.         $query->setParameter('orderL'$lessonBase->getOrder());
  1162.         $query->setParameter('orderLM'$lessonModule->getOrder());
  1163.         $query->setParameter('lessonStatus'LessonEnum::PUBLISHED);
  1164.         $query->setParameter('lessonModuleStatus'LessonModuleEnum::PUBLISHED);
  1165.         $query->addOrderBy('lm.order''ASC');
  1166.         $query->addOrderBy('l.order''ASC');
  1167.         $query->setMaxResults(1);
  1168.         $lesson = (object)$query->getQuery()->getOneOrNullResult();
  1169.         if(!empty($lesson->id)){
  1170.             return $this->findOneBy([ "id" => $lesson->id ]);
  1171.         }
  1172.         return;
  1173.     }
  1174.     public function getCurrentLesson(Lesson $lessonBaseUser $user)
  1175.     {
  1176.         $course $lessonBase->getCourse();
  1177.         $lessonModule $lessonBase->getLessonModule();
  1178.         $query $this->createQueryBuilder('l');
  1179.         $query->select('l.id');
  1180.         $query->innerJoin('EADPlataforma:Course''c''WITH''c.id = l.course');
  1181.         $query->innerJoin(
  1182.             'EADPlataforma:LessonModule''lm''WITH''lm.id = l.lessonModule'
  1183.         );
  1184.         $query->innerJoin('EADPlataforma:Library''lb''WITH''lb.id = l.library');
  1185.         
  1186.         $query->andWhere('c.deleted = 0');
  1187.         $query->andWhere('l.deleted = 0');
  1188.         $query->andWhere('lm.deleted = 0');
  1189.         $query->andWhere('lb.deleted = 0');
  1190.         $query->andWhere('l.controlRequirement = :requirement');
  1191.         $query->andWhere('l.status = :lessonStatus');
  1192.         $query->andWhere('lm.status = :lessonModuleStatus');
  1193.         $query->andWhere('l.course = :course');
  1194.         $query->andWhere('l.id != :lessonBase');
  1195.         if($lessonBase->getOrder() == LessonEnum::YES){
  1196.             $query->andWhere('(
  1197.                 ( l.order <= :orderL AND lm.order = :orderLM ) OR ( lm.order < :orderLM )
  1198.             )');
  1199.         }else{
  1200.             $query->andWhere('(
  1201.                 ( l.order < :orderL AND lm.order = :orderLM ) OR ( lm.order < :orderLM )
  1202.             )');
  1203.         }
  1204.         $query->setParameter('course'$lessonBase->getCourse()->getId());
  1205.         $query->setParameter('requirement'LessonEnum::YES);
  1206.         $query->setParameter('orderL'$lessonBase->getOrder());
  1207.         $query->setParameter('orderLM'$lessonModule->getOrder());
  1208.         $query->setParameter('lessonStatus'LessonEnum::PUBLISHED);
  1209.         $query->setParameter('lessonModuleStatus'LessonModuleEnum::PUBLISHED);
  1210.         $query->setParameter('lessonBase'$lessonBase->getId());
  1211.         $query->addOrderBy('lm.order''DESC');
  1212.         $query->addOrderBy('l.order''DESC');
  1213.         $query->setMaxResults(1);
  1214.         $lesson = (object)$query->getQuery()->getOneOrNullResult();
  1215.         if(!empty($lesson->id)){
  1216.             $logId "{$course->getId()}#{$user->getId()}#{$lesson->id}";
  1217.             $log $this->emEadmin->getRepository(LessonLog::class)->find($logId);
  1218.             if(!$log || ($log && empty($log->getDateConclusion()))){
  1219.                 return $this->findOneBy([ "id" => $lesson->id ]);
  1220.             }
  1221.         }
  1222.         return;
  1223.     }
  1224.     public function getCourseLessonsIds(
  1225.         Course $course
  1226.         ?LessonModule $lessonModule null
  1227.         ?string $searchText null
  1228.     )
  1229.     {
  1230.         $userPermissionUtil $this->generalService->getUtil('UserPermissionUtil');
  1231.         $permission $userPermissionUtil->getPermission("course""see");
  1232.         $user $userPermissionUtil->getUser();
  1233.         $lessonModuleId = ($lessonModule $lessonModule->getId() : null);
  1234.         $keyCache md5(
  1235.             "getCourseLessonsIds{$course->getId()}-{$user->getId()}-{$lessonModuleId}"
  1236.         );
  1237.         if(isset($this->lessonIdsCourse[$keyCache])){
  1238.             return $this->lessonIdsCourse[$keyCache];
  1239.         }
  1240.         $isInTeam $this->em->getRepository(CourseTeam::class)->userExistInCourseTeam(
  1241.             $course
  1242.             $user
  1243.         );
  1244.         
  1245.         $query $this->createQueryBuilder('l');
  1246.         $query->select('
  1247.             l.id, 
  1248.             l.title,
  1249.             IDENTITY(l.course) AS courseId,
  1250.             IDENTITY(l.lessonModule) AS moduleId,
  1251.             lm.title AS moduleTitle
  1252.         ');
  1253.         $query->innerJoin(
  1254.             'EADPlataforma:LessonModule''lm''WITH''lm.id = l.lessonModule'
  1255.         );
  1256.         if(!empty($searchText)){
  1257.             $query->andWhere('l.title LIKE :searchText');
  1258.             $query->setParameter('searchText'"%{$searchText}%");
  1259.         }
  1260.         $query->andWhere('l.deleted = 0');
  1261.         $query->andWhere('lm.deleted = 0');
  1262.         $query->andWhere('l.course = :courseId');
  1263.         $query->setParameter('courseId'$course->getId());
  1264.         if($lessonModule){
  1265.             $query->andWhere('l.lessonModule = :lessonModuleId');
  1266.             $query->setParameter('lessonModuleId'$lessonModule->getId());
  1267.         }
  1268.         if(
  1269.             $userPermissionUtil->isLow($permission) || 
  1270.             ($userPermissionUtil->isMiddle($permission) && !$isInTeam)
  1271.         ){
  1272.             $query->andWhere('l.status = :lessonStatus');
  1273.             $query->andWhere('lm.status = :lessonModuleStatus');
  1274.             $query->setParameter('lessonStatus'LessonEnum::PUBLISHED);
  1275.             $query->setParameter('lessonModuleStatus'LessonModuleEnum::PUBLISHED);
  1276.         }
  1277.         
  1278.         $query->addOrderBy('lm.order''ASC');
  1279.         $query->addOrderBy('l.order''ASC');
  1280.         
  1281.         $data $query->getQuery()->execute();
  1282.         $this->lessonIdsCourse[$keyCache] = $data;
  1283.         return $this->lessonIdsCourse[$keyCache];
  1284.     }
  1285.     public function getLessonIdBeforeThat(Lesson $lessonBasebool $complete false)
  1286.     {
  1287.         $lessons $this->getCourseLessonsIds($lessonBase->getCourse());
  1288.         foreach ($lessons as $key => $lesson) {
  1289.             $lesson = (object)$lesson;
  1290.             if($lesson->id == $lessonBase->getId()){
  1291.                 if($key == 0){
  1292.                     return;
  1293.                 }else{
  1294.                     $obj = (object)$lessons[$key-1];
  1295.                     if($complete){
  1296.                         return $obj;
  1297.                     }
  1298.                     
  1299.                     return $obj->id;
  1300.                 }
  1301.             }
  1302.         }
  1303.         return;
  1304.     }
  1305.     public function getLessonIdAfterThat(Lesson $lessonBasebool $complete false)
  1306.     {
  1307.         $lessons $this->getCourseLessonsIds($lessonBase->getCourse());
  1308.         $maxPosition count($lessons) - 1;
  1309.         foreach ($lessons as $key => $lesson) {
  1310.             $lesson = (object)$lesson;
  1311.             if($lesson->id == $lessonBase->getId()){
  1312.                 if($key == $maxPosition){
  1313.                     return;
  1314.                 }else{
  1315.                     $obj = (object)$lessons[$key+1];
  1316.                     if($complete){
  1317.                         return $obj;
  1318.                     }
  1319.                     return $obj->id;
  1320.                 }
  1321.             }
  1322.         }
  1323.         return;
  1324.     }
  1325.     public function refreshOrder(
  1326.         $courseId
  1327.         $lessonModuleId
  1328.         $oldOrder
  1329.         $newOrder
  1330.         $isDelete false
  1331.     )
  1332.     {
  1333.         $sql "UPDATE EADPlataforma:Lesson AS l 
  1334.                 SET l.order = l.order - 1 
  1335.                 WHERE l.order > :oldOrder
  1336.                 AND l.deleted = 0
  1337.                 AND l.course = :courseId
  1338.                 AND l.lessonModule = :lessonModuleId ";
  1339.         
  1340.         $query $this->em->createQuery($sql);
  1341.         $query->setParameter('oldOrder'$oldOrder);
  1342.         $query->setParameter('courseId'$courseId);
  1343.         $query->setParameter('lessonModuleId'$lessonModuleId);
  1344.         
  1345.         $query->execute();
  1346.         if(!$isDelete){
  1347.             $sql "UPDATE EADPlataforma:Lesson AS l 
  1348.                     SET l.order = l.order + 1 
  1349.                     WHERE l.order >= :newOrder
  1350.                     AND l.deleted = 0
  1351.                     AND l.course = :courseId
  1352.                     AND l.lessonModule = :lessonModuleId ";
  1353.             $query $this->em->createQuery($sql);
  1354.             
  1355.             $query->setParameter('newOrder'$newOrder);
  1356.             $query->setParameter('courseId'$courseId);
  1357.             $query->setParameter('lessonModuleId'$lessonModuleId);
  1358.             $query->execute();
  1359.         }
  1360.     }
  1361.     public function copyLesson(
  1362.         LessonModule $newModule
  1363.         Lesson $lesson
  1364.         $isPartial false,
  1365.         $isModulePartial false
  1366.         $copyType null
  1367.     )
  1368.     {
  1369.         $course $newModule->getCourse();
  1370.         $courseId $course->getId();
  1371.     
  1372.         $newLesson = clone $lesson;
  1373.         $newLesson->setLessonModule($newModule);
  1374.         $newLesson->setCourse($course);
  1375.         if($copyType == LessonEnum::COPY_PUBLISHED){
  1376.             $newLesson->setStatus(LessonEnum::PUBLISHED);
  1377.         }else if($copyType == LessonEnum::COPY_DRAFT){
  1378.             $newLesson->setStatus(LessonEnum::DRAFT);
  1379.         }
  1380.         
  1381.         if($isPartial){
  1382.             $lastOrder $this->count([ 
  1383.                 "course" => $courseId,
  1384.                 "lessonModule" => $newModule->getId(),
  1385.                 "deleted" => LessonEnum::ITEM_NO_DELETED 
  1386.             ]);
  1387.             $newLesson->setOrder($lastOrder 1);
  1388.         }
  1389.         $this->em->persist($newLesson);
  1390.         $this->em->flush();
  1391.         if($isPartial || $isModulePartial){
  1392.             $userLessonId $newLesson->getUser()->getId();
  1393.             $courseTeam $this->em->getRepository(CourseTeam::class)->findOneBy([
  1394.                 "course" => $courseId
  1395.                 "user" => $userLessonId,
  1396.                 "deleted" => LessonEnum::ITEM_NO_DELETED
  1397.             ]);
  1398.             
  1399.             if(!$courseTeam){
  1400.                 $newCourseTeam = new CourseTeam();
  1401.                 $newCourseTeam->setCourse($course);
  1402.                 $newCourseTeam->setUser($newLesson->getUser());
  1403.                 $this->em->persist($newCourseTeam);
  1404.                 $this->em->flush();
  1405.             }
  1406.         }
  1407.         
  1408.         $files $this->em->getRepository(LessonXLibrary::class)->findBy([
  1409.             "lesson"=> $lesson->getId(),
  1410.             "deleted"=> LessonXLibraryEnum::ITEM_NO_DELETED
  1411.         ]);
  1412.         
  1413.         if($files){
  1414.             foreach ($files as $key => $file) {
  1415.                 $newFile = clone $file;
  1416.                 $newFile->setLesson($newLesson);
  1417.                 
  1418.                 $this->em->persist($newFile);
  1419.                 $this->em->flush();
  1420.             }
  1421.         }
  1422.         
  1423.         $this->em->getRepository(Exam::class)->copyExamByLesson($lesson$newLesson);
  1424.         $this->em->getRepository(Exam::class)->copyQuizByLesson($lesson$newLesson);
  1425.     }
  1426.     public function notifyStudentsLesson(Lesson $lesson$type)
  1427.     {   
  1428.         if(!$this->configuration->checkModuleIsAbleOnPlan('notificationFunction')){
  1429.             return;
  1430.         }
  1431.         $today date('Y-m-d');
  1432.         $dateLastNotify $lesson->getDateLastNotify('Y-m-d');
  1433.         if($today != $dateLastNotify && $lesson->getStatus() == LessonEnum::PUBLISHED){
  1434.             $course $lesson->getCourse();
  1435.             $lessonModule $lesson->getLessonModule();
  1436.             if(
  1437.                 $course->getStatus() == CourseEnum::PUBLISHED && 
  1438.                 $lessonModule->getStatus() == LessonModuleEnum::PUBLISHED
  1439.             ){
  1440.                 $taskQueueService $this->generalService->getService('TaskQueueService');
  1441.                 $enrollmentRepository $this->em->getRepository(Enrollment::class);
  1442.                 
  1443.                 $enrollments $enrollmentRepository->getEnrollmentsByCourse(
  1444.                     $course->getId(), 
  1445.                     EnrollmentEnum::STATUS_ACTIVE
  1446.                 );
  1447.                 foreach ($enrollments as $key => $enrollment) {
  1448.                     if($enrollment->getUser()->getAllowNotifyNewLesson() == LessonEnum::YES){
  1449.                         $taskQueueService->insert($type$enrollment->getUser(), $lesson->getId());
  1450.                     }
  1451.                 }
  1452.                 $lesson->setDateLastNotify(date('Y-m-d H:i:s'));
  1453.                 $this->em->flush();
  1454.             }
  1455.         }
  1456.     }
  1457.     public function lessonUpdateOrder($modules)
  1458.     {
  1459.         $dqlModule "UPDATE EADPlataforma:LessonModule AS lm SET lm.order = CASE";
  1460.         $idModules "";
  1461.         $dqlExam "";
  1462.         $newModuleId "";
  1463.         $whereExam "";
  1464.         $caseLesson1 " l.order = CASE ";
  1465.         $caseLesson2 " l.lessonModule = CASE ";
  1466.         $caseLesson3 " e.lessonModule = CASE ";
  1467.         $idsLesson "";
  1468.         foreach ($modules as $key => $value) {
  1469.             
  1470.             $orderNew $key 1;
  1471.             $dqlModule .= " WHEN lm.id = {$value->moduleId} THEN {$orderNew}";
  1472.             if(empty($idModules)){
  1473.                 $idModules .= "{$value->moduleId}";
  1474.             }else{
  1475.                 $idModules .= ", {$value->moduleId}";
  1476.             }
  1477.             foreach ($value->lessons as $key_lesson => $valLesson) {
  1478.                 if(!empty($valLesson->lessonId)){
  1479.                     $ordemNewLesson $key_lesson 1;
  1480.                 
  1481.                     $caseLesson1 .= " WHEN l.id = {$valLesson->lessonId} THEN {$ordemNewLesson}";
  1482.                     $caseLesson2 .= " WHEN l.id = {$valLesson->lessonId} THEN {$value->moduleId}";
  1483.                     $caseLesson3 .= " WHEN e.lesson = {$valLesson->lessonId} THEN {$value->moduleId}";
  1484.                 
  1485.                     if(empty($idsLesson)){
  1486.                         $idsLesson .= {$valLesson->lessonId} ";
  1487.                     }else{
  1488.                         $idsLesson .= ", {$valLesson->lessonId} ";
  1489.                     }
  1490.                 }
  1491.             }
  1492.         }
  1493.         
  1494.         $dqlModule .= " ELSE lm.order END WHERE lm.id IN ({$idModules}) ";
  1495.         $dqlLesson "UPDATE EADPlataforma:Lesson AS l 
  1496.                       SET {$caseLesson1} ELSE l.order END, 
  1497.                           {$caseLesson2} ELSE IDENTITY(l.lessonModule) END 
  1498.                      WHERE l.id IN ({$idsLesson}) ";
  1499.         $dqlExam "UPDATE EADPlataforma:Exam AS e
  1500.                     SET {$caseLesson3} ELSE IDENTITY(e.lessonModule) END 
  1501.                     WHERE e.lesson IN ({$idsLesson}) ";
  1502.         $query $this->em->createQuery($dqlModule);
  1503.         $queryLesson $this->em->createQuery($dqlLesson);
  1504.         $queryExam $this->em->createQuery($dqlExam);
  1505.         $query->execute();
  1506.         $queryLesson->execute();
  1507.         $queryExam->execute();
  1508.     }
  1509.     public function getLessonPublicNumber(
  1510.         Course $course
  1511.         ?LessonModule $lessonModule null,
  1512.         ?bool $required false
  1513.         ?bool $filterModule false
  1514.     )
  1515.     {
  1516.         $query $this->createQueryBuilder('l');
  1517.         $query->select('COUNT(l.id) AS total');
  1518.         $query->innerJoin(
  1519.             'EADPlataforma:LessonModule'
  1520.             'lm'
  1521.             'WITH'
  1522.             'lm.id = l.lessonModule AND lm.deleted = :deleted'
  1523.         );
  1524.         $query->innerJoin(
  1525.             'EADPlataforma:Course'
  1526.             'c'
  1527.             'WITH'
  1528.             'c.id = l.course AND c.deleted = :deleted'
  1529.         );
  1530.         if($lessonModule){
  1531.             $query->andWhere('lm.order <= :lessonModuleOrder');
  1532.             $query->setParameter('lessonModuleOrder'$lessonModule->getOrder());
  1533.             if($filterModule){
  1534.                 $query->andWhere('lm.id = :lessonModule');
  1535.                 $query->setParameter('lessonModule'$lessonModule->getId());
  1536.             }
  1537.         }
  1538.         if($required){
  1539.             $query->andWhere('l.controlRequirement = :required');
  1540.             $query->setParameter('required'LessonEnum::YES);
  1541.         }
  1542.         $query->andWhere('l.deleted = :deleted');
  1543.         $query->andWhere('l.course = :courseId');
  1544.         $query->andWhere('l.status = :lessonStatus');
  1545.         $query->andWhere('lm.status = :lessonModuleStatus');
  1546.         
  1547.         $query->setParameter('courseId'$course->getId());
  1548.         $query->setParameter('deleted'LessonEnum::ITEM_NO_DELETED);
  1549.         $query->setParameter('lessonStatus'LessonEnum::PUBLISHED);
  1550.         $query->setParameter('lessonModuleStatus'LessonModuleEnum::PUBLISHED);
  1551.         $query->setMaxResults(1);
  1552.         $result = (object)$query->getQuery()->getOneOrNullResult();
  1553.         return $result->total;
  1554.     }
  1555.     public function countCourseLessons(
  1556.         Course $course,
  1557.         int $userId,
  1558.         ?LessonModule $lessonModule null
  1559.         $isHistory false
  1560.     )
  1561.     {
  1562.         $userPermissionUtil $this->generalService->getUtil('UserPermissionUtil');
  1563.         $permission $userPermissionUtil->getPermission("course""see");
  1564.         $user $userPermissionUtil->getUser();
  1565.         $isInTeam $this->em->getRepository(CourseTeam::class)->userExistInCourseTeam(
  1566.             $course
  1567.             $user
  1568.         );
  1569.         $sqlDateConclusion "
  1570.             SELECT
  1571.                 (
  1572.                     CASE 
  1573.                         WHEN ll.complete = :yes THEN ll.date_conclusion 
  1574.                         ELSE CURDATE() 
  1575.                     END
  1576.                 ) AS date_conclusion
  1577.             FROM lesson_log AS ll
  1578.             WHERE ll.lesson_id = l.id
  1579.             AND ll.course_id = l.course_id
  1580.             AND ll.user_id = :userId
  1581.             ORDER BY ll.id DESC
  1582.             LIMIT 1
  1583.         ";
  1584.         $sqlDateRegisterEnrollement "
  1585.             SELECT
  1586.                 e.date_register
  1587.             FROM enrollment AS e
  1588.             WHERE e.deleted = :deletedEnrollment
  1589.             AND e.course_id = l.course_id
  1590.             AND e.user_id = :userId
  1591.             ORDER BY e.id DESC
  1592.             LIMIT 1
  1593.         ";
  1594.         $sql "
  1595.             SELECT
  1596.                 COUNT(l.id) AS total
  1597.             FROM lesson AS l
  1598.             INNER JOIN course AS c ON (c.id = l.course_id AND c.deleted = :deletedCourse)
  1599.             INNER JOIN lesson_module AS lm ON (
  1600.                 lm.id = l.lesson_module_id AND lm.deleted = :deletedModule
  1601.             )
  1602.             LEFT JOIN library AS lb ON (lb.id = l.library_id AND lb.deleted = :deletedLibrary)
  1603.             WHERE l.deleted = :deleted
  1604.             AND l.course_id = :courseId
  1605.             /* AND  CASO FOSSE CONSIDERADA DATA DE LIBERAÇÃO DA AULA PARA ENCERRAR O ACESSO
  1606.             (
  1607.                 (
  1608.                     l.control_close_period = 0 OR 
  1609.                     l.control_close_period IS NULL
  1610.                 )
  1611.                 OR
  1612.                 (
  1613.                     l.control_close_period > 0
  1614.                     AND l.control_release_type = 0
  1615.                     AND DATEDIFF(CURDATE(), (DATE(
  1616.                         (
  1617.                             CASE 
  1618.                                 WHEN ({$sqlDateConclusion}) IS NOT NULL THEN ({$sqlDateConclusion})
  1619.                                 ELSE CURDATE()
  1620.                             END
  1621.                         )
  1622.                     ) + INTERVAL l.control_close_period DAY)) < 0
  1623.                 )
  1624.                 OR
  1625.                 (
  1626.                     l.control_close_period > 0
  1627.                     AND l.control_release_type = 1
  1628.                     AND DATEDIFF(CURDATE(), (DATE(
  1629.                         (l.control_date_release)
  1630.                     ) + INTERVAL l.control_close_period DAY)) < 0
  1631.                 )
  1632.                 OR
  1633.                 (
  1634.                     l.control_close_period > 0
  1635.                     AND l.control_release_type = 2
  1636.                     AND l.control_release_after_type = 1
  1637.                     AND DATEDIFF(CURDATE(), (DATE(
  1638.                         ({$sqlDateRegisterEnrollement})
  1639.                     ) + INTERVAL l.control_close_period DAY)) < 0
  1640.                 )
  1641.                 OR
  1642.                 (
  1643.                     l.control_close_period > 0
  1644.                     AND l.control_release_type = 2
  1645.                     AND l.control_release_after_type = 2
  1646.                 )
  1647.             )*/
  1648.         ";
  1649.         if($lessonModule){
  1650.             $sql .= " AND l.lesson_module_id = :lessonModuleId ";
  1651.         }
  1652.         if(
  1653.             $isHistory ||
  1654.             $userPermissionUtil->isLow($permission) || 
  1655.             ($userPermissionUtil->isMiddle($permission) && !$isInTeam)
  1656.         ){
  1657.             $sql .= " AND l.status = :lessonStatus ";
  1658.             $sql .= " AND lm.status = :lessonModuleStatus ";
  1659.         }
  1660.         $stmt $this->em->getConnection()->prepare($sql);
  1661.         //$stmt->bindValue(':deletedEnrollment', EnrollmentEnum::ITEM_NO_DELETED, PDO::PARAM_INT);
  1662.         $stmt->bindValue(':deletedCourse'CourseEnum::ITEM_NO_DELETEDPDO::PARAM_INT);
  1663.         $stmt->bindValue(':deletedModule'LessonModuleEnum::ITEM_NO_DELETEDPDO::PARAM_INT);
  1664.         $stmt->bindValue(':deletedLibrary'LibraryEnum::ITEM_NO_DELETEDPDO::PARAM_INT);
  1665.         $stmt->bindValue(':deleted'LessonEnum::ITEM_NO_DELETEDPDO::PARAM_INT);
  1666.         $stmt->bindValue(':courseId'$course->getId(), PDO::PARAM_INT);
  1667.         //$stmt->bindValue(':yes', LessonEnum::YES, PDO::PARAM_INT);
  1668.         //$stmt->bindValue(':userId', $userId, PDO::PARAM_INT);
  1669.         if($lessonModule){
  1670.             $stmt->bindValue(':lessonModuleId'$lessonModule->getId(), PDO::PARAM_INT);
  1671.         }
  1672.         if(
  1673.             $isHistory ||
  1674.             $userPermissionUtil->isLow($permission) ||
  1675.             ($userPermissionUtil->isMiddle($permission) && !$isInTeam)
  1676.         ){
  1677.             $stmt->bindValue(':lessonStatus'LessonEnum::PUBLISHEDPDO::PARAM_INT);
  1678.             $stmt->bindValue(':lessonModuleStatus'LessonModuleEnum::PUBLISHEDPDO::PARAM_INT);
  1679.         }
  1680.         
  1681.         $stmt->execute();
  1682.         $result $stmt->fetchAll(PDO::FETCH_CLASS);
  1683.         return $result[0]->total;
  1684.     }
  1685.     public function getCourseLessons(
  1686.         Course $course
  1687.         ?LessonModule $lessonModule null
  1688.         ?array $lessonIds null
  1689.         ?string $searchText null
  1690.     )
  1691.     {
  1692.         $userPermissionUtil $this->generalService->getUtil('UserPermissionUtil');
  1693.         $permission $userPermissionUtil->getPermission("course""see");
  1694.         $user $userPermissionUtil->getUser();
  1695.         $isInTeam $this->em->getRepository(CourseTeam::class)->userExistInCourseTeam(
  1696.             $course
  1697.             $user
  1698.         );
  1699.         
  1700.         $query $this->createQueryBuilder('l');
  1701.         $query->innerJoin(
  1702.             'EADPlataforma:LessonModule'
  1703.             'lm'
  1704.             'WITH'
  1705.             'lm.id = l.lessonModule AND lm.deleted = 0'
  1706.         );
  1707.         $query->innerJoin(
  1708.             'EADPlataforma:Course'
  1709.             'c'
  1710.             'WITH'
  1711.             'c.id = l.course AND c.deleted = 0'
  1712.         );
  1713.         $query->leftJoin(
  1714.             'EADPlataforma:Library'
  1715.             'lb'
  1716.             'WITH'
  1717.             'lb.id = l.library AND lb.deleted = 0'
  1718.         );
  1719.         $query->andWhere('l.deleted = 0');
  1720.         $query->andWhere('l.course = :courseId');
  1721.         $query->setParameter('courseId'$course->getId());
  1722.         if(!empty($searchText)){
  1723.             $query->andWhere('(l.title LIKE :searchText OR lm.title LIKE :searchText)');
  1724.             $query->setParameter('searchText'"%{$searchText}%");
  1725.         }
  1726.         if($lessonModule){
  1727.             $query->andWhere('l.lessonModule = :lessonModuleId');
  1728.             $query->setParameter('lessonModuleId'$lessonModule->getId());
  1729.         }
  1730.         if(is_array($lessonIds) && !empty($lessonIds)){
  1731.             $query->andWhere($query->expr()->in('l.id'$lessonIds));
  1732.         }
  1733.         if(
  1734.             $userPermissionUtil->isLow($permission) || 
  1735.             ($userPermissionUtil->isMiddle($permission) && !$isInTeam)
  1736.         ){
  1737.             $query->andWhere('l.status = :lessonStatus');
  1738.             $query->andWhere('lm.status = :lessonModuleStatus');
  1739.             $query->setParameter('lessonStatus'LessonEnum::PUBLISHED);
  1740.             $query->setParameter('lessonModuleStatus'LessonModuleEnum::PUBLISHED);
  1741.         }
  1742.         $query->addOrderBy('l.order''ASC');
  1743.         $query->addOrderBy('l.lessonModule''ASC');
  1744.         $data $query->getQuery()->execute();
  1745.         return $data;
  1746.     }
  1747.     public function getFirstLesson(Course $course, ?LessonModule $lessonModule null)
  1748.     {
  1749.         $userPermissionUtil $this->generalService->getUtil('UserPermissionUtil');
  1750.         $permission $userPermissionUtil->getPermission("course""see");
  1751.         $user $userPermissionUtil->getUser();
  1752.         $courseTeamRepository $this->em->getRepository(CourseTeam::class);
  1753.         $isInTeam $courseTeamRepository->userExistInCourseTeam($course$user);
  1754.         
  1755.         $query $this->createQueryBuilder('l');
  1756.         $query->innerJoin(
  1757.             'EADPlataforma:LessonModule'
  1758.             'lm'
  1759.             'WITH'
  1760.             'lm.id = l.lessonModule AND lm.deleted = 0'
  1761.         );
  1762.         $query->innerJoin(
  1763.             'EADPlataforma:Course'
  1764.             'c'
  1765.             'WITH'
  1766.             'c.id = l.course AND c.deleted = 0'
  1767.         );
  1768.         $query->andWhere('l.deleted = 0');
  1769.         $query->andWhere('l.course = :courseId');
  1770.         $query->setParameter('courseId'$course->getId());
  1771.         if($lessonModule){
  1772.             $query->andWhere('l.lessonModule = :lessonModuleId');
  1773.             $query->setParameter('lessonModuleId'$lessonModule->getId());
  1774.         }
  1775.         if(
  1776.             $userPermissionUtil->isLow($permission) || 
  1777.             ($userPermissionUtil->isMiddle($permission) && !$isInTeam)
  1778.         ){
  1779.             $query->andWhere('l.status = :lessonStatus');
  1780.             $query->andWhere('lm.status = :lessonModuleStatus');
  1781.             $query->setParameter('lessonStatus'LessonEnum::PUBLISHED);
  1782.             $query->setParameter('lessonModuleStatus'LessonModuleEnum::PUBLISHED);
  1783.         }
  1784.         $query->addOrderBy('lm.order''ASC');
  1785.         $query->addOrderBy('l.order''ASC');
  1786.         $query->setMaxResults(1);
  1787.         return $query->getQuery()->getOneOrNullResult();
  1788.     }
  1789.     public function getLastLesson(Course $course, ?LessonModule $lessonModule null)
  1790.     {
  1791.         $userPermissionUtil $this->generalService->getUtil('UserPermissionUtil');
  1792.         $permission $userPermissionUtil->getPermission("course""see");
  1793.         $user $userPermissionUtil->getUser();
  1794.         $isInTeam $this->em->getRepository(CourseTeam::class)->userExistInCourseTeam(
  1795.             $course
  1796.             $user
  1797.         );
  1798.         
  1799.         $query $this->createQueryBuilder('l');
  1800.         $query->innerJoin(
  1801.             'EADPlataforma:LessonModule'
  1802.             'lm'
  1803.             'WITH'
  1804.             'lm.id = l.lessonModule AND lm.deleted = 0'
  1805.         );
  1806.         $query->innerJoin(
  1807.             'EADPlataforma:Course'
  1808.             'c'
  1809.             'WITH'
  1810.             'c.id = l.course AND c.deleted = 0'
  1811.         );
  1812.         $query->andWhere('l.deleted = 0');
  1813.         $query->andWhere('l.course = :courseId');
  1814.         $query->setParameter('courseId'$course->getId());
  1815.         if($lessonModule){
  1816.             $query->andWhere('l.lessonModule = :lessonModuleId');
  1817.             $query->setParameter('lessonModuleId'$lessonModule->getId());
  1818.         }
  1819.         if(
  1820.             $userPermissionUtil->isLow($permission) || 
  1821.             ($userPermissionUtil->isMiddle($permission) && !$isInTeam)
  1822.         ){
  1823.             $query->andWhere('l.status = :lessonStatus');
  1824.             $query->andWhere('lm.status = :lessonModuleStatus');
  1825.             $query->setParameter('lessonStatus'LessonEnum::PUBLISHED);
  1826.             $query->setParameter('lessonModuleStatus'LessonModuleEnum::PUBLISHED);
  1827.         }
  1828.         $query->addOrderBy('lm.order''DESC');
  1829.         $query->addOrderBy('l.order''DESC');
  1830.         $query->setMaxResults(1);
  1831.         return $query->getQuery()->getOneOrNullResult();
  1832.     }
  1833.     public function getLessonByLessonModule(LessonModule $lessonModule)
  1834.     {   
  1835.         $query $this->createQueryBuilder('l');
  1836.         $query->leftJoin(
  1837.             'EADPlataforma:Library'
  1838.             'li'
  1839.             'WITH'
  1840.             'li.id = l.library AND li.deleted = 0'
  1841.         );
  1842.         $query->andWhere('l.deleted = 0');
  1843.         $query->andWhere('l.status = :status');
  1844.         $query->andWhere('l.course = :courseId');
  1845.         $query->andWhere('l.lessonModule = :lessonModule');
  1846.         $query->setParameter('courseId'$lessonModule->getCourse()->getId());
  1847.         $query->setParameter('lessonModule'$lessonModule->getId());
  1848.         $query->setParameter('status'LessonEnum::PUBLISHED);
  1849.         $query->addOrderBy('l.order''ASC');
  1850.         return $query->getQuery()->execute();
  1851.     }
  1852.     public function getNextLessonByLessonModule(
  1853.         int $order
  1854.         LessonModule $lessonModule
  1855.         int $limit 3
  1856.     )
  1857.     {   
  1858.         $userPermissionUtil $this->generalService->getUtil('UserPermissionUtil');
  1859.         $permission $userPermissionUtil->getPermission("course""see");
  1860.         $course $lessonModule->getCourse();
  1861.         $user $userPermissionUtil->getUser();
  1862.         $isInTeam $this->em->getRepository(CourseTeam::class)->userExistInCourseTeam(
  1863.             $course
  1864.             $user
  1865.         );
  1866.         $query $this->createQueryBuilder('l');
  1867.         $query->select('
  1868.             l.id,
  1869.             l.order,
  1870.             l.title,
  1871.             l.status,
  1872.             l.controlRequirement,
  1873.             l.controlReleaseType,
  1874.             l.controlReleaseAfterType,
  1875.             l.controlDateRelease,
  1876.             l.controlReleasePeriod,
  1877.             l.controlClosePeriod,
  1878.             lm.id AS lessonModule,
  1879.             li.type AS libraryType,
  1880.             li.pagesNumber,
  1881.             li.duration
  1882.         ');
  1883.         $query->innerJoin(
  1884.             'EADPlataforma:LessonModule'
  1885.             'lm'
  1886.             'WITH'
  1887.             'lm.id = l.lessonModule AND lm.deleted = 0'
  1888.         );
  1889.         $query->innerJoin(
  1890.             'EADPlataforma:Course'
  1891.             'c'
  1892.             'WITH'
  1893.             'c.id = l.course AND c.deleted = 0'
  1894.         );
  1895.         $query->leftJoin(
  1896.             'EADPlataforma:Library'
  1897.             'li'
  1898.             'WITH'
  1899.             'li.id = l.library AND li.deleted = 0'
  1900.         );
  1901.         if(
  1902.             $userPermissionUtil->isLow($permission) || 
  1903.             ($userPermissionUtil->isMiddle($permission) && !$isInTeam)
  1904.         ){
  1905.             $query->andWhere('l.status = :lessonStatus');
  1906.             $query->andWhere('lm.status = :lessonModuleStatus');
  1907.             $query->setParameter('lessonStatus'LessonEnum::PUBLISHED);
  1908.             $query->setParameter('lessonModuleStatus'LessonModuleEnum::PUBLISHED);
  1909.         }
  1910.         $query->andWhere('l.deleted = 0');
  1911.         $query->andWhere('l.course = :courseId');
  1912.         $query->andWhere('l.lessonModule = :lessonModule');
  1913.         $query->andWhere('l.order > :order');
  1914.         $query->setParameter('courseId'$course->getId());
  1915.         $query->setParameter('lessonModule'$lessonModule->getId());
  1916.         $query->setParameter('order'$order);
  1917.         $query->addOrderBy('l.order''ASC');
  1918.         $query->setMaxResults($limit);
  1919.         return $query->getQuery()->execute();
  1920.     }
  1921.     public function getLessonNotView($userId$courseId$lessonId null)
  1922.     {
  1923.         $and '';
  1924.         if(!empty($lessonId))
  1925.         {
  1926.             $and .= " AND l.id = :lessonId";
  1927.         }
  1928.         if(empty($courseId) || empty($userId))
  1929.         {
  1930.             return [];
  1931.         }
  1932.         $dql "SELECT
  1933.                     l.id,
  1934.                     l.title,
  1935.                     lm.title AS titleModule,
  1936.                     '0' AS totalView,
  1937.                     '' AS lastAccess,
  1938.                     '--' AS timeWatch,
  1939.                     '0' AS completed,
  1940.                     '---' AS dateConclusion
  1941.                 FROM EADPlataforma:Lesson AS l
  1942.                 INNER JOIN EADPlataforma:LessonModule AS lm WITH (lm.id = l.lessonModule AND lm.deleted = 0)
  1943.                 WHERE l.deleted = 0
  1944.                 AND l.course = :courseId
  1945.                 {$and}
  1946.                 AND l.id NOT IN (
  1947.                     SELECT
  1948.                         IDENTITY (ll.lesson)
  1949.                     FROM EADPlataforma:LessonLogOrigin AS ll
  1950.                     WHERE ll.user != 1
  1951.                     AND ll.user = :userId
  1952.                     AND ll.course = :courseId
  1953.                 )
  1954.                 ORDER BY l.order ASC";
  1955.                 
  1956.         $query $this->em->createQuery($dql);
  1957.         $query->setParameter('userId'$userId);
  1958.         $query->setParameter('courseId'$courseId);
  1959.       
  1960.         if(!empty($lessonId))
  1961.         {
  1962.             $query->setParameter('lessonId'$lessonId);
  1963.         }
  1964.         $data $query->execute();
  1965.         
  1966.         $result = [];
  1967.         foreach ($data as $key => $value) {
  1968.             $result[] = [
  1969.                 $value["id"],
  1970.                 $value["title"],
  1971.                 $value["titleModule"],
  1972.                 $value["totalView"],
  1973.                 $value["lastAccess"],
  1974.                 $value["timeWatch"],
  1975.                 $value["completed"],
  1976.                 $value["dateConclusion"]
  1977.             ];
  1978.         }
  1979.         return $result;
  1980.     }
  1981.     public function isFirstCourseLesson(?Lesson $lesson)
  1982.     {
  1983.         if(!$lesson){
  1984.             return false;
  1985.         }
  1986.         $firstLesson $this->getFirstLesson($lesson->getCourse());
  1987.         if($firstLesson){
  1988.             return $firstLesson->getId() == $lesson->getId();
  1989.         }
  1990.         return true;
  1991.     }
  1992.     public function isFirstModuleLesson(?Lesson $lesson)
  1993.     {
  1994.         if(!$lesson){
  1995.             return false;
  1996.         }
  1997.         $firstLesson $this->getFirstLesson($lesson->getCourse(), $lesson->getLessonModule());
  1998.         if($firstLesson){
  1999.             return $firstLesson->getId() == $lesson->getId();
  2000.         }
  2001.         return true;
  2002.     }
  2003.     public function isLastCourseLesson(?Lesson $lesson)
  2004.     {
  2005.         if(!$lesson){
  2006.             return false;
  2007.         }
  2008.         $firstLesson $this->getLastLesson($lesson->getCourse());
  2009.         if($firstLesson){
  2010.             return $firstLesson->getId() == $lesson->getId();
  2011.         }
  2012.         return true;
  2013.     }
  2014.     public function isLastModuleLesson(?Lesson $lesson)
  2015.     {
  2016.         if(!$lesson){
  2017.             return false;
  2018.         }
  2019.         $firstLesson $this->getLastLesson(
  2020.             $lesson->getCourse(), 
  2021.             $lesson->getLessonModule()
  2022.         );
  2023.         if($firstLesson){
  2024.             return $firstLesson->getId() == $lesson->getId();
  2025.         }
  2026.         return true;
  2027.     }
  2028.     public function isLessonVisibleToStudent(
  2029.         Lesson $lesson
  2030.         ?bool $ignoreControlFunction false,
  2031.         ?bool $isStudent true
  2032.     ): bool
  2033.     {
  2034.         $info $this->checkLessonVisibleToStudent($lesson$ignoreControlFunction$isStudent);
  2035.         return $info->visible;
  2036.     }
  2037.     public function checkLessonVisibleToStudent(
  2038.         Lesson $lesson
  2039.         ?bool $ignoreControlFunction false,
  2040.         ?bool $isStudent true
  2041.     ): object
  2042.     {
  2043.         $userPermissionUtil $this->generalService->getUtil('UserPermissionUtil');
  2044.         $user $userPermissionUtil->getUser();
  2045.         $info = (object)[
  2046.             "visible" => true,
  2047.             "message" => "",
  2048.         ];
  2049.         if(!$user){
  2050.             $info->visible false;
  2051.             return $info;
  2052.         }
  2053.         $course $lesson->getCourse();
  2054.         $lessonModule $lesson->getLessonModule();
  2055.         if($lesson->isDeleted() || $lessonModule->isDeleted() || $course->isDeleted()){
  2056.             $info->visible false;
  2057.             return $info;
  2058.         }
  2059.         if(!$isStudent){
  2060.             $info->visible true;
  2061.             return $info;
  2062.         }
  2063.         $enrollmentRepository $this->em->getRepository(Enrollment::class);
  2064.         $isEnroll $enrollmentRepository->isValidEnrollmentByUser(
  2065.             $user->getId(), 
  2066.             $course->getId()
  2067.         );
  2068.         $hasLessonControl $this->configuration->checkModuleIsAbleOnPlan(
  2069.             'lessonControlFunction'
  2070.         );
  2071.             
  2072.         if($lesson->getStatus() == LessonEnum::DRAFT){
  2073.             $info->visible false;
  2074.             return $info;
  2075.         }
  2076.         if($lessonModule->getStatus() == LessonModuleEnum::DRAFT){
  2077.             $info->visible false;
  2078.             return $info;
  2079.         }
  2080.         if($course->getStatus() == CourseEnum::DRAFT){
  2081.             $info->visible false;
  2082.             return $info;
  2083.         }
  2084.         if(!$isEnroll){
  2085.             $info->visible false;
  2086.             return $info;
  2087.         }
  2088.         
  2089.         if(!$ignoreControlFunction){
  2090.             if($lesson->getControlReleaseType() == LessonEnum::FIXED_DATE){
  2091.                 $todaySeconds strtotime(date('Y-m-d H:i:s'));
  2092.                 if($hasLessonControl){
  2093.                     $controlClosePeriod $lesson->getControlClosePeriod();
  2094.                     if(!empty($controlClosePeriod)){
  2095.                         $timeLog strtotime(
  2096.                             "{$lesson->getControlDateRelease()} + {$controlClosePeriod} day"
  2097.                         );
  2098.                         if($todaySeconds $timeLog){
  2099.                             $info->visible false;
  2100.                             return $info;
  2101.                         }
  2102.                     }
  2103.                 }
  2104.             }else{
  2105.                 $lessonLogRepository $this->emEadmin->getRepository(LessonLog::class);
  2106.                 $logId "{$course->getId()}#{$user->getId()}#{$lesson->getId()}";
  2107.                 $lessonLog $lessonLogRepository->find($logId);
  2108.                 //check lesson to user is closed
  2109.                 if($lessonLog){
  2110.                     $todaySeconds strtotime(date('Y-m-d H:i:s'));
  2111.                     if($hasLessonControl){
  2112.                         $controlClosePeriod $lesson->getControlClosePeriod();
  2113.                         if(
  2114.                             !empty($controlClosePeriod) && 
  2115.                             $lessonLog->getComplete() == LessonEnum::YES
  2116.                         ){
  2117.                             $timeLog strtotime(
  2118.                                 "{$lessonLog->getDateConclusion()} + {$controlClosePeriod} day"
  2119.                             );
  2120.                             if($todaySeconds $timeLog){
  2121.                                 $info->visible false;
  2122.                                 return $info;
  2123.                             }
  2124.                         }
  2125.                     }
  2126.                 }
  2127.             } 
  2128.         }
  2129.         return $info;
  2130.     }
  2131.     public function getLessonNumberByProduct(Product $product)
  2132.     {
  2133.         $query $this->createQueryBuilder('l');
  2134.         $query->select("COUNT(l.id) AS total");
  2135.         $query->innerJoin('EADPlataforma:Course''c''WITH''l.course = c.id');
  2136.         $query->innerJoin('EADPlataforma:Product''p''WITH''l.course MEMBER OF p.course');
  2137.         $query->innerJoin('EADPlataforma:LessonModule''lm''WITH''l.lessonModule = lm.id');
  2138.         $query->andWhere('l.deleted = :deletedLesson');
  2139.         $query->andWhere('c.deleted = :deletedCourse');
  2140.         $query->andWhere('lm.deleted = :deletedLessonModule');
  2141.         $query->andWhere('l.status = :statusLesson');
  2142.         $query->andWhere('c.status = :statusCourse');
  2143.         $query->andWhere('lm.status = :statusLessonModule');
  2144.         $query->andWhere('p.id = :productId');
  2145.         $query->setParameter('deletedLesson'LessonEnum::ITEM_NO_DELETED);
  2146.         $query->setParameter('deletedCourse'CourseEnum::ITEM_NO_DELETED);
  2147.         $query->setParameter('deletedLessonModule'LessonModuleEnum::ITEM_NO_DELETED);
  2148.         $query->setParameter('statusLesson'LessonEnum::PUBLISHED);
  2149.         $query->setParameter('statusCourse'CourseEnum::PUBLISHED);
  2150.         $query->setParameter('statusLessonModule'LessonModuleEnum::PUBLISHED);
  2151.         $query->setParameter('productId'$product->getId());
  2152.         $result = (object)$query->getQuery()->getOneOrNullResult();
  2153.         return $result->total;
  2154.     }
  2155.     public function getLessonForExportPresence(Course $course$lessonModuleId null){
  2156.         $query $this->createQueryBuilder('l');
  2157.         $query->select('
  2158.             l.id,
  2159.             lm.id AS lessonModuleId,
  2160.             l.order,
  2161.             lm.title,
  2162.             l.title AS lessonTitle
  2163.         ');
  2164.         $query->innerJoin(
  2165.             'EADPlataforma:LessonModule'
  2166.             'lm'
  2167.             'WITH'
  2168.             'lm.id = l.lessonModule AND lm.deleted = 0'
  2169.         );
  2170.         if($lessonModuleId){
  2171.             $query->andWhere('lm.id = :lessonModuleId');
  2172.             $query->setParameter('lessonModuleId'$lessonModuleId);
  2173.         }
  2174.         
  2175.         $query->andWhere('l.status = :lessonStatus');
  2176.         $query->andWhere('lm.status = :lessonModuleStatus');
  2177.         $query->setParameter('lessonStatus'LessonEnum::PUBLISHED);
  2178.         $query->setParameter('lessonModuleStatus'LessonModuleEnum::PUBLISHED);
  2179.         
  2180.         $query->andWhere('l.deleted = 0');
  2181.         $query->andWhere('l.course = :courseId');
  2182.         $query->setParameter('courseId'$course->getId());
  2183.         $query->addOrderBy('lm.order''ASC');
  2184.         $query->addOrderBy('lm.id''ASC');
  2185.         $query->addOrderBy('l.order''ASC');
  2186.         $query->addOrderBy('l.id''ASC');
  2187.         return $query->getQuery()->execute();
  2188.     }
  2189.     public function restore(Lesson $lesson$typeItem$key)
  2190.     {
  2191.         $lessonId $lesson->getId();
  2192.         $dateNow date('Y-m-d H:i:s');
  2193.         $lastOrder $this->count(
  2194.             [ 
  2195.                 "deleted" => LessonEnum::ITEM_NO_DELETED
  2196.                 "lessonModule" => $lesson->getLessonModule()->getId() 
  2197.             ]
  2198.         ) + ($key 1);
  2199.         $lesson->setOrder($lastOrder);
  2200.         $courseRepository =  $this->em->getRepository(Course::class);
  2201.         $lesson->setUserDelete($this->getUser());
  2202.         $lesson->setDateDelete($dateNow);
  2203.         $lesson->restore();
  2204.         $deleted LessonEnum::ITEM_NO_DELETED;
  2205.         $typeDelete LessonEnum::CASCADE;
  2206.         $this->em->getRepository(Exam::class)->restoreByLesson(
  2207.             $lessonId
  2208.             $deleted
  2209.             $typeDelete
  2210.             $this->getUser(), 
  2211.             $dateNow
  2212.         );
  2213.         $this->em->getRepository(ExamUser::class)->restoreByLesson(
  2214.             $lessonId
  2215.             $deleted
  2216.             $typeDelete
  2217.             $this->getUser(), 
  2218.             $dateNow
  2219.         );
  2220.         $this->em->getRepository(LessonSupport::class)->restoreByLesson(
  2221.             $lessonId
  2222.             $deleted
  2223.             $typeDelete
  2224.             $this->getUser(), 
  2225.             $dateNow
  2226.         );
  2227.         $this->em->getRepository(LessonXLibrary::class)->restoreByLesson(
  2228.             $lessonId
  2229.             $deleted
  2230.             $typeDelete
  2231.             $this->getUser(), 
  2232.             $dateNow
  2233.         );
  2234.         $this->em->getRepository(Trash::class)->deleteTrash($lessonId$typeItem);
  2235.     }
  2236.     public function delete(Lesson $lesson$typeItem$permission$isTrash)
  2237.     {
  2238.         $lessonId $lesson->getId();
  2239.         $dateNow date('Y-m-d H:i:s');
  2240.         $typeDelete LessonEnum::CASCADE;
  2241.         $notificationService $this->generalService->getService('NotificationService');
  2242.         $userPermissionUtil $this->generalService->getUtil('UserPermissionUtil');
  2243.         $lessonXLibrarys $this->em->getRepository(LessonXLibrary::class)->findBy([
  2244.             "lesson" => $lesson->getId(),
  2245.         ]);
  2246.         $libraryRepository $this->em->getRepository(Library::class);
  2247.         $libraryModule $this->configuration->checkModuleIsAbleOnPlan('libraryModule');
  2248.         if($isTrash == LessonEnum::YES){
  2249.             $permission $userPermissionUtil->getPermission("trash""delete");
  2250.         }
  2251.         $isLessonTeacher $this->isLessonTeacher($lesson$this->getUser());
  2252.         if($isLessonTeacher || $userPermissionUtil->isHigh($permission)){
  2253.             $lesson->setUserDelete($this->getUser());
  2254.             $lesson->setDateDelete($dateNow);
  2255.             $lesson->individual();
  2256.             
  2257.             if($lesson->isOnTrash() || $lesson->isDeleted()){
  2258.                 
  2259.                 $lesson->delete();
  2260.                 $deleted LessonEnum::ITEM_DELETED;
  2261.                 $this->em->getRepository(Trash::class)->deleteTrash($lessonId$typeItem);
  2262.                 $this->em->flush();
  2263.                 foreach ($lessonXLibrarys as $key => $lessonXLibrary) {
  2264.                     $library $lessonXLibrary->getLibrary();
  2265.                     if(!$libraryModule && $library){
  2266.                         $library->cascade();
  2267.                         $libraryRepository->deleteContent($library);
  2268.                     }
  2269.                     $lessonXLibrary->delete();
  2270.                 }
  2271.                 $library $lesson->getLibrary();
  2272.                 if(!$libraryModule && $library){
  2273.                     $library->cascade();
  2274.                     $libraryRepository->deleteContent($library);
  2275.                 }
  2276.             }
  2277.             if(!$lesson->isOnTrash() && !$lesson->isDeleted()){
  2278.                 $order $lesson->getOrder();
  2279.                 $this->refreshOrder(
  2280.                     $lesson->getCourse()->getId(), 
  2281.                     $lesson->getLessonModule()->getId(),
  2282.                     $order,
  2283.                     null,
  2284.                     true
  2285.                 );
  2286.                 $notificationService->delete(
  2287.                     NotificationEnum::ORIGIN_LESSON_SUPPORT_LIKE
  2288.                     $lessonId
  2289.                 );
  2290.                 $notificationService->delete(
  2291.                     NotificationEnum::ORIGIN_LESSON_SUPPORT_ANSWER_LIKE
  2292.                     $lessonId
  2293.                 );
  2294.                 $notificationService->delete(
  2295.                     NotificationEnum::ORIGIN_LESSON_SUPPORT_NEW
  2296.                     $lessonId
  2297.                 );
  2298.                 $notificationService->delete(
  2299.                     NotificationEnum::ORIGIN_LESSON_SUPPORT_ANSWER_NEW
  2300.                     $lessonId
  2301.                 );
  2302.                 $notificationService->delete(
  2303.                     NotificationEnum::ORIGIN_LESSON_NEW
  2304.                     $lessonId
  2305.                 );
  2306.                 $notificationService->delete(
  2307.                     NotificationEnum::ORIGIN_LESSON_CHANGE
  2308.                     $lessonId
  2309.                 );
  2310.                 $lesson->trash();
  2311.                 $deleted LessonEnum::ITEM_ON_TRASH;
  2312.                 $this->em->getRepository(Trash::class)->insertTrash(
  2313.                     $lesson
  2314.                     $typeItem
  2315.                     "Aula: {$lesson->getTitle()} | Prof. {$lesson->getUser()->getName()}"
  2316.                 );
  2317.             }
  2318.         }
  2319.         
  2320.         $this->em->getRepository(Exam::class)->deleteByLesson(
  2321.             $lessonId
  2322.             $deleted
  2323.             $typeDelete
  2324.             $this->getUser(), 
  2325.             $dateNow
  2326.         );
  2327.         $this->em->getRepository(ExamUser::class)->deleteByLesson(
  2328.             $lessonId
  2329.             $deleted
  2330.             $typeDelete
  2331.             $this->getUser(), 
  2332.             $dateNow
  2333.         );
  2334.         $this->em->getRepository(LessonSupport::class)->deleteByLesson(
  2335.             $lessonId
  2336.             $deleted
  2337.             $typeDelete
  2338.             $this->getUser(), 
  2339.             $dateNow
  2340.         );
  2341.         $this->em->getRepository(LessonXLibrary::class)->deleteByLesson(
  2342.             $lessonId
  2343.             $deleted
  2344.             $typeDelete
  2345.             $this->getUser(), 
  2346.             $dateNow
  2347.         );
  2348.     }
  2349.     public function restoreByCourse($courseId$deleted$typeDelete){
  2350.         $typeItem TrashEnum::LESSON;
  2351.         $lessons $this->em->getRepository(Lesson::class)->findBy([
  2352.             "course" => $courseId,
  2353.             "deleted" => ($deleted),
  2354.             "typeDelete" => $typeDelete
  2355.         ]);
  2356.         foreach ($lessons as $key => $lesson) {
  2357.             $this->restore($lesson$typeItem$key);
  2358.         }
  2359.     }
  2360.     public function deleteByCourse($courseId$deleted){
  2361.         $typeItem TrashEnum::LESSON;
  2362.         $isTrash LessonEnum::NO;
  2363.         if($deleted == LessonEnum::ITEM_ON_TRASH){
  2364.             $isTrash LessonEnum::YES;
  2365.         }
  2366.         $userPermissionUtil $this->generalService->getUtil('UserPermissionUtil');
  2367.         $permission $userPermissionUtil->getPermission(
  2368.             "course"
  2369.             "lesson"
  2370.             "delete"
  2371.         );
  2372.         $lessons $this->em->getRepository(Lesson::class)->findBy([
  2373.             "course" => $courseId,
  2374.             "deleted" => ($deleted 1)
  2375.         ]);
  2376.         foreach ($lessons as $key => $lesson) {
  2377.             $this->delete($lesson$typeItem$permission$isTrash);
  2378.             $lesson->cascade();
  2379.         }
  2380.     }
  2381.     public function restoreByLessonModule($lessonModuleId$deleted$typeDelete){
  2382.         $typeItem TrashEnum::LESSON;
  2383.         $lessons $this->em->getRepository(Lesson::class)->findBy([
  2384.             "lessonModule" => $lessonModuleId,
  2385.             "deleted" => ($deleted),
  2386.             "typeDelete" => $typeDelete
  2387.         ]);
  2388.         foreach ($lessons as $key => $lesson) {
  2389.             $this->restore($lesson$typeItem$key);
  2390.         }
  2391.     }
  2392.     public function deleteByLessonModule($lessonModuleId$deleted){
  2393.         $typeItem TrashEnum::LESSON;
  2394.         $isTrash LessonEnum::NO;
  2395.         if($deleted == LessonEnum::ITEM_ON_TRASH){
  2396.             $isTrash LessonEnum::YES;
  2397.         }
  2398.         $userPermissionUtil $this->generalService->getUtil('UserPermissionUtil');
  2399.         $permission $userPermissionUtil->getPermission(
  2400.             "course"
  2401.             "lesson"
  2402.             "delete"
  2403.         );
  2404.         $lessons $this->em->getRepository(Lesson::class)->findBy([
  2405.             "lessonModule" => $lessonModuleId,
  2406.             "deleted" => ($deleted 1)
  2407.         ]);
  2408.         foreach ($lessons as $key => $lesson) {
  2409.             $this->delete($lesson$typeItem$permission$isTrash);
  2410.             $lesson->cascade();
  2411.         }
  2412.     }
  2413.     public function deleteTrashCron()
  2414.     {
  2415.         $sql "UPDATE EADPlataforma:Lesson AS l SET l.deleted = 2 
  2416.                 WHERE l.dateDelete <= :date AND l.deleted = 1 ";
  2417.         $query $this->em->createQuery($sql);
  2418.         $query->setParameter('date'date('Y-m-d H:i:s'strtotime('-90 days')));
  2419.         $query->execute();
  2420.     }
  2421. }