src/Services/EntityServices/CourseIndexService.php line 379

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): ?Enrollment
  121.     {
  122.         $enrollment $this->enrollmentRepository->findOneBy([
  123.             "user" => $this->user->getId(),
  124.             "course" => $course->getId(),
  125.             "deleted" => EnrollmentEnum::ITEM_NO_DELETED,
  126.         ], [ "id" => "DESC" ]);
  127.         if($enrollment){
  128.             return $enrollment;
  129.         }
  130.             
  131.         $isStudent $this->isStudent($course$this->user);
  132.         if($isStudent && $course->getFree() == CourseEnum::NO){
  133.             return null;
  134.         }
  135.         $enrollmentService $this->generalService->getService('EnrollmentService');
  136.         $info $enrollmentService->enrollUser($this->user$course);
  137.         if(!empty($info->errors)){
  138.             return null;
  139.         }
  140.         
  141.         return $info->enrollment;
  142.     }
  143.     public function isValidEnrollment(Course $course): bool
  144.     {
  145.         $isStudent $this->isStudent($course$this->user);
  146.         if(!$isStudent){
  147.             return true;
  148.         }
  149.         return $this->enrollmentRepository->isValidEnrollmentByUser(
  150.             $this->user->getId(), 
  151.             $course->getId()
  152.         );
  153.     }
  154.     public function isEnrollment(Course $course): bool
  155.     {
  156.         $isStudent $this->isStudent($course$this->user);
  157.         if(!$isStudent){
  158.             return true;
  159.         }
  160.         return $this->enrollmentRepository->isEnrollment(
  161.             $this->user->getId(), 
  162.             $course->getId()
  163.         );
  164.     }
  165.     public function getOffersPeriod(Enrollment $enrollment)
  166.     {
  167.         $course $enrollment->getCourse();
  168.         $findAllPeriods false;
  169.         $dateNow = new \Datetime();
  170.         $datePeriodEnrollment = new \Datetime($enrollment->getDatePeriod());
  171.         $deadlineEndPeriod $dateNow->diff($datePeriodEnrollment);
  172.         $productOffersPeriod = [];
  173.         if($deadlineEndPeriod->format('%r%a') <= 30){
  174.             $findAllPeriods true;
  175.             $productOffersPeriod $this->productOfferRepository->getProductOffersByCourse(
  176.                 $course
  177.                 ProductEnum::PERIOD
  178.             );
  179.         }
  180.         foreach ($productOffersPeriod as $key => $productOffer) {
  181.             $productOffersPeriod[$key] = $productOffer->toReturnClean();
  182.             $productOffersPeriod[$key]->type ProductEnum::PERIOD;
  183.             $linkBuy $this->generalService->generateUrl("cartAdd", [
  184.                 "poID" => $productOffer->getId(),
  185.                 "courseId" => $course->getId(),
  186.             ]);
  187.             $productOffersPeriod[$key]->link $linkBuy;
  188.         }
  189.         $dateSupportEnrollment = new \Datetime($enrollment->getDateSupport());
  190.         $deadlineEndSupport $dateNow->diff($dateSupportEnrollment);
  191.         $productOffersSupport = [];
  192.         if($deadlineEndSupport->format('%r%a') <= 30){
  193.             $findAllPeriods true;
  194.             $productOffersSupport $this->productOfferRepository->getProductOffersByCourse(
  195.                 $course
  196.                 ProductEnum::SUPPORT
  197.             );
  198.         }
  199.         
  200.         foreach ($productOffersSupport as $key => $productOffer) {
  201.             $productOffersSupport[$key] = $productOffer->toReturnClean();
  202.             $productOffersSupport[$key]->type ProductEnum::SUPPORT;
  203.             $linkBuy $this->generalService->generateUrl("cartAdd", [
  204.                 "poID" => $productOffer->getId(),
  205.                 "courseId" => $course->getId(),
  206.             ]);
  207.             $productOffersSupport[$key]->link $linkBuy;
  208.         }
  209.         $productOffersPeriodSupport = [];
  210.         if($findAllPeriods){
  211.             $productOffersPeriodSupport $this->productOfferRepository->getProductOffersByCourse(
  212.                 $course,
  213.                 ProductEnum::PERIOD_SUPORT
  214.             );
  215.         }
  216.         foreach ($productOffersPeriodSupport as $key => $productOffer) {
  217.             $productOffersPeriodSupport[$key] = $productOffer->toReturnClean();
  218.             $productOffersPeriodSupport[$key]->type ProductEnum::PERIOD_SUPORT;
  219.             $linkBuy $this->generalService->generateUrl("cartAdd", [
  220.                 "poID" => $productOffer->getId(),
  221.                 "courseId" => $course->getId(),
  222.             ]);
  223.             $productOffersPeriodSupport[$key]->link $linkBuy;
  224.         }
  225.         $certificateOffer null;
  226.         if($enrollment->getCertificate() == EnrollmentEnum::NO){
  227.             $certificateOffer $this->productOfferRepository->getProductOfferCertificateByCourse(
  228.                 $course
  229.             );
  230.             if($certificateOffer){
  231.                 $linkBuy $this->generalService->generateUrl("cartAdd", [
  232.                     "poID" => $certificateOffer->getId(),
  233.                     "courseId" => $course->getId(),
  234.                 ]);
  235.                 
  236.                 $certificateOffer = (object)$certificateOffer->toReturnClean();
  237.                 
  238.                 $certificateOffer->type ProductEnum::CERTIFICATE;
  239.                 $certificateOffer->link $linkBuy;
  240.             }
  241.         }
  242.         return (object)[
  243.             "productOffersCertificate" => $certificateOffer,
  244.             "productOffersPeriod" => array_merge(
  245.                 $productOffersPeriodSupport
  246.                 $productOffersPeriod
  247.             ),
  248.             "productOffersSupport" => array_merge(
  249.                 $productOffersPeriodSupport
  250.                 $productOffersSupport
  251.             ),
  252.         ];
  253.     }
  254.     public function getCourseNewGeneral(Course $courseEnrollment $enrollment): array
  255.     {
  256.         $session $this->user->getSession();
  257.         $userOrigin $session->getUserOrigin();
  258.         $fileService $this->generalService->getService('FileService');
  259.         $dataImg = [
  260.             "fileName" => $this->user->getPhoto(),
  261.             "pathConst" => UserEnum::PATH_PROFILES,
  262.             "option" => "l-user-profile",
  263.             "addUpload" => true,
  264.             "addStream" => true,
  265.         ];
  266.         $translations = ([ 
  267.             "translations" => $this->configuration->getLanguageModule('lesson_view'),
  268.             "language" => $this->configuration->getSystemLanguage()
  269.         ]);
  270.         $offers $this->getOffersPeriod($enrollment);
  271.         
  272.         $clientConfig $this->configuration->getClientConfiguration();
  273.         $userSessionService $this->generalService->getService('UserSessionService');
  274.         $session $userSessionService->getSession();
  275.         
  276.         $userToken md5($clientConfig->getId()) . md5(
  277.             $session->getId() . $session->getToken()
  278.         );
  279.         $allowTestimonial $this->configuration->get('allow_testimonial');
  280.         $requiredTextTestimonial $this->configuration->get('required_text_testimonial');
  281.         $testimonialConfig = (object)[
  282.             "allowTestimonial" => (int)$allowTestimonial,
  283.             "requiredTextTestimonial" => (int)$requiredTextTestimonial,
  284.         ];
  285.         $data = [
  286.             "productOffersCertificate" => $offers->productOffersCertificate,
  287.             "productOffersPeriod" => $offers->productOffersPeriod,
  288.             "productOffersSupport" => $offers->productOffersSupport,
  289.             "client" => $this->configuration->getClientInfo(),
  290.             "translations" => $translations,
  291.             "drm" => $this->configuration->getDrmConfiguration(),
  292.             "testimonialConfig" => $testimonialConfig,
  293.             "date" => date('Y-m-d H:i:s'),
  294.             "user" => [
  295.                 "id" => $this->user->getId(),
  296.                 "ip" => $this->generalService->getClientIp(),
  297.                 "name" => $this->user->getName(),
  298.                 "hasUserOrigin" => ($userOrigin true false),
  299.                 "canAccessAdm" => $this->userPermissionUtil->canAccessAdm(),
  300.                 "screenSize" => $this->user->getScreenSize(),
  301.                 "autoplay" => $this->user->getAutoplay(),
  302.                 "document" => $this->user->getDocument(),
  303.                 "username" => $this->user->getUsername(),
  304.                 "token" => $userToken,
  305.                 "photo" => $fileService->getFilePathObj($dataImg),
  306.                 "permissionName" => (
  307.                     $this->user->getUserProfile() ? 
  308.                     $this->user->getUserProfile()->getName() 
  309.                     : null
  310.                 ),
  311.             ],
  312.         ];
  313.         return $data;
  314.     }
  315.     public function getCourseIndex(Enrollment $enrollment): array
  316.     {
  317.         return (array)$this->enrollmentRepository->getCourseIndexByEnrollmentNew($enrollment);
  318.     }
  319. }