src/Services/EntityServices/CourseIndexService.php line 392

Open in your IDE?
  1. <?php
  2. namespace EADPlataforma\Services\EntityServices;
  3. use EADPlataforma\Entity\Course;
  4. use EADPlataforma\Entity\CourseTeam;
  5. use EADPlataforma\Entity\Enrollment;
  6. use EADPlataforma\Entity\ProductOffer;
  7. use EADPlataforma\Entity\User;
  8. use EADPlataforma\Enum\UserEnum;
  9. use EADPlataforma\Enum\CourseEnum;
  10. use EADPlataforma\Enum\EnrollmentEnum;
  11. use EADPlataforma\Enum\ProductEnum;
  12. use EADPlataforma\Repository\CourseRepository;
  13. use EADPlataforma\Repository\CourseTeamRepository;
  14. use EADPlataforma\Repository\EnrollmentRepository;
  15. use EADPlataforma\Repository\ProductOfferRepository;
  16. use EADPlataforma\Services\GeneralService;
  17. use EADPlataforma\Services\SchoolEntityManager;
  18. use EADPlataforma\Services\ConfigurationService;
  19. use EADPlataforma\Util\UserPermissionUtil;
  20. /**
  21.  * CourseIndex Service
  22.  */
  23. class CourseIndexService
  24. {
  25.     /**
  26.      * @var SchoolEntityManager $em
  27.      */
  28.     protected $em;
  29.     /**
  30.      * @var CourseRepository
  31.      */
  32.     private $repository;
  33.     /**
  34.      * @var CourseTeamRepository
  35.      */
  36.     private $courseTeamRepository;
  37.     /**
  38.      * @var EnrollmentRepository
  39.      */
  40.     private $enrollmentRepository;
  41.     /**
  42.      * @var ProductOfferRepository
  43.      */
  44.     private $productOfferRepository;
  45.     /**
  46.      * @var ConfigurationService
  47.      */
  48.     private $configuration;
  49.     /**
  50.      * @var UserPermissionUtil
  51.      */
  52.     protected $userPermissionUtil;
  53.     /**
  54.      * @var User
  55.      */
  56.     private $user;
  57.     /**
  58.      * @var Array
  59.      */
  60.     private $isStudentArray = [];
  61.     /**
  62.      * Constructor
  63.      *
  64.      * @param GeneralService $generalService
  65.      * @param CourseRepository $courseRepository
  66.      */
  67.     public function __construct(
  68.         GeneralService $generalService
  69.         CourseRepository $courseRepository
  70.     )
  71.     {
  72.         $this->repository $courseRepository;
  73.         $this->em $courseRepository->em;
  74.         $this->courseTeamRepository $this->em->getRepository(CourseTeam::class);
  75.         $this->enrollmentRepository $this->em->getRepository(Enrollment::class);
  76.         $this->productOfferRepository $this->em->getRepository(ProductOffer::class);
  77.         $this->generalService $generalService;
  78.         $this->configuration $this->generalService->getService('ConfigurationService');
  79.         $this->user $this->generalService->getService('UserSessionService')->getUser();
  80.         $this->userPermissionUtil $this->generalService->getUtil('UserPermissionUtil');
  81.     }
  82.     public function searchCourse(int $courseId): ?Course
  83.     {
  84.         return $this->repository->findOneBy([
  85.             "id" => $courseId,
  86.             "deleted" => CourseEnum::ITEM_NO_DELETED
  87.         ]);
  88.     }
  89.     public function isInTeam(Course $course): bool
  90.     {
  91.         return $this->courseTeamRepository->userExistInCourseTeam(
  92.             $course
  93.             $this->user
  94.         );
  95.     }
  96.     public function isStudent(Course $course): bool
  97.     {
  98.         $key "{$this->user->getId()}_{$course->getId()}";
  99.         if(isset($this->isStudentArray[$key])){
  100.             return $this->isStudentArray[$key];
  101.         }
  102.         $permission $this->userPermissionUtil->getPermission("course""see");
  103.         if($this->user->getId() == EnrollmentEnum::YES){
  104.             $this->isStudentArray[$key] = false;
  105.             return $this->isStudentArray[$key];
  106.         }
  107.         
  108.         if($this->userPermissionUtil->isHigh($permission)){
  109.             $this->isStudentArray[$key] = false;
  110.             return $this->isStudentArray[$key];
  111.         }
  112.         $isInTeam $this->isInTeam($course$this->user);
  113.         if($isInTeam && $this->userPermissionUtil->isMiddle($permission)){
  114.             $this->isStudentArray[$key] = false;
  115.             return $this->isStudentArray[$key];
  116.         }
  117.         $this->isStudentArray[$key] = true;
  118.         return $this->isStudentArray[$key];
  119.     }
  120.     public function searchEnrollment(Course $course$host null): ?Enrollment
  121.     {
  122.         $discordService $this->generalService->getService('DiscordService');
  123.         $discordService->setChannel('debug');
  124.         $enrollment $this->enrollmentRepository->findOneBy([
  125.             "user" => $this->user->getId(),
  126.             "course" => $course->getId(),
  127.             "deleted" => EnrollmentEnum::ITEM_NO_DELETED,
  128.         ], [ "id" => "DESC" ]);
  129.         if($host == 'local-dev.eadplataforma.com' || $host == 'culinayra.eadplataforma.app'){
  130.             $discordService->setMessage("User Service: {$this->user->getId()}");
  131.             $discordService->sendDiscord();
  132.         }
  133.         if($enrollment){
  134.             return $enrollment;
  135.         }
  136.             
  137.         $isStudent $this->isStudent($course$this->user);
  138.         if($isStudent && $course->getFree() == CourseEnum::NO){
  139.             return null;
  140.         }
  141.         $enrollmentService $this->generalService->getService('EnrollmentService');
  142.         $info $enrollmentService->enrollUser($this->user$course);
  143.         if(!empty($info->errors)){
  144.             return null;
  145.         }
  146.         if($host == 'local-dev.eadplataforma.com' || $host == 'culinayra.eadplataforma.app'){
  147.             $discordService->setMessage("ID Matriculou: {$info->enrollment->getId()}");
  148.             $discordService->sendDiscord();
  149.         }
  150.         return $info->enrollment;
  151.     }
  152.     public function isValidEnrollment(Course $course): bool
  153.     {
  154.         $isStudent $this->isStudent($course$this->user);
  155.         if(!$isStudent){
  156.             return true;
  157.         }
  158.         return $this->enrollmentRepository->isValidEnrollmentByUser(
  159.             $this->user->getId(), 
  160.             $course->getId()
  161.         );
  162.     }
  163.     public function isEnrollment(Course $course): bool
  164.     {
  165.         $isStudent $this->isStudent($course$this->user);
  166.         if(!$isStudent){
  167.             return true;
  168.         }
  169.         return $this->enrollmentRepository->isEnrollment(
  170.             $this->user->getId(), 
  171.             $course->getId()
  172.         );
  173.     }
  174.     public function getOffersPeriod(Enrollment $enrollment)
  175.     {
  176.         $course $enrollment->getCourse();
  177.         $findAllPeriods false;
  178.         $dateNow = new \Datetime();
  179.         $datePeriodEnrollment = new \Datetime($enrollment->getDatePeriod());
  180.         $deadlineEndPeriod $dateNow->diff($datePeriodEnrollment);
  181.         $productOffersPeriod = [];
  182.         if($deadlineEndPeriod->format('%r%a') <= 30){
  183.             $findAllPeriods true;
  184.             $productOffersPeriod $this->productOfferRepository->getProductOffersByCourse(
  185.                 $course
  186.                 ProductEnum::PERIOD
  187.             );
  188.         }
  189.         foreach ($productOffersPeriod as $key => $productOffer) {
  190.             $productOffersPeriod[$key] = $productOffer->toReturnClean();
  191.             $productOffersPeriod[$key]->type ProductEnum::PERIOD;
  192.             $linkBuy $this->generalService->generateUrl("cartAdd", [
  193.                 "poID" => $productOffer->getId(),
  194.                 "courseId" => $course->getId(),
  195.             ]);
  196.             $productOffersPeriod[$key]->link $linkBuy;
  197.         }
  198.         $dateSupportEnrollment = new \Datetime($enrollment->getDateSupport());
  199.         $deadlineEndSupport $dateNow->diff($dateSupportEnrollment);
  200.         $productOffersSupport = [];
  201.         if($deadlineEndSupport->format('%r%a') <= 30){
  202.             $findAllPeriods true;
  203.             $productOffersSupport $this->productOfferRepository->getProductOffersByCourse(
  204.                 $course
  205.                 ProductEnum::SUPPORT
  206.             );
  207.         }
  208.         
  209.         foreach ($productOffersSupport as $key => $productOffer) {
  210.             $productOffersSupport[$key] = $productOffer->toReturnClean();
  211.             $productOffersSupport[$key]->type ProductEnum::SUPPORT;
  212.             $linkBuy $this->generalService->generateUrl("cartAdd", [
  213.                 "poID" => $productOffer->getId(),
  214.                 "courseId" => $course->getId(),
  215.             ]);
  216.             $productOffersSupport[$key]->link $linkBuy;
  217.         }
  218.         $productOffersPeriodSupport = [];
  219.         if($findAllPeriods){
  220.             $productOffersPeriodSupport $this->productOfferRepository->getProductOffersByCourse(
  221.                 $course,
  222.                 ProductEnum::PERIOD_SUPORT
  223.             );
  224.         }
  225.         foreach ($productOffersPeriodSupport as $key => $productOffer) {
  226.             $productOffersPeriodSupport[$key] = $productOffer->toReturnClean();
  227.             $productOffersPeriodSupport[$key]->type ProductEnum::PERIOD_SUPORT;
  228.             $linkBuy $this->generalService->generateUrl("cartAdd", [
  229.                 "poID" => $productOffer->getId(),
  230.                 "courseId" => $course->getId(),
  231.             ]);
  232.             $productOffersPeriodSupport[$key]->link $linkBuy;
  233.         }
  234.         $certificateOffer null;
  235.         if($enrollment->getCertificate() == EnrollmentEnum::NO){
  236.             $certificateOffer $this->productOfferRepository->getProductOfferCertificateByCourse(
  237.                 $course
  238.             );
  239.             if($certificateOffer){
  240.                 $linkBuy $this->generalService->generateUrl("cartAdd", [
  241.                     "poID" => $certificateOffer->getId(),
  242.                     "courseId" => $course->getId(),
  243.                 ]);
  244.                 
  245.                 $certificateOffer = (object)$certificateOffer->toReturnClean();
  246.                 
  247.                 $certificateOffer->type ProductEnum::CERTIFICATE;
  248.                 $certificateOffer->link $linkBuy;
  249.             }
  250.         }
  251.         return (object)[
  252.             "productOffersCertificate" => $certificateOffer,
  253.             "productOffersPeriod" => array_merge(
  254.                 $productOffersPeriodSupport
  255.                 $productOffersPeriod
  256.             ),
  257.             "productOffersSupport" => array_merge(
  258.                 $productOffersPeriodSupport
  259.                 $productOffersSupport
  260.             ),
  261.         ];
  262.     }
  263.     public function getCourseNewGeneral(Course $courseEnrollment $enrollment): array
  264.     {
  265.         $session $this->user->getSession();
  266.         $userOrigin $session->getUserOrigin();
  267.         $fileService $this->generalService->getService('FileService');
  268.         $dataImg = [
  269.             "fileName" => $this->user->getPhoto(),
  270.             "pathConst" => UserEnum::PATH_PROFILES,
  271.             "option" => "l-user-profile",
  272.             "addUpload" => true,
  273.             "addStream" => true,
  274.         ];
  275.         $translations = ([ 
  276.             "translations" => $this->configuration->getLanguageModule('lesson_view'),
  277.             "language" => $this->configuration->getSystemLanguage()
  278.         ]);
  279.         $offers $this->getOffersPeriod($enrollment);
  280.         
  281.         $clientConfig $this->configuration->getClientConfiguration();
  282.         $userSessionService $this->generalService->getService('UserSessionService');
  283.         $session $userSessionService->getSession();
  284.         
  285.         $userToken md5($clientConfig->getId()) . md5(
  286.             $session->getId() . $session->getToken()
  287.         );
  288.         $allowTestimonial $this->configuration->get('allow_testimonial');
  289.         $requiredTextTestimonial $this->configuration->get('required_text_testimonial');
  290.         $testimonialConfig = (object)[
  291.             "allowTestimonial" => (int)$allowTestimonial,
  292.             "requiredTextTestimonial" => (int)$requiredTextTestimonial,
  293.         ];
  294.         $data = [
  295.             "productOffersCertificate" => $offers->productOffersCertificate,
  296.             "productOffersPeriod" => $offers->productOffersPeriod,
  297.             "productOffersSupport" => $offers->productOffersSupport,
  298.             "client" => $this->configuration->getClientInfo(),
  299.             "translations" => $translations,
  300.             "drm" => $this->configuration->getDrmConfiguration(),
  301.             "testimonialConfig" => $testimonialConfig,
  302.             "date" => date('Y-m-d H:i:s'),
  303.             "user" => [
  304.                 "id" => $this->user->getId(),
  305.                 "ip" => $this->generalService->getClientIp(),
  306.                 "name" => $this->user->getName(),
  307.                 "hasUserOrigin" => ($userOrigin true false),
  308.                 "canAccessAdm" => $this->userPermissionUtil->canAccessAdm(),
  309.                 "screenSize" => $this->user->getScreenSize(),
  310.                 "autoplay" => $this->user->getAutoplay(),
  311.                 "document" => $this->user->getDocument(),
  312.                 "username" => $this->user->getUsername(),
  313.                 "token" => $userToken,
  314.                 "photo" => $fileService->getFilePathObj($dataImg),
  315.                 "permissionName" => (
  316.                     $this->user->getUserProfile() ? 
  317.                     $this->user->getUserProfile()->getName() 
  318.                     : null
  319.                 ),
  320.             ],
  321.         ];
  322.         return $data;
  323.     }
  324.     public function getCourseIndex(Enrollment $enrollment): array
  325.     {
  326.         return (array)$this->enrollmentRepository->getCourseIndexByEnrollmentNew($enrollment);
  327.     }
  328. }