diff --git a/plugin/toplinks/index.php b/plugin/toplinks/index.php index a4abe2dafcb..d9ee24753c0 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 3645fbc877c..1c02f49399d 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 @@ public function setIcon(string $icon): 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 00000000000..772b854c104 --- /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 dacea6b31b2..a412f732426 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 @@ public function getAdminUrl() 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 @@ public function install() $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 @@ public function uninstall() $tableReferences = [ 'toplinks_link' => $em->getClassMetadata(TopLink::class), + 'toplinks_link_rel_tool' => $em->getClassMetadata(TopLinkRelTool::class), ]; $schemaTool = new SchemaTool($em);