src/Repository/LessonRepository.php line 53

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.         $this->em->beginTransaction();
  1334.         
  1335.         try {
  1336.             $sql "SELECT l.id 
  1337.                     FROM EADPlataforma:Lesson AS l
  1338.                     WHERE l.course = :courseId 
  1339.                     AND l.lessonModule = :lessonModuleId
  1340.                     AND l.deleted = :deleted
  1341.                     ORDER BY l.order ASC";
  1342.             $query $this->em->createQuery($sql);
  1343.             $query->setParameter('courseId'$courseId);
  1344.             $query->setParameter('lessonModuleId'$lessonModuleId);
  1345.             $query->setParameter('deleted'$isDelete);
  1346.             $lessons $query->getResult();
  1347.             $order 1;
  1348.             foreach ($lessons as $lesson) {
  1349.                 $updateSql "UPDATE EADPlataforma:Lesson AS l
  1350.                             SET l.order = :order
  1351.                             WHERE l.id = :id";
  1352.                 $updateQuery $this->em->createQuery($updateSql);
  1353.                 $updateQuery->setParameter('order'$order);
  1354.                 $updateQuery->setParameter('id'$lesson['id']);
  1355.                 $updateQuery->execute();
  1356.                 $order++;
  1357.             }
  1358.             $maxOrder count($lessons);
  1359.             if ($newOrder 1) {
  1360.                 $newOrder 1;
  1361.             } elseif ($newOrder $maxOrder) {
  1362.                 $newOrder $maxOrder;
  1363.             }
  1364.             if ($oldOrder != $newOrder) {
  1365.                 $sql = ($oldOrder $newOrder
  1366.                     ? "UPDATE EADPlataforma:Lesson AS l
  1367.                     SET l.order = l.order - 1
  1368.                     WHERE l.order > :oldOrder
  1369.                     AND l.order <= :newOrder
  1370.                     AND l.course = :courseId
  1371.                     AND l.lessonModule = :lessonModuleId
  1372.                     AND l.deleted = :deleted"
  1373.                     "UPDATE EADPlataforma:Lesson AS l
  1374.                     SET l.order = l.order + 1
  1375.                     WHERE l.order < :oldOrder
  1376.                     AND l.order >= :newOrder
  1377.                     AND l.course = :courseId
  1378.                     AND l.lessonModule = :lessonModuleId
  1379.                     AND l.deleted = :deleted";
  1380.                 $shiftQuery $this->em->createQuery($sql);
  1381.                 $shiftQuery->setParameter('oldOrder'$oldOrder);
  1382.                 $shiftQuery->setParameter('newOrder'$newOrder);
  1383.                 $shiftQuery->setParameter('courseId'$courseId);
  1384.                 $shiftQuery->setParameter('lessonModuleId'$lessonModuleId);
  1385.                 $shiftQuery->setParameter('deleted'$isDelete);
  1386.                 $shiftQuery->execute();
  1387.             }
  1388.             $finalSql "UPDATE EADPlataforma:Lesson AS l
  1389.                         SET l.order = :newOrder
  1390.                         WHERE l.id = :lessonId";
  1391.             $finalUpdateQuery $this->em->createQuery($finalSql);
  1392.             $finalUpdateQuery->setParameter('newOrder'$newOrder);
  1393.             $finalUpdateQuery->setParameter('lessonId'$oldOrder);
  1394.             $finalUpdateQuery->execute();
  1395.             $this->em->commit();
  1396.         } catch (\Exception $e) {
  1397.             $this->em->rollback();
  1398.             throw $e;
  1399.         }
  1400.     }
  1401.     public function copyLesson(
  1402.         LessonModule $newModule
  1403.         Lesson $lesson
  1404.         $isPartial false,
  1405.         $isModulePartial false
  1406.         $copyType null
  1407.     )
  1408.     {
  1409.         $course $newModule->getCourse();
  1410.         $courseId $course->getId();
  1411.     
  1412.         $newLesson = clone $lesson;
  1413.         $newLesson->setLessonModule($newModule);
  1414.         $newLesson->setCourse($course);
  1415.         if($copyType == LessonEnum::COPY_PUBLISHED){
  1416.             $newLesson->setStatus(LessonEnum::PUBLISHED);
  1417.         }else if($copyType == LessonEnum::COPY_DRAFT){
  1418.             $newLesson->setStatus(LessonEnum::DRAFT);
  1419.         }
  1420.         
  1421.         if($isPartial){
  1422.             $lastOrder $this->count([ 
  1423.                 "course" => $courseId,
  1424.                 "lessonModule" => $newModule->getId(),
  1425.                 "deleted" => LessonEnum::ITEM_NO_DELETED 
  1426.             ]);
  1427.             $newLesson->setOrder($lastOrder 1);
  1428.         }
  1429.         $this->em->persist($newLesson);
  1430.         $this->em->flush();
  1431.         if($isPartial || $isModulePartial){
  1432.             $userLessonId $newLesson->getUser()->getId();
  1433.             $courseTeam $this->em->getRepository(CourseTeam::class)->findOneBy([
  1434.                 "course" => $courseId
  1435.                 "user" => $userLessonId,
  1436.                 "deleted" => LessonEnum::ITEM_NO_DELETED
  1437.             ]);
  1438.             
  1439.             if(!$courseTeam){
  1440.                 $newCourseTeam = new CourseTeam();
  1441.                 $newCourseTeam->setCourse($course);
  1442.                 $newCourseTeam->setUser($newLesson->getUser());
  1443.                 $this->em->persist($newCourseTeam);
  1444.                 $this->em->flush();
  1445.             }
  1446.         }
  1447.         
  1448.         $files $this->em->getRepository(LessonXLibrary::class)->findBy([
  1449.             "lesson"=> $lesson->getId(),
  1450.             "deleted"=> LessonXLibraryEnum::ITEM_NO_DELETED
  1451.         ]);
  1452.         
  1453.         if($files){
  1454.             foreach ($files as $key => $file) {
  1455.                 $newFile = clone $file;
  1456.                 $newFile->setLesson($newLesson);
  1457.                 
  1458.                 $this->em->persist($newFile);
  1459.                 $this->em->flush();
  1460.             }
  1461.         }
  1462.         
  1463.         $this->em->getRepository(Exam::class)->copyExamByLesson($lesson$newLesson);
  1464.         $this->em->getRepository(Exam::class)->copyQuizByLesson($lesson$newLesson);
  1465.     }
  1466.     public function notifyStudentsLesson(Lesson $lesson$type)
  1467.     {   
  1468.         if(!$this->configuration->checkModuleIsAbleOnPlan('notificationFunction')){
  1469.             return;
  1470.         }
  1471.         $today date('Y-m-d');
  1472.         $dateLastNotify $lesson->getDateLastNotify('Y-m-d');
  1473.         if($today != $dateLastNotify && $lesson->getStatus() == LessonEnum::PUBLISHED){
  1474.             $course $lesson->getCourse();
  1475.             $lessonModule $lesson->getLessonModule();
  1476.             if(
  1477.                 $course->getStatus() == CourseEnum::PUBLISHED && 
  1478.                 $lessonModule->getStatus() == LessonModuleEnum::PUBLISHED
  1479.             ){
  1480.                 $taskQueueService $this->generalService->getService('TaskQueueService');
  1481.                 $enrollmentRepository $this->em->getRepository(Enrollment::class);
  1482.                 
  1483.                 $enrollments $enrollmentRepository->getEnrollmentsByCourse(
  1484.                     $course->getId(), 
  1485.                     EnrollmentEnum::STATUS_ACTIVE
  1486.                 );
  1487.                 foreach ($enrollments as $key => $enrollment) {
  1488.                     if($enrollment->getUser()->getAllowNotifyNewLesson() == LessonEnum::YES){
  1489.                         $taskQueueService->insert($type$enrollment->getUser(), $lesson->getId());
  1490.                     }
  1491.                 }
  1492.                 $lesson->setDateLastNotify(date('Y-m-d H:i:s'));
  1493.                 $this->em->flush();
  1494.             }
  1495.         }
  1496.     }
  1497.     public function lessonUpdateOrder($modules)
  1498.     {
  1499.         $dqlModule "UPDATE EADPlataforma:LessonModule AS lm SET lm.order = CASE";
  1500.         $idModules "";
  1501.         $dqlExam "";
  1502.         $newModuleId "";
  1503.         $whereExam "";
  1504.         $caseLesson1 " l.order = CASE ";
  1505.         $caseLesson2 " l.lessonModule = CASE ";
  1506.         $caseLesson3 " e.lessonModule = CASE ";
  1507.         $idsLesson "";
  1508.         foreach ($modules as $key => $value) {
  1509.             
  1510.             $orderNew $key 1;
  1511.             $dqlModule .= " WHEN lm.id = {$value->moduleId} THEN {$orderNew}";
  1512.             if(empty($idModules)){
  1513.                 $idModules .= "{$value->moduleId}";
  1514.             }else{
  1515.                 $idModules .= ", {$value->moduleId}";
  1516.             }
  1517.             foreach ($value->lessons as $key_lesson => $valLesson) {
  1518.                 if(!empty($valLesson->lessonId)){
  1519.                     $ordemNewLesson $key_lesson 1;
  1520.                 
  1521.                     $caseLesson1 .= " WHEN l.id = {$valLesson->lessonId} THEN {$ordemNewLesson}";
  1522.                     $caseLesson2 .= " WHEN l.id = {$valLesson->lessonId} THEN {$value->moduleId}";
  1523.                     $caseLesson3 .= " WHEN e.lesson = {$valLesson->lessonId} THEN {$value->moduleId}";
  1524.                 
  1525.                     if(empty($idsLesson)){
  1526.                         $idsLesson .= {$valLesson->lessonId} ";
  1527.                     }else{
  1528.                         $idsLesson .= ", {$valLesson->lessonId} ";
  1529.                     }
  1530.                 }
  1531.             }
  1532.         }
  1533.         
  1534.         $dqlModule .= " ELSE lm.order END WHERE lm.id IN ({$idModules}) ";
  1535.         $dqlLesson "UPDATE EADPlataforma:Lesson AS l 
  1536.                       SET {$caseLesson1} ELSE l.order END, 
  1537.                           {$caseLesson2} ELSE IDENTITY(l.lessonModule) END 
  1538.                      WHERE l.id IN ({$idsLesson}) ";
  1539.         $dqlExam "UPDATE EADPlataforma:Exam AS e
  1540.                     SET {$caseLesson3} ELSE IDENTITY(e.lessonModule) END 
  1541.                     WHERE e.lesson IN ({$idsLesson}) ";
  1542.         $query $this->em->createQuery($dqlModule);
  1543.         $queryLesson $this->em->createQuery($dqlLesson);
  1544.         $queryExam $this->em->createQuery($dqlExam);
  1545.         $query->execute();
  1546.         $queryLesson->execute();
  1547.         $queryExam->execute();
  1548.     }
  1549.     public function getLessonPublicNumber(
  1550.         Course $course
  1551.         ?LessonModule $lessonModule null,
  1552.         ?bool $required false
  1553.         ?bool $filterModule false
  1554.     )
  1555.     {
  1556.         $query $this->createQueryBuilder('l');
  1557.         $query->select('COUNT(l.id) AS total');
  1558.         $query->innerJoin(
  1559.             'EADPlataforma:LessonModule'
  1560.             'lm'
  1561.             'WITH'
  1562.             'lm.id = l.lessonModule AND lm.deleted = :deleted'
  1563.         );
  1564.         $query->innerJoin(
  1565.             'EADPlataforma:Course'
  1566.             'c'
  1567.             'WITH'
  1568.             'c.id = l.course AND c.deleted = :deleted'
  1569.         );
  1570.         if($lessonModule){
  1571.             $query->andWhere('lm.order <= :lessonModuleOrder');
  1572.             $query->setParameter('lessonModuleOrder'$lessonModule->getOrder());
  1573.             if($filterModule){
  1574.                 $query->andWhere('lm.id = :lessonModule');
  1575.                 $query->setParameter('lessonModule'$lessonModule->getId());
  1576.             }
  1577.         }
  1578.         if($required){
  1579.             $query->andWhere('l.controlRequirement = :required');
  1580.             $query->setParameter('required'LessonEnum::YES);
  1581.         }
  1582.         $query->andWhere('l.deleted = :deleted');
  1583.         $query->andWhere('l.course = :courseId');
  1584.         $query->andWhere('l.status = :lessonStatus');
  1585.         $query->andWhere('lm.status = :lessonModuleStatus');
  1586.         
  1587.         $query->setParameter('courseId'$course->getId());
  1588.         $query->setParameter('deleted'LessonEnum::ITEM_NO_DELETED);
  1589.         $query->setParameter('lessonStatus'LessonEnum::PUBLISHED);
  1590.         $query->setParameter('lessonModuleStatus'LessonModuleEnum::PUBLISHED);
  1591.         $query->setMaxResults(1);
  1592.         $result = (object)$query->getQuery()->getOneOrNullResult();
  1593.         return $result->total;
  1594.     }
  1595.     public function countCourseLessons(
  1596.         Course $course,
  1597.         int $userId,
  1598.         ?LessonModule $lessonModule null
  1599.         $isHistory false
  1600.     )
  1601.     {
  1602.         $userPermissionUtil $this->generalService->getUtil('UserPermissionUtil');
  1603.         $permission $userPermissionUtil->getPermission("course""see");
  1604.         $user $userPermissionUtil->getUser();
  1605.         $isInTeam $this->em->getRepository(CourseTeam::class)->userExistInCourseTeam(
  1606.             $course
  1607.             $user
  1608.         );
  1609.         $sqlDateConclusion "
  1610.             SELECT
  1611.                 (
  1612.                     CASE 
  1613.                         WHEN ll.complete = :yes THEN ll.date_conclusion 
  1614.                         ELSE CURDATE() 
  1615.                     END
  1616.                 ) AS date_conclusion
  1617.             FROM lesson_log AS ll
  1618.             WHERE ll.lesson_id = l.id
  1619.             AND ll.course_id = l.course_id
  1620.             AND ll.user_id = :userId
  1621.             ORDER BY ll.id DESC
  1622.             LIMIT 1
  1623.         ";
  1624.         $sqlDateRegisterEnrollement "
  1625.             SELECT
  1626.                 e.date_register
  1627.             FROM enrollment AS e
  1628.             WHERE e.deleted = :deletedEnrollment
  1629.             AND e.course_id = l.course_id
  1630.             AND e.user_id = :userId
  1631.             ORDER BY e.id DESC
  1632.             LIMIT 1
  1633.         ";
  1634.         $sql "
  1635.             SELECT
  1636.                 COUNT(l.id) AS total
  1637.             FROM lesson AS l
  1638.             INNER JOIN course AS c ON (c.id = l.course_id AND c.deleted = :deletedCourse)
  1639.             INNER JOIN lesson_module AS lm ON (
  1640.                 lm.id = l.lesson_module_id AND lm.deleted = :deletedModule
  1641.             )
  1642.             LEFT JOIN library AS lb ON (lb.id = l.library_id AND lb.deleted = :deletedLibrary)
  1643.             WHERE l.deleted = :deleted
  1644.             AND l.course_id = :courseId
  1645.             /* AND  CASO FOSSE CONSIDERADA DATA DE LIBERAÇÃO DA AULA PARA ENCERRAR O ACESSO
  1646.             (
  1647.                 (
  1648.                     l.control_close_period = 0 OR 
  1649.                     l.control_close_period IS NULL
  1650.                 )
  1651.                 OR
  1652.                 (
  1653.                     l.control_close_period > 0
  1654.                     AND l.control_release_type = 0
  1655.                     AND DATEDIFF(CURDATE(), (DATE(
  1656.                         (
  1657.                             CASE 
  1658.                                 WHEN ({$sqlDateConclusion}) IS NOT NULL THEN ({$sqlDateConclusion})
  1659.                                 ELSE CURDATE()
  1660.                             END
  1661.                         )
  1662.                     ) + INTERVAL l.control_close_period DAY)) < 0
  1663.                 )
  1664.                 OR
  1665.                 (
  1666.                     l.control_close_period > 0
  1667.                     AND l.control_release_type = 1
  1668.                     AND DATEDIFF(CURDATE(), (DATE(
  1669.                         (l.control_date_release)
  1670.                     ) + INTERVAL l.control_close_period DAY)) < 0
  1671.                 )
  1672.                 OR
  1673.                 (
  1674.                     l.control_close_period > 0
  1675.                     AND l.control_release_type = 2
  1676.                     AND l.control_release_after_type = 1
  1677.                     AND DATEDIFF(CURDATE(), (DATE(
  1678.                         ({$sqlDateRegisterEnrollement})
  1679.                     ) + INTERVAL l.control_close_period DAY)) < 0
  1680.                 )
  1681.                 OR
  1682.                 (
  1683.                     l.control_close_period > 0
  1684.                     AND l.control_release_type = 2
  1685.                     AND l.control_release_after_type = 2
  1686.                 )
  1687.             )*/
  1688.         ";
  1689.         if($lessonModule){
  1690.             $sql .= " AND l.lesson_module_id = :lessonModuleId ";
  1691.         }
  1692.         if(
  1693.             $isHistory ||
  1694.             $userPermissionUtil->isLow($permission) || 
  1695.             ($userPermissionUtil->isMiddle($permission) && !$isInTeam)
  1696.         ){
  1697.             $sql .= " AND l.status = :lessonStatus ";
  1698.             $sql .= " AND lm.status = :lessonModuleStatus ";
  1699.         }
  1700.         $stmt $this->em->getConnection()->prepare($sql);
  1701.         //$stmt->bindValue(':deletedEnrollment', EnrollmentEnum::ITEM_NO_DELETED, PDO::PARAM_INT);
  1702.         $stmt->bindValue(':deletedCourse'CourseEnum::ITEM_NO_DELETEDPDO::PARAM_INT);
  1703.         $stmt->bindValue(':deletedModule'LessonModuleEnum::ITEM_NO_DELETEDPDO::PARAM_INT);
  1704.         $stmt->bindValue(':deletedLibrary'LibraryEnum::ITEM_NO_DELETEDPDO::PARAM_INT);
  1705.         $stmt->bindValue(':deleted'LessonEnum::ITEM_NO_DELETEDPDO::PARAM_INT);
  1706.         $stmt->bindValue(':courseId'$course->getId(), PDO::PARAM_INT);
  1707.         //$stmt->bindValue(':yes', LessonEnum::YES, PDO::PARAM_INT);
  1708.         //$stmt->bindValue(':userId', $userId, PDO::PARAM_INT);
  1709.         if($lessonModule){
  1710.             $stmt->bindValue(':lessonModuleId'$lessonModule->getId(), PDO::PARAM_INT);
  1711.         }
  1712.         if(
  1713.             $isHistory ||
  1714.             $userPermissionUtil->isLow($permission) ||
  1715.             ($userPermissionUtil->isMiddle($permission) && !$isInTeam)
  1716.         ){
  1717.             $stmt->bindValue(':lessonStatus'LessonEnum::PUBLISHEDPDO::PARAM_INT);
  1718.             $stmt->bindValue(':lessonModuleStatus'LessonModuleEnum::PUBLISHEDPDO::PARAM_INT);
  1719.         }
  1720.         
  1721.         $stmt->execute();
  1722.         $result $stmt->fetchAll(PDO::FETCH_CLASS);
  1723.         return $result[0]->total;
  1724.     }
  1725.     public function getCourseLessons(
  1726.         Course $course
  1727.         ?LessonModule $lessonModule null
  1728.         ?array $lessonIds null
  1729.         ?string $searchText null
  1730.     )
  1731.     {
  1732.         $userPermissionUtil $this->generalService->getUtil('UserPermissionUtil');
  1733.         $permission $userPermissionUtil->getPermission("course""see");
  1734.         $user $userPermissionUtil->getUser();
  1735.         $isInTeam $this->em->getRepository(CourseTeam::class)->userExistInCourseTeam(
  1736.             $course
  1737.             $user
  1738.         );
  1739.         
  1740.         $query $this->createQueryBuilder('l');
  1741.         $query->innerJoin(
  1742.             'EADPlataforma:LessonModule'
  1743.             'lm'
  1744.             'WITH'
  1745.             'lm.id = l.lessonModule AND lm.deleted = 0'
  1746.         );
  1747.         $query->innerJoin(
  1748.             'EADPlataforma:Course'
  1749.             'c'
  1750.             'WITH'
  1751.             'c.id = l.course AND c.deleted = 0'
  1752.         );
  1753.         $query->leftJoin(
  1754.             'EADPlataforma:Library'
  1755.             'lb'
  1756.             'WITH'
  1757.             'lb.id = l.library AND lb.deleted = 0'
  1758.         );
  1759.         $query->andWhere('l.deleted = 0');
  1760.         $query->andWhere('l.course = :courseId');
  1761.         $query->setParameter('courseId'$course->getId());
  1762.         if(!empty($searchText)){
  1763.             $query->andWhere('(l.title LIKE :searchText OR lm.title LIKE :searchText)');
  1764.             $query->setParameter('searchText'"%{$searchText}%");
  1765.         }
  1766.         if($lessonModule){
  1767.             $query->andWhere('l.lessonModule = :lessonModuleId');
  1768.             $query->setParameter('lessonModuleId'$lessonModule->getId());
  1769.         }
  1770.         if(is_array($lessonIds) && !empty($lessonIds)){
  1771.             $query->andWhere($query->expr()->in('l.id'$lessonIds));
  1772.         }
  1773.         if(
  1774.             $userPermissionUtil->isLow($permission) || 
  1775.             ($userPermissionUtil->isMiddle($permission) && !$isInTeam)
  1776.         ){
  1777.             $query->andWhere('l.status = :lessonStatus');
  1778.             $query->andWhere('lm.status = :lessonModuleStatus');
  1779.             $query->setParameter('lessonStatus'LessonEnum::PUBLISHED);
  1780.             $query->setParameter('lessonModuleStatus'LessonModuleEnum::PUBLISHED);
  1781.         }
  1782.         $query->addOrderBy('l.order''ASC');
  1783.         $query->addOrderBy('l.lessonModule''ASC');
  1784.         $data $query->getQuery()->execute();
  1785.         return $data;
  1786.     }
  1787.     public function getFirstLesson(Course $course, ?LessonModule $lessonModule null)
  1788.     {
  1789.         $userPermissionUtil $this->generalService->getUtil('UserPermissionUtil');
  1790.         $permission $userPermissionUtil->getPermission("course""see");
  1791.         $user $userPermissionUtil->getUser();
  1792.         $courseTeamRepository $this->em->getRepository(CourseTeam::class);
  1793.         $isInTeam $courseTeamRepository->userExistInCourseTeam($course$user);
  1794.         
  1795.         $query $this->createQueryBuilder('l');
  1796.         $query->innerJoin(
  1797.             'EADPlataforma:LessonModule'
  1798.             'lm'
  1799.             'WITH'
  1800.             'lm.id = l.lessonModule AND lm.deleted = 0'
  1801.         );
  1802.         $query->innerJoin(
  1803.             'EADPlataforma:Course'
  1804.             'c'
  1805.             'WITH'
  1806.             'c.id = l.course AND c.deleted = 0'
  1807.         );
  1808.         $query->andWhere('l.deleted = 0');
  1809.         $query->andWhere('l.course = :courseId');
  1810.         $query->setParameter('courseId'$course->getId());
  1811.         if($lessonModule){
  1812.             $query->andWhere('l.lessonModule = :lessonModuleId');
  1813.             $query->setParameter('lessonModuleId'$lessonModule->getId());
  1814.         }
  1815.         if(
  1816.             $userPermissionUtil->isLow($permission) || 
  1817.             ($userPermissionUtil->isMiddle($permission) && !$isInTeam)
  1818.         ){
  1819.             $query->andWhere('l.status = :lessonStatus');
  1820.             $query->andWhere('lm.status = :lessonModuleStatus');
  1821.             $query->setParameter('lessonStatus'LessonEnum::PUBLISHED);
  1822.             $query->setParameter('lessonModuleStatus'LessonModuleEnum::PUBLISHED);
  1823.         }
  1824.         $query->addOrderBy('lm.order''ASC');
  1825.         $query->addOrderBy('l.order''ASC');
  1826.         $query->setMaxResults(1);
  1827.         return $query->getQuery()->getOneOrNullResult();
  1828.     }
  1829.     public function getLastLesson(Course $course, ?LessonModule $lessonModule null)
  1830.     {
  1831.         $userPermissionUtil $this->generalService->getUtil('UserPermissionUtil');
  1832.         $permission $userPermissionUtil->getPermission("course""see");
  1833.         $user $userPermissionUtil->getUser();
  1834.         $isInTeam $this->em->getRepository(CourseTeam::class)->userExistInCourseTeam(
  1835.             $course
  1836.             $user
  1837.         );
  1838.         
  1839.         $query $this->createQueryBuilder('l');
  1840.         $query->innerJoin(
  1841.             'EADPlataforma:LessonModule'
  1842.             'lm'
  1843.             'WITH'
  1844.             'lm.id = l.lessonModule AND lm.deleted = 0'
  1845.         );
  1846.         $query->innerJoin(
  1847.             'EADPlataforma:Course'
  1848.             'c'
  1849.             'WITH'
  1850.             'c.id = l.course AND c.deleted = 0'
  1851.         );
  1852.         $query->andWhere('l.deleted = 0');
  1853.         $query->andWhere('l.course = :courseId');
  1854.         $query->setParameter('courseId'$course->getId());
  1855.         if($lessonModule){
  1856.             $query->andWhere('l.lessonModule = :lessonModuleId');
  1857.             $query->setParameter('lessonModuleId'$lessonModule->getId());
  1858.         }
  1859.         if(
  1860.             $userPermissionUtil->isLow($permission) || 
  1861.             ($userPermissionUtil->isMiddle($permission) && !$isInTeam)
  1862.         ){
  1863.             $query->andWhere('l.status = :lessonStatus');
  1864.             $query->andWhere('lm.status = :lessonModuleStatus');
  1865.             $query->setParameter('lessonStatus'LessonEnum::PUBLISHED);
  1866.             $query->setParameter('lessonModuleStatus'LessonModuleEnum::PUBLISHED);
  1867.         }
  1868.         $query->addOrderBy('lm.order''DESC');
  1869.         $query->addOrderBy('l.order''DESC');
  1870.         $query->setMaxResults(1);
  1871.         return $query->getQuery()->getOneOrNullResult();
  1872.     }
  1873.     public function getLessonByLessonModule(LessonModule $lessonModule)
  1874.     {   
  1875.         $query $this->createQueryBuilder('l');
  1876.         $query->leftJoin(
  1877.             'EADPlataforma:Library'
  1878.             'li'
  1879.             'WITH'
  1880.             'li.id = l.library AND li.deleted = 0'
  1881.         );
  1882.         $query->andWhere('l.deleted = 0');
  1883.         $query->andWhere('l.status = :status');
  1884.         $query->andWhere('l.course = :courseId');
  1885.         $query->andWhere('l.lessonModule = :lessonModule');
  1886.         $query->setParameter('courseId'$lessonModule->getCourse()->getId());
  1887.         $query->setParameter('lessonModule'$lessonModule->getId());
  1888.         $query->setParameter('status'LessonEnum::PUBLISHED);
  1889.         $query->addOrderBy('l.order''ASC');
  1890.         return $query->getQuery()->execute();
  1891.     }
  1892.     public function getNextLessonByLessonModule(
  1893.         int $order
  1894.         LessonModule $lessonModule
  1895.         int $limit 3
  1896.     )
  1897.     {   
  1898.         $userPermissionUtil $this->generalService->getUtil('UserPermissionUtil');
  1899.         $permission $userPermissionUtil->getPermission("course""see");
  1900.         $course $lessonModule->getCourse();
  1901.         $user $userPermissionUtil->getUser();
  1902.         $isInTeam $this->em->getRepository(CourseTeam::class)->userExistInCourseTeam(
  1903.             $course
  1904.             $user
  1905.         );
  1906.         $query $this->createQueryBuilder('l');
  1907.         $query->select('
  1908.             l.id,
  1909.             l.order,
  1910.             l.title,
  1911.             l.status,
  1912.             l.controlRequirement,
  1913.             l.controlReleaseType,
  1914.             l.controlReleaseAfterType,
  1915.             l.controlDateRelease,
  1916.             l.controlReleasePeriod,
  1917.             l.controlClosePeriod,
  1918.             lm.id AS lessonModule,
  1919.             li.type AS libraryType,
  1920.             li.pagesNumber,
  1921.             li.duration
  1922.         ');
  1923.         $query->innerJoin(
  1924.             'EADPlataforma:LessonModule'
  1925.             'lm'
  1926.             'WITH'
  1927.             'lm.id = l.lessonModule AND lm.deleted = 0'
  1928.         );
  1929.         $query->innerJoin(
  1930.             'EADPlataforma:Course'
  1931.             'c'
  1932.             'WITH'
  1933.             'c.id = l.course AND c.deleted = 0'
  1934.         );
  1935.         $query->leftJoin(
  1936.             'EADPlataforma:Library'
  1937.             'li'
  1938.             'WITH'
  1939.             'li.id = l.library AND li.deleted = 0'
  1940.         );
  1941.         if(
  1942.             $userPermissionUtil->isLow($permission) || 
  1943.             ($userPermissionUtil->isMiddle($permission) && !$isInTeam)
  1944.         ){
  1945.             $query->andWhere('l.status = :lessonStatus');
  1946.             $query->andWhere('lm.status = :lessonModuleStatus');
  1947.             $query->setParameter('lessonStatus'LessonEnum::PUBLISHED);
  1948.             $query->setParameter('lessonModuleStatus'LessonModuleEnum::PUBLISHED);
  1949.         }
  1950.         $query->andWhere('l.deleted = 0');
  1951.         $query->andWhere('l.course = :courseId');
  1952.         $query->andWhere('l.lessonModule = :lessonModule');
  1953.         $query->andWhere('l.order > :order');
  1954.         $query->setParameter('courseId'$course->getId());
  1955.         $query->setParameter('lessonModule'$lessonModule->getId());
  1956.         $query->setParameter('order'$order);
  1957.         $query->addOrderBy('l.order''ASC');
  1958.         $query->setMaxResults($limit);
  1959.         return $query->getQuery()->execute();
  1960.     }
  1961.     public function getLessonNotView($userId$courseId$lessonId null)
  1962.     {
  1963.         $and '';
  1964.         if(!empty($lessonId))
  1965.         {
  1966.             $and .= " AND l.id = :lessonId";
  1967.         }
  1968.         if(empty($courseId) || empty($userId))
  1969.         {
  1970.             return [];
  1971.         }
  1972.         $dql "SELECT
  1973.                     l.id,
  1974.                     l.title,
  1975.                     lm.title AS titleModule,
  1976.                     '0' AS totalView,
  1977.                     '' AS lastAccess,
  1978.                     '--' AS timeWatch,
  1979.                     '0' AS completed,
  1980.                     '---' AS dateConclusion
  1981.                 FROM EADPlataforma:Lesson AS l
  1982.                 INNER JOIN EADPlataforma:LessonModule AS lm WITH (lm.id = l.lessonModule AND lm.deleted = 0)
  1983.                 WHERE l.deleted = 0
  1984.                 AND l.course = :courseId
  1985.                 {$and}
  1986.                 AND l.id NOT IN (
  1987.                     SELECT
  1988.                         IDENTITY (ll.lesson)
  1989.                     FROM EADPlataforma:LessonLogOrigin AS ll
  1990.                     WHERE ll.user != 1
  1991.                     AND ll.user = :userId
  1992.                     AND ll.course = :courseId
  1993.                 )
  1994.                 ORDER BY l.order ASC";
  1995.                 
  1996.         $query $this->em->createQuery($dql);
  1997.         $query->setParameter('userId'$userId);
  1998.         $query->setParameter('courseId'$courseId);
  1999.       
  2000.         if(!empty($lessonId))
  2001.         {
  2002.             $query->setParameter('lessonId'$lessonId);
  2003.         }
  2004.         $data $query->execute();
  2005.         
  2006.         $result = [];
  2007.         foreach ($data as $key => $value) {
  2008.             $result[] = [
  2009.                 $value["id"],
  2010.                 $value["title"],
  2011.                 $value["titleModule"],
  2012.                 $value["totalView"],
  2013.                 $value["lastAccess"],
  2014.                 $value["timeWatch"],
  2015.                 $value["completed"],
  2016.                 $value["dateConclusion"]
  2017.             ];
  2018.         }
  2019.         return $result;
  2020.     }
  2021.     public function isFirstCourseLesson(?Lesson $lesson)
  2022.     {
  2023.         if(!$lesson){
  2024.             return false;
  2025.         }
  2026.         $firstLesson $this->getFirstLesson($lesson->getCourse());
  2027.         if($firstLesson){
  2028.             return $firstLesson->getId() == $lesson->getId();
  2029.         }
  2030.         return true;
  2031.     }
  2032.     public function isFirstModuleLesson(?Lesson $lesson)
  2033.     {
  2034.         if(!$lesson){
  2035.             return false;
  2036.         }
  2037.         $firstLesson $this->getFirstLesson($lesson->getCourse(), $lesson->getLessonModule());
  2038.         if($firstLesson){
  2039.             return $firstLesson->getId() == $lesson->getId();
  2040.         }
  2041.         return true;
  2042.     }
  2043.     public function isLastCourseLesson(?Lesson $lesson)
  2044.     {
  2045.         if(!$lesson){
  2046.             return false;
  2047.         }
  2048.         $firstLesson $this->getLastLesson($lesson->getCourse());
  2049.         if($firstLesson){
  2050.             return $firstLesson->getId() == $lesson->getId();
  2051.         }
  2052.         return true;
  2053.     }
  2054.     public function isLastModuleLesson(?Lesson $lesson)
  2055.     {
  2056.         if(!$lesson){
  2057.             return false;
  2058.         }
  2059.         $firstLesson $this->getLastLesson(
  2060.             $lesson->getCourse(), 
  2061.             $lesson->getLessonModule()
  2062.         );
  2063.         if($firstLesson){
  2064.             return $firstLesson->getId() == $lesson->getId();
  2065.         }
  2066.         return true;
  2067.     }
  2068.     public function isLessonVisibleToStudent(
  2069.         Lesson $lesson
  2070.         ?bool $ignoreControlFunction false,
  2071.         ?bool $isStudent true
  2072.     ): bool
  2073.     {
  2074.         $info $this->checkLessonVisibleToStudent($lesson$ignoreControlFunction$isStudent);
  2075.         return $info->visible;
  2076.     }
  2077.     public function checkLessonVisibleToStudent(
  2078.         Lesson $lesson
  2079.         ?bool $ignoreControlFunction false,
  2080.         ?bool $isStudent true
  2081.     ): object
  2082.     {
  2083.         $userPermissionUtil $this->generalService->getUtil('UserPermissionUtil');
  2084.         $user $userPermissionUtil->getUser();
  2085.         $info = (object)[
  2086.             "visible" => true,
  2087.             "message" => "",
  2088.         ];
  2089.         if(!$user){
  2090.             $info->visible false;
  2091.             return $info;
  2092.         }
  2093.         $course $lesson->getCourse();
  2094.         $lessonModule $lesson->getLessonModule();
  2095.         if($lesson->isDeleted() || $lessonModule->isDeleted() || $course->isDeleted()){
  2096.             $info->visible false;
  2097.             return $info;
  2098.         }
  2099.         if(!$isStudent){
  2100.             $info->visible true;
  2101.             return $info;
  2102.         }
  2103.         $enrollmentRepository $this->em->getRepository(Enrollment::class);
  2104.         $isEnroll $enrollmentRepository->isValidEnrollmentByUser(
  2105.             $user->getId(), 
  2106.             $course->getId()
  2107.         );
  2108.         $hasLessonControl $this->configuration->checkModuleIsAbleOnPlan(
  2109.             'lessonControlFunction'
  2110.         );
  2111.             
  2112.         if($lesson->getStatus() == LessonEnum::DRAFT){
  2113.             $info->visible false;
  2114.             return $info;
  2115.         }
  2116.         if($lessonModule->getStatus() == LessonModuleEnum::DRAFT){
  2117.             $info->visible false;
  2118.             return $info;
  2119.         }
  2120.         if($course->getStatus() == CourseEnum::DRAFT){
  2121.             $info->visible false;
  2122.             return $info;
  2123.         }
  2124.         if(!$isEnroll){
  2125.             $info->visible false;
  2126.             return $info;
  2127.         }
  2128.         
  2129.         if(!$ignoreControlFunction){
  2130.             if($lesson->getControlReleaseType() == LessonEnum::FIXED_DATE){
  2131.                 $todaySeconds strtotime(date('Y-m-d H:i:s'));
  2132.                 if($hasLessonControl){
  2133.                     $controlClosePeriod $lesson->getControlClosePeriod();
  2134.                     if(!empty($controlClosePeriod)){
  2135.                         $timeLog strtotime(
  2136.                             "{$lesson->getControlDateRelease()} + {$controlClosePeriod} day"
  2137.                         );
  2138.                         if($todaySeconds $timeLog){
  2139.                             $info->visible false;
  2140.                             return $info;
  2141.                         }
  2142.                     }
  2143.                 }
  2144.             }else{
  2145.                 $lessonLogRepository $this->emEadmin->getRepository(LessonLog::class);
  2146.                 $logId "{$course->getId()}#{$user->getId()}#{$lesson->getId()}";
  2147.                 $lessonLog $lessonLogRepository->find($logId);
  2148.                 //check lesson to user is closed
  2149.                 if($lessonLog){
  2150.                     $todaySeconds strtotime(date('Y-m-d H:i:s'));
  2151.                     if($hasLessonControl){
  2152.                         $controlClosePeriod $lesson->getControlClosePeriod();
  2153.                         if(
  2154.                             !empty($controlClosePeriod) && 
  2155.                             $lessonLog->getComplete() == LessonEnum::YES
  2156.                         ){
  2157.                             $timeLog strtotime(
  2158.                                 "{$lessonLog->getDateConclusion()} + {$controlClosePeriod} day"
  2159.                             );
  2160.                             if($todaySeconds $timeLog){
  2161.                                 $info->visible false;
  2162.                                 return $info;
  2163.                             }
  2164.                         }
  2165.                     }
  2166.                 }
  2167.             } 
  2168.         }
  2169.         return $info;
  2170.     }
  2171.     public function getLessonNumberByProduct(Product $product)
  2172.     {
  2173.         $query $this->createQueryBuilder('l');
  2174.         $query->select("COUNT(l.id) AS total");
  2175.         $query->innerJoin('EADPlataforma:Course''c''WITH''l.course = c.id');
  2176.         $query->innerJoin('EADPlataforma:Product''p''WITH''l.course MEMBER OF p.course');
  2177.         $query->innerJoin('EADPlataforma:LessonModule''lm''WITH''l.lessonModule = lm.id');
  2178.         $query->andWhere('l.deleted = :deletedLesson');
  2179.         $query->andWhere('c.deleted = :deletedCourse');
  2180.         $query->andWhere('lm.deleted = :deletedLessonModule');
  2181.         $query->andWhere('l.status = :statusLesson');
  2182.         $query->andWhere('c.status = :statusCourse');
  2183.         $query->andWhere('lm.status = :statusLessonModule');
  2184.         $query->andWhere('p.id = :productId');
  2185.         $query->setParameter('deletedLesson'LessonEnum::ITEM_NO_DELETED);
  2186.         $query->setParameter('deletedCourse'CourseEnum::ITEM_NO_DELETED);
  2187.         $query->setParameter('deletedLessonModule'LessonModuleEnum::ITEM_NO_DELETED);
  2188.         $query->setParameter('statusLesson'LessonEnum::PUBLISHED);
  2189.         $query->setParameter('statusCourse'CourseEnum::PUBLISHED);
  2190.         $query->setParameter('statusLessonModule'LessonModuleEnum::PUBLISHED);
  2191.         $query->setParameter('productId'$product->getId());
  2192.         $result = (object)$query->getQuery()->getOneOrNullResult();
  2193.         return $result->total;
  2194.     }
  2195.     public function getLessonForExportPresence(Course $course$lessonModuleId null){
  2196.         $query $this->createQueryBuilder('l');
  2197.         $query->select('
  2198.             l.id,
  2199.             lm.id AS lessonModuleId,
  2200.             l.order,
  2201.             lm.title,
  2202.             l.title AS lessonTitle
  2203.         ');
  2204.         $query->innerJoin(
  2205.             'EADPlataforma:LessonModule'
  2206.             'lm'
  2207.             'WITH'
  2208.             'lm.id = l.lessonModule AND lm.deleted = 0'
  2209.         );
  2210.         if($lessonModuleId){
  2211.             $query->andWhere('lm.id = :lessonModuleId');
  2212.             $query->setParameter('lessonModuleId'$lessonModuleId);
  2213.         }
  2214.         
  2215.         $query->andWhere('l.status = :lessonStatus');
  2216.         $query->andWhere('lm.status = :lessonModuleStatus');
  2217.         $query->setParameter('lessonStatus'LessonEnum::PUBLISHED);
  2218.         $query->setParameter('lessonModuleStatus'LessonModuleEnum::PUBLISHED);
  2219.         
  2220.         $query->andWhere('l.deleted = 0');
  2221.         $query->andWhere('l.course = :courseId');
  2222.         $query->setParameter('courseId'$course->getId());
  2223.         $query->addOrderBy('lm.order''ASC');
  2224.         $query->addOrderBy('lm.id''ASC');
  2225.         $query->addOrderBy('l.order''ASC');
  2226.         $query->addOrderBy('l.id''ASC');
  2227.         return $query->getQuery()->execute();
  2228.     }
  2229.     public function restore(Lesson $lesson$typeItem$key)
  2230.     {
  2231.         $lessonId $lesson->getId();
  2232.         $dateNow date('Y-m-d H:i:s');
  2233.         $lastOrder $this->count(
  2234.             [ 
  2235.                 "deleted" => LessonEnum::ITEM_NO_DELETED
  2236.                 "lessonModule" => $lesson->getLessonModule()->getId() 
  2237.             ]
  2238.         ) + ($key 1);
  2239.         $lesson->setOrder($lastOrder);
  2240.         $courseRepository =  $this->em->getRepository(Course::class);
  2241.         $lesson->setUserDelete($this->getUser());
  2242.         $lesson->setDateDelete($dateNow);
  2243.         $lesson->restore();
  2244.         $deleted LessonEnum::ITEM_NO_DELETED;
  2245.         $typeDelete LessonEnum::CASCADE;
  2246.         $this->em->getRepository(Exam::class)->restoreByLesson(
  2247.             $lessonId
  2248.             $deleted
  2249.             $typeDelete
  2250.             $this->getUser(), 
  2251.             $dateNow
  2252.         );
  2253.         $this->em->getRepository(ExamUser::class)->restoreByLesson(
  2254.             $lessonId
  2255.             $deleted
  2256.             $typeDelete
  2257.             $this->getUser(), 
  2258.             $dateNow
  2259.         );
  2260.         $this->em->getRepository(LessonSupport::class)->restoreByLesson(
  2261.             $lessonId
  2262.             $deleted
  2263.             $typeDelete
  2264.             $this->getUser(), 
  2265.             $dateNow
  2266.         );
  2267.         $this->em->getRepository(LessonXLibrary::class)->restoreByLesson(
  2268.             $lessonId
  2269.             $deleted
  2270.             $typeDelete
  2271.             $this->getUser(), 
  2272.             $dateNow
  2273.         );
  2274.         $this->em->getRepository(Trash::class)->deleteTrash($lessonId$typeItem);
  2275.     }
  2276.     public function delete(Lesson $lesson$typeItem$permission$isTrash)
  2277.     {
  2278.         $lessonId $lesson->getId();
  2279.         $dateNow date('Y-m-d H:i:s');
  2280.         $typeDelete LessonEnum::CASCADE;
  2281.         $notificationService $this->generalService->getService('NotificationService');
  2282.         $userPermissionUtil $this->generalService->getUtil('UserPermissionUtil');
  2283.         $lessonXLibrarys $this->em->getRepository(LessonXLibrary::class)->findBy([
  2284.             "lesson" => $lesson->getId(),
  2285.         ]);
  2286.         $libraryRepository $this->em->getRepository(Library::class);
  2287.         $libraryModule $this->configuration->checkModuleIsAbleOnPlan('libraryModule');
  2288.         if($isTrash == LessonEnum::YES){
  2289.             $permission $userPermissionUtil->getPermission("trash""delete");
  2290.         }
  2291.         $isLessonTeacher $this->isLessonTeacher($lesson$this->getUser());
  2292.         if($isLessonTeacher || $userPermissionUtil->isHigh($permission)){
  2293.             $lesson->setUserDelete($this->getUser());
  2294.             $lesson->setDateDelete($dateNow);
  2295.             $lesson->individual();
  2296.             
  2297.             if($lesson->isOnTrash() || $lesson->isDeleted()){
  2298.                 
  2299.                 $lesson->delete();
  2300.                 $deleted LessonEnum::ITEM_DELETED;
  2301.                 $this->em->getRepository(Trash::class)->deleteTrash($lessonId$typeItem);
  2302.                 $this->em->flush();
  2303.                 foreach ($lessonXLibrarys as $key => $lessonXLibrary) {
  2304.                     $library $lessonXLibrary->getLibrary();
  2305.                     if(!$libraryModule && $library){
  2306.                         $library->cascade();
  2307.                         $libraryRepository->deleteContent($library);
  2308.                     }
  2309.                     $lessonXLibrary->delete();
  2310.                 }
  2311.                 $library $lesson->getLibrary();
  2312.                 if(!$libraryModule && $library){
  2313.                     $library->cascade();
  2314.                     $libraryRepository->deleteContent($library);
  2315.                 }
  2316.             }
  2317.             if(!$lesson->isOnTrash() && !$lesson->isDeleted()){
  2318.                 $order $lesson->getOrder();
  2319.                 $this->refreshOrder(
  2320.                     $lesson->getCourse()->getId(), 
  2321.                     $lesson->getLessonModule()->getId(),
  2322.                     $order,
  2323.                     null,
  2324.                     true
  2325.                 );
  2326.                 $notificationService->delete(
  2327.                     NotificationEnum::ORIGIN_LESSON_SUPPORT_LIKE
  2328.                     $lessonId
  2329.                 );
  2330.                 $notificationService->delete(
  2331.                     NotificationEnum::ORIGIN_LESSON_SUPPORT_ANSWER_LIKE
  2332.                     $lessonId
  2333.                 );
  2334.                 $notificationService->delete(
  2335.                     NotificationEnum::ORIGIN_LESSON_SUPPORT_NEW
  2336.                     $lessonId
  2337.                 );
  2338.                 $notificationService->delete(
  2339.                     NotificationEnum::ORIGIN_LESSON_SUPPORT_ANSWER_NEW
  2340.                     $lessonId
  2341.                 );
  2342.                 $notificationService->delete(
  2343.                     NotificationEnum::ORIGIN_LESSON_NEW
  2344.                     $lessonId
  2345.                 );
  2346.                 $notificationService->delete(
  2347.                     NotificationEnum::ORIGIN_LESSON_CHANGE
  2348.                     $lessonId
  2349.                 );
  2350.                 $lesson->trash();
  2351.                 $deleted LessonEnum::ITEM_ON_TRASH;
  2352.                 $this->em->getRepository(Trash::class)->insertTrash(
  2353.                     $lesson
  2354.                     $typeItem
  2355.                     "Aula: {$lesson->getTitle()} | Prof. {$lesson->getUser()->getName()}"
  2356.                 );
  2357.             }
  2358.         }
  2359.         
  2360.         $this->em->getRepository(Exam::class)->deleteByLesson(
  2361.             $lessonId
  2362.             $deleted
  2363.             $typeDelete
  2364.             $this->getUser(), 
  2365.             $dateNow
  2366.         );
  2367.         $this->em->getRepository(ExamUser::class)->deleteByLesson(
  2368.             $lessonId
  2369.             $deleted
  2370.             $typeDelete
  2371.             $this->getUser(), 
  2372.             $dateNow
  2373.         );
  2374.         $this->em->getRepository(LessonSupport::class)->deleteByLesson(
  2375.             $lessonId
  2376.             $deleted
  2377.             $typeDelete
  2378.             $this->getUser(), 
  2379.             $dateNow
  2380.         );
  2381.         $this->em->getRepository(LessonXLibrary::class)->deleteByLesson(
  2382.             $lessonId
  2383.             $deleted
  2384.             $typeDelete
  2385.             $this->getUser(), 
  2386.             $dateNow
  2387.         );
  2388.     }
  2389.     public function restoreByCourse($courseId$deleted$typeDelete){
  2390.         $typeItem TrashEnum::LESSON;
  2391.         $lessons $this->em->getRepository(Lesson::class)->findBy([
  2392.             "course" => $courseId,
  2393.             "deleted" => ($deleted),
  2394.             "typeDelete" => $typeDelete
  2395.         ]);
  2396.         foreach ($lessons as $key => $lesson) {
  2397.             $this->restore($lesson$typeItem$key);
  2398.         }
  2399.     }
  2400.     public function deleteByCourse($courseId$deleted){
  2401.         $typeItem TrashEnum::LESSON;
  2402.         $isTrash LessonEnum::NO;
  2403.         if($deleted == LessonEnum::ITEM_ON_TRASH){
  2404.             $isTrash LessonEnum::YES;
  2405.         }
  2406.         $userPermissionUtil $this->generalService->getUtil('UserPermissionUtil');
  2407.         $permission $userPermissionUtil->getPermission(
  2408.             "course"
  2409.             "lesson"
  2410.             "delete"
  2411.         );
  2412.         $lessons $this->em->getRepository(Lesson::class)->findBy([
  2413.             "course" => $courseId,
  2414.             "deleted" => ($deleted 1)
  2415.         ]);
  2416.         foreach ($lessons as $key => $lesson) {
  2417.             $this->delete($lesson$typeItem$permission$isTrash);
  2418.             $lesson->cascade();
  2419.         }
  2420.     }
  2421.     public function restoreByLessonModule($lessonModuleId$deleted$typeDelete){
  2422.         $typeItem TrashEnum::LESSON;
  2423.         $lessons $this->em->getRepository(Lesson::class)->findBy([
  2424.             "lessonModule" => $lessonModuleId,
  2425.             "deleted" => ($deleted),
  2426.             "typeDelete" => $typeDelete
  2427.         ]);
  2428.         foreach ($lessons as $key => $lesson) {
  2429.             $this->restore($lesson$typeItem$key);
  2430.         }
  2431.     }
  2432.     public function deleteByLessonModule($lessonModuleId$deleted){
  2433.         $typeItem TrashEnum::LESSON;
  2434.         $isTrash LessonEnum::NO;
  2435.         if($deleted == LessonEnum::ITEM_ON_TRASH){
  2436.             $isTrash LessonEnum::YES;
  2437.         }
  2438.         $userPermissionUtil $this->generalService->getUtil('UserPermissionUtil');
  2439.         $permission $userPermissionUtil->getPermission(
  2440.             "course"
  2441.             "lesson"
  2442.             "delete"
  2443.         );
  2444.         $lessons $this->em->getRepository(Lesson::class)->findBy([
  2445.             "lessonModule" => $lessonModuleId,
  2446.             "deleted" => ($deleted 1)
  2447.         ]);
  2448.         foreach ($lessons as $key => $lesson) {
  2449.             $this->delete($lesson$typeItem$permission$isTrash);
  2450.             $lesson->cascade();
  2451.         }
  2452.     }
  2453.     public function deleteTrashCron()
  2454.     {
  2455.         $sql "UPDATE EADPlataforma:Lesson AS l SET l.deleted = 2 
  2456.                 WHERE l.dateDelete <= :date AND l.deleted = 1 ";
  2457.         $query $this->em->createQuery($sql);
  2458.         $query->setParameter('date'date('Y-m-d H:i:s'strtotime('-90 days')));
  2459.         $query->execute();
  2460.     }
  2461. }