|
|
|
@ -8,10 +8,10 @@ namespace Chamilo\CoreBundle\Command; |
|
|
|
|
|
|
|
|
|
use Chamilo\CoreBundle\Entity\Session; |
|
|
|
|
use Chamilo\CoreBundle\Entity\SessionRelUser; |
|
|
|
|
use Chamilo\CourseBundle\Entity\CLp; |
|
|
|
|
use Chamilo\CourseBundle\Repository\CLpRepository; |
|
|
|
|
use Chamilo\CoreBundle\Entity\SessionRelCourse; |
|
|
|
|
use Chamilo\CoreBundle\Entity\GradebookCertificate; |
|
|
|
|
use Chamilo\CoreBundle\Repository\SessionRepository; |
|
|
|
|
use Chamilo\CourseBundle\Entity\CLpView; |
|
|
|
|
use Chamilo\CoreBundle\Repository\GradebookCertificateRepository; |
|
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
|
|
|
use Symfony\Component\Console\Command\Command; |
|
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
|
|
@ -22,25 +22,25 @@ class ReinscriptionCheckCommand extends Command |
|
|
|
|
{ |
|
|
|
|
protected static $defaultName = 'app:reinscription-check'; |
|
|
|
|
|
|
|
|
|
private CLpRepository $lpRepository; |
|
|
|
|
private SessionRepository $sessionRepository; |
|
|
|
|
private GradebookCertificateRepository $certificateRepository; |
|
|
|
|
private EntityManagerInterface $entityManager; |
|
|
|
|
|
|
|
|
|
public function __construct( |
|
|
|
|
CLpRepository $lpRepository, |
|
|
|
|
SessionRepository $sessionRepository, |
|
|
|
|
GradebookCertificateRepository $certificateRepository, |
|
|
|
|
EntityManagerInterface $entityManager |
|
|
|
|
) { |
|
|
|
|
parent::__construct(); |
|
|
|
|
$this->lpRepository = $lpRepository; |
|
|
|
|
$this->sessionRepository = $sessionRepository; |
|
|
|
|
$this->certificateRepository = $certificateRepository; |
|
|
|
|
$this->entityManager = $entityManager; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected function configure(): void |
|
|
|
|
{ |
|
|
|
|
$this |
|
|
|
|
->setDescription('Checks for users whose course completions have expired and reinscribe them into new sessions if needed.') |
|
|
|
|
->setDescription('Checks for users who have validated all gradebooks and reinscribe them into new sessions if needed.') |
|
|
|
|
->addOption( |
|
|
|
|
'debug', |
|
|
|
|
null, |
|
|
|
@ -53,93 +53,201 @@ class ReinscriptionCheckCommand extends Command |
|
|
|
|
{ |
|
|
|
|
$debug = $input->getOption('debug'); |
|
|
|
|
|
|
|
|
|
$expiredViews = $this->lpRepository->findExpiredViews(0); |
|
|
|
|
$sessions = $this->sessionRepository->findAll(); |
|
|
|
|
|
|
|
|
|
if ($debug) { |
|
|
|
|
$output->writeln(sprintf('Found %d expired views.', count($expiredViews))); |
|
|
|
|
} |
|
|
|
|
foreach ($sessions as $session) { |
|
|
|
|
if ($session->getValidityInDays() === null) { |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
foreach ($expiredViews as $view) { |
|
|
|
|
$user = $view->getUser(); |
|
|
|
|
$session = $view->getSession(); |
|
|
|
|
$users = $this->getUsersForSession($session); |
|
|
|
|
|
|
|
|
|
if ($this->isUserAlreadyEnrolledInChildSession($user, $session)) { |
|
|
|
|
foreach ($users as $user) { |
|
|
|
|
if ($debug) { |
|
|
|
|
$output->writeln(sprintf('User %d is already enrolled in a valid child session.', $user->getId())); |
|
|
|
|
$output->writeln(sprintf('Processing user %d in session %d.', $user->getId(), $session->getId())); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if ($this->isUserReinscribed($user, $session)) { |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if ($this->isUserAlreadyEnrolledInChildSession($user, $session)) { |
|
|
|
|
if ($debug) { |
|
|
|
|
$output->writeln(sprintf('User %d is already enrolled in a valid child session.', $user->getId())); |
|
|
|
|
} |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$certificates = $this->getUserCertificatesForSession($user, $session); |
|
|
|
|
|
|
|
|
|
if ($this->hasUserValidatedAllGradebooks($session, $certificates)) { |
|
|
|
|
$latestValidationDate = $this->getLatestCertificateDate($certificates); |
|
|
|
|
|
|
|
|
|
if ($latestValidationDate !== null) { |
|
|
|
|
$reinscriptionDate = (clone $latestValidationDate)->modify("+{$session->getValidityInDays()} days"); |
|
|
|
|
|
|
|
|
|
if ($debug) { |
|
|
|
|
$output->writeln(sprintf( |
|
|
|
|
'User %d - Latest certificate date: %s, Reinscription date: %s', |
|
|
|
|
$user->getId(), |
|
|
|
|
$latestValidationDate->format('Y-m-d'), |
|
|
|
|
$reinscriptionDate->format('Y-m-d') |
|
|
|
|
)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (new \DateTime() >= $reinscriptionDate) { |
|
|
|
|
$validSession = $this->findValidSessionInHierarchy($session); |
|
|
|
|
|
|
|
|
|
if ($validSession) { |
|
|
|
|
$this->enrollUserInSession($user, $validSession, $session); |
|
|
|
|
if ($debug) { |
|
|
|
|
$output->writeln(sprintf( |
|
|
|
|
'User %d re-enrolled into session %d.', |
|
|
|
|
$user->getId(), |
|
|
|
|
$validSession->getId() |
|
|
|
|
)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
if ($debug) { |
|
|
|
|
$output->writeln(sprintf( |
|
|
|
|
'User %d has no valid certificates for session %d.', |
|
|
|
|
$user->getId(), |
|
|
|
|
$session->getId() |
|
|
|
|
)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$lp = $view->getLp(); |
|
|
|
|
return Command::SUCCESS; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Retrieves all users associated with the session. |
|
|
|
|
*/ |
|
|
|
|
private function getUsersForSession(Session $session): array |
|
|
|
|
{ |
|
|
|
|
$usersToNotify = []; |
|
|
|
|
$sessionCourses = $this->entityManager->getRepository(SessionRelCourse::class)->findBy(['session' => $session]); |
|
|
|
|
|
|
|
|
|
foreach ($sessionCourses as $courseRel) { |
|
|
|
|
$course = $courseRel->getCourse(); |
|
|
|
|
|
|
|
|
|
if ($debug) { |
|
|
|
|
$output->writeln(sprintf( |
|
|
|
|
'User %d completed course %d associated with session %d, and its validity has expired.', |
|
|
|
|
$user->getId(), |
|
|
|
|
$lp->getIid(), |
|
|
|
|
$session->getId() |
|
|
|
|
)); |
|
|
|
|
$studentSubscriptions = $session->getSessionRelCourseRelUsersByStatus($course, Session::STUDENT); |
|
|
|
|
foreach ($studentSubscriptions as $studentSubscription) { |
|
|
|
|
$usersToNotify[$studentSubscription->getUser()->getId()] = $studentSubscription->getUser(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$validSession = $this->findValidSessionInHierarchy($session); |
|
|
|
|
$coachSubscriptions = $session->getSessionRelCourseRelUsersByStatus($course, Session::COURSE_COACH); |
|
|
|
|
foreach ($coachSubscriptions as $coachSubscription) { |
|
|
|
|
$usersToNotify[$coachSubscription->getUser()->getId()] = $coachSubscription->getUser(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if ($validSession) { |
|
|
|
|
$this->enrollUserInSession($user, $validSession); |
|
|
|
|
if ($debug) { |
|
|
|
|
$output->writeln(sprintf( |
|
|
|
|
'User %d re-enrolled into session %d.', |
|
|
|
|
$user->getId(), |
|
|
|
|
$validSession->getId() |
|
|
|
|
)); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
if ($debug) { |
|
|
|
|
$output->writeln(sprintf( |
|
|
|
|
'No valid session found for user %d.', |
|
|
|
|
$user->getId() |
|
|
|
|
)); |
|
|
|
|
} |
|
|
|
|
$generalCoaches = $session->getGeneralCoaches(); |
|
|
|
|
foreach ($generalCoaches as $generalCoach) { |
|
|
|
|
$usersToNotify[$generalCoach->getId()] = $generalCoach; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return array_values($usersToNotify); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Checks if the user is already enrolled in a valid child session. |
|
|
|
|
*/ |
|
|
|
|
private function isUserAlreadyEnrolledInChildSession($user, $parentSession): bool |
|
|
|
|
{ |
|
|
|
|
$childSessions = $this->sessionRepository->findChildSessions($parentSession); |
|
|
|
|
|
|
|
|
|
foreach ($childSessions as $childSession) { |
|
|
|
|
if ($this->findUserSubscriptionInSession($user, $childSession)) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return Command::SUCCESS; |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Find users with expired completion based on "validity_in_days". |
|
|
|
|
* Gets the user's certificates for the courses in the session. |
|
|
|
|
*/ |
|
|
|
|
private function findExpiredCompletions($lp, $validityDays) |
|
|
|
|
private function getUserCertificatesForSession($user, Session $session): array |
|
|
|
|
{ |
|
|
|
|
$now = new \DateTime(); |
|
|
|
|
$expirationDate = (clone $now)->modify('-' . $validityDays . ' days'); |
|
|
|
|
|
|
|
|
|
// Find users with 100% completion and whose last access date (start_time) is older than 'validity_in_days' |
|
|
|
|
return $this->entityManager->getRepository(CLpView::class) |
|
|
|
|
->createQueryBuilder('v') |
|
|
|
|
->innerJoin('Chamilo\CourseBundle\Entity\CLpItemView', 'iv', 'WITH', 'iv.view = v') |
|
|
|
|
->where('v.lp = :lp') |
|
|
|
|
->andWhere('v.progress = 100') |
|
|
|
|
->andWhere('iv.startTime < :expirationDate') |
|
|
|
|
->setParameter('lp', $lp) |
|
|
|
|
->setParameter('expirationDate', $expirationDate->getTimestamp()) |
|
|
|
|
$courses = $this->entityManager->getRepository(SessionRelCourse::class) |
|
|
|
|
->findBy(['session' => $session]); |
|
|
|
|
|
|
|
|
|
$courseIds = array_map(fn($rel) => $rel->getCourse()->getId(), $courses); |
|
|
|
|
|
|
|
|
|
return $this->certificateRepository->createQueryBuilder('gc') |
|
|
|
|
->join('gc.category', 'cat') |
|
|
|
|
->where('gc.user = :user') |
|
|
|
|
->andWhere('cat.course IN (:courses)') |
|
|
|
|
->setParameter('user', $user) |
|
|
|
|
->setParameter('courses', $courseIds) |
|
|
|
|
->getQuery() |
|
|
|
|
->getResult(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Enrolls a user into a session. |
|
|
|
|
* Checks if the user has validated all gradebooks in the session. |
|
|
|
|
*/ |
|
|
|
|
private function hasUserValidatedAllGradebooks(Session $session, array $certificates): bool |
|
|
|
|
{ |
|
|
|
|
$courses = $this->entityManager->getRepository(SessionRelCourse::class) |
|
|
|
|
->findBy(['session' => $session]); |
|
|
|
|
|
|
|
|
|
return count($certificates) === count($courses); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns the latest certificate creation date. |
|
|
|
|
*/ |
|
|
|
|
private function enrollUserInSession($user, $session): void |
|
|
|
|
private function getLatestCertificateDate(array $certificates): ?\DateTime |
|
|
|
|
{ |
|
|
|
|
$existingSubscription = $this->findUserSubscriptionInSession($user, $session); |
|
|
|
|
$dates = array_map(fn($cert) => $cert->getCreatedAt(), $certificates); |
|
|
|
|
|
|
|
|
|
if (empty($dates)) { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return max($dates); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Enrolls the user in a new session and updates the previous session subscription. |
|
|
|
|
*/ |
|
|
|
|
private function enrollUserInSession($user, $newSession, $oldSession): void |
|
|
|
|
{ |
|
|
|
|
$existingSubscription = $this->findUserSubscriptionInSession($user, $newSession); |
|
|
|
|
|
|
|
|
|
if (!$existingSubscription) { |
|
|
|
|
$session->addUserInSession(Session::STUDENT, $user); |
|
|
|
|
$this->entityManager->persist($session); |
|
|
|
|
$newSession->addUserInSession(Session::STUDENT, $user); |
|
|
|
|
|
|
|
|
|
$subscription = $this->findUserSubscriptionInSession($user, $oldSession); |
|
|
|
|
if ($subscription) { |
|
|
|
|
$subscription->setNewSubscriptionSessionId($newSession->getId()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$this->entityManager->persist($newSession); |
|
|
|
|
$this->entityManager->flush(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Determines if the user has already been reinscribed. |
|
|
|
|
*/ |
|
|
|
|
private function isUserReinscribed($user, Session $session): bool |
|
|
|
|
{ |
|
|
|
|
$subscription = $this->findUserSubscriptionInSession($user, $session); |
|
|
|
|
return $subscription && $subscription->getNewSubscriptionSessionId() !== null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Finds the user's subscription in the specified session. |
|
|
|
|
*/ |
|
|
|
|
private function findUserSubscriptionInSession($user, $session) |
|
|
|
|
{ |
|
|
|
|
return $this->entityManager->getRepository(SessionRelUser::class) |
|
|
|
@ -149,6 +257,9 @@ class ReinscriptionCheckCommand extends Command |
|
|
|
|
]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Finds a valid session within the session hierarchy. |
|
|
|
|
*/ |
|
|
|
|
private function findValidSessionInHierarchy(Session $session): ?Session |
|
|
|
|
{ |
|
|
|
|
$childSessions = $this->sessionRepository->findChildSessions($session); |
|
|
|
@ -159,36 +270,14 @@ class ReinscriptionCheckCommand extends Command |
|
|
|
|
if (new \DateTime() <= $validUntil) { |
|
|
|
|
return $child; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$validChild = $this->findValidSessionInHierarchy($child); |
|
|
|
|
if ($validChild) { |
|
|
|
|
return $validChild; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* @var Session $parentSession */ |
|
|
|
|
$parentSession = $this->sessionRepository->findParentSession($session); |
|
|
|
|
|
|
|
|
|
if ($parentSession) { |
|
|
|
|
$validUntil = (clone $parentSession->getAccessEndDate())->modify("-{$parentSession->getDaysToReinscription()} days"); |
|
|
|
|
if (new \DateTime() <= $validUntil) { |
|
|
|
|
return $parentSession; |
|
|
|
|
} |
|
|
|
|
if ($parentSession && new \DateTime() <= $parentSession->getAccessEndDate()) { |
|
|
|
|
return $parentSession; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private function isUserAlreadyEnrolledInChildSession($user, $parentSession): bool |
|
|
|
|
{ |
|
|
|
|
$childSessions = $this->sessionRepository->findChildSessions($parentSession); |
|
|
|
|
|
|
|
|
|
foreach ($childSessions as $childSession) { |
|
|
|
|
if ($this->findUserSubscriptionInSession($user, $childSession)) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|