66
77namespace Chamilo \CoreBundle \EventListener ;
88
9+ use Chamilo \CoreBundle \Entity \TrackELogin ;
10+ use Chamilo \CoreBundle \Entity \TrackEOnline ;
911use Chamilo \CoreBundle \Entity \User ;
10- use Database ;
1112use Doctrine \ORM \EntityManagerInterface ;
12- use Symfony \Component \HttpFoundation \JsonResponse ;
1313use Symfony \Component \HttpFoundation \RedirectResponse ;
1414use Symfony \Component \Routing \Generator \UrlGeneratorInterface ;
1515use Symfony \Component \Security \Core \Authentication \Token \Storage \TokenStorageInterface ;
@@ -42,15 +42,11 @@ public function onSymfonyComponentSecurityHttpEventLogoutEvent(LogoutEvent $even
4242 {
4343 $ request = $ event ->getRequest ();
4444
45- // Chamilo logout
45+ // Chamilo logout operations
4646 $ request ->getSession ()->remove ('_selected_locale ' );
4747 $ request ->getSession ()->remove ('_locale ' );
4848 $ request ->getSession ()->remove ('_locale_user ' );
4949
50- /*if (api_is_global_chat_enabled()) {
51- $chat = new \Chat();
52- $chat->setUserStatus(0);
53- }*/
5450 $ token = $ this ->storage ->getToken ();
5551 if (null === $ token ) {
5652 $ login = $ this ->router ->generate ('index ' );
@@ -62,40 +58,20 @@ public function onSymfonyComponentSecurityHttpEventLogoutEvent(LogoutEvent $even
6258 $ user = $ token ->getUser ();
6359 if ($ user instanceof User) {
6460 $ userId = $ user ->getId ();
65- $ table = Database::get_main_table (TABLE_STATISTIC_TRACK_E_LOGIN );
66-
67- $ sql = "SELECT login_id, login_date
68- FROM {$ table }
69- WHERE login_user_id = {$ userId }
70- ORDER BY login_date DESC
71- LIMIT 0,1 " ;
72- $ loginId = null ;
73- $ connection = $ this ->em ->getConnection ();
74- $ result = $ connection ->executeQuery ($ sql );
75- if ($ result ->rowCount () > 0 ) {
76- $ row = $ result ->fetchAssociative ();
77- if ($ row ) {
78- $ loginId = $ row ['login_id ' ];
79- }
80- }
8161
62+ $ trackELoginRepository = $ this ->em ->getRepository (TrackELogin::class);
8263 $ loginAs = $ this ->checker ->isGranted ('ROLE_PREVIOUS_ADMIN ' );
8364 if (!$ loginAs ) {
84- $ current_date = api_get_utc_datetime ();
85- $ sql = "UPDATE {$ table }
86- SET logout_date=' " .$ current_date ."'
87- WHERE login_id=' {$ loginId }' " ;
88- $ connection ->executeQuery ($ sql );
65+ $ currentDate = new \DateTime ("now " , new \DateTimeZone ('UTC ' ));
66+ $ trackELoginRepository ->updateLastLoginLogoutDate ($ userId , $ currentDate );
8967 }
9068
91- $ table = Database::get_main_table (TABLE_STATISTIC_TRACK_E_ONLINE );
92- $ sql = "DELETE FROM $ table WHERE login_user_id = $ userId " ;
93- $ connection ->executeQuery ($ sql );
69+ $ trackEOnlineRepository = $ this ->em ->getRepository (TrackEOnline::class);
70+ $ trackEOnlineRepository ->removeOnlineSessionsByUser ($ userId );
9471 }
9572
9673 $ login = $ this ->router ->generate ('index ' );
9774
9875 return new RedirectResponse ($ login );
99- // return new JsonResponse('logout out', 200);
10076 }
10177}
0 commit comments