src/Repository/EnrollmentRepository.php line 1011

Open in your IDE?
  1. <?php
  2. namespace EADPlataforma\Repository;
  3. use EADPlataforma\Entity\Enrollment;
  4. use EADPlataforma\Entity\Course;
  5. use EADPlataforma\Entity\Library;
  6. use EADPlataforma\Entity\Lesson;
  7. use EADPlataforma\Entity\LessonLog;
  8. use EADPlataforma\Entity\LessonLogOrigin;
  9. use EADPlataforma\Entity\LessonModule;
  10. use EADPlataforma\Entity\LessonSupport;
  11. use EADPlataforma\Entity\LessonAnnotation;
  12. use EADPlataforma\Entity\Exam;
  13. use EADPlataforma\Entity\ExamUser;
  14. use EADPlataforma\Entity\CourseTeam;
  15. use EADPlataforma\Entity\CourseTestimonial;
  16. use EADPlataforma\Entity\CourseCertificate;
  17. use EADPlataforma\Entity\UserSubscription;
  18. use EADPlataforma\Entity\Product;
  19. use EADPlataforma\Entity\ProductOffer;
  20. use EADPlataforma\Entity\User;
  21. use EADPlataforma\Entity\Trash;
  22. use EADPlataforma\Enum\ExamEnum;
  23. use EADPlataforma\Enum\CourseEnum;
  24. use EADPlataforma\Enum\CourseTestimonialEnum;
  25. use EADPlataforma\Enum\CourseCertificateEnum;
  26. use EADPlataforma\Enum\ExamUserEnum;
  27. use EADPlataforma\Enum\GroupEnum;
  28. use EADPlataforma\Enum\LessonEnum;
  29. use EADPlataforma\Enum\EnrollmentEnum;
  30. use EADPlataforma\Enum\UserSubscriptionEnum;
  31. use EADPlataforma\Enum\LessonAnnotationEnum;
  32. use EADPlataforma\Enum\LessonSupportEnum;
  33. use EADPlataforma\Enum\ProductEnum;
  34. use EADPlataforma\Enum\NotificationEnum;
  35. use EADPlataforma\Enum\UserEnum;
  36. use EADPlataforma\Enum\TagsMarketingEnum;
  37. use EADPlataforma\Enum\WebhookEnum;
  38. /**
  39.  * @method Enrollment|null find($id, $lockMode = null, $lockVersion = null)
  40.  * @method Enrollment|null findOneBy(array $criteria, array $orderBy = null)
  41.  * @method Enrollment[]    findAll()
  42.  * @method Enrollment[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  43.  */
  44. class EnrollmentRepository extends AbstractRepository
  45. {
  46.     public function getEntityClass(){
  47.         return Enrollment::class;
  48.     }
  49.     public function getConnectionName(){
  50.         return "school";
  51.     }
  52.     public function findItems()
  53.     {
  54.         return $this->createQueryBuilder('e')->getQuery()->getResult();
  55.     }
  56.     public function getStringStatus($status){
  57.         $string '';
  58.         switch ($status) {
  59.             case EnrollmentEnum::STATUS_ACTIVE:
  60.                 $string $this->configuration->getLanguage('active''enrollments');
  61.                 break;
  62.             case EnrollmentEnum::STATUS_EXPIRED:
  63.                 $string $this->configuration->getLanguage('expired''enrollments');    
  64.                 break;
  65.             case EnrollmentEnum::STATUS_SUSPENDED:
  66.                 $string $this->configuration->getLanguage('suspended''enrollments'); 
  67.                 break;
  68.             case EnrollmentEnum::STATUS_CANCELED:
  69.                 $string $this->configuration->getLanguage('canceled''enrollments'); 
  70.                 break;
  71.         }
  72.         return $string;
  73.     }
  74.     public function getOriginString($origin){
  75.         $string '';
  76.         switch ($origin) {
  77.             case EnrollmentEnum::ORIGIN_FREE:
  78.                 $string $this->configuration->getLanguage('free''enrollments');
  79.             break;
  80.             case EnrollmentEnum::ORIGIN_SALE:
  81.                 $string $this->configuration->getLanguage('sale''enrollments');    
  82.             break;
  83.             case EnrollmentEnum::ORIGIN_SUBSCRIPTION:
  84.                 $string $this->configuration->getLanguage('subscription''enrollments'); 
  85.             break;
  86.             case EnrollmentEnum::ORIGIN_GROUP:
  87.                 $string $this->configuration->getLanguage('group''enrollments'); 
  88.             break;
  89.             case EnrollmentEnum::ORIGIN_COUPOM:
  90.                 $string $this->configuration->getLanguage('coupom''enrollments'); 
  91.             break;
  92.             case EnrollmentEnum::ORIGIN_API:
  93.                 $string 'API'
  94.             break;
  95.             case EnrollmentEnum::ORIGIN_CHARGE:
  96.                 $string $this->configuration->getLanguage('charge''enrollments'); 
  97.             break;
  98.         }
  99.         return $string;
  100.     }
  101.     public function getEnrollmentOrigins(){
  102.         $data = [
  103.             (object)[
  104.                 "id" => EnrollmentEnum::ORIGIN_FREE,
  105.                 "title" => $this->configuration->getLanguage('free''enrollments'),
  106.                 "selected" => EnrollmentEnum::NO,
  107.             ],
  108.             (object)[
  109.                 "id" => EnrollmentEnum::ORIGIN_SALE,
  110.                 "title" => $this->configuration->getLanguage('sale''enrollments'),
  111.                 "selected" => EnrollmentEnum::NO,
  112.             ],
  113.             (object)[
  114.                 "id" => EnrollmentEnum::ORIGIN_SUBSCRIPTION,
  115.                 "title" => $this->configuration->getLanguage('subscription''enrollments'),
  116.                 "selected" => EnrollmentEnum::NO,
  117.             ],
  118.             (object)[
  119.                 "id" => EnrollmentEnum::ORIGIN_GROUP,
  120.                 "title" => $this->configuration->getLanguage('group''enrollments'),
  121.                 "selected" => EnrollmentEnum::NO,
  122.             ],
  123.             (object)[
  124.                 "id" => EnrollmentEnum::ORIGIN_COUPOM,
  125.                 "title" => $this->configuration->getLanguage('coupom''enrollments'),
  126.                 "selected" => EnrollmentEnum::NO,
  127.             ],
  128.             (object)[
  129.                 "id" => EnrollmentEnum::ORIGIN_API,
  130.                 "title" => "API",
  131.                 "selected" => EnrollmentEnum::NO,
  132.             ],
  133.             (object)[
  134.                 "id" => EnrollmentEnum::ORIGIN_CHARGE,
  135.                 "title" => $this->configuration->getLanguage('charge''enrollments'),
  136.                 "selected" => EnrollmentEnum::NO,
  137.             ],
  138.         ];
  139.         return $data;
  140.     }
  141.     public function sendWebhook(Enrollment $enrollment)
  142.     {
  143.         $user $enrollment->getUser();
  144.         $course $enrollment->getCourse();
  145.         $userWebhook $this->em->getRepository(User::class)->getToWebhook($user);
  146.         $dataObj = (object)[
  147.             "user" => $userWebhook,
  148.             "course" => (object)[
  149.                 "id" => (string)$course->getId(),
  150.                 "name" => $course->getTitle(),
  151.                 "workload" => $course->getWorkload(),
  152.             ],
  153.             "enrollment" => $enrollment->toWebhook(),
  154.         ];
  155.         if ($enrollment->getGroup()) {
  156.             $dataObj->group = (object)[
  157.                 "id" => (string)$enrollment->getGroup()->getId(),
  158.                 "name" => $enrollment->getGroup()->getName(),
  159.             ];
  160.         };
  161.         $webhookService $this->generalService->getService('WebhookService');
  162.         $webhookService->addItemList(WebhookEnum::ENROLLMENT$dataObj);
  163.         return;
  164.     }
  165.     public function getPublicEnrollments(
  166.         $userId
  167.         bool $onlyStarted false
  168.         $notCompleted false
  169.     )
  170.     {
  171.         $query $this->createQueryBuilder('e');
  172.         $query->innerJoin(
  173.             'EADPlataforma:Course''c''WITH''e.course = c AND c.deleted = 0'
  174.         );
  175.         $query->andWhere('e.user = :userId');
  176.         $query->andWhere('e.deleted = 0');
  177.         $query->setParameter('userId'$userId);
  178.         if($onlyStarted){
  179.             $query->andWhere('e.lessonNumberComplete != 0');
  180.         }
  181.         $query->addOrderBy('e.id''DESC');
  182.         $data $query->getQuery()->execute();
  183.         if($notCompleted){
  184.             $dataNotCompleted = [];
  185.             foreach ($data as $key => $enrollment) {
  186.                 $historyMetrics $this->getHistoryMetricsByEnrollment($enrollment->id);
  187.                 if($historyMetrics->progress 100){
  188.                     $dataNotCompleted[] = $enrollment;
  189.                 }
  190.             }
  191.             $data $dataNotCompleted;
  192.         }
  193.         return $data;
  194.     }
  195.     
  196.     public function getResumeEnrollment(
  197.         int $userId
  198.         ?array $situation = [], 
  199.         ?int $status null,
  200.         ?string $searchText null
  201.         ?string $categories null
  202.         ?int $offset 0
  203.         ?int $limit 10
  204.     )
  205.     {
  206.         $query $this->createQueryBuilder('e');
  207.         $query->innerJoin(
  208.             'EADPlataforma:Course'
  209.             'c'
  210.             'WITH'
  211.             'e.course = c 
  212.             AND c.deleted = :courseDeleted 
  213.             AND c.status = :courseStatus
  214.             AND c.dateRelease <= :now'
  215.         );
  216.         $query->andWhere('e.user = :userId');
  217.         $query->andWhere('e.deleted = 0');
  218.         
  219.         $situationWhere = [];
  220.         if(in_array(EnrollmentEnum::SITUATION_STARTED$situation)){
  221.             $situationWhere[] = '
  222.                 (
  223.                     (e.lessonNumberComplete * 100)/c.numberLesson < 100
  224.                     AND (e.lessonNumberComplete * 100)/c.numberLesson > 0
  225.                 )';
  226.         }
  227.         if(in_array(EnrollmentEnum::SITUATION_NOT_STARTED$situation)){
  228.             $situationWhere[] = '(
  229.                 c.numberLesson = 0 
  230.                 OR (e.lessonNumberComplete * 100)/c.numberLesson <= 0
  231.             )';
  232.         }
  233.         if(in_array(EnrollmentEnum::SITUATION_COMPLETED$situation)){
  234.             $situationWhere[] = '((e.lessonNumberComplete * 100)/c.numberLesson >= 100)';
  235.         }
  236.         $situationWhereString "";
  237.         foreach ($situationWhere as $key => $value) {
  238.             $situationWhereString .= (empty($situationWhereString) ? $value " OR {$value} " );
  239.         }
  240.         if(!empty($situationWhereString)){
  241.             $query->andWhere("({$situationWhereString})");
  242.         }
  243.         
  244.         if(!empty($searchText)){
  245.             $query->andWhere('c.title LIKE :searchText');
  246.             $query->setParameter('searchText''%'.$searchText.'%');
  247.         }
  248.         if(!empty($categories)){
  249.             $query->andWhere('c.category IN (:categories) ');
  250.             $query->setParameter('categories'$categories);
  251.         }
  252.         if(!empty($status)){
  253.             $query->andWhere('e.status = :status');
  254.             $query->andWhere('e.datePeriod >= :now');
  255.     
  256.             $query->setParameter('status'$status);
  257.             $query->setParameter('now'date("Y-m-d H:i:s"));
  258.         }else{
  259.             $query->andWhere('e.status = :status');
  260.             $query->setParameter('status'EnrollmentEnum::STATUS_ACTIVE);
  261.             $courseRepository $this->em->getRepository(Course::class);
  262.             
  263.             $subQuery $courseRepository->createQueryBuilder('c1');
  264.             $subQuery->select('c1.id');
  265.             $subQuery->innerJoin(
  266.                 'EADPlataforma:Product'
  267.                 'p'
  268.                 'WITH'
  269.                 'c1 MEMBER OF p.course'
  270.             );
  271.             $subQuery->andWhere('p.deleted = :productDeleted ');
  272.             $subQuery->andWhere('p.status = :productStatus');
  273.             $subQuery->andWhere('(
  274.                 p.type = :productTypePeriod OR p.type = :productTypePeriodSupport
  275.             )');
  276.             $subQuery->andWhere('c1.deleted = :c1Deleted');
  277.             $subQuery->andWhere('c1.status = :c1Status');
  278.             $query->setParameter('c1Deleted'CourseEnum::ITEM_NO_DELETED);
  279.             $query->setParameter('c1Status'CourseEnum::PUBLISHED);
  280.             $query->setParameter('productDeleted'ProductEnum::ITEM_NO_DELETED);
  281.             $query->setParameter('productStatus'ProductEnum::PUBLISHED);
  282.             $query->setParameter('productTypePeriod'ProductEnum::PERIOD);
  283.             $query->setParameter('productTypePeriodSupport'ProductEnum::PERIOD_SUPORT);
  284.             $subQuery $subQuery->getDQL();
  285.             $exp $query->expr()->in('e.course'$subQuery);
  286.             $query->andWhere("(e.datePeriod >= :now OR {$exp})");
  287.             $query->setParameter('now'date("Y-m-d H:i:s"));
  288.         }
  289.         $query->setParameter('userId'$userId);
  290.         $query->setParameter('courseStatus'CourseEnum::PUBLISHED);
  291.         $query->setParameter('courseDeleted'CourseEnum::ITEM_NO_DELETED);
  292.         $query->setFirstResult($offset);
  293.         if($limit != 0){
  294.             $query->setMaxResults($limit);
  295.         }
  296.         $query->addOrderBy('e.dateLastAccess''DESC');
  297.         
  298.         $data $query->getQuery()->execute();
  299.         return $data;
  300.     }
  301.     
  302.     public function countResumeEnrollment(
  303.         int $userId
  304.         ?array $situation = [], 
  305.         ?int $status null
  306.         ?string $searchText null
  307.         ?string $categories null
  308.     )
  309.     {
  310.         $query $this->createQueryBuilder('e');
  311.         $query->select("count(e.id) AS total");
  312.         $query->innerJoin(
  313.             'EADPlataforma:Course'
  314.             'c'
  315.             'WITH'
  316.             'e.course = c 
  317.             AND c.deleted = 0 
  318.             AND c.status = :courseStatus
  319.             AND c.dateRelease <= :now'
  320.         );
  321.         $query->andWhere('e.user = :userId');
  322.         $query->andWhere('e.deleted = 0');
  323.         
  324.         $situationWhere = [];
  325.         if(in_array(EnrollmentEnum::SITUATION_STARTED$situation)){
  326.             $situationWhere[] = '
  327.                 (
  328.                     (e.lessonNumberComplete * 100)/c.numberLesson < 100
  329.                     AND (e.lessonNumberComplete * 100)/c.numberLesson > 0
  330.                 )';
  331.         }
  332.         if(in_array(EnrollmentEnum::SITUATION_NOT_STARTED$situation)){
  333.             $situationWhere[] = '((e.lessonNumberComplete * 100)/c.numberLesson <= 0)';
  334.         }
  335.         if(in_array(EnrollmentEnum::SITUATION_COMPLETED$situation)){
  336.             $situationWhere[] = '((e.lessonNumberComplete * 100)/c.numberLesson >= 100)';
  337.         }
  338.         $situationWhereString "";
  339.         foreach ($situationWhere as $key => $value) {
  340.             $situationWhereString .= (empty($situationWhereString) ? $value " OR {$value} " );
  341.         }
  342.         if(!empty($situationWhereString)){
  343.             $query->andWhere("({$situationWhereString})");
  344.         }
  345.         if(!empty($searchText)){
  346.             $query->andWhere('c.title LIKE :searchText');
  347.             $query->setParameter('searchText''%'.$searchText.'%');
  348.         }
  349.         if(!empty($categories)){
  350.             $query->andWhere('c.category IN (:categories) ');
  351.             $query->setParameter('categories'$categories);
  352.         }
  353.         if(!empty($status)){
  354.             $query->andWhere('e.status = :status');
  355.             $query->andWhere('e.datePeriod >= :now');
  356.     
  357.             $query->setParameter('status'$status);
  358.             $query->setParameter('now'date("Y-m-d H:i:s"));
  359.         }else{
  360.             $query->andWhere('e.status = :status');
  361.             $query->setParameter('status'EnrollmentEnum::STATUS_ACTIVE);
  362.             $courseRepository $this->em->getRepository(Course::class);
  363.             
  364.             $subQuery $courseRepository->createQueryBuilder('c1');
  365.             $subQuery->select('c1.id');
  366.             $subQuery->innerJoin(
  367.                 'EADPlataforma:Product'
  368.                 'p'
  369.                 'WITH'
  370.                 'c1 MEMBER OF p.course'
  371.             );
  372.             $subQuery->andWhere('p.deleted = :productDeleted ');
  373.             $subQuery->andWhere('p.status = :productStatus');
  374.             $subQuery->andWhere('(
  375.                 p.type = :productTypePeriod OR p.type = :productTypePeriodSupport
  376.             )');
  377.             $subQuery->andWhere('c1.deleted = :c1Deleted');
  378.             $subQuery->andWhere('c1.status = :c1Status');
  379.             $query->setParameter('c1Deleted'CourseEnum::ITEM_NO_DELETED);
  380.             $query->setParameter('c1Status'CourseEnum::PUBLISHED);
  381.             $query->setParameter('productDeleted'ProductEnum::ITEM_NO_DELETED);
  382.             $query->setParameter('productStatus'ProductEnum::PUBLISHED);
  383.             $query->setParameter('productTypePeriod'ProductEnum::PERIOD);
  384.             $query->setParameter('productTypePeriodSupport'ProductEnum::PERIOD_SUPORT);
  385.             $subQuery $subQuery->getDQL();
  386.             $exp $query->expr()->in('e.course'$subQuery);
  387.             $query->andWhere("(e.datePeriod >= :now OR {$exp})");
  388.             $query->setParameter('now'date("Y-m-d H:i:s"));
  389.         }
  390.         $query->setParameter('userId'$userId);
  391.         $query->setParameter('courseStatus'CourseEnum::PUBLISHED);
  392.         $query->addOrderBy('e.dateLastAccess''DESC');
  393.         $result = (object)$query->getQuery()->getOneOrNullResult();
  394.         return $result->total;
  395.     }
  396.     public function countUserEnrollment(int $userId, ?bool $all false)
  397.     {
  398.         $query $this->createQueryBuilder('e');
  399.         $query->select("count(e.id) AS total");
  400.         $query->innerJoin(
  401.             'EADPlataforma:Course'
  402.             'c'
  403.             'WITH',
  404.             'e.course = c 
  405.             AND c.deleted = 0 
  406.             AND c.status = :courseStatus'
  407.         );
  408.         $query->andWhere('e.user = :userId');
  409.         $query->andWhere('e.deleted = 0');
  410.         if($all){
  411.             $query->andWhere('e.status = :status');
  412.             $query->andWhere('e.datePeriod >= :now');
  413.             $query->andWhere('c.dateRelease <= :now');
  414.         
  415.             $query->setParameter('status'EnrollmentEnum::STATUS_ACTIVE);
  416.             $query->setParameter('now'date("Y-m-d H:i:s"));
  417.         }   
  418.         $query->setParameter('userId'$userId);
  419.         $query->setParameter('courseStatus'CourseEnum::PUBLISHED);
  420.         $result = (object)$query->getQuery()->getOneOrNullResult();
  421.         return $result->total;
  422.     }
  423.     public function getEnrollmentNumber(
  424.         ?string $dateStart null
  425.         ?string $dateEnd null
  426.         ?int $userId null
  427.         ?int $status null
  428.     )
  429.     {
  430.         $query $this->createQueryBuilder('e');
  431.         $query->select("COUNT(0) AS total");
  432.         $query->andWhere('e.deleted = :deleted');
  433.         if($userId 0){
  434.             $subQuery $this->createQueryBuilder('course_team');
  435.             $subQuery->select('IDENTITY(ct.course)');
  436.             $subQuery->from('EADPlataforma:CourseTeam''ct');
  437.             $subQuery->andWhere('ct.user = :userId');
  438.             $subQuery $subQuery->getDQL();
  439.             $query->andWhere($query->expr()->in('e.course'$subQuery));
  440.             $query->setParameter('userId'$userId);
  441.         }
  442.         if(!is_null($dateStart) && !is_null($dateEnd)){
  443.             $query->andWhere('DATE(e.dateRegister) BETWEEN :dateStart AND :dateEnd');
  444.             $query->setParameter('dateStart'$dateStart);
  445.             $query->setParameter('dateEnd'$dateEnd);
  446.         }
  447.         if(!empty($status)){
  448.             $query->andWhere('e.status = :status');
  449.             $query->setParameter('status'$status);
  450.         }
  451.         $query->setParameter('deleted'EnrollmentEnum::ITEM_NO_DELETED);
  452.         $query->setMaxResults(1);
  453.         $result = (object)$query->getQuery()->getOneOrNullResult();
  454.         return $result->total;
  455.     }
  456.     public function checkFinishCourseByEnrollment(Enrollment $enrollment)
  457.     {
  458.         $historyMetrics $this->getHistoryMetricsByEnrollment($enrollment->getId());
  459.         $progress $historyMetrics->progress;
  460.         if($progress 100 || !empty($enrollment->getDateConclusion())){
  461.             return;
  462.         }
  463.         $user $enrollment->getUser();
  464.         $course $enrollment->getCourse();
  465.         if($course->getStatus() == CourseEnum::DRAFT){
  466.             return;
  467.         }
  468.         $marketingService $this->generalService->getService(
  469.             'Marketing\\MarketingService'
  470.         );
  471.         if(
  472.             $this->configuration->isModuleActive("exam_module") && 
  473.             $this->configuration->checkModuleIsAbleOnPlan("examFunction")
  474.         ){
  475.             $examRepository $this->em->getRepository(Exam::class);
  476.             $examUserRepository $this->em->getRepository(ExamUser::class);
  477.             $numberExamConfig $examRepository->countValidExamByCourse($course->getId());
  478.             $numberExamUserConfig $examUserRepository->countValidExamUserByCourse(
  479.                 $course->getId(),
  480.                 $user->getId()
  481.             );
  482.             if(empty($numberExamConfig)){
  483.                 $enrollment->setDateConclusion(date('Y-m-d H:i:s'));
  484.                 $marketingService->setTag(TagsMarketingEnum::TAG_FINISH_COURSE);
  485.                 $marketingService->setTextComplement($course->getTitle());
  486.                 $marketingService->setUser($this->getUser());
  487.                 $marketingService->send();
  488.             }else if($numberExamConfig == $numberExamUserConfig){
  489.                 $finalAverage $examUserRepository->getFinalAvarege(
  490.                     $user->getId(), 
  491.                     $course->getId()
  492.                 );
  493.                 $certificateAverage $course->getCertificateAverage();
  494.                 if($finalAverage >= $certificateAverage){
  495.                     $enrollment->setDateConclusion(date('Y-m-d H:i:s'));
  496.                     $marketingService->setTag(TagsMarketingEnum::TAG_FINISH_COURSE);
  497.                     $marketingService->setTextComplement($course->getTitle());
  498.                     $marketingService->setUser($this->getUser());
  499.                     $marketingService->send();
  500.                 }
  501.             }
  502.         }else{
  503.             $enrollment->setDateConclusion(date('Y-m-d H:i:s'));
  504.             $marketingService->setTag(TagsMarketingEnum::TAG_FINISH_COURSE);
  505.             $marketingService->setTextComplement($course->getTitle());
  506.             $marketingService->setUser($this->getUser());
  507.             $marketingService->send();
  508.         }
  509.         $this->em->flush();
  510.         return;
  511.     }
  512.     public function checkAndIssueCertificate(Enrollment $enrollment$finalAverage null)
  513.     {
  514.         if(!$this->configuration->isModuleActive("course_certificate_module")){
  515.             return;
  516.         }
  517.         if(!$this->configuration->checkModuleIsAbleOnPlan("certificateFunction")){
  518.             return;
  519.         }
  520.         if($enrollment->getCertificate() == EnrollmentEnum::NO){
  521.             return;
  522.         }
  523.         $historyMetrics $this->getHistoryMetricsByEnrollment($enrollment->getId());
  524.         $progress $historyMetrics->progress;
  525.         if($progress 100){
  526.             return;
  527.         }
  528.         $user $enrollment->getUser();
  529.         $course $enrollment->getCourse();
  530.         if($course->getStatus() == CourseEnum::DRAFT){
  531.             return;
  532.         }
  533.         $courseCertificateTemplate $course->getCourseCertificateTemplate();
  534.         if(!$courseCertificateTemplate){
  535.             return;
  536.         }
  537.         $courseCertificateRepository $this->em->getRepository(CourseCertificate::class);
  538.         $certificate $courseCertificateRepository->findOneBy([
  539.             "course" => $enrollment->getCourse()->getId(),
  540.             "user" => $enrollment->getUser()->getId(),
  541.             "deleted" => CourseCertificateEnum::ITEM_NO_DELETED
  542.         ]);
  543.         if($certificate){
  544.             return $certificate;
  545.         }
  546.         $create false;
  547.         if(
  548.             $this->configuration->isModuleActive("exam_module") && 
  549.             $this->configuration->checkModuleIsAbleOnPlan("examFunction")
  550.         ){
  551.             $examUserRepository $this->em->getRepository(ExamUser::class);
  552.             $examRepository $this->em->getRepository(Exam::class);
  553.             $numberExamConfig $examRepository->countValidExamByCourse($course->getId());
  554.             /*$numberExamUserConfig = $examUserRepository->countValidExamUserByCourse(
  555.                 $course->getId(),
  556.                 $user->getId()
  557.             );*/
  558.             if(empty($finalAverage)){
  559.                 $finalAverage $examUserRepository->getFinalAvarege(
  560.                     $user->getId(), 
  561.                     $course->getId()
  562.                 );
  563.             }
  564.             $certificateAverage $course->getCertificateAverage();
  565.             if(empty($numberExamConfig)){
  566.                 $finalAverage 10;
  567.                 $create true;
  568.             }else if($finalAverage >= $certificateAverage){
  569.                 $create true;
  570.             }
  571.         }else{
  572.             $finalAverage 10;
  573.             $create true;
  574.         }
  575.         if($create){
  576.             $info $courseCertificateRepository->createCertificate(
  577.                 $enrollment,
  578.                 $courseCertificateTemplate,
  579.                 $finalAverage
  580.             );
  581.             $certificate $info->courseCertificate;
  582.         }
  583.         $this->em->flush();
  584.         return $certificate;
  585.     }
  586.     public function getCourseIndexByEnrollmentNew(Enrollment $enrollment)
  587.     {   
  588.         $enrollment $this->updateDataAccessLog($enrollment);
  589.         $certificate $this->checkAndIssueCertificate($enrollment);
  590.         $this->checkFinishCourseByEnrollment($enrollment);
  591.         $addExams true;
  592.         if(
  593.             !$this->configuration->isModuleActive("exam_module") || 
  594.             !$this->configuration->checkModuleIsAbleOnPlan("examFunction")
  595.         ){
  596.             $addExams false;
  597.         }
  598.         $course $enrollment->getCourse();
  599.         $user $enrollment->getUser();
  600.         $lessonRepository $this->em->getRepository(Lesson::class);
  601.         $logOriginRepository $this->em->getRepository(LessonLogOrigin::class);
  602.         $courseRepository $this->em->getRepository(Course::class);
  603.         $isStudent $courseRepository->isStudent($course);
  604.         $lessonModules $this->getModulesByEnrollment($enrollment);
  605.         $courseTestimonialRepository $this->em->getRepository(CourseTestimonial::class);
  606.         $courseTestimonial $courseTestimonialRepository->findOneBy([
  607.             "user" => $user->getId(),
  608.             "course" => $course->getId(),
  609.             "deleted" => CourseTestimonialEnum::ITEM_NO_DELETED
  610.         ]);
  611.         $testimonial null;
  612.         if($courseTestimonial){
  613.             $testimonial = (object)[
  614.                 "id" => $courseTestimonial->getId(),
  615.                 "score" => $courseTestimonial->getScore(),
  616.                 "testimonial" => $courseTestimonial->getTestimonial(),
  617.             ];
  618.         }
  619.         $certificateLink null;
  620.         if($certificate){
  621.             $certificateLink $this->generalService->generateUrl("viewCertificate", [
  622.                 "code" => $certificate->getNewCode() ?? $certificate->getCode(),
  623.                 "date" => $certificate->getDateEnd('Y-m-d'),
  624.             ]);
  625.         }
  626.         $allowCertificate true;
  627.         if($enrollment->getCertificate() == EnrollmentEnum::NO){
  628.             $poRepository $this->em->getRepository(ProductOffer::class);
  629.             $certificateOffer $poRepository->getProductOfferCertificateByCourse(
  630.                 $course
  631.             );
  632.             $allowCertificate = ($certificateOffer true false);
  633.         }
  634.         $allowSupport = ($course->getSupport() == CourseEnum::YES);
  635.         $dateSupport $enrollment->getDateSupport();
  636.         $date date('Y-m-d H:i:s');
  637.         $data = (object)[
  638.             "id" => $course->getId(),
  639.             "status" => $course->getStatus(),
  640.             "title" => $course->getTitle(),
  641.             "allowSupport" => $allowSupport,
  642.             "supportDate" => ($allowSupport $dateSupport null),
  643.             "supportExpired" => ($allowSupport && $dateSupport $date false true),
  644.             "exam" => null,
  645.             "testimonial" => $testimonial,
  646.             "modules" => $lessonModules,
  647.             "lessonCompleted" => (int)$logOriginRepository->countLessonLogCompleteByUser(
  648.                 $course,
  649.                 $user
  650.             ),
  651.             "lessonTotal" => (int)$lessonRepository->countCourseLessons($course$user->getId()),
  652.             "allowCertificate" => $allowCertificate,
  653.             "certificateLink" => $certificateLink,
  654.         ];
  655.         if($addExams){
  656.             $examRepository $this->em->getRepository(Exam::class);
  657.             $data->exam $examRepository->getExamToIndexNew(
  658.                 $isStudent
  659.                 ExamEnum::COURSE
  660.                 $course
  661.             );
  662.         }
  663.         return $data;
  664.     }
  665.     public function getCourseIndexByEnrollment(Enrollment $enrollment$addExams false)
  666.     {   
  667.         $enrollment $this->updateDataAccessLog($enrollment);
  668.         $this->checkAndIssueCertificate($enrollment);
  669.         $this->checkFinishCourseByEnrollment($enrollment);
  670.         $addExamsLimited $addExams;
  671.         if($addExams){
  672.             if(
  673.                 !$this->configuration->isModuleActive("exam_module") || 
  674.                 !$this->configuration->checkModuleIsAbleOnPlan("examFunction")
  675.             ){
  676.                 $addExams false;
  677.                 $addExamsLimited false;
  678.             }else{
  679.                 $addExamsLimited $this->configuration->checkModuleIsAbleOnPlan(
  680.                     "unlimitedExamFunction"
  681.                 );
  682.             }
  683.         }
  684.         $certificate $this->checkAndIssueCertificate($enrollment);
  685.         $course $enrollment->getCourse();
  686.         $courseCoord $course->getUser();
  687.         $courseRepository $this->em->getRepository(Course::class);
  688.         $isStudent $courseRepository->isStudent($course);
  689.         $lessonModules $this->getHistoryByEnrollment(
  690.             $enrollment
  691.             $addExamsLimited
  692.             $isStudent
  693.         );
  694.         $today date('Y-m-d H-i-s');
  695.         $courseTestimonialRepository $this->em->getRepository(CourseTestimonial::class);
  696.         $courseTestimonial $courseTestimonialRepository->findOneBy([
  697.             "user" => $enrollment->getUser()->getId(),
  698.             "course" => $course->getId(),
  699.             "deleted" => CourseTestimonialEnum::ITEM_NO_DELETED
  700.         ]);
  701.         $timeToday strtotime(date('Y-m-d H:i:s'));
  702.         $courseDateRelease strtotime($course->getDateRelease());
  703.         $courseHasRelease = !($courseDateRelease >= $timeToday);
  704.         $courseHasCertificate = ($course->getCertificate() == CourseEnum::YES);
  705.         $certificateSale false;
  706.         $linkPdf null;
  707.         $linkBuy null;
  708.         if($courseHasCertificate){
  709.             if(!$certificate && $enrollment->getCertificate() == EnrollmentEnum::NO){
  710.                 $poRepository $this->em->getRepository(ProductOffer::class);
  711.                 $certificateOffer $poRepository->getProductOfferCertificateByCourse(
  712.                     $course
  713.                 );
  714.                 if($certificateOffer){
  715.                     $certificateSale true;
  716.                     $linkBuy $this->generalService->generateUrl("cartAdd", [
  717.                         "poID" => $certificateOffer->getId(),
  718.                         "courseId" => $course->getId(),
  719.                     ]);
  720.                 }
  721.             }
  722.         }
  723.         if($certificate){
  724.             $linkPdf $this->generalService->generateUrl("viewCertificate", [
  725.                 "code" => $certificate->getCode(),
  726.                 "date" => $certificate->getDateEnd('Y-m-d'),
  727.             ]);
  728.         }
  729.         $accessExpired CourseEnum::NO;
  730.         if(strtotime($enrollment->getDatePeriod()) < time()){
  731.             $accessExpired CourseEnum::YES;
  732.         }
  733.         $lessonSupport $course->getSupport();
  734.         $supportExpired CourseEnum::NO;
  735.         //check user has active support
  736.         if($lessonSupport == CourseEnum::YES && $isStudent){
  737.             $userDateSupport $enrollment->getDateSupport();
  738.             $userDateSupport strtotime($userDateSupport);
  739.             if($timeToday $userDateSupport){
  740.                 $supportExpired CourseEnum::YES;
  741.             }
  742.         }
  743.         $historyMetrics $this->getHistoryMetricsByEnrollment($enrollment->getId());
  744.         $data = (object)[
  745.             "id" => $course->getId(),
  746.             "status" => $course->getStatus(),
  747.             "courseTitle" => $course->getTitle(),
  748.             "courseTime" => $course->getWorkload(),
  749.             "courseDateRelease" => $course->getDateRelease(),
  750.             "courseHasRelease" => $courseHasRelease,
  751.             "courseHasCertificate" => $courseHasCertificate,
  752.             "courseAllowSupport" => $course->getSupport(),
  753.             "coorName" => $courseCoord->getName(),
  754.             "coorPhoto" => $courseCoord->getPhoto(),
  755.             "coorUsername" => $courseCoord->getUsername(),
  756.             "courseExam" => null,
  757.             "courseLessonTotal" => $historyMetrics->lessonTotal,
  758.             "enrollmentPercent" => $historyMetrics->progress,
  759.             "enrollmentLessonView" => $historyMetrics->lessonCompleted,
  760.             "enrollmentDatePeriod" => $enrollment->getDatePeriod(),
  761.             "enrollmentAllowAccess" => ($enrollment->getDatePeriod() >= $today),
  762.             "enrollmentDateSupport" => $enrollment->getDateSupport(),
  763.             "enrollmentAllowSupport" => ($enrollment->getDateSupport() >= $today),
  764.             "courseTestimonial" => (
  765.                 $courseTestimonial $courseTestimonial->toReturn() : null
  766.             ),
  767.             "lessonModules" => $lessonModules,
  768.             "certificateIssue" => ($certificate true false),
  769.             "certificateLink" => $linkPdf,
  770.             "certificateSale" => $certificateSale,
  771.             "certificateLinkBuy" => $linkBuy,
  772.             "isStudent" => $isStudent,
  773.             "accessExpired" => $accessExpired,
  774.             "supportExpired" => $supportExpired,
  775.             "lessonSupport" => $lessonSupport,
  776.         ];
  777.         if($addExams){
  778.             $examRepository $this->em->getRepository(Exam::class);
  779.             $data->courseExam $examRepository->getExamToIndex(ExamEnum::COURSE$course);
  780.         }
  781.         return $data;
  782.     }
  783.     public function getModulesByEnrollmentNew(
  784.         Enrollment $enrollment
  785.         ?bool $isStudent true
  786.         ?string $searchText null
  787.     )
  788.     {
  789.         $addExams false;
  790.         if(
  791.             $this->configuration->isModuleActive("exam_module") &&
  792.             $this->configuration->checkModuleIsAbleOnPlan("examFunction") &&
  793.             $this->configuration->checkModuleIsAbleOnPlan("unlimitedExamFunction")
  794.         ){
  795.             $addExams true;
  796.         }
  797.         $course $enrollment->getCourse();
  798.         $user $enrollment->getUser();
  799.         $data = [];
  800.         $logOriginRepository $this->em->getRepository(LessonLogOrigin::class);
  801.         $lessonRepository $this->em->getRepository(Lesson::class);
  802.         $examRepository $this->em->getRepository(Exam::class);
  803.         $lessonModuleRepository $this->em->getRepository(LessonModule::class);
  804.         $lessonModules $lessonModuleRepository->getCourseLessonModules($course);
  805.         foreach ($lessonModules as $keyModule => $lessonModule) {
  806.             $lessonsSearch $this->getHistoryByEnrollmentModuleNew(
  807.                 $enrollment,
  808.                 $lessonModule,
  809.                 $addExams,
  810.                 $isStudent,
  811.                 $searchText
  812.             );
  813.             if(!empty($lessonsSearch)){
  814.                 $lessonModuleObj = (object)[
  815.                     "id" => $lessonModule->getId(),
  816.                     "title" => $lessonModule->getTitle(),
  817.                     "description" => $lessonModule->getDescription(),
  818.                     "status" => $lessonModule->getStatus(),
  819.                     "lessonCompleted" => (int)$logOriginRepository->countLessonLogCompleteByUser(
  820.                         $course,
  821.                         $user,
  822.                         $lessonModule
  823.                     ),
  824.                     "lessonTotal" => (int)$lessonRepository->countCourseLessons(
  825.                         $course,
  826.                         $user->getId(),
  827.                         $lessonModule
  828.                     ),
  829.                     "exam" => null,
  830.                     "lessons" => $lessonsSearch,
  831.                 ];
  832.                 if($addExams){
  833.                     $lessonModuleObj->exam $examRepository->getExamToIndexNew(
  834.                         $isStudent,
  835.                         ExamEnum::MODULE
  836.                         $course,
  837.                         $lessonModule
  838.                     );
  839.                 }
  840.                 
  841.                 $data[] = $lessonModuleObj;
  842.             }
  843.         }
  844.         return $data;
  845.     }
  846.     public function getModuleIndexByEnrollmentNew(
  847.         Enrollment $enrollment
  848.         LessonModule $lessonModule,
  849.         ?bool $isStudent true
  850.     )
  851.     {
  852.         $addExams false;
  853.         if(
  854.             $this->configuration->isModuleActive("exam_module") &&
  855.             $this->configuration->checkModuleIsAbleOnPlan("examFunction") &&
  856.             $this->configuration->checkModuleIsAbleOnPlan("unlimitedExamFunction")
  857.         ){
  858.             $addExams true;
  859.         }
  860.         $course $enrollment->getCourse();
  861.         $data = (object)[
  862.             "id" => $lessonModule->getId(),
  863.             "title" => $lessonModule->getTitle(),
  864.             "description" => $lessonModule->getDescription(),
  865.             "status" => $lessonModule->getStatus(),
  866.             "exam" => null,
  867.             "lessons" => $this->getHistoryByEnrollmentModuleNew(
  868.                 $enrollment,
  869.                 $lessonModule,
  870.                 $addExams,
  871.                 $isStudent
  872.             ),
  873.         ];
  874.         if($addExams){
  875.             $examRepository $this->em->getRepository(Exam::class);
  876.             $data->exam $examRepository->getExamToIndexNew(
  877.                 $isStudent,
  878.                 ExamEnum::MODULE
  879.                 $course,
  880.                 $lessonModule
  881.             );
  882.         }
  883.         return $data;
  884.     }
  885.     public function getModulesByEnrollment(Enrollment $enrollment)
  886.     {
  887.         $lessonModuleRepository $this->em->getRepository(LessonModule::class);
  888.         $lessonRepository $this->em->getRepository(Lesson::class);
  889.         $logOriginRepository $this->em->getRepository(LessonLogOrigin::class);
  890.         $examRepository $this->em->getRepository(Exam::class);
  891.         
  892.         $course $enrollment->getCourse();
  893.         $user $enrollment->getUser();
  894.         $data = [];
  895.         $lessonModules $lessonModuleRepository->getCourseLessonModules($course);
  896.         foreach ($lessonModules as $keyModule => $lessonModule) {
  897.             $lessonModuleObj = (object)[
  898.                 "id" => $lessonModule->getId(),
  899.                 "title" => $lessonModule->getTitle(),
  900.                 "description" => $lessonModule->getDescription(),
  901.                 "status" => $lessonModule->getStatus(),
  902.                 "lessonCompleted" => (int)$logOriginRepository->countLessonLogCompleteByUser(
  903.                     $course,
  904.                     $user,
  905.                     $lessonModule
  906.                 ),
  907.                 "lessonTotal" => (int)$lessonRepository->countCourseLessons(
  908.                     $course,
  909.                     $user->getId(),
  910.                     $lessonModule
  911.                 ),
  912.             ];
  913.             if($lessonModuleObj->lessonCompleted $lessonModuleObj->lessonTotal){
  914.                 $lessonModuleObj->lessonCompleted $lessonModuleObj->lessonTotal;
  915.             }
  916.             
  917.             $data[] = $lessonModuleObj;
  918.         }
  919.         return $data;
  920.     }
  921.     public function getHistoryMetricsByEnrollment($enrollmentId)
  922.     {
  923.         $lessonModuleRepository $this->em->getRepository(LessonModule::class);
  924.         $lessonRepository $this->em->getRepository(Lesson::class);
  925.         $lessonLogRepository $this->emEadmin->getRepository(LessonLog::class);
  926.         $lessonSupportRepository $this->em->getRepository(LessonSupport::class);
  927.         $dateTimeUtil $this->generalService->getUtil('DateTimeUtil');
  928.         $enrollment $this->findOneBy([
  929.             "id" => $enrollmentId,
  930.             "deleted" => EnrollmentEnum::ITEM_NO_DELETED
  931.         ]);
  932.         $course $enrollment->getCourse();
  933.         $user $enrollment->getUser();
  934.         $lessonModules $lessonModuleRepository->getCourseLessonModules($course);
  935.         $lessons $lessonRepository->getCourseLessons($course);
  936.         $auxLogs = [];
  937.         $dataLesson = [];
  938.         $dataModule = [];
  939.         
  940.         $lessonLogs $lessonLogRepository->findBy(
  941.             [
  942.               "user" => $user->getId(),
  943.               "course" => $course->getId(),
  944.             ],
  945.            null
  946.            null
  947.            null
  948.            [ $course->getId(), "{$user->getId()}#" ]
  949.         );
  950.         foreach ($lessonLogs as $key => $log) {
  951.             if($log->getLesson()){
  952.                 $auxLogs[$log->getLesson()->getId()] = $log;
  953.             }
  954.         }
  955.         foreach ($lessonModules as $keyModule => $lessonModule) {
  956.             $lessonModuleObj = (object)[
  957.                 "id" => $lessonModule->getId(),
  958.                 "lessonCompleted" => 0,
  959.                 "lessonTotal" => 0
  960.             ];
  961.             foreach ($lessons as $keyLesson => $lesson) {
  962.                 if($lessonModuleObj->id == $lesson->getLessonModule()->getId()){
  963.                     if($lesson->getStatus() == LessonEnum::PUBLISHED){
  964.                         $lessonModuleObj->lessonTotal $lessonModuleObj->lessonTotal 1;
  965.                     }
  966.                     $lessonObj = (object)[
  967.                         "timeWatch" => null,
  968.                         "numberAccess" => null,
  969.                         "numberSupport" => null,
  970.                     ];
  971.                     $lessonLog = ( 
  972.                         isset($auxLogs[$lesson->getId()]) ?
  973.                         $auxLogs[$lesson->getId()] :
  974.                         null
  975.                     );
  976.                 
  977.                     if($lessonLog){
  978.                         if($lessonLog->getComplete() == LessonEnum::YES){
  979.                             $lessonModuleObj->lessonCompleted $lessonModuleObj->lessonCompleted 1;
  980.                         }
  981.                         $lessonObj->numberSupport $lessonSupportRepository->count([
  982.                             "user" => $user->getId(),
  983.                             "lesson" => $lesson->getId(),
  984.                             "lessonSupport" => null,
  985.                         ]);
  986.                         $lessonObj->timeWatch $lessonLog->getTimeWatch();
  987.                         $lessonObj->numberAccess $lessonLog->getNumberAccess();
  988.                     }
  989.                     $dataLesson[] = $lessonObj;
  990.                 }
  991.             }
  992.             if($lessonModuleObj->lessonCompleted $lessonModuleObj->lessonTotal){
  993.                 $lessonModuleObj->lessonCompleted $lessonModuleObj->lessonTotal;
  994.             }
  995.             $dataModule[] = $lessonModuleObj;
  996.         }
  997.         $timeWatch 0;
  998.         $numberAccess 0;
  999.         $numberSupport 0;
  1000.         $lessonCompleted 0;
  1001.         $lessonTotal 0;
  1002.         $progress 0;
  1003.         foreach ($dataLesson as $key => $value) {
  1004.             $seconds $dateTimeUtil->timeToSec($value->timeWatch);
  1005.             $timeWatch $timeWatch $seconds;
  1006.             $numberAccess $numberAccess $value->numberAccess;
  1007.             $numberSupport $numberSupport $value->numberSupport;
  1008.         }
  1009.         foreach ($dataModule as $key => $value) {
  1010.             $lessonCompleted $lessonCompleted $value->lessonCompleted;
  1011.             $lessonTotal $lessonTotal $value->lessonTotal;
  1012.         }
  1013.         $timeWatch $dateTimeUtil->secToTime($timeWatch);
  1014.         if(!empty($lessonTotal)){
  1015.             $progress round(($lessonCompleted $lessonTotal) * 1000);
  1016.         }
  1017.         return (object)[
  1018.             "timeWatch" => $timeWatch,
  1019.             "numberAccess" => $numberAccess,
  1020.             "numberSupport" => $numberSupport,
  1021.             "lessonCompleted" => $lessonCompleted,
  1022.             "lessonTotal" => $lessonTotal,
  1023.             "progress" => $progress
  1024.         ];
  1025.     }
  1026.     public function getHistoryByEnrollment(
  1027.         Enrollment $enrollment
  1028.         ?bool $addExams false
  1029.         ?bool $isStudent true
  1030.     )
  1031.     {
  1032.         $lessonModuleRepository $this->em->getRepository(LessonModule::class);
  1033.         $lessonRepository $this->em->getRepository(Lesson::class);
  1034.         $lessonLogRepository $this->emEadmin->getRepository(LessonLog::class);
  1035.         $lessonSupportRepository $this->em->getRepository(LessonSupport::class);
  1036.         $lessonAnnotationRepository $this->em->getRepository(LessonAnnotation::class);
  1037.         $examRepository $this->em->getRepository(Exam::class);
  1038.         
  1039.         $stringUtil $this->generalService->getUtil('StringUtil');
  1040.         $course $enrollment->getCourse();
  1041.         $user $enrollment->getUser();
  1042.         $data = [];
  1043.         $lessonModules $lessonModuleRepository->getCourseLessonModules($course);
  1044.         $lessons $lessonRepository->getCourseLessons($course);
  1045.         $auxLogs = [];
  1046.         $lessonLogs $lessonLogRepository->findBy(
  1047.             [
  1048.               "user" => $user->getId(),
  1049.               "course" => $course->getId(),
  1050.             ],
  1051.            null
  1052.            null
  1053.            null
  1054.            [ $course->getId(), "{$user->getId()}#" ]
  1055.         );
  1056.         foreach ($lessonLogs as $key => $log) {
  1057.             $auxLogs[$log->getLesson()->getId()] = $log;
  1058.         }
  1059.         $examModule $this->configuration->isModuleActive("exam_module");
  1060.         $blockNextLesson false;
  1061.         $lessonIdBefore null;
  1062.         foreach ($lessonModules as $keyModule => $lessonModule) {
  1063.             $lessonModuleObj = (object)[
  1064.                 "id" => $lessonModule->getId(),
  1065.                 "courseId" => $course->getId(),
  1066.                 "title" => $lessonModule->getTitle(),
  1067.                 "order" => $lessonModule->getOrder(),
  1068.                 "status" => $lessonModule->getStatus(),
  1069.                 "controlRequirement" => $lessonModule->getControlRequirement(),
  1070.                 "controlReleaseType" => $lessonModule->getControlReleaseType(),
  1071.                 "controlReleaseAfterType" => $lessonModule->getControlReleaseAfterType(),
  1072.                 "controlDateRelease" => $lessonModule->getControlDateRelease(),
  1073.                 "controlReleasePeriod" => $lessonModule->getControlReleasePeriod(),
  1074.                 "controlClosePeriod" => $lessonModule->getControlClosePeriod(),
  1075.                 "moduleIsAccessible" => false,
  1076.                 "lessonCompleted" => 0,
  1077.                 "lessonTotal" => 0,
  1078.                 "exam" => null,
  1079.                 "lessons" => [],
  1080.             ];
  1081.             if($addExams){
  1082.                 $lessonModuleObj->exam $examRepository->getExamToIndex(
  1083.                     ExamEnum::MODULE$course$lessonModule
  1084.                 );
  1085.             }
  1086.             foreach ($lessons as $keyLesson => $lesson) {
  1087.                 if($lessonModuleObj->id == $lesson->getLessonModule()->getId()){
  1088.                     if($lesson->getStatus() == LessonEnum::PUBLISHED){
  1089.                         $lessonModuleObj->lessonTotal $lessonModuleObj->lessonTotal 1;
  1090.                     }
  1091.                     $lessonLog = ( 
  1092.                         isset($auxLogs[$lesson->getId()]) ?
  1093.                         $auxLogs[$lesson->getId()] :
  1094.                         null
  1095.                     );
  1096.                     $lessonIsAccessible $lessonRepository->checkLessonIsAccessibleToUser(
  1097.                         $lesson,
  1098.                         $enrollment,
  1099.                         $lessonLog,
  1100.                         $isStudent,
  1101.                         $lessonIdBefore,
  1102.                         $blockNextLesson
  1103.                     );
  1104.                     if(
  1105.                         !$lessonIsAccessible->isAccessible && 
  1106.                         $lesson->getControlRequirement() == LessonEnum::YES
  1107.                     ){
  1108.                         $blockNextLesson true;
  1109.                     }
  1110.                     if(empty($lessonModuleObj->lessons)){
  1111.                         $lessonModuleObj->moduleIsAccessible $lessonIsAccessible->isAccessible;
  1112.                     }
  1113.                     $dateReleaseAccess $lessonRepository->getLessonDateReleaseAccess(
  1114.                         $lesson,
  1115.                         $enrollment,
  1116.                         $isStudent,
  1117.                         $lessonIdBefore
  1118.                     );
  1119.                     $lessonIdBefore $lesson->getId();
  1120.                     $contentPagesNumber null;
  1121.                     $contentDuration null;
  1122.                     $contentType null;
  1123.                     $library $lesson->getLibrary();
  1124.                     if($library){
  1125.                         $contentPagesNumber $library->getPagesNumber();
  1126.                         $contentDuration $library->getDuration();
  1127.                         $contentType $library->getType();
  1128.                     }
  1129.                     $lessonObj = (object)[
  1130.                         "id" => $lesson->getId(),
  1131.                         "courseId" => $course->getId(),
  1132.                         "lessonModuleId" => $lessonModuleObj->id,
  1133.                         "title" => $lesson->getTitle(),
  1134.                         "order" => $lesson->getOrder(),
  1135.                         "status" => $lesson->getStatus(),
  1136.                         "lessonIsAccessible" => $lessonIsAccessible->isAccessible,
  1137.                         "dateReleaseAccess" => $dateReleaseAccess,
  1138.                         "exam" => null,
  1139.                         "quiz" => null,
  1140.                         "timeWatch" => null,
  1141.                         "numberAccess" => null,
  1142.                         "numberSupport" => null,
  1143.                         "dateLastAccess" => null,
  1144.                         "dateConclusion" => null,
  1145.                         "contentPagesNumber" => $contentPagesNumber,
  1146.                         "contentDuration" => $contentDuration,
  1147.                         "contentType" => $contentType,
  1148.                         "allowCheck" => EnrollmentEnum::YES,
  1149.                         "controlRequirement" => $lesson->getControlRequirement(),
  1150.                         "controlReleaseType" => $lesson->getControlReleaseType(),
  1151.                         "controlReleaseAfterType" => $lesson->getControlReleaseAfterType(),
  1152.                         "controlDateRelease" => $lesson->getControlDateRelease(),
  1153.                         "controlReleasePeriod" => $lesson->getControlReleasePeriod(),
  1154.                         "controlClosePeriod" => $lesson->getControlClosePeriod(),
  1155.                         "controlTime" => (
  1156.                             $lesson->getControlRequirement() ? 
  1157.                             $lesson->getControlTime() : 
  1158.                             LessonEnum::NO
  1159.                         ),
  1160.                         "controlTimeStay" => $lesson->getControlTimeStay(),
  1161.                         "controlViewLimit" => $lesson->getControlViewLimit(),
  1162.                         "controlViewNumber" => $lesson->getControlViewNumber(),
  1163.                         "controlPauseNumber" => $lesson->getControlPauseNumber(),
  1164.                     ];
  1165.                     if($lesson->getControlRequirement() == LessonEnum::YES){
  1166.                         $lessonObj->allowCheck LessonEnum::NO;
  1167.                     }
  1168.                     if(
  1169.                         !empty($lesson->getControlTimeStay()) && 
  1170.                         $lesson->getControlTimeStay() != '00:00:00'
  1171.                     ){
  1172.                         $lessonObj->allowCheck LessonEnum::NO;
  1173.                     }
  1174.                     if($addExams){
  1175.                         $lessonObj->exam $examRepository->getExamToIndex(
  1176.                             ExamEnum::LESSON
  1177.                             $course
  1178.                             $lessonModule
  1179.                             $lesson
  1180.                         );
  1181.                     }
  1182.                     $lessonObj->quiz $examRepository->getExamToIndex(
  1183.                         ExamEnum::QUIZ,
  1184.                         $course,
  1185.                         $lessonModule,
  1186.                         $lesson
  1187.                     );
  1188.                     $lessonObj->numberSupport $lessonSupportRepository->count([
  1189.                         "user" => $user->getId(),
  1190.                         "lesson" => $lesson->getId(),
  1191.                         "lessonSupport" => null,
  1192.                     ]);
  1193.                     $lessonObj->supports = [];
  1194.                     if(!empty($lessonObj->numberSupport)){
  1195.                         $supports $lessonSupportRepository->findBy([
  1196.                             "user" => $user->getId(),
  1197.                             "lesson" => $lesson->getId(),
  1198.                             "lessonSupport" => null,
  1199.                             "deleted" => LessonSupportEnum::ITEM_NO_DELETED
  1200.                         ]);
  1201.                         foreach ($supports  as $key => $support) {
  1202.                             $txt strip_tags(html_entity_decode($support->getSupport()));
  1203.                             $txt str_replace("\xc2\xa0"''$txt);
  1204.                             
  1205.                             $lessonObj->supports[] = (object)[
  1206.                                 "id" => $support->getId(),
  1207.                                 "date" => $support->getDate(),
  1208.                                 "support" => $stringUtil->shortTextCleanNew($txt),
  1209.                             ];
  1210.                         }
  1211.                     }
  1212.                     $lessonObj->notes = [];
  1213.                     $lessonNotes $lessonAnnotationRepository->findBy([
  1214.                         "user" => $user->getId(),
  1215.                         "lesson" => $lesson->getId(),
  1216.                         "deleted" => LessonAnnotationEnum::ITEM_NO_DELETED
  1217.                     ], [ "id" => "DESC" ]);
  1218.                     if(!empty($lessonNotes)){
  1219.                         foreach ($lessonNotes  as $key => $note) {
  1220.                             $lessonObj->notes[] = (object)[
  1221.                                 "id" => $note->getId(),
  1222.                                 "date" => $note->getDate(),
  1223.                                 "time" => $note->getTime(),
  1224.                                 "note" => $note->getAnnotation(),
  1225.                                 "options" => $note->getOptions()
  1226.                             ];
  1227.                         }
  1228.                     }
  1229.                     if($lessonLog){
  1230.                         if($lessonLog->getComplete() == LessonEnum::YES){
  1231.                             $lessonModuleObj->lessonCompleted $lessonModuleObj->lessonCompleted 1;
  1232.                         }
  1233.                         $lessonObj->complete $lessonLog->getComplete();
  1234.                         $lessonObj->favorite $lessonLog->getFavorite();
  1235.                         $lessonObj->timeWatch $lessonLog->getTimeWatch();
  1236.                         $lessonObj->numberAccess $lessonLog->getNumberAccess();
  1237.                         $lessonObj->dateLastAccess $lessonLog->getDateAccess();
  1238.                         $lessonObj->dateConclusion $lessonLog->getDateConclusion();
  1239.                         if(!empty($lessonObj->dateConclusion)){
  1240.                             $lessonObj->allowCheck LessonEnum::YES;
  1241.                         }
  1242.                     }
  1243.                     $lessonModuleObj->lessons[] = $lessonObj;
  1244.                 }
  1245.             }
  1246.             if($lessonModuleObj->lessonCompleted $lessonModuleObj->lessonTotal){
  1247.                 $lessonModuleObj->lessonCompleted $lessonModuleObj->lessonTotal;
  1248.             }
  1249.             $data[] = $lessonModuleObj;
  1250.         }
  1251.         return $data;
  1252.     }
  1253.     public function getHistoryByEnrollmentModuleNew(
  1254.         Enrollment $enrollment
  1255.         LessonModule $lessonModule,
  1256.         ?bool $addExams false
  1257.         ?bool $isStudent true,
  1258.         ?string $searchText null
  1259.     )
  1260.     {
  1261.         $lessonRepository $this->em->getRepository(Lesson::class);
  1262.         $libraryRepository $this->em->getRepository(Library::class);
  1263.         $lessonLogRepository $this->emEadmin->getRepository(LessonLog::class);
  1264.         $examRepository $this->em->getRepository(Exam::class);
  1265.         
  1266.         $stringUtil $this->generalService->getUtil('StringUtil');
  1267.         $course $enrollment->getCourse();
  1268.         $user $enrollment->getUser();
  1269.         $data = [];
  1270.         $lessons $lessonRepository->getCourseLessons(
  1271.             $course
  1272.             $lessonModule
  1273.             null
  1274.             $searchText
  1275.         );
  1276.         $blockNextLesson false;
  1277.         $lessonIdBefore null;
  1278.         $timeUtil $this->generalService->getUtil('DateTimeUtil');
  1279.         
  1280.         foreach ($lessons as $keyLesson => $lesson) {
  1281.             $logId "{$course->getId()}#{$user->getId()}#{$lesson->getId()}";
  1282.             $lessonLog $lessonLogRepository->find($logId);
  1283.             $lessonIsAccessible $lessonRepository->checkLessonIsAccessibleToUser(
  1284.                 $lesson,
  1285.                 $enrollment,
  1286.                 $lessonLog,
  1287.                 $isStudent,
  1288.                 $lessonIdBefore,
  1289.                 $blockNextLesson
  1290.             );
  1291.             if($lessonIsAccessible->isAccessPeriodExpired){
  1292.                 continue;
  1293.             }
  1294.             if(
  1295.                 !$lessonIsAccessible->isAccessible && 
  1296.                 $lesson->getControlRequirement() == LessonEnum::YES
  1297.             ){
  1298.                 $blockNextLesson true;
  1299.             }
  1300.             /*$dateReleaseAccess = $lessonRepository->getLessonDateReleaseAccess(
  1301.                 $lesson,
  1302.                 $enrollment,
  1303.                 $isStudent,
  1304.                 $lessonIdBefore
  1305.             );*/
  1306.             $lessonIdBefore $lesson->getId();
  1307.             $contentPagesNumber null;
  1308.             $contentDuration null;
  1309.             $contentType null;
  1310.             $contentThumb null;
  1311.             $library $lesson->getLibrary();
  1312.             if($library){
  1313.                 $contentPagesNumber $library->getPagesNumber();
  1314.                 $contentDuration $library->getDuration();
  1315.                 $contentType $library->getType();
  1316.                 $contentThumb $libraryRepository->getCover($library);
  1317.             }
  1318.             $lessonObj = (object)[
  1319.                 "id" => $lesson->getId(),
  1320.                 "title" => $lesson->getTitle(),
  1321.                 "status" => $lesson->getStatus(),
  1322.                 "required" => $lesson->getControlRequirement(),
  1323.                 "lessonIsAccessible" => $lessonIsAccessible->isAccessible,
  1324.                 "acessMessage" => $lessonIsAccessible->message,
  1325.                 "contentPagesNumber" => $contentPagesNumber,
  1326.                 "contentDuration" => (
  1327.                     $contentDuration $timeUtil->timeToSec($contentDuration) : null
  1328.                 ),
  1329.                 "contentType" => $contentType,
  1330.                 "contentThumb" => $contentThumb,
  1331.                 "exam" => null,
  1332.                 "quiz" => null,
  1333.                 "allowCheck" => EnrollmentEnum::YES,
  1334.                 "completed" => LessonEnum::NO,
  1335.             ];
  1336.             if($lessonLog && $lessonLog->getComplete() == LessonEnum::YES){
  1337.                 $lessonObj->completed $lessonLog->getViewed();
  1338.             }
  1339.             if($lesson->getControlRequirement() == LessonEnum::YES){
  1340.                 $lessonObj->allowCheck LessonEnum::NO;
  1341.             }
  1342.             if(
  1343.                 !empty($lesson->getControlTimeStay()) && 
  1344.                 $lesson->getControlTimeStay() != '00:00:00'
  1345.             ){
  1346.                 $lessonObj->allowCheck LessonEnum::NO;
  1347.             }
  1348.             if($addExams){
  1349.                 $lessonObj->exam $examRepository->getExamToIndexNew(
  1350.                     $isStudent,
  1351.                     ExamEnum::LESSON
  1352.                     $course
  1353.                     $lessonModule
  1354.                     $lesson
  1355.                 );
  1356.             }
  1357.             $lessonObj->quiz $examRepository->getExamToIndexNew(
  1358.                 $isStudent,
  1359.                 ExamEnum::QUIZ,
  1360.                 $course,
  1361.                 $lessonModule,
  1362.                 $lesson
  1363.             );
  1364.             
  1365.             $data[] = $lessonObj;
  1366.         }
  1367.         return $data;
  1368.     }
  1369.     public function getExpiredEnrollmentsByDate($date)
  1370.     {
  1371.         $query $this->createQueryBuilder('e');
  1372.         $query->andWhere('e.deleted = 0');
  1373.         $query->andWhere('e.datePeriod = :date');
  1374.         $query->setParameter('date'$date);
  1375.         $query->andWhere('e.status = :status');
  1376.         $query->setParameter('status'EnrollmentEnum::STATUS_ACTIVE);
  1377.         return $query->getQuery()->execute();
  1378.     }
  1379.     public function isValidEnrollmentByUser($userId$courseId)
  1380.     {
  1381.         $query $this->createQueryBuilder('e');
  1382.         
  1383.         $query->innerJoin(
  1384.             'EADPlataforma:Course''c'
  1385.             'WITH''e.course = c AND c.deleted = :deletedCourse'
  1386.         );
  1387.         $query->andWhere('e.deleted = :deleted');
  1388.         $query->andWhere('e.user = :userId');
  1389.         $query->andWhere('e.course = :courseId');
  1390.         if($userId != EnrollmentEnum::YES){
  1391.             $query->andWhere('e.status = :status');
  1392.             $query->setParameter('status'EnrollmentEnum::STATUS_ACTIVE);
  1393.             $query->andWhere('e.datePeriod >= :now');
  1394.             $query->setParameter('now'date('Y-m-d H:i:s'));
  1395.         }
  1396.         $query->setParameter('deletedCourse'CourseEnum::ITEM_NO_DELETED);
  1397.         $query->setParameter('deleted'EnrollmentEnum::ITEM_NO_DELETED);
  1398.         $query->setParameter('userId'$userId);
  1399.         $query->setParameter('courseId'$courseId);
  1400.         $query->select('COUNT(0) AS total');
  1401.         $result = (object)$query->getQuery()->getOneOrNullResult();
  1402.         return ($result->total 0);
  1403.     }
  1404.     public function isEnrollment($userId$courseId)
  1405.     {
  1406.         $query $this->createQueryBuilder('e');
  1407.         $query->innerJoin(
  1408.             'EADPlataforma:Course''c'
  1409.             'WITH''e.course = c AND c.deleted = :deletedCourse'
  1410.         );
  1411.         $query->andWhere('e.deleted = :deleted');
  1412.         $query->andWhere('e.user = :userId');
  1413.         $query->andWhere('e.course = :courseId');
  1414.         $query->andWhere('e.status = :status');
  1415.         $query->setParameter('deletedCourse'CourseEnum::ITEM_NO_DELETED);
  1416.         $query->setParameter('deleted'EnrollmentEnum::ITEM_NO_DELETED);
  1417.         $query->setParameter('userId'$userId);
  1418.         $query->setParameter('courseId'$courseId);
  1419.         $query->setParameter('status'EnrollmentEnum::STATUS_ACTIVE);
  1420.         $query->select('COUNT(0) AS total');
  1421.         $result = (object)$query->getQuery()->getOneOrNullResult();
  1422.         return ($result->total 0);
  1423.     }
  1424.     public function countEnrollmentsByCourse(
  1425.         int $courseId
  1426.         ?int $status null
  1427.         ?int $startType null
  1428.     )
  1429.     {
  1430.         $query $this->createQueryBuilder('e');
  1431.         $query->select("count(e.id) AS total");
  1432.         $query->andWhere('e.deleted = 0');
  1433.         $query->andWhere('e.course = :courseId');
  1434.         $query->setParameter('courseId'$courseId);
  1435.         if(!empty($status)){
  1436.             if($status == EnrollmentEnum::STATUS_EXPIRED){
  1437.                 $query->andWhere('e.status = :status');
  1438.                 $query->setParameter('status'EnrollmentEnum::STATUS_ACTIVE);
  1439.                 $query->andWhere('e.datePeriod <= :now');
  1440.                 $query->setParameter('now'date('Y-m-d H:i:s'));
  1441.             }else if($status == EnrollmentEnum::STATUS_ACTIVE){
  1442.                 $query->andWhere('e.status = :status');
  1443.                 $query->setParameter('status'EnrollmentEnum::STATUS_ACTIVE);
  1444.                 $query->andWhere('e.datePeriod >= :now');
  1445.                 $query->setParameter('now'date('Y-m-d H:i:s'));
  1446.             }else{
  1447.                 $query->andWhere('e.status = :status');
  1448.                 $query->setParameter('status'$status);
  1449.             }
  1450.         }
  1451.         if($startType == EnrollmentEnum::SITUATION_STARTED){
  1452.             $query->andWhere('e.dateStart IS NOT NULL');
  1453.         }else if($startType == EnrollmentEnum::SITUATION_NOT_STARTED){
  1454.             $query->andWhere('e.dateStart IS NULL');
  1455.         }
  1456.         $result = (object)$query->getQuery()->getOneOrNullResult();
  1457.         return $result->total;
  1458.     }
  1459.     public function getEnrollmentsByCourse(
  1460.         int $courseId
  1461.         ?int $status null
  1462.         $startType null
  1463.     )
  1464.     {
  1465.         $query $this->createQueryBuilder('e');
  1466.         $query->andWhere('e.deleted = 0');
  1467.         $query->andWhere('e.course = :courseId');
  1468.         $query->setParameter('courseId'$courseId);
  1469.         if(!empty($status)){
  1470.             if($status == EnrollmentEnum::STATUS_EXPIRED){
  1471.                 $query->andWhere('e.status = :status');
  1472.                 $query->setParameter('status'EnrollmentEnum::STATUS_ACTIVE);
  1473.                 $query->andWhere('e.datePeriod <= :now');
  1474.                 $query->setParameter('now'date('Y-m-d H:i:s'));
  1475.             }else if($status == EnrollmentEnum::STATUS_ACTIVE){
  1476.                 $query->andWhere('e.status = :status');
  1477.                 $query->setParameter('status'EnrollmentEnum::STATUS_ACTIVE);
  1478.                 $query->andWhere('e.datePeriod >= :now');
  1479.                 $query->setParameter('now'date('Y-m-d H:i:s'));
  1480.             }else{
  1481.                 $query->andWhere('e.status = :status');
  1482.                 $query->setParameter('status'$status);
  1483.             }
  1484.         }
  1485.         if($startType == EnrollmentEnum::SITUATION_STARTED){
  1486.             $query->andWhere('e.dateStart IS NOT NULL');
  1487.         }else if($startType == EnrollmentEnum::SITUATION_NOT_STARTED){
  1488.             $query->andWhere('e.dateStart IS NULL');
  1489.         }
  1490.         return $query->getQuery()->execute();
  1491.     }
  1492.     public function getEnrollmentsByGroup(
  1493.         int $groupId
  1494.         ?int $status null
  1495.         ?int $userId null
  1496.         ?int $courseId null
  1497.     )
  1498.     {
  1499.         $query $this->createQueryBuilder('e');
  1500.         $query->innerJoin(
  1501.             'EADPlataforma:Group''g''WITH'
  1502.             'e.course MEMBER OF g.course AND e.user MEMBER OF g.user'
  1503.         );
  1504.         $query->andWhere('e.deleted = :deleted');
  1505.         $query->andWhere('g.id = :groupId');
  1506.         $query->setParameter('groupId'$groupId);
  1507.         $query->setParameter('deleted'EnrollmentEnum::ITEM_NO_DELETED);
  1508.         if(!empty($status)){
  1509.             if($status == EnrollmentEnum::STATUS_EXPIRED){
  1510.                 $query->andWhere('e.datePeriod < :now');
  1511.                 $query->setParameter('now'date('Y-m-d H:i:s'));
  1512.                 $query->andWhere('e.status = :status');
  1513.                 $query->setParameter('status'EnrollmentEnum::STATUS_ACTIVE);
  1514.             }else{
  1515.                 $query->andWhere('e.status = :status');
  1516.                 $query->setParameter('status'$status);
  1517.             }
  1518.         }
  1519.         if($userId 0){
  1520.             $query->andWhere('e.user = :userId');
  1521.             $query->setParameter('userId'$userId);
  1522.         }
  1523.         if($courseId 0){
  1524.             $query->andWhere('e.course = :courseId');
  1525.             $query->setParameter('courseId'$courseId);
  1526.         }
  1527.         
  1528.         return $query->getQuery()->execute();
  1529.     }
  1530.     public function getEnrollmentsToEditMany(
  1531.         ?array $users null
  1532.         $courseId null
  1533.         $groupId null
  1534.         $productId null
  1535.         $teacherId null
  1536.         $applyTo null
  1537.     )
  1538.     {
  1539.         $query $this->createQueryBuilder('e');
  1540.         $query->andWhere('e.deleted = 0');
  1541.         if(!empty($users) && is_array($users)){
  1542.             $users implode(','$users);
  1543.             $query->andWhere("e.user IN ({$users})");
  1544.         }
  1545.         if($courseId 0){
  1546.             $query->andWhere('e.course = :courseId')->setParameter('courseId'$courseId);
  1547.         }
  1548.         if($groupId 0){
  1549.             $query->innerJoin(
  1550.                 'EADPlataforma:Group''g''WITH'
  1551.                 'e.course MEMBER OF g.course AND e.user MEMBER OF g.user'
  1552.             );
  1553.             $query->andWhere('g.id = :groupId');
  1554.             $query->setParameter('groupId'$groupId);
  1555.         }
  1556.         if($productId 0){
  1557.             $query->innerJoin(
  1558.                 'EADPlataforma:Product''p''WITH''e.course MEMBER OF p.course '
  1559.             );
  1560.             $query->andWhere('p.id = :productId');
  1561.             $query->setParameter('productId'$productId);
  1562.         }
  1563.         if($teacherId 0){
  1564.             $subQuery $this->createQueryBuilder('course_team');
  1565.             $subQuery->select('IDENTITY(ct.course)');
  1566.             $subQuery->from('EADPlataforma:CourseTeam''ct');
  1567.             $subQuery->andWhere('ct.user = :teacherId');
  1568.             $subQuery $subQuery->getDQL();
  1569.             $query->andWhere($query->expr()->in('e.course'$subQuery));
  1570.             $query->setParameter('teacherId'$teacherId);
  1571.         }
  1572.         if(empty($applyTo)){
  1573.             $apply EnrollmentEnum::APPLY_TO_ALL;
  1574.         }
  1575.         switch ($applyTo) {
  1576.             case EnrollmentEnum::APPLY_TO_EXPIRED
  1577.                 $query->andWhere('e.datePeriod < :now');
  1578.                 $query->setParameter('now'date('Y-m-d H:i:s'));
  1579.                 break;
  1580.             case EnrollmentEnum::APPLY_TO_VALID_PERIOD
  1581.                 $query->andWhere('e.datePeriod >= :now');
  1582.                 $query->setParameter('now'date('Y-m-d H:i:s'));
  1583.                 break;
  1584.             case EnrollmentEnum::APPLY_TO_SUPPORT_EXPIRED
  1585.                 $query->andWhere('e.dateSupport < :now');
  1586.                 $query->setParameter('now'date('Y-m-d H:i:s'));
  1587.                 break;
  1588.             case EnrollmentEnum::APPLY_TO_CANCELED
  1589.                 $query->andWhere('e.status = :statusCanceled');
  1590.                 $query->setParameter('statusCanceled'EnrollmentEnum::STATUS_CANCELED);
  1591.                 break;
  1592.         }
  1593.         return $query->getQuery()->execute();
  1594.     }
  1595.     public function updateEnrollmentMany(
  1596.         array $enrollmentsIds = [],
  1597.         ?int $status null,
  1598.         ?int $access null,
  1599.         ?int $support null,
  1600.         ?int $certificate null
  1601.     )
  1602.     {
  1603.         $upStatus "";
  1604.         if(!is_null($status)){
  1605.             $upStatus " e.status = :status ";
  1606.         }
  1607.         if($access == EnrollmentEnum::ENROLL_ORIGINAL){
  1608.             $lifetimePeriod $enrollment->getCourse()->getLifetimePeriod();
  1609.             if($lifetimePeriod == CourseEnum::YES){
  1610.                 $datePeriod "9999-09-09 00:00:00";
  1611.             }else{
  1612.                 $accessPeriod $enrollment->getCourse()->getAccessPeriod();
  1613.                 $dateRegister $enrollment->getDateRegister();
  1614.     
  1615.                 $datePeriod strtotime("+ {$accessPeriod} day"strtotime($dateRegister));
  1616.                 $datePeriod date('Y-m-d H:i:s'$datePeriod);
  1617.             }
  1618.                 
  1619.             $enrollment->setDatePeriod($datePeriod);
  1620.         }elseif($access == EnrollmentEnum::ENROLL_CHANGE){
  1621.             if(!empty($dateAccess)){
  1622.                 $enrollment->setDatePeriod($dateAccess);
  1623.             }
  1624.         }elseif($access == EnrollmentEnum::ENROLL_LIFETIME){
  1625.             if($accessLifetimePeriod == EnrollmentEnum::YES){
  1626.                 $dateAccess "9999-09-09 00:00:00";
  1627.                 $enrollment->setDatePeriod($dateAccess);
  1628.             }
  1629.         }
  1630.         if($support == EnrollmentEnum::ENROLL_ORIGINAL){
  1631.             $supportCourse $enrollment->getCourse()->getSupport();
  1632.             if($supportCourse == CourseEnum::YES){
  1633.                 $lifetimeSupport $enrollment->getCourse()->getLifetimeSupport();
  1634.                 if($lifetimeSupport == CourseEnum::YES){
  1635.                     $dateSupport "9999-09-09 00:00:00";
  1636.                 }else{
  1637.                     $supportPeriod $enrollment->getCourse()->getSupportPeriod();
  1638.                     $dateRegister $enrollment->getDateRegister();
  1639.         
  1640.                     $dateSupport strtotime("+ {$supportPeriod} day"strtotime($dateRegister));
  1641.                     $dateSupport date('Y-m-d H:i:s'$dateSupport);
  1642.                 }
  1643.             }else{
  1644.                 $dateSupport "0000-00-00 00:00:00";
  1645.             }
  1646.             
  1647.             $enrollment->setDateSupport($dateSupport);
  1648.         }elseif($support == EnrollmentEnum::ENROLL_CHANGE){
  1649.             if(!empty($dateSupport)){
  1650.                 $enrollment->setDateSupport($dateSupport);
  1651.             }
  1652.         }elseif($support == EnrollmentEnum::ENROLL_LIFETIME){
  1653.             if($supportLifetimePeriod == EnrollmentEnum::YES){
  1654.                 $dateSupport "9999-09-09 00:00:00";
  1655.                 $enrollment->setDateSupport($dateSupport);
  1656.             }
  1657.         }
  1658.         if($certificate == EnrollmentEnum::ENROLL_CHANGE){
  1659.             $enrollment->setCertificate(EnrollmentEnum::YES);
  1660.         }elseif($certificate == EnrollmentEnum::ENROLL_ORIGINAL){
  1661.             $enrollment->setCertificate(EnrollmentEnum::NO);
  1662.         }
  1663.         $sql "UPDATE EADPlataforma:Enrollment AS e 
  1664.                 SET e.deleted = :deleted, e.typeDelete = :typeDelete,
  1665.                     e.userDelete = :userDelete, e.dateDelete = :dateNow
  1666.                 WHERE e.deleted = (:deleted - 1)
  1667.                 AND e.user = :userId";
  1668.         $query $this->em->createQuery($sql);
  1669.         $query->setParameter('userId'$userId);
  1670.         $query->setParameter('deleted'$deleted);
  1671.         $query->setParameter('typeDelete'$typeDelete);
  1672.         $query->setParameter('userDelete'$userDelete);
  1673.         $query->setParameter('dateNow'$dateNow);
  1674.         $query->execute();
  1675.     }
  1676.     public function getEnrollmentsByUserSubscription(UserSubscription $userSubscription)
  1677.     {
  1678.         $query $this->createQueryBuilder('e');
  1679.         $query->innerJoin(
  1680.             'EADPlataforma:UserSubscription''us''WITH''e.user = us.user'
  1681.         );
  1682.         $query->innerJoin(
  1683.             'EADPlataforma:Product''p''WITH''e.course MEMBER OF p.course '
  1684.         );
  1685.         $query->andWhere('e.deleted = 0');
  1686.         $query->andWhere('us.id = :userSubscriptionId');
  1687.         $query->andWhere('p.id = :productId');
  1688.         $query->andWhere('e.user = :userId');
  1689.         $query->setParameter('userSubscriptionId'$userSubscription->getId());
  1690.         $query->setParameter('productId'$userSubscription->getProduct()->getId());
  1691.         $query->setParameter('userId'$userSubscription->getUser()->getId());
  1692.         return $query->getQuery()->execute();
  1693.     }
  1694.     public function notify(Enrollment $enrollment)
  1695.     {
  1696.         //send email
  1697.         $emailService $this->generalService->getService('EmailService');
  1698.         $client $this->configuration->getClient();
  1699.         $user $enrollment->getUser();
  1700.         if(!$emailService->checkUserToSend($user)){
  1701.             return;
  1702.         }
  1703.         $emailService->setToEmail($user->getEmail());
  1704.         $emailService->setToName($user->getName());
  1705.         
  1706.         $subText1 $this->configuration->getLanguage('enrollment.subject1''email');
  1707.         $subText2 $this->configuration->getLanguage('enrollment.subject2''email');
  1708.         $subject $subText1 $enrollment->getCourse()->getTitle() . $subText2;
  1709.         $emailService->setSubject($subject);
  1710.         
  1711.         $domain $client->getDomainPrimary();
  1712.         $emailService->setData([
  1713.             "userName" => $user->getName(),
  1714.             "courseTitle" => $enrollment->getCourse()->getTitle(),
  1715.             "btnLink" => "https://{$domain}/enrollment/{$enrollment->getId()}",
  1716.         ]);
  1717.         $emailService->setTemplateBody("enrollment");
  1718.         $emailService->send();
  1719.         //notify user
  1720.         $notificationService $this->generalService->getService('NotificationService');
  1721.         $notificationService->create(
  1722.             $enrollment->getCourse()->getUser(),
  1723.             $user,
  1724.             NotificationEnum::ORIGIN_ENROLLMENT_NEW,
  1725.             $enrollment->getId()
  1726.         );
  1727.     }
  1728.     public function generateCertificate(
  1729.         Enrollment $enrollment
  1730.         bool $download false
  1731.     )
  1732.     {
  1733.         $pdfService $this->generalService->getService('PdfService');
  1734.                                             
  1735.         $user $enrollment->getUser();
  1736.         $course $enrollment->getCourse();
  1737.         $examUserRepository $this->em->getRepository(ExamUser::class);
  1738.         $userExams $examUserRepository->getStudentReportCard($user->getId(), $course->getId());
  1739.         $finalAvarage $examUserRepository->getFinalAvarege($user->getId(), $course->getId());
  1740.         $history $this->getHistoryByEnrollment($enrollment);
  1741.         $historyMetrics $this->getHistoryMetricsByEnrollment($enrollment->getId());
  1742.         $data = [
  1743.             "enrollmentUserPhoto" => $user->getPhoto(),
  1744.             "enrollmentUserName" => mb_strtoupper($user->getName()),
  1745.             "enrollmentCourseTitle" => $course->getTitle(),
  1746.             "enrollmentDateStart" => $enrollment->getDateStart(),
  1747.             "enrollmentLastAccess" => $enrollment->getDateLastAccess(),
  1748.             "courseProgress" => $historyMetrics->progress,
  1749.             "userExams" => $userExams,
  1750.             "finalAvarage" => $finalAvarage,
  1751.             "dataAccessLesson" => $historyMetrics->lessonCompleted,
  1752.             "dataAccessLessonTotal" => $historyMetrics->lessonTotal,
  1753.             "dataAccessPreview" => $historyMetrics->numberAccess,
  1754.             "dataAccessSopportRequests" => $historyMetrics->numberSupport,
  1755.             "dataAccessTimeCourse" => $historyMetrics->timeWatch,
  1756.             "history" => $history
  1757.         ];
  1758.         $pdfService->setFileName("historico_do_aluno");
  1759.         $pdfService->setTemplateBody("enrollment_history");
  1760.         $pdfService->setData($data);
  1761.         return $pdfService->generate(false$download);
  1762.     }
  1763.     public function export(
  1764.         $courseId null
  1765.         $userId null
  1766.         $groupId null
  1767.         $teacherId null
  1768.         $status null
  1769.         $dateStart null
  1770.         $dateEnd null
  1771.     )
  1772.     {
  1773.         $query $this->createQueryBuilder('e');
  1774.         $query->select("
  1775.             e.id,
  1776.             e.status,
  1777.             e.lessonNumberComplete,
  1778.             DATE_FORMAT(e.dateRegister, '%Y-%m-%d %H:%i:%s') AS dateRegister,
  1779.             DATE_FORMAT(e.dateConclusion, '%Y-%m-%d %H:%i:%s') AS dateConclusion,
  1780.             DATE_FORMAT(e.dateStart, '%Y-%m-%d %H:%i:%s') AS dateStart,
  1781.             DATE_FORMAT(e.dateSupport, '%Y-%m-%d %H:%i:%s') AS dateSupport,
  1782.             DATE_FORMAT(e.datePeriod, '%Y-%m-%d %H:%i:%s') AS datePeriod,
  1783.             DATE_FORMAT(e.dateLastAccess, '%Y-%m-%d %H:%i:%s') AS dateLastAccess,
  1784.             e.lessonNumberViews AS lessonNumberViews,
  1785.             e.supportNumberRequest AS supportNumberRequest,
  1786.             e.couponKey AS couponKey,
  1787.             e.certificate AS certificate,
  1788.             e.origin,
  1789.             e.finalAverage,
  1790.             
  1791.             u.id AS userId,
  1792.             u.status AS userStatus,
  1793.             u.name AS userName,
  1794.             u.email AS userEmail,
  1795.             u.document AS userDocument,
  1796.             DATE_FORMAT(u.birthDate, '%Y-%m-%d') AS userBirthDate,
  1797.             u.phone AS userPhone,
  1798.             u.address AS userAddress,
  1799.             u.addressNumber AS userAddressNumber,
  1800.             u.addressComplement AS userAddressComplement,
  1801.             u.addressNeighborhood AS userAddressNeighborhood,
  1802.             u.zipCode AS userZipCode,
  1803.             u.occupation AS userOccupation,
  1804.             u.notes AS userNotes,
  1805.             u.custom AS userCustom,
  1806.             DATE_FORMAT(u.dateRegister, '%Y-%m-%d %H:%i:%s') AS userDateRegister,
  1807.             u.acceptTerms AS userAcceptTerms,
  1808.             u.customField AS userCustomField,
  1809.             ct.name AS cityName,
  1810.             st.name AS stateName,
  1811.             cty.name AS countryName,
  1812.             c.title,
  1813.             c.numberLesson,
  1814.             c.certificateAverage,
  1815.             ca.category,
  1816.             g.name AS groupName
  1817.         ");
  1818.         $query->innerJoin('EADPlataforma:Course''c''WITH''c.id = e.course');
  1819.         $query->innerJoin('EADPlataforma:Category''ca''WITH''ca.id = c.category');
  1820.         $query->innerJoin('EADPlataforma:User''u''WITH''u.id = e.user');
  1821.         $query->leftJoin('EADPlataforma:City''ct''WITH''ct.id = u.city');
  1822.         $query->leftJoin('EADPlataforma:State''st''WITH''st.id = u.state');
  1823.         $query->leftJoin('EADPlataforma:Country''cty''WITH''cty.id = u.country');
  1824.         
  1825.         $query->andWhere('e.deleted = :deleted');
  1826.         $query->andWhere('u.deleted = :deleted');
  1827.         $query->andWhere('c.deleted = :deleted');
  1828.         $query->andWhere('u.id > 1');
  1829.         $query->andWhere('u.status != :userStatus');
  1830.         $query->setParameter('userStatus'UserEnum::INACTIVE);
  1831.         $query->setParameter('deleted'UserEnum::ITEM_NO_DELETED);
  1832.         if(!is_null($dateStart) && !is_null($dateEnd)){
  1833.             $query->andWhere('DATE(e.dateRegister) BETWEEN :dateStart AND :dateEnd');
  1834.             $query->setParameter('dateStart'$dateStart);
  1835.             $query->setParameter('dateEnd'$dateEnd);
  1836.         }
  1837.         if(!empty($courseId)){
  1838.             $query->andWhere('e.course = :course')->setParameter('course'$courseId);
  1839.         }
  1840.        
  1841.         if(!empty($groupId)){
  1842.             $query->innerJoin(
  1843.                 'EADPlataforma:Group''g''WITH'
  1844.                 'e.course MEMBER OF g.course AND e.user MEMBER OF g.user'
  1845.             );
  1846.             $query->andWhere('g.id = :groupId');
  1847.             $query->setParameter('groupId'$groupId);
  1848.         }else{
  1849.             $query->leftJoin(
  1850.                 'EADPlataforma:Group''g''WITH'
  1851.                 'e.course MEMBER OF g.course AND e.user MEMBER OF g.user'
  1852.             );
  1853.         }
  1854.         
  1855.         if(!empty($userId)){
  1856.             $query->andWhere('e.user = :user')->setParameter('user'$userId);
  1857.         }
  1858.         
  1859.         if(!empty($teacherId)){
  1860.             $subQuery $this->createQueryBuilder('course_team');
  1861.             $subQuery->select('IDENTITY(cteam.course)');
  1862.             $subQuery->from('EADPlataforma:CourseTeam''cteam');
  1863.             $subQuery->andWhere('cteam.user = :teacherId');
  1864.             $subQuery $subQuery->getDQL();
  1865.             $query->andWhere($query->expr()->in('e.course'$subQuery));
  1866.             $query->setParameter('teacherId'$teacherId);
  1867.         }
  1868.         if($status == EnrollmentEnum::STATUS_EXPIRED){
  1869.             $query->andWhere('e.datePeriod < :now');
  1870.             $query->setParameter('now'date('Y-m-d H:i:s'));
  1871.             $query->andWhere('e.status = :status');
  1872.             $query->setParameter('status'EnrollmentEnum::STATUS_ACTIVE);
  1873.         }else if($status == EnrollmentEnum::STATUS_ACTIVE){
  1874.             $query->andWhere('e.datePeriod >= :now');
  1875.             $query->setParameter('now'date('Y-m-d H:i:s'));
  1876.             $query->andWhere('e.status = :status');
  1877.             $query->setParameter('status'EnrollmentEnum::STATUS_ACTIVE);
  1878.         }else if(!empty($status)){
  1879.             $query->andWhere('e.status = :status');
  1880.             $query->setParameter('status'$status);
  1881.         }
  1882.         
  1883.         $data $query->getQuery()->execute();
  1884.         return $data;
  1885.     }
  1886.     public function getEnrollmentBeforeExpireByCourse(Course $coursestring $dateExpire)
  1887.     {
  1888.         $query $this->createQueryBuilder('e');
  1889.         $query->andWhere('e.deleted = 0');
  1890.         $query->andWhere('e.course = :course');
  1891.         $query->andWhere('DATE(e.datePeriod) = :datePeriod');
  1892.         $query->setParameter('course'$course->getId());
  1893.         $query->setParameter('datePeriod'$dateExpire);
  1894.         return $query->getQuery()->execute();
  1895.     }
  1896.     public function updateDataAccessLog(Enrollment $enrollment, ?bool $updateDate true){
  1897.         $course $enrollment->getCourse();
  1898.         $courseId $enrollment->getCourse()->getId();
  1899.         $userId $enrollment->getUser()->getId();
  1900.         $courseRepository $this->em->getRepository(Course::class);
  1901.         $lessonSupportRepository $this->em->getRepository(LessonSupport::class);
  1902.         $lessonLogRepository $this->emEadmin->getRepository(LessonLog::class);
  1903.         $lessonLogOriginRepository $this->em->getRepository(LessonLogOrigin::class);
  1904.         $isStudent $courseRepository->isStudent($course);
  1905.         $lessonsNumberComplete $lessonLogRepository->countLessonLogCompleteByUser(
  1906.             $course,
  1907.             $enrollment->getUser()
  1908.         );
  1909.         $lessonsDateLastAccess $lessonLogRepository->getLessonDateLastAccess(
  1910.             $courseId
  1911.             $userId
  1912.         );
  1913.         $lessonsNumberViews $lessonLogRepository->getLessonNumberViews(
  1914.             $courseId
  1915.             $userId
  1916.         );
  1917.         $lessonTimeWatch $lessonLogRepository->userLessonTimeWatch(
  1918.             $course
  1919.             $enrollment->getUser()
  1920.         );
  1921.         $supportNumberRequest $lessonSupportRepository->getSupportNumberRequestByEnrollment(
  1922.             $enrollment
  1923.         );
  1924.         $enrollment->setLessonTimeWatch(
  1925.             !empty($lessonTimeWatch) ? $lessonTimeWatch null
  1926.         );
  1927.         if($this->getUser()){
  1928.             if($this->getUser()->getId() == $userId && $updateDate){
  1929.                 if(empty($lessonsDateLastAccess)){
  1930.                     $lessonsDateLastAccess date('Y-m-d H:i:s');
  1931.                 }
  1932.                 $enrollment->setDateLastAccess(
  1933.                     !empty($lessonsDateLastAccess) ? $lessonsDateLastAccess null
  1934.                 );
  1935.                 if(empty($enrollment->getDateStart()) && !empty($lessonsDateLastAccess)){
  1936.                     if($isStudent){
  1937.                         //send mkt start course
  1938.                         $marketingService $this->generalService->getService(
  1939.                             'Marketing\\MarketingService'
  1940.                         );
  1941.             
  1942.                         $marketingService->setTag(TagsMarketingEnum::TAG_BEGIN_COURSE);
  1943.                         $marketingService->setTextComplement($course->getTitle());
  1944.                         $marketingService->setUser($this->getUser());
  1945.                         $marketingService->send();
  1946.                     }
  1947.                     $enrollment->setDateStart(date('Y-m-d H:i:s'));
  1948.                 }
  1949.             }
  1950.         }
  1951.         
  1952.         $enrollment->setLessonNumberComplete((int)$lessonsNumberComplete);
  1953.         $enrollment->setLessonNumberViews((int)$lessonsNumberViews);
  1954.         $enrollment->setSupportNumberRequest((int)$supportNumberRequest);
  1955.         $this->em->flush();
  1956.         return $enrollment;
  1957.     }
  1958.     public function getEnrollmentForPresence(
  1959.         Course $course
  1960.         ?bool $active true
  1961.         ?int $user null
  1962.         ?int $groupId null
  1963.         ?string $dateStart null
  1964.         ?string $dateEnd null
  1965.     )
  1966.     {
  1967.         $query $this->createQueryBuilder('e');
  1968.         $query->innerJoin('EADPlataforma:User''u''WITH''u.id = e.user');
  1969.         if($active){
  1970.             $query->andWhere('e.datePeriod >= :now');
  1971.             $query->setParameter('now'date('Y-m-d H:i:s'));
  1972.             $query->andWhere('e.status = :status');
  1973.             $query->setParameter('status'EnrollmentEnum::STATUS_ACTIVE);
  1974.         }
  1975.         if(!empty($user)){
  1976.             $query->andWhere('e.user = :user');
  1977.             $query->setParameter('user'$user);
  1978.         }
  1979.         if(!empty($groupId)){
  1980.             $query->innerJoin(
  1981.                 'EADPlataforma:Group''g''WITH'
  1982.                 'e.course MEMBER OF g.course AND e.user MEMBER OF g.user'
  1983.             );
  1984.             $query->andWhere('g.id = :groupId');
  1985.             $query->setParameter('groupId'$groupId);
  1986.         }
  1987.         if(!is_null($dateStart) && !is_null($dateEnd)){
  1988.             $query->andWhere('DATE(e.dateLastAccess) BETWEEN :dateStart AND :dateEnd');
  1989.             $query->setParameter('dateStart'$dateStart);
  1990.             $query->setParameter('dateEnd'$dateEnd);
  1991.         }
  1992.         $query->andWhere('u.status != :userStatus');
  1993.         $query->setParameter('userStatus'UserEnum::INACTIVE);
  1994.         
  1995.         $query->andWhere('e.deleted = 0');
  1996.         $query->andWhere('u.deleted = 0');
  1997.         $query->andWhere('u.id > 1');
  1998.         $query->andWhere('e.course = :course');
  1999.         $query->setParameter('course'$course->getId());
  2000.         return $query->getQuery()->execute();
  2001.     }
  2002.     public function restore(Enrollment $enrollment$typeItem)
  2003.     {
  2004.         $itemId $enrollment->getId();
  2005.         $dateNow date('Y-m-d H:i:s');
  2006.         $enrollment->setUserDelete($this->getUser());
  2007.         $enrollment->setDateDelete(date('Y-m-d H:i:s'));
  2008.         $enrollment->restore();
  2009.         $this->em->getRepository(Trash::class)->deleteTrash($itemId$typeItem);
  2010.         
  2011.         $examUserRepository $this->em->getRepository(ExamUser::class);
  2012.             $examUsers $examUserRepository->findBy([
  2013.                 "course" => $enrollment->getCourse()->getId(),
  2014.                 "user" => $enrollment->getUser()->getId(),
  2015.                 "deleted" => ExamUserEnum::ITEM_ON_TRASH
  2016.             ]);
  2017.             foreach ($examUsers as $examUser) {
  2018.                 $this->em->getRepository(ExamUser::class)->restoreByExamUser(
  2019.                     $examUser->getId(),
  2020.                     EnrollmentEnum::ITEM_NO_DELETED,
  2021.                     EnrollmentEnum::CASCADE
  2022.                     $this->getUser() ?? null
  2023.                     $dateNow
  2024.                 );           
  2025.             }
  2026.     }
  2027.     public function delete(Enrollment $enrollment$typeItem$permission$isTrash)
  2028.     {
  2029.         $userPermissionUtil $this->generalService->getUtil('UserPermissionUtil');
  2030.         if($isTrash == 1){
  2031.             $permission $userPermissionUtil->getPermission("trash""delete");
  2032.         }
  2033.         $isInTeam false;
  2034.         $course $enrollment->getCourse();
  2035.         $dateNow date('Y-m-d H:i:s');
  2036.         if($this->getUser()){
  2037.             $isInTeam $this->em->getRepository(CourseTeam::class)->userExistInCourseTeam(
  2038.                 $course
  2039.                 $this->getUser()
  2040.             );
  2041.         }
  2042.         
  2043.         if($isInTeam || $userPermissionUtil->isHigh($permission)){
  2044.             $enrollment->setUserDelete($this->getUser());
  2045.             $enrollment->setDateDelete(date('Y-m-d H:i:s'));
  2046.             $enrollment->individual();
  2047.             if($enrollment->isOnTrash() || $enrollment->isDeleted()){
  2048.                 $enrollment->delete();
  2049.                 $itemId $enrollment->getId();
  2050.                 $this->em->getRepository(Trash::class)->deleteTrash($itemId$typeItem);
  2051.             }
  2052.             if(!$enrollment->isOnTrash() && !$enrollment->isDeleted()){
  2053.                 $enrollment->trash();
  2054.                 $this->em->getRepository(Trash::class)->insertTrash(
  2055.                     $enrollment
  2056.                     $typeItem
  2057.                     "Aluno: {$enrollment->getUser()->getName()} | Curso: {$enrollment->getCourse()->getTitle()}"
  2058.                 );
  2059.             }
  2060.             $examUserRepository $this->em->getRepository(ExamUser::class);
  2061.             $examUsers $examUserRepository->findBy([
  2062.                 "course" => $enrollment->getCourse()->getId(),
  2063.                 "user" => $enrollment->getUser()->getId(),
  2064.                 "deleted" => ExamUserEnum::ITEM_NO_DELETED
  2065.             ]);
  2066.             foreach ($examUsers as $examUser) {              
  2067.                 $this->em->getRepository(ExamUser::class)->deleteByExamUser(
  2068.                     $examUser->getId(),
  2069.                     EnrollmentEnum::ITEM_ON_TRASH
  2070.                     EnrollmentEnum::CASCADE
  2071.                     ($this->getUser() ? $this->getUser()->getId() : null),
  2072.                     $dateNow
  2073.                 );           
  2074.             }
  2075.         }
  2076.         $this->em->flush();
  2077.     }
  2078.     public function restoreByUser($userId$deleted$typeDelete$userDelete$dateNow){
  2079.         $sql "UPDATE EADPlataforma:Enrollment AS e 
  2080.                 SET e.deleted = :deleted, e.userDelete = :userDelete, 
  2081.                     e.dateDelete = :dateNow
  2082.                 WHERE e.deleted = (1 - :deleted)
  2083.                 AND e.typeDelete = :typeDelete
  2084.                 AND e.user = :userId";
  2085.         $query $this->em->createQuery($sql);
  2086.         $query->setParameter('userId'$userId);
  2087.         $query->setParameter('deleted'$deleted);
  2088.         $query->setParameter('typeDelete'$typeDelete);
  2089.         $query->setParameter('userDelete'$userDelete);
  2090.         $query->setParameter('dateNow'$dateNow);
  2091.         $query->execute();
  2092.         
  2093.         return true;
  2094.     }
  2095.     public function deleteByUser($userId$deleted$typeDelete$userDelete$dateNow){
  2096.         $sql "UPDATE EADPlataforma:Enrollment AS e 
  2097.                 SET e.deleted = :deleted, e.typeDelete = :typeDelete,
  2098.                     e.userDelete = :userDelete, e.dateDelete = :dateNow
  2099.                 WHERE e.deleted = (:deleted - 1)
  2100.                 AND e.user = :userId";
  2101.         $query $this->em->createQuery($sql);
  2102.         $query->setParameter('userId'$userId);
  2103.         $query->setParameter('deleted'$deleted);
  2104.         $query->setParameter('typeDelete'$typeDelete);
  2105.         $query->setParameter('userDelete'$userDelete);
  2106.         $query->setParameter('dateNow'$dateNow);
  2107.         $query->execute();
  2108.         
  2109.         return true;
  2110.     }
  2111.     public function restoreByCourse($courseId$deleted$typeDelete$userDelete$dateNow){
  2112.         $sql "UPDATE EADPlataforma:Enrollment AS e 
  2113.                 SET e.deleted = :deleted, e.userDelete = :userDelete, 
  2114.                     e.dateDelete = :dateNow
  2115.                 WHERE e.deleted = (1 - :deleted)
  2116.                 AND e.typeDelete = :typeDelete
  2117.                 AND e.course = :courseId";
  2118.         $query $this->em->createQuery($sql);
  2119.         $query->setParameter('courseId'$courseId);
  2120.         $query->setParameter('deleted'$deleted);
  2121.         $query->setParameter('typeDelete'$typeDelete);
  2122.         $query->setParameter('userDelete'$userDelete);
  2123.         $query->setParameter('dateNow'$dateNow);
  2124.         $query->execute();
  2125.         
  2126.         return true;
  2127.     }
  2128.     public function deleteByCourse($courseId$deleted$typeDelete$userDelete$dateNow){
  2129.         $sql "UPDATE EADPlataforma:Enrollment AS e 
  2130.                 SET e.deleted = :deleted, e.typeDelete = :typeDelete,
  2131.                     e.userDelete = :userDelete, e.dateDelete = :dateNow
  2132.                 WHERE e.deleted = (:deleted - 1)
  2133.                 AND e.course = :courseId";
  2134.         $query $this->em->createQuery($sql);
  2135.         $query->setParameter('courseId'$courseId);
  2136.         $query->setParameter('deleted'$deleted);
  2137.         $query->setParameter('typeDelete'$typeDelete);
  2138.         $query->setParameter('userDelete'$userDelete);
  2139.         $query->setParameter('dateNow'$dateNow);
  2140.         $query->execute();
  2141.         
  2142.         return true;
  2143.     }
  2144.     public function deleteTrashCron()
  2145.     {
  2146.         $sql "UPDATE EADPlataforma:Enrollment AS e SET e.deleted = 2 
  2147.                 WHERE e.dateDelete <= :date AND e.deleted = 1 ";
  2148.         $query $this->em->createQuery($sql);
  2149.         $query->setParameter('date'date('Y-m-d H:i:s'strtotime('-90 days')));
  2150.         $query->execute();
  2151.     }
  2152.     public function bulkUpdateStatus(array $enrollmentsint $status): array
  2153.     {
  2154.         foreach ($enrollments as $enrollment) {
  2155.             $enrollment->setStatus($status);
  2156.         }
  2157.         $this->em->flush();
  2158.         return $enrollments;
  2159.     }
  2160.     public function updateEnrollmentsWithGroup(array $enrollments, array $data)
  2161.     {
  2162.         $dates $this->verifyDate($data);
  2163.         foreach ($enrollments as $enrollment) {
  2164.             $enrollment->setDatePeriod($dates['dateAccessConclusion']);
  2165.             $enrollment->setDateSupport($dates['dateSupportConclusion']);
  2166.         }
  2167.         $this->em->flush();
  2168.         return $enrollments;
  2169.     }
  2170.     public function verifyDate($data)
  2171.     {
  2172.         $dates = [];
  2173.         if($data["typeDateAccess"] == GroupEnum::TYPE_DATE_FIXED){
  2174.             $dateAccessConclusion $data["dateAccessConclusion"];
  2175.         }
  2176.         if($data["typeDateSupport"] == GroupEnum::TYPE_DATE_FIXED){
  2177.             $dateSupportConclusion $data["dateSupportConclusion"];
  2178.         }
  2179.         if($data["typeDateAccess"] == GroupEnum::TYPE_DATE_PERIOD){
  2180.             $period $data["dateAccessDays"];
  2181.             $dateNow date('Y-m-d H:i:s');
  2182.             $dateSupportConclusion date('Y-m-d H:i:s'strtotime("{$dateNow} + {$period} days"));
  2183.         }
  2184.         if($data["typeDateSupport"] == GroupEnum::TYPE_DATE_PERIOD){
  2185.             $period $data["dateSupportDays"];
  2186.             $dateNow date('Y-m-d H:i:s');
  2187.             $dateSupportConclusion date('Y-m-d H:i:s'strtotime("{$dateNow} + {$period} days"));           
  2188.         }
  2189.         $dates = [
  2190.             "dateAccessConclusion"  => $dateAccessConclusion,
  2191.             "dateSupportConclusion" => $dateSupportConclusion,
  2192.         ];
  2193.         return $dates;
  2194.     }
  2195.     public function getSituation(int $progress, ?float $certificateAverage, ?float $finalAverage)
  2196.     {
  2197.         $situation EnrollmentEnum::SITUATION_NOT_STARTED;
  2198.         if($progress >= 100){
  2199.             $situation EnrollmentEnum::SITUATION_COMPLETED;
  2200.         }else if($progress 0){
  2201.             $situation EnrollmentEnum::SITUATION_STARTED;
  2202.         }
  2203.         if(!empty($finalAverage) && $situation == EnrollmentEnum::SITUATION_COMPLETED){
  2204.             if($finalAverage >= $certificateAverage){
  2205.                 return $this->configuration->getLanguage('approved''enrollments');
  2206.             }else{
  2207.                 return $this->configuration->getLanguage('disapproved''enrollments');
  2208.             }
  2209.         }
  2210.         $situationLabels = [
  2211.             EnrollmentEnum::SITUATION_NOT_STARTED => "not_started",
  2212.             EnrollmentEnum::SITUATION_STARTED => "in_progress",
  2213.             EnrollmentEnum::SITUATION_COMPLETED => "completed",
  2214.         ];
  2215.         $index $situationLabels[$situation];
  2216.         return $this->configuration->getLanguage($index'enrollments');
  2217.     }
  2218. }