diff --git a/plugin/toplinks/admin.php b/plugin/toplinks/admin.php index 8eb95dbeef..10c57c29f5 100644 --- a/plugin/toplinks/admin.php +++ b/plugin/toplinks/admin.php @@ -2,6 +2,7 @@ /* For license terms, see /license.txt */ +use Chamilo\CoreBundle\Entity\Course as CourseEntity; use Chamilo\PluginBundle\Entity\TopLinks\TopLink; use Chamilo\PluginBundle\Entity\TopLinks\TopLinkRelTool; use Chamilo\PluginBundle\TopLinks\Form\LinkForm as TopLinkForm; @@ -75,16 +76,37 @@ switch ($httpRequest->query->getAlpha('action', 'list')) { ); $table->set_column_filter( 1, - function (int $id) use ($pageBaseUrl) { - return Display::url( - Display::return_icon('edit.png', get_lang('Edit')), - "$pageBaseUrl?".http_build_query(['action' => 'edit', 'link' => $id]) - ) - .PHP_EOL - .Display::url( - Display::return_icon('delete.png', get_lang('Delete')), - "$pageBaseUrl?".http_build_query(['action' => 'delete', 'link' => $id]) + function (int $id) use ($pageBaseUrl, $em, $plugin) { + $missingCourses = $em->getRepository(TopLinkRelTool::class)->getMissingCoursesForTool($id); + $countMissingCourses = count($missingCourses); + + $actions = []; + $actions[] = Display::url( + Display::return_icon('edit.png', get_lang('Edit')), + "$pageBaseUrl?".http_build_query(['action' => 'edit', 'link' => $id]) + ); + + if (count($missingCourses) > 0) { + $actions[] = Display::url( + Display::return_icon( + 'view_tree.png', + sprintf($plugin->get_lang('ReplicateInXMissingCourses'), $countMissingCourses) + ), + "$pageBaseUrl?".http_build_query(['action' => 'replicate', 'link' => $id]) ); + } else { + $actions[] = Display::return_icon( + 'view_tree_na.png', + $plugin->get_lang('AlreadyReplicatedInAllCourses') + ); + } + + $actions[] = Display::url( + Display::return_icon('delete.png', get_lang('Delete')), + "$pageBaseUrl?".http_build_query(['action' => 'delete', 'link' => $id]) + ); + + return implode(PHP_EOL, $actions); } ); @@ -201,6 +223,31 @@ switch ($httpRequest->query->getAlpha('action', 'list')) { Display::return_message(get_lang('LinkDeleted'), 'success') ); + header("Location: $pageBaseUrl"); + exit; + case 'replicate': + $link = $em->find(TopLink::class, $httpRequest->query->getInt('link')); + + if (null === $link) { + Display::addFlash( + Display::return_message(get_lang('NotFound'), 'error') + ); + + header("Location: $pageBaseUrl"); + exit; + } + + $missingCourses = $em->getRepository(TopLinkRelTool::class)->getMissingCoursesForTool($link->getId()); + + /** @var CourseEntity $missingCourse */ + foreach ($missingCourses as $missingCourse) { + $plugin->addToolInCourse($missingCourse->getId(), $link); + } + + Display::addFlash( + Display::return_message($plugin->get_lang('LinkReplicated'), 'success') + ); + header("Location: $pageBaseUrl"); exit; } diff --git a/plugin/toplinks/lang/english.php b/plugin/toplinks/lang/english.php index 6e6ef1f438..74df70ebde 100644 --- a/plugin/toplinks/lang/english.php +++ b/plugin/toplinks/lang/english.php @@ -4,3 +4,6 @@ $strings['plugin_title'] = 'Top Links'; $strings['plugin_comment'] = 'Create links in all courses.'; +$strings['ReplicateInXMissingCourses'] = 'Replicate in %d missing courses'; +$strings['AlreadyReplicatedInAllCourses'] = 'Already replicated in all courses'; +$strings['LinkReplicated'] = 'Link replicated in courses'; diff --git a/plugin/toplinks/src/Entity/Repository/TopLinkRelToolRepository.php b/plugin/toplinks/src/Entity/Repository/TopLinkRelToolRepository.php index ea439a5ecd..8019f1b295 100644 --- a/plugin/toplinks/src/Entity/Repository/TopLinkRelToolRepository.php +++ b/plugin/toplinks/src/Entity/Repository/TopLinkRelToolRepository.php @@ -7,6 +7,7 @@ namespace Chamilo\PluginBundle\Entity\TopLinks\Repository; use Chamilo\CoreBundle\Entity\Course; use Chamilo\CourseBundle\Entity\CTool; use Chamilo\PluginBundle\Entity\TopLinks\TopLink; +use Chamilo\PluginBundle\Entity\TopLinks\TopLinkRelTool; use Doctrine\ORM\EntityRepository; use Doctrine\ORM\Query\Expr\Join; @@ -54,4 +55,24 @@ class TopLinkRelToolRepository extends EntityRepository ->getQuery() ->execute(); } + + public function getMissingCoursesForTool(int $linkId) + { + $qb = $this->_em->createQueryBuilder(); + + $subQb = $this->_em->createQueryBuilder(); + $subQb + ->select('t.cId') + ->from(CTool::class, 't') + ->innerJoin(TopLinkRelTool::class, 'tlrt', Join::WITH, $subQb->expr()->eq('t.iid', 'tlrt.tool')) + ->where($subQb->expr()->eq('tlrt.link', ':link_id')); + + return $qb + ->select('c') + ->from(Course::class, 'c') + ->where($qb->expr()->notIn('c.id', $subQb->getDQL())) + ->setParameter('link_id', $linkId) + ->getQuery() + ->getResult(); + } }