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'); $debug = $input->getOption('debug');
// 1. Find all lessons with "validity_in_days" > 0 $expiredViews = $this->lpRepository->findExpiredViews(0);
$learningPaths = $this->lpRepository->findWithValidity();
/* @var CLp $lp */ if ($debug) {
foreach ($learningPaths as $lp) { $output->writeln(sprintf('Found %d expired views.', count($expiredViews)));
$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;
}
// 2. Get the session of the lesson foreach ($expiredViews as $view) {
$session = $this->sessionRepository->find($sessionId); $user = $view->getUser();
if (!$session) { $session = $view->getSession();
if ($debug) { $lp = $view->getLp();
$output->writeln('Session not found for ID: ' . $sessionId);
} if ($debug) {
continue; $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 ($session->getLastRepetition()) {
if ($debug) { if ($debug) {
$output->writeln('Session ' . $session->getId() . ' is the last repetition. Skipping...'); $output->writeln('The session is marked as the last repetition. Skipping...');
} }
continue; continue;
} }
// 3. Find users who completed the lesson and whose validity has expired // Find a valid child session
$expiredUsers = $this->findExpiredCompletions($lp, $validityDays); $validChildSession = $this->sessionRepository->findValidChildSession($session);
if (count($expiredUsers) === 0) { if ($validChildSession) {
$this->enrollUserInSession($user, $validChildSession);
if ($debug) { 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; 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) { 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()
));
} }
} else {
// 4. Find the last valid child session if ($debug) {
$validChildSession = $this->sessionRepository->findValidChildSession($session); $output->writeln(sprintf(
'No valid child or parent session found for user %d.',
if ($validChildSession) { $user->getId()
// 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());
}
}
} }
} }
} }

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

@ -138,10 +138,21 @@ final class CLpRepository extends ResourceRepository implements ResourceWithLink
return null; 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') ->where('lp.validityInDays > 0')
->andWhere('v.progress = 100')
->andWhere('v.session IS NOT NULL')
->andWhere('v.lastItem < :expirationDate')
->setParameter('expirationDate', $expirationDate->getTimestamp())
->getQuery() ->getQuery()
->getResult(); ->getResult();
} }

Loading…
Cancel
Save