src/Services/EntityServices/LessonService.php line 434

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 $isStudent true): ?bool
  115.     {
  116.         return $this->repository->isLessonVisibleToStudent($lessonfalse$isStudent);
  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(
  135.         Lesson $lesson
  136.         Enrollment $enrollment,
  137.         ?bool $isStudent true
  138.     ): ?object
  139.     {
  140.         $library $lesson->getLibrary();
  141.         if(!$library){
  142.             return null;
  143.         }
  144.         $libraryInfo $this->libraryRepository->getContentInfo($libraryfalse$lesson);
  145.         $text $library->getText();
  146.         $type $library->getType();
  147.         $fileExtension $library->getFileExtension();
  148.         if(
  149.             $type == LibraryEnum::CONTENT_TEXT && 
  150.             $library->getShowTextPdf() == LibraryEnum::YES
  151.         ){
  152.             $text null;
  153.             $type LibraryEnum::CONTENT_FILES;
  154.             $fileExtension "pdf";
  155.         }
  156.         $timeUtil $this->generalService->getUtil('DateTimeUtil');
  157.         $duration $library->getDuration();
  158.         $infoTag $this->repository->setLessonTag(
  159.             $lesson
  160.             $enrollment->getUser(), 
  161.             $text
  162.             $libraryInfo->url
  163.         );
  164.         $showRecording false;
  165.         if(!$isStudent){
  166.             if($library->getType() == LibraryEnum::CONTENT_CONFERENCE){
  167.                 $userRecord $library->getRecordUser();
  168.                 if(empty($userRecord) || $userRecord->getId() == $this->user->getId()){
  169.                     $showRecording true;
  170.                 }
  171.             }else if($library->getType() == LibraryEnum::CONTENT_NEW_LIVE){
  172.                 if(!empty($library->getVdocipherVideoId())){
  173.                     $showRecording true;
  174.                 }
  175.             }
  176.         }
  177.         $this->libraryRepository->setRecordSize($library);
  178.         $libraryReturn = (object)[
  179.             "id" => $library->getId(),
  180.             "title" => $library->getTitle(),
  181.             "type" => $type,
  182.             "link" => $infoTag->link,
  183.             "text" => $infoTag->content,
  184.             "file" => $library->getId(),
  185.             "fileExtension" => $fileExtension,
  186.             "liveStart" => $library->getLiveStart(),
  187.             "liveEmbedId" => $libraryInfo->embedId,
  188.             "pdfSvgMode" => $library->getPdfSvgMode(),
  189.             "pagesNumber" => $library->getPagesNumber(),
  190.             "duration" => $duration $timeUtil->timeToSec($duration) : null,
  191.             "showRecording" => $showRecording,
  192.             "isRecording" => $library->getRecord(),
  193.             "meetCredentials" => $this->repository->getMeetCredentials(
  194.                 $lesson
  195.                 $enrollment->getUser()
  196.             ),
  197.         ];
  198.         $showDocument = ($lesson->getControlShowDocument() == LibraryEnum::YES);
  199.         $drmVideo $this->configuration->get("drm_video");
  200.         $hasLessonControl $this->configuration->checkModuleIsAbleOnPlan(
  201.             'lessonControlFunction'
  202.         );
  203.         $annotate = [];
  204.         if($hasLessonControl && $showDocument && $drmVideo == LibraryEnum::YES){
  205.             $annotate $this->libraryRepository->getVideoDRM($this->user);
  206.         }
  207.         $credentials $this->libraryRepository->getVideoCredentials(
  208.             $library,
  209.             $annotate
  210.         );
  211.         if($credentials){
  212.             $libraryReturn->credentials $credentials;
  213.         }
  214.         return $libraryReturn;
  215.     }
  216.     public function getOfferByLesson(Lesson $lesson): ?array
  217.     {
  218.         $types = [
  219.             LessonAdvertisementEnum::EXECUTE_IN_LESSON_START,
  220.             LessonAdvertisementEnum::EXECUTE_IN_DURING_LESSON,
  221.             LessonAdvertisementEnum::EXECUTE_IN_FINAL_LESSON,
  222.         ];
  223.         $fileService $this->generalService->getService('FileService');
  224.         $offers = [];
  225.         foreach ($types as $key => $type) {
  226.             $advertisementInternal $this->laRepository->getInternalByLesson(
  227.                 $lesson,
  228.                 $type
  229.             );
  230.             $advertisementExternal $this->laRepository->getExternalByLesson(
  231.                 $lesson,
  232.                 $type
  233.             );
  234.             if(!$advertisementInternal && !$advertisementExternal){
  235.                 continue;
  236.             }
  237.             if($advertisementInternal){
  238.                 $productOffer $advertisementInternal->getProductOffer();
  239.                 $product $productOffer->getProduct();
  240.                 if(!$this->productRepository->userHasProduct($this->user$product)){
  241.                     $cover $fileService->getFilePathComplete(
  242.                         $advertisementInternal->getCover(),
  243.                         LessonAdvertisementEnum::PATH_LESSON_AD,
  244.                         true
  245.                     );
  246.                     $linkBuy $this->generalService->generateUrl("cartAdd", [
  247.                         "poID" => $productOffer->getId(),
  248.                         "courseId" => $lesson->getCourse()->getId(),
  249.                     ]);
  250.                     if(
  251.                         $advertisementInternal->getAllowRedirect() == LessonAdvertisementEnum::YES &&
  252.                         !empty($advertisementInternal->getLinkRedirect())
  253.                     ){
  254.                         $linkBuy $advertisementInternal->getLinkRedirect();
  255.                     }
  256.                     $offer = [
  257.                         "id" => $productOffer->getId(),
  258.                         "currencySymbol" => $productOffer->getCurrencySymbol(),
  259.                         "product" => $product->getId(),
  260.                         "type" => $product->getType(),
  261.                         "title" => $advertisementInternal->getTitle(),
  262.                         "price" => $productOffer->getPriceReal(),
  263.                         "priceDisplay" => $productOffer->getPriceDisplay(),
  264.                         "image" => $cover,
  265.                         "description" => $advertisementInternal->getDescription(),
  266.                         "link" => $linkBuy,
  267.                         "buttonText" => $advertisementInternal->getButtonText(),
  268.                         "executionType" => $type,
  269.                         "executionTime" => $advertisementInternal->getTriggerTime(),
  270.                         "exhibitionType" => $advertisementInternal->getExhibitionType(),
  271.                         "modelType" => $advertisementInternal->getModelType(),
  272.                         "endTime" => $advertisementInternal->getEndTime(),
  273.                     ];
  274.                     $offers[] = $offer;
  275.                 }
  276.             }
  277.             if($advertisementExternal){
  278.                 $cover $fileService->getFilePathComplete(
  279.                     $advertisementExternal->getCover(),
  280.                     LessonAdvertisementEnum::PATH_LESSON_AD,
  281.                     true
  282.                 );
  283.                 $linkBuy $advertisementExternal->getLinkRedirect();
  284.                 $offer = [
  285.                     "id" => null,
  286.                     "currencySymbol" => null,
  287.                     "product" => null,
  288.                     "type" => null,
  289.                     "title" => $advertisementExternal->getTitle(),
  290.                     "price" => null,
  291.                     "priceDisplay" => null,
  292.                     "image" => $cover,
  293.                     "description" => $advertisementExternal->getDescription(),
  294.                     "link" => $linkBuy,
  295.                     "buttonText" => $advertisementExternal->getButtonText(),
  296.                     "executionType" => $type,
  297.                     "executionTime" => $advertisementExternal->getTriggerTime(),
  298.                     "exhibitionType" => $advertisementExternal->getExhibitionType(),
  299.                     "modelType" => $advertisementExternal->getModelType(),
  300.                     "endTime" => $advertisementExternal->getEndTime(),
  301.                 ];
  302.                 $offers[] = $offer;
  303.             }
  304.         }
  305.         return !empty($offers) ? $offers null;
  306.     }
  307.     public function getLessonIndex(
  308.         Lesson $lesson,
  309.         Enrollment $enrollment,
  310.         ?bool $isStudent true,
  311.         ?bool $chatNew false
  312.     ): array
  313.     {
  314.         $course $lesson->getCourse();
  315.         $lessonModule $lesson->getLessonModule();
  316.         $lessonLog $this->searchLessonLog($lesson);
  317.         $lessonLogOld $lessonLog;
  318.         $numberAccess null;
  319.         if($lessonLogOld){
  320.             $numberAccess $lessonLogOld->getNumberAccess();
  321.         }
  322.         $hasLessonControl $this->configuration->checkModuleIsAbleOnPlan(
  323.             'lessonControlFunction'
  324.         );
  325.         $controlActive = (
  326.             $lesson->getControlTime() == LessonEnum::YES || 
  327.             !empty($lesson->getControlTimeStay())
  328.         );
  329.         //create user log
  330.         if(!$hasLessonControl || !$controlActive){
  331.             $lessonLog $this->lessonLogRepository->updateOrCreateLessonLog(
  332.                 $lesson,
  333.                 $enrollment
  334.             );
  335.         }
  336.         $infoLog $this->lessonLogRepository->getTimeInfo($lesson$lessonLog);
  337.         $nextContent $this->repository->getLessonNextContent($lesson$lessonLog);
  338.         $lastContent $this->repository->getLessonLastContent($lesson);
  339.         if($lesson->getControlViewLimit() == LessonEnum::YES && $numberAccess){
  340.             if($numberAccess >= $lesson->getControlViewNumber()){
  341.                 throw new \EADPlataforma\Error\ActionInvalidException(
  342.                     $this->configuration->getLanguage('error_lesson_access_limit''lesson_view_error'),
  343.                     [
  344.                         "nextContent" => $nextContent,
  345.                         "lastContent" => $lastContent,
  346.                     ]
  347.                 );
  348.             }
  349.         }
  350.         $chapterService $this->generalService->getService(
  351.             'EntityServices\\LibraryChapterService'
  352.         );
  353.         $library $lesson->getLibrary();
  354.         $data = [
  355.             "nextContent" => $nextContent,
  356.             "lastContent" => $lastContent,
  357.             "id" => $lesson->getId(),
  358.             "module" => (object)[
  359.                 "id" => $lessonModule->getId(),
  360.                 "title" => $lessonModule->getTitle(),
  361.             ],
  362.             "courseId" => $course->getId(),
  363.             "title" => $lesson->getTitle(),
  364.             "description" => $lesson->getDescription(),
  365.             "controlTime" => $hasLessonControl $lesson->getControlTime() : LessonEnum::NO,
  366.             "controlViewLimit" => (
  367.                 $hasLessonControl 
  368.                 $lesson->getControlViewLimit() : 
  369.                 LessonEnum::NO
  370.             ),
  371.             "controlViewNumber" => (
  372.                 $hasLessonControl 
  373.                 $lesson->getControlViewNumber() : 
  374.                 LessonEnum::NO
  375.             ),
  376.             "controlPauseNumber" => (
  377.                 $hasLessonControl 
  378.                 $lesson->getControlPauseNumber() : 
  379.                 LessonEnum::NO
  380.             ),
  381.             "controlShowDocument" => (
  382.                 $hasLessonControl 
  383.                 $lesson->getControlShowDocument() : 
  384.                 LessonEnum::NO
  385.             ),
  386.             "showLiveChat" => $this->repository->getShowLiveChat($lesson$isStudent),
  387.             "teacher" => $this->repository->getLessonTeacher($lesson),
  388.             "rates" => $this->lessonLogRepository->countLessonLogRate(
  389.                 $course->getId(),
  390.                 $lesson->getId()
  391.             ),
  392.             "required" => $lesson->getControlRequirement(),
  393.             "rate" => ($lessonLog $lessonLog->getRate() : LessonEnum::NO),
  394.             "favorite" => ($lessonLog $lessonLog->getFavorite() : LessonEnum::NO),
  395.             "completed" => ($lessonLog $lessonLog->getViewed() : LessonEnum::NO),
  396.             "numberAccess" => ($lessonLog $lessonLog->getNumberAccess() : null),
  397.             "timeToStaySeconds" => $infoLog->timeToStaySeconds,
  398.             "timeRestToComplete" => $infoLog->timeRestToComplete,
  399.             "blockContent" => $this->lessonLogRepository->checkBlockView($lessonLogOld),
  400.             "chat" => (
  401.                 $chatNew ?
  402.                 $this->repository->getLessonChatCredentialsNew($lesson) :
  403.                 $this->repository->getLessonChatCredentials($lesson)
  404.             ),
  405.             "library" => $this->getLibraryIndex($lesson$enrollment$isStudent),
  406.             "productOffers" => $this->getOfferByLesson($lesson),
  407.             "chapters" => (
  408.                 $library 
  409.                 $chapterService->searchAllChaptersByLibrary($library->getId()) :
  410.                 null
  411.             )
  412.         ];
  413.         return $data;
  414.     }
  415. }