Internal: Improve session reinscription logic using direct CLpView validation - refs BT#22057

pull/5831/head
christianbeeznst 10 months ago
parent 238cbdb383
commit e9cca0b6ac
  1. 95
      src/CoreBundle/Command/ReinscriptionCheckCommand.php
  2. 3
      src/CoreBundle/Command/SessionRepetitionCommand.php
  3. 15
      src/CourseBundle/Repository/CLpRepository.php

@ -53,76 +53,67 @@ class ReinscriptionCheckCommand extends Command
{
$debug = $input->getOption('debug');
// 1. Find all lessons with "validity_in_days" > 0
$learningPaths = $this->lpRepository->findWithValidity();
$expiredViews = $this->lpRepository->findExpiredViews(0);
/* @var CLp $lp */
foreach ($learningPaths as $lp) {
$validityDays = $lp->getValidityInDays();
$sessionId = $this->lpRepository->getLpSessionId($lp->getIid());
if (!$sessionId) {
if ($debug) {
$output->writeln('Session ID not found for Learning Path ID: ' . $lp->getIid());
}
continue;
}
if ($debug) {
$output->writeln(sprintf('Found %d expired views.', count($expiredViews)));
}
// 2. Get the session of the lesson
$session = $this->sessionRepository->find($sessionId);
if (!$session) {
if ($debug) {
$output->writeln('Session not found for ID: ' . $sessionId);
}
continue;
foreach ($expiredViews as $view) {
$user = $view->getUser();
$session = $view->getSession();
$lp = $view->getLp();
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()
));
}
// Process only if the session is not the last repetition
// Check if the session is marked as the last repetition
if ($session->getLastRepetition()) {
if ($debug) {
$output->writeln('Session ' . $session->getId() . ' is the last repetition. Skipping...');
$output->writeln('The session is marked as the last repetition. Skipping...');
}
continue;
}
// 3. Find users who completed the lesson and whose validity has expired
$expiredUsers = $this->findExpiredCompletions($lp, $validityDays);
// Find a valid child session
$validChildSession = $this->sessionRepository->findValidChildSession($session);
if (count($expiredUsers) === 0) {
if ($validChildSession) {
$this->enrollUserInSession($user, $validChildSession);
if ($debug) {
$output->writeln('No expired users found for Learning Path ID: ' . $lp->getIid());
$output->writeln(sprintf(
'User %d re-enrolled into the valid child session %d.',
$user->getId(),
$validChildSession->getId()
));
}
continue;
}
foreach ($expiredUsers as $user) {
// If no valid child session exists, check the parent session
$validParentSession = $this->sessionRepository->findValidParentSession($session);
if ($validParentSession) {
$this->enrollUserInSession($user, $validParentSession);
if ($debug) {
$output->writeln('User ' . $user->getUser()->getId() . ' has expired completion for LP ' . $lp->getIid());
$output->writeln(sprintf(
'User %d re-enrolled into the valid parent session %d.',
$user->getId(),
$validParentSession->getId()
));
}
// 4. Find the last valid child session
$validChildSession = $this->sessionRepository->findValidChildSession($session);
if ($validChildSession) {
// Reinscribe user in the valid child session
$this->enrollUserInSession($user->getUser(), $validChildSession);
if ($debug) {
$output->writeln('Reinscribed user ' . $user->getUser()->getId() . ' into child session ' . $validChildSession->getId());
}
} else {
// 5. If no valid child session, find the valid parent session
$validParentSession = $this->sessionRepository->findValidParentSession($session);
if ($validParentSession) {
// Reinscribe user in the valid parent session
$this->enrollUserInSession($user->getUser(), $validParentSession);
if ($debug) {
$output->writeln('Reinscribed user ' . $user->getUser()->getId() . ' into parent session ' . $validParentSession->getId());
}
} else {
if ($debug) {
$output->writeln('No valid parent or child session found for user ' . $user->getUser()->getId());
}
}
} else {
if ($debug) {
$output->writeln(sprintf(
'No valid child or parent session found for user %d.',
$user->getId()
));
}
}
}

@ -121,7 +121,8 @@ class SessionRepetitionCommand extends Command
}
// Copy the courses from the original session
foreach ($session->getCourses() as $course) {
foreach ($session->getCourses() as $sessionRelCourse) {
$course = $sessionRelCourse->getCourse();
$newSession->addCourse($course);
}

@ -138,10 +138,21 @@ final class CLpRepository extends ResourceRepository implements ResourceWithLink
return null;
}
public function findWithValidity(): array
public function findExpiredViews(int $validityDays): array
{
return $this->createQueryBuilder('lp')
$now = new \DateTime();
$expirationDate = (clone $now)->modify('-' . $validityDays . ' days');
return $this->getEntityManager()
->createQueryBuilder()
->select('v')
->from('Chamilo\CourseBundle\Entity\CLpView', 'v')
->join('v.lp', 'lp')
->where('lp.validityInDays > 0')
->andWhere('v.progress = 100')
->andWhere('v.session IS NOT NULL')
->andWhere('v.lastItem < :expirationDate')
->setParameter('expirationDate', $expirationDate->getTimestamp())
->getQuery()
->getResult();
}

Loading…
Cancel
Save