<?php
namespace EADPlataforma\Services\EntityServices;
use EADPlataforma\Entity\Course;
use EADPlataforma\Entity\CourseTeam;
use EADPlataforma\Entity\Enrollment;
use EADPlataforma\Entity\ProductOffer;
use EADPlataforma\Entity\User;
use EADPlataforma\Enum\UserEnum;
use EADPlataforma\Enum\CourseEnum;
use EADPlataforma\Enum\EnrollmentEnum;
use EADPlataforma\Enum\ProductEnum;
use EADPlataforma\Repository\CourseRepository;
use EADPlataforma\Repository\CourseTeamRepository;
use EADPlataforma\Repository\EnrollmentRepository;
use EADPlataforma\Repository\ProductOfferRepository;
use EADPlataforma\Services\GeneralService;
use EADPlataforma\Services\SchoolEntityManager;
use EADPlataforma\Services\ConfigurationService;
use EADPlataforma\Util\UserPermissionUtil;
/**
* CourseIndex Service
*/
class CourseIndexService
{
/**
* @var SchoolEntityManager $em
*/
protected $em;
/**
* @var CourseRepository
*/
private $repository;
/**
* @var CourseTeamRepository
*/
private $courseTeamRepository;
/**
* @var EnrollmentRepository
*/
private $enrollmentRepository;
/**
* @var ProductOfferRepository
*/
private $productOfferRepository;
/**
* @var ConfigurationService
*/
private $configuration;
/**
* @var UserPermissionUtil
*/
protected $userPermissionUtil;
/**
* @var User
*/
private $user;
/**
* @var Array
*/
private $isStudentArray = [];
/**
* Constructor
*
* @param GeneralService $generalService
* @param CourseRepository $courseRepository
*/
public function __construct(
GeneralService $generalService,
CourseRepository $courseRepository
)
{
$this->repository = $courseRepository;
$this->em = $courseRepository->em;
$this->courseTeamRepository = $this->em->getRepository(CourseTeam::class);
$this->enrollmentRepository = $this->em->getRepository(Enrollment::class);
$this->productOfferRepository = $this->em->getRepository(ProductOffer::class);
$this->generalService = $generalService;
$this->configuration = $this->generalService->getService('ConfigurationService');
$this->user = $this->generalService->getService('UserSessionService')->getUser();
$this->userPermissionUtil = $this->generalService->getUtil('UserPermissionUtil');
}
public function searchCourse(int $courseId): ?Course
{
return $this->repository->findOneBy([
"id" => $courseId,
"deleted" => CourseEnum::ITEM_NO_DELETED
]);
}
public function isInTeam(Course $course): bool
{
return $this->courseTeamRepository->userExistInCourseTeam(
$course,
$this->user
);
}
public function isStudent(Course $course): bool
{
$key = "{$this->user->getId()}_{$course->getId()}";
if(isset($this->isStudentArray[$key])){
return $this->isStudentArray[$key];
}
$permission = $this->userPermissionUtil->getPermission("course", "see");
if($this->user->getId() == EnrollmentEnum::YES){
$this->isStudentArray[$key] = false;
return $this->isStudentArray[$key];
}
if($this->userPermissionUtil->isHigh($permission)){
$this->isStudentArray[$key] = false;
return $this->isStudentArray[$key];
}
$isInTeam = $this->isInTeam($course, $this->user);
if($isInTeam && $this->userPermissionUtil->isMiddle($permission)){
$this->isStudentArray[$key] = false;
return $this->isStudentArray[$key];
}
$this->isStudentArray[$key] = true;
return $this->isStudentArray[$key];
}
public function searchEnrollment(Course $course): ?Enrollment
{
$enrollment = $this->enrollmentRepository->findOneBy([
"user" => $this->user->getId(),
"course" => $course->getId(),
"deleted" => EnrollmentEnum::ITEM_NO_DELETED,
], [ "id" => "DESC" ]);
if($enrollment){
return $enrollment;
}
$isStudent = $this->isStudent($course, $this->user);
if($isStudent && $course->getFree() == CourseEnum::NO){
return null;
}
$enrollmentService = $this->generalService->getService('EnrollmentService');
$info = $enrollmentService->enrollUser($this->user, $course);
if(!empty($info->errors)){
return null;
}
return $info->enrollment;
}
public function isValidEnrollment(Course $course): bool
{
$isStudent = $this->isStudent($course, $this->user);
if(!$isStudent){
return true;
}
return $this->enrollmentRepository->isValidEnrollmentByUser(
$this->user->getId(),
$course->getId()
);
}
public function isEnrollment(Course $course): bool
{
$isStudent = $this->isStudent($course, $this->user);
if(!$isStudent){
return true;
}
return $this->enrollmentRepository->isEnrollment(
$this->user->getId(),
$course->getId()
);
}
public function getOffersPeriod(Enrollment $enrollment)
{
$course = $enrollment->getCourse();
$findAllPeriods = false;
$dateNow = new \Datetime();
$datePeriodEnrollment = new \Datetime($enrollment->getDatePeriod());
$deadlineEndPeriod = $dateNow->diff($datePeriodEnrollment);
$productOffersPeriod = [];
if($deadlineEndPeriod->format('%r%a') <= 30){
$findAllPeriods = true;
$productOffersPeriod = $this->productOfferRepository->getProductOffersByCourse(
$course,
ProductEnum::PERIOD
);
}
foreach ($productOffersPeriod as $key => $productOffer) {
$productOffersPeriod[$key] = $productOffer->toReturnClean();
$productOffersPeriod[$key]->type = ProductEnum::PERIOD;
$linkBuy = $this->generalService->generateUrl("cartAdd", [
"poID" => $productOffer->getId(),
"courseId" => $course->getId(),
]);
$productOffersPeriod[$key]->link = $linkBuy;
}
$dateSupportEnrollment = new \Datetime($enrollment->getDateSupport());
$deadlineEndSupport = $dateNow->diff($dateSupportEnrollment);
$productOffersSupport = [];
if($deadlineEndSupport->format('%r%a') <= 30){
$findAllPeriods = true;
$productOffersSupport = $this->productOfferRepository->getProductOffersByCourse(
$course,
ProductEnum::SUPPORT
);
}
foreach ($productOffersSupport as $key => $productOffer) {
$productOffersSupport[$key] = $productOffer->toReturnClean();
$productOffersSupport[$key]->type = ProductEnum::SUPPORT;
$linkBuy = $this->generalService->generateUrl("cartAdd", [
"poID" => $productOffer->getId(),
"courseId" => $course->getId(),
]);
$productOffersSupport[$key]->link = $linkBuy;
}
$productOffersPeriodSupport = [];
if($findAllPeriods){
$productOffersPeriodSupport = $this->productOfferRepository->getProductOffersByCourse(
$course,
ProductEnum::PERIOD_SUPORT
);
}
foreach ($productOffersPeriodSupport as $key => $productOffer) {
$productOffersPeriodSupport[$key] = $productOffer->toReturnClean();
$productOffersPeriodSupport[$key]->type = ProductEnum::PERIOD_SUPORT;
$linkBuy = $this->generalService->generateUrl("cartAdd", [
"poID" => $productOffer->getId(),
"courseId" => $course->getId(),
]);
$productOffersPeriodSupport[$key]->link = $linkBuy;
}
$certificateOffer = null;
if($enrollment->getCertificate() == EnrollmentEnum::NO){
$certificateOffer = $this->productOfferRepository->getProductOfferCertificateByCourse(
$course
);
if($certificateOffer){
$linkBuy = $this->generalService->generateUrl("cartAdd", [
"poID" => $certificateOffer->getId(),
"courseId" => $course->getId(),
]);
$certificateOffer = (object)$certificateOffer->toReturnClean();
$certificateOffer->type = ProductEnum::CERTIFICATE;
$certificateOffer->link = $linkBuy;
}
}
return (object)[
"productOffersCertificate" => $certificateOffer,
"productOffersPeriod" => array_merge(
$productOffersPeriodSupport,
$productOffersPeriod
),
"productOffersSupport" => array_merge(
$productOffersPeriodSupport,
$productOffersSupport
),
];
}
public function getCourseNewGeneral(Course $course, Enrollment $enrollment): array
{
$session = $this->user->getSession();
$userOrigin = $session->getUserOrigin();
$fileService = $this->generalService->getService('FileService');
$dataImg = [
"fileName" => $this->user->getPhoto(),
"pathConst" => UserEnum::PATH_PROFILES,
"option" => "l-user-profile",
"addUpload" => true,
"addStream" => true,
];
$translations = ([
"translations" => $this->configuration->getLanguageModule('lesson_view'),
"language" => $this->configuration->getSystemLanguage()
]);
$offers = $this->getOffersPeriod($enrollment);
$clientConfig = $this->configuration->getClientConfiguration();
$userSessionService = $this->generalService->getService('UserSessionService');
$session = $userSessionService->getSession();
$userToken = md5($clientConfig->getId()) . md5(
$session->getId() . $session->getToken()
);
$allowTestimonial = $this->configuration->get('allow_testimonial');
$requiredTextTestimonial = $this->configuration->get('required_text_testimonial');
$testimonialConfig = (object)[
"allowTestimonial" => (int)$allowTestimonial,
"requiredTextTestimonial" => (int)$requiredTextTestimonial,
];
$data = [
"productOffersCertificate" => $offers->productOffersCertificate,
"productOffersPeriod" => $offers->productOffersPeriod,
"productOffersSupport" => $offers->productOffersSupport,
"client" => $this->configuration->getClientInfo(),
"translations" => $translations,
"drm" => $this->configuration->getDrmConfiguration(),
"testimonialConfig" => $testimonialConfig,
"date" => date('Y-m-d H:i:s'),
"user" => [
"id" => $this->user->getId(),
"ip" => $this->generalService->getClientIp(),
"name" => $this->user->getName(),
"hasUserOrigin" => ($userOrigin ? true : false),
"canAccessAdm" => $this->userPermissionUtil->canAccessAdm(),
"screenSize" => $this->user->getScreenSize(),
"autoplay" => $this->user->getAutoplay(),
"document" => $this->user->getDocument(),
"username" => $this->user->getUsername(),
"token" => $userToken,
"photo" => $fileService->getFilePathObj($dataImg),
"permissionName" => (
$this->user->getUserProfile() ?
$this->user->getUserProfile()->getName()
: null
),
],
];
return $data;
}
public function getCourseIndex(Enrollment $enrollment): array
{
return (array)$this->enrollmentRepository->getCourseIndexByEnrollmentNew($enrollment);
}
}