From 8bdbc49a4e64ca5c740d0cc3656c0c44e7c35312 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos <1697880+AngelFQC@users.noreply.github.com> Date: Wed, 20 Nov 2024 18:26:50 -0500 Subject: [PATCH] Fix resource visibility when displayed from base course in session course - refs BT#22190 --- public/main/lp/learnpath.class.php | 7 ++- public/main/lp/learnpathList.class.php | 2 +- .../Repository/ResourceRepository.php | 53 ++++++++++++++----- 3 files changed, 47 insertions(+), 15 deletions(-) diff --git a/public/main/lp/learnpath.class.php b/public/main/lp/learnpath.class.php index b829935ab0..c111d8427a 100644 --- a/public/main/lp/learnpath.class.php +++ b/public/main/lp/learnpath.class.php @@ -3315,10 +3315,13 @@ class learnpath $visibility = (int) $visibility; + $course = api_get_course_entity(); + $session = api_get_session_entity(); + if (1 === $visibility) { - $repo->setVisibilityPublished($lp); + $repo->setVisibilityPublished($lp, $course, $session); } else { - $repo->setVisibilityDraft($lp); + $repo->setVisibilityDraft($lp, $course, $session); } return true; diff --git a/public/main/lp/learnpathList.class.php b/public/main/lp/learnpathList.class.php index a9c97c13a9..506346aacc 100644 --- a/public/main/lp/learnpathList.class.php +++ b/public/main/lp/learnpathList.class.php @@ -119,7 +119,7 @@ class LearnpathList $lp->getId(), $session_id );*/ - $visibility = $lp->isVisible($course); + $visibility = $lp->isVisible($course, $session); // If option is not true then don't show invisible LP to user if (false === $ignoreLpVisibility) { diff --git a/src/CoreBundle/Repository/ResourceRepository.php b/src/CoreBundle/Repository/ResourceRepository.php index 019497625c..3ce1dd3189 100644 --- a/src/CoreBundle/Repository/ResourceRepository.php +++ b/src/CoreBundle/Repository/ResourceRepository.php @@ -18,6 +18,7 @@ use Chamilo\CoreBundle\Entity\ResourceShowCourseResourcesInSessionInterface; use Chamilo\CoreBundle\Entity\ResourceType; use Chamilo\CoreBundle\Entity\Session; use Chamilo\CoreBundle\Entity\User; +use Chamilo\CoreBundle\Entity\Usergroup; use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter; use Chamilo\CoreBundle\Traits\NonResourceRepository; use Chamilo\CoreBundle\Traits\Repository\RepositoryQueryBuilderTrait; @@ -600,19 +601,28 @@ abstract class ResourceRepository extends ServiceEntityRepository } } - public function setVisibilityPublished(AbstractResource $resource): void - { - $this->setLinkVisibility($resource, ResourceLink::VISIBILITY_PUBLISHED); + public function setVisibilityPublished( + AbstractResource $resource, + ?Course $course = null, + ?Session $session = null, + ): void { + $this->setLinkVisibility($resource, ResourceLink::VISIBILITY_PUBLISHED, true, $course, $session); } - public function setVisibilityDraft(AbstractResource $resource): void - { - $this->setLinkVisibility($resource, ResourceLink::VISIBILITY_DRAFT); + public function setVisibilityDraft( + AbstractResource $resource, + ?Course $course = null, + ?Session $session = null, + ): void { + $this->setLinkVisibility($resource, ResourceLink::VISIBILITY_DRAFT, true, $course, $session); } - public function setVisibilityPending(AbstractResource $resource): void - { - $this->setLinkVisibility($resource, ResourceLink::VISIBILITY_PENDING); + public function setVisibilityPending( + AbstractResource $resource, + ?Course $course = null, + ?Session $session = null, + ): void { + $this->setLinkVisibility($resource, ResourceLink::VISIBILITY_PENDING, true, $course, $session); } public function addResourceNode( @@ -844,8 +854,15 @@ abstract class ResourceRepository extends ServiceEntityRepository return $qb; } - private function setLinkVisibility(AbstractResource $resource, int $visibility, bool $recursive = true): bool - { + private function setLinkVisibility( + AbstractResource $resource, + int $visibility, + bool $recursive = true, + ?Course $course = null, + ?Session $session = null, + ?CGroup $group = null, + ?User $user = null, + ): bool { $resourceNode = $resource->getResourceNode(); if (null === $resourceNode) { @@ -868,7 +885,19 @@ abstract class ResourceRepository extends ServiceEntityRepository } } - $links = $resourceNode->getResourceLinks(); + if ($resource instanceof ResourceShowCourseResourcesInSessionInterface) { + $link = $resource->getFirstResourceLinkFromCourseSession($course, $session); + + if (!$link) { + $resource->parentResource = $course; + $resource->addCourseLink($course, $session); + } + + $link = $resource->getFirstResourceLinkFromCourseSession($course, $session); + $links = [$link]; + } else { + $links = $resourceNode->getResourceLinks(); + } /** @var ResourceLink $link */ foreach ($links as $link) {