From f4e78c2015bef35775468a3a22493a146d67db01 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Thu, 18 Mar 2021 12:26:35 -0500 Subject: [PATCH] XAPI: Add repository for ToolLaunch entity - refs BT#18403 --- .../Repository/ToolLaunchRepository.php | 53 +++++++++++++++++++ plugin/xapi/src/Entity/ToolLaunch.php | 2 +- plugin/xapi/start.php | 19 +++---- 3 files changed, 60 insertions(+), 14 deletions(-) create mode 100644 plugin/xapi/src/Entity/Repository/ToolLaunchRepository.php diff --git a/plugin/xapi/src/Entity/Repository/ToolLaunchRepository.php b/plugin/xapi/src/Entity/Repository/ToolLaunchRepository.php new file mode 100644 index 0000000000..e2dfec20d1 --- /dev/null +++ b/plugin/xapi/src/Entity/Repository/ToolLaunchRepository.php @@ -0,0 +1,53 @@ + $course, + 'session' => null, + ]; + + if ($session) { + $criteria['session'] = $session; + } + + return $this->findBy($criteria, $orderBy, $limit, $start); + } + + public function countByCourseAndSession(Course $course, Session $session = null): int + { + $qb = $this->createQueryBuilder('tl'); + $qb->select($qb->expr()->count('tl')) + ->where($qb->expr()->eq('tl.course', ':course')) + ->setParameter('course', $course); + + if ($session) { + $qb->andWhere($qb->expr()->eq('tl.session', ':session')) + ->setParameter('session', $session); + } else { + $qb->andWhere($qb->expr()->isNull('tl.session')); + } + + return (int) $qb->getQuery()->getSingleScalarResult(); + } +} diff --git a/plugin/xapi/src/Entity/ToolLaunch.php b/plugin/xapi/src/Entity/ToolLaunch.php index e0d8f4ca22..91e2ec15aa 100644 --- a/plugin/xapi/src/Entity/ToolLaunch.php +++ b/plugin/xapi/src/Entity/ToolLaunch.php @@ -15,7 +15,7 @@ use Doctrine\ORM\Mapping as ORM; * @package Chamilo\PluginBundle\Entity\XApi * * @ORM\Table(name="xapi_tool_launch") - * @ORM\Entity() + * @ORM\Entity(repositoryClass="Chamilo\PluginBundle\Entity\XApi\Repository\ToolLaunchRepository") */ class ToolLaunch { diff --git a/plugin/xapi/start.php b/plugin/xapi/start.php index e30f6fd6d3..a2bcf5ffef 100644 --- a/plugin/xapi/start.php +++ b/plugin/xapi/start.php @@ -22,20 +22,13 @@ $cidReq = api_get_cidreq(); $table = new SortableTable( 'tbl_xapi', - function () use ($em, $course) { - return $em - ->createQuery('SELECT COUNT(tl) FROM ChamiloPluginBundle:XApi\ToolLaunch tl WHERE tl.course = :course') - ->setParameter('course', $course) - ->getSingleScalarResult(); + function () use ($em, $course, $session) { + return $em->getRepository(ToolLaunch::class) + ->countByCourseAndSession($course, $session); }, - function ($start, $limit, $orderBy, $orderDir) use ($em, $course, $isAllowedToEdit) { - $tools = $em->getRepository('ChamiloPluginBundle:XApi\ToolLaunch') - ->findBy( - ['course' => $course], - ['title' => $orderDir], - $limit, - $start - ); + function ($start, $limit, $orderBy, $orderDir) use ($em, $course, $session, $isAllowedToEdit) { + $tools = $em->getRepository(ToolLaunch::class) + ->findByCourseAndSession($course, $session, ['title' => $orderDir], $limit, $start); $data = [];