|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* For licensing terms, see /license.txt */ |
|
6
|
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\Command; |
|
8
|
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\Entity\Session; |
|
10
|
|
|
use Chamilo\CoreBundle\Repository\SessionRepository; |
|
11
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
12
|
|
|
use Symfony\Component\Console\Command\Command; |
|
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
16
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
17
|
|
|
use DateTime; |
|
18
|
|
|
|
|
19
|
|
|
class UpdateSessionStatusCommand extends Command |
|
20
|
|
|
{ |
|
21
|
|
|
protected static $defaultName = 'app:update-session-status'; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct( |
|
24
|
|
|
private readonly EntityManagerInterface $entityManager, |
|
25
|
|
|
private readonly SessionRepository $sessionRepository |
|
26
|
|
|
) |
|
27
|
|
|
{ |
|
28
|
|
|
parent::__construct(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
protected function configure(): void |
|
32
|
|
|
{ |
|
33
|
|
|
$this |
|
34
|
|
|
->setDescription('Updates the status of training sessions based on their dates and user count.') |
|
35
|
|
|
->addOption('debug', null, InputOption::VALUE_NONE, 'Enable debug mode'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
39
|
|
|
{ |
|
40
|
|
|
$io = new SymfonyStyle($input, $output); |
|
41
|
|
|
$debug = $input->getOption('debug'); |
|
42
|
|
|
$lineBreak = PHP_SAPI === 'cli' ? PHP_EOL : '<br />'; |
|
43
|
|
|
|
|
44
|
|
|
$now = new DateTime('now', new \DateTimeZone('UTC')); |
|
45
|
|
|
$io->text('Today is: ' . $now->format('Y-m-d H:i:s') . $lineBreak); |
|
46
|
|
|
|
|
47
|
|
|
$sessions = $this->sessionRepository->findAll(); |
|
48
|
|
|
|
|
49
|
|
|
foreach ($sessions as $session) { |
|
50
|
|
|
$id = $session->getId(); |
|
51
|
|
|
$start = $session->getDisplayStartDate(); |
|
52
|
|
|
$end = $session->getDisplayEndDate(); |
|
53
|
|
|
$userCount = $this->sessionRepository->countUsersBySession($session->getId()); |
|
54
|
|
|
|
|
55
|
|
|
$status = $this->determineSessionStatus($start, $end, $userCount, $now); |
|
56
|
|
|
|
|
57
|
|
|
if ($debug) { |
|
58
|
|
|
$startFormatted = $start ? $start->format('Y-m-d H:i:s') : 'N/A'; |
|
59
|
|
|
$endFormatted = $end ? $end->format('Y-m-d H:i:s') : 'N/A'; |
|
60
|
|
|
$io->note("Session #$id: Start date: {$startFormatted} - End date: {$endFormatted}"); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$session->setStatus($status); |
|
64
|
|
|
$this->sessionRepository->update($session); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ($debug) { |
|
68
|
|
|
$io->success('Session statuses have been updated in debug mode (changes are not saved).'); |
|
69
|
|
|
} else { |
|
70
|
|
|
$this->entityManager->flush(); |
|
71
|
|
|
$io->success('Session statuses have been updated successfully.'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return Command::SUCCESS; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Determines the status of a session based on its start/end dates and user count. |
|
79
|
|
|
*/ |
|
80
|
|
|
private function determineSessionStatus(?DateTime $start, ?DateTime $end, int $userCount, DateTime $now): int |
|
81
|
|
|
{ |
|
82
|
|
|
if ($start > $now) { |
|
83
|
|
|
return Session::STATUS_PLANNED; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if ($userCount >= 2 && $start <= $now && $end > $now) { |
|
87
|
|
|
return Session::STATUS_PROGRESS; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if ($userCount === 0 && $now > $start) { |
|
91
|
|
|
return Session::STATUS_CANCELLED; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if ($now > $end && $userCount >= 2) { |
|
95
|
|
|
return Session::STATUS_FINISHED; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return Session::STATUS_UNKNOWN; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|