diff --git a/src/CoreBundle/Controller/ContactCategoryController.php b/src/CoreBundle/Controller/ContactCategoryController.php index 250c32303c..ad64642b4c 100644 --- a/src/CoreBundle/Controller/ContactCategoryController.php +++ b/src/CoreBundle/Controller/ContactCategoryController.php @@ -1,8 +1,9 @@ getRepository(ContactCategory::class) - ->findAll(); + ->findAll() + ; return $this->render('@ChamiloCore/ContactCategory/index.html.twig', [ 'contact_categories' => $contactCategories, diff --git a/src/CoreBundle/Controller/ContactController.php b/src/CoreBundle/Controller/ContactController.php index f22ff4d386..1fad06e194 100644 --- a/src/CoreBundle/Controller/ContactController.php +++ b/src/CoreBundle/Controller/ContactController.php @@ -1,8 +1,9 @@ text( "Sender: {$contactData['email']}\n". "Message: {$contactData['message']}" - ); + ) + ; // Send the email $mailer->send($email); diff --git a/src/CoreBundle/Controller/CourseController.php b/src/CoreBundle/Controller/CourseController.php index d9f8f73ed8..e07a197971 100644 --- a/src/CoreBundle/Controller/CourseController.php +++ b/src/CoreBundle/Controller/CourseController.php @@ -1,9 +1,9 @@ getUser(); + + if (!$user) { + return new JsonResponse(['error' => 'User not found'], Response::HTTP_UNAUTHORIZED); + } + + $isEnrolledInCourses = $this->isUserEnrolledInAnyCourse($user, $em); + $isEnrolledInSessions = $this->isUserEnrolledInAnySession($user, $em); + + if (!$isEnrolledInCourses && !$isEnrolledInSessions) { + $defaultMenuEntry = $settingsManager->getSetting('platform.default_menu_entry_for_course_or_session'); + $isEnrolledInCourses = 'my_courses' === $defaultMenuEntry; + $isEnrolledInSessions = 'my_sessions' === $defaultMenuEntry; + } + + return new JsonResponse([ + 'isEnrolledInCourses' => $isEnrolledInCourses, + 'isEnrolledInSessions' => $isEnrolledInSessions, + ]); + } + private function autoLaunch(): void { $autoLaunchWarning = ''; @@ -900,37 +925,13 @@ class CourseController extends ToolBaseController return $link.'?'.$this->getCourseUrlQuery(); } - #[Route('/check-enrollments', name: 'chamilo_core_check_enrollments', methods: ['GET'])] - public function checkEnrollments(EntityManagerInterface $em, SettingsManager $settingsManager): JsonResponse - { - /** @var User|null $user */ - $user = $this->getUser(); - - if (!$user) { - return new JsonResponse(['error' => 'User not found'], Response::HTTP_UNAUTHORIZED); - } - - $isEnrolledInCourses = $this->isUserEnrolledInAnyCourse($user, $em); - $isEnrolledInSessions = $this->isUserEnrolledInAnySession($user, $em); - - if (!$isEnrolledInCourses && !$isEnrolledInSessions) { - $defaultMenuEntry = $settingsManager->getSetting('platform.default_menu_entry_for_course_or_session'); - $isEnrolledInCourses = 'my_courses' === $defaultMenuEntry; - $isEnrolledInSessions = 'my_sessions' === $defaultMenuEntry; - } - - return new JsonResponse([ - 'isEnrolledInCourses' => $isEnrolledInCourses, - 'isEnrolledInSessions' => $isEnrolledInSessions, - ]); - } - // Implement the real logic to check course enrollment private function isUserEnrolledInAnyCourse(User $user, EntityManagerInterface $em): bool { $enrollmentCount = $em ->getRepository(CourseRelUser::class) - ->count(['user' => $user]); + ->count(['user' => $user]) + ; return $enrollmentCount > 0; } @@ -939,9 +940,9 @@ class CourseController extends ToolBaseController private function isUserEnrolledInAnySession(User $user, EntityManagerInterface $em): bool { $enrollmentCount = $em->getRepository(SessionRelUser::class) - ->count(['user' => $user]); + ->count(['user' => $user]) + ; return $enrollmentCount > 0; } - } diff --git a/src/CoreBundle/Entity/ContactCategory.php b/src/CoreBundle/Entity/ContactCategory.php index c27b88aa0f..6f54b24d87 100644 --- a/src/CoreBundle/Entity/ContactCategory.php +++ b/src/CoreBundle/Entity/ContactCategory.php @@ -1,29 +1,30 @@ name = $name; + return $this; } @@ -50,6 +52,7 @@ class ContactCategory public function setEmail(string $email): self { $this->email = $email; + return $this; } } diff --git a/src/CoreBundle/EventListener/CourseListener.php b/src/CoreBundle/EventListener/CourseListener.php index a89d61c2aa..a4cb561cdd 100644 --- a/src/CoreBundle/EventListener/CourseListener.php +++ b/src/CoreBundle/EventListener/CourseListener.php @@ -1,9 +1,9 @@ isMainRequest()) { // don't do anything if it's not the master request return; @@ -67,9 +72,13 @@ class CourseListener return; } + if (true === $cidReset) { + $this->removeCourseFromSession($request); + + return; + } + $sessionHandler = $request->getSession(); - $container = $this->container; - $translator = $container->get('translator'); $twig = $this->twig; $course = null; @@ -79,46 +88,25 @@ class CourseListener $courseId = (int) $request->get('cid'); $checker = $this->authorizationChecker; - /** @var EntityManager $em */ - $em = $container->get('doctrine')->getManager(); - - //dump("cid value in request: $courseId"); if (!empty($courseId)) { - $course = null; if ($sessionHandler->has('course')) { /** @var Course $courseFromSession */ $courseFromSession = $sessionHandler->get('course'); if ($courseId === $courseFromSession->getId()) { $course = $courseFromSession; $courseInfo = $sessionHandler->get('_course'); - //dump("Course #$courseId loaded from Session "); } } - //$course = null; //force loading from database - //if (null === $course) { - $course = $em->getRepository(Course::class)->find($courseId); if (null === $course) { - throw new NotFoundHttpException($translator->trans('Course does not exist')); + $course = $this->entityManager->find(Course::class, $courseId); + $courseInfo = api_get_course_info($course->getCode()); } - //dump("Course loaded from DB #$courseId"); - $courseInfo = api_get_course_info($course->getCode()); - //} - - /*if (null === $course) { - throw new NotFoundHttpException($translator->trans('Course does not exist')); - }*/ - } - - global $cidReset; - if (true === $cidReset) { - $this->removeCourseFromSession($request); - - return; - } + if (null === $course) { + throw new NotFoundHttpException($this->translator->trans('Course does not exist')); + } - if (null !== $course) { // Setting variables in the session. $sessionHandler->set('course', $course); $sessionHandler->set('_real_cid', $course->getId()); @@ -129,32 +117,27 @@ class CourseListener // Setting variables for the twig templates. $twig->addGlobal('course', $course); + if (false === $checker->isGranted(CourseVoter::VIEW, $course)) { + throw new AccessDeniedException($this->translator->trans('You\'re not allowed in this course')); + } + // Checking if sid is used. $sessionId = (int) $request->get('sid'); - $session = null; + if (empty($sessionId)) { $sessionHandler->remove('session_name'); $sessionHandler->remove('sid'); $sessionHandler->remove('session'); - // Check if user is allowed to this course - // See CourseVoter.php - //dump("Checkisgranted"); - if (false === $checker->isGranted(CourseVoter::VIEW, $course)) { - throw new AccessDeniedException($translator->trans('You\'re not allowed in this course')); - } } else { //dump("Load chamilo session from DB"); - $session = $em->getRepository(Session::class)->find($sessionId); + $session = $this->entityManager->find(Session::class, $sessionId); if (null !== $session) { - if (!$session->hasCourse($course)) { - throw new AccessDeniedException($translator->trans('Course is not registered in the Session')); - } //$course->setCurrentSession($session); $session->setCurrentCourse($course); // Check if user is allowed to this course-session // See SessionVoter.php if (false === $checker->isGranted(SessionVoter::VIEW, $session)) { - throw new AccessDeniedException($translator->trans('You\'re not allowed in this session')); + throw new AccessDeniedException($this->translator->trans('You\'re not allowed in this session')); } $sessionHandler->set('session_name', $session->getName()); $sessionHandler->set('sid', $session->getId()); @@ -162,7 +145,7 @@ class CourseListener $twig->addGlobal('session', $session); } else { - throw new NotFoundHttpException($translator->trans('Session not found')); + throw new NotFoundHttpException($this->translator->trans('Session not found')); } } @@ -173,16 +156,16 @@ class CourseListener $sessionHandler->remove('gid'); } else { //dump('Load chamilo group from DB'); - $group = $em->getRepository(CGroup::class)->find($groupId); + $group = $this->entityManager->getRepository(CGroup::class)->find($groupId); if (null === $group) { - throw new NotFoundHttpException($translator->trans('Group not found')); + throw new NotFoundHttpException($this->translator->trans('Group not found')); } $group->setParent($course); if (false === $checker->isGranted(GroupVoter::VIEW, $group)) { - throw new AccessDeniedException($translator->trans('You\'re not allowed in this group')); + throw new AccessDeniedException($this->translator->trans('You\'re not allowed in this group')); } $sessionHandler->set('gid', $groupId); @@ -191,11 +174,11 @@ class CourseListener // Check if user is allowed to this course-group // See GroupVoter.php if (false === $checker->isGranted(GroupVoter::VIEW, $group)) { - throw new AccessDeniedException($translator->trans('Unauthorised access to group')); + throw new AccessDeniedException($this->translator->trans('Unauthorised access to group')); } $sessionHandler->set('gid', $groupId); } else { - throw new AccessDeniedException($translator->trans('Group does not exist in course')); + throw new AccessDeniedException($this->translator->trans('Group does not exist in course')); }*/ } diff --git a/src/CoreBundle/EventSubscriber/AnonymousUserSubscriber.php b/src/CoreBundle/EventSubscriber/AnonymousUserSubscriber.php index 01dfadcce3..4418b44ee4 100644 --- a/src/CoreBundle/EventSubscriber/AnonymousUserSubscriber.php +++ b/src/CoreBundle/EventSubscriber/AnonymousUserSubscriber.php @@ -1,13 +1,15 @@ security->getUser() !== null) { + if (null !== $this->security->getUser()) { return; } @@ -41,7 +43,7 @@ class AnonymousUserSubscriber implements EventSubscriberInterface $userIp = $request->getClientIp(); $anonymousUserId = $this->getOrCreateAnonymousUserId($userIp); - if ($anonymousUserId !== null) { + if (null !== $anonymousUserId) { $trackLoginRepository = $this->entityManager->getRepository(TrackELogin::class); // Check if a login record already exists for this user and IP @@ -50,8 +52,9 @@ class AnonymousUserSubscriber implements EventSubscriberInterface // Record the access if it does not exist $trackLogin = new TrackELogin(); $trackLogin->setUserIp($userIp) - ->setLoginDate(new \DateTime()) - ->setUser($this->entityManager->getReference(User::class, $anonymousUserId)); + ->setLoginDate(new DateTime()) + ->setUser($this->entityManager->getReference(User::class, $anonymousUserId)) + ; $this->entityManager->persist($trackLogin); $this->entityManager->flush(); @@ -88,7 +91,15 @@ class AnonymousUserSubscriber implements EventSubscriberInterface } } - private function getOrCreateAnonymousUserId(string $userIp): ?int { + public static function getSubscribedEvents() + { + return [ + KernelEvents::REQUEST => 'onKernelRequest', + ]; + } + + private function getOrCreateAnonymousUserId(string $userIp): ?int + { $userRepository = $this->entityManager->getRepository(User::class); $trackLoginRepository = $this->entityManager->getRepository(TrackELogin::class); @@ -102,16 +113,17 @@ class AnonymousUserSubscriber implements EventSubscriberInterface foreach ($anonymousUsers as $user) { $loginRecord = $trackLoginRepository->findOneBy(['userIp' => $userIp, 'user' => $user]); if ($loginRecord) { - error_log('Existing login found for user ID: ' . $user->getId()); + error_log('Existing login found for user ID: '.$user->getId()); + return $user->getId(); } } // Delete excess anonymous users - while (count($anonymousUsers) >= $maxAnonymousUsers) { + while (\count($anonymousUsers) >= $maxAnonymousUsers) { $oldestAnonymousUser = array_shift($anonymousUsers); if ($oldestAnonymousUser) { - error_log('Deleting oldest anonymous user: ' . $oldestAnonymousUser->getId()); + error_log('Deleting oldest anonymous user: '.$oldestAnonymousUser->getId()); $this->entityManager->remove($oldestAnonymousUser); $this->entityManager->flush(); } @@ -123,25 +135,20 @@ class AnonymousUserSubscriber implements EventSubscriberInterface ->setSkipResourceNode(true) ->setLastname('Joe') ->setFirstname('Anonymous') - ->setUsername('anon_' . $uniqueId) + ->setUsername('anon_'.$uniqueId) ->setStatus(User::ANONYMOUS) ->setPlainPassword('anon') - ->setEmail('anon_' . $uniqueId . '@localhost.local') + ->setEmail('anon_'.$uniqueId.'@localhost.local') ->setOfficialCode('anonymous') ->setCreatorId(1) - ->addRole('ROLE_ANONYMOUS'); + ->addRole('ROLE_ANONYMOUS') + ; $this->entityManager->persist($anonymousUser); $this->entityManager->flush(); - error_log('New anonymous user created: ' . $anonymousUser->getId()); - return $anonymousUser->getId(); - } + error_log('New anonymous user created: '.$anonymousUser->getId()); - public static function getSubscribedEvents() - { - return [ - KernelEvents::REQUEST => 'onKernelRequest', - ]; + return $anonymousUser->getId(); } } diff --git a/src/CoreBundle/Form/ContactCategoryType.php b/src/CoreBundle/Form/ContactCategoryType.php index 7b9b960cfa..5c9c153247 100644 --- a/src/CoreBundle/Form/ContactCategoryType.php +++ b/src/CoreBundle/Form/ContactCategoryType.php @@ -1,5 +1,9 @@ 'btn btn--primary hover:bg-blue-700 text-white font-bold py-2 px-4 rounded cursor-pointer', 'style' => 'border: none;', ], - ]); + ]) + ; } public function configureOptions(OptionsResolver $resolver): void diff --git a/src/CoreBundle/Migrations/Schema/V200/Version20230904173400.php b/src/CoreBundle/Migrations/Schema/V200/Version20230904173400.php index bc04f0e2da..dfa981d07b 100644 --- a/src/CoreBundle/Migrations/Schema/V200/Version20230904173400.php +++ b/src/CoreBundle/Migrations/Schema/V200/Version20230904173400.php @@ -11,7 +11,6 @@ use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; use Chamilo\CourseBundle\Entity\CCalendarEvent; use DateTime; use DateTimeZone; -use Doctrine\DBAL\Connection; use Doctrine\DBAL\Schema\Schema; use Doctrine\ORM\Exception\ORMException; use Exception; @@ -83,7 +82,7 @@ class Version20230904173400 extends AbstractMigrationChamilo if ($subscriptionsEnabled) { $subscriptionsInfo = $this->getSubscriptions((int) $personalAgenda['id']); - if (count($subscriptionsInfo) > 0) { + if (\count($subscriptionsInfo) > 0) { $hasSubscriptions = true; $invitationsOrSubscriptionsInfo = $subscriptionsInfo; diff --git a/src/CoreBundle/Migrations/Schema/V200/Version20231110194300.php b/src/CoreBundle/Migrations/Schema/V200/Version20231110194300.php index 7a99ae5e04..87673fd00e 100644 --- a/src/CoreBundle/Migrations/Schema/V200/Version20231110194300.php +++ b/src/CoreBundle/Migrations/Schema/V200/Version20231110194300.php @@ -14,7 +14,7 @@ final class Version20231110194300 extends AbstractMigrationChamilo { public function getDescription(): string { - return "Copy custom theme folder to assets and update webpack.config"; + return 'Copy custom theme folder to assets and update webpack.config'; } public function up(Schema $schema): void @@ -54,13 +54,12 @@ final class Version20231110194300 extends AbstractMigrationChamilo 'fruity_orange', 'medical', 'simplex', - 'tasty_olive' + 'tasty_olive', ]; - $sourceDir = $rootPath.'/app/Resources/public/css/themes'; $destinationDir = $rootPath.'/assets/css/themes/'; - $chamiloDefaultCssPath = $destinationDir . 'chamilo/default.css'; + $chamiloDefaultCssPath = $destinationDir.'chamilo/default.css'; if (!file_exists($sourceDir)) { return; @@ -72,16 +71,16 @@ final class Version20231110194300 extends AbstractMigrationChamilo foreach ($finder as $folder) { $folderName = $folder->getRelativePathname(); - if (!in_array($folderName, $customThemesFolders, true)) { + if (!\in_array($folderName, $customThemesFolders, true)) { $sourcePath = $folder->getRealPath(); - $destinationPath = $destinationDir . $folderName; + $destinationPath = $destinationDir.$folderName; if (!file_exists($destinationPath)) { $this->copyDirectory($sourcePath, $destinationPath); $newThemes[] = $folderName; if (file_exists($chamiloDefaultCssPath)) { - $newThemeDefaultCssPath = $destinationPath . '/default.css'; + $newThemeDefaultCssPath = $destinationPath.'/default.css'; copy($chamiloDefaultCssPath, $newThemeDefaultCssPath); } } @@ -96,11 +95,11 @@ final class Version20231110194300 extends AbstractMigrationChamilo $dir = opendir($src); @mkdir($dst); while (false !== ($file = readdir($dir))) { - if (($file != '.') && ($file != '..')) { - if (is_dir($src . '/' . $file)) { - $this->copyDirectory($src . '/' . $file, $dst . '/' . $file); + if (('.' !== $file) && ('..' !== $file)) { + if (is_dir($src.'/'.$file)) { + $this->copyDirectory($src.'/'.$file, $dst.'/'.$file); } else { - copy($src . '/' . $file, $dst . '/' . $file); + copy($src.'/'.$file, $dst.'/'.$file); } } } @@ -109,7 +108,7 @@ final class Version20231110194300 extends AbstractMigrationChamilo private function updateWebpackConfig(string $rootPath, array $newThemes): void { - $webpackConfigPath = $rootPath . '/webpack.config.js'; + $webpackConfigPath = $rootPath.'/webpack.config.js'; if (!file_exists($webpackConfigPath)) { return; @@ -117,13 +116,14 @@ final class Version20231110194300 extends AbstractMigrationChamilo $content = file_get_contents($webpackConfigPath); - $pattern = "/(const themes = \[\n\s*)([^\]]*?)(\s*\];)/s"; - $replacement = function($matches) use ($newThemes) { + $pattern = "/(const themes = \\[\n\\s*)([^\\]]*?)(\\s*\\];)/s"; + $replacement = function ($matches) use ($newThemes) { $existingThemesString = rtrim($matches[2], ", \n"); $newThemesString = implode("',\n '", $newThemes); - $formattedNewThemesString = $existingThemesString . - (empty($existingThemesString) ? '' : ",\n '") . $newThemesString . "'"; - return $matches[1] . $formattedNewThemesString . $matches[3]; + $formattedNewThemesString = $existingThemesString. + (empty($existingThemesString) ? '' : ",\n '").$newThemesString."'"; + + return $matches[1].$formattedNewThemesString.$matches[3]; }; $newContent = preg_replace_callback($pattern, $replacement, $content); diff --git a/src/CoreBundle/Security/Authorization/Voter/AnonymousVoter.php b/src/CoreBundle/Security/Authorization/Voter/AnonymousVoter.php index 90d6e6f0a9..1f1bf2ccb7 100644 --- a/src/CoreBundle/Security/Authorization/Voter/AnonymousVoter.php +++ b/src/CoreBundle/Security/Authorization/Voter/AnonymousVoter.php @@ -1,19 +1,20 @@ getStatus() === User::ANONYMOUS; + return User::ANONYMOUS === $user->getStatus(); } } diff --git a/src/CoreBundle/Settings/StylesheetsSettingsSchema.php b/src/CoreBundle/Settings/StylesheetsSettingsSchema.php index 54a1cb1656..a3584cb2f8 100644 --- a/src/CoreBundle/Settings/StylesheetsSettingsSchema.php +++ b/src/CoreBundle/Settings/StylesheetsSettingsSchema.php @@ -1,16 +1,16 @@ 'chamilo', ] - ); + ) + ; $allowedTypes = [ 'stylesheets' => ['string'], ]; @@ -50,7 +51,7 @@ class StylesheetsSettingsSchema extends AbstractSettingsSchema private function getThemeChoices(): array { $projectDir = $this->parameterBag->get('kernel.project_dir'); - $themesDirectory = $projectDir . '/assets/css/themes/'; + $themesDirectory = $projectDir.'/assets/css/themes/'; $finder = new Finder(); $choices = []; @@ -66,7 +67,6 @@ class StylesheetsSettingsSchema extends AbstractSettingsSchema return $choices; } - private function formatFolderName(string $name): string { return ucwords(str_replace('_', ' ', $name)); diff --git a/src/CourseBundle/Entity/CCalendarEvent.php b/src/CourseBundle/Entity/CCalendarEvent.php index 2ab545e037..74d789c3d0 100644 --- a/src/CourseBundle/Entity/CCalendarEvent.php +++ b/src/CourseBundle/Entity/CCalendarEvent.php @@ -376,7 +376,7 @@ class CCalendarEvent extends AbstractResource implements ResourceInterface, Stri return $this->invitaionType; } - public function setInvitaionType(string $invitaionType): CCalendarEvent + public function setInvitaionType(string $invitaionType): self { $this->invitaionType = $invitaionType; @@ -388,7 +388,7 @@ class CCalendarEvent extends AbstractResource implements ResourceInterface, Stri return $this->subscriptionVisibility; } - public function setSubscriptionVisibility(int $subscriptionVisibility): CCalendarEvent + public function setSubscriptionVisibility(int $subscriptionVisibility): self { $this->subscriptionVisibility = $subscriptionVisibility; @@ -400,7 +400,7 @@ class CCalendarEvent extends AbstractResource implements ResourceInterface, Stri return $this->subscriptionItemId; } - public function setSubscriptionItemId(?int $subscriptionItemId): CCalendarEvent + public function setSubscriptionItemId(?int $subscriptionItemId): self { $this->subscriptionItemId = $subscriptionItemId; @@ -412,7 +412,7 @@ class CCalendarEvent extends AbstractResource implements ResourceInterface, Stri return $this->maxAttendees; } - public function setMaxAttendees(int $maxAttendees): CCalendarEvent + public function setMaxAttendees(int $maxAttendees): self { $this->maxAttendees = $maxAttendees;