<?php
namespace EADPlataforma\Services\EntityServices;
use EADPlataforma\Entity\Lesson;
use EADPlataforma\Entity\User;
use EADPlataforma\Entity\Enrollment;
use EADPlataforma\Entity\LessonLog;
use EADPlataforma\Entity\LessonLogOrigin;
use EADPlataforma\Entity\Library;
use EADPlataforma\Entity\LessonAdvertisement;
use EADPlataforma\Entity\Product;
use EADPlataforma\Enum\UserEnum;
use EADPlataforma\Enum\LessonEnum;
use EADPlataforma\Enum\LibraryEnum;
use EADPlataforma\Enum\ProductEnum;
use EADPlataforma\Enum\LessonAdvertisementEnum;
use EADPlataforma\Repository\LessonRepository;
use EADPlataforma\Repository\LessonLogRepository;
use EADPlataforma\Repository\EnrollmentRepository;
use EADPlataforma\Repository\LibraryRepository;
use EADPlataforma\Repository\LessonAdvertisementRepository;
use EADPlataforma\Repository\ProductRepository;
use EADPlataforma\Services\GeneralService;
use EADPlataforma\Services\SchoolEntityManager;
use EADPlataforma\Services\EadminEntityManager;
use EADPlataforma\Services\ConfigurationService;
use EADPlataforma\Util\UserPermissionUtil;
/**
* Lesson Service
*/
class LessonService
{
/**
* @var SchoolEntityManager $em
*/
protected $em;
/**
* @var EadminEntityManager @em
*/
protected $emEadmin;
/**
* @var LessonModuleRepository
*/
private $repository;
/**
* @var EnrollmentRepository
*/
private $enrollmentRepository;
/**
* @var LessonLogRepository
*/
private $lessonLogRepository;
/**
* @var LibraryRepository
*/
private $libraryRepository;
/**
* @var LessonAdvertisementRepository
*/
private $laRepository;
/**
* @var ProductRepository
*/
private $productRepository;
/**
* @var ConfigurationService
*/
private $configuration;
/**
* @var UserPermissionUtil
*/
protected $userPermissionUtil;
/**
* @var User
*/
private $user;
/**
* Constructor
*
* @param GeneralService $generalService
* @param LessonRepository $lessonRepository
*/
public function __construct(
GeneralService $generalService,
LessonRepository $lessonRepository
)
{
$this->repository = $lessonRepository;
$this->em = $lessonRepository->em;
$this->generalService = $generalService;
$this->configuration = $this->generalService->getService('ConfigurationService');
$this->emEadmin = $this->generalService->getService('EadminEntityManager');
$this->enrollmentRepository = $this->em->getRepository(Enrollment::class);
$this->lessonLogRepository = $this->emEadmin->getRepository(LessonLog::class);
$this->libraryRepository = $this->em->getRepository(Library::class);
$this->laRepository = $this->em->getRepository(LessonAdvertisement::class);
$this->productRepository = $this->em->getRepository(Product::class);
$this->user = $this->generalService->getService('UserSessionService')->getUser();
$this->userPermissionUtil = $this->generalService->getUtil('UserPermissionUtil');
}
public function searchLesson(int $lesson): ?Lesson
{
return $this->repository->findOneBy([
"id" => $lesson,
"deleted" => LessonEnum::ITEM_NO_DELETED
]);
}
public function searchLessonLog(Lesson $lesson): ?LessonLog
{
$course = $lesson->getCourse();
$logId = "{$course->getId()}#{$this->user->getId()}#{$lesson->getId()}";
return $this->lessonLogRepository->find($logId);
}
public function isLessonVisibleToStudent(Lesson $lesson, ?bool $isStudent = true): ?bool
{
return $this->repository->isLessonVisibleToStudent($lesson, false, $isStudent);
}
public function checkLessonIsAccessibleToUser(
Lesson $lesson,
Enrollment $enrollment,
?bool $isStudent = true
): object
{
$lessonLog = $this->searchLessonLog($lesson);
$lessonBefore = $this->repository->getLessonIdBeforeThat($lesson, true);
return $this->repository->checkLessonIsAccessibleToUser(
$lesson,
$enrollment,
$lessonLog,
$isStudent,
( $lessonBefore ? $lessonBefore->id : null )
);
}
public function getLibraryIndex(
Lesson $lesson,
Enrollment $enrollment,
?bool $isStudent = true
): ?object
{
$library = $lesson->getLibrary();
if(!$library){
return null;
}
$libraryInfo = $this->libraryRepository->getContentInfo($library, false, $lesson);
$text = $library->getText();
$type = $library->getType();
$fileExtension = $library->getFileExtension();
if(
$type == LibraryEnum::CONTENT_TEXT &&
$library->getShowTextPdf() == LibraryEnum::YES
){
$text = null;
$type = LibraryEnum::CONTENT_FILES;
$fileExtension = "pdf";
}
$timeUtil = $this->generalService->getUtil('DateTimeUtil');
$duration = $library->getDuration();
$infoTag = $this->repository->setLessonTag(
$lesson,
$enrollment->getUser(),
$text,
$libraryInfo->url
);
$showRecording = false;
if(!$isStudent){
if($library->getType() == LibraryEnum::CONTENT_CONFERENCE){
$userRecord = $library->getRecordUser();
if(empty($userRecord) || $userRecord->getId() == $this->user->getId()){
$showRecording = true;
}
}else if($library->getType() == LibraryEnum::CONTENT_NEW_LIVE){
if(!empty($library->getVdocipherVideoId())){
$showRecording = true;
}
}
}
$this->libraryRepository->setRecordSize($library);
$libraryReturn = (object)[
"id" => $library->getId(),
"title" => $library->getTitle(),
"type" => $type,
"link" => $infoTag->link,
"text" => $infoTag->content,
"file" => $library->getId(),
"fileExtension" => $fileExtension,
"liveStart" => $library->getLiveStart(),
"liveEmbedId" => $libraryInfo->embedId,
"pdfSvgMode" => $library->getPdfSvgMode(),
"pagesNumber" => $library->getPagesNumber(),
"duration" => $duration ? $timeUtil->timeToSec($duration) : null,
"showRecording" => $showRecording,
"isRecording" => $library->getRecord(),
"meetCredentials" => $this->repository->getMeetCredentials(
$lesson,
$enrollment->getUser()
),
];
$showDocument = ($lesson->getControlShowDocument() == LibraryEnum::YES);
$drmVideo = $this->configuration->get("drm_video");
$hasLessonControl = $this->configuration->checkModuleIsAbleOnPlan(
'lessonControlFunction'
);
$annotate = [];
if($hasLessonControl && $showDocument && $drmVideo == LibraryEnum::YES){
$annotate = $this->libraryRepository->getVideoDRM($this->user);
}
$credentials = $this->libraryRepository->getVideoCredentials(
$library,
$annotate
);
if($credentials){
$libraryReturn->credentials = $credentials;
}
return $libraryReturn;
}
public function getOfferByLesson(Lesson $lesson): ?array
{
$types = [
LessonAdvertisementEnum::EXECUTE_IN_LESSON_START,
LessonAdvertisementEnum::EXECUTE_IN_DURING_LESSON,
LessonAdvertisementEnum::EXECUTE_IN_FINAL_LESSON,
];
$fileService = $this->generalService->getService('FileService');
$offers = [];
foreach ($types as $key => $type) {
$advertisementInternal = $this->laRepository->getInternalByLesson(
$lesson,
$type
);
$advertisementExternal = $this->laRepository->getExternalByLesson(
$lesson,
$type
);
if(!$advertisementInternal && !$advertisementExternal){
continue;
}
if($advertisementInternal){
$productOffer = $advertisementInternal->getProductOffer();
$product = $productOffer->getProduct();
if(!$this->productRepository->userHasProduct($this->user, $product)){
$cover = $fileService->getFilePathComplete(
$advertisementInternal->getCover(),
LessonAdvertisementEnum::PATH_LESSON_AD,
true
);
$linkBuy = $this->generalService->generateUrl("cartAdd", [
"poID" => $productOffer->getId(),
"courseId" => $lesson->getCourse()->getId(),
]);
if(
$advertisementInternal->getAllowRedirect() == LessonAdvertisementEnum::YES &&
!empty($advertisementInternal->getLinkRedirect())
){
$linkBuy = $advertisementInternal->getLinkRedirect();
}
$offer = [
"id" => $productOffer->getId(),
"currencySymbol" => $productOffer->getCurrencySymbol(),
"product" => $product->getId(),
"type" => $product->getType(),
"title" => $advertisementInternal->getTitle(),
"price" => $productOffer->getPriceReal(),
"priceDisplay" => $productOffer->getPriceDisplay(),
"image" => $cover,
"description" => $advertisementInternal->getDescription(),
"link" => $linkBuy,
"buttonText" => $advertisementInternal->getButtonText(),
"executionType" => $type,
"executionTime" => $advertisementInternal->getTriggerTime(),
"exhibitionType" => $advertisementInternal->getExhibitionType(),
"modelType" => $advertisementInternal->getModelType(),
"endTime" => $advertisementInternal->getEndTime(),
];
$offers[] = $offer;
}
}
if($advertisementExternal){
$cover = $fileService->getFilePathComplete(
$advertisementExternal->getCover(),
LessonAdvertisementEnum::PATH_LESSON_AD,
true
);
$linkBuy = $advertisementExternal->getLinkRedirect();
$offer = [
"id" => null,
"currencySymbol" => null,
"product" => null,
"type" => null,
"title" => $advertisementExternal->getTitle(),
"price" => null,
"priceDisplay" => null,
"image" => $cover,
"description" => $advertisementExternal->getDescription(),
"link" => $linkBuy,
"buttonText" => $advertisementExternal->getButtonText(),
"executionType" => $type,
"executionTime" => $advertisementExternal->getTriggerTime(),
"exhibitionType" => $advertisementExternal->getExhibitionType(),
"modelType" => $advertisementExternal->getModelType(),
"endTime" => $advertisementExternal->getEndTime(),
];
$offers[] = $offer;
}
}
return !empty($offers) ? $offers : null;
}
public function getLessonIndex(
Lesson $lesson,
Enrollment $enrollment,
?bool $isStudent = true,
?bool $chatNew = false
): array
{
$course = $lesson->getCourse();
$lessonModule = $lesson->getLessonModule();
$lessonLog = $this->searchLessonLog($lesson);
$lessonLogOld = $lessonLog;
$numberAccess = null;
if($lessonLogOld){
$numberAccess = $lessonLogOld->getNumberAccess();
}
$hasLessonControl = $this->configuration->checkModuleIsAbleOnPlan(
'lessonControlFunction'
);
$controlActive = (
$lesson->getControlTime() == LessonEnum::YES ||
!empty($lesson->getControlTimeStay())
);
//create user log
if(!$hasLessonControl || !$controlActive){
$lessonLog = $this->lessonLogRepository->updateOrCreateLessonLog(
$lesson,
$enrollment
);
}
$infoLog = $this->lessonLogRepository->getTimeInfo($lesson, $lessonLog);
$nextContent = $this->repository->getLessonNextContent($lesson, $lessonLog);
$lastContent = $this->repository->getLessonLastContent($lesson);
if($lesson->getControlViewLimit() == LessonEnum::YES && $numberAccess){
if($numberAccess >= $lesson->getControlViewNumber()){
throw new \EADPlataforma\Error\ActionInvalidException(
$this->configuration->getLanguage('error_lesson_access_limit', 'lesson_view_error'),
[
"nextContent" => $nextContent,
"lastContent" => $lastContent,
]
);
}
}
$chapterService = $this->generalService->getService(
'EntityServices\\LibraryChapterService'
);
$library = $lesson->getLibrary();
$data = [
"nextContent" => $nextContent,
"lastContent" => $lastContent,
"id" => $lesson->getId(),
"module" => (object)[
"id" => $lessonModule->getId(),
"title" => $lessonModule->getTitle(),
],
"courseId" => $course->getId(),
"title" => $lesson->getTitle(),
"description" => $lesson->getDescription(),
"controlTime" => $hasLessonControl ? $lesson->getControlTime() : LessonEnum::NO,
"controlViewLimit" => (
$hasLessonControl ?
$lesson->getControlViewLimit() :
LessonEnum::NO
),
"controlViewNumber" => (
$hasLessonControl ?
$lesson->getControlViewNumber() :
LessonEnum::NO
),
"controlPauseNumber" => (
$hasLessonControl ?
$lesson->getControlPauseNumber() :
LessonEnum::NO
),
"controlShowDocument" => (
$hasLessonControl ?
$lesson->getControlShowDocument() :
LessonEnum::NO
),
"showLiveChat" => $this->repository->getShowLiveChat($lesson, $isStudent),
"teacher" => $this->repository->getLessonTeacher($lesson),
"rates" => $this->lessonLogRepository->countLessonLogRate(
$course->getId(),
$lesson->getId()
),
"required" => $lesson->getControlRequirement(),
"rate" => ($lessonLog ? $lessonLog->getRate() : LessonEnum::NO),
"favorite" => ($lessonLog ? $lessonLog->getFavorite() : LessonEnum::NO),
"completed" => ($lessonLog ? $lessonLog->getViewed() : LessonEnum::NO),
"numberAccess" => ($lessonLog ? $lessonLog->getNumberAccess() : null),
"timeToStaySeconds" => $infoLog->timeToStaySeconds,
"timeRestToComplete" => $infoLog->timeRestToComplete,
"blockContent" => $this->lessonLogRepository->checkBlockView($lessonLogOld),
"chat" => (
$chatNew ?
$this->repository->getLessonChatCredentialsNew($lesson) :
$this->repository->getLessonChatCredentials($lesson)
),
"library" => $this->getLibraryIndex($lesson, $enrollment, $isStudent),
"productOffers" => $this->getOfferByLesson($lesson),
"chapters" => (
$library ?
$chapterService->searchAllChaptersByLibrary($library->getId()) :
null
)
];
return $data;
}
}