From fb11547fd28ba5b0efc091fe1aaa01a391062853 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Thu, 25 Mar 2021 02:20:10 -0500 Subject: [PATCH] TopLinks: Allow position them on top of tools - refs BT#18474 --- plugin/toplinks/index.php | 34 ++++++ .../Repository/TopLinkRelToolRepository.php | 29 +++++ plugin/toplinks/src/Entity/TopLink.php | 29 +++++ plugin/toplinks/src/Entity/TopLinkRelTool.php | 102 ++++++++++++++++++ plugin/toplinks/src/TopLinksPlugin.php | 17 ++- 5 files changed, 210 insertions(+), 1 deletion(-) create mode 100644 plugin/toplinks/src/Entity/Repository/TopLinkRelToolRepository.php create mode 100644 plugin/toplinks/src/Entity/TopLinkRelTool.php diff --git a/plugin/toplinks/index.php b/plugin/toplinks/index.php index a4abe2dafc..d9ee24753c 100644 --- a/plugin/toplinks/index.php +++ b/plugin/toplinks/index.php @@ -1,2 +1,36 @@ getScriptName() && !api_is_allowed_to_edit()) { + $course = api_get_course_entity(); + + $em = Database::getManager(); + $linkToolRepo = $em->getRepository(TopLinkRelTool::class); + + $linkTools = $linkToolRepo->findInCourse($course); + + $toolIds = []; + + /** @var TopLinkRelTool $linkTool */ + foreach ($linkTools as $linkTool) { + $toolIds[] = $linkTool->getTool()->getIid(); + } + ?> + + createQueryBuilder('tlrt'); + + return $qb + ->innerJoin('tlrt.tool', 'tool', Join::WITH) + ->where($qb->expr()->eq('tool.cId', ':course')) + ->setParameter('course', $course) + ->getQuery() + ->getResult(); + } +} diff --git a/plugin/toplinks/src/Entity/TopLink.php b/plugin/toplinks/src/Entity/TopLink.php index 3645fbc877..1c02f49399 100644 --- a/plugin/toplinks/src/Entity/TopLink.php +++ b/plugin/toplinks/src/Entity/TopLink.php @@ -4,6 +4,10 @@ namespace Chamilo\PluginBundle\Entity\TopLinks; +use Chamilo\CoreBundle\Entity\Course; +use Chamilo\CourseBundle\Entity\CTool; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Criteria; use Doctrine\ORM\Mapping as ORM; /** @@ -48,11 +52,18 @@ class TopLink * @ORM\Column(name="icon", type="string", nullable=true) */ private $icon; + /** + * @var \Doctrine\Common\Collections\Collection + * + * @ORM\OneToMany(targetEntity="Chamilo\PluginBundle\Entity\TopLinks\TopLinkRelTool", mappedBy="link", orphanRemoval=true, cascade={"persist", "remove"}) + */ + private $tools; public function __construct() { $this->target = '_blank'; $this->icon = null; + $this->tools = new ArrayCollection(); } /** @@ -142,4 +153,22 @@ class TopLink return $this; } + + /** + * @return \Doctrine\Common\Collections\Collection + */ + public function getTools() + { + return $this->tools; + } + + public function addTool(CTool $tool) + { + $linkTool = new TopLinkRelTool(); + $linkTool + ->setTool($tool) + ->setLink($this); + + $this->tools->add($linkTool); + } } diff --git a/plugin/toplinks/src/Entity/TopLinkRelTool.php b/plugin/toplinks/src/Entity/TopLinkRelTool.php new file mode 100644 index 0000000000..772b854c10 --- /dev/null +++ b/plugin/toplinks/src/Entity/TopLinkRelTool.php @@ -0,0 +1,102 @@ +id; + } + + /** + * @param int $id + * + * @return TopLinkRelTool + */ + public function setId(int $id): TopLinkRelTool + { + $this->id = $id; + + return $this; + } + + /** + * @return \Chamilo\PluginBundle\Entity\TopLinks\TopLink + */ + public function getLink(): TopLink + { + return $this->link; + } + + /** + * @param \Chamilo\PluginBundle\Entity\TopLinks\TopLink $link + * + * @return TopLinkRelTool + */ + public function setLink(TopLink $link): TopLinkRelTool + { + $this->link = $link; + + return $this; + } + + /** + * @return \Chamilo\CourseBundle\Entity\CTool + */ + public function getTool(): CTool + { + return $this->tool; + } + + /** + * @param \Chamilo\CourseBundle\Entity\CTool $tool + * + * @return TopLinkRelTool + */ + public function setTool(CTool $tool): TopLinkRelTool + { + $this->tool = $tool; + + return $this; + } +} diff --git a/plugin/toplinks/src/TopLinksPlugin.php b/plugin/toplinks/src/TopLinksPlugin.php index dacea6b31b..a412f73242 100644 --- a/plugin/toplinks/src/TopLinksPlugin.php +++ b/plugin/toplinks/src/TopLinksPlugin.php @@ -3,6 +3,7 @@ /* For license terms, see /license.txt */ use Chamilo\PluginBundle\Entity\TopLinks\TopLink; +use Chamilo\PluginBundle\Entity\TopLinks\TopLinkRelTool; use Doctrine\ORM\Tools\SchemaTool; /** @@ -48,12 +49,24 @@ class TopLinksPlugin extends Plugin implements HookPluginInterface public function addToolInCourse(int $courseId, TopLink $link) { - $this->createLinkToCourseTool( + $tool = $this->createLinkToCourseTool( $link->getTitle(), $courseId, null, 'toplinks/start.php?'.http_build_query(['link' => $link->getId()]) ); + + if (null === $tool) { + return; + } + + $tool->setTarget($link->getTarget()); + + $link->addTool($tool); + + $em = Database::getManager(); + $em->persist($link); + $em->flush(); } public function install() @@ -63,6 +76,7 @@ class TopLinksPlugin extends Plugin implements HookPluginInterface $tableReferences = [ 'toplinks_link' => $em->getClassMetadata(TopLink::class), + 'toplinks_link_rel_tool' => $em->getClassMetadata(TopLinkRelTool::class), ]; $tablesExists = $schemaManager->tablesExist(array_keys($tableReferences)); @@ -91,6 +105,7 @@ class TopLinksPlugin extends Plugin implements HookPluginInterface $tableReferences = [ 'toplinks_link' => $em->getClassMetadata(TopLink::class), + 'toplinks_link_rel_tool' => $em->getClassMetadata(TopLinkRelTool::class), ]; $schemaTool = new SchemaTool($em);