Skip to content

Commit

Permalink
TopLinks: Allow position them on top of tools - refs BT#18474
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelFQC committed Apr 7, 2021
1 parent 49e3bd0 commit fb11547
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 1 deletion.
34 changes: 34 additions & 0 deletions plugin/toplinks/index.php
@@ -1,2 +1,36 @@
<?php

/* For license terms, see /license.txt */

use Chamilo\PluginBundle\Entity\TopLinks\TopLinkRelTool;

$httpRequest = \Symfony\Component\HttpFoundation\Request::createFromGlobals();

if ('/main/course_home/course_home.php' === $httpRequest->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();
}
?>
<script>
$(function () {
var ids = JSON.parse('<?php echo json_encode($toolIds) ?>');

$(ids).each(function (index, id) {
var $toolA = $('#istooldesc_' + id).parents('.course-tool').parent();

$toolA.prependTo($toolA.parent());
});
});
</script>
<?php
}
29 changes: 29 additions & 0 deletions plugin/toplinks/src/Entity/Repository/TopLinkRelToolRepository.php
@@ -0,0 +1,29 @@
<?php

/* For license terms, see /license.txt */

namespace Chamilo\PluginBundle\Entity\TopLinks\Repository;

use Chamilo\CoreBundle\Entity\Course;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query\Expr\Join;

/**
* Class TopLinkRelToolRepository.
*
* @package Chamilo\PluginBundle\Entity\TopLinks\Repository
*/
class TopLinkRelToolRepository extends EntityRepository
{
public function findInCourse(Course $course)
{
$qb = $this->createQueryBuilder('tlrt');

return $qb
->innerJoin('tlrt.tool', 'tool', Join::WITH)
->where($qb->expr()->eq('tool.cId', ':course'))
->setParameter('course', $course)
->getQuery()
->getResult();
}
}
29 changes: 29 additions & 0 deletions plugin/toplinks/src/Entity/TopLink.php
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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);
}
}
102 changes: 102 additions & 0 deletions plugin/toplinks/src/Entity/TopLinkRelTool.php
@@ -0,0 +1,102 @@
<?php

/* For license terms, see /license.txt */

namespace Chamilo\PluginBundle\Entity\TopLinks;

use Chamilo\CourseBundle\Entity\CTool;
use Doctrine\ORM\Mapping as ORM;

/**
* Class TopLinkRelTool.
*
* @package Chamilo\PluginBundle\Entity\TopLinks
*
* @ORM\Table(name="toplinks_link_rel_tool")
* @ORM\Entity(repositoryClass="Chamilo\PluginBundle\Entity\TopLinks\Repository\TopLinkRelToolRepository")
*/
class TopLinkRelTool
{
/**
* @var int
*
* @ORM\Column(type="integer", name="id")
* @ORM\Id()
* @ORM\GeneratedValue()
*/
private $id;
/**
* @var \Chamilo\PluginBundle\Entity\TopLinks\TopLink
*
* @ORM\ManyToOne(targetEntity="Chamilo\PluginBundle\Entity\TopLinks\TopLink", inversedBy="tools")
* @ORM\JoinColumn(name="link_id", referencedColumnName="id")
*/
private $link;
/**
* @var \Chamilo\CourseBundle\Entity\CTool
*
* @ORM\OneToOne(targetEntity="Chamilo\CourseBundle\Entity\CTool")
* @ORM\JoinColumn(name="tool_id", referencedColumnName="iid")
*/
private $tool;

/**
* @return int
*/
public function getId(): int
{
return $this->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;
}
}
17 changes: 16 additions & 1 deletion plugin/toplinks/src/TopLinksPlugin.php
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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()
Expand All @@ -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));
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit fb11547

Please sign in to comment.