src/Services/EntityServices/LessonService.php line 400

Open in your IDE?
  1. <?php
  2. namespace EADPlataforma\Services\EntityServices;
  3. use EADPlataforma\Entity\Lesson;
  4. use EADPlataforma\Entity\User;
  5. use EADPlataforma\Entity\Enrollment;
  6. use EADPlataforma\Entity\LessonLog;
  7. use EADPlataforma\Entity\LessonLogOrigin;
  8. use EADPlataforma\Entity\Library;
  9. use EADPlataforma\Entity\LessonAdvertisement;
  10. use EADPlataforma\Entity\Product;
  11. use EADPlataforma\Enum\UserEnum;
  12. use EADPlataforma\Enum\LessonEnum;
  13. use EADPlataforma\Enum\LibraryEnum;
  14. use EADPlataforma\Enum\ProductEnum;
  15. use EADPlataforma\Enum\LessonAdvertisementEnum;
  16. use EADPlataforma\Repository\LessonRepository;
  17. use EADPlataforma\Repository\LessonLogRepository;
  18. use EADPlataforma\Repository\EnrollmentRepository;
  19. use EADPlataforma\Repository\LibraryRepository;
  20. use EADPlataforma\Repository\LessonAdvertisementRepository;
  21. use EADPlataforma\Repository\ProductRepository;
  22. use EADPlataforma\Services\GeneralService;
  23. use EADPlataforma\Services\SchoolEntityManager;
  24. use EADPlataforma\Services\EadminEntityManager;
  25. use EADPlataforma\Services\ConfigurationService;
  26. use EADPlataforma\Util\UserPermissionUtil;
  27. /**
  28.  * Lesson Service
  29.  */
  30. class LessonService
  31. {
  32.     /**
  33.      * @var SchoolEntityManager $em
  34.      */
  35.     protected $em;
  36.     /**
  37.      * @var EadminEntityManager @em
  38.      */
  39.     protected $emEadmin;
  40.     /**
  41.      * @var LessonModuleRepository
  42.      */
  43.     private $repository;
  44.     /**
  45.      * @var EnrollmentRepository
  46.      */
  47.     private $enrollmentRepository;
  48.     /**
  49.      * @var LessonLogRepository
  50.      */
  51.     private $lessonLogRepository;
  52.     /**
  53.      * @var LibraryRepository
  54.      */
  55.     private $libraryRepository;
  56.     /**
  57.      * @var LessonAdvertisementRepository
  58.      */
  59.     private $laRepository;
  60.     /**
  61.      * @var ProductRepository
  62.      */
  63.     private $productRepository;
  64.     /**
  65.      * @var ConfigurationService
  66.      */
  67.     private $configuration;
  68.     /**
  69.      * @var UserPermissionUtil
  70.      */
  71.     protected $userPermissionUtil;
  72.     /**
  73.      * @var User
  74.      */
  75.     private $user;
  76.     /**
  77.      * Constructor
  78.      *
  79.      * @param GeneralService $generalService
  80.      * @param LessonRepository $lessonRepository
  81.      */
  82.     public function __construct(
  83.         GeneralService $generalService
  84.         LessonRepository $lessonRepository
  85.     )
  86.     {
  87.         $this->repository $lessonRepository;
  88.         $this->em $lessonRepository->em;
  89.         $this->generalService $generalService;
  90.         $this->configuration $this->generalService->getService('ConfigurationService');
  91.         $this->emEadmin $this->generalService->getService('EadminEntityManager');
  92.         $this->enrollmentRepository $this->em->getRepository(Enrollment::class);
  93.         $this->lessonLogRepository $this->emEadmin->getRepository(LessonLog::class);
  94.         $this->libraryRepository $this->em->getRepository(Library::class);
  95.         $this->laRepository $this->em->getRepository(LessonAdvertisement::class);
  96.         $this->productRepository $this->em->getRepository(Product::class);
  97.         
  98.         $this->user $this->generalService->getService('UserSessionService')->getUser();
  99.         $this->userPermissionUtil $this->generalService->getUtil('UserPermissionUtil');
  100.     }
  101.     public function searchLesson(int $lesson): ?Lesson
  102.     {
  103.         return $this->repository->findOneBy([
  104.             "id" => $lesson,
  105.             "deleted" => LessonEnum::ITEM_NO_DELETED
  106.         ]);
  107.     }
  108.     public function searchLessonLog(Lesson $lesson): ?LessonLog
  109.     {
  110.         $course $lesson->getCourse();
  111.         $logId "{$course->getId()}#{$this->user->getId()}#{$lesson->getId()}";
  112.         return $this->lessonLogRepository->find($logId);
  113.     }
  114.     public function isLessonVisibleToStudent(Lesson $lesson): ?bool
  115.     {
  116.         return $this->repository->isLessonVisibleToStudent($lesson);
  117.     }
  118.     public function checkLessonIsAccessibleToUser(
  119.         Lesson $lesson,
  120.         Enrollment $enrollment,
  121.         ?bool $isStudent true
  122.     ): object
  123.     {
  124.         $lessonLog $this->searchLessonLog($lesson);
  125.         $lessonBefore $this->repository->getLessonIdBeforeThat($lessontrue);
  126.         return $this->repository->checkLessonIsAccessibleToUser(
  127.             $lesson
  128.             $enrollment,
  129.             $lessonLog,
  130.             $isStudent,
  131.             ( $lessonBefore $lessonBefore->id null )
  132.         );
  133.     }
  134.     public function getLibraryIndex(Lesson $lesson): ?object
  135.     {
  136.         $library $lesson->getLibrary();
  137.         if(!$library){
  138.             return null;
  139.         }
  140.         $libraryInfo $this->libraryRepository->getContentInfo($libraryfalse$lesson);
  141.         $text $library->getText();
  142.         $type $library->getType();
  143.         $fileExtension $library->getFileExtension();
  144.         if(
  145.             $type == LibraryEnum::CONTENT_TEXT && 
  146.             $library->getShowTextPdf() == LibraryEnum::YES
  147.         ){
  148.             $text null;
  149.             $type LibraryEnum::CONTENT_FILES;
  150.             $fileExtension "pdf";
  151.         }
  152.         $timeUtil $this->generalService->getUtil('DateTimeUtil');
  153.         $duration $library->getDuration();
  154.         $libraryReturn = (object)[
  155.             "id" => $library->getId(),
  156.             "title" => $library->getTitle(),
  157.             "type" => $type,
  158.             "link" => $libraryInfo->url,
  159.             "text" => $text,
  160.             "file" => $library->getId(),
  161.             "fileExtension" => $fileExtension,
  162.             "liveStart" => $library->getLiveStart(),
  163.             "liveEmbedId" => $libraryInfo->embedId,
  164.             "pdfSvgMode" => $library->getPdfSvgMode(),
  165.             "pagesNumber" => $library->getPagesNumber(),
  166.             "duration" => $duration $timeUtil->timeToSec($duration) : null,
  167.             "meetCredentials" => $this->repository->getMeetCredentials($lesson$this->user),
  168.         ];
  169.         $showDocument = ($lesson->getControlShowDocument() == LibraryEnum::YES);
  170.         $drmVideo $this->configuration->get("drm_video");
  171.         $hasLessonControl $this->configuration->checkModuleIsAbleOnPlan(
  172.             'lessonControlFunction'
  173.         );
  174.         $annotate = [];
  175.         if($hasLessonControl && $showDocument && $drmVideo == LibraryEnum::YES){
  176.             $annotate $this->libraryRepository->getVideoDRM($this->user);
  177.         }
  178.         $credentials $this->libraryRepository->getVideoCredentials(
  179.             $library,
  180.             $annotate
  181.         );
  182.         if($credentials){
  183.             $libraryReturn->credentials $credentials;
  184.         }
  185.         return $libraryReturn;
  186.     }
  187.     public function getOfferByLesson(Lesson $lesson): ?array
  188.     {
  189.         $types = [
  190.             LessonAdvertisementEnum::EXECUTE_IN_LESSON_START,
  191.             LessonAdvertisementEnum::EXECUTE_IN_DURING_LESSON,
  192.             LessonAdvertisementEnum::EXECUTE_IN_FINAL_LESSON,
  193.         ];
  194.         $fileService $this->generalService->getService('FileService');
  195.         $offers = [];
  196.         foreach ($types as $key => $type) {
  197.             $advertisementInternal $this->laRepository->getInternalByLesson(
  198.                 $lesson,
  199.                 $type
  200.             );
  201.             $advertisementExternal $this->laRepository->getExternalByLesson(
  202.                 $lesson,
  203.                 $type
  204.             );
  205.             if(!$advertisementInternal && !$advertisementExternal){
  206.                 continue;
  207.             }
  208.             if($advertisementInternal){
  209.                 $productOffer $advertisementInternal->getProductOffer();
  210.                 $product $productOffer->getProduct();
  211.                 if(!$this->productRepository->userHasProduct($this->user$product)){
  212.                     $cover $fileService->getFilePathComplete(
  213.                         $advertisementInternal->getCover(),
  214.                         LessonAdvertisementEnum::PATH_LESSON_AD,
  215.                         true
  216.                     );
  217.                     $linkBuy $this->generalService->generateUrl("cartAdd", [
  218.                         "poID" => $productOffer->getId(),
  219.                         "courseId" => $lesson->getCourse()->getId(),
  220.                     ]);
  221.                     if(
  222.                         $advertisementInternal->getAllowRedirect() == LessonAdvertisementEnum::YES &&
  223.                         !empty($advertisementInternal->getLinkRedirect())
  224.                     ){
  225.                         $linkBuy $advertisementInternal->getLinkRedirect();
  226.                     }
  227.                     $offer = [
  228.                         "id" => $productOffer->getId(),
  229.                         "currencySymbol" => $productOffer->getCurrencySymbol(),
  230.                         "product" => $product->getId(),
  231.                         "type" => $product->getType(),
  232.                         "title" => $advertisementInternal->getTitle(),
  233.                         "price" => $productOffer->getPriceReal(),
  234.                         "priceDisplay" => $productOffer->getPriceDisplay(),
  235.                         "image" => $cover,
  236.                         "description" => $advertisementInternal->getDescription(),
  237.                         "link" => $linkBuy,
  238.                         "buttonText" => $advertisementInternal->getButtonText(),
  239.                         "executionType" => $type,
  240.                         "executionTime" => $advertisementInternal->getTriggerTime(),
  241.                         "exhibitionType" => $advertisementInternal->getExhibitionType(),
  242.                         "modelType" => $advertisementInternal->getModelType(),
  243.                         "endTime" => $advertisementInternal->getEndTime(),
  244.                     ];
  245.                     $offers[] = $offer;
  246.                 }
  247.             }
  248.             if($advertisementExternal){
  249.                 $cover $fileService->getFilePathComplete(
  250.                     $advertisementExternal->getCover(),
  251.                     LessonAdvertisementEnum::PATH_LESSON_AD,
  252.                     true
  253.                 );
  254.                 $linkBuy $advertisementExternal->getLinkRedirect();
  255.                 $offer = [
  256.                     "id" => null,
  257.                     "currencySymbol" => null,
  258.                     "product" => null,
  259.                     "type" => null,
  260.                     "title" => $advertisementExternal->getTitle(),
  261.                     "price" => null,
  262.                     "priceDisplay" => null,
  263.                     "image" => $cover,
  264.                     "description" => $advertisementExternal->getDescription(),
  265.                     "link" => $linkBuy,
  266.                     "buttonText" => $advertisementExternal->getButtonText(),
  267.                     "executionType" => $type,
  268.                     "executionTime" => $advertisementExternal->getTriggerTime(),
  269.                     "exhibitionType" => $advertisementExternal->getExhibitionType(),
  270.                     "modelType" => $advertisementExternal->getModelType(),
  271.                     "endTime" => $advertisementExternal->getEndTime(),
  272.                 ];
  273.                 $offers[] = $offer;
  274.             }
  275.         }
  276.         return !empty($offers) ? $offers null;
  277.     }
  278.     public function getLessonIndex(
  279.         Lesson $lesson,
  280.         Enrollment $enrollment,
  281.         ?bool $isStudent true,
  282.         ?bool $chatNew false
  283.     ): array
  284.     {
  285.         $course $lesson->getCourse();
  286.         $lessonModule $lesson->getLessonModule();
  287.         $lessonLog $this->searchLessonLog($lesson);
  288.         $lessonLogOld $lessonLog;
  289.         $numberAccess null;
  290.         if($lessonLogOld){
  291.             $numberAccess $lessonLogOld->getNumberAccess();
  292.         }
  293.         $hasLessonControl $this->configuration->checkModuleIsAbleOnPlan(
  294.             'lessonControlFunction'
  295.         );
  296.         $controlActive = (
  297.             $lesson->getControlTime() == LessonEnum::YES || 
  298.             !empty($lesson->getControlTimeStay())
  299.         );
  300.         //create user log
  301.         if(!$hasLessonControl || !$controlActive){
  302.             $lessonLog $this->lessonLogRepository->updateOrCreateLessonLog(
  303.                 $lesson,
  304.                 $enrollment
  305.             );
  306.         }
  307.         $infoLog $this->lessonLogRepository->getTimeInfo($lesson$lessonLog);
  308.         $nextContent $this->repository->getLessonNextContent($lesson$lessonLog);
  309.         $lastContent $this->repository->getLessonLastContent($lesson);
  310.         if($lesson->getControlViewLimit() == LessonEnum::YES && $numberAccess){
  311.             if($numberAccess >= $lesson->getControlViewNumber()){
  312.                 throw new \EADPlataforma\Error\ActionInvalidException(
  313.                     $this->configuration->getLanguage('error_lesson_access_limit''lesson_view_error'),
  314.                     [
  315.                         "nextContent" => $nextContent,
  316.                         "lastContent" => $lastContent,
  317.                     ]
  318.                 );
  319.             }
  320.         }
  321.         $chapterService $this->generalService->getService(
  322.             'EntityServices\\LibraryChapterService'
  323.         );
  324.         $library $lesson->getLibrary();
  325.         $data = [
  326.             "nextContent" => $nextContent,
  327.             "lastContent" => $lastContent,
  328.             "id" => $lesson->getId(),
  329.             "module" => (object)[
  330.                 "id" => $lessonModule->getId(),
  331.                 "title" => $lessonModule->getTitle(),
  332.             ],
  333.             "courseId" => $course->getId(),
  334.             "title" => $lesson->getTitle(),
  335.             "description" => $lesson->getDescription(),
  336.             "controlTime" => $hasLessonControl $lesson->getControlTime() : LessonEnum::NO,
  337.             "controlViewLimit" => (
  338.                 $hasLessonControl 
  339.                 $lesson->getControlViewLimit() : 
  340.                 LessonEnum::NO
  341.             ),
  342.             "controlViewNumber" => (
  343.                 $hasLessonControl 
  344.                 $lesson->getControlViewNumber() : 
  345.                 LessonEnum::NO
  346.             ),
  347.             "controlPauseNumber" => (
  348.                 $hasLessonControl 
  349.                 $lesson->getControlPauseNumber() : 
  350.                 LessonEnum::NO
  351.             ),
  352.             "controlShowDocument" => (
  353.                 $hasLessonControl 
  354.                 $lesson->getControlShowDocument() : 
  355.                 LessonEnum::NO
  356.             ),
  357.             "showLiveChat" => $this->repository->getShowLiveChat($lesson$isStudent),
  358.             "teacher" => $this->repository->getLessonTeacher($lesson),
  359.             "rates" => $this->lessonLogRepository->countLessonLogRate(
  360.                 $course->getId(),
  361.                 $lesson->getId()
  362.             ),
  363.             "required" => $lesson->getControlRequirement(),
  364.             "rate" => ($lessonLog $lessonLog->getRate() : LessonEnum::NO),
  365.             "favorite" => ($lessonLog $lessonLog->getFavorite() : LessonEnum::NO),
  366.             "completed" => ($lessonLog $lessonLog->getViewed() : LessonEnum::NO),
  367.             "numberAccess" => ($lessonLog $lessonLog->getNumberAccess() : null),
  368.             "timeToStaySeconds" => $infoLog->timeToStaySeconds,
  369.             "timeRestToComplete" => $infoLog->timeRestToComplete,
  370.             "blockContent" => $this->lessonLogRepository->checkBlockView($lessonLogOld),
  371.             "chat" => (
  372.                 $chatNew ?
  373.                 $this->repository->getLessonChatCredentialsNew($lesson) :
  374.                 $this->repository->getLessonChatCredentials($lesson)
  375.             ),
  376.             "library" => $this->getLibraryIndex($lesson),
  377.             "productOffers" => $this->getOfferByLesson($lesson),
  378.             "chapters" => (
  379.                 $library 
  380.                 $chapterService->searchAllChaptersByLibrary($library->getId()) :
  381.                 null
  382.             )
  383.         ];
  384.         return $data;
  385.     }
  386. }